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 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          //Stop flow to not consume message again
65          SimpleFlowConstruct simpleFlowConstruct = muleContext.getRegistry().get("flowWithoutExceptionStrategyAndTx");
66          simpleFlowConstruct.stop();
67          //Check message rollback
68          MuleMessage muleMessage = muleClient.request("jms://in", TIMEOUT);
69          assertThat(muleMessage, notNullValue());
70          assertThat((String) muleMessage.getPayload(), Is.is(MESSAGE));
71          //Check outbound-endpoint was not executed
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          //Stop flow to not consume message again
83          SimpleFlowConstruct simpleFlowConstruct = muleContext.getRegistry().get("flowWithoutExceptionStrategyAndNoTx");
84          simpleFlowConstruct.stop();
85          //Check message was consumed
86          MuleMessage muleMessage = muleClient.request("jms://in2", TIMEOUT);
87          assertThat(muleMessage, IsNull.<Object>nullValue());
88  
89          //Check outbound-endpoint was not executed
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         //Stop flow to not consume message again
101         SimpleFlowConstruct flow = muleContext.getRegistry().get("flowWithDefaultStrategyConfigured");
102         flow.stop();
103         //Check message was no consumed
104         MuleMessage muleMessage = muleClient.request("jms://in3", TIMEOUT);
105         assertThat(muleMessage, IsNull.<Object>nullValue());
106         
107         //Check outbound-endpoint was not executed
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         //Stop flow to not consume message again
119         SimpleFlowConstruct flow = muleContext.getRegistry().get("flowWithExceptionNotification");
120         flow.stop();
121         //Check message was no consumed
122         MuleMessage muleMessage = muleClient.request("jms://in4", TIMEOUT);
123         assertThat(muleMessage, IsNull.<Object>nullValue());
124 
125         // Check exception notification was sent
126         MuleMessage exceptionMessage = muleClient.request("jms://exception4", TIMEOUT);
127         assertThat(exceptionMessage, IsNull.<Object> notNullValue());
128         assertThat(exceptionMessage.getPayload(), IsNull.<Object> notNullValue());
129         
130         //Check outbound-endpoint was not executed
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         //Stop flow to not consume message again
142         SimpleFlowConstruct flow = muleContext.getRegistry().get("flowConfiguredForDeadLetterQueue");
143         flow.stop();
144         //Check message was no consumed
145         MuleMessage muleMessage = muleClient.request("jms://in5", TIMEOUT);
146         assertThat(muleMessage, IsNull.<Object>nullValue());
147 
148         // Check exception notification was sent
149         MuleMessage deadLetter = muleClient.request("jms://DLQ5", TIMEOUT);
150         assertThat(deadLetter, IsNull.<Object> notNullValue());
151         assertThat(deadLetter.getPayload(), IsNull.<Object> notNullValue());
152         
153         //Check outbound-endpoint was not executed
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         //Stop flow to not consume message again
165         SimpleFlowConstruct flow = muleContext.getRegistry().get("flowConfiguredForDeadLetterQueueTx");
166         flow.stop();
167         //Check message was no consumed
168         MuleMessage muleMessage = muleClient.request("jms://in6", TIMEOUT);
169         assertThat(muleMessage, IsNull.<Object>nullValue());
170 
171         // Check exception notification was sent
172         MuleMessage deadLetter = muleClient.request("jms://DLQ6", TIMEOUT);
173         assertThat(deadLetter, IsNull.<Object> notNullValue());
174         assertThat(deadLetter.getPayload(), IsNull.<Object> notNullValue());
175 
176         //Check outbound-endpoint was not executed
177         MuleMessage outboundMessage = muleClient.request("jms://out6", SHORT_TIMEOUT);
178         assertThat(outboundMessage, IsNull.<Object>nullValue());
179     }
180 
181 
182 }
183