1
2
3
4
5
6
7 package org.mule.test.integration.exceptions;
8
9 import static org.junit.Assert.assertThat;
10 import static org.junit.Assert.fail;
11 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
12 import org.hamcrest.core.IsNull;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runners.Parameterized;
16 import org.mule.api.MuleEventContext;
17 import org.mule.api.MuleMessage;
18 import org.mule.api.client.MuleClient;
19 import org.mule.api.construct.FlowConstruct;
20 import org.mule.api.context.notification.ExceptionNotificationListener;
21 import org.mule.context.notification.ExceptionNotification;
22 import org.mule.tck.AbstractServiceAndFlowTestCase;
23 import org.mule.tck.functional.EventCallback;
24 import org.mule.tck.functional.FunctionalTestComponent;
25 import org.mule.util.concurrent.Latch;
26
27 import java.io.IOException;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.concurrent.atomic.AtomicReference;
31
32 public class ExceptionStrategyExceptionPatternTestCase extends AbstractServiceAndFlowTestCase
33 {
34
35 public static final String PAYLOAD = "some text";
36 public static final int TIMEOUT = 5000;
37 private Latch exceptionLatch = new Latch();
38 private AtomicReference<Exception> exceptionHolder = new AtomicReference<Exception>();
39
40 public ExceptionStrategyExceptionPatternTestCase(ConfigVariant variant, String configResources)
41 {
42 super(variant, configResources);
43 }
44
45 @Parameterized.Parameters
46 public static Collection<Object[]> parameters()
47 {
48 return Arrays.asList(new Object[][]{{AbstractServiceAndFlowTestCase.ConfigVariant.SERVICE, "org/mule/test/integration/exceptions/exception-strategy-exception-pattern-service.xml"},
49 {AbstractServiceAndFlowTestCase.ConfigVariant.FLOW, "org/mule/test/integration/exceptions/exception-strategy-exception-pattern-flow.xml"}});
50 }
51
52 @Before
53 public void setUp() throws Exception
54 {
55 muleContext.registerListener(new ExceptionNotificationListener<ExceptionNotification>() {
56 public void onNotification(ExceptionNotification notification)
57 {
58 exceptionLatch.release();
59 }
60 });
61 FunctionalTestComponent failingFlow = getFunctionalTestComponent("failingFlow");
62 failingFlow.setEventCallback(new EventCallback()
63 {
64 public void eventReceived(MuleEventContext context, Object component) throws Exception
65 {
66 throw exceptionHolder.get();
67 }
68 });
69 }
70
71 @Test
72 public void testThrowExceptionAndCommit() throws Exception
73 {
74
75 MuleClient client = muleContext.getClient();
76 exceptionHolder.set(new IOException());
77 client.dispatch("jms://in", PAYLOAD,null);
78 if (!exceptionLatch.await(TIMEOUT, TimeUnit.MILLISECONDS))
79 {
80 fail("exception should be thrown");
81 }
82 MuleMessage muleMessage = client.request("jms://out", TIMEOUT);
83 assertThat(muleMessage, IsNull.notNullValue());
84 }
85
86 @Test
87 public void testThrowExceptionAndRollback() throws Exception
88 {
89
90 MuleClient client = muleContext.getClient();
91 exceptionHolder.set(new IllegalArgumentException());
92 client.dispatch("jms://in", PAYLOAD,null);
93 if (!exceptionLatch.await(TIMEOUT, TimeUnit.MILLISECONDS))
94 {
95 fail("exception should be thrown");
96 }
97 MuleMessage muleMessage = client.request("jms://out", TIMEOUT);
98 assertThat(muleMessage, IsNull.nullValue());
99 }
100 }