View Javadoc

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