View Javadoc

1   /*
2    * $Id: TransactionCoordinationTestCase.java 22377 2011-07-11 12:41:42Z 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.transaction;
12  
13  import org.mule.api.transaction.Transaction;
14  import org.mule.tck.junit4.AbstractMuleTestCase;
15  
16  import org.junit.After;
17  import org.junit.Before;
18  import org.junit.Test;
19  import org.mockito.Mockito;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.fail;
24  
25  public class TransactionCoordinationTestCase extends AbstractMuleTestCase
26  {
27      private TransactionCoordination tc;
28  
29      @Before
30      public void setUpTransaction() throws Exception
31      {
32          tc = TransactionCoordination.getInstance();
33      }
34  
35      @After
36      public void unbindTransaction() throws Exception
37      {
38          tc.unbindTransaction(tc.getTransaction());
39      }
40  
41      @Test
42      public void testBindTransaction() throws Exception
43      {
44          assertNull(tc.getTransaction());
45          Transaction tx = Mockito.mock(Transaction.class);
46  
47          tc.bindTransaction(tx);
48          assertEquals(tx, tc.getTransaction());
49          tc.unbindTransaction(tx);
50      }
51  
52      @Test
53      public void testBindTransactionWithAlreadyBound() throws Exception
54      {
55          assertNull(tc.getTransaction());
56          Transaction tx = Mockito.mock(Transaction.class);
57  
58          tc.bindTransaction(tx);
59          assertEquals(tx, tc.getTransaction());
60  
61          try
62          {
63              Transaction tx2 = Mockito.mock(Transaction.class);
64              tc.bindTransaction(tx2);
65              fail();
66          }
67          catch (IllegalTransactionStateException e)
68          {
69              // expected
70          }
71  
72          tc.unbindTransaction(tx);
73      }
74  
75      @Test
76      public void testUnbindTransactionWithoutBound() throws Exception
77      {
78          assertNull(tc.getTransaction());
79          Transaction tx = Mockito.mock(Transaction.class);
80  
81          tc.unbindTransaction(tx);
82      }
83  
84      @Test
85      public void testSetInstanceWithBound() throws Exception
86      {
87          assertNull(tc.getTransaction());
88          Transaction tx = Mockito.mock(Transaction.class);
89  
90          tc.bindTransaction(tx);
91  
92          tc.unbindTransaction(tx);
93      }
94  }