View Javadoc

1   /*
2    * $Id: IsTransactedTestCase.java 22654 2011-08-12 07:32:57Z mike.schilling $
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  package org.mule.transaction;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.MuleRuntimeException;
14  import org.mule.api.transaction.Transaction;
15  import org.mule.api.transaction.TransactionConfig;
16  import org.mule.api.transaction.TransactionException;
17  import org.mule.api.transaction.TransactionFactory;
18  import org.mule.tck.junit4.AbstractMuleTestCase;
19  
20  import org.junit.Test;
21  import org.mule.tck.testmodels.mule.TestTransaction;
22  
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  public class IsTransactedTestCase extends AbstractMuleTestCase
27  {
28      @Test
29      public void testIsTransacted() throws Exception
30      {
31          MuleTransactionConfig cfg = new MuleTransactionConfig();
32          TestTransaction testTx = new TestTransaction(null);
33  
34          cfg.setAction(TransactionConfig.ACTION_NEVER);
35          assertFalse(cfg.isTransacted());
36  
37          cfg.setAction(TransactionConfig.ACTION_NEVER);
38          assertFalse(cfg.isTransacted());
39  
40          cfg.setFactory(new TransactedFactory());
41          cfg.setAction(TransactionConfig.ACTION_ALWAYS_BEGIN);
42          assertTrue(cfg.isTransacted());
43          cfg.setAction(TransactionConfig.ACTION_ALWAYS_JOIN);
44          assertTrue(cfg.isTransacted());
45          cfg.setAction(TransactionConfig.ACTION_BEGIN_OR_JOIN);
46          assertTrue(cfg.isTransacted());
47          cfg.setAction(TransactionConfig.ACTION_JOIN_IF_POSSIBLE);
48          assertFalse(cfg.isTransacted());
49          TransactionCoordination.getInstance().bindTransaction(testTx);
50          assertTrue(cfg.isTransacted());
51          TransactionCoordination.getInstance().unbindTransaction(testTx);
52          cfg.setAction(TransactionConfig.ACTION_INDIFFERENT);
53          assertFalse(cfg.isTransacted());
54          TransactionCoordination.getInstance().bindTransaction(testTx);
55          assertTrue(cfg.isTransacted());
56          TransactionCoordination.getInstance().unbindTransaction(testTx);
57  
58          cfg.setFactory(new NonTransactedFactory());
59          cfg.setAction(TransactionConfig.ACTION_ALWAYS_BEGIN);
60          assertFalse(cfg.isTransacted());
61          cfg.setAction(TransactionConfig.ACTION_ALWAYS_JOIN);
62          assertFalse(cfg.isTransacted());
63          cfg.setAction(TransactionConfig.ACTION_BEGIN_OR_JOIN);
64          assertFalse(cfg.isTransacted());
65          cfg.setAction(TransactionConfig.ACTION_JOIN_IF_POSSIBLE);
66          assertFalse(cfg.isTransacted());
67          TransactionCoordination.getInstance().bindTransaction(testTx);
68          assertFalse(cfg.isTransacted());
69          TransactionCoordination.getInstance().unbindTransaction(testTx);
70          cfg.setAction(TransactionConfig.ACTION_INDIFFERENT);
71          assertFalse(cfg.isTransacted());
72          TransactionCoordination.getInstance().bindTransaction(testTx);
73          assertFalse(cfg.isTransacted());
74          TransactionCoordination.getInstance().unbindTransaction(testTx);
75      }
76  
77      @Test(expected = MuleRuntimeException.class)
78      public void testExpectException1()
79      {
80          MuleTransactionConfig cfg = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
81          cfg.isTransacted();
82      }
83  
84      @Test(expected = MuleRuntimeException.class)
85      public void testExpectException2()
86      {
87          MuleTransactionConfig cfg = new MuleTransactionConfig(TransactionConfig.ACTION_JOIN_IF_POSSIBLE);
88          cfg.isTransacted();
89      }
90  
91      public static class TransactedFactory implements TransactionFactory
92      {
93          @Override
94          public Transaction beginTransaction(MuleContext muleContext) throws TransactionException
95          {
96              return null;
97          }
98  
99          @Override
100         public boolean isTransacted()
101         {
102             return true;
103         }
104     }
105 
106     public static class NonTransactedFactory implements TransactionFactory
107     {
108         @Override
109         public Transaction beginTransaction(MuleContext muleContext) throws TransactionException
110         {
111             return null;
112         }
113 
114         @Override
115         public boolean isTransacted()
116         {
117             return false;
118         }
119     }
120 
121 }