View Javadoc

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