1
2
3
4
5
6
7
8
9
10
11 package org.mule.processor;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNotSame;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertSame;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20
21 import org.mule.MessageExchangePattern;
22 import org.mule.api.MessagingException;
23 import org.mule.api.MuleEvent;
24 import org.mule.api.MuleException;
25 import org.mule.api.context.WorkManager;
26 import org.mule.api.context.WorkManagerSource;
27 import org.mule.api.processor.MessageProcessor;
28 import org.mule.api.transaction.Transaction;
29 import org.mule.construct.Flow;
30 import org.mule.processor.strategy.AsynchronousProcessingStrategy;
31 import org.mule.tck.junit4.AbstractMuleContextTestCase;
32 import org.mule.tck.testmodels.mule.TestTransaction;
33 import org.mule.transaction.TransactionCoordination;
34 import org.mule.util.concurrent.Latch;
35
36 import java.beans.ExceptionListener;
37 import java.util.concurrent.TimeUnit;
38
39 import org.junit.Test;
40
41 public class AsyncDelegateMessageProcessorTestCase extends AbstractMuleContextTestCase
42 implements ExceptionListener
43 {
44
45 protected AsyncDelegateMessageProcessor messageProcessor;
46 protected TestListener target = new TestListener();
47 protected Exception exceptionThrown;
48 protected Latch latch = new Latch();
49
50 public AsyncDelegateMessageProcessorTestCase()
51 {
52 setStartContext(true);
53 }
54
55 @Override
56 protected void doSetUp() throws Exception
57 {
58 super.doSetUp();
59 messageProcessor = createAsyncDelegatMessageProcessor(target);
60 messageProcessor.initialise();
61 messageProcessor.start();
62 }
63
64 @Test
65 public void testProcessOneWay() throws Exception
66 {
67 MuleEvent event = getTestEvent(TEST_MESSAGE, getTestInboundEndpoint(MessageExchangePattern.ONE_WAY));
68
69 MuleEvent result = messageProcessor.process(event);
70
71 latch.await(10000, TimeUnit.MILLISECONDS);
72 assertNotNull(target.sensedEvent);
73
74
75 assertNotSame(event, target.sensedEvent);
76 assertEquals(event.getMessageAsString(), target.sensedEvent.getMessageAsString());
77
78 assertSame(event, result);
79 assertNull(exceptionThrown);
80 assertNotSame(Thread.currentThread(), target.thread);
81 }
82
83 @Test
84 public void testProcessRequestResponse() throws Exception
85 {
86 MuleEvent event = getTestEvent(TEST_MESSAGE,
87 getTestInboundEndpoint(MessageExchangePattern.REQUEST_RESPONSE));
88
89 MuleEvent result = messageProcessor.process(event);
90
91 latch.await(10000, TimeUnit.MILLISECONDS);
92 assertNotNull(target.sensedEvent);
93
94
95 assertNotSame(event, target.sensedEvent);
96 assertEquals(event.getMessageAsString(), target.sensedEvent.getMessageAsString());
97
98 assertSame(event, result);
99 assertNull(exceptionThrown);
100 assertNotSame(Thread.currentThread(), target.thread);
101 }
102
103 @Test
104 public void testProcessOneWayWithTx() throws Exception
105 {
106 MuleEvent event = getTestEvent(TEST_MESSAGE,
107 getTestTransactedInboundEndpoint(MessageExchangePattern.ONE_WAY));
108 Transaction transaction = new TestTransaction(muleContext);
109 TransactionCoordination.getInstance().bindTransaction(transaction);
110
111 try
112 {
113 messageProcessor.process(event);
114 fail("Exception expected");
115 }
116 catch (Exception e)
117 {
118 assertTrue(e instanceof MessagingException);
119 assertNull(target.sensedEvent);
120 }
121 finally
122 {
123 TransactionCoordination.getInstance().unbindTransaction(transaction);
124 }
125 }
126
127 @Test
128 public void testProcessRequestResponseWithTx() throws Exception
129 {
130 MuleEvent event = getTestEvent(TEST_MESSAGE,
131 getTestTransactedInboundEndpoint(MessageExchangePattern.REQUEST_RESPONSE));
132 Transaction transaction = new TestTransaction(muleContext);
133 TransactionCoordination.getInstance().bindTransaction(transaction);
134
135 try
136 {
137 assertAsync(messageProcessor, event);
138 fail("Exception expected");
139 }
140 catch (Exception e)
141 {
142 }
143 finally
144 {
145 TransactionCoordination.getInstance().unbindTransaction(transaction);
146 }
147 }
148
149 protected void assertSync(MessageProcessor processor, MuleEvent event) throws MuleException
150 {
151 MuleEvent result = processor.process(event);
152
153 assertSame(event, target.sensedEvent);
154 assertSame(event, result);
155 }
156
157 protected void assertAsync(MessageProcessor processor, MuleEvent event)
158 throws MuleException, InterruptedException
159 {
160 MuleEvent result = processor.process(event);
161
162 latch.await(10000, TimeUnit.MILLISECONDS);
163 assertNotNull(target.sensedEvent);
164
165
166 assertNotSame(event, target.sensedEvent);
167 assertEquals(event.getMessageAsString(), target.sensedEvent.getMessageAsString());
168
169 assertNull(result);
170 assertNull(exceptionThrown);
171 }
172
173 protected AsyncDelegateMessageProcessor createAsyncDelegatMessageProcessor(MessageProcessor listener)
174 throws Exception
175 {
176 AsyncDelegateMessageProcessor mp = new AsyncDelegateMessageProcessor(listener,
177 new AsynchronousProcessingStrategy(), "thread");
178 mp.setMuleContext(muleContext);
179 mp.setFlowConstruct(new Flow("flow", muleContext));
180 mp.initialise();
181 return mp;
182 }
183
184 class TestListener implements MessageProcessor
185 {
186 MuleEvent sensedEvent;
187 Thread thread;
188
189 @Override
190 public MuleEvent process(MuleEvent event) throws MuleException
191 {
192 sensedEvent = event;
193 thread = Thread.currentThread();
194 latch.countDown();
195 return event;
196 }
197 }
198
199 @Override
200 public void exceptionThrown(Exception e)
201 {
202 exceptionThrown = e;
203 }
204
205 class TestWorkManagerSource implements WorkManagerSource
206 {
207 @Override
208 public WorkManager getWorkManager() throws MuleException
209 {
210 return muleContext.getWorkManager();
211 }
212 }
213
214 }