1   /*
2    * $Id: SafeProtocolTestCase.java 10789 2008-02-12 20:04:43Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.tcp.protocols;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.FunctionalTestCase;
17  
18  public class SafeProtocolTestCase extends FunctionalTestCase
19  {
20  
21      protected static String TEST_MESSAGE = "Test TCP Request";
22  
23      protected String getConfigResources()
24      {
25          return "safe-protocol-test.xml";
26      }
27  
28      public void testSafeToSafe() throws MuleException
29      {
30          MuleClient client = new MuleClient();
31          assertResponseOk(client.send("tcp://localhost:65432?connector=safe", TEST_MESSAGE, null));
32      }
33  
34      public void testUnsafeToSafe() throws MuleException
35      {
36          MuleClient client = new MuleClient();
37          assertResponseBad(client.send("tcp://localhost:65432?connector=unsafe", TEST_MESSAGE, null));
38      }
39  
40      private void assertResponseOk(MuleMessage message)
41      {
42          assertNotNull("Null message", message);
43          Object payload = message.getPayload();
44          assertNotNull("Null payload", payload);
45          assertTrue("Payload not byte[]", payload instanceof byte[]);
46          assertEquals(TEST_MESSAGE + " Received", new String((byte[]) payload));
47      }
48  
49      protected void assertResponseBad(MuleMessage message)
50      {
51          try
52          {
53              if (message.getPayloadAsString().equals(TEST_MESSAGE + " Received"))
54              {
55                  fail("expected error");
56              }
57          }
58          catch (Exception e)
59          {
60              // expected
61          }
62      }
63  
64  }