View Javadoc

1   /*
2    * $Id: AsyncInterceptingMessageProcessorTestCase.java 20320 2010-11-24 15:03:31Z 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 org.mule.MessageExchangePattern;
14  import org.mule.api.MessagingException;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleException;
17  import org.mule.api.context.WorkManager;
18  import org.mule.api.context.WorkManagerSource;
19  import org.mule.api.processor.MessageProcessor;
20  import org.mule.api.transaction.Transaction;
21  import org.mule.tck.AbstractMuleTestCase;
22  import org.mule.tck.testmodels.mule.TestTransaction;
23  import org.mule.transaction.TransactionCoordination;
24  import org.mule.util.concurrent.Latch;
25  
26  import java.beans.ExceptionListener;
27  
28  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
29  
30  public class AsyncInterceptingMessageProcessorTestCase extends AbstractMuleTestCase
31      implements ExceptionListener
32  {
33  
34      protected MessageProcessor messageProcessor;
35      protected TestListener target = new TestListener();
36      protected Exception exceptionThrown;
37      protected Latch latch = new Latch();
38  
39      public AsyncInterceptingMessageProcessorTestCase()
40      {
41          setStartContext(true);
42      }
43  
44      @Override
45      protected void doSetUp() throws Exception
46      {
47          super.doSetUp();
48          messageProcessor = createAsyncInterceptingMessageProcessor(target);
49      }
50  
51      public void testProcessOneWay() throws Exception
52      {
53          MuleEvent event = getTestEvent(TEST_MESSAGE, getTestInboundEndpoint(MessageExchangePattern.ONE_WAY));
54  
55          MuleEvent result = messageProcessor.process(event);
56  
57          latch.await(10000, TimeUnit.MILLISECONDS);
58          assertNotNull(target.sensedEvent);
59          // Event is not the same because it gets copied in
60          // AbstractMuleEventWork#run()
61          assertNotSame(event, target.sensedEvent);
62          assertEquals(event.getMessageAsString(), target.sensedEvent.getMessageAsString());
63  
64          assertNull(result);
65          assertNull(exceptionThrown);
66      }
67  
68      public void testProcessRequestResponse() throws Exception
69      {
70          MuleEvent event = getTestEvent(TEST_MESSAGE, getTestInboundEndpoint(MessageExchangePattern.ONE_WAY));
71  
72          assertAsync(messageProcessor, event);
73      }
74  
75      public void testProcessOneWayWithTx() throws Exception
76      {
77          MuleEvent event = getTestEvent(TEST_MESSAGE,
78              getTestTransactedInboundEndpoint(MessageExchangePattern.ONE_WAY));
79          Transaction transaction = new TestTransaction(muleContext);
80          TransactionCoordination.getInstance().bindTransaction(transaction);
81  
82          try
83          {
84              messageProcessor.process(event);
85              fail("Exception expected");
86          }
87          catch (Exception e)
88          {
89              assertTrue(e instanceof MessagingException);
90              assertNull(target.sensedEvent);
91          }
92          finally
93          {
94              TransactionCoordination.getInstance().unbindTransaction(transaction);
95          }
96      }
97  
98      public void testProcessRequestResponseWithTx() throws Exception
99      {
100         MuleEvent event = getTestEvent(TEST_MESSAGE,
101             getTestTransactedInboundEndpoint(MessageExchangePattern.REQUEST_RESPONSE));
102         Transaction transaction = new TestTransaction(muleContext);
103         TransactionCoordination.getInstance().bindTransaction(transaction);
104 
105         try
106         {
107             messageProcessor.process(event);
108             fail("Exception expected");
109         }
110         catch (Exception e)
111         {
112             assertTrue(e instanceof MessagingException);
113             assertNull(target.sensedEvent);
114         }
115         finally
116         {
117             TransactionCoordination.getInstance().unbindTransaction(transaction);
118         }
119     }
120 
121     protected void assertSync(MessageProcessor processor, MuleEvent event) throws MuleException
122     {
123         MuleEvent result = processor.process(event);
124 
125         assertSame(event, target.sensedEvent);
126         assertSame(event, result);
127     }
128 
129     protected void assertAsync(MessageProcessor processor, MuleEvent event)
130         throws MuleException, InterruptedException
131     {
132         MuleEvent result = processor.process(event);
133 
134         latch.await(10000, TimeUnit.MILLISECONDS);
135         assertNotNull(target.sensedEvent);
136         // Event is not the same because it gets copied in
137         // AbstractMuleEventWork#run()
138         assertNotSame(event, target.sensedEvent);
139         assertEquals(event.getMessageAsString(), target.sensedEvent.getMessageAsString());
140 
141         assertNull(result);
142         assertNull(exceptionThrown);
143     }
144 
145     protected AsyncInterceptingMessageProcessor createAsyncInterceptingMessageProcessor(MessageProcessor listener)
146         throws Exception
147     {
148         AsyncInterceptingMessageProcessor mp = new AsyncInterceptingMessageProcessor(
149             new TestWorkManagerSource(), true);
150         mp.setListener(listener);
151         return mp;
152     }
153 
154     class TestListener implements MessageProcessor
155     {
156         MuleEvent sensedEvent;
157 
158         public MuleEvent process(MuleEvent event) throws MuleException
159         {
160             sensedEvent = event;
161             latch.countDown();
162             return event;
163         }
164     }
165 
166     public void exceptionThrown(Exception e)
167     {
168         exceptionThrown = e;
169     }
170 
171     class TestWorkManagerSource implements WorkManagerSource
172     {
173         public WorkManager getWorkManager() throws MuleException
174         {
175             return muleContext.getWorkManager();
176         }
177     }
178 
179 }