View Javadoc

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