1 package be
.nikiroo
.utils
.test
;
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
;
14 class 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
);
22 series
.addTest(new TestCase("Simple connection " + ssls
) {
24 public void test() throws Exception
{
25 final String
[] rec
= new String
[1];
27 ServerString server
= new ServerString(this.getName(), 0, ssl
) {
29 protected String
onRequest(
30 ConnectActionServerString action
,
31 Version clientVersion
, String data
)
37 protected void onError(Exception e
) {
41 int port
= server
.getPort();
42 assertEquals("A port should have been assigned", true, port
> 0);
46 ServerBridge br
= null;
48 br
= new ServerBridge(0, ssl
, "", port
, ssl
);
49 br
.setTraceHandler(null);
53 "A port should have been assigned to the bridge",
61 new ConnectActionClientObject(null, port
, ssl
) {
63 public void action(Version serverVersion
)
77 assertNotNull("The client action was not run", rec
[0]);
78 assertEquals("ok", rec
[0]);
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];
88 public void test() throws Exception
{
89 ServerString server
= new ServerString(this.getName(), 0, ssl
) {
91 protected String
onRequest(
92 ConnectActionServerString action
,
93 Version clientVersion
, String data
)
100 protected void onError(Exception e
) {
105 int port
= server
.getPort();
109 ServerBridge br
= null;
111 br
= new ServerBridge(0, ssl
, "", port
, ssl
);
112 br
.setTraceHandler(null);
119 new ConnectActionClientString(null, port
, ssl
) {
121 public void action(Version serverVersion
)
123 recd
[0] = send("ping");
135 if (err
[0] != null) {
136 fail("An exception was thrown: " + err
[0].getMessage());
139 assertEquals("ping", sent
[0]);
140 assertEquals("pong", recd
[0]);
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];
150 public void test() throws Exception
{
151 ServerString server
= new ServerString(this.getName(), 0, ssl
) {
153 protected String
onRequest(
154 ConnectActionServerString action
,
155 Version clientVersion
, String data
)
159 sent
[1] = action
.rec();
164 protected void onError(Exception e
) {
169 int port
= server
.getPort();
173 ServerBridge br
= null;
175 br
= new ServerBridge(0, ssl
, "", port
, ssl
);
176 br
.setTraceHandler(null);
183 new ConnectActionClientString(null, port
, ssl
) {
185 public void action(Version serverVersion
)
187 recd
[0] = send("ping");
188 recd
[1] = send("ping2");
200 if (err
[0] != null) {
201 fail("An exception was thrown: " + err
[0].getMessage());
204 assertEquals("ping", sent
[0]);
205 assertEquals("pong", recd
[0]);
206 assertEquals("ping2", sent
[1]);
207 assertEquals("pong2", recd
[1]);
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];
217 public void test() throws Exception
{
218 ServerString server
= new ServerString(this.getName(), 0, ssl
) {
220 protected String
onRequest(
221 ConnectActionServerString action
,
222 Version clientVersion
, String data
)
224 sent
[Integer
.parseInt(data
)] = data
;
225 return "" + (Integer
.parseInt(data
) * 2);
229 protected void onError(Exception e
) {
234 int port
= server
.getPort();
238 ServerBridge br
= null;
240 br
= new ServerBridge(0, ssl
, "", port
, ssl
);
241 br
.setTraceHandler(null);
248 new ConnectActionClientString(null, port
, ssl
) {
250 public void action(Version serverVersion
)
252 for (int i
= 0; i
< 3; i
++) {
253 recd
[i
] = send("" + i
);
266 if (err
[0] != null) {
267 fail("An exception was thrown: " + err
[0].getMessage());
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]);
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
);
289 series
.addTest(new TestCase("Simple connection " + ssls
) {
291 public void test() throws Exception
{
292 final Object
[] rec
= new Object
[1];
294 ServerObject server
= new ServerObject(this.getName(), 0, ssl
) {
296 protected Object
onRequest(
297 ConnectActionServerObject action
,
298 Version clientVersion
, Object data
)
304 protected void onError(Exception e
) {
308 int port
= server
.getPort();
309 assertEquals("A port should have been assigned", true, port
> 0);
313 ServerBridge br
= null;
315 br
= new ServerBridge(0, ssl
, "", port
, ssl
);
316 br
.setTraceHandler(null);
323 new ConnectActionClientObject(null, port
, ssl
) {
325 public void action(Version serverVersion
)
331 protected void onError(Exception e
) {
343 assertNotNull("The client action was not run", rec
[0]);
344 assertEquals(true, (boolean) ((Boolean
) rec
[0]));
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];
354 public void test() throws Exception
{
355 ServerObject server
= new ServerObject(this.getName(), 0, ssl
) {
357 protected Object
onRequest(
358 ConnectActionServerObject action
,
359 Version clientVersion
, Object data
)
366 protected void onError(Exception e
) {
371 int port
= server
.getPort();
375 ServerBridge br
= null;
377 br
= new ServerBridge(0, ssl
, "", port
, ssl
);
378 br
.setTraceHandler(null);
385 new ConnectActionClientObject(null, port
, ssl
) {
387 public void action(Version serverVersion
)
389 recd
[0] = send("ping");
401 if (err
[0] != null) {
402 fail("An exception was thrown: " + err
[0].getMessage());
405 assertEquals("ping", sent
[0]);
406 assertEquals("pong", recd
[0]);
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];
416 public void test() throws Exception
{
417 ServerObject server
= new ServerObject(this.getName(), 0, ssl
) {
419 protected Object
onRequest(
420 ConnectActionServerObject action
,
421 Version clientVersion
, Object data
)
425 sent
[1] = action
.rec();
430 protected void onError(Exception e
) {
435 int port
= server
.getPort();
439 ServerBridge br
= null;
441 br
= new ServerBridge(0, ssl
, "", port
, ssl
);
442 br
.setTraceHandler(null);
449 new ConnectActionClientObject(null, port
, ssl
) {
451 public void action(Version serverVersion
)
453 recd
[0] = send("ping");
454 recd
[1] = send("ping2");
466 if (err
[0] != null) {
467 fail("An exception was thrown: " + err
[0].getMessage());
470 assertEquals("ping", sent
[0]);
471 assertEquals("pong", recd
[0]);
472 assertEquals("ping2", sent
[1]);
473 assertEquals("pong2", recd
[1]);
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];
483 public void test() throws Exception
{
484 ServerObject server
= new ServerObject(this.getName(), 0, ssl
) {
486 protected Object
onRequest(
487 ConnectActionServerObject action
,
488 Version clientVersion
, Object data
)
491 return new Object
[] { "ACK" };
495 protected void onError(Exception e
) {
500 int port
= server
.getPort();
504 ServerBridge br
= null;
506 br
= new ServerBridge(0, ssl
, "", port
, ssl
);
507 br
.setTraceHandler(null);
514 new ConnectActionClientObject(null, port
, ssl
) {
516 public void action(Version serverVersion
)
518 recd
[0] = send(new Object
[] {
521 "https://example.com/from_client"),
522 "https://example.com/from_client" });
534 if (err
[0] != null) {
535 fail("An exception was thrown: " + err
[0].getMessage());
538 Object
[] sento
= (Object
[]) (sent
[0]);
539 Object
[] recdo
= (Object
[]) (recd
[0]);
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]);
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];
555 public void test() throws Exception
{
556 ServerObject server
= new ServerObject(this.getName(), 0, ssl
) {
558 protected Object
onRequest(
559 ConnectActionServerObject action
,
560 Version clientVersion
, Object data
)
562 sent
[(Integer
) data
] = data
;
563 return ((Integer
) data
) * 2;
567 protected void onError(Exception e
) {
572 int port
= server
.getPort();
576 ServerBridge br
= null;
578 br
= new ServerBridge(0, ssl
, "", port
, ssl
);
579 br
.setTraceHandler(null);
586 new ConnectActionClientObject(null, port
, ssl
) {
588 public void action(Version serverVersion
)
590 for (int i
= 0; i
< 3; i
++) {
604 if (err
[0] != null) {
605 fail("An exception was thrown: " + err
[0].getMessage());
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]);
620 public SerialServerTest(String
[] args
) {
621 super("SerialServer test", args
);
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
));