1
2
3
4
5
6
7
8
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
65 final int parts = (int) Math.ceil((SerializationUtils.serialize(simpleSerializableObject).length / (double) 2));
66
67
68 muleContext.registerListener(new FunctionalTestNotificationListener()
69 {
70 public void onNotification(ServerNotification notification)
71 {
72
73
74 assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
75
76 Object reply = ((FunctionalTestNotification) notification).getEventContext().getMessage().getPayload();
77
78 assertTrue(reply instanceof SimpleSerializableObject);
79 SimpleSerializableObject replySimpleSerializableObject = (SimpleSerializableObject) reply;
80
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
89
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
105 assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
106
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
116 muleContext.registerListener(new FunctionalTestNotificationListener()
117 {
118 public void onNotification(ServerNotification notification)
119 {
120
121
122 assertEquals("ChunkingReceiver", notification.getResourceIdentifier());
123
124 Object reply = ((FunctionalTestNotification) notification).getReplyMessage();
125 assertEquals(data + " Received", reply);
126 chunkingReceiverLatch.countDown();
127 }
128 }, "ChunkingReceiver");
129
130
131
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
147 assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
148
149 assertEquals(partsCount, messagePartsCount.get());
150 }
151
152 }