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.MuleContext;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.processor.MessageProcessor;
12  import org.mule.api.transaction.TransactionException;
13  import org.mule.exception.DefaultServiceExceptionStrategy;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.junit4.FunctionalTestCase;
16  import org.mule.transaction.TransactionCoordination;
17  
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.fail;
22  
23  /**
24   * When exception strategies are used with transactions it should be possible to send
25   * the exception message while rolling back the transaction. See MULE-4338
26   */
27  public class ExceptionStrategyTransactionTestCase extends FunctionalTestCase
28  {
29  
30      private static String failure;
31  
32      @Override
33      protected String getConfigResources()
34      {
35          return "org/mule/test/integration/exceptions/exception-strategy-transaction-test.xml";
36      }
37  
38      @Test
39      public void testRequestReply() throws Exception
40      {
41          MuleClient client = new MuleClient(muleContext);
42          client.dispatch("InputQueueClient", "payload", null);
43  
44          // There should be a message on ExceptionQueue
45          assertNotNull(client.request("ExceptionQueue", 10000));
46  
47          if (failure != null)
48          {
49              fail(failure);
50          }
51      }
52  
53      @Test
54      public void testNoInfiniteLoop() throws Exception
55      {
56          MuleClient client = new MuleClient(muleContext);
57          client.send("InputQueueClient2", "payload", null);
58  
59          Thread.sleep(500);
60          
61          if (failure != null)
62          {
63              fail(failure);
64          }
65  
66      }
67  
68      static class AssertRollbackServiceExceptionStrategy extends DefaultServiceExceptionStrategy
69      {
70          private int visits = 0;
71  
72          public AssertRollbackServiceExceptionStrategy(MuleContext context)
73          {
74              super(context);
75          }
76          
77          @Override
78          protected void routeException(MuleEvent event, MessageProcessor target, Throwable t)
79          {
80              super.routeException(event, target, t);
81  
82              if (visits++ > 1)
83              {
84                  failure = "Exception strategy should only be called once";
85                  fail("Exception strategy should only be called once");
86              }
87  
88              try
89              {
90                  if (TransactionCoordination.getInstance().getTransaction() != null &&
91                      !TransactionCoordination.getInstance().getTransaction().isRollbackOnly())
92                  {
93                      failure = "transaction should have been set for rollback";
94                  }
95              }
96              catch (TransactionException e)
97              {
98                  failure = e.getMessage();
99                  fail(e.getMessage());
100             }
101         }
102     }
103 
104 }