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;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.endpoint.InboundEndpoint;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.tck.junit4.rule.DynamicPort;
14  
15  import java.util.Arrays;
16  
17  import org.junit.Rule;
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  public class TcpSyncTestCase extends FunctionalTestCase
25  {
26  
27      @Rule
28      public DynamicPort dynamicPort = new DynamicPort("port1");
29      
30      @Override
31      protected String getConfigResources()
32      {
33          return "tcp-sync.xml";
34      }
35  
36      protected MuleMessage send(Object payload) throws Exception
37      {
38          MuleClient client = new MuleClient(muleContext);
39          return client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inService")).getAddress(), payload, null);
40      }
41  
42      @Test
43      public void testSendString() throws Exception
44      {
45          MuleMessage message = send("data");
46          assertNotNull(message);
47          String response = message.getPayloadAsString();
48          assertEquals("data", response);
49      }
50  
51      @Test
52      public void testSyncResponseOfBufferSize() throws Exception
53      {
54          int size = 1024 * 16;
55          TcpConnector tcp = (TcpConnector)muleContext.getRegistry().lookupConnector("tcpConnector");
56          tcp.setSendBufferSize(size);
57          tcp.setReceiveBufferSize(size);
58          byte[] data = fillBuffer(new byte[size]);
59          MuleMessage message = send(data);
60          assertNotNull(message);
61          byte[] response = message.getPayloadAsBytes();
62          assertEquals(data.length, response.length);
63          assertTrue(Arrays.equals(data, response));
64      }
65  
66      @Test
67      public void testManySyncResponseOfBufferSize() throws Exception
68      {
69          int size = 1024 * 16;
70          TcpConnector tcp = (TcpConnector)muleContext.getRegistry().lookupConnector("tcpConnector");
71          tcp.setSendBufferSize(size);
72          tcp.setReceiveBufferSize(size);
73          byte[] data = fillBuffer(new byte[size]);
74          for (int i = 0; i < 20; ++i)
75          {
76              MuleMessage message = send(data);
77              assertNotNull(message);
78              byte[] response = message.getPayloadAsBytes();
79              assertEquals(data.length, response.length);
80              assertTrue(Arrays.equals(data, response));
81          }
82      }
83  
84      @Test
85      public void testSyncResponseVeryBig() throws Exception
86      {
87          byte[] data = fillBuffer(new byte[1024 * 1024]);
88          MuleMessage message = send(data);
89          assertNotNull(message);
90          byte[] response = message.getPayloadAsBytes();
91          assertEquals(data.length, response.length);
92          assertTrue(Arrays.equals(data, response));
93      }
94  
95      protected byte[] fillBuffer(byte[] buffer)
96      {
97          for (int i = 0; i < buffer.length; ++i)
98          {
99              buffer[i] = (byte) (i % 255);
100         }
101         return buffer;
102     }
103 
104 }