1   /*
2    * $Id$
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.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.service.Service;
16  import org.mule.message.ExceptionMessage;
17  import org.mule.module.client.MuleClient;
18  import org.mule.service.DefaultServiceExceptionStrategy;
19  import org.mule.tck.FunctionalTestCase;
20  import org.mule.tck.exceptions.FunctionalTestException;
21  
22  public class DefaultServiceExceptionStrategyTestCase extends FunctionalTestCase
23  {
24  
25      // @Override
26      protected String getConfigResources()
27      {
28          return "org/mule/test/integration/exceptions/default-service-exception-strategy-config.xml";
29      }
30  
31      public void testDefaultExceptionStrategySingleEndpoint() throws MuleException
32      {
33          Service service1 = muleContext.getRegistry().lookupService("testService1");
34          assertNotNull(service1);
35          assertNotNull(service1.getExceptionListener());
36          assertTrue(service1.getExceptionListener() instanceof DefaultServiceExceptionStrategy);
37          assertEquals(1, ((DefaultServiceExceptionStrategy) service1.getExceptionListener()).getEndpoints().size());
38  
39          MuleClient mc = new MuleClient();
40          mc.dispatch("vm://in1", "test", null);
41          assertExceptionMessage(mc.request("vm://out1", FunctionalTestCase.RECEIVE_TIMEOUT));
42      }
43  
44      public void testDefaultExceptionStrategyMultipleEndpoints() throws MuleException
45      {
46          Service service2 = muleContext.getRegistry().lookupService("testService2");
47          assertNotNull(service2);
48          assertNotNull(service2.getExceptionListener());
49          assertTrue(service2.getExceptionListener() instanceof DefaultServiceExceptionStrategy);
50          assertEquals(2, ((DefaultServiceExceptionStrategy) service2.getExceptionListener()).getEndpoints().size());
51  
52          MuleClient mc = new MuleClient();
53          mc.dispatch("vm://in2", "test", null);
54          MuleMessage out2 = mc.request("vm://out2", FunctionalTestCase.RECEIVE_TIMEOUT);
55          MuleMessage out3 = mc.request("vm://out3", FunctionalTestCase.RECEIVE_TIMEOUT);
56          assertExceptionMessage(out2);
57          assertExceptionMessage(out3);
58          assertNotSame(out2, out3);
59          assertEquals(out2.getPayload(), out3.getPayload());
60      }
61  
62      private void assertExceptionMessage(MuleMessage out)
63      {
64          assertTrue(out.getPayload() instanceof ExceptionMessage);
65          assertEquals(FunctionalTestException.class, ((ExceptionMessage) out.getPayload()).getException()
66              .getCause()
67              .getClass());
68          assertEquals("test", ((ExceptionMessage) out.getPayload()).getPayload());
69      }
70  }