1   /*
2    * $Id: MessageChunkingTestCase.java 12247 2008-07-07 21:25:01Z dfeist $
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.test.integration.routing;
12  
13  
14  import org.mule.api.context.notification.EndpointMessageNotificationListener;
15  import org.mule.api.context.notification.ServerNotification;
16  import org.mule.context.notification.EndpointMessageNotification;
17  import org.mule.module.client.MuleClient;
18  import org.mule.tck.FunctionalTestCase;
19  import org.mule.tck.functional.FunctionalTestNotification;
20  import org.mule.tck.functional.FunctionalTestNotificationListener;
21  import org.mule.util.concurrent.Latch;
22  
23  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
24  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
25  
26  import org.apache.commons.lang.SerializationUtils;
27  
28  public class MessageChunkingTestCase extends FunctionalTestCase
29  {
30  
31      protected String getConfigResources()
32      {
33          return "org/mule/test/integration/routing/message-chunking.xml";
34      }
35  
36      public void testMessageChunkingWithEvenSplit() throws Exception
37      {
38          doMessageChunking("0123456789", 5);
39      }
40  
41      public void testMessageChunkingWithOddSplit() throws Exception
42      {
43          doMessageChunking("01234567890", 6);
44      }
45  
46      public void testMessageChunkingWith100Splits() throws Exception
47      {
48          doMessageChunking("0123456789012345678901234567890123456789012345678901234567890123456789"
49                            + "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
50                            + "01234567890123456789012345678901234567890123456789", 100);
51      }
52  
53      public void testMessageChunkingOneChunk() throws Exception
54      {
55          doMessageChunking("x", 1);
56      }
57  
58      public void testMessageChunkingObject() throws Exception
59      {
60          final AtomicInteger messagePartsCount = new AtomicInteger(0);
61          final Latch chunkingReceiverLatch = new Latch();
62          final SimpleSerializableObject simpleSerializableObject = new SimpleSerializableObject("Test String", true, 99);
63  
64          // find number of chunks
65          final int parts = (int) Math.ceil((SerializationUtils.serialize(simpleSerializableObject).length / (double) 2));
66  
67          // Listen to events fired by the ChunkingReceiver service
68          muleContext.registerListener(new FunctionalTestNotificationListener()
69          {
70              public void onNotification(ServerNotification notification)
71              {
72                  // Not strictly necessary to test for this as when we register the
73                  // listener we supply the ComponentName as the subscription filter
74                  assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
75                  // Test that we have received all chunks in the correct order
76                  Object reply = ((FunctionalTestNotification) notification).getEventContext().getMessage().getPayload();
77                  // Check if Object is of Correct Type
78                  assertTrue(reply instanceof SimpleSerializableObject);
79                  SimpleSerializableObject replySimpleSerializableObject = (SimpleSerializableObject) reply;
80                  // Check that Contents are Identical
81                  assertEquals(simpleSerializableObject.b, replySimpleSerializableObject.b);
82                  assertEquals(simpleSerializableObject.i, replySimpleSerializableObject.i);
83                  assertEquals(simpleSerializableObject.s, replySimpleSerializableObject.s);
84                  chunkingReceiverLatch.countDown();
85              }
86          }, "ChunkingObjectReceiver");
87  
88          // Listen to Message Notifications on the Chunking receiver so we can
89          // determine how many message parts have been received
90          muleContext.registerListener(new EndpointMessageNotificationListener()
91          {
92              public void onNotification(ServerNotification notification)
93              {
94                  if (notification.getAction() == EndpointMessageNotification.MESSAGE_RECEIVED)
95                  {
96                      messagePartsCount.getAndIncrement();
97                  }
98                  assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
99              }
100         }, "ChunkingObjectReceiver");
101 
102         MuleClient client = new MuleClient();
103         client.dispatch("vm://inbound.object.channel", simpleSerializableObject, null);
104         // Wait for the message to be received and tested (in the listener above)
105         assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
106         // Ensure we processed expected number of message parts
107         assertEquals(parts, messagePartsCount.get());
108     }
109 
110     protected void doMessageChunking(final String data, int partsCount) throws Exception
111     {
112         final AtomicInteger messagePartsCount = new AtomicInteger(0);
113         final Latch chunkingReceiverLatch = new Latch();
114 
115         // Listen to events fired by the ChunkingReceiver service
116         muleContext.registerListener(new FunctionalTestNotificationListener()
117         {
118             public void onNotification(ServerNotification notification)
119             {
120                 // Not strictly necessary to test for this as when we register the
121                 // listener we supply the ComponentName as the subscription filter
122                 assertEquals("ChunkingReceiver", notification.getResourceIdentifier());
123                 // Test that we have received all chunks in the correct order
124                 Object reply = ((FunctionalTestNotification) notification).getReplyMessage();
125                 assertEquals(data + " Received", reply);
126                 chunkingReceiverLatch.countDown();
127             }
128         }, "ChunkingReceiver");
129 
130         // Listen to Message Notifications on the Chunking receiver so we can
131         // determine how many message parts have been received
132         muleContext.registerListener(new EndpointMessageNotificationListener()
133         {
134             public void onNotification(ServerNotification notification)
135             {
136                 if (notification.getAction() == EndpointMessageNotification.MESSAGE_RECEIVED)
137                 {
138                     messagePartsCount.getAndIncrement();
139                 }
140                 assertEquals("ChunkingReceiver", notification.getResourceIdentifier());
141             }
142         }, "ChunkingReceiver");
143 
144         MuleClient client = new MuleClient();
145         client.dispatch("vm://inbound.channel", data, null);
146         // Wait for the message to be received and tested (in the listener above)
147         assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
148         // Ensure we processed expected number of message parts
149         assertEquals(partsCount, messagePartsCount.get());
150     }
151 
152 }