1
2
3
4
5
6
7 package org.mule.transport.jms;
8
9 import org.mule.api.MuleEventContext;
10 import org.mule.api.MuleException;
11 import org.mule.api.MuleMessage;
12 import org.mule.api.client.MuleClient;
13 import org.mule.api.context.notification.ExceptionNotificationListener;
14 import org.mule.context.notification.ExceptionNotification;
15 import org.mule.context.notification.NotificationException;
16 import org.mule.message.ExceptionMessage;
17 import org.mule.tck.AbstractServiceAndFlowTestCase;
18 import org.mule.tck.exceptions.FunctionalTestException;
19 import org.mule.tck.functional.CounterCallback;
20 import org.mule.tck.functional.FunctionalTestComponent;
21 import org.mule.transport.jms.redelivery.MessageRedeliveredException;
22 import org.mule.util.concurrent.Latch;
23
24 import java.util.Arrays;
25 import java.util.Collection;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.runners.Parameterized;
30
31 import static org.junit.Assert.assertNotNull;
32 import static org.junit.Assert.assertNull;
33 import static org.junit.Assert.assertTrue;
34
35 public abstract class AbstractJmsRedeliveryTestCase extends AbstractServiceAndFlowTestCase
36 {
37
38 protected static final String JMS_INPUT_QUEUE = "jms://in";
39 protected static final String JMS_DEAD_LETTER = "jms://dead.letter";
40 protected final int timeout = getTestTimeoutSecs() * 1000 / 4;
41
42 protected MuleClient client;
43 protected Latch messageRedeliveryExceptionFired;
44 protected CounterCallback callback;
45
46 public AbstractJmsRedeliveryTestCase(ConfigVariant variant, String configResources)
47 {
48 super(variant, configResources);
49 System.setProperty("maxRedelivery", String.valueOf(getMaxRedelivery()));
50 }
51
52 @Parameterized.Parameters
53 public static Collection<Object[]> parameters()
54 {
55 return Arrays.asList(new Object[][] {{ConfigVariant.SERVICE, "jms-redelivery-service.xml"},
56 {ConfigVariant.FLOW, "jms-redelivery-flow.xml"}});
57 }
58
59 @Before
60 public void setUp() throws Exception
61 {
62 client = muleContext.getClient();
63 messageRedeliveryExceptionFired = new Latch();
64 registerEventListener(messageRedeliveryExceptionFired);
65 purgeQueue();
66 setupCallback();
67 }
68
69 protected void assertMessageInDlq() throws MuleException
70 {
71 MuleMessage dl = client.request(JMS_DEAD_LETTER, 1000);
72 assertNotNull(dl);
73 assertTrue(dl.getPayload() instanceof ExceptionMessage);
74 ExceptionMessage em = (ExceptionMessage) dl.getPayload();
75 assertNotNull(em.getException());
76 assertTrue(em.getException() instanceof MessageRedeliveredException);
77 }
78
79 protected void purgeQueue() throws MuleException
80 {
81
82
83 while (client.request(JMS_INPUT_QUEUE, 1000) != null)
84 {
85 logger.warn("Destination " + JMS_INPUT_QUEUE + " isn't empty, draining it");
86 }
87 }
88
89 protected void setupCallback() throws Exception
90 {
91 callback = createCallback();
92
93 FunctionalTestComponent ftc = getFunctionalTestComponent("Bouncer");
94 ftc.setEventCallback(callback);
95 }
96
97 private CounterCallback createCallback()
98 {
99
100 return new CounterCallback()
101 {
102 @Override
103 public void eventReceived(MuleEventContext context, Object Component) throws Exception
104 {
105 final int count = incCallbackCount();
106 logger.info("Message Delivery Count is: " + count);
107 throw new FunctionalTestException();
108 }
109 };
110 }
111
112 private void registerEventListener(final Latch messageRedeliveryExceptionFired) throws NotificationException
113 {
114 muleContext.registerListener(new ExceptionNotificationListener<ExceptionNotification>()
115 {
116 public void onNotification(ExceptionNotification notification)
117 {
118 if (notification.getException() instanceof MessageRedeliveredException)
119 {
120 messageRedeliveryExceptionFired.countDown();
121 }
122 }
123 });
124 }
125
126 protected void assertNoMessageInDlq(String location) throws MuleException
127 {
128 assertNull(client.request(location, 1000));
129 }
130
131 @After
132 public void cleanUpMaxRedelivery()
133 {
134 System.clearProperty("maxRedelivery");
135 }
136
137 protected abstract int getMaxRedelivery();
138 }