View Javadoc

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