1   /*
2    * $Id: JmsXAAlwaysBeginTestCase.java 10787 2008-02-12 18:51:50Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  package org.mule.transport.jms.integration;
11  
12  
13  import java.util.List;
14  
15  import javax.transaction.Transaction;
16  import javax.transaction.xa.XAException;
17  import javax.transaction.xa.XAResource;
18  import javax.transaction.xa.Xid;
19  
20  import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  public class JmsXAAlwaysBeginTestCase extends AbstractJmsFunctionalTestCase
25  {
26  
27      private static final List committedTx = new CopyOnWriteArrayList();
28      private static final List rolledbackTx = new CopyOnWriteArrayList();
29      protected static final Log logger = LogFactory.getLog(JmsXAAlwaysBeginTestCase.class);
30  
31      protected String getConfigResources()
32      {
33          return "providers/activemq/jms-xa-tx-ALWAYS_BEGIN.xml";
34      }
35  
36      public void testAlwaysBeginTx() throws Exception
37      {
38          send(scenarioNoTx);
39          receive(scenarioNoTx);
40          receive(scenarioNoTx);
41          receive(scenarioNotReceive);
42          assertEquals(committedTx.size(), 0);
43          assertEquals(rolledbackTx.size(), 2);
44      }
45  
46  
47      public static class TestRollbackComponent
48      {
49  
50          public Object processObject(Object a) throws Exception
51          {
52              logger.debug("TestRollbackComponent " + a);
53              TestResource res = new TestResource();
54              Transaction currentTrans = muleContext.getTransactionManager().getTransaction();
55              currentTrans.enlistResource(res);
56              currentTrans.setRollbackOnly();
57              return DEFAULT_OUTPUT_MESSAGE;
58          }
59      }
60  
61      public static class TestResource implements XAResource
62      {
63  
64          public void commit(Xid id, boolean onePhase) throws XAException
65          {
66              committedTx.add(id);
67              logger.debug("XA_COMMIT[" + id + "]");
68          }
69  
70          public void end(Xid xid, int flags) throws XAException
71          {
72              logger.debug("XA_END[" + xid + "] Flags=" + flags);
73          }
74  
75          public void forget(Xid xid) throws XAException
76          {
77              logger.debug("XA_FORGET[" + xid + "]");
78          }
79  
80          public int getTransactionTimeout() throws XAException
81          {
82              return (_timeout);
83          }
84  
85          public boolean isSameRM(XAResource xares) throws XAException
86          {
87              return (xares.equals(this));
88          }
89  
90          public int prepare(Xid xid) throws XAException
91          {
92              logger.debug("XA_PREPARE[" + xid + "]");
93  
94              return (XA_OK);
95          }
96  
97          public Xid[] recover(int flag) throws XAException
98          {
99              logger.debug("RECOVER[" + flag + "]");
100             return (null);
101         }
102 
103         public void rollback(Xid xid) throws XAException
104         {
105             rolledbackTx.add(xid);
106             logger.debug("XA_ROLLBACK[" + xid + "]");
107         }
108 
109         public boolean setTransactionTimeout(int seconds) throws XAException
110         {
111             _timeout = seconds;
112             return (true);
113         }
114 
115         public void start(Xid xid, int flags) throws XAException
116         {
117             logger.debug("XA_START[" + xid + "] Flags=" + flags);
118         }
119 
120         protected int _timeout = 0;
121     }
122 }