View Javadoc

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