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