View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.tcp.issues;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  import org.mule.tck.junit4.rule.DynamicPort;
13  
14  import edu.emory.mathcs.backport.java.util.Arrays;
15  import org.junit.Rule;
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertTrue;
20  import static org.junit.Assert.fail;
21  
22  public class LengthProtocolLengthTestCase extends FunctionalTestCase
23  {
24  
25      @Rule
26      public DynamicPort dynamicPort1 = new DynamicPort("port1");
27  
28      @Rule
29      public DynamicPort dynamicPort2 = new DynamicPort("port2");
30  
31      @Override
32      protected String getConfigResources()
33      {
34          return "length-protocol-length-test.xml";
35      }
36  
37      @Test
38      public void testLength() throws Exception
39      {
40          doTest("length", 5, true);
41          doTest("length", 15, false);
42      }
43  
44      @Test
45      public void testSafe() throws Exception
46      {
47          doTest("safe", 5, true);
48          doTest("safe", 15, false);
49      }
50  
51      protected void doTest(String endpoint, int length, boolean ok) throws Exception
52      {
53          byte[] message = new byte[length];
54          for (int i = 0; i < length; ++i)
55          {
56              message[i] = (byte)(i % 255);
57          }
58          MuleClient client = new MuleClient(muleContext);
59          if (ok)
60          {
61              MuleMessage response = client.send(endpoint, message, null);
62              assertNotNull(response);
63              assertNotNull(response.getPayload());
64              assertTrue(Arrays.equals(message, response.getPayloadAsBytes()));
65          }
66          else
67          {
68              assertResponseBad(client.send(endpoint, message, null));
69          }
70      }
71  
72      protected void assertResponseBad(MuleMessage message)
73      {
74          try
75          {
76              if (message.getPayloadAsString().equals(TEST_MESSAGE + " Received"))
77              {
78                  fail("expected error");
79              }
80          }
81          catch (Exception e)
82          {
83              // expected
84          }
85      }
86  
87  }