jtulach@297
|
1 |
package org.apidesign.aserverinfo.test;
|
jtulach@297
|
2 |
|
jtulach@340
|
3 |
import java.util.logging.Logger;
|
jtulach@340
|
4 |
import java.util.logging.Level;
|
jtulach@297
|
5 |
import java.net.MalformedURLException;
|
jtulach@297
|
6 |
import java.net.URL;
|
jtulach@297
|
7 |
import org.apidesign.aserverinfo.factories.ServerConnector;
|
jtulach@297
|
8 |
import org.apidesign.aserverinfo.spi.NameProvider;
|
jtulach@297
|
9 |
import org.apidesign.aserverinfo.spi.ResetHandler;
|
jtulach@297
|
10 |
import org.apidesign.aserverinfo.spi.ShutdownHandler;
|
jtulach@297
|
11 |
import org.apidesign.aserverinfo.spi.URLProvider;
|
jtulach@297
|
12 |
import org.junit.After;
|
jtulach@297
|
13 |
import org.junit.Before;
|
jtulach@297
|
14 |
import org.junit.Test;
|
jtulach@297
|
15 |
import static org.junit.Assert.*;
|
jtulach@297
|
16 |
|
jtulach@297
|
17 |
public class FactoriesTest {
|
jtulach@297
|
18 |
|
jtulach@297
|
19 |
public FactoriesTest() {
|
jtulach@297
|
20 |
}
|
jtulach@297
|
21 |
|
jtulach@297
|
22 |
@Before
|
jtulach@297
|
23 |
public void setUp() {
|
jtulach@297
|
24 |
}
|
jtulach@297
|
25 |
|
jtulach@297
|
26 |
@After
|
jtulach@297
|
27 |
public void tearDown() {
|
jtulach@297
|
28 |
}
|
jtulach@297
|
29 |
|
jtulach@297
|
30 |
@Test
|
jtulach@297
|
31 |
public void showUseOfFactoryVersion10() throws Exception {
|
jtulach@297
|
32 |
Prov p = new Prov();
|
jtulach@297
|
33 |
NameProvider np = p;
|
jtulach@297
|
34 |
URLProvider up = p;
|
jtulach@297
|
35 |
ResetHandler res = p;
|
jtulach@297
|
36 |
ServerConnector inf;
|
jtulach@297
|
37 |
|
jtulach@297
|
38 |
// BEGIN: ServerConnector.factory.creation
|
jtulach@297
|
39 |
inf = ServerConnector.create(np, up, res);
|
jtulach@297
|
40 |
// END: ServerConnector.factory.creation
|
jtulach@297
|
41 |
|
jtulach@297
|
42 |
assertEquals("API Design Server", inf.getName());
|
jtulach@297
|
43 |
assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
|
jtulach@297
|
44 |
inf.reset();
|
jtulach@297
|
45 |
assertEquals("Once reset", 1, p.resets);
|
jtulach@297
|
46 |
|
jtulach@297
|
47 |
}
|
jtulach@297
|
48 |
@Test
|
jtulach@297
|
49 |
public void showUseOfFactoryVersion20() throws Exception {
|
jtulach@297
|
50 |
Prov p = new Prov();
|
jtulach@297
|
51 |
NameProvider np = p;
|
jtulach@297
|
52 |
URLProvider up = p;
|
jtulach@297
|
53 |
ResetHandler res = p;
|
jtulach@297
|
54 |
ShutdownHandler shutdown = new ShutdownHandler() {
|
jtulach@297
|
55 |
public void shutdown() {
|
jtulach@297
|
56 |
// OK
|
jtulach@297
|
57 |
}
|
jtulach@297
|
58 |
};
|
jtulach@297
|
59 |
ServerConnector inf;
|
jtulach@297
|
60 |
|
jtulach@298
|
61 |
// BEGIN: ServerConnector.factory.creation2
|
jtulach@297
|
62 |
inf = ServerConnector.create(np, up, res, shutdown);
|
jtulach@298
|
63 |
// END: ServerConnector.factory.creation2
|
jtulach@297
|
64 |
|
jtulach@297
|
65 |
assertEquals("API Design Server", inf.getName());
|
jtulach@297
|
66 |
assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
|
jtulach@297
|
67 |
inf.reset();
|
jtulach@297
|
68 |
assertEquals("Once reset", 1, p.resets);
|
jtulach@297
|
69 |
|
jtulach@297
|
70 |
}
|
jtulach@297
|
71 |
|
jtulach@297
|
72 |
private static class Prov implements NameProvider, URLProvider, ResetHandler {
|
jtulach@297
|
73 |
int resets;
|
jtulach@297
|
74 |
|
jtulach@297
|
75 |
public String getName() {
|
jtulach@297
|
76 |
return "API Design Server";
|
jtulach@297
|
77 |
}
|
jtulach@297
|
78 |
|
jtulach@297
|
79 |
public URL getURL() {
|
jtulach@297
|
80 |
try {
|
jtulach@297
|
81 |
return new URL("http://www.apidesign.org");
|
jtulach@297
|
82 |
} catch (MalformedURLException ex) {
|
jtulach@340
|
83 |
Logger.getLogger(FactoriesTest.class.getName()).log(Level.SEVERE, null, ex);
|
jtulach@297
|
84 |
return null;
|
jtulach@297
|
85 |
}
|
jtulach@297
|
86 |
}
|
jtulach@297
|
87 |
|
jtulach@297
|
88 |
public void reset() {
|
jtulach@297
|
89 |
resets++;
|
jtulach@297
|
90 |
}
|
jtulach@297
|
91 |
|
jtulach@297
|
92 |
}
|
jtulach@297
|
93 |
|
jtulach@297
|
94 |
} |