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