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.integration;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
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 org.junit.Rule;
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertTrue;
21  
22  /**
23   * This test was set for the new changes due to Mule1199
24   */
25  public class MuleMessageProtocolChunkingTestCase extends FunctionalTestCase
26  {
27  
28      public static final long WAIT_MS = 3000L;
29      private static int messages = 2;
30      private static int messagelength = 10;
31  
32      @Rule
33      public DynamicPort dynamicPort = new DynamicPort("port1");
34      
35      @Override
36      protected String getConfigResources()
37      {
38          return "mule-message-protocol-mule-config.xml";
39      }
40  
41      @Test
42      public void testChunking() throws Exception
43      {
44          String message = "";
45          for (int i = 0; i < messagelength; i++)
46          {
47              for (int j = 0; j < 10; j++)
48                  message += i;
49          }
50          sendString(message);
51      }
52  
53      @Test
54      public void testHugeChunk() throws Exception
55      {
56          StringBuffer message = new StringBuffer();
57          // send 50K of stuff;
58          for (int i = 1000; i < 2000; i++)
59          {
60              message.append(i);
61          }
62          sendString(message.toString());
63      }
64  
65      @Test
66      public void testCustomObject() throws Exception
67      {
68          MuleClient client = new MuleClient(muleContext);
69          StringBuffer sBuffer = new StringBuffer();
70          // send 50K of stuff;
71          for (int i = 10000; i < 20000; i++)
72          {
73              sBuffer.append(i);
74          }
75          MessageObject message = new MessageObject(1, sBuffer.toString(), true);
76  
77          for (int i = 0; i < messages; i++)
78          {
79              client.dispatch("vm://in", new DefaultMuleMessage(message, muleContext));
80          }
81  
82          for (int i = 0; i < messages; i++)
83          {
84              MuleMessage msg = client.request("vm://out", WAIT_MS);
85              assertNotNull(msg);
86              assertTrue(msg.getPayload() instanceof MessageObject);
87              MessageObject received = (MessageObject)msg.getPayload();
88              assertEquals(message.s, received.s);
89              assertEquals(1, received.i);
90              assertEquals(true, received.b);
91          }
92      }
93  
94      private void sendString(String message) throws Exception
95      {
96          MuleClient client = new MuleClient(muleContext);
97  
98          for (int i = 0; i < messages; i++)
99          {
100             client.dispatch("vm://in", new DefaultMuleMessage(message, muleContext));
101         }
102         for (int i = 0; i < messages; i++)
103         {
104             MuleMessage msg = client.request("vm://out", WAIT_MS);
105             assertEquals(message, new String((byte[])msg.getPayload()));
106         }
107     }
108 
109 }