View Javadoc

1   /*
2    * $Id: JmsRedeliveryTestCase.java 23336 2011-11-07 07:58:44Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.jms;
12  
13  import static org.junit.Assert.*;
14  
15  import org.apache.activemq.protobuf.Message;
16  import org.junit.Assert;
17  import org.mule.api.MuleEventContext;
18  import org.mule.api.client.MuleClient;
19  import org.mule.api.context.notification.ExceptionNotificationListener;
20  import org.mule.api.endpoint.InboundEndpoint;
21  import org.mule.api.service.Service;
22  import org.mule.api.source.MessageSource;
23  import org.mule.construct.Flow;
24  import org.mule.context.notification.ExceptionNotification;
25  import org.mule.service.ServiceCompositeMessageSource;
26  import org.mule.tck.AbstractServiceAndFlowTestCase;
27  import org.mule.tck.exceptions.FunctionalTestException;
28  import org.mule.tck.functional.CounterCallback;
29  import org.mule.tck.functional.FunctionalTestComponent;
30  import org.mule.transport.jms.filters.JmsSelectorFilter;
31  import org.mule.transport.jms.redelivery.MessageRedeliveredException;
32  import org.mule.util.concurrent.Latch;
33  
34  import java.util.Arrays;
35  import java.util.Collection;
36  import java.util.concurrent.TimeUnit;
37  
38  import org.junit.Test;
39  import org.junit.runners.Parameterized.Parameters;
40  
41  public class JmsRedeliveryTestCase extends AbstractServiceAndFlowTestCase
42  {
43  
44      private final int timeout = getTestTimeoutSecs() * 1000 / 4;
45      private static final String DESTINATION = "jms://in";
46      private static final int MAX_REDELIVERY = 3;
47  
48      public JmsRedeliveryTestCase(ConfigVariant variant, String configResources)
49      {
50          super(variant, configResources);
51      }
52  
53      @Parameters
54      public static Collection<Object[]> parameters()
55      {
56          return Arrays.asList(new Object[][]{{ConfigVariant.SERVICE, "jms-redelivery-service.xml"},
57              {ConfigVariant.FLOW, "jms-redelivery-flow.xml"}});
58      }
59  
60      @Test
61      public void testRedelivery() throws Exception
62      {
63          MessageSource source = null;
64          Object flowOrService = muleContext.getRegistry().lookupObject("TestSelector");
65          assertNotNull(flowOrService);
66          if (flowOrService instanceof Service)
67          {
68              Service svc = (Service) flowOrService;
69              source = svc.getMessageSource();
70          }
71          else
72          {
73              Flow flow = (Flow)flowOrService;
74              source = flow.getMessageSource();
75          }
76          InboundEndpoint ep = null;
77          if (source instanceof InboundEndpoint)
78          {
79              ep = (InboundEndpoint) source;
80          }
81          else if (source instanceof ServiceCompositeMessageSource)
82          {
83              ep = ((ServiceCompositeMessageSource)source).getEndpoints().get(0);
84          }
85          assertNotNull(ep);
86  
87          JmsConnector cnctr = (JmsConnector) muleContext.getRegistry().lookupConnector("jmsConnector");
88          assertTrue(cnctr.getSelector(ep) instanceof JmsSelectorFilter);
89  
90          MuleClient client = muleContext.getClient();
91          // required if broker is not restarted with the test - it tries to deliver those messages to the
92          // client
93          // purge the queue
94          while (client.request(DESTINATION, 1000) != null)
95          {
96              logger.warn("Destination " + DESTINATION + " isn't empty, draining it");
97          }
98  
99          FunctionalTestComponent ftc = getFunctionalTestComponent("Bouncer");
100 
101         // whether a MessageRedeliverdException has been fired
102         final Latch messageRedeliveryExceptionFired = new Latch();
103         muleContext.registerListener(new ExceptionNotificationListener<ExceptionNotification>()
104         {
105             public void onNotification(ExceptionNotification notification)
106             {
107                 logger.debug("onNotification() = " + notification.getException().getClass().getName());
108                 if (notification.getException() instanceof MessageRedeliveredException)
109                 {
110                     messageRedeliveryExceptionFired.countDown();
111                     // Test for MULE-4630
112                     assertEquals(DESTINATION,
113                         ((MessageRedeliveredException) notification.getException()).getEndpoint()
114                             .getEndpointURI()
115                             .toString());
116                     assertEquals(MAX_REDELIVERY,
117                         ((MessageRedeliveredException) notification.getException()).getMaxRedelivery());
118                     assertTrue(((MessageRedeliveredException) notification.getException()).getMuleMessage()
119                         .getPayload() instanceof javax.jms.Message);
120                 }
121             }
122         });
123 
124         // enhance the counter callback to count, then throw an exception
125         final CounterCallback callback = new CounterCallback()
126         {
127             @Override
128             public void eventReceived(MuleEventContext context, Object Component) throws Exception
129             {
130                 final int count = incCallbackCount();
131                 logger.info("Message Delivery Count is: " + count);
132                 throw new FunctionalTestException();
133             }
134         };
135         ftc.setEventCallback(callback);
136 
137         client.dispatch(DESTINATION, TEST_MESSAGE, null);
138 
139         Thread.sleep(2000);
140         if (!messageRedeliveryExceptionFired.await(timeout, TimeUnit.MILLISECONDS))
141         {
142             fail("Exception from FunctionalTestComponent was not triggered three times");
143         }
144         assertEquals("MessageRedeliveredException never fired.", 0, messageRedeliveryExceptionFired.getCount());
145         assertEquals("Wrong number of delivery attempts", MAX_REDELIVERY + 1, callback.getCallbackCount());
146 
147     }
148 }