javacommons/comms/tsrc/src/find.cpp
changeset 21 2a9601315dfc
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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 #include <errno.h>
       
    19 #include "monitor.h"
       
    20 #include "TestHarness.h"
       
    21 #include "logger.h"
       
    22 
       
    23 #include "commsserverendpoint.h"
       
    24 #include "commsclientendpoint.h"
       
    25 #include "commsmessage.h"
       
    26 #include "echoserver.h"
       
    27 
       
    28 using namespace java::comms;
       
    29 using java::util::Monitor;
       
    30 
       
    31 TEST_GROUP(Find)
       
    32 {
       
    33     TEST_SETUP()
       
    34     {
       
    35         {
       
    36             CommsClientEndpoint client;
       
    37         }
       
    38     }
       
    39 
       
    40     TEST_TEARDOWN()
       
    41     {
       
    42     }
       
    43 };
       
    44 
       
    45 /**
       
    46  * Test find client endpoint
       
    47  * 1. Named endpoint exists
       
    48  * 2. Named endpoint does not exist
       
    49  * 3. Multiple named endpoints
       
    50  */
       
    51 
       
    52 TEST(Find, FindClient)
       
    53 {
       
    54     EchoServer server;
       
    55     CHECK(!server.start(IPC_ADDRESS_COMMS_MODULE_TEST));
       
    56 
       
    57     CommsMessage msg;
       
    58     CommsMessage receivedMsg;
       
    59 
       
    60     // 1. Named endpoint exists
       
    61     CommsClientEndpoint client(L"CommsModuleTest");
       
    62     CHECK(!client.connect(IPC_ADDRESS_COMMS_MODULE_TEST));
       
    63 
       
    64     CommsClientEndpoint* pClient = CommsClientEndpoint::find(L"CommsModuleTest");
       
    65     CHECK(pClient);
       
    66 
       
    67     CHECK(!client.send(msg));
       
    68     CHECK(!pClient->send(msg));
       
    69 
       
    70     CHECK(!pClient->disconnect());
       
    71     CHECK(!pClient->connect(IPC_ADDRESS_COMMS_MODULE_TEST));
       
    72 
       
    73     CHECK(!client.sendReceive(msg, receivedMsg, WAIT_FOR_EVER));
       
    74     CHECK(!pClient->sendReceive(msg, receivedMsg, WAIT_FOR_EVER));
       
    75 
       
    76     CHECK(!client.disconnect());
       
    77 
       
    78     // 2. Named endpoint does not exist
       
    79     pClient = CommsClientEndpoint::find(L"commsmoduletest");
       
    80     CHECK(!pClient);
       
    81 
       
    82     pClient = CommsClientEndpoint::find(L"");
       
    83     CHECK(!pClient);
       
    84 
       
    85     pClient = CommsClientEndpoint::find(L"CommsModuleTest ");
       
    86     CHECK(!pClient);
       
    87 
       
    88     {
       
    89         CommsClientEndpoint cli(L"clientendpoint");
       
    90         pClient = CommsClientEndpoint::find(L"clientendpoint");
       
    91         CHECK(pClient);
       
    92     }
       
    93     pClient = CommsClientEndpoint::find(L"clientendpoint");
       
    94     CHECK(!pClient);
       
    95 
       
    96     // 3. Multiple named endpoints
       
    97     CommsClientEndpoint client1(L"client1");
       
    98     CommsClientEndpoint client2(L"client2");
       
    99     CommsClientEndpoint client3(L"client3");
       
   100 
       
   101     pClient = CommsClientEndpoint::find(L"client1");
       
   102     CHECK(pClient);
       
   103     pClient = CommsClientEndpoint::find(L"client2");
       
   104     CHECK(pClient);
       
   105     pClient = CommsClientEndpoint::find(L"client3");
       
   106     CHECK(pClient);
       
   107 
       
   108     CHECK(!server.stop());
       
   109 }
       
   110 
       
   111 
       
   112 /**
       
   113  * Test find server endpoint
       
   114  * 1. Named endpoint exists
       
   115  * 2. Named endpoint does not exist
       
   116  * 3. Multiple named endpoints
       
   117  */
       
   118 
       
   119 TEST(Find, FindServer)
       
   120 {
       
   121     CommsMessage msg;
       
   122     CommsMessage receivedMsg;
       
   123 
       
   124     CommsClientEndpoint client;
       
   125 
       
   126     // 1. Named endpoint exists
       
   127     CommsServerEndpoint server(L"CommsModuleTest");
       
   128     CHECK(!server.start(IPC_ADDRESS_COMMS_MODULE_TEST));
       
   129     CHECK(!client.connect(IPC_ADDRESS_COMMS_MODULE_TEST));
       
   130 
       
   131     CommsServerEndpoint* pServer = CommsServerEndpoint::find(L"CommsModuleTest");
       
   132     CHECK(pServer);
       
   133 
       
   134     CHECK(!client.send(msg));
       
   135     CHECK(!pServer->stop());
       
   136 
       
   137     CHECK(client.send(msg) != 0);
       
   138     CHECK(!client.disconnect());
       
   139 
       
   140     CHECK(!pServer->start(IPC_ADDRESS_COMMS_MODULE_TEST));
       
   141     CHECK(!client.connect(IPC_ADDRESS_COMMS_MODULE_TEST));
       
   142 
       
   143     CHECK(!client.send(msg));
       
   144     CHECK(!server.stop());
       
   145 
       
   146     CHECK(client.send(msg) != 0);
       
   147     CHECK(!client.disconnect());
       
   148 
       
   149     // 2. Named endpoint does not exist
       
   150     pServer = CommsServerEndpoint::find(L"commsmoduletest");
       
   151     CHECK(!pServer);
       
   152 
       
   153     pServer = CommsServerEndpoint::find(L"");
       
   154     CHECK(!pServer);
       
   155 
       
   156     pServer = CommsServerEndpoint::find(L"CommsModuleTest ");
       
   157     CHECK(!pServer);
       
   158 
       
   159     {
       
   160         CommsServerEndpoint cli(L"serverendpoint");
       
   161         pServer = CommsServerEndpoint::find(L"serverendpoint");
       
   162         CHECK(pServer);
       
   163     }
       
   164     pServer = CommsServerEndpoint::find(L"serverendpoint");
       
   165     CHECK(!pServer);
       
   166 
       
   167     // 3. Multiple named endpoints
       
   168     CommsServerEndpoint server1(L"server1");
       
   169     CommsServerEndpoint server2(L"server2");
       
   170     CommsServerEndpoint server3(L"server3");
       
   171 
       
   172     pServer = CommsServerEndpoint::find(L"server1");
       
   173     CHECK(pServer);
       
   174     pServer = CommsServerEndpoint::find(L"server2");
       
   175     CHECK(pServer);
       
   176     pServer = CommsServerEndpoint::find(L"server3");
       
   177     CHECK(pServer);
       
   178 }
       
   179 
       
   180 
       
   181 /**
       
   182  * Server and client endpoint with same name
       
   183  * 1. Server and client endpoint with same name
       
   184  */
       
   185 
       
   186 TEST(Find, FindSameName)
       
   187 {
       
   188 #ifdef __SYMBIAN32__
       
   189     EXPECT_N_LEAKS(2);
       
   190 #else
       
   191     EXPECT_N_LEAKS(1);
       
   192 #endif
       
   193     // 1. Server and client endpoint with same name
       
   194     CommsServerEndpoint server(L"hello world");
       
   195     CommsClientEndpoint client(L"hello world");
       
   196 
       
   197     CommsServerEndpoint* pServer = CommsServerEndpoint::find(L"hello world");
       
   198     CHECK(pServer);
       
   199     CommsClientEndpoint* pClient = CommsClientEndpoint::find(L"hello world");
       
   200     CHECK(pClient);
       
   201 }