View Javadoc

1   /*
2    * $Id: ExceptionPropagationMule5737TestCase.java 22772 2011-08-27 15:20:15Z dfeist $
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.test.integration.exceptions;
12  
13  import static org.junit.Assert.assertFalse;
14  import static org.junit.Assert.assertTrue;
15  
16  import org.mule.api.MuleEvent;
17  import org.mule.api.MuleException;
18  import org.mule.api.client.MuleClient;
19  import org.mule.api.construct.FlowConstruct;
20  import org.mule.exception.AbstractMessagingExceptionStrategy;
21  import org.mule.tck.junit4.FunctionalTestCase;
22  
23  import org.junit.Test;
24  
25  /**
26   * Assert that flows do not propagate exceptions via vm request-response endpoints or use of flow-ref. Also
27   * assert that a sub-flow/processor-chain does not handle it's own exception but they are rather handled by
28   * calling flow.
29   */
30  public class ExceptionPropagationMule5737TestCase extends FunctionalTestCase
31  {
32  
33      @Override
34      protected String getConfigResources()
35      {
36          return "org/mule/test/integration/exceptions/exception-propagation-mule-5737-config.xml";
37      }
38  
39      @Test
40      public void testVMRequestResponseEndpointExceptionPropagation() throws MuleException
41      {
42          MuleClient client = muleContext.getClient();
43          client.send("vm://flow-in", "", null);
44      }
45  
46      @Test
47      public void testFlowWithChildFlowExceptionPropagation() throws MuleException
48      {
49          MuleClient client = muleContext.getClient();
50          FlowConstruct flow = muleContext.getRegistry().lookupFlowConstruct("flowWithChildFlow");
51          FlowConstruct childFlow = muleContext.getRegistry().lookupFlowConstruct("childFlow");
52          SensingExceptionStrategy parentES = (SensingExceptionStrategy) flow.getExceptionListener();
53          SensingExceptionStrategy childFlowES = (SensingExceptionStrategy) childFlow.getExceptionListener();
54  
55          client.send("vm://flowWithChildFlow-in", "", null);
56  
57          assertFalse(parentES.caught);
58          assertTrue(childFlowES.caught);
59  
60      }
61  
62      @Test
63      public void testFlowWithSubFlowExceptionPropagation() throws MuleException
64      {
65          MuleClient client = muleContext.getClient();
66          SensingExceptionStrategy parentES = (SensingExceptionStrategy) muleContext.getRegistry()
67              .lookupFlowConstruct("flowWithSubFlow")
68              .getExceptionListener();
69  
70          client.send("vm://flowWithSubFlow-in", "", null);
71  
72          assertTrue(parentES.caught);
73      }
74  
75      @Test
76      public void testFlowWithChildServiceExceptionPropagation() throws MuleException
77      {
78          MuleClient client = muleContext.getClient();
79          SensingExceptionStrategy parentES = (SensingExceptionStrategy) muleContext.getRegistry()
80              .lookupFlowConstruct("flowWithChildService")
81              .getExceptionListener();
82          SensingExceptionStrategy childServiceES = (SensingExceptionStrategy) muleContext.getRegistry()
83              .lookupFlowConstruct("childService")
84              .getExceptionListener();
85  
86          client.send("vm://flowWithChildService-in", "", null);
87  
88          assertFalse(parentES.caught);
89          assertTrue(childServiceES.caught);
90      }
91  
92      public static class SensingExceptionStrategy extends AbstractMessagingExceptionStrategy
93      {
94  
95          public SensingExceptionStrategy()
96          {
97              super(null);
98          }
99  
100         boolean caught;
101 
102         @Override
103         public MuleEvent handleException(Exception e, MuleEvent event)
104         {
105             caught = true;
106             return super.handleException(e, event);
107         }
108 
109     }
110 
111 }