View Javadoc

1   /*
2    * $Id: FlowAsyncBeforeAfterOutboundTestCase.java 22695 2011-08-17 12:13:51Z justin.calleja $
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.test.construct;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  
17  import org.junit.Test;
18  import org.mule.api.MuleEvent;
19  import org.mule.api.MuleException;
20  import org.mule.api.MuleMessage;
21  import org.mule.api.processor.MessageProcessor;
22  import org.mule.module.client.MuleClient;
23  import org.mule.tck.junit4.FunctionalTestCase;
24  
25  public class FlowAsyncBeforeAfterOutboundTestCase extends FunctionalTestCase
26  {
27  
28      @Override
29      protected String getConfigResources()
30      {
31          return "org/mule/test/construct/flow-async-before-after-outbound.xml";
32      }
33  
34      @Test
35      public void testAsyncBefore() throws Exception
36      {
37          MuleClient client = new MuleClient(muleContext);
38          
39          MuleMessage msgSync = client.send("vm://test.before.sync.in", "message", null);
40          
41          MuleMessage msgAsync = client.request("vm://test.before.async.out", RECEIVE_TIMEOUT);
42          MuleMessage msgOut = client.request("vm://test.before.out", RECEIVE_TIMEOUT);
43                 
44          assertCorrectThreads(msgSync, msgAsync, msgOut);
45          
46      }
47      
48      @Test
49      public void testAsyncAfter() throws Exception
50      {
51          MuleClient client = new MuleClient(muleContext);
52          
53          MuleMessage msgSync = client.send("vm://test.after.sync.in", "message", null);
54          
55          MuleMessage msgAsync = client.request("vm://test.after.async.out", RECEIVE_TIMEOUT);
56          MuleMessage msgOut = client.request("vm://test.after.out", RECEIVE_TIMEOUT);
57          
58          assertCorrectThreads(msgSync, msgAsync, msgOut);
59      }
60      
61      private void assertCorrectThreads(MuleMessage msgSync, MuleMessage msgAsync, MuleMessage msgOut) throws Exception
62      {
63          assertNotNull(msgSync);
64          assertNotNull(msgAsync);
65          assertNotNull(msgOut);
66          
67          assertEquals(msgSync.getInboundProperty("request-response-thread"), 
68              msgOut.getInboundProperty("request-response-thread"));
69                  
70          assertTrue(!msgAsync.getInboundProperty("async-thread").
71              equals(msgSync.getInboundProperty("request-response-thread")));
72          
73          assertTrue(!msgAsync.getInboundProperty("async-thread").
74              equals(msgOut.getInboundProperty("request-response-thread")));   
75      }
76      
77      public static class ThreadSensingMessageProcessor implements MessageProcessor
78      {
79          @Override
80          public MuleEvent process(MuleEvent event) throws MuleException
81          {
82              String propName = event.getMessage().getInvocationProperty("property-name");
83              
84              event.getMessage().setOutboundProperty(propName, Thread.currentThread().getName());
85              return event;
86          }
87      }
88  
89  }