jtulach@295
|
1 |
package org.apidesign.aserverinfo.test;
|
jtulach@295
|
2 |
|
jtulach@295
|
3 |
import java.net.MalformedURLException;
|
jtulach@295
|
4 |
import java.net.URL;
|
jtulach@340
|
5 |
import java.util.logging.Level;
|
jtulach@340
|
6 |
import java.util.logging.Logger;
|
jtulach@295
|
7 |
import org.apidesign.aserverinfo.magicalbagfactory.ServerConnector;
|
jtulach@295
|
8 |
import org.apidesign.aserverinfo.spi.NameProvider;
|
jtulach@295
|
9 |
import org.apidesign.aserverinfo.spi.ResetHandler;
|
jtulach@295
|
10 |
import org.apidesign.aserverinfo.spi.URLProvider;
|
jtulach@295
|
11 |
import org.junit.After;
|
jtulach@295
|
12 |
import org.junit.Before;
|
jtulach@295
|
13 |
import org.junit.Test;
|
jtulach@295
|
14 |
import static org.junit.Assert.*;
|
jtulach@295
|
15 |
import org.openide.util.Lookup;
|
jtulach@295
|
16 |
import org.openide.util.lookup.AbstractLookup;
|
jtulach@295
|
17 |
import org.openide.util.lookup.InstanceContent;
|
jtulach@295
|
18 |
|
jtulach@295
|
19 |
public class MagicalBagFactoryTest {
|
jtulach@295
|
20 |
|
jtulach@295
|
21 |
public MagicalBagFactoryTest() {
|
jtulach@295
|
22 |
}
|
jtulach@295
|
23 |
|
jtulach@295
|
24 |
@Before
|
jtulach@295
|
25 |
public void setUp() {
|
jtulach@295
|
26 |
}
|
jtulach@295
|
27 |
|
jtulach@295
|
28 |
@After
|
jtulach@295
|
29 |
public void tearDown() {
|
jtulach@295
|
30 |
}
|
jtulach@295
|
31 |
|
jtulach@295
|
32 |
@Test
|
jtulach@295
|
33 |
public void showUseOfMagicalBagFactory() throws Exception {
|
jtulach@295
|
34 |
Prov provider = new Prov();
|
jtulach@295
|
35 |
ServerConnector inf;
|
jtulach@295
|
36 |
|
jtulach@295
|
37 |
// BEGIN: ServerConnector.magicalbag.creation
|
jtulach@295
|
38 |
InstanceContent magicalContent = new InstanceContent();
|
jtulach@295
|
39 |
Lookup magicalBag = new AbstractLookup(magicalContent);
|
jtulach@295
|
40 |
magicalContent.add(provider);
|
jtulach@295
|
41 |
|
jtulach@295
|
42 |
inf = ServerConnector.create(magicalBag);
|
jtulach@295
|
43 |
// END: ServerConnector.magicalbag.creation
|
jtulach@295
|
44 |
|
jtulach@295
|
45 |
assertEquals("API Design Server", inf.getName());
|
jtulach@295
|
46 |
assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
|
jtulach@295
|
47 |
inf.reset();
|
jtulach@295
|
48 |
assertEquals("Once reset", 1, provider.resets);
|
jtulach@295
|
49 |
|
jtulach@295
|
50 |
provider.resets = 0;
|
jtulach@295
|
51 |
// BEGIN: ServerConnector.magicalbag.change
|
jtulach@295
|
52 |
magicalContent.remove(provider);
|
jtulach@295
|
53 |
inf.reset();
|
jtulach@295
|
54 |
assertEquals("No reset called now", 0, provider.resets);
|
jtulach@295
|
55 |
// END: ServerConnector.magicalbag.change
|
jtulach@295
|
56 |
}
|
jtulach@295
|
57 |
|
jtulach@295
|
58 |
private static class Prov implements NameProvider, URLProvider, ResetHandler {
|
jtulach@295
|
59 |
int resets;
|
jtulach@295
|
60 |
|
jtulach@295
|
61 |
public String getName() {
|
jtulach@295
|
62 |
return "API Design Server";
|
jtulach@295
|
63 |
}
|
jtulach@295
|
64 |
|
jtulach@295
|
65 |
public URL getURL() {
|
jtulach@295
|
66 |
try {
|
jtulach@295
|
67 |
return new URL("http://www.apidesign.org");
|
jtulach@295
|
68 |
} catch (MalformedURLException ex) {
|
jtulach@340
|
69 |
Logger.getLogger(MagicalBagFactoryTest.class.getName()).log(Level.SEVERE, null, ex);
|
jtulach@295
|
70 |
return null;
|
jtulach@295
|
71 |
}
|
jtulach@295
|
72 |
}
|
jtulach@295
|
73 |
|
jtulach@295
|
74 |
public void reset() {
|
jtulach@295
|
75 |
resets++;
|
jtulach@295
|
76 |
}
|
jtulach@295
|
77 |
|
jtulach@295
|
78 |
}
|
jtulach@295
|
79 |
|
jtulach@295
|
80 |
} |