1
2
3
4
5
6
7
8
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 import java.util.concurrent.CountDownLatch;
19 import java.util.concurrent.TimeUnit;
20
21 import org.junit.Test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26
27
28
29
30
31
32
33 public abstract class AbstractJmsSingleTransactionSingleServiceTestCase extends AbstractJmsFunctionalTestCase
34 {
35
36 public static final String JMS_QUEUE_INPUT_CONF_A = "in1";
37 public static final String JMS_QUEUE_OUTPUT_CONF_A = "out1";
38 public static final String JMS_QUEUE_INPUT_CONF_B = "in2";
39 public static final String JMS_QUEUE_OUTPUT_CONF_B = "out2";
40 public static final String JMS_QUEUE_INPUT_CONF_C = "in3";
41 public static final String JMS_QUEUE_OUTPUT_CONF_C = "out3";
42 public static final String JMS_QUEUE_INPUT_CONF_D = "in4";
43 public static final String JMS_QUEUE_OUTPUT_CONF_D = "out4";
44 public static final String JMS_QUEUE_INPUT_CONF_E = "in5";
45 public static final String JMS_QUEUE_OUTPUT_CONF_E = "out5";
46
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
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
141
142
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 @Override
153 public void onException(Throwable t)
154 {
155 assertTrue(ExceptionUtils.containsType(t,
156 org.mule.transaction.IllegalTransactionStateException.class));
157 assertEquals(1, exceptionLatch.getCount());
158
159
160 exceptionLatch.countDown();
161 }
162 };
163 TestExceptionStrategy exceptionStrategy = (TestExceptionStrategy) muleContext.getRegistry()
164 .lookupService(serviceName)
165 .getExceptionListener();
166 exceptionStrategy.setExceptionCallback(exceptionCallback);
167
168 TestExceptionStrategy globalExceptionStrategy = (TestExceptionStrategy) muleContext.getExceptionListener();
169 globalExceptionStrategy.setExceptionCallback(exceptionCallback);
170
171 assertTrue(exceptionLatch.await(10, TimeUnit.SECONDS));
172 receive(scenarioNotReceive);
173 }
174
175
176
177
178
179 protected void runTransactionPass() throws Exception
180 {
181 send(scenarioCommit);
182 receive(scenarioRollback);
183 receive(scenarioCommit);
184 receive(scenarioNotReceive);
185 }
186 }