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