1   /*
2    * $Id: JcaServiceTestCase.java 11373 2008-03-15 05:03:10Z 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.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.resolvers.DefaultEntryPointResolverSet;
18  import org.mule.tck.AbstractMuleTestCase;
19  
20  import java.lang.reflect.Method;
21  
22  import javax.resource.ResourceException;
23  import javax.resource.spi.UnavailableException;
24  import javax.resource.spi.endpoint.MessageEndpoint;
25  import javax.resource.spi.endpoint.MessageEndpointFactory;
26  import javax.transaction.xa.XAResource;
27  
28  public class JcaServiceTestCase extends AbstractMuleTestCase // AbstractServiceTestCase
29  {
30  
31      // Cannot extend AbstractServiceTestCase because of inconsistent behaviour. See
32      // MULE-2843
33  
34      private Service service;
35  
36      private TestJCAWorkManager workManager;
37  
38      protected void doSetUp() throws Exception
39      {
40          // Create and register JcaModel
41          workManager = new TestJCAWorkManager();
42          JcaModel jcaModel = new JcaModel();
43          muleContext.getRegistry().registerModel(jcaModel);
44  
45          // Create, register, initialise and start JcaService
46          String name = "JcaService#";
47          service = new JcaService();
48          service.setName(name);
49          service.setModel(jcaModel);
50          service.setComponent(new JcaComponent(new TestMessageEndpointFactory(), new DefaultEntryPointResolverSet(),
51              service, workManager));
52          muleContext.getRegistry().registerService(service);
53  
54          assertNotNull(service);
55      }
56  
57      protected void doTearDown() throws Exception
58      {
59          workManager = null;
60          service = null;
61      }
62  
63      public void testSendEvent() throws Exception
64      {
65          service.start();
66          ImmutableEndpoint endpoint = getTestInboundEndpoint("jcaInFlowEndpoint");
67          MuleEvent event = getTestEvent("Message", endpoint);
68  
69          try
70          {
71              service.sendEvent(event);
72              fail("Exception expected, JcaService does not support sendEvent()");
73          }
74          catch (Exception e)
75          {
76              // expected
77          }
78      }
79  
80      public void testDispatchEvent() throws Exception
81      {
82          service.start();
83          ImmutableEndpoint endpoint = getTestInboundEndpoint("jcaInFlowEndpoint");
84          MuleEvent event = getTestEvent("Message", endpoint);
85  
86          service.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()
93      {
94          try
95          {
96              service.pause();
97              fail("Exception expected, JcaService does not support pause()");
98          }
99          catch (MuleException e)
100         {
101             // expected
102         }
103     }
104 
105     public void testResume()
106     {
107         try
108         {
109             service.resume();
110             fail("Exception expected, JcaService does not support resume()");
111         }
112         catch (MuleException e)
113         {
114             // expected
115         }
116     }
117     
118     class TestMessageEndpointFactory implements MessageEndpointFactory
119     {
120 
121         public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException
122         {
123             return null;
124         }
125 
126         public boolean isDeliveryTransacted(Method method) throws NoSuchMethodException
127         {
128             return false;
129         }
130 
131     }
132 
133     class TestMessageEndoint implements MessageEndpoint
134     {
135 
136         public void afterDelivery() throws ResourceException
137         {
138             // TODO Auto-generated method stub
139 
140         }
141 
142         public void beforeDelivery(Method method) throws NoSuchMethodException, ResourceException
143         {
144             // TODO Auto-generated method stub
145 
146         }
147 
148         public void release()
149         {
150             // TODO Auto-generated method stub
151 
152         }
153 
154     }
155 
156 }