View Javadoc

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