javacommons/comms/tsrc/javaapi/javasrc/com/nokia/mj/test/comms/TestCommsEndpoint.java
changeset 21 2a9601315dfc
child 87 1627c337e51e
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.mj.test.comms;
       
    20 
       
    21 import j2meunit.framework.Test;
       
    22 import j2meunit.framework.TestCase;
       
    23 import j2meunit.framework.TestSuite;
       
    24 import j2meunit.framework.TestMethod;
       
    25 
       
    26 import com.nokia.mj.impl.comms.CommsMessage;
       
    27 import com.nokia.mj.impl.comms.CommsEndpoint;
       
    28 import com.nokia.mj.impl.comms.CommsListener;
       
    29 import com.nokia.mj.impl.comms.exception.CommsException;
       
    30 
       
    31 import java.io.IOException;
       
    32 
       
    33 public class TestCommsEndpoint extends TestCase
       
    34 {
       
    35     private final int MODULE_ID_A           = 900;
       
    36     private final int MODULE_ID_B           = 901;
       
    37     private final int MODULE_ID_DATA_CHECK  = 902;
       
    38     private final int MODULE_ID_NO_REPLY    = 903;      // no reply is sent
       
    39     private final int MODULE_ID_SLEEP_1S    = 1000;     // reply msg is delayed by 1s
       
    40     private final int MODULE_ID_SLEEP_5S    = 5000;
       
    41     private final int MODULE_ID_SLEEP_10S   = 10000;
       
    42 
       
    43     private final int PLUGIN_ID_JAVACAPTAIN_COMMS_TESTER_C = 101; // see comms.h
       
    44 
       
    45     private final int SERVER_ADDRESS = CommsEndpoint.JAVA_CAPTAIN;
       
    46     private final String SERVER_NAME = "InstallerJavaCaptain";
       
    47 
       
    48     private final int TIMEOUT = 10;
       
    49     public TestCommsEndpoint()
       
    50     {
       
    51     }
       
    52 
       
    53     public TestCommsEndpoint(String sTestName, TestMethod rTestMethod)
       
    54     {
       
    55         super(sTestName, rTestMethod);
       
    56     }
       
    57 
       
    58     public Test suite()
       
    59     {
       
    60         TestSuite aSuite = new TestSuite();
       
    61 
       
    62         aSuite.addTest(new TestCommsEndpoint("testLoadCommsExtensionPlugin", new TestMethod()
       
    63         {
       
    64             public void run(TestCase tc)
       
    65             {
       
    66                 ((TestCommsEndpoint) tc).testLoadCommsExtensionPlugin();
       
    67             }
       
    68         }));
       
    69 
       
    70         aSuite.addTest(new TestCommsEndpoint("testConstructor", new TestMethod()
       
    71         {
       
    72             public void run(TestCase tc)
       
    73             {
       
    74                 ((TestCommsEndpoint) tc).testConstructor();
       
    75             }
       
    76         }));
       
    77 
       
    78         aSuite.addTest(new TestCommsEndpoint("testFind", new TestMethod()
       
    79         {
       
    80             public void run(TestCase tc)
       
    81             {
       
    82                 ((TestCommsEndpoint) tc).testFind();
       
    83             }
       
    84         }));
       
    85 
       
    86         aSuite.addTest(new TestCommsEndpoint("testRegisterListener", new TestMethod()
       
    87         {
       
    88             public void run(TestCase tc)
       
    89             {
       
    90                 ((TestCommsEndpoint) tc).testRegisterListener();
       
    91             }
       
    92         }));
       
    93 
       
    94         aSuite.addTest(new TestCommsEndpoint("testNoUnregisterListener", new TestMethod()
       
    95         {
       
    96             public void run(TestCase tc)
       
    97             {
       
    98                 ((TestCommsEndpoint) tc).testNoUnregisterListener();
       
    99             }
       
   100         }));
       
   101 
       
   102         aSuite.addTest(new TestCommsEndpoint("testRegisterListenerInListener", new TestMethod()
       
   103         {
       
   104             public void run(TestCase tc)
       
   105             {
       
   106                 ((TestCommsEndpoint) tc).testRegisterListenerInListener();
       
   107             }
       
   108         }));
       
   109 
       
   110         aSuite.addTest(new TestCommsEndpoint("testSend", new TestMethod()
       
   111         {
       
   112             public void run(TestCase tc)
       
   113             {
       
   114                 ((TestCommsEndpoint) tc).testSend();
       
   115             }
       
   116         }));
       
   117 
       
   118         aSuite.addTest(new TestCommsEndpoint("testSendReceive", new TestMethod()
       
   119         {
       
   120             public void run(TestCase tc)
       
   121             {
       
   122                 ((TestCommsEndpoint) tc).testSendReceive();
       
   123             }
       
   124         }));
       
   125 
       
   126         aSuite.addTest(new TestCommsEndpoint("testSendReceiveWithWaitForEver", new TestMethod()
       
   127         {
       
   128             public void run(TestCase tc)
       
   129             {
       
   130                 ((TestCommsEndpoint) tc).testSendReceiveWithWaitForEver();
       
   131             }
       
   132         }));
       
   133 
       
   134         aSuite.addTest(new TestCommsEndpoint("testSendReceiveBigTimeout", new TestMethod()
       
   135         {
       
   136             public void run(TestCase tc)
       
   137             {
       
   138                 ((TestCommsEndpoint) tc).testSendReceiveBigTimeout();
       
   139             }
       
   140         }));
       
   141 
       
   142         aSuite.addTest(new TestCommsEndpoint("testSendReceiveTimeout", new TestMethod()
       
   143         {
       
   144             public void run(TestCase tc)
       
   145             {
       
   146                 ((TestCommsEndpoint) tc).testSendReceiveTimeout();
       
   147             }
       
   148         }));
       
   149 
       
   150         aSuite.addTest(new TestCommsEndpoint("testSendReceiveMultiThread", new TestMethod()
       
   151         {
       
   152             public void run(TestCase tc)
       
   153             {
       
   154                 ((TestCommsEndpoint) tc).testSendReceiveMultiThread();
       
   155             }
       
   156         }));
       
   157 
       
   158         aSuite.addTest(new TestCommsEndpoint("testExitWhileSendReceivePending", new TestMethod()
       
   159         {
       
   160             public void run(TestCase tc)
       
   161             {
       
   162                 ((TestCommsEndpoint) tc).testExitWhileSendReceivePending();
       
   163             }
       
   164         }));
       
   165 
       
   166         aSuite.addTest(new TestCommsEndpoint("testSendAndSendReceiveMultiThread", new TestMethod()
       
   167         {
       
   168             public void run(TestCase tc)
       
   169             {
       
   170                 ((TestCommsEndpoint) tc).testSendAndSendReceiveMultiThread();
       
   171             }
       
   172         }));
       
   173 
       
   174         aSuite.addTest(new TestCommsEndpoint("testSendWhenNotConnected", new TestMethod()
       
   175         {
       
   176             public void run(TestCase tc)
       
   177             {
       
   178                 ((TestCommsEndpoint) tc).testSendWhenNotConnected();
       
   179             }
       
   180         }));
       
   181 
       
   182         aSuite.addTest(new TestCommsEndpoint("testCheckMessageInListener", new TestMethod()
       
   183         {
       
   184             public void run(TestCase tc)
       
   185             {
       
   186                 ((TestCommsEndpoint) tc).testCheckMessageInListener();
       
   187             }
       
   188         }));
       
   189 
       
   190         aSuite.addTest(new TestCommsEndpoint("testCheckNativePayload", new TestMethod()
       
   191         {
       
   192             public void run(TestCase tc)
       
   193             {
       
   194                 ((TestCommsEndpoint) tc).testCheckNativePayload();
       
   195             }
       
   196         }));
       
   197 
       
   198         aSuite.addTest(new TestCommsEndpoint("testDestroy", new TestMethod()
       
   199         {
       
   200             public void run(TestCase tc)
       
   201             {
       
   202                 ((TestCommsEndpoint) tc).testDestroy();
       
   203             }
       
   204         }));
       
   205 
       
   206         return aSuite;
       
   207     }
       
   208 
       
   209     public void assertEquals(byte[] a, byte[] b)
       
   210     {
       
   211         assertEquals(a.length, b.length);
       
   212         for (int i = 0; i < a.length; i++)
       
   213         {
       
   214             assertEquals(a[i], b[i]);
       
   215         }
       
   216     }
       
   217 
       
   218     public void testLoadCommsExtensionPlugin()
       
   219     {
       
   220         System.out.println("TestCommsEndpoint.testLoadCommsExtensionPlugin()");
       
   221         CommsEndpoint comms = null;
       
   222         try
       
   223         {
       
   224             comms = new CommsEndpoint();
       
   225             comms.connect(SERVER_ADDRESS);
       
   226             // loads comms extension plugin in JavaCaptain
       
   227             CommsMessage message = new CommsMessage();
       
   228             message.setModuleId(PLUGIN_ID_JAVACAPTAIN_COMMS_TESTER_C);
       
   229             CommsMessage msg = comms.sendReceive(message, TIMEOUT);
       
   230         }
       
   231         catch (Exception e)
       
   232         {
       
   233             String error = "\n\nError !!!!!!!!!!!!!!!!!!!!!!\n\n"+
       
   234                            "Loading comms extension plugin failed.\n"+
       
   235                            "Check that comms extension plugin is built (make).\n"+
       
   236                            "This failure causes other tests to fail also.\n\n";
       
   237             System.out.println(error);
       
   238             fail(error);
       
   239         }
       
   240         finally
       
   241         {
       
   242             comms.destroy();
       
   243         }
       
   244     }
       
   245 
       
   246     public void testConstructor()
       
   247     {
       
   248         System.out.println("TestCommsEndpoint.testConstructor()");
       
   249 
       
   250         CommsEndpoint comms = null;
       
   251         // ok case
       
   252         try
       
   253         {
       
   254             comms = new CommsEndpoint();
       
   255             comms.connect(SERVER_ADDRESS);
       
   256             comms.disconnect();
       
   257         }
       
   258         catch (CommsException e)
       
   259         {
       
   260             fail("ctor(SERVER_NAME) failed");
       
   261         }
       
   262         finally
       
   263         {
       
   264             comms.destroy();
       
   265             comms = null;
       
   266         }
       
   267 
       
   268         // multiple endpoints
       
   269         CommsEndpoint comms1 = null;
       
   270         CommsEndpoint comms2 = null;
       
   271         CommsEndpoint comms3 = null;
       
   272         try
       
   273         {
       
   274             comms1 = new CommsEndpoint();
       
   275             comms2 = new CommsEndpoint();
       
   276             comms3 = new CommsEndpoint();
       
   277             comms1.connect(SERVER_ADDRESS);
       
   278             comms2.connect(SERVER_ADDRESS);
       
   279             comms3.connect(SERVER_ADDRESS);
       
   280         }
       
   281         catch (CommsException e)
       
   282         {
       
   283             fail(" multiple endpoints failed");
       
   284         }
       
   285         finally
       
   286         {
       
   287             comms1.destroy();
       
   288             comms2.destroy();
       
   289             comms3.destroy();
       
   290         }
       
   291 
       
   292         // multiple connect/disconnect calls
       
   293         try
       
   294         {
       
   295             comms = new CommsEndpoint();
       
   296             comms.disconnect();
       
   297 
       
   298             comms.connect(SERVER_ADDRESS);
       
   299             comms.connect(SERVER_ADDRESS);
       
   300 
       
   301             comms.disconnect();
       
   302             comms.disconnect();
       
   303         }
       
   304         catch (CommsException e)
       
   305         {
       
   306             fail("multiple start/stop calls failed");
       
   307         }
       
   308         finally
       
   309         {
       
   310             comms.destroy();
       
   311             comms = null;
       
   312         }
       
   313 
       
   314         // connect fails
       
   315         try
       
   316         {
       
   317             comms = new CommsEndpoint();
       
   318             comms.connect(666313);
       
   319             fail("connect to non-existing server did not fail");
       
   320         }
       
   321         catch (CommsException e)
       
   322         {
       
   323         }
       
   324         finally
       
   325         {
       
   326             comms.destroy();
       
   327             comms = null;
       
   328         }
       
   329     }
       
   330 
       
   331     public void testFind()
       
   332     {
       
   333         System.out.println("TestCommsEndpoint.testFind()");
       
   334 
       
   335         CommsEndpoint comms = null;
       
   336 
       
   337         // ok case
       
   338         try
       
   339         {
       
   340             comms = CommsEndpoint.find(SERVER_NAME);
       
   341             assertNotNull(comms);
       
   342             comms.send(new CommsMessage());
       
   343         }
       
   344         catch (CommsException e)
       
   345         {
       
   346             fail("ok case");
       
   347         }
       
   348         finally
       
   349         {
       
   350             comms.destroy();
       
   351             comms = null;
       
   352         }
       
   353 
       
   354         // not existing
       
   355         comms = CommsEndpoint.find("not existing");
       
   356         assertEquals(comms, null);
       
   357         comms = null;
       
   358 
       
   359         // multiple endpoints
       
   360         CommsEndpoint comms1 = null;
       
   361         CommsEndpoint comms2 = null;
       
   362         CommsEndpoint comms3 = null;
       
   363         try
       
   364         {
       
   365             comms1 = CommsEndpoint.find(SERVER_NAME);
       
   366             comms2 = CommsEndpoint.find(SERVER_NAME);
       
   367             comms3 = CommsEndpoint.find(SERVER_NAME);
       
   368 
       
   369             assertNotNull(comms1);
       
   370             assertNotNull(comms2);
       
   371             assertNotNull(comms3);
       
   372 
       
   373             comms1.send(new CommsMessage());
       
   374             comms2.send(new CommsMessage());
       
   375             comms3.send(new CommsMessage());
       
   376         }
       
   377         catch (CommsException e)
       
   378         {
       
   379             fail(" multiple endpoints failed");
       
   380         }
       
   381         finally
       
   382         {
       
   383             comms1.destroy();
       
   384             comms2.destroy();
       
   385             comms3.destroy();
       
   386         }
       
   387     }
       
   388 
       
   389     private void testRegisterListener()
       
   390     {
       
   391         System.out.println("TestCommsEndpoint.testRegisterListener()");
       
   392 
       
   393         class TestListener implements CommsListener
       
   394         {
       
   395             public void processMessage(CommsMessage message) {}
       
   396         }
       
   397 
       
   398         CommsEndpoint comms = null;
       
   399         try
       
   400         {
       
   401             comms = new CommsEndpoint();
       
   402 
       
   403             // register listener
       
   404             try
       
   405             {
       
   406                 comms.registerListener(MODULE_ID_A, new TestListener());
       
   407                 comms.unregisterListener(MODULE_ID_A);
       
   408             }
       
   409             catch (CommsException e)
       
   410             {
       
   411                 fail("register listener");
       
   412             }
       
   413 
       
   414             // register multiple listeners
       
   415             try
       
   416             {
       
   417                 int i = 100;
       
   418                 for (; i < 300; i++)
       
   419                 {
       
   420                     comms.registerListener(i, new TestListener());
       
   421                 }
       
   422                 for (i--; i > 99; i--)
       
   423                 {
       
   424                     comms.unregisterListener(i);
       
   425                 }
       
   426             }
       
   427             catch (CommsException e)
       
   428             {
       
   429                 fail("register multiple listeners");
       
   430             }
       
   431 
       
   432             // listener already exists
       
   433             try
       
   434             {
       
   435                 comms.registerListener(MODULE_ID_A, new TestListener());
       
   436                 comms.registerListener(MODULE_ID_A, new TestListener());
       
   437                 fail("registerListener did not fail");
       
   438             }
       
   439             catch (CommsException e) {}
       
   440 
       
   441             // listener doesn't exists
       
   442             try
       
   443             {
       
   444                 comms.unregisterListener(MODULE_ID_A);
       
   445                 comms.unregisterListener(MODULE_ID_A);
       
   446                 fail("unregisterListener did not fail");
       
   447             }
       
   448             catch (CommsException e) {}
       
   449 
       
   450             // listener null
       
   451             try
       
   452             {
       
   453                 comms.registerListener(MODULE_ID_A, null);
       
   454                 fail("registerListener did not fail with null");
       
   455             }
       
   456             catch (Exception e) {}
       
   457 
       
   458             // register default listener
       
   459             try
       
   460             {
       
   461                 comms.registerDefaultListener(new TestListener());
       
   462                 comms.unregisterDefaultListener();
       
   463             }
       
   464             catch (Exception e)
       
   465             {
       
   466                 fail("registerDefaultListener");
       
   467             }
       
   468 
       
   469             // default listener already exists
       
   470             try
       
   471             {
       
   472                 comms.registerDefaultListener(new TestListener());
       
   473                 comms.registerDefaultListener(new TestListener());
       
   474                 fail("registerListener did not fail");
       
   475             }
       
   476             catch (CommsException e) {}
       
   477 
       
   478             // default listener doesn't exists
       
   479             try
       
   480             {
       
   481                 comms.unregisterDefaultListener();
       
   482                 comms.unregisterDefaultListener();
       
   483                 fail("unregisterDefaultListener did not fail");
       
   484             }
       
   485             catch (CommsException e) {}
       
   486 
       
   487             // default listener null
       
   488             try
       
   489             {
       
   490                 comms.registerDefaultListener(null);
       
   491                 fail("registerDefaultListener did not fail with null");
       
   492             }
       
   493             catch (Exception e) {}
       
   494 
       
   495         }
       
   496         finally
       
   497         {
       
   498             comms.destroy();
       
   499         }
       
   500     }
       
   501 
       
   502     private void testNoUnregisterListener()
       
   503     {
       
   504         System.out.println("TestCommsEndpoint.testNoUnregisterListener()");
       
   505 
       
   506         class TestListener implements CommsListener
       
   507         {
       
   508             public void processMessage(CommsMessage message) {}
       
   509         }
       
   510 
       
   511         CommsEndpoint comms = null;
       
   512         try
       
   513         {
       
   514             comms = new CommsEndpoint();
       
   515             comms.connect(SERVER_ADDRESS);
       
   516 
       
   517             // register multiple listeners but no unregister
       
   518             try
       
   519             {
       
   520                 comms.registerDefaultListener(new TestListener());
       
   521                 for (int i = 0; i < 300; i++)
       
   522                 {
       
   523                     comms.registerListener(i, new TestListener());
       
   524                 }
       
   525             }
       
   526             catch (CommsException e)
       
   527             {
       
   528                 fail("register multiple listeners");
       
   529             }
       
   530 
       
   531         }
       
   532         finally
       
   533         {
       
   534             comms.destroy();
       
   535         }
       
   536     }
       
   537 
       
   538     public void testRegisterListenerInListener()
       
   539     {
       
   540         System.out.println("TestCommsEndpoint.testRegisterListenerInListener()");
       
   541 
       
   542         CommsEndpoint comms = null;
       
   543         try
       
   544         {
       
   545             comms = new CommsEndpoint();
       
   546             comms.connect(SERVER_ADDRESS);
       
   547 
       
   548             class TestListener implements CommsListener
       
   549             {
       
   550                 private CommsEndpoint comms = null;
       
   551                 public boolean pass = false;
       
   552 
       
   553                 public TestListener(CommsEndpoint comms)
       
   554                 {
       
   555                     this.comms = comms;
       
   556                 }
       
   557 
       
   558                 public void processMessage(CommsMessage message)
       
   559                 {
       
   560                     try
       
   561                     {
       
   562                         comms.registerListener(MODULE_ID_B, this);
       
   563                         comms.unregisterListener(MODULE_ID_B);
       
   564                         pass = true;
       
   565                     }
       
   566                     catch (CommsException e)
       
   567                     {
       
   568                         e.printStackTrace();
       
   569                         fail("processMessage");
       
   570                     }
       
   571                 }
       
   572             }
       
   573             TestListener listener = new TestListener(comms);
       
   574             comms.registerListener(MODULE_ID_A, listener);
       
   575 
       
   576             CommsMessage message = new CommsMessage();
       
   577             message.setMessageId(2);
       
   578             message.setModuleId(MODULE_ID_A);
       
   579 
       
   580             comms.send(message);
       
   581             Thread.sleep(300); // give time to receive message
       
   582             comms.unregisterListener(MODULE_ID_A);
       
   583             assertTrue(listener.pass);
       
   584         }
       
   585         catch (Exception e)
       
   586         {
       
   587             e.printStackTrace();
       
   588             fail("testRegisterListenerInListener");
       
   589         }
       
   590         finally
       
   591         {
       
   592             comms.destroy();
       
   593         }
       
   594     }
       
   595 
       
   596     public void testSend()
       
   597     {
       
   598         System.out.println("TestCommsEndpoint.testSend()");
       
   599 
       
   600         CommsEndpoint comms = null;
       
   601         try
       
   602         {
       
   603             comms = new CommsEndpoint();
       
   604             comms.connect(SERVER_ADDRESS);
       
   605 
       
   606             CommsMessage message = new CommsMessage();
       
   607             message.setMessageId(888);
       
   608             message.setModuleId(MODULE_ID_A);
       
   609             message.write("hello world");
       
   610 
       
   611             // normal case
       
   612             comms.send(message);
       
   613 
       
   614             // empty message
       
   615             comms.send(new CommsMessage());
       
   616 
       
   617             // null message
       
   618             try
       
   619             {
       
   620                 comms.send(null);
       
   621                 fail("send did not fail with null");
       
   622             }
       
   623             catch (IllegalArgumentException e) {}
       
   624 
       
   625             // large message
       
   626             CommsMessage bigOne = new CommsMessage();
       
   627             bigOne.setMessageId(888);
       
   628             bigOne.setModuleId(MODULE_ID_A);
       
   629             bigOne.write(new byte [50000]);
       
   630             comms.send(message);
       
   631 
       
   632         }
       
   633         catch (Exception e)
       
   634         {
       
   635             e.printStackTrace();
       
   636             fail("testSend failed");
       
   637         }
       
   638         finally
       
   639         {
       
   640             comms.destroy();
       
   641         }
       
   642     }
       
   643 
       
   644     public void testSendReceive()
       
   645     {
       
   646         System.out.println("TestCommsEndpoint.testSendReceive()");
       
   647 
       
   648         CommsEndpoint comms = null;
       
   649         try
       
   650         {
       
   651             comms = new CommsEndpoint();
       
   652             comms.connect(SERVER_ADDRESS);
       
   653 
       
   654             CommsMessage message = new CommsMessage();
       
   655             message.setMessageId(888);
       
   656             message.setModuleId(MODULE_ID_A);
       
   657             byte[] bytes = "abcdefghijklmnopqrstyvwxyz".getBytes();
       
   658             message.write(bytes);
       
   659 
       
   660             // normal case
       
   661             CommsMessage msg = comms.sendReceive(message, TIMEOUT);
       
   662             assertEquals(msg.getMessageId(), message.getMessageId());
       
   663             assertEquals(msg.getModuleId(), message.getModuleId());
       
   664             byte[] b = msg.readBytes();
       
   665             assertEquals(b, bytes);
       
   666 
       
   667             // empty message
       
   668             CommsMessage empty = new CommsMessage();
       
   669             empty.setModuleId(MODULE_ID_A); // module id needs to be set so that reply will be sent
       
   670             CommsMessage msg2 = comms.sendReceive(empty, TIMEOUT);
       
   671             assertNotNull(msg2);
       
   672             assertEquals(msg2.getMessageId(), empty.getMessageId());
       
   673             assertEquals(msg2.getModuleId(), empty.getModuleId());
       
   674 
       
   675             // null message
       
   676             try
       
   677             {
       
   678                 comms.sendReceive(null, TIMEOUT);
       
   679                 fail("sendReceive did not fail with null");
       
   680             }
       
   681             catch (IllegalArgumentException e) {}
       
   682 
       
   683             // large message
       
   684             CommsMessage bigOne = new CommsMessage();
       
   685             bigOne.setMessageId(888);
       
   686             bigOne.setModuleId(MODULE_ID_A);
       
   687             byte[] array = new byte [50000];
       
   688             for (int i = 0; i < array.length; i++)
       
   689             {
       
   690                 array[i] = (byte)i;
       
   691             }
       
   692             bigOne.write(array);
       
   693             CommsMessage msg3 = comms.sendReceive(bigOne, TIMEOUT);
       
   694             assertEquals(msg3.getMessageId(), bigOne.getMessageId());
       
   695             assertEquals(msg3.getModuleId(), bigOne.getModuleId());
       
   696             byte[] big = msg3.readBytes();
       
   697             assertEquals(big, array);
       
   698 
       
   699             // Unicode string
       
   700             StringBuffer sb = new StringBuffer();
       
   701             sb.append('\u00a5'); // Japanese Yen symbol
       
   702             sb.append('\u01FC'); // Roman AE with acute accent
       
   703             sb.append('\u0391'); // Greek Capital Alpha
       
   704             sb.append('\u03A9'); // Greek Capital Omega
       
   705             sb.append('\u20ac'); // Euro symbol
       
   706             sb.append('\u20a8'); // Rupee symbol
       
   707             sb.append('\u040F'); // Cyrillic capital letter DZHE
       
   708             sb.append('\u062A'); // Arabic letter TEH
       
   709 
       
   710             String in = sb.toString();
       
   711             message = new CommsMessage();
       
   712             message.setModuleId(MODULE_ID_A);
       
   713             message.write(in);
       
   714             msg = comms.sendReceive(message, TIMEOUT);
       
   715             String out = msg.readString();
       
   716             assertEquals(in, out);
       
   717 
       
   718         }
       
   719         catch (Exception e)
       
   720         {
       
   721             e.printStackTrace();
       
   722             fail("testSendReceive failed");
       
   723         }
       
   724         finally
       
   725         {
       
   726             comms.destroy();
       
   727         }
       
   728     }
       
   729 
       
   730     public void testSendReceiveWithWaitForEver()
       
   731     {
       
   732         System.out.println("TestCommsEndpoint.testSendReceiveWithWaitForEver()");
       
   733 
       
   734         CommsEndpoint comms = null;
       
   735         try
       
   736         {
       
   737             comms = new CommsEndpoint();
       
   738             comms.connect(SERVER_ADDRESS);
       
   739 
       
   740             CommsMessage message = new CommsMessage();
       
   741             message.setMessageId(888);
       
   742             message.setModuleId(MODULE_ID_A);
       
   743             byte[] bytes = "abcdefghijklmnopqrstyvwxyz".getBytes();
       
   744             message.write(bytes);
       
   745 
       
   746             // normal case
       
   747             CommsMessage msg = comms.sendReceive(message, CommsEndpoint.WAIT_FOR_EVER);
       
   748             assertEquals(msg.getMessageId(), message.getMessageId());
       
   749             assertEquals(msg.getModuleId(), message.getModuleId());
       
   750             byte[] b = msg.readBytes();
       
   751             assertEquals(b, bytes);
       
   752 
       
   753             // empty message
       
   754             CommsMessage empty = new CommsMessage();
       
   755             empty.setModuleId(MODULE_ID_A); // module id needs to be set so that reply will be sent
       
   756             CommsMessage msg2 = comms.sendReceive(empty, CommsEndpoint.WAIT_FOR_EVER);
       
   757             assertNotNull(msg2);
       
   758             assertEquals(msg2.getMessageId(), empty.getMessageId());
       
   759             assertEquals(msg2.getModuleId(), empty.getModuleId());
       
   760 
       
   761             // null message
       
   762             try
       
   763             {
       
   764                 comms.sendReceive(null, CommsEndpoint.WAIT_FOR_EVER);
       
   765                 fail("sendReceive did not fail with null");
       
   766             }
       
   767             catch (IllegalArgumentException e) {}
       
   768 
       
   769             // large message
       
   770             CommsMessage bigOne = new CommsMessage();
       
   771             bigOne.setMessageId(888);
       
   772             bigOne.setModuleId(MODULE_ID_A);
       
   773             byte[] array = new byte [50000];
       
   774             for (int i = 0; i < array.length; i++)
       
   775             {
       
   776                 array[i] = (byte)i;
       
   777             }
       
   778             bigOne.write(array);
       
   779             CommsMessage msg3 = comms.sendReceive(bigOne, CommsEndpoint.WAIT_FOR_EVER);
       
   780             assertEquals(msg3.getMessageId(), bigOne.getMessageId());
       
   781             assertEquals(msg3.getModuleId(), bigOne.getModuleId());
       
   782             byte[] big = msg3.readBytes();
       
   783             assertEquals(big, array);
       
   784 
       
   785         }
       
   786         catch (Exception e)
       
   787         {
       
   788             e.printStackTrace();
       
   789             fail("testSendReceiveWithWaitForEver failed");
       
   790         }
       
   791         finally
       
   792         {
       
   793             comms.destroy();
       
   794         }
       
   795     }
       
   796 
       
   797     public void testSendReceiveBigTimeout()
       
   798     {
       
   799         System.out.println("TestCommsEndpoint.testSendReceiveBigTimeout()");
       
   800 
       
   801         CommsEndpoint comms = null;
       
   802         try
       
   803         {
       
   804             comms = new CommsEndpoint();
       
   805             comms.connect(SERVER_ADDRESS);
       
   806 
       
   807             CommsMessage message = new CommsMessage();
       
   808             message.setMessageId(888);
       
   809             message.setModuleId(MODULE_ID_A);
       
   810             byte[] bytes = "abcdefghijklmnopqrstyvwxyz".getBytes();
       
   811             message.write(bytes);
       
   812 
       
   813             // OpenC timeout limit
       
   814             CommsMessage msg = comms.sendReceive(message, 2148);
       
   815             assertEquals(msg.getMessageId(), message.getMessageId());
       
   816             assertEquals(msg.getModuleId(), message.getModuleId());
       
   817             byte[] b = msg.readBytes();
       
   818             assertEquals(b, bytes);
       
   819 
       
   820             // 1h
       
   821             msg = comms.sendReceive(message, 3600);
       
   822             assertEquals(msg.getMessageId(), message.getMessageId());
       
   823             assertEquals(msg.getModuleId(), message.getModuleId());
       
   824             b = msg.readBytes();
       
   825             assertEquals(b, bytes);
       
   826 
       
   827             // 24h
       
   828             msg = comms.sendReceive(message, 86400);
       
   829             assertEquals(msg.getMessageId(), message.getMessageId());
       
   830             assertEquals(msg.getModuleId(), message.getModuleId());
       
   831             b = msg.readBytes();
       
   832             assertEquals(b, bytes);
       
   833 
       
   834         }
       
   835         catch (Exception e)
       
   836         {
       
   837             e.printStackTrace();
       
   838             fail("testSendReceiveBigTimeout failed");
       
   839         }
       
   840         finally
       
   841         {
       
   842             comms.destroy();
       
   843         }
       
   844     }
       
   845 
       
   846 
       
   847     public void testSendReceiveTimeout()
       
   848     {
       
   849         System.out.println("TestCommsEndpoint.testSendReceiveTimeout()");
       
   850         CommsEndpoint comms = null;
       
   851         try
       
   852         {
       
   853             comms = new CommsEndpoint();
       
   854             comms.connect(SERVER_ADDRESS);
       
   855 
       
   856             CommsMessage message = new CommsMessage();
       
   857             message.setMessageId(888);
       
   858             message.write("hello world");
       
   859 
       
   860             try
       
   861             {
       
   862                 message.setModuleId(MODULE_ID_NO_REPLY);
       
   863                 comms.sendReceive(message, 1);
       
   864                 fail("sendReceive did not timeout (no reply)");
       
   865             }
       
   866             catch (CommsException ie) { }
       
   867 
       
   868             try
       
   869             {
       
   870                 message.setModuleId(MODULE_ID_SLEEP_5S);
       
   871                 comms.sendReceive(message, 3);
       
   872                 fail("sendReceive did not timeout (5s)");
       
   873             }
       
   874             catch (CommsException ie) { }
       
   875 
       
   876             try
       
   877             {
       
   878                 message.setModuleId(MODULE_ID_SLEEP_10S);
       
   879                 comms.sendReceive(message, 8);
       
   880                 fail("sendReceive did not timeout (10s)");
       
   881             }
       
   882             catch (CommsException ie) { }
       
   883 
       
   884         }
       
   885         catch (Exception e)
       
   886         {
       
   887             e.printStackTrace();
       
   888             fail("testSendReceiveTimeout");
       
   889         }
       
   890         finally
       
   891         {
       
   892             comms.destroy();
       
   893         }
       
   894     }
       
   895 
       
   896     private void testSendReceiveMultiThread()
       
   897     {
       
   898         System.out.println("TestCommsEndpoint.testSendReceiveMultiThread()");
       
   899 
       
   900         CommsEndpoint comms = null;
       
   901         try
       
   902         {
       
   903             comms = new CommsEndpoint();
       
   904             comms.connect(SERVER_ADDRESS);
       
   905 
       
   906             class Sender extends Thread
       
   907             {
       
   908                 private CommsEndpoint comms = null;
       
   909                 public int sendCount = 0;
       
   910 
       
   911                 public Sender(CommsEndpoint comms)
       
   912                 {
       
   913                     this.comms = comms;
       
   914                 }
       
   915 
       
   916                 public void run()
       
   917                 {
       
   918                     for (int i=0; i<5; i++)
       
   919                     {
       
   920                         try
       
   921                         {
       
   922                             Thread.sleep((100));
       
   923                         }
       
   924                         catch (InterruptedException e) { }
       
   925 
       
   926                         try
       
   927                         {
       
   928                             CommsMessage message = new CommsMessage();
       
   929                             message.setMessageId(2);
       
   930                             message.setModuleId(MODULE_ID_A);
       
   931                             comms.sendReceive(message, TIMEOUT);
       
   932                             comms.sendReceive(message, CommsEndpoint.WAIT_FOR_EVER);
       
   933                             sendCount++;
       
   934                         }
       
   935                         catch (Exception e)
       
   936                         {
       
   937                             e.printStackTrace();
       
   938                             fail("run");
       
   939                         }
       
   940                     }
       
   941                 }
       
   942             }
       
   943 
       
   944             Sender t1 = new Sender(comms);
       
   945             Sender t2 = new Sender(comms);
       
   946             Sender t3 = new Sender(comms);
       
   947 
       
   948             t1.start();
       
   949             t2.start();
       
   950             t3.start();
       
   951             t1.join();
       
   952             t2.join();
       
   953             t3.join();
       
   954 
       
   955             assertEquals(t1.sendCount, 5);
       
   956             assertEquals(t2.sendCount, 5);
       
   957             assertEquals(t3.sendCount, 5);
       
   958 
       
   959         }
       
   960         catch (Exception e)
       
   961         {
       
   962             e.printStackTrace();
       
   963             fail("testSendReceiveMultiThread");
       
   964         }
       
   965         finally
       
   966         {
       
   967             comms.destroy();
       
   968         }
       
   969     }
       
   970 
       
   971     private void testExitWhileSendReceivePending()
       
   972     {
       
   973         System.out.println("TestCommsEndpoint.testExitWhileSendReceivePending()");
       
   974 
       
   975         CommsEndpoint comms = null;
       
   976         try
       
   977         {
       
   978             comms = new CommsEndpoint();
       
   979             comms.connect(SERVER_ADDRESS);
       
   980 
       
   981             class Sender extends Thread
       
   982             {
       
   983                 private CommsEndpoint comms = null;
       
   984                 private int timeout = 0;
       
   985                 public Sender(CommsEndpoint comms, int aTimeout)
       
   986                 {
       
   987                     this.comms = comms;
       
   988                     timeout = aTimeout;
       
   989                 }
       
   990                 public void run()
       
   991                 {
       
   992                     try
       
   993                     {
       
   994                         CommsMessage message = new CommsMessage();
       
   995                         message.setModuleId(MODULE_ID_NO_REPLY);
       
   996                         comms.sendReceive(message, timeout);
       
   997                     }
       
   998                     catch (CommsException ce)
       
   999                     {
       
  1000                     }
       
  1001                 }
       
  1002             }
       
  1003 
       
  1004             Sender t1 = new Sender(comms, CommsEndpoint.WAIT_FOR_EVER);
       
  1005             Sender t2 = new Sender(comms, TIMEOUT);
       
  1006             Sender t3 = new Sender(comms, CommsEndpoint.WAIT_FOR_EVER);
       
  1007             Sender t4 = new Sender(comms, TIMEOUT);
       
  1008 
       
  1009             t1.start();
       
  1010             t2.start();
       
  1011             t3.start();
       
  1012             t4.start();
       
  1013             Thread.sleep(300);
       
  1014             // exit while there is pending sendReceive threads
       
  1015             comms.destroy();
       
  1016             t1.join();
       
  1017             t2.join();
       
  1018             t3.join();
       
  1019             t4.join();
       
  1020 
       
  1021         }
       
  1022         catch (Exception e)
       
  1023         {
       
  1024             e.printStackTrace();
       
  1025             fail("testExitWhileSendReceivePending");
       
  1026         }
       
  1027         finally
       
  1028         {
       
  1029             comms.destroy();
       
  1030         }
       
  1031     }
       
  1032 
       
  1033     private void testSendAndSendReceiveMultiThread()
       
  1034     {
       
  1035         System.out.println("TestCommsEndpoint.testSendAndSendReceiveMultiThread()");
       
  1036         CommsEndpoint comms = null;
       
  1037         try
       
  1038         {
       
  1039             class TestListener implements CommsListener
       
  1040             {
       
  1041                 public void processMessage(CommsMessage message) { }
       
  1042             }
       
  1043 
       
  1044             class Sender extends Thread
       
  1045             {
       
  1046                 private CommsEndpoint comms = null;
       
  1047                 private boolean doSendReceive = false;
       
  1048                 public int sendCount = 0;
       
  1049 
       
  1050                 public Sender(CommsEndpoint comms, boolean doSendReceive)
       
  1051                 {
       
  1052                     this.comms = comms;
       
  1053                     this.doSendReceive = doSendReceive;
       
  1054                 }
       
  1055 
       
  1056                 public void run()
       
  1057                 {
       
  1058                     for (int i=0; i<5; i++)
       
  1059                     {
       
  1060                         try
       
  1061                         {
       
  1062                             Thread.sleep((100));
       
  1063                         }
       
  1064                         catch (InterruptedException e) { }
       
  1065 
       
  1066                         try
       
  1067                         {
       
  1068                             CommsMessage message = new CommsMessage();
       
  1069                             message.setMessageId(2);
       
  1070                             message.setModuleId(MODULE_ID_A);
       
  1071                             if (doSendReceive)
       
  1072                             {
       
  1073                                 comms.sendReceive(message, TIMEOUT);
       
  1074                                 comms.sendReceive(message, CommsEndpoint.WAIT_FOR_EVER);
       
  1075                             }
       
  1076                             else
       
  1077                             {
       
  1078                                 comms.send(message);
       
  1079                             }
       
  1080                             sendCount++;
       
  1081                         }
       
  1082                         catch (Exception e)
       
  1083                         {
       
  1084                             e.printStackTrace();
       
  1085                             fail("run");
       
  1086                         }
       
  1087                     }
       
  1088                 }
       
  1089             }
       
  1090 
       
  1091             comms = new CommsEndpoint();
       
  1092             comms.connect(SERVER_ADDRESS);
       
  1093             comms.registerListener(MODULE_ID_A, new TestListener());
       
  1094 
       
  1095             Sender t0 = new Sender(comms, true);
       
  1096             Sender t1 = new Sender(comms, true);
       
  1097             Sender t2 = new Sender(comms, true);
       
  1098             Sender t3 = new Sender(comms, true);
       
  1099             Sender t4 = new Sender(comms, true);
       
  1100             Sender t5 = new Sender(comms, false);
       
  1101             Sender t6 = new Sender(comms, false);
       
  1102             Sender t7 = new Sender(comms, false);
       
  1103             Sender t8 = new Sender(comms, false);
       
  1104             Sender t9 = new Sender(comms, false);
       
  1105 
       
  1106             t0.start();
       
  1107             t1.start();
       
  1108             t2.start();
       
  1109             t3.start();
       
  1110             t4.start();
       
  1111             t5.start();
       
  1112             t6.start();
       
  1113             t7.start();
       
  1114             t8.start();
       
  1115             t9.start();
       
  1116             t0.join();
       
  1117             t1.join();
       
  1118             t2.join();
       
  1119             t3.join();
       
  1120             t4.join();
       
  1121             t5.join();
       
  1122             t6.join();
       
  1123             t7.join();
       
  1124             t8.join();
       
  1125             t9.join();
       
  1126 
       
  1127             comms.unregisterListener(MODULE_ID_A);
       
  1128 
       
  1129             assertEquals(t0.sendCount, 5);
       
  1130             assertEquals(t1.sendCount, 5);
       
  1131             assertEquals(t2.sendCount, 5);
       
  1132             assertEquals(t3.sendCount, 5);
       
  1133             assertEquals(t4.sendCount, 5);
       
  1134             assertEquals(t5.sendCount, 5);
       
  1135             assertEquals(t6.sendCount, 5);
       
  1136             assertEquals(t7.sendCount, 5);
       
  1137             assertEquals(t8.sendCount, 5);
       
  1138             assertEquals(t9.sendCount, 5);
       
  1139 
       
  1140         }
       
  1141         catch (Exception e)
       
  1142         {
       
  1143             e.printStackTrace();
       
  1144             fail("testSendAndSendReceiveMultiThread");
       
  1145         }
       
  1146         finally
       
  1147         {
       
  1148             comms.destroy();
       
  1149         }
       
  1150     }
       
  1151 
       
  1152     public void testSendWhenNotConnected()
       
  1153     {
       
  1154         System.out.println("TestCommsEndpoint.testSendWhenNotConnected()");
       
  1155 
       
  1156         CommsEndpoint comms = null;
       
  1157         try
       
  1158         {
       
  1159             comms = new CommsEndpoint();
       
  1160 
       
  1161             // send message
       
  1162             try
       
  1163             {
       
  1164                 comms.send(new CommsMessage());
       
  1165                 fail("send did not fail");
       
  1166             }
       
  1167             catch (CommsException e) {}
       
  1168 
       
  1169             // send message
       
  1170             try
       
  1171             {
       
  1172                 comms.sendReceive(new CommsMessage(), TIMEOUT);
       
  1173                 fail("sendReceive did not fail");
       
  1174             }
       
  1175             catch (CommsException e) {}
       
  1176 
       
  1177         }
       
  1178         catch (Exception e)
       
  1179         {
       
  1180             e.printStackTrace();
       
  1181             fail("testSendWhenNotConnected failed");
       
  1182         }
       
  1183         finally
       
  1184         {
       
  1185             comms.destroy();
       
  1186         }
       
  1187     }
       
  1188 
       
  1189 
       
  1190     private void testCheckMessageInListener()
       
  1191     {
       
  1192         System.out.println("TestCommsEndpoint.testCheckMessageInListener()");
       
  1193         CommsEndpoint comms = null;
       
  1194         try
       
  1195         {
       
  1196             class TestListener implements CommsListener
       
  1197             {
       
  1198                 public boolean pass = false;
       
  1199                 String s = null;
       
  1200                 byte[] b = null;
       
  1201                 public TestListener(String s, byte[] b)
       
  1202                 {
       
  1203                     this.s = s;
       
  1204                     this.b = b;
       
  1205                 }
       
  1206 
       
  1207                 public void processMessage(CommsMessage message)
       
  1208                 {
       
  1209                     String str = message.readString();
       
  1210                     byte[] arr = message.readBytes();
       
  1211 
       
  1212                     assertEquals(str.compareTo(s), 0);
       
  1213                     assertEquals(arr, b);
       
  1214                     pass = true;
       
  1215                 }
       
  1216             }
       
  1217 
       
  1218             String s = "abcdefghijklmnopqrstyvwxyz";
       
  1219             byte[] array = new byte[1500];
       
  1220             for (int i=0; i<array.length; i++)
       
  1221             {
       
  1222                 array[i] = (byte)i;
       
  1223             }
       
  1224 
       
  1225             comms = new CommsEndpoint();
       
  1226             comms.connect(SERVER_ADDRESS);
       
  1227             TestListener listener = new TestListener(s, array);
       
  1228             comms.registerListener(MODULE_ID_A, listener);
       
  1229 
       
  1230             CommsMessage message = new CommsMessage();
       
  1231             message.setMessageId(2);
       
  1232             message.setModuleId(MODULE_ID_A);
       
  1233             message.write(s);
       
  1234             message.write(array);
       
  1235 
       
  1236             comms.send(message);
       
  1237             Thread.sleep(300); // give time to receive message
       
  1238             comms.unregisterListener(MODULE_ID_A);
       
  1239 
       
  1240             assertTrue(listener.pass);
       
  1241 
       
  1242         }
       
  1243         catch (Exception e)
       
  1244         {
       
  1245             e.printStackTrace();
       
  1246             fail("testCheckMessageInListener");
       
  1247         }
       
  1248         finally
       
  1249         {
       
  1250             comms.destroy();
       
  1251         }
       
  1252     }
       
  1253 
       
  1254     private void testCheckNativePayload()
       
  1255     {
       
  1256         System.out.println("TestCommsEndpoint.testCheckNativePayload()");
       
  1257 
       
  1258         CommsEndpoint comms = null;
       
  1259         try
       
  1260         {
       
  1261             // keep this in sync with native side test case
       
  1262 
       
  1263             comms = new CommsEndpoint();
       
  1264             comms.connect(SERVER_ADDRESS);
       
  1265 
       
  1266             CommsMessage message = new CommsMessage();
       
  1267 
       
  1268             message.setMessageId(888);
       
  1269             message.setModuleId(MODULE_ID_DATA_CHECK);
       
  1270 
       
  1271             String input = "abcdefghijklmnopqrstyvwxyz";
       
  1272             // latin small letter a with diaeresis, latin small letter a with ring above, latin small letter o with diaeresis
       
  1273             String utf16 = "\u00E4 \u00E5 \u00F6 \u20AC";
       
  1274             String utf8 = new String(utf16.getBytes("UTF8"), "UTF8");
       
  1275 
       
  1276             // Japanese Yen symbol, Roman AE with acute accent, Greek Capital Alpha, Greek Capital Omega,
       
  1277             // Euro symbol, Rupee symbol, Cyrillic capital letter DZHE, Arabic letter TEH
       
  1278             String symbols = "\u00a5\u01FC\u0391\u03A9\u20ac\u20a8\u040F\u062A";
       
  1279 
       
  1280             // devanagari letter a, tibetan digit zero, katakana letter ya
       
  1281             String letters = "\u0905 \u0F20 \u30E4";
       
  1282 
       
  1283             // check that all byte values go through ok
       
  1284             // UTF-16 byte-order marks (0xfe, 0xff) do not appear in UTF-8
       
  1285             byte[] array = new byte[0xff];
       
  1286             for (int i=0; i< array.length; i++)
       
  1287             {
       
  1288                 array[i] = (byte)i;
       
  1289             }
       
  1290 
       
  1291             int in = 0xCAFEBABE;
       
  1292             int zero = 0;
       
  1293             int one = 1;
       
  1294             int max = 0xFFFFFFFF;
       
  1295             message.write(input);
       
  1296             message.write(in);
       
  1297             message.write(zero);
       
  1298             message.write(one);
       
  1299             message.write(max);
       
  1300             message.write(utf8);
       
  1301             message.write(utf16);
       
  1302             message.write(symbols);
       
  1303             message.write(symbols.getBytes("UTF8"));
       
  1304             message.write(letters);
       
  1305             message.write(letters.getBytes("UTF8"));
       
  1306             message.write(array);
       
  1307 
       
  1308             CommsMessage msg = comms.sendReceive(message, 1);
       
  1309 
       
  1310             String output = msg.readString();
       
  1311             int out = msg.readInt();
       
  1312             zero = msg.readInt();
       
  1313             one = msg.readInt();
       
  1314             max = msg.readInt();
       
  1315             String letters_out = msg.readString();
       
  1316             String symbols_out = msg.readString();
       
  1317             String utf16_out = msg.readString();
       
  1318             String utf8_out = msg.readString();
       
  1319             byte[] array_out = msg.readBytes();
       
  1320 
       
  1321             String s = "ABCDEFGHIJKLMNOPQRSTYWXYZ";
       
  1322             int i = 0xDEADBEEF;
       
  1323 
       
  1324             assertEquals(output.compareTo(s), 0);
       
  1325             assertEquals(out, i);
       
  1326             assertEquals(zero, 0);
       
  1327             assertEquals(one, 1);
       
  1328             assertEquals(max, 0xFFFFFFFF);
       
  1329             assertEquals(letters_out.compareTo(letters), 0);
       
  1330             assertEquals(symbols_out.compareTo(symbols), 0);
       
  1331             assertEquals(utf16_out.compareTo(utf16), 0);
       
  1332             assertEquals(utf8_out.compareTo(utf8), 0);
       
  1333             assertEquals(array_out, array);
       
  1334         }
       
  1335         catch (Exception e)
       
  1336         {
       
  1337             e.printStackTrace();
       
  1338             fail("testCheckNativePayload");
       
  1339         }
       
  1340         finally
       
  1341         {
       
  1342             comms.destroy();
       
  1343         }
       
  1344     }
       
  1345 
       
  1346 
       
  1347     public void testDestroy()
       
  1348     {
       
  1349         System.out.println("TestCommsEndpoint.testDestroy()");
       
  1350         class TestListener implements CommsListener
       
  1351         {
       
  1352             public void processMessage(CommsMessage message) {}
       
  1353         }
       
  1354 
       
  1355         CommsMessage msg = new CommsMessage();
       
  1356 
       
  1357         CommsEndpoint comms = null;
       
  1358         // simple case
       
  1359         try
       
  1360         {
       
  1361             comms = new CommsEndpoint();
       
  1362             comms.destroy();
       
  1363         }
       
  1364         catch (CommsException e)
       
  1365         {
       
  1366             fail("simple case failed");
       
  1367         }
       
  1368         finally
       
  1369         {
       
  1370             comms.destroy();
       
  1371             comms = null;
       
  1372         }
       
  1373 
       
  1374         // destroy when connected
       
  1375         try
       
  1376         {
       
  1377             comms = new CommsEndpoint();
       
  1378             comms.connect(SERVER_ADDRESS);
       
  1379             comms.destroy();
       
  1380         }
       
  1381         catch (CommsException e)
       
  1382         {
       
  1383             fail("destroy when connected failed");
       
  1384         }
       
  1385         finally
       
  1386         {
       
  1387             comms.destroy();
       
  1388             comms = null;
       
  1389         }
       
  1390 
       
  1391         // destroy when disconnected
       
  1392         try
       
  1393         {
       
  1394             comms = new CommsEndpoint();
       
  1395             comms.connect(SERVER_ADDRESS);
       
  1396             comms.disconnect();
       
  1397             comms.destroy();
       
  1398         }
       
  1399         catch (CommsException e)
       
  1400         {
       
  1401             fail("destroy when disconnected failed");
       
  1402         }
       
  1403         finally
       
  1404         {
       
  1405             comms.destroy();
       
  1406             comms = null;
       
  1407         }
       
  1408 
       
  1409 
       
  1410 
       
  1411         // calling methods after close
       
  1412         try
       
  1413         {
       
  1414             comms = new CommsEndpoint();
       
  1415             comms.destroy();
       
  1416             comms.destroy();
       
  1417             try
       
  1418             {
       
  1419                 comms.connect(SERVER_ADDRESS);
       
  1420                 fail("connect");
       
  1421             }
       
  1422             catch (CommsException e) {}
       
  1423 
       
  1424             try
       
  1425             {
       
  1426                 comms.disconnect();
       
  1427                 fail("disconnect");
       
  1428             }
       
  1429             catch (CommsException e) {}
       
  1430 
       
  1431             try
       
  1432             {
       
  1433                 comms.registerListener(MODULE_ID_A, new TestListener());
       
  1434                 fail("registerListener");
       
  1435             }
       
  1436             catch (Exception e) {}
       
  1437 
       
  1438             try
       
  1439             {
       
  1440                 comms.unregisterListener(MODULE_ID_A);
       
  1441                 fail("unregisterListener");
       
  1442             }
       
  1443             catch (Exception e) {}
       
  1444 
       
  1445             try
       
  1446             {
       
  1447                 comms.registerDefaultListener(new TestListener());
       
  1448                 fail("registerDefaultListener");
       
  1449             }
       
  1450             catch (Exception e) {}
       
  1451 
       
  1452             try
       
  1453             {
       
  1454                 comms.unregisterDefaultListener();
       
  1455                 fail("unregisterDefaultListener");
       
  1456             }
       
  1457             catch (Exception e) {}
       
  1458 
       
  1459             try
       
  1460             {
       
  1461                 comms.send(msg);
       
  1462                 fail("send");
       
  1463             }
       
  1464             catch (Exception e) {}
       
  1465 
       
  1466             try
       
  1467             {
       
  1468                 CommsMessage m = comms.sendReceive(msg, TIMEOUT);
       
  1469                 fail("send");
       
  1470             }
       
  1471             catch (Exception e) {}
       
  1472 
       
  1473         }
       
  1474         catch (CommsException e)
       
  1475         {
       
  1476             fail(" multiple endpoints failed");
       
  1477         }
       
  1478         finally
       
  1479         {
       
  1480             comms.destroy();
       
  1481             comms = null;
       
  1482         }
       
  1483 
       
  1484     }
       
  1485 
       
  1486 }
       
  1487