1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.construct;
12
13 import org.mule.api.DefaultMuleException;
14 import org.mule.api.MuleEvent;
15 import org.mule.api.MuleException;
16 import org.mule.api.MuleMessage;
17 import org.mule.api.client.MuleClient;
18 import org.mule.api.construct.FlowConstruct;
19 import org.mule.api.endpoint.InboundEndpoint;
20 import org.mule.api.endpoint.OutboundEndpoint;
21 import org.mule.api.lifecycle.CreateException;
22 import org.mule.api.processor.MessageProcessor;
23 import org.mule.api.transport.Connector;
24 import org.mule.api.transport.MessageDispatcher;
25 import org.mule.tck.FunctionalTestCase;
26 import org.mule.transport.vm.VMMessageDispatcher;
27 import org.mule.transport.vm.VMMessageDispatcherFactory;
28 import org.mule.transport.vm.VMMessageReceiver;
29
30 public class FlowDefaultProcessingStrategyTestCase extends FunctionalTestCase
31 {
32
33 @Override
34 protected String getConfigResources()
35 {
36 return "org/mule/test/construct/flow-default-processing-strategy-config.xml";
37 }
38
39 public void testDispatchToOneWayInbound() throws Exception
40 {
41 MuleClient client = muleContext.getClient();
42 client.dispatch("vm://oneway-in", "a", null);
43
44 MuleMessage result = client.request("vm://oneway-out", RECEIVE_TIMEOUT);
45
46 assertAllProcessingAsync(result);
47 }
48
49 public void testSendToOneWayInbound() throws Exception
50 {
51 MuleClient client = muleContext.getClient();
52 MuleMessage response = client.send("vm://oneway-in", "a", null);
53
54 assertNull(response);
55
56 MuleMessage result = client.request("vm://oneway-out", RECEIVE_TIMEOUT);
57
58 assertNotNull(result);
59
60 String receiverThread = result.getInboundProperty("receiver-thread");
61 String flowThread = result.getInboundProperty("processor-thread");
62 String dispatcherThread = result.getInboundProperty("dispatcher-thread");
63
64 assertEquals(Thread.currentThread().getName(), receiverThread);
65 assertFalse(receiverThread.equals(flowThread));
66 assertFalse(flowThread.equals(dispatcherThread));
67 }
68
69 public void testDispatchToOneWayTx() throws Exception
70 {
71 MuleClient client = muleContext.getClient();
72 client.dispatch("vm://oneway-tx-in", "a", null);
73
74 MuleMessage result = client.request("vm://oneway-tx-out", RECEIVE_TIMEOUT);
75
76 assertAllProcessingInRecieverThread(result);
77 }
78
79 public void testSendToOneWayTx() throws Exception
80 {
81 MuleClient client = muleContext.getClient();
82 MuleMessage response = client.send("vm://oneway-tx-in", "a", null);
83
84 assertNull(response);
85
86 MuleMessage result = client.request("vm://oneway-tx-out", RECEIVE_TIMEOUT);
87 assertAllProcessingInClientThread(result);
88 }
89
90
91 public void testDispatchToOneWayInboundTxOnly() throws Exception
92 {
93 MuleClient client = muleContext.getClient();
94 client.dispatch("vm://oneway-inboundtx-in", "a", null);
95
96 MuleMessage result = client.request("vm://oneway-inboundtx-out", RECEIVE_TIMEOUT);
97
98 assertAllProcessingInRecieverThread(result);
99 }
100
101 public void testDispatchToOneWayOutboundTxOnly() throws Exception
102 {
103 MuleClient client = muleContext.getClient();
104 client.dispatch("vm://oneway-outboundtx-in", "a", null);
105
106 MuleMessage result = client.request("vm://oneway-outboundtx-out", RECEIVE_TIMEOUT);
107
108 assertAllProcessingAsync(result);
109 }
110
111 public void testSendRequestResponseInbound() throws Exception
112 {
113 MuleClient client = muleContext.getClient();
114 MuleMessage response = client.send("vm://requestresponse-in", "a", null);
115
116 assertAllProcessingInClientThread(response);
117 }
118
119 public void testDispatchToRequestResponseInboundOneWayOutbound() throws Exception
120 {
121 MuleClient client = muleContext.getClient();
122
123 client.dispatch("vm://requestresponse-oneway-in", "a", null);
124
125
126 assertNull(client.request("vm://requestresponse-oneway-out", RECEIVE_TIMEOUT));
127 }
128
129 public void testSendToRequestResponseInboundOneWayOutbound() throws Exception
130 {
131 MuleClient client = muleContext.getClient();
132
133 MuleMessage response = client.send("vm://requestresponse-oneway-in", "a", null);
134 assertNull(response);
135
136 MuleMessage result = client.request("vm://requestresponse-oneway-out", RECEIVE_TIMEOUT);
137
138 assertAllProcessingInClientThread(result);
139 }
140
141 protected void assertAllProcessingInClientThread(MuleMessage result)
142 {
143 assertSync(result);
144 assertEquals(Thread.currentThread().getName(), result.getInboundProperty("receiver-thread"));
145 }
146
147 protected void assertAllProcessingInRecieverThread(MuleMessage result)
148 {
149 assertSync(result);
150 assertTrue(((String) result.getInboundProperty("receiver-thread")).startsWith("vm.receiver"));
151 }
152
153 protected void assertSync(MuleMessage result)
154 {
155 assertNotNull(result);
156
157 String receiverThread = result.getInboundProperty("receiver-thread");
158 String flowThread = result.getInboundProperty("processor-thread");
159 String dispatcherThread = result.getInboundProperty("dispatcher-thread");
160
161 assertEquals(receiverThread, flowThread);
162 assertEquals(flowThread, dispatcherThread);
163 }
164
165 protected void assertAllProcessingAsync(MuleMessage result)
166 {
167 assertNotNull(result);
168
169 String receiverThread = result.getInboundProperty("receiver-thread");
170 String flowThread = result.getInboundProperty("processor-thread");
171 String dispatcherThread = result.getInboundProperty("dispatcher-thread");
172
173 assertTrue(receiverThread.startsWith("vm.receiver"));
174 assertFalse(receiverThread.equals(flowThread));
175 assertFalse(flowThread.equals(dispatcherThread));
176 assertFalse(receiverThread.equals(dispatcherThread));
177 }
178
179 public void testRequestResponseInboundFailingOneWayOutbound() throws Exception
180 {
181 MuleClient client = muleContext.getClient();
182
183 try
184 {
185 MuleMessage response = client.send("vm://requestresponse-failingoneway-in", "a", null);
186 fail("exception expected");
187 }
188 catch (Exception e)
189 {
190
191 }
192 }
193
194 public static class ThreadSensingMessageProcessor implements MessageProcessor
195 {
196 @Override
197 public MuleEvent process(MuleEvent event) throws MuleException
198 {
199 event.getMessage().setOutboundProperty("processor-thread", Thread.currentThread().getName());
200 return event;
201 }
202 }
203
204 public static class ThreadSensingVMMessageDispatcherFactory extends VMMessageDispatcherFactory
205 {
206
207 @Override
208 public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException
209 {
210 return new ThreadSensingVMMessageDispatcher(endpoint);
211 }
212 }
213
214 public static class ThreadSensingVMMessageDispatcher extends VMMessageDispatcher
215 {
216 public ThreadSensingVMMessageDispatcher(OutboundEndpoint endpoint)
217 {
218 super(endpoint);
219 }
220
221 @Override
222 protected void doDispatch(MuleEvent event) throws Exception
223 {
224 event.getMessage().setOutboundProperty("dispatcher-thread", Thread.currentThread().getName());
225 super.doDispatch(event);
226 }
227
228 @Override
229 protected MuleMessage doSend(MuleEvent event) throws Exception
230 {
231 event.getMessage().setOutboundProperty("dispatcher-thread", Thread.currentThread().getName());
232 return super.doSend(event);
233 }
234 }
235
236 public static class ThreadSensingVMMessageReceiver extends VMMessageReceiver
237 {
238
239 public ThreadSensingVMMessageReceiver(Connector connector,
240 FlowConstruct flowConstruct,
241 InboundEndpoint endpoint) throws CreateException
242 {
243 super(connector, flowConstruct, endpoint);
244 }
245
246 public MuleMessage onCall(MuleMessage message) throws MuleException
247 {
248 try
249 {
250 message.setOutboundProperty("receiver-thread", Thread.currentThread().getName());
251 MuleEvent event = routeMessage(message);
252 MuleMessage returnedMessage = event == null ? null : event.getMessage();
253
254
255
256 return returnedMessage;
257 }
258 catch (Exception e)
259 {
260 throw new DefaultMuleException(e);
261 }
262 }
263
264 @Override
265 protected void processMessage(Object msg) throws Exception
266 {
267 MuleMessage message = (MuleMessage) msg;
268
269
270 MuleMessage newMessage = message.createInboundMessage();
271 newMessage.setOutboundProperty("receiver-thread", Thread.currentThread().getName());
272 routeMessage(newMessage);
273 }
274
275 }
276
277 }