View Javadoc

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