1
2
3
4
5
6
7 package org.mule.test.integration.routing;
8
9
10 import org.mule.api.context.notification.EndpointMessageNotificationListener;
11 import org.mule.api.context.notification.ServerNotification;
12 import org.mule.context.notification.EndpointMessageNotification;
13 import org.mule.module.client.MuleClient;
14 import org.mule.tck.functional.FunctionalTestNotification;
15 import org.mule.tck.functional.FunctionalTestNotificationListener;
16 import org.mule.tck.junit4.FunctionalTestCase;
17 import org.mule.util.concurrent.Latch;
18
19 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
20 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
21 import org.apache.commons.lang.SerializationUtils;
22 import org.junit.Test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
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 @Test
36 public void testMessageChunkingWithEvenSplit() throws Exception
37 {
38 doMessageChunking("0123456789", 5);
39 }
40
41 @Test
42 public void testMessageChunkingWithOddSplit() throws Exception
43 {
44 doMessageChunking("01234567890", 6);
45 }
46
47 @Test
48 public void testMessageChunkingWith100Splits() throws Exception
49 {
50 doMessageChunking("0123456789012345678901234567890123456789012345678901234567890123456789"
51 + "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
52 + "01234567890123456789012345678901234567890123456789", 100);
53 }
54
55 @Test
56 public void testMessageChunkingOneChunk() throws Exception
57 {
58 doMessageChunking("x", 1);
59 }
60
61 @Test
62 public void testMessageChunkingObject() throws Exception
63 {
64 final AtomicInteger messagePartsCount = new AtomicInteger(0);
65 final Latch chunkingReceiverLatch = new Latch();
66 final SimpleSerializableObject simpleSerializableObject = new SimpleSerializableObject("Test String", true, 99);
67
68
69 final int parts = (int) Math.ceil((SerializationUtils.serialize(simpleSerializableObject).length / (double) 2));
70
71
72 muleContext.registerListener(new FunctionalTestNotificationListener()
73 {
74 public void onNotification(ServerNotification notification)
75 {
76
77
78 assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
79
80 Object reply = ((FunctionalTestNotification) notification).getEventContext().getMessage().getPayload();
81
82 assertTrue(reply instanceof SimpleSerializableObject);
83 SimpleSerializableObject replySimpleSerializableObject = (SimpleSerializableObject) reply;
84
85 assertEquals(simpleSerializableObject.b, replySimpleSerializableObject.b);
86 assertEquals(simpleSerializableObject.i, replySimpleSerializableObject.i);
87 assertEquals(simpleSerializableObject.s, replySimpleSerializableObject.s);
88 chunkingReceiverLatch.countDown();
89 }
90 }, "ChunkingObjectReceiver");
91
92
93
94 muleContext.registerListener(new EndpointMessageNotificationListener<EndpointMessageNotification>()
95 {
96 public void onNotification(EndpointMessageNotification notification)
97 {
98 if (notification.getAction() == EndpointMessageNotification.MESSAGE_RECEIVED)
99 {
100 messagePartsCount.getAndIncrement();
101 }
102 assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
103 }
104 }, "ChunkingObjectReceiver");
105
106 MuleClient client = new MuleClient(muleContext);
107 client.dispatch("vm://inbound.object.channel", simpleSerializableObject, null);
108
109 assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
110
111 assertEquals(parts, messagePartsCount.get());
112 }
113
114 protected void doMessageChunking(final String data, int partsCount) throws Exception
115 {
116 final AtomicInteger messagePartsCount = new AtomicInteger(0);
117 final Latch chunkingReceiverLatch = new Latch();
118
119
120 muleContext.registerListener(new FunctionalTestNotificationListener()
121 {
122 public void onNotification(ServerNotification notification)
123 {
124
125
126 assertEquals("ChunkingReceiver", notification.getResourceIdentifier());
127
128
129 Object reply = ((FunctionalTestNotification) notification).getReplyMessage();
130 assertEquals(data + " Received", reply);
131 chunkingReceiverLatch.countDown();
132 }
133 }, "ChunkingReceiver");
134
135
136
137 muleContext.registerListener(new EndpointMessageNotificationListener<EndpointMessageNotification>()
138 {
139 public void onNotification(EndpointMessageNotification notification)
140 {
141 if (notification.getAction() == EndpointMessageNotification.MESSAGE_RECEIVED)
142 {
143 messagePartsCount.getAndIncrement();
144 }
145 assertEquals("ChunkingReceiver", notification.getResourceIdentifier());
146 }
147 }, "ChunkingReceiver");
148
149 MuleClient client = new MuleClient(muleContext);
150 client.dispatch("vm://inbound.channel", data, null);
151
152 assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
153
154 assertEquals(partsCount, messagePartsCount.get());
155 }
156 }