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;
8   
9   import org.mule.api.transaction.TransactionConfig;
10  import org.mule.api.transaction.TransactionException;
11  import org.mule.tck.junit4.AbstractMuleContextTestCase;
12  import org.mule.tck.testmodels.mule.TestTransactionFactory;
13  import org.mule.transaction.MuleTransactionConfig;
14  
15  import org.junit.Test;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.fail;
20  
21  public class MuleTransactionConfigTestCase extends AbstractMuleContextTestCase
22  {
23      @Test
24      public void testActionAndStringConversion()
25      {
26          MuleTransactionConfig c = new MuleTransactionConfig();
27          c.setMuleContext(muleContext);
28  
29          c.setAction(MuleTransactionConfig.ACTION_ALWAYS_BEGIN);
30          assertEquals(MuleTransactionConfig.ACTION_ALWAYS_BEGIN_STRING, c.getActionAsString());
31  
32          c.setAction(MuleTransactionConfig.ACTION_ALWAYS_JOIN);
33          assertEquals(MuleTransactionConfig.ACTION_ALWAYS_JOIN_STRING, c.getActionAsString());
34  
35          c.setAction(MuleTransactionConfig.ACTION_BEGIN_OR_JOIN);
36          assertEquals(MuleTransactionConfig.ACTION_BEGIN_OR_JOIN_STRING, c.getActionAsString());
37  
38          c.setAction(MuleTransactionConfig.ACTION_JOIN_IF_POSSIBLE);
39          assertEquals(MuleTransactionConfig.ACTION_JOIN_IF_POSSIBLE_STRING, c.getActionAsString());
40  
41          c.setAction(MuleTransactionConfig.ACTION_NONE);
42          assertEquals(MuleTransactionConfig.ACTION_NONE_STRING, c.getActionAsString());
43      }
44  
45      @Test
46      public void testDefaults() throws Exception {
47          MuleTransactionConfig c = new MuleTransactionConfig();
48          c.setMuleContext(muleContext);
49          assertEquals("Wrong default TX timeout", 30000, c.getTimeout());
50      }
51  
52      @Test
53      public void testTransactionJoinIfPossible() throws TransactionException
54      {      
55          MuleTransactionConfig txConfig = new MuleTransactionConfig();
56          txConfig.setMuleContext(muleContext);
57          txConfig.setAction(TransactionConfig.ACTION_JOIN_IF_POSSIBLE);
58          txConfig.setFactory(new TestTransactionFactory());
59          assertFalse(txConfig.isTransacted());
60      }
61  
62      @Test
63      public void testFailNoFactory()
64      {
65          MuleTransactionConfig txConfig = new MuleTransactionConfig();
66          txConfig.setMuleContext(muleContext);
67          txConfig.setAction(TransactionConfig.ACTION_ALWAYS_BEGIN);
68          // note how we don't set a factory here so the default in MTC is null
69          
70          try
71          {
72              txConfig.isTransacted();
73              fail("isTransacted() must fail if no factory is set");
74          }
75          catch (RuntimeException re)
76          {
77              // this was expected
78          }
79      }
80  
81  }