View Javadoc

1   /*
2    * $Id: LengthProtocolLengthTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.issues;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.DynamicPortTestCase;
16  
17  import edu.emory.mathcs.backport.java.util.Arrays;
18  
19  public class LengthProtocolLengthTestCase extends DynamicPortTestCase
20  {
21  
22      protected String getConfigResources()
23      {
24          return "length-protocol-length-test.xml";
25      }
26  
27      public void testLength() throws Exception
28      {
29          doTest("length", 5, true);
30          doTest("length", 15, false);
31      }
32  
33      public void testSafe() throws Exception
34      {
35          doTest("safe", 5, true);
36          doTest("safe", 15, false);
37      }
38  
39      protected void doTest(String endpoint, int length, boolean ok) throws Exception
40      {
41          byte[] message = new byte[length];
42          for (int i = 0; i < length; ++i)
43          {
44              message[i] = (byte)(i % 255);
45          }
46          MuleClient client = new MuleClient(muleContext);
47          if (ok)
48          {
49              MuleMessage response = client.send(endpoint, message, null);
50              assertNotNull(response);
51              assertNotNull(response.getPayload());
52              assertTrue(Arrays.equals(message, response.getPayloadAsBytes()));
53          }
54          else
55          {
56              assertResponseBad(client.send(endpoint, message, null));
57          }
58      }
59  
60      protected void assertResponseBad(MuleMessage message)
61      {
62          try
63          {
64              if (message.getPayloadAsString().equals(TEST_MESSAGE + " Received"))
65              {
66                  fail("expected error");
67              }
68          }
69          catch (Exception e)
70          {
71              // expected
72          }
73      }
74  
75      @Override
76      protected int getNumPortsToFind()
77      {
78          return 2;
79      }
80  
81  }