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.processor;
8   
9   import org.mule.MessageExchangePattern;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.endpoint.OutboundEndpoint;
12  import org.mule.api.processor.InterceptingMessageProcessor;
13  import org.mule.endpoint.AbstractMessageProcessorTestCase;
14  import org.mule.transport.NullPayload;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertSame;
22  import static org.junit.Assert.assertTrue;
23  
24  public class ExceptionHandlingMessageProcessorTestCase extends AbstractMessageProcessorTestCase
25  {
26      private TestExceptionListener exceptionListener;
27  
28      @Override
29      protected void doSetUp() throws Exception
30      {
31          super.doSetUp();
32          exceptionListener = new TestExceptionListener();
33      }
34  
35      @Test
36      public void testNoCatch() throws Exception
37      {
38          OutboundEndpoint endpoint = createTestOutboundEndpoint(null, null);
39          InterceptingMessageProcessor mp = new ExceptionHandlingMessageProcessor();
40          TestListener listener = new TestListener();
41          mp.setListener(listener);
42  
43          MuleEvent event = createTestOutboundEvent(endpoint);
44  
45          MuleEvent result = mp.process(event);
46  
47          assertSame(event, listener.sensedEvent);
48          assertSame(event, result);
49          assertNull(exceptionListener.sensedException);
50      }
51  
52      @Test
53      public void testCatchRuntimeExceptionSync() throws Exception
54      {
55          OutboundEndpoint endpoint = createTestOutboundEndpoint(null, null);
56          InterceptingMessageProcessor mp = new ExceptionHandlingMessageProcessor();
57          mp.setListener(new ExceptionThrowingMessageProcessr());
58  
59          MuleEvent event = createTestOutboundEvent(endpoint, exceptionListener);
60  
61          MuleEvent resultEvent = mp.process(event);
62          assertNotNull(resultEvent);
63          assertNotNull("exception expected", resultEvent.getMessage().getExceptionPayload());
64          assertTrue(resultEvent.getMessage().getExceptionPayload().getException() instanceof IllegalStateException);
65  
66          assertEquals(NullPayload.getInstance(), resultEvent.getMessage().getPayload());
67          assertNotNull(exceptionListener.sensedException);
68      }
69  
70      @Test
71      public void testCatchRuntimeExceptionAsync() throws Exception
72      {
73          OutboundEndpoint endpoint = createTestOutboundEndpoint(null, null, 
74              MessageExchangePattern.ONE_WAY, null);
75          InterceptingMessageProcessor mp = new ExceptionHandlingMessageProcessor();
76          mp.setListener(new ExceptionThrowingMessageProcessr());
77  
78          MuleEvent event = createTestOutboundEvent(endpoint, exceptionListener);
79  
80          MuleEvent resultEvent = mp.process(event);
81          assertNotNull(resultEvent);
82          assertNotNull("exception expected", resultEvent.getMessage().getExceptionPayload());
83          assertTrue(resultEvent.getMessage().getExceptionPayload().getException() instanceof IllegalStateException);
84  
85          assertEquals(NullPayload.getInstance(), resultEvent.getMessage().getPayload());
86          assertNotNull(exceptionListener.sensedException);
87      }
88  
89      @Test
90      public void testCatchDispatchExceptionSync() throws Exception
91      {
92          OutboundEndpoint endpoint = createTestOutboundEndpoint(null, null);
93          InterceptingMessageProcessor mp = new ExceptionHandlingMessageProcessor();
94          mp.setListener(new ExceptionThrowingMessageProcessr());
95  
96          MuleEvent event = createTestOutboundEvent(endpoint, exceptionListener);
97  
98          MuleEvent resultEvent = mp.process(event);
99          assertNotNull(resultEvent);
100         assertNotNull("exception expected", resultEvent.getMessage().getExceptionPayload());
101         assertTrue(resultEvent.getMessage().getExceptionPayload().getException() instanceof IllegalStateException);
102 
103         assertEquals(NullPayload.getInstance(), resultEvent.getMessage().getPayload());
104         assertNotNull(exceptionListener.sensedException);
105     }
106 
107     @Test
108     public void testCatchDispatchExceptionAsync() throws Exception
109     {
110         OutboundEndpoint endpoint = createTestOutboundEndpoint(null, null, 
111             MessageExchangePattern.ONE_WAY, null);
112         InterceptingMessageProcessor mp = new ExceptionHandlingMessageProcessor();
113         mp.setListener(new ExceptionThrowingMessageProcessr());
114 
115         MuleEvent event = createTestOutboundEvent(endpoint, exceptionListener);
116 
117         MuleEvent resultEvent = mp.process(event);
118         assertNotNull(resultEvent);
119         assertNotNull("exception expected", resultEvent.getMessage().getExceptionPayload());
120         assertTrue(resultEvent.getMessage().getExceptionPayload().getException() instanceof IllegalStateException);
121 
122         assertEquals(NullPayload.getInstance(), resultEvent.getMessage().getPayload());
123         assertNotNull(exceptionListener.sensedException);
124     }
125 }