View Javadoc

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