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