Auto cache cleaning + better err in ImageUtilsAnd
[fanfix.git] / src / be / nikiroo / utils / test / SerialServerTest.java
... / ...
CommitLineData
1package be.nikiroo.utils.test;
2
3import java.net.URL;
4
5import be.nikiroo.utils.Version;
6import be.nikiroo.utils.serial.server.ConnectActionClientObject;
7import be.nikiroo.utils.serial.server.ConnectActionClientString;
8import be.nikiroo.utils.serial.server.ConnectActionServerObject;
9import be.nikiroo.utils.serial.server.ConnectActionServerString;
10import be.nikiroo.utils.serial.server.ServerBridge;
11import be.nikiroo.utils.serial.server.ServerObject;
12import be.nikiroo.utils.serial.server.ServerString;
13
14class SerialServerTest extends TestLauncher {
15 private TestLauncher createServerStringTestCases(final String[] args,
16 final boolean ssl, final boolean bridge) {
17 final String ssls = (ssl ? "(ssl)" : "(plain text)");
18 final String bridges = (bridge ? " with bridge" : "");
19 TestLauncher series = new TestLauncher(
20 "ServerString " + ssls + bridges, args);
21
22 series.addTest(new TestCase("Simple connection " + ssls) {
23 @Override
24 public void test() throws Exception {
25 final String[] rec = new String[1];
26
27 ServerString server = new ServerString(this.getName(), 0, ssl) {
28 @Override
29 protected String onRequest(
30 ConnectActionServerString action,
31 Version clientVersion, String data)
32 throws Exception {
33 return null;
34 }
35
36 @Override
37 protected void onError(Exception e) {
38 }
39 };
40
41 int port = server.getPort();
42 assertEquals("A port should have been assigned", true, port > 0);
43
44 server.start();
45
46 ServerBridge br = null;
47 if (bridge) {
48 br = new ServerBridge(0, ssl, "", port, ssl);
49 br.setTraceHandler(null);
50
51 port = br.getPort();
52 assertEquals(
53 "A port should have been assigned to the bridge",
54 true, port > 0);
55
56 br.start();
57 }
58
59 try {
60 try {
61 new ConnectActionClientObject(null, port, ssl) {
62 @Override
63 public void action(Version serverVersion)
64 throws Exception {
65 rec[0] = "ok";
66 }
67 }.connect();
68 } finally {
69 server.stop();
70 }
71 } finally {
72 if (br != null) {
73 br.stop();
74 }
75 }
76
77 assertNotNull("The client action was not run", rec[0]);
78 assertEquals("ok", rec[0]);
79 }
80 });
81
82 series.addTest(new TestCase("Simple exchange " + ssls) {
83 final String[] sent = new String[1];
84 final String[] recd = new String[1];
85 final Exception[] err = new Exception[1];
86
87 @Override
88 public void test() throws Exception {
89 ServerString server = new ServerString(this.getName(), 0, ssl) {
90 @Override
91 protected String onRequest(
92 ConnectActionServerString action,
93 Version clientVersion, String data)
94 throws Exception {
95 sent[0] = data;
96 return "pong";
97 }
98
99 @Override
100 protected void onError(Exception e) {
101 err[0] = e;
102 }
103 };
104
105 int port = server.getPort();
106
107 server.start();
108
109 ServerBridge br = null;
110 if (bridge) {
111 br = new ServerBridge(0, ssl, "", port, ssl);
112 br.setTraceHandler(null);
113 port = br.getPort();
114 br.start();
115 }
116
117 try {
118 try {
119 new ConnectActionClientString(null, port, ssl) {
120 @Override
121 public void action(Version serverVersion)
122 throws Exception {
123 recd[0] = send("ping");
124 }
125 }.connect();
126 } finally {
127 server.stop();
128 }
129 } finally {
130 if (br != null) {
131 br.stop();
132 }
133 }
134
135 if (err[0] != null) {
136 fail("An exception was thrown: " + err[0].getMessage());
137 }
138
139 assertEquals("ping", sent[0]);
140 assertEquals("pong", recd[0]);
141 }
142 });
143
144 series.addTest(new TestCase("Multiple exchanges " + ssls) {
145 final String[] sent = new String[3];
146 final String[] recd = new String[3];
147 final Exception[] err = new Exception[1];
148
149 @Override
150 public void test() throws Exception {
151 ServerString server = new ServerString(this.getName(), 0, ssl) {
152 @Override
153 protected String onRequest(
154 ConnectActionServerString action,
155 Version clientVersion, String data)
156 throws Exception {
157 sent[0] = data;
158 action.send("pong");
159 sent[1] = action.rec();
160 return "pong2";
161 }
162
163 @Override
164 protected void onError(Exception e) {
165 err[0] = e;
166 }
167 };
168
169 int port = server.getPort();
170
171 server.start();
172
173 ServerBridge br = null;
174 if (bridge) {
175 br = new ServerBridge(0, ssl, "", port, ssl);
176 br.setTraceHandler(null);
177 port = br.getPort();
178 br.start();
179 }
180
181 try {
182 try {
183 new ConnectActionClientString(null, port, ssl) {
184 @Override
185 public void action(Version serverVersion)
186 throws Exception {
187 recd[0] = send("ping");
188 recd[1] = send("ping2");
189 }
190 }.connect();
191 } finally {
192 server.stop();
193 }
194 } finally {
195 if (br != null) {
196 br.stop();
197 }
198 }
199
200 if (err[0] != null) {
201 fail("An exception was thrown: " + err[0].getMessage());
202 }
203
204 assertEquals("ping", sent[0]);
205 assertEquals("pong", recd[0]);
206 assertEquals("ping2", sent[1]);
207 assertEquals("pong2", recd[1]);
208 }
209 });
210
211 series.addTest(new TestCase("Multiple call from client " + ssls) {
212 final String[] sent = new String[3];
213 final String[] recd = new String[3];
214 final Exception[] err = new Exception[1];
215
216 @Override
217 public void test() throws Exception {
218 ServerString server = new ServerString(this.getName(), 0, ssl) {
219 @Override
220 protected String onRequest(
221 ConnectActionServerString action,
222 Version clientVersion, String data)
223 throws Exception {
224 sent[Integer.parseInt(data)] = data;
225 return "" + (Integer.parseInt(data) * 2);
226 }
227
228 @Override
229 protected void onError(Exception e) {
230 err[0] = e;
231 }
232 };
233
234 int port = server.getPort();
235
236 server.start();
237
238 ServerBridge br = null;
239 if (bridge) {
240 br = new ServerBridge(0, ssl, "", port, ssl);
241 br.setTraceHandler(null);
242 port = br.getPort();
243 br.start();
244 }
245
246 try {
247 try {
248 new ConnectActionClientString(null, port, ssl) {
249 @Override
250 public void action(Version serverVersion)
251 throws Exception {
252 for (int i = 0; i < 3; i++) {
253 recd[i] = send("" + i);
254 }
255 }
256 }.connect();
257 } finally {
258 server.stop();
259 }
260 } finally {
261 if (br != null) {
262 br.stop();
263 }
264 }
265
266 if (err[0] != null) {
267 fail("An exception was thrown: " + err[0].getMessage());
268 }
269
270 assertEquals("0", sent[0]);
271 assertEquals("0", recd[0]);
272 assertEquals("1", sent[1]);
273 assertEquals("2", recd[1]);
274 assertEquals("2", sent[2]);
275 assertEquals("4", recd[2]);
276 }
277 });
278
279 return series;
280 }
281
282 private TestLauncher createServerObjectTestCases(final String[] args,
283 final boolean ssl, final boolean bridge) {
284 final String ssls = (ssl ? "(ssl)" : "(plain text)");
285 final String bridges = (bridge ? " with bridge" : "");
286 TestLauncher series = new TestLauncher(
287 "ServerObject " + ssls + bridges, args);
288
289 series.addTest(new TestCase("Simple connection " + ssls) {
290 @Override
291 public void test() throws Exception {
292 final Object[] rec = new Object[1];
293
294 ServerObject server = new ServerObject(this.getName(), 0, ssl) {
295 @Override
296 protected Object onRequest(
297 ConnectActionServerObject action,
298 Version clientVersion, Object data)
299 throws Exception {
300 return null;
301 }
302
303 @Override
304 protected void onError(Exception e) {
305 }
306 };
307
308 int port = server.getPort();
309 assertEquals("A port should have been assigned", true, port > 0);
310
311 server.start();
312
313 ServerBridge br = null;
314 if (bridge) {
315 br = new ServerBridge(0, ssl, "", port, ssl);
316 br.setTraceHandler(null);
317 port = br.getPort();
318 br.start();
319 }
320
321 try {
322 try {
323 new ConnectActionClientObject(null, port, ssl) {
324 @Override
325 public void action(Version serverVersion)
326 throws Exception {
327 rec[0] = true;
328 }
329
330 @Override
331 protected void onError(Exception e) {
332 }
333 }.connect();
334 } finally {
335 server.stop();
336 }
337 } finally {
338 if (br != null) {
339 br.stop();
340 }
341 }
342
343 assertNotNull("The client action was not run", rec[0]);
344 assertEquals(true, (boolean) ((Boolean) rec[0]));
345 }
346 });
347
348 series.addTest(new TestCase("Simple exchange " + ssls) {
349 final Object[] sent = new Object[1];
350 final Object[] recd = new Object[1];
351 final Exception[] err = new Exception[1];
352
353 @Override
354 public void test() throws Exception {
355 ServerObject server = new ServerObject(this.getName(), 0, ssl) {
356 @Override
357 protected Object onRequest(
358 ConnectActionServerObject action,
359 Version clientVersion, Object data)
360 throws Exception {
361 sent[0] = data;
362 return "pong";
363 }
364
365 @Override
366 protected void onError(Exception e) {
367 err[0] = e;
368 }
369 };
370
371 int port = server.getPort();
372
373 server.start();
374
375 ServerBridge br = null;
376 if (bridge) {
377 br = new ServerBridge(0, ssl, "", port, ssl);
378 br.setTraceHandler(null);
379 port = br.getPort();
380 br.start();
381 }
382
383 try {
384 try {
385 new ConnectActionClientObject(null, port, ssl) {
386 @Override
387 public void action(Version serverVersion)
388 throws Exception {
389 recd[0] = send("ping");
390 }
391 }.connect();
392 } finally {
393 server.stop();
394 }
395 } finally {
396 if (br != null) {
397 br.stop();
398 }
399 }
400
401 if (err[0] != null) {
402 fail("An exception was thrown: " + err[0].getMessage());
403 }
404
405 assertEquals("ping", sent[0]);
406 assertEquals("pong", recd[0]);
407 }
408 });
409
410 series.addTest(new TestCase("Multiple exchanges " + ssls) {
411 final Object[] sent = new Object[3];
412 final Object[] recd = new Object[3];
413 final Exception[] err = new Exception[1];
414
415 @Override
416 public void test() throws Exception {
417 ServerObject server = new ServerObject(this.getName(), 0, ssl) {
418 @Override
419 protected Object onRequest(
420 ConnectActionServerObject action,
421 Version clientVersion, Object data)
422 throws Exception {
423 sent[0] = data;
424 action.send("pong");
425 sent[1] = action.rec();
426 return "pong2";
427 }
428
429 @Override
430 protected void onError(Exception e) {
431 err[0] = e;
432 }
433 };
434
435 int port = server.getPort();
436
437 server.start();
438
439 ServerBridge br = null;
440 if (bridge) {
441 br = new ServerBridge(0, ssl, "", port, ssl);
442 br.setTraceHandler(null);
443 port = br.getPort();
444 br.start();
445 }
446
447 try {
448 try {
449 new ConnectActionClientObject(null, port, ssl) {
450 @Override
451 public void action(Version serverVersion)
452 throws Exception {
453 recd[0] = send("ping");
454 recd[1] = send("ping2");
455 }
456 }.connect();
457 } finally {
458 server.stop();
459 }
460 } finally {
461 if (br != null) {
462 br.stop();
463 }
464 }
465
466 if (err[0] != null) {
467 fail("An exception was thrown: " + err[0].getMessage());
468 }
469
470 assertEquals("ping", sent[0]);
471 assertEquals("pong", recd[0]);
472 assertEquals("ping2", sent[1]);
473 assertEquals("pong2", recd[1]);
474 }
475 });
476
477 series.addTest(new TestCase("Object array of URLs " + ssls) {
478 final Object[] sent = new Object[1];
479 final Object[] recd = new Object[1];
480 final Exception[] err = new Exception[1];
481
482 @Override
483 public void test() throws Exception {
484 ServerObject server = new ServerObject(this.getName(), 0, ssl) {
485 @Override
486 protected Object onRequest(
487 ConnectActionServerObject action,
488 Version clientVersion, Object data)
489 throws Exception {
490 sent[0] = data;
491 return new Object[] { "ACK" };
492 }
493
494 @Override
495 protected void onError(Exception e) {
496 err[0] = e;
497 }
498 };
499
500 int port = server.getPort();
501
502 server.start();
503
504 ServerBridge br = null;
505 if (bridge) {
506 br = new ServerBridge(0, ssl, "", port, ssl);
507 br.setTraceHandler(null);
508 port = br.getPort();
509 br.start();
510 }
511
512 try {
513 try {
514 new ConnectActionClientObject(null, port, ssl) {
515 @Override
516 public void action(Version serverVersion)
517 throws Exception {
518 recd[0] = send(new Object[] {
519 "key",
520 new URL(
521 "https://example.com/from_client"),
522 "https://example.com/from_client" });
523 }
524 }.connect();
525 } finally {
526 server.stop();
527 }
528 } finally {
529 if (br != null) {
530 br.stop();
531 }
532 }
533
534 if (err[0] != null) {
535 fail("An exception was thrown: " + err[0].getMessage());
536 }
537
538 Object[] sento = (Object[]) (sent[0]);
539 Object[] recdo = (Object[]) (recd[0]);
540
541 assertEquals("key", sento[0]);
542 assertEquals("https://example.com/from_client",
543 ((URL) sento[1]).toString());
544 assertEquals("https://example.com/from_client", sento[2]);
545 assertEquals("ACK", recdo[0]);
546 }
547 });
548
549 series.addTest(new TestCase("Multiple call from client " + ssls) {
550 final Object[] sent = new Object[3];
551 final Object[] recd = new Object[3];
552 final Exception[] err = new Exception[1];
553
554 @Override
555 public void test() throws Exception {
556 ServerObject server = new ServerObject(this.getName(), 0, ssl) {
557 @Override
558 protected Object onRequest(
559 ConnectActionServerObject action,
560 Version clientVersion, Object data)
561 throws Exception {
562 sent[(Integer) data] = data;
563 return ((Integer) data) * 2;
564 }
565
566 @Override
567 protected void onError(Exception e) {
568 err[0] = e;
569 }
570 };
571
572 int port = server.getPort();
573
574 server.start();
575
576 ServerBridge br = null;
577 if (bridge) {
578 br = new ServerBridge(0, ssl, "", port, ssl);
579 br.setTraceHandler(null);
580 port = br.getPort();
581 br.start();
582 }
583
584 try {
585 try {
586 new ConnectActionClientObject(null, port, ssl) {
587 @Override
588 public void action(Version serverVersion)
589 throws Exception {
590 for (int i = 0; i < 3; i++) {
591 recd[i] = send(i);
592 }
593 }
594 }.connect();
595 } finally {
596 server.stop();
597 }
598 } finally {
599 if (br != null) {
600 br.stop();
601 }
602 }
603
604 if (err[0] != null) {
605 fail("An exception was thrown: " + err[0].getMessage());
606 }
607
608 assertEquals(0, sent[0]);
609 assertEquals(0, recd[0]);
610 assertEquals(1, sent[1]);
611 assertEquals(2, recd[1]);
612 assertEquals(2, sent[2]);
613 assertEquals(4, recd[2]);
614 }
615 });
616
617 return series;
618 }
619
620 public SerialServerTest(String[] args) {
621 super("SerialServer test", args);
622
623 for (boolean ssl : new Boolean[] { false, true }) {
624 for (boolean bridge : new Boolean[] { false, true }) {
625 addSeries(createServerObjectTestCases(args, ssl, bridge));
626 addSeries(createServerStringTestCases(args, ssl, bridge));
627 }
628 }
629 }
630}