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 org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.processor.MessageProcessor;
13  import org.mule.message.ExceptionMessage;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.exceptions.FunctionalTestException;
16  import org.mule.tck.junit4.FunctionalTestCase;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertTrue;
25  
26  public class ExceptionStrategyConstructsTestCase extends FunctionalTestCase
27  {
28      protected transient Log logger = LogFactory.getLog(getClass());
29  
30      @Override
31      protected String getConfigResources()
32      {
33          return "org/mule/test/integration/exceptions/exception-strategy-constructs-config.xml";
34      }
35  
36      @Test
37      public void testDefaultExceptionStrategySingleEndpoint() throws Exception
38      {
39          MuleClient mc = new MuleClient(muleContext);
40         
41          mc.dispatch("vm://inservice2", "test", null);
42          assertExceptionMessage(mc.request("vm://modelout", RECEIVE_TIMEOUT));
43  
44          mc.dispatch("vm://inservice1", "test", null);
45          assertExceptionMessage(mc.request("vm://service1out", RECEIVE_TIMEOUT));
46  
47          // request one more time to ensure the model's exception strategy did not run
48          assertNull(mc.request("vm://modelout", RECEIVE_TIMEOUT));
49  
50          mc.dispatch("vm://inflow1", "test", null);
51          assertExceptionMessage(mc.request("vm://flow1out", RECEIVE_TIMEOUT));
52  
53          // request one more time to ensure the model's exception strategy did not run
54          assertNull(mc.request("vm://modelout", RECEIVE_TIMEOUT));
55  
56          mc.send("vm://inss1", "test", null);
57          assertExceptionMessage(mc.request("vm://ss1out", RECEIVE_TIMEOUT));
58  
59          // request one more time to ensure the model's exception strategy did not run
60          assertNull(mc.request("vm://modelout", RECEIVE_TIMEOUT));
61  
62          mc.send("vm://inss2", "test", null);
63          MuleMessage modelError = mc.request("vm://modelout", RECEIVE_TIMEOUT);
64  
65          // This should not be null.  MULE-5087
66          assertEquals(null, modelError);
67      }
68  
69      private void assertExceptionMessage(MuleMessage out)
70      {
71          assertTrue(out.getPayload() instanceof ExceptionMessage);
72          ExceptionMessage exceptionMessage = (ExceptionMessage) out.getPayload();
73          assertTrue(exceptionMessage.getException().getClass() == FunctionalTestException.class ||
74                     exceptionMessage.getException().getCause().getClass() == FunctionalTestException.class);
75          assertEquals("test", exceptionMessage.getPayload());
76      }
77  
78      public static class ExceptionThrowingProcessor implements MessageProcessor
79      {
80          public MuleEvent process(MuleEvent event) throws MuleException
81          {
82              throw new FunctionalTestException();
83          }
84      }
85  
86      public static class ExceptionThrowingComponent
87      {
88          public byte[] process(byte[] msg) throws MuleException
89          {
90              throw new FunctionalTestException();
91          }
92      }
93      
94  }