View Javadoc

1   /*
2    * $Id: JcaServiceTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.module.jca;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.endpoint.ImmutableEndpoint;
16  import org.mule.api.service.Service;
17  import org.mule.model.AbstractServiceTestCase;
18  import org.mule.model.resolvers.DefaultEntryPointResolverSet;
19  
20  import java.lang.reflect.Method;
21  
22  import javax.resource.spi.UnavailableException;
23  import javax.resource.spi.endpoint.MessageEndpoint;
24  import javax.resource.spi.endpoint.MessageEndpointFactory;
25  import javax.transaction.xa.XAResource;
26  
27  public class JcaServiceTestCase extends AbstractServiceTestCase
28  {
29      private Service service;
30  
31      protected void doSetUp() throws Exception
32      {
33          // Create and initialise JcaModel
34          workManager = new TestJCAWorkManager();
35          JcaModel jcaModel = new JcaModel();
36          jcaModel.setMuleContext(muleContext);
37          jcaModel.initialise();
38  
39          String name = "JcaService#";
40          service = new JcaService(muleContext);
41          service.setName(name);
42          service.setModel(jcaModel);
43          service.setComponent(new JcaComponent(new TestMessageEndpointFactory(), new DefaultEntryPointResolverSet(),
44                  service, workManager));
45      }
46  
47      @Override
48      protected Service getService()
49      {
50          return service;
51      }
52  
53      private TestJCAWorkManager workManager;
54  
55      protected void doTearDown() throws Exception
56      {
57          workManager = null;
58          service = null;
59      }
60  
61      public void testSendEvent() throws Exception
62      {
63          getService().initialise();
64          getService().start();
65          ImmutableEndpoint endpoint = getTestInboundEndpoint("jcaInFlowEndpoint");
66          MuleEvent event = getTestEvent("Message", endpoint);
67  
68          try
69          {
70              service.sendEvent(event);
71              fail("Exception expected, JcaService does not support sendEvent()");
72          }
73          catch (Exception e)
74          {
75              // expected
76          }
77      }
78  
79      public void testDispatchEvent() throws Exception
80      {
81          getService().initialise();
82          getService().start();
83          ImmutableEndpoint endpoint = getTestInboundEndpoint("jcaInFlowEndpoint");
84          MuleEvent event = getTestEvent("Message", endpoint);
85  
86          getService().dispatchEvent(event);
87          assertEquals(1, workManager.getScheduledWorkList().size());
88          assertEquals(0, workManager.getStartWorkList().size());
89          assertEquals(0, workManager.getDoWorkList().size());
90      }
91  
92      public void testPause() throws MuleException
93      {
94          try
95          {
96              getService().pause();
97              fail("Exception expected, JcaService does not support pause()");
98          }
99          catch (IllegalStateException e)
100         {
101             // expected
102         }
103 
104     }
105 
106     public void testResume() throws MuleException
107     {
108         try
109         {
110             service.resume();
111             fail("Exception expected, JcaService does not support resume()");
112         }
113         catch (IllegalStateException e)
114         {
115             // expected
116         }
117     }
118 
119     class TestMessageEndpointFactory implements MessageEndpointFactory
120     {
121 
122         public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException
123         {
124             return null;
125         }
126 
127         public boolean isDeliveryTransacted(Method method) throws NoSuchMethodException
128         {
129             return false;
130         }
131     }
132 }