1 package be
.nikiroo
.utils
.test_code
;
5 import be
.nikiroo
.utils
.Version
;
6 import be
.nikiroo
.utils
.serial
.server
.ConnectActionClientObject
;
7 import be
.nikiroo
.utils
.serial
.server
.ConnectActionClientString
;
8 import be
.nikiroo
.utils
.serial
.server
.ConnectActionServerObject
;
9 import be
.nikiroo
.utils
.serial
.server
.ConnectActionServerString
;
10 import be
.nikiroo
.utils
.serial
.server
.ServerBridge
;
11 import be
.nikiroo
.utils
.serial
.server
.ServerObject
;
12 import be
.nikiroo
.utils
.serial
.server
.ServerString
;
13 import be
.nikiroo
.utils
.test
.TestCase
;
14 import be
.nikiroo
.utils
.test
.TestLauncher
;
16 class SerialServerTest
extends TestLauncher
{
17 public SerialServerTest(String
[] args
) {
18 super("SerialServer test", args
);
20 for (String key
: new String
[] { null,
21 "some super secret encryption key" }) {
22 for (boolean bridge
: new Boolean
[] { false, true }) {
23 final String skey
= (key
!= null ?
"(encrypted)"
25 final String sbridge
= (bridge ?
" with bridge" : "");
27 addSeries(new SerialServerTest(args
, key
, skey
, bridge
,
28 sbridge
, "ServerString"));
30 addSeries(new SerialServerTest(args
, key
, skey
, bridge
,
31 sbridge
, new Object() {
33 public String
toString() {
34 return "ServerObject";
41 private SerialServerTest(final String
[] args
, final String key
,
42 final String skey
, final boolean bridge
, final String sbridge
,
45 super(title
+ " " + skey
+ sbridge
, args
);
47 addTest(new TestCase("Simple connection " + skey
) {
49 public void test() throws Exception
{
50 final String
[] rec
= new String
[1];
52 ServerString server
= new ServerString(this.getName(), 0, key
) {
54 protected String
onRequest(
55 ConnectActionServerString action
, Version version
,
56 String data
, long id
) throws Exception
{
61 protected void onError(Exception e
) {
65 int port
= server
.getPort();
66 assertEquals("A port should have been assigned", true, port
> 0);
70 ServerBridge br
= null;
72 br
= new ServerBridge(0, key
, "", port
, key
);
73 br
.setTraceHandler(null);
77 "A port should have been assigned to the bridge",
85 new ConnectActionClientString(null, port
, key
) {
87 public void action(Version version
)
101 assertNotNull("The client action was not run", rec
[0]);
102 assertEquals("ok", rec
[0]);
106 addTest(new TestCase("Simple exchange " + skey
) {
107 final String
[] sent
= new String
[1];
108 final String
[] recd
= new String
[1];
109 final Exception
[] err
= new Exception
[1];
112 public void test() throws Exception
{
113 ServerString server
= new ServerString(this.getName(), 0, key
) {
115 protected String
onRequest(
116 ConnectActionServerString action
, Version version
,
117 String data
, long id
) throws Exception
{
123 protected void onError(Exception e
) {
128 int port
= server
.getPort();
132 ServerBridge br
= null;
134 br
= new ServerBridge(0, key
, "", port
, key
);
135 br
.setTraceHandler(null);
142 new ConnectActionClientString(null, port
, key
) {
144 public void action(Version version
)
146 recd
[0] = send("ping");
158 if (err
[0] != null) {
159 fail("An exception was thrown: " + err
[0].getMessage(),
163 assertEquals("ping", sent
[0]);
164 assertEquals("pong", recd
[0]);
168 addTest(new TestCase("Multiple exchanges " + skey
) {
169 final String
[] sent
= new String
[3];
170 final String
[] recd
= new String
[3];
171 final Exception
[] err
= new Exception
[1];
174 public void test() throws Exception
{
175 ServerString server
= new ServerString(this.getName(), 0, key
) {
177 protected String
onRequest(
178 ConnectActionServerString action
, Version version
,
179 String data
, long id
) throws Exception
{
182 sent
[1] = action
.rec();
187 protected void onError(Exception e
) {
192 int port
= server
.getPort();
196 ServerBridge br
= null;
198 br
= new ServerBridge(0, key
, "", port
, key
);
199 br
.setTraceHandler(null);
206 new ConnectActionClientString(null, port
, key
) {
208 public void action(Version version
)
210 recd
[0] = send("ping");
211 recd
[1] = send("ping2");
223 if (err
[0] != null) {
224 fail("An exception was thrown: " + err
[0].getMessage(),
228 assertEquals("ping", sent
[0]);
229 assertEquals("pong", recd
[0]);
230 assertEquals("ping2", sent
[1]);
231 assertEquals("pong2", recd
[1]);
235 addTest(new TestCase("Multiple call from client " + skey
) {
236 final String
[] sent
= new String
[3];
237 final String
[] recd
= new String
[3];
238 final Exception
[] err
= new Exception
[1];
241 public void test() throws Exception
{
242 ServerString server
= new ServerString(this.getName(), 0, key
) {
244 protected String
onRequest(
245 ConnectActionServerString action
, Version version
,
246 String data
, long id
) throws Exception
{
247 sent
[Integer
.parseInt(data
)] = data
;
248 return "" + (Integer
.parseInt(data
) * 2);
252 protected void onError(Exception e
) {
257 int port
= server
.getPort();
261 ServerBridge br
= null;
263 br
= new ServerBridge(0, key
, "", port
, key
);
264 br
.setTraceHandler(null);
271 new ConnectActionClientString(null, port
, key
) {
273 public void action(Version version
)
275 for (int i
= 0; i
< 3; i
++) {
276 recd
[i
] = send("" + i
);
289 if (err
[0] != null) {
290 fail("An exception was thrown: " + err
[0].getMessage(),
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]);
304 private SerialServerTest(final String
[] args
, final String key
,
305 final String skey
, final boolean bridge
, final String sbridge
,
306 final Object title
) {
308 super(title
+ " " + skey
+ sbridge
, args
);
310 addTest(new TestCase("Simple connection " + skey
) {
312 public void test() throws Exception
{
313 final Object
[] rec
= new Object
[1];
315 ServerObject server
= new ServerObject(this.getName(), 0, key
) {
317 protected Object
onRequest(
318 ConnectActionServerObject action
, Version version
,
319 Object data
, long id
) throws Exception
{
324 protected void onError(Exception e
) {
328 int port
= server
.getPort();
329 assertEquals("A port should have been assigned", true, port
> 0);
333 ServerBridge br
= null;
335 br
= new ServerBridge(0, key
, "", port
, key
);
336 br
.setTraceHandler(null);
343 new ConnectActionClientObject(null, port
, key
) {
345 public void action(Version version
)
351 protected void onError(Exception e
) {
363 assertNotNull("The client action was not run", rec
[0]);
364 assertEquals(true, (boolean) ((Boolean
) rec
[0]));
368 addTest(new TestCase("Simple exchange " + skey
) {
369 final Object
[] sent
= new Object
[1];
370 final Object
[] recd
= new Object
[1];
371 final Exception
[] err
= new Exception
[1];
374 public void test() throws Exception
{
375 ServerObject server
= new ServerObject(this.getName(), 0, key
) {
377 protected Object
onRequest(
378 ConnectActionServerObject action
, Version version
,
379 Object data
, long id
) throws Exception
{
385 protected void onError(Exception e
) {
390 int port
= server
.getPort();
394 ServerBridge br
= null;
396 br
= new ServerBridge(0, key
, "", port
, key
);
397 br
.setTraceHandler(null);
404 new ConnectActionClientObject(null, port
, key
) {
406 public void action(Version version
)
408 recd
[0] = send("ping");
420 if (err
[0] != null) {
421 fail("An exception was thrown: " + err
[0].getMessage(),
425 assertEquals("ping", sent
[0]);
426 assertEquals("pong", recd
[0]);
430 addTest(new TestCase("Multiple exchanges " + skey
) {
431 final Object
[] sent
= new Object
[3];
432 final Object
[] recd
= new Object
[3];
433 final Exception
[] err
= new Exception
[1];
436 public void test() throws Exception
{
437 ServerObject server
= new ServerObject(this.getName(), 0, key
) {
439 protected Object
onRequest(
440 ConnectActionServerObject action
, Version version
,
441 Object data
, long id
) throws Exception
{
444 sent
[1] = action
.rec();
449 protected void onError(Exception e
) {
454 int port
= server
.getPort();
458 ServerBridge br
= null;
460 br
= new ServerBridge(0, key
, "", port
, key
);
461 br
.setTraceHandler(null);
468 new ConnectActionClientObject(null, port
, key
) {
470 public void action(Version version
)
472 recd
[0] = send("ping");
473 recd
[1] = send("ping2");
485 if (err
[0] != null) {
486 fail("An exception was thrown: " + err
[0].getMessage(),
490 assertEquals("ping", sent
[0]);
491 assertEquals("pong", recd
[0]);
492 assertEquals("ping2", sent
[1]);
493 assertEquals("pong2", recd
[1]);
497 addTest(new TestCase("Object array of URLs " + skey
) {
498 final Object
[] sent
= new Object
[1];
499 final Object
[] recd
= new Object
[1];
500 final Exception
[] err
= new Exception
[1];
503 public void test() throws Exception
{
504 ServerObject server
= new ServerObject(this.getName(), 0, key
) {
506 protected Object
onRequest(
507 ConnectActionServerObject action
, Version version
,
508 Object data
, long id
) throws Exception
{
510 return new Object
[] { "ACK" };
514 protected void onError(Exception e
) {
519 int port
= server
.getPort();
523 ServerBridge br
= null;
525 br
= new ServerBridge(0, key
, "", port
, key
);
526 br
.setTraceHandler(null);
533 new ConnectActionClientObject(null, port
, key
) {
535 public void action(Version version
)
537 recd
[0] = send(new Object
[] {
540 "https://example.com/from_client"),
541 "https://example.com/from_client" });
553 if (err
[0] != null) {
554 fail("An exception was thrown: " + err
[0].getMessage(),
558 Object
[] sento
= (Object
[]) (sent
[0]);
559 Object
[] recdo
= (Object
[]) (recd
[0]);
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]);
569 addTest(new TestCase("Multiple call from client " + skey
) {
570 final Object
[] sent
= new Object
[3];
571 final Object
[] recd
= new Object
[3];
572 final Exception
[] err
= new Exception
[1];
575 public void test() throws Exception
{
576 ServerObject server
= new ServerObject(this.getName(), 0, key
) {
578 protected Object
onRequest(
579 ConnectActionServerObject action
, Version version
,
580 Object data
, long id
) throws Exception
{
581 sent
[(Integer
) data
] = data
;
582 return ((Integer
) data
) * 2;
586 protected void onError(Exception e
) {
591 int port
= server
.getPort();
595 ServerBridge br
= null;
597 br
= new ServerBridge(0, key
, "", port
, key
);
598 br
.setTraceHandler(null);
605 new ConnectActionClientObject(null, port
, key
) {
607 public void action(Version version
)
609 for (int i
= 0; i
< 3; i
++) {
623 if (err
[0] != null) {
624 fail("An exception was thrown: " + err
[0].getMessage(),
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]);