View Javadoc

1   /*
2    * $Id: TcpSyncTestCase.java 22450 2011-07-19 08:20:41Z dirk.olmes $
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.AbstractServiceAndFlowTestCase;
17  import org.mule.tck.junit4.rule.DynamicPort;
18  
19  import java.util.Arrays;
20  import java.util.Collection;
21  
22  import org.junit.Rule;
23  import org.junit.Test;
24  import org.junit.runners.Parameterized.Parameters;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertTrue;
29  
30  public class TcpSyncTestCase extends AbstractServiceAndFlowTestCase
31  {
32      @Rule
33      public DynamicPort port1 = new DynamicPort("port1");
34      
35      @Parameters
36      public static Collection<Object[]> parameters()
37      {
38          return Arrays.asList(new Object[][]{
39              {ConfigVariant.SERVICE, "tcp-sync-service.xml"},
40              {ConfigVariant.FLOW, "tcp-sync-flow.xml"}
41          });
42      }
43  
44      public TcpSyncTestCase(ConfigVariant variant, String configResources)
45      {
46          super(variant, configResources);
47      }
48  
49      protected MuleMessage send(Object payload) throws Exception
50      {
51          MuleClient client = new MuleClient(muleContext);
52          return client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inService")).getAddress(), payload, null);
53      }
54  
55      @Test
56      public void testSendString() throws Exception
57      {
58          MuleMessage message = send("data");
59          assertNotNull(message);
60          String response = message.getPayloadAsString();
61          assertEquals("data", response);
62      }
63  
64      @Test
65      public void testSyncResponseOfBufferSize() throws Exception
66      {
67          int size = 1024 * 16;
68          TcpConnector tcp = (TcpConnector)muleContext.getRegistry().lookupConnector("tcpConnector");
69          tcp.setSendBufferSize(size);
70          tcp.setReceiveBufferSize(size);
71          byte[] data = fillBuffer(new byte[size]);
72          MuleMessage message = send(data);
73          assertNotNull(message);
74          byte[] response = message.getPayloadAsBytes();
75          assertEquals(data.length, response.length);
76          assertTrue(Arrays.equals(data, response));
77      }
78  
79      @Test
80      public void testManySyncResponseOfBufferSize() throws Exception
81      {
82          int size = 1024 * 16;
83          TcpConnector tcp = (TcpConnector)muleContext.getRegistry().lookupConnector("tcpConnector");
84          tcp.setSendBufferSize(size);
85          tcp.setReceiveBufferSize(size);
86          byte[] data = fillBuffer(new byte[size]);
87          for (int i = 0; i < 20; ++i)
88          {
89              MuleMessage message = send(data);
90              assertNotNull(message);
91              byte[] response = message.getPayloadAsBytes();
92              assertEquals(data.length, response.length);
93              assertTrue(Arrays.equals(data, response));
94          }
95      }
96  
97      @Test
98      public void testSyncResponseVeryBig() throws Exception
99      {
100         byte[] data = fillBuffer(new byte[1024 * 1024]);
101         MuleMessage message = send(data);
102         assertNotNull(message);
103         byte[] response = message.getPayloadAsBytes();
104         assertEquals(data.length, response.length);
105         assertTrue(Arrays.equals(data, response));
106     }
107 
108     protected byte[] fillBuffer(byte[] buffer)
109     {
110         for (int i = 0; i < buffer.length; ++i)
111         {
112             buffer[i] = (byte) (i % 255);
113         }
114         return buffer;
115     }
116 }