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