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.test.integration.transaction;
8   
9   import org.mule.module.jboss.transaction.JBossArjunaTransactionManagerFactory;
10  import org.mule.tck.junit4.AbstractMuleContextTestCase;
11  import org.mule.util.xa.AbstractTransactionContext;
12  import org.mule.util.xa.AbstractXAResourceManager;
13  import org.mule.util.xa.DefaultXASession;
14  import org.mule.util.xa.ResourceManagerException;
15  
16  import javax.transaction.Transaction;
17  import javax.transaction.TransactionManager;
18  import javax.transaction.xa.XAResource;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.junit.Test;
23  
24  public class XAResourceManagerTestCase extends AbstractMuleContextTestCase
25  {
26      private TransactionManager tm;
27  
28      protected void doSetUp() throws Exception
29      {
30          tm = new JBossArjunaTransactionManagerFactory().create(muleContext.getConfiguration());
31      }
32  
33      protected void doTearDown() throws Exception
34      {
35          tm = null;
36      }
37  
38      @Test
39      public void testTxBehaviour() throws Exception
40      {
41          TestXAResourceManager rm = new TestXAResourceManager();
42          rm.start();
43          DefaultXASession s = rm.createSession();
44  
45          tm.begin();
46          Transaction tx = tm.getTransaction();
47          tx.enlistResource(s);
48  
49          tx.delistResource(s, XAResource.TMSUCCESS);
50          tx.commit();
51      }
52  
53      protected static class TestXAResourceManager extends AbstractXAResourceManager
54      {
55  
56          private static Log logger = LogFactory.getLog(TestXAResourceManager.class);
57  
58          public DefaultXASession createSession()
59          {
60              return new DefaultXASession(this);
61          }
62  
63          protected Log getLogger()
64          {
65              return logger;
66          }
67  
68          protected AbstractTransactionContext createTransactionContext(Object session)
69          {
70              return new AbstractTransactionContext() 
71              {
72              };
73          }
74  
75          protected void doBegin(AbstractTransactionContext context)
76          {
77              // template method
78          }
79  
80          protected int doPrepare(AbstractTransactionContext context)
81          {
82              // template method
83              return 0;
84          }
85  
86          protected void doCommit(AbstractTransactionContext context) throws ResourceManagerException
87          {
88              // template method
89          }
90  
91          protected void doRollback(AbstractTransactionContext context) throws ResourceManagerException
92          {
93              // template method
94          }
95      }
96  }