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