View Javadoc

1   /*
2    * $Id: AsyncInterceptingMessageProcessorTestCase.java 23144 2011-10-08 08:52:25Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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.MuleEvent;
23  import org.mule.api.MuleException;
24  import org.mule.api.context.WorkManager;
25  import org.mule.api.context.WorkManagerSource;
26  import org.mule.api.exception.MessagingExceptionHandler;
27  import org.mule.api.exception.RollbackSourceCallback;
28  import org.mule.api.processor.MessageProcessor;
29  import org.mule.api.transaction.Transaction;
30  import org.mule.config.i18n.CoreMessages;
31  import org.mule.construct.Flow;
32  import org.mule.routing.filters.WildcardFilter;
33  import org.mule.tck.junit4.AbstractMuleContextTestCase;
34  import org.mule.tck.testmodels.mule.TestTransaction;
35  import org.mule.transaction.TransactionCoordination;
36  import org.mule.util.concurrent.Latch;
37  
38  import java.beans.ExceptionListener;
39  import java.util.concurrent.TimeUnit;
40  
41  import org.junit.Test;
42  
43  public class AsyncInterceptingMessageProcessorTestCase extends AbstractMuleContextTestCase
44      implements ExceptionListener
45  {
46  
47      protected AsyncInterceptingMessageProcessor messageProcessor;
48      protected TestListener target = new TestListener();
49      protected Exception exceptionThrown;
50      protected Latch latch = new Latch();
51  
52      public AsyncInterceptingMessageProcessorTestCase()
53      {
54          setStartContext(true);
55      }
56  
57      @Override
58      protected void doSetUp() throws Exception
59      {
60          super.doSetUp();
61          messageProcessor = createAsyncInterceptingMessageProcessor(target);
62      }
63  
64      @Test
65      public void testProcessOneWay() throws Exception
66      {
67          MuleEvent event = getTestEvent(TEST_MESSAGE, getTestInboundEndpoint(MessageExchangePattern.ONE_WAY));
68  
69          assertAsync(messageProcessor, event);
70      }
71  
72      @Test
73      public void testProcessRequestResponse() throws Exception
74      {
75          MuleEvent event = getTestEvent(TEST_MESSAGE,
76              getTestInboundEndpoint(MessageExchangePattern.REQUEST_RESPONSE));
77  
78          try
79          {
80              messageProcessor.process(event);
81              fail("Exception expect: 'Unable to process a synchonrous event asyncronously'");
82          }
83          catch (Exception e)
84          {
85          }
86      }
87  
88      @Test
89      public void testProcessOneWayWithTx() throws Exception
90      {
91          MuleEvent event = getTestEvent(TEST_MESSAGE,
92              getTestTransactedInboundEndpoint(MessageExchangePattern.ONE_WAY));
93          Transaction transaction = new TestTransaction(muleContext);
94          TransactionCoordination.getInstance().bindTransaction(transaction);
95  
96          try
97          {
98              messageProcessor.process(event);
99              fail("Exception expect: 'Unable to process a synchonrous event asyncronously'");
100         }
101         catch (Exception e)
102         {
103         }
104         finally
105         {
106             TransactionCoordination.getInstance().unbindTransaction(transaction);
107         }
108     }
109 
110     @Test
111     public void testProcessRequestResponseWithTx() throws Exception
112     {
113         MuleEvent event = getTestEvent(TEST_MESSAGE,
114             getTestTransactedInboundEndpoint(MessageExchangePattern.REQUEST_RESPONSE));
115         Transaction transaction = new TestTransaction(muleContext);
116         TransactionCoordination.getInstance().bindTransaction(transaction);
117 
118         try
119         {
120             messageProcessor.process(event);
121             fail("Exception expect: 'Unable to process a synchonrous event asyncronously'");
122         }
123         catch (Exception e)
124         {
125         }
126         finally
127         {
128             TransactionCoordination.getInstance().unbindTransaction(transaction);
129         }
130     }
131 
132     @Test
133     public void testWorkException() throws Exception
134     {
135 
136         Flow flow = new Flow("flow", muleContext);
137         LatchedExceptionListener exceptionListener = new LatchedExceptionListener();
138         flow.setExceptionListener(exceptionListener);
139         initialiseObject(flow);
140 
141         MuleEvent event = getTestEvent(TEST_MESSAGE, flow, MessageExchangePattern.ONE_WAY);
142 
143         MessageProcessor next = new MessageProcessor()
144         {
145             public MuleEvent process(MuleEvent event) throws MuleException
146             {
147                 throw new org.mule.api.expression.RequiredValueException(CoreMessages.createStaticMessage(""));
148             }
149         };
150 
151         messageProcessor.setListener(next);
152 
153         messageProcessor.process(event);
154 
155         assertTrue(exceptionListener.latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
156     }
157 
158     protected void assertSync(MessageProcessor processor, MuleEvent event) throws MuleException
159     {
160         MuleEvent result = processor.process(event);
161 
162         assertSame(event, target.sensedEvent);
163         assertSame(event, result);
164         assertSame(Thread.currentThread(), target.thread);
165     }
166 
167     protected void assertAsync(MessageProcessor processor, MuleEvent event)
168         throws MuleException, InterruptedException
169     {
170         MuleEvent result = processor.process(event);
171 
172         latch.await(10000, TimeUnit.MILLISECONDS);
173         assertNotNull(target.sensedEvent);
174         // Event is not the same because it gets copied in
175         // AbstractMuleEventWork#run()
176         assertNotSame(event, target.sensedEvent);
177         assertEquals(event.getMessageAsString(), target.sensedEvent.getMessageAsString());
178         assertNotSame(Thread.currentThread(), target.thread);
179 
180         assertNull(result);
181         assertNull(exceptionThrown);
182     }
183 
184     protected AsyncInterceptingMessageProcessor createAsyncInterceptingMessageProcessor(MessageProcessor listener)
185         throws Exception
186     {
187         AsyncInterceptingMessageProcessor mp = new AsyncInterceptingMessageProcessor(
188             new TestWorkManagerSource());
189         mp.setListener(listener);
190         return mp;
191     }
192 
193     class TestListener implements MessageProcessor
194     {
195         MuleEvent sensedEvent;
196         Thread thread;
197 
198         @Override
199         public MuleEvent process(MuleEvent event) throws MuleException
200         {
201             thread = Thread.currentThread();
202             sensedEvent = event;
203             latch.countDown();
204             return event;
205         }
206     }
207 
208     @Override
209     public void exceptionThrown(Exception e)
210     {
211         exceptionThrown = e;
212     }
213 
214     class TestWorkManagerSource implements WorkManagerSource
215     {
216         @Override
217         public WorkManager getWorkManager() throws MuleException
218         {
219             return muleContext.getWorkManager();
220         }
221     }
222 
223     private static class LatchedExceptionListener implements MessagingExceptionHandler
224     {
225 
226         Latch latch = new Latch();
227 
228         public WildcardFilter getCommitTxFilter()
229         {
230             return null;
231         }
232 
233         public WildcardFilter getRollbackTxFilter()
234         {
235             return null;
236         }
237 
238         @Override
239         public MuleEvent handleException(Exception exception, MuleEvent event)
240         {
241             latch.countDown();
242             return null;
243         }
244 
245         @Override
246         public MuleEvent handleException(Exception exception,
247                                          MuleEvent event,
248                                          RollbackSourceCallback rollbackMethod)
249         {
250             latch.countDown();
251             return null;
252         }
253 
254     }
255 
256 }