Commit | Line | Data |
---|---|---|
db31c358 NR |
1 | package be.nikiroo.utils.test; |
2 | ||
f157aed8 | 3 | import be.nikiroo.utils.TraceHandler; |
ce0974c4 | 4 | import be.nikiroo.utils.Version; |
08a58812 | 5 | import be.nikiroo.utils.serial.ConnectActionClient; |
ce0974c4 | 6 | import be.nikiroo.utils.serial.ConnectActionServer; |
db31c358 NR |
7 | import be.nikiroo.utils.serial.Exporter; |
8 | import be.nikiroo.utils.serial.Importer; | |
ce0974c4 | 9 | import be.nikiroo.utils.serial.Server; |
db31c358 NR |
10 | |
11 | class SerialTest extends TestLauncher { | |
12 | private SerialTest() { | |
13 | super("Serial test", null); | |
14 | } | |
15 | ||
f157aed8 NR |
16 | private TestLauncher createServerTestCases(final String[] args, |
17 | final boolean ssl) { | |
08a58812 | 18 | final String ssls = (ssl ? "(ssl)" : "(plain text)"); |
f157aed8 NR |
19 | TestLauncher series = new TestLauncher("Client/Server " + ssls, args); |
20 | ||
21 | series.addTest(new TestCase("Simple connection " + ssls) { | |
22 | @Override | |
23 | public void test() throws Exception { | |
24 | final Object[] rec = new Object[1]; | |
25 | ||
26 | Server server = new Server(this.getName(), 0, ssl) { | |
08a58812 | 27 | @Override |
f157aed8 NR |
28 | protected Object onRequest(ConnectActionServer action, |
29 | Version clientVersion, Object data) | |
30 | throws Exception { | |
31 | return null; | |
32 | } | |
33 | }; | |
34 | ||
35 | assertEquals("A port should have been assigned", true, | |
36 | server.getPort() > 0); | |
37 | ||
38 | // TODO: remove | |
39 | server.setTraceHandler(new TraceHandler(true, true, true)); | |
40 | ||
d463d266 | 41 | server.start(); |
f157aed8 NR |
42 | |
43 | try { | |
44 | new ConnectActionClient(null, server.getPort(), ssl) { | |
45 | @Override | |
46 | public void action(Version serverVersion) | |
47 | throws Exception { | |
48 | rec[0] = true; | |
08a58812 | 49 | } |
f157aed8 NR |
50 | }.connect(); |
51 | } finally { | |
52 | server.stop(); | |
53 | } | |
54 | ||
55 | assertNotNull("The client action was not run", rec[0]); | |
56 | assertEquals(true, (boolean) ((Boolean) rec[0])); | |
57 | } | |
58 | }); | |
08a58812 | 59 | |
f157aed8 NR |
60 | series.addTest(new TestCase("Simple exchange " + ssls) { |
61 | final Object[] sent = new Object[1]; | |
62 | final Object[] recd = new Object[1]; | |
63 | final Exception[] err = new Exception[1]; | |
64 | ||
65 | @Override | |
66 | public void test() throws Exception { | |
67 | Server server = new Server(this.getName(), 0, ssl) { | |
68 | @Override | |
69 | protected Object onRequest(ConnectActionServer action, | |
70 | Version clientVersion, Object data) | |
71 | throws Exception { | |
72 | sent[0] = data; | |
73 | return "pong"; | |
08a58812 | 74 | } |
08a58812 NR |
75 | |
76 | @Override | |
f157aed8 NR |
77 | protected void onError(Exception e) { |
78 | super.onError(e); | |
79 | err[0] = e; | |
80 | } | |
81 | }; | |
08a58812 | 82 | |
d463d266 | 83 | server.start(); |
08a58812 | 84 | |
f157aed8 NR |
85 | try { |
86 | new ConnectActionClient(null, server.getPort(), ssl) { | |
87 | @Override | |
88 | public void action(Version serverVersion) | |
89 | throws Exception { | |
90 | recd[0] = send("ping"); | |
08a58812 | 91 | } |
f157aed8 NR |
92 | }.connect(); |
93 | } finally { | |
94 | server.stop(); | |
95 | } | |
08a58812 | 96 | |
f157aed8 NR |
97 | if (err[0] != null) { |
98 | fail("An exception was thrown: " + err[0].getMessage()); | |
99 | } | |
100 | ||
101 | assertEquals("ping", sent[0]); | |
102 | assertEquals("pong", recd[0]); | |
103 | } | |
104 | }); | |
105 | ||
106 | series.addTest(new TestCase("Multiple exchanges " + ssls) { | |
107 | final Object[] sent = new Object[3]; | |
108 | final Object[] recd = new Object[3]; | |
109 | final Exception[] err = new Exception[1]; | |
110 | ||
111 | @Override | |
112 | public void test() throws Exception { | |
113 | Server server = new Server(this.getName(), 0, ssl) { | |
114 | @Override | |
115 | protected Object onRequest(ConnectActionServer action, | |
116 | Version clientVersion, Object data) | |
117 | throws Exception { | |
118 | sent[0] = data; | |
119 | action.send("pong"); | |
d463d266 | 120 | sent[1] = action.rec(); |
f157aed8 | 121 | return "pong2"; |
08a58812 | 122 | } |
08a58812 NR |
123 | |
124 | @Override | |
f157aed8 NR |
125 | protected void onError(Exception e) { |
126 | super.onError(e); | |
127 | err[0] = e; | |
128 | } | |
129 | }; | |
08a58812 | 130 | |
d463d266 | 131 | server.start(); |
08a58812 | 132 | |
f157aed8 NR |
133 | try { |
134 | new ConnectActionClient(null, server.getPort(), ssl) { | |
135 | @Override | |
136 | public void action(Version serverVersion) | |
137 | throws Exception { | |
138 | recd[0] = send("ping"); | |
139 | recd[1] = send("ping2"); | |
08a58812 | 140 | } |
f157aed8 NR |
141 | }.connect(); |
142 | } finally { | |
143 | server.stop(); | |
144 | } | |
145 | ||
146 | if (err[0] != null) { | |
147 | fail("An exception was thrown: " + err[0].getMessage()); | |
148 | } | |
149 | ||
150 | assertEquals("ping", sent[0]); | |
151 | assertEquals("pong", recd[0]); | |
152 | assertEquals("ping2", sent[1]); | |
153 | assertEquals("pong2", recd[1]); | |
154 | } | |
155 | }); | |
08a58812 | 156 | |
f157aed8 NR |
157 | series.addTest(new TestCase("Multiple call from client " + ssls) { |
158 | final Object[] sent = new Object[3]; | |
159 | final Object[] recd = new Object[3]; | |
160 | final Exception[] err = new Exception[1]; | |
161 | ||
162 | @Override | |
163 | public void test() throws Exception { | |
164 | Server server = new Server(this.getName(), 0, ssl) { | |
165 | @Override | |
166 | protected Object onRequest(ConnectActionServer action, | |
167 | Version clientVersion, Object data) | |
168 | throws Exception { | |
169 | sent[(Integer) data] = data; | |
170 | return ((Integer) data) * 2; | |
08a58812 | 171 | } |
08a58812 NR |
172 | |
173 | @Override | |
f157aed8 NR |
174 | protected void onError(Exception e) { |
175 | super.onError(e); | |
176 | err[0] = e; | |
177 | } | |
178 | }; | |
179 | ||
d463d266 | 180 | server.start(); |
08a58812 | 181 | |
f157aed8 NR |
182 | try { |
183 | new ConnectActionClient(null, server.getPort(), ssl) { | |
184 | @Override | |
185 | public void action(Version serverVersion) | |
186 | throws Exception { | |
187 | for (int i = 0; i < 3; i++) { | |
188 | recd[i] = send(i); | |
08a58812 | 189 | } |
08a58812 | 190 | } |
f157aed8 NR |
191 | }.connect(); |
192 | } finally { | |
193 | server.stop(); | |
194 | } | |
08a58812 | 195 | |
f157aed8 NR |
196 | if (err[0] != null) { |
197 | fail("An exception was thrown: " + err[0].getMessage()); | |
198 | } | |
08a58812 | 199 | |
f157aed8 NR |
200 | assertEquals(0, sent[0]); |
201 | assertEquals(0, recd[0]); | |
202 | assertEquals(1, sent[1]); | |
203 | assertEquals(2, recd[1]); | |
204 | assertEquals(2, sent[2]); | |
205 | assertEquals(4, recd[2]); | |
206 | } | |
207 | }); | |
208 | ||
209 | return series; | |
08a58812 NR |
210 | } |
211 | ||
db31c358 NR |
212 | public SerialTest(String[] args) { |
213 | super("Serial test", args); | |
214 | ||
f157aed8 | 215 | addSeries(createServerTestCases(args, false)); |
08a58812 | 216 | |
f157aed8 | 217 | addSeries(createServerTestCases(args, true)); |
08a58812 | 218 | |
db31c358 NR |
219 | addTest(new TestCase("Simple class Import/Export") { |
220 | @Override | |
221 | public void test() throws Exception { | |
222 | Data data = new Data(42); | |
223 | String encoded = new Exporter().append(data).toString(false); | |
224 | Object redata = new Importer().read(encoded).getValue(); | |
225 | String reencoded = new Exporter().append(redata) | |
226 | .toString(false); | |
227 | ||
228 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), | |
229 | reencoded.replaceAll("@[0-9]*", "@REF")); | |
230 | } | |
231 | }); | |
232 | ||
233 | addTest(new TestCase("Import/Export with nested objects") { | |
234 | @Override | |
235 | public void test() throws Exception { | |
236 | Data data = new DataObject(new Data(21)); | |
237 | String encoded = new Exporter().append(data).toString(false); | |
238 | Object redata = new Importer().read(encoded).getValue(); | |
239 | String reencoded = new Exporter().append(redata) | |
240 | .toString(false); | |
241 | ||
242 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), | |
243 | reencoded.replaceAll("@[0-9]*", "@REF")); | |
244 | } | |
245 | }); | |
246 | ||
247 | addTest(new TestCase("Import/Export with nested objects forming a loop") { | |
248 | @Override | |
249 | public void test() throws Exception { | |
250 | DataLoop data = new DataLoop("looping"); | |
251 | data.next = new DataLoop("level 2"); | |
252 | data.next.next = data; | |
253 | ||
254 | String encoded = new Exporter().append(data).toString(false); | |
255 | Object redata = new Importer().read(encoded).getValue(); | |
256 | String reencoded = new Exporter().append(redata) | |
257 | .toString(false); | |
258 | ||
259 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), | |
260 | reencoded.replaceAll("@[0-9]*", "@REF")); | |
261 | } | |
262 | }); | |
ce0974c4 NR |
263 | |
264 | addTest(new TestCase("Array in Object Import/Export") { | |
265 | @Override | |
266 | public void test() throws Exception { | |
267 | Object data = new DataArray();// new String[] { "un", "deux" }; | |
268 | String encoded = new Exporter().append(data).toString(false); | |
269 | Object redata = new Importer().read(encoded).getValue(); | |
270 | String reencoded = new Exporter().append(redata) | |
271 | .toString(false); | |
272 | ||
273 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), | |
274 | reencoded.replaceAll("@[0-9]*", "@REF")); | |
275 | } | |
276 | }); | |
277 | ||
278 | addTest(new TestCase("Array Import/Export") { | |
279 | @Override | |
280 | public void test() throws Exception { | |
281 | Object data = new String[] { "un", "deux" }; | |
282 | String encoded = new Exporter().append(data).toString(false); | |
283 | Object redata = new Importer().read(encoded).getValue(); | |
284 | String reencoded = new Exporter().append(redata) | |
285 | .toString(false); | |
286 | ||
287 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), | |
288 | reencoded.replaceAll("@[0-9]*", "@REF")); | |
289 | } | |
290 | }); | |
e570f7eb NR |
291 | |
292 | addTest(new TestCase("Enum Import/Export") { | |
293 | @Override | |
294 | public void test() throws Exception { | |
295 | Object data = EnumToSend.FANFAN; | |
296 | String encoded = new Exporter().append(data).toString(false); | |
297 | Object redata = new Importer().read(encoded).getValue(); | |
298 | String reencoded = new Exporter().append(redata) | |
299 | .toString(false); | |
300 | ||
301 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), | |
302 | reencoded.replaceAll("@[0-9]*", "@REF")); | |
303 | } | |
304 | }); | |
ce0974c4 NR |
305 | } |
306 | ||
307 | class DataArray { | |
308 | public String[] data = new String[] { "un", "deux" }; | |
db31c358 NR |
309 | } |
310 | ||
311 | @SuppressWarnings("unused") | |
312 | class Data { | |
313 | private int value; | |
314 | ||
315 | private Data() { | |
316 | } | |
317 | ||
318 | public Data(int value) { | |
319 | this.value = value; | |
320 | } | |
321 | } | |
322 | ||
323 | @SuppressWarnings("unused") | |
324 | class DataObject extends Data { | |
325 | private Data data; | |
326 | ||
327 | @SuppressWarnings("synthetic-access") | |
328 | private DataObject() { | |
329 | } | |
330 | ||
331 | @SuppressWarnings("synthetic-access") | |
332 | public DataObject(Data data) { | |
333 | this.data = data; | |
334 | } | |
335 | } | |
336 | ||
337 | @SuppressWarnings("unused") | |
338 | class DataLoop extends Data { | |
339 | public DataLoop next; | |
340 | private String value; | |
341 | ||
342 | @SuppressWarnings("synthetic-access") | |
343 | private DataLoop() { | |
344 | } | |
345 | ||
346 | @SuppressWarnings("synthetic-access") | |
347 | public DataLoop(String value) { | |
348 | this.value = value; | |
349 | } | |
350 | } | |
e570f7eb NR |
351 | |
352 | enum EnumToSend { | |
353 | FANFAN, TULIPE, | |
354 | } | |
db31c358 | 355 | } |