View Javadoc

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