View Javadoc

1   /*
2    * $Id: ExceptionStrategyConstructsTestCase.java 22421 2011-07-15 05:05:06Z dirk.olmes $
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.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.processor.MessageProcessor;
17  import org.mule.message.ExceptionMessage;
18  import org.mule.module.client.MuleClient;
19  import org.mule.tck.AbstractServiceAndFlowTestCase;
20  import org.mule.tck.exceptions.FunctionalTestException;
21  
22  import java.util.Arrays;
23  import java.util.Collection;
24  
25  import org.junit.Test;
26  import org.junit.runners.Parameterized.Parameters;
27  
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertNotNull;
30  import static org.junit.Assert.assertNull;
31  import static org.junit.Assert.assertTrue;
32  
33  public class ExceptionStrategyConstructsTestCase extends AbstractServiceAndFlowTestCase
34  {
35      @Parameters
36      public static Collection<Object[]> parameters()
37      {
38          return Arrays.asList(new Object[][]{
39              {ConfigVariant.SERVICE,
40                  "org/mule/test/integration/exceptions/exception-strategy-constructs-config-service.xml"},
41              {ConfigVariant.FLOW,
42                  "org/mule/test/integration/exceptions/exception-strategy-constructs-config-flow.xml"}});
43      }
44  
45      public ExceptionStrategyConstructsTestCase(ConfigVariant variant, String configResources)
46      {
47          super(variant, configResources);
48      }
49  
50      @Test
51      public void testDefaultExceptionStrategySingleEndpoint() throws Exception
52      {
53          MuleClient mc = new MuleClient(muleContext);
54  
55          mc.dispatch("vm://inservice2", "test", null);
56          assertExceptionMessage(mc.request("vm://modelout", RECEIVE_TIMEOUT));
57  
58          mc.dispatch("vm://inservice1", "test", null);
59          assertExceptionMessage(mc.request("vm://service1out", RECEIVE_TIMEOUT));
60  
61          // request one more time to ensure the model's exception strategy did not run
62          assertNull(mc.request("vm://modelout", RECEIVE_TIMEOUT));
63  
64          mc.dispatch("vm://inflow1", "test", null);
65          assertExceptionMessage(mc.request("vm://flow1out", RECEIVE_TIMEOUT));
66  
67          // request one more time to ensure the model's exception strategy did not run
68          assertNull(mc.request("vm://modelout", RECEIVE_TIMEOUT));
69  
70          // The following tests no longer apply because if the exchange is synchronous
71          // (which is hard-coded for <pattern:simple-service>), then the exception
72          // will be
73          // thrown back to the caller and no exception strategy will be invoked.
74          /*
75           * mc.send("vm://inss1", "test", null);
76           * assertExceptionMessage(mc.request("vm://ss1out", RECEIVE_TIMEOUT)); //
77           * request one more time to ensure the model's exception strategy did not run
78           * assertNull(mc.request("vm://modelout", RECEIVE_TIMEOUT));
79           * mc.send("vm://inss2", "test", null); MuleMessage modelError =
80           * mc.request("vm://modelout", RECEIVE_TIMEOUT); // This should not be null.
81           * MULE-5087 assertEquals(null, modelError);
82           */
83      }
84  
85      private void assertExceptionMessage(MuleMessage out)
86      {
87          assertNotNull(out);
88          assertTrue(out.getPayload() instanceof ExceptionMessage);
89          ExceptionMessage exceptionMessage = (ExceptionMessage) out.getPayload();
90          assertTrue(exceptionMessage.getException().getClass() == FunctionalTestException.class
91                     || exceptionMessage.getException().getCause().getClass() == FunctionalTestException.class);
92          assertEquals("test", exceptionMessage.getPayload());
93      }
94  
95      public static class ExceptionThrowingProcessor implements MessageProcessor
96      {
97          @Override
98          public MuleEvent process(MuleEvent event) throws MuleException
99          {
100             throw new FunctionalTestException();
101         }
102     }
103 
104     public static class ExceptionThrowingComponent
105     {
106         public byte[] process(byte[] msg) throws MuleException
107         {
108             throw new FunctionalTestException();
109         }
110     }
111 }