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