1
2
3
4
5
6
7
8
9
10 package org.mule.transport.jms.integration;
11
12 import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 import static org.junit.Assert.assertThat;
14
15 import org.mule.api.MuleEvent;
16 import org.mule.api.MuleException;
17 import org.mule.api.MuleMessage;
18 import org.mule.api.processor.MessageProcessor;
19 import org.mule.construct.Flow;
20 import org.mule.exception.DefaultMessagingExceptionStrategy;
21 import org.mule.module.client.MuleClient;
22 import org.mule.util.concurrent.Latch;
23
24 import org.hamcrest.core.IsNull;
25 import org.junit.Test;
26
27 public class JmsExceptionStrategyTestCase extends AbstractJmsFunctionalTestCase
28 {
29
30 public static final String MESSAGE = "some message";
31 public static final int TIMEOUT = 3000;
32 public static final int SHORT_TIMEOUT = 500;
33 private Latch latch;
34 private MuleClient muleClient;
35 private static final long LATCH_AWAIT_TIMEOUT = 3000;
36
37
38 @Override
39 protected String getConfigResources()
40 {
41 return "integration/jms-exception-strategy.xml";
42 }
43
44 @Override
45 protected void doSetUp() throws Exception
46 {
47 latch = new Latch();
48 muleClient = new MuleClient(muleContext);
49 DefaultMessagingExceptionStrategy exceptionStrategy = (DefaultMessagingExceptionStrategy)muleContext.getRegistry().lookupFlowConstruct("flowWithoutExceptionStrategyAndTx").getExceptionListener();
50 exceptionStrategy.getMessageProcessors().add(new MessageProcessor()
51 {
52 public MuleEvent process(MuleEvent event) throws MuleException
53 {
54 latch.countDown();
55 return event;
56 }
57 });
58 }
59
60 @Test
61 public void testInExceptionDoRollbackJmsTx() throws Exception
62 {
63 muleClient = new MuleClient(muleContext);
64 muleClient.dispatch("jms://in", MESSAGE, null);
65 latch.await(LATCH_AWAIT_TIMEOUT, MILLISECONDS);
66
67 Flow flow = muleContext.getRegistry().get("flowWithoutExceptionStrategyAndTx");
68 flow.stop();
69
70
71 MuleMessage muleMessage = muleClient.request("jms://in", TIMEOUT);
72 assertThat(muleMessage, IsNull.<Object>notNullValue());
73
74
75
76
77
78 MuleMessage outboundMessage = muleClient.request("jms://out", SHORT_TIMEOUT);
79 assertThat(outboundMessage, IsNull.<Object>nullValue());
80 }
81
82 @Test
83 public void testInExceptionDoRollbackJmsNoTx() throws Exception
84 {
85 muleClient = new MuleClient(muleContext);
86 muleClient.dispatch("jms://in2", MESSAGE, null);
87 latch.await(LATCH_AWAIT_TIMEOUT, MILLISECONDS);
88
89 Flow flow = muleContext.getRegistry().get("flowWithoutExceptionStrategyAndNoTx");
90 flow.stop();
91
92 MuleMessage muleMessage = muleClient.request("jms://in2", TIMEOUT);
93 assertThat(muleMessage, IsNull.<Object>notNullValue());
94
95
96 MuleMessage outboundMessage = muleClient.request("jms://out2", SHORT_TIMEOUT);
97 assertThat(outboundMessage, IsNull.<Object>nullValue());
98 }
99
100 @Test
101 public void testDefaultStrategyConfigured() throws Exception
102 {
103 muleClient = new MuleClient(muleContext);
104 muleClient.dispatch("jms://in3", MESSAGE, null);
105 latch.await(LATCH_AWAIT_TIMEOUT, MILLISECONDS);
106
107 Flow flow = muleContext.getRegistry().get("flowWithDefaultStrategyConfigured");
108 flow.stop();
109
110 MuleMessage muleMessage = muleClient.request("jms://in3", TIMEOUT);
111 assertThat(muleMessage, IsNull.<Object>notNullValue());
112
113
114 MuleMessage outboundMessage = muleClient.request("jms://out3", SHORT_TIMEOUT);
115 assertThat(outboundMessage, IsNull.<Object>nullValue());
116 }
117
118 @Test
119 public void testSendExceptionNofication() throws Exception
120 {
121 muleClient = new MuleClient(muleContext);
122 muleClient.dispatch("jms://in4", MESSAGE, null);
123 latch.await(LATCH_AWAIT_TIMEOUT, MILLISECONDS);
124
125 Flow flow = muleContext.getRegistry().get("flowWithExceptionNotification");
126 flow.stop();
127
128 MuleMessage muleMessage = muleClient.request("jms://in4", TIMEOUT);
129 assertThat(muleMessage, IsNull.<Object>notNullValue());
130
131
132 MuleMessage exceptionMessage = muleClient.request("jms://exception4", TIMEOUT);
133 assertThat(exceptionMessage, IsNull.<Object> notNullValue());
134 assertThat(exceptionMessage.getPayload(), IsNull.<Object> notNullValue());
135
136
137 MuleMessage outboundMessage = muleClient.request("jms://out4", SHORT_TIMEOUT);
138 assertThat(outboundMessage, IsNull.<Object>nullValue());
139 }
140
141 @Test
142 public void testFlowConfiguredForDeadLetterQueue() throws Exception
143 {
144 muleClient = new MuleClient(muleContext);
145 muleClient.dispatch("jms://in5", MESSAGE, null);
146 latch.await(LATCH_AWAIT_TIMEOUT, MILLISECONDS);
147
148 Flow flow = muleContext.getRegistry().get("flowConfiguredForDeadLetterQueue");
149 flow.stop();
150
151 MuleMessage muleMessage = muleClient.request("jms://in5", TIMEOUT);
152 assertThat(muleMessage, IsNull.<Object>nullValue());
153
154
155 MuleMessage deadLetter = muleClient.request("jms://DLQ5", TIMEOUT);
156 assertThat(deadLetter, IsNull.<Object> notNullValue());
157 assertThat(deadLetter.getPayload(), IsNull.<Object> notNullValue());
158
159
160 MuleMessage outboundMessage = muleClient.request("jms://out5", SHORT_TIMEOUT);
161 assertThat(outboundMessage, IsNull.<Object>nullValue());
162 }
163
164 @Test
165 public void testFlowConfiguredForDeadLetterQueueTx() throws Exception
166 {
167 muleClient = new MuleClient(muleContext);
168 muleClient.dispatch("jms://in6", MESSAGE, null);
169 latch.await(LATCH_AWAIT_TIMEOUT, MILLISECONDS);
170
171 Flow flow = muleContext.getRegistry().get("flowConfiguredForDeadLetterQueueTx");
172 flow.stop();
173
174 MuleMessage muleMessage = muleClient.request("jms://in6", TIMEOUT);
175 assertThat(muleMessage, IsNull.<Object>nullValue());
176
177
178 MuleMessage deadLetter = muleClient.request("jms://DLQ6", TIMEOUT);
179 assertThat(deadLetter, IsNull.<Object> notNullValue());
180 assertThat(deadLetter.getPayload(), IsNull.<Object> notNullValue());
181
182
183 MuleMessage outboundMessage = muleClient.request("jms://out6", SHORT_TIMEOUT);
184 assertThat(outboundMessage, IsNull.<Object>nullValue());
185 }
186
187
188 }
189