View Javadoc

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