1   /*
2    * $Id: TcpSyncTestCase.java 11343 2008-03-13 10:58:26Z tcarlson $
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;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.DefaultMuleEvent;
15  import org.mule.DefaultMuleSession;
16  import org.mule.NullSessionHandler;
17  import org.mule.api.MuleMessage;
18  import org.mule.api.endpoint.ImmutableEndpoint;
19  import org.mule.tck.FunctionalTestCase;
20  import org.mule.transport.tcp.TcpConnector;
21  
22  import java.util.Arrays;
23  
24  public class TcpSyncTestCase extends FunctionalTestCase
25  {
26  
27      private static final String endpointUri = "tcp://localhost:45441";
28  
29      protected String getConfigResources()
30      {
31          return "tcp-sync.xml";
32      }
33  
34      protected MuleMessage send(Object payload) throws Exception
35      {
36          MuleMessage message = new DefaultMuleMessage(payload);
37          ImmutableEndpoint endpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(
38              endpointUri);
39          DefaultMuleSession session = new DefaultMuleSession(message, new NullSessionHandler(), muleContext);
40          DefaultMuleEvent event = new DefaultMuleEvent(message, endpoint, session, true);
41          event.setTimeout(60000);
42          return event.getSession().sendEvent(event);
43      }
44  
45      public void testSendString() throws Exception
46      {
47          MuleMessage message = send("data");
48          assertNotNull(message);
49          String response = message.getPayloadAsString();
50          assertEquals("data", response);
51      }
52  
53      public void testSyncResponseOfBufferSize() throws Exception
54      {
55          int size = 1024 * 16;
56          TcpConnector tcp = (TcpConnector)muleContext.getRegistry().lookupConnector("tcpConnector");
57          tcp.setSendBufferSize(size);
58          tcp.setReceiveBufferSize(size);
59          byte[] data = fillBuffer(new byte[size]);
60          MuleMessage message = send(data);
61          assertNotNull(message);
62          byte[] response = message.getPayloadAsBytes();
63          assertEquals(data.length, response.length);
64          assertTrue(Arrays.equals(data, response));
65      }
66  
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      public void testSyncResponseVeryBig() throws Exception
85      {
86          byte[] data = fillBuffer(new byte[1024 * 1024]);
87          MuleMessage message = send(data);
88          assertNotNull(message);
89          byte[] response = message.getPayloadAsBytes();
90          assertEquals(data.length, response.length);
91          assertTrue(Arrays.equals(data, response));
92      }
93  
94      protected byte[] fillBuffer(byte[] buffer)
95      {
96          for (int i = 0; i < buffer.length; ++i)
97          {
98              buffer[i] = (byte) (i % 255);
99          }
100         return buffer;
101     }
102 
103 }