1   /*
2    * $Id: SpringEventsJmsAsyncExampleTestCase.java 11371 2008-03-15 03:12:09Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.integration.spring.events.async;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.FunctionalTestCase;
17  import org.mule.tck.functional.EventCallback;
18  import org.mule.test.integration.spring.events.Order;
19  import org.mule.test.integration.spring.events.OrderManagerBean;
20  
21  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
22  
23  /**
24   * <code>SpringEventsJmsExampleTestCase</code> is a testcase used to test the
25   * example config in the docco. this test is not run when building this module as it
26   * relies on Jms, it's used to verify the example config works.
27   */
28  public class SpringEventsJmsAsyncExampleTestCase extends FunctionalTestCase
29  {
30      private final AtomicInteger eventCount = new AtomicInteger(0);
31  
32      protected void doSetUp() throws Exception
33      {
34          super.doSetUp();
35          eventCount.set(0);
36      }
37  
38      protected String getConfigResources()
39      {
40          return "org/mule/test/integration/spring/events/async/mule-events-example-async-app-context.xml";
41      }
42  
43      public void testReceivingASubscriptionEvent() throws Exception
44      {
45          OrderManagerBean subscriptionBean = (OrderManagerBean) muleContext.getRegistry().lookupObject(
46              "orderManagerBean");
47          assertNotNull(subscriptionBean);
48          // when an event is received by 'testEventBean1' this callback will be
49          // invoked
50          EventCallback callback = new EventCallback()
51          {
52              public void eventReceived(MuleEventContext context, Object o) throws Exception
53              {
54                  eventCount.incrementAndGet();
55              }
56          };
57          subscriptionBean.setEventCallback(callback);
58  
59          MuleClient client = new MuleClient();
60          Order order = new Order("Sausage and Mash");
61          client.send("jms://orders.queue", order, null);
62          Thread.sleep(1000);
63          assertTrue(eventCount.get() == 1);
64  
65          MuleMessage result = client.request("jms://processed.queue", 10000);
66          assertEquals(1, eventCount.intValue());
67          assertNotNull(result);
68          assertEquals("Order 'Sausage and Mash' Processed", result.getPayloadAsString());
69      }
70  
71      public void testReceiveAsWebService() throws Exception
72      {
73          MuleClient client = new MuleClient();
74          OrderManagerBean orderManager = (OrderManagerBean) muleContext.getRegistry().lookupObject("orderManagerBean");
75          assertNotNull(orderManager);
76          EventCallback callback = new EventCallback()
77          {
78              public void eventReceived(MuleEventContext context, Object o) throws Exception
79              {
80                  eventCount.incrementAndGet();
81              }
82          };
83          orderManager.setEventCallback(callback);
84  
85          Order order = new Order("Sausage and Mash");
86          // Make an async call
87          client.dispatch("axis:http://localhost:44444/mule/orderManager?method=processOrderAsync", order, null);
88  
89          MuleMessage result = client.request("jms://processed.queue", 10000);
90          assertNotNull(result);
91          assertEquals("Order 'Sausage and Mash' Processed Async", result.getPayload());
92      }
93  }