View Javadoc

1   /*
2    * $Id: AbstractJmsSingleTransactionSingleServiceTestCase.java 20320 2010-11-24 15:03:31Z dfeist $
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.transport.jms.integration;
12  
13  import org.mule.tck.testmodels.mule.TestExceptionStrategy;
14  import org.mule.tck.testmodels.mule.TestExceptionStrategy.ExceptionCallback;
15  import org.mule.util.ExceptionUtils;
16  
17  import java.util.Properties;
18  
19  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
20  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  /**
26   * This tests single resource transactions through a single service definition (even
27   * though the mule config file will have many service definitions). The idea is to
28   * test all possible combinations of single resource transaction types.
29   * 
30   * @author dzapata
31   */
32  public abstract class AbstractJmsSingleTransactionSingleServiceTestCase extends AbstractJmsFunctionalTestCase
33  {
34      // queue names
35      public static final String JMS_QUEUE_INPUT_CONF_A = "in1";
36      public static final String JMS_QUEUE_OUTPUT_CONF_A = "out1";
37      public static final String JMS_QUEUE_INPUT_CONF_B = "in2";
38      public static final String JMS_QUEUE_OUTPUT_CONF_B = "out2";
39      public static final String JMS_QUEUE_INPUT_CONF_C = "in3";
40      public static final String JMS_QUEUE_OUTPUT_CONF_C = "out3";
41      public static final String JMS_QUEUE_INPUT_CONF_D = "in4";
42      public static final String JMS_QUEUE_OUTPUT_CONF_D = "out4";
43      public static final String JMS_QUEUE_INPUT_CONF_E = "in5";
44      public static final String JMS_QUEUE_OUTPUT_CONF_E = "out5";
45  
46      @Before
47      @Override
48      protected void doSetUp() throws Exception
49      {
50          super.doSetUp();
51          muleContext.setExceptionListener(new TestExceptionStrategy());
52      }
53  
54      @Override
55      protected Properties getStartUpProperties()
56      {
57          Properties props = super.getStartUpProperties();
58          // Inject endpoint names into the config
59          props.put(INBOUND_ENDPOINT_KEY + "1", getJmsConfig().getInboundEndpoint() + "1");
60          props.put(INBOUND_ENDPOINT_KEY + "2", getJmsConfig().getInboundEndpoint() + "2");
61          props.put(INBOUND_ENDPOINT_KEY + "3", getJmsConfig().getInboundEndpoint() + "3");
62          props.put(INBOUND_ENDPOINT_KEY + "4", getJmsConfig().getInboundEndpoint() + "4");
63          props.put(INBOUND_ENDPOINT_KEY + "5", getJmsConfig().getInboundEndpoint() + "5");
64  
65          props.put(OUTBOUND_ENDPOINT_KEY + "1", getJmsConfig().getOutboundEndpoint() + "1");
66          props.put(OUTBOUND_ENDPOINT_KEY + "2", getJmsConfig().getOutboundEndpoint() + "2");
67          props.put(OUTBOUND_ENDPOINT_KEY + "3", getJmsConfig().getOutboundEndpoint() + "3");
68          props.put(OUTBOUND_ENDPOINT_KEY + "4", getJmsConfig().getOutboundEndpoint() + "4");
69          props.put(OUTBOUND_ENDPOINT_KEY + "5", getJmsConfig().getOutboundEndpoint() + "5");
70  
71          return props;
72      }
73  
74      @Test
75      public void testNone() throws Exception
76      {
77          scenarioCommit.setInputDestinationName(JMS_QUEUE_INPUT_CONF_A);
78          scenarioRollback.setInputDestinationName(JMS_QUEUE_INPUT_CONF_A);
79          scenarioNotReceive.setInputDestinationName(JMS_QUEUE_INPUT_CONF_A);
80          scenarioCommit.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_A);
81          scenarioRollback.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_A);
82          scenarioNotReceive.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_A);
83  
84          runTransactionPass();
85      }
86  
87      @Test
88      public void testAlwaysBegin() throws Exception
89      {
90          scenarioCommit.setInputDestinationName(JMS_QUEUE_INPUT_CONF_B);
91          scenarioRollback.setInputDestinationName(JMS_QUEUE_INPUT_CONF_B);
92          scenarioNotReceive.setInputDestinationName(JMS_QUEUE_INPUT_CONF_B);
93          scenarioCommit.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_B);
94          scenarioRollback.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_B);
95          scenarioNotReceive.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_B);
96  
97          runTransactionPass();
98      }
99  
100     @Test
101     public void testBeginOrJoin() throws Exception
102     {
103         scenarioCommit.setInputDestinationName(JMS_QUEUE_INPUT_CONF_C);
104         scenarioRollback.setInputDestinationName(JMS_QUEUE_INPUT_CONF_C);
105         scenarioNotReceive.setInputDestinationName(JMS_QUEUE_INPUT_CONF_C);
106         scenarioCommit.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_C);
107         scenarioRollback.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_C);
108         scenarioNotReceive.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_C);
109 
110         runTransactionPass();
111     }
112 
113     @Test
114     public void testAlwaysJoin() throws Exception
115     {
116         scenarioCommit.setInputDestinationName(JMS_QUEUE_INPUT_CONF_D);
117         scenarioRollback.setInputDestinationName(JMS_QUEUE_INPUT_CONF_D);
118         scenarioNotReceive.setInputDestinationName(JMS_QUEUE_INPUT_CONF_D);
119         scenarioCommit.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_D);
120         scenarioRollback.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_D);
121         scenarioNotReceive.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_D);
122 
123         runTransactionPass();
124     }
125 
126     @Test
127     public void testJoinIfPossible() throws Exception
128     {
129         scenarioCommit.setInputDestinationName(JMS_QUEUE_INPUT_CONF_E);
130         scenarioRollback.setInputDestinationName(JMS_QUEUE_INPUT_CONF_E);
131         scenarioNotReceive.setInputDestinationName(JMS_QUEUE_INPUT_CONF_E);
132         scenarioCommit.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_E);
133         scenarioRollback.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_E);
134         scenarioNotReceive.setOutputDestinationName(JMS_QUEUE_OUTPUT_CONF_E);
135 
136         runTransactionPass();
137     }
138 
139     /**
140      * Call this when you expect the transaction to fail
141      * @param serviceName
142      * @throws Exception
143      */
144     protected void runTransactionFail(String serviceName) throws Exception
145     {
146         final CountDownLatch exceptionLatch = new CountDownLatch(1);
147 
148         send(scenarioCommit);
149 
150         final ExceptionCallback exceptionCallback = new ExceptionCallback()
151         {
152             public void onException(Throwable t)
153             {
154                 assertTrue(ExceptionUtils.containsType(t,
155                     org.mule.transaction.IllegalTransactionStateException.class));
156                 assertEquals(1, exceptionLatch.getCount()); // make sure this
157                                                             // exception doesn't
158                                                             // happen more than once
159                 exceptionLatch.countDown();
160             }
161         };
162         TestExceptionStrategy exceptionStrategy = (TestExceptionStrategy) muleContext.getRegistry()
163             .lookupService(serviceName)
164             .getExceptionListener();
165         exceptionStrategy.setExceptionCallback(exceptionCallback);
166 
167         TestExceptionStrategy globalExceptionStrategy = (TestExceptionStrategy) muleContext.getExceptionListener();
168         globalExceptionStrategy.setExceptionCallback(exceptionCallback);
169 
170         assertTrue(exceptionLatch.await(10, TimeUnit.SECONDS));
171         receive(scenarioNotReceive);
172     }
173 
174     /**
175      * Call this when you expect the message to make it to the outbound endpoint 
176      * @throws Exception
177      */
178     protected void runTransactionPass() throws Exception
179     {
180         send(scenarioCommit);
181         receive(scenarioRollback);
182         receive(scenarioCommit);
183         receive(scenarioNotReceive);
184     }
185 }