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