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 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
64 final int parts = (int) Math.ceil((SerializationUtils.serialize(simpleSerializableObject).length / (double) 2));
65
66
67 muleContext.registerListener(new FunctionalTestNotificationListener()
68 {
69 public void onNotification(ServerNotification notification)
70 {
71
72
73 assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
74
75 Object reply = ((FunctionalTestNotification) notification).getEventContext().getMessage().getPayload();
76
77 assertTrue(reply instanceof SimpleSerializableObject);
78 SimpleSerializableObject replySimpleSerializableObject = (SimpleSerializableObject) reply;
79
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
88
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
104 assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
105
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
115 muleContext.registerListener(new FunctionalTestNotificationListener()
116 {
117 public void onNotification(ServerNotification notification)
118 {
119
120
121 assertEquals("ChunkingReceiver", notification.getResourceIdentifier());
122
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<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
147 assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
148
149 assertEquals(partsCount, messagePartsCount.get());
150 }
151 }