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.DefaultMuleException;
10  import org.mule.api.transaction.Transaction;
11  import org.mule.config.i18n.CoreMessages;
12  import org.mule.exception.DefaultSystemExceptionStrategy;
13  import org.mule.routing.filters.WildcardFilter;
14  import org.mule.tck.junit4.AbstractMuleContextTestCase;
15  import org.mule.tck.testmodels.mule.TestTransaction;
16  import org.mule.transaction.TransactionCoordination;
17  
18  import java.io.FileNotFoundException;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  
25  public class ExceptionRollbackTestCase extends AbstractMuleContextTestCase
26  {
27      private DefaultSystemExceptionStrategy strategy;
28      private Transaction tx;
29  
30      @Override
31      protected void doSetUp() throws Exception
32      {
33          strategy = new DefaultSystemExceptionStrategy(muleContext);
34          strategy.setCommitTxFilter(new WildcardFilter("java.io.*"));
35          strategy.setRollbackTxFilter(new WildcardFilter("org.mule.*, javax.*"));
36  
37          initialiseObject(strategy);
38          tx = new TestTransaction(muleContext);
39          TransactionCoordination.getInstance().bindTransaction(tx);
40      }
41  
42      @Override
43      protected void doTearDown() throws Exception
44      {
45          TransactionCoordination.getInstance().unbindTransaction(tx);
46      }
47  
48      @Test
49      public void testCommit() throws Exception
50      {
51          strategy.handleException(new FileNotFoundException());
52          assertFalse(tx.isRollbackOnly());
53          //There is nothing to actually commit the transaction since we are not running in a real tx
54          //assertTrue(tx.isCommitted());
55      }
56  
57      @Test
58      public void testRollback() throws Exception
59      {
60          strategy.handleException(new DefaultMuleException(CoreMessages.agentsRunning()));
61          assertTrue(tx.isRolledBack());
62          //There is nothing to actually commit the transaction since we are not running in a real tx
63          assertFalse(tx.isCommitted());
64      }
65  
66      @Test
67      public void testRollbackByDefault() throws Exception
68      {
69          strategy.handleException(new IllegalAccessException());
70          assertTrue(tx.isRolledBack());
71          //There is nothing to actually commit the transaction since we are not running in a real tx
72          assertFalse(tx.isCommitted());
73      }
74  }