View Javadoc

1   /*
2    * $Id: JmsXAAlwaysBeginTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.transport.jms.integration;
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  import org.junit.Ignore;
24  import org.junit.Test;
25  
26  public class JmsXAAlwaysBeginTestCase extends AbstractJmsFunctionalTestCase
27  {
28  
29      private static final List committedTx = new CopyOnWriteArrayList();
30      private static final List rolledbackTx = new CopyOnWriteArrayList();
31      protected static final Log logger = LogFactory.getLog(JmsXAAlwaysBeginTestCase.class);
32  
33      protected String getConfigResources()
34      {
35          return "integration/jms-xa-tx-ALWAYS_BEGIN.xml";
36      }
37  
38      @Override
39      protected void suitePreSetUp() throws Exception
40      {
41          super.suitePreSetUp();
42          purge(getInboundQueueName());
43          purge(getOutboundQueueName());
44      }
45  
46      @Override
47      protected void suitePostTearDown() throws Exception
48      {
49          super.suitePostTearDown();
50          purge(getInboundQueueName());
51          purge(getOutboundQueueName());
52      }
53  
54      @Test
55      public void testAlwaysBeginTx() throws Exception
56      {
57          send(scenarioNoTx);
58          receive(scenarioNoTx);
59          receive(scenarioNoTx);
60          receive(scenarioNotReceive);
61          assertEquals(committedTx.size(), 0);
62          assertEquals(rolledbackTx.size(), 2);
63      }
64  
65      @Ignore
66      public static class TestRollbackComponent
67      {
68  
69          public Object processObject(Object a) throws Exception
70          {
71              logger.debug("TestRollbackComponent " + a);
72              TestResource res = new TestResource();
73              Transaction currentTrans = muleContext.getTransactionManager().getTransaction();
74              currentTrans.enlistResource(res);
75              currentTrans.setRollbackOnly();
76              return DEFAULT_OUTPUT_MESSAGE;
77          }
78      }
79  
80      @Ignore
81      public static class TestResource implements XAResource
82      {
83  
84          public void commit(Xid id, boolean onePhase) throws XAException
85          {
86              committedTx.add(id);
87              logger.debug("XA_COMMIT[" + id + "]");
88          }
89  
90          public void end(Xid xid, int flags) throws XAException
91          {
92              logger.debug("XA_END[" + xid + "] Flags=" + flags);
93          }
94  
95          public void forget(Xid xid) throws XAException
96          {
97              logger.debug("XA_FORGET[" + xid + "]");
98          }
99  
100         public int getTransactionTimeout() throws XAException
101         {
102             return (_timeout);
103         }
104 
105         public boolean isSameRM(XAResource xares) throws XAException
106         {
107             return (xares.equals(this));
108         }
109 
110         public int prepare(Xid xid) throws XAException
111         {
112             logger.debug("XA_PREPARE[" + xid + "]");
113 
114             return (XA_OK);
115         }
116 
117         public Xid[] recover(int flag) throws XAException
118         {
119             logger.debug("RECOVER[" + flag + "]");
120             return (null);
121         }
122 
123         public void rollback(Xid xid) throws XAException
124         {
125             rolledbackTx.add(xid);
126             logger.debug("XA_ROLLBACK[" + xid + "]");
127         }
128 
129         public boolean setTransactionTimeout(int seconds) throws XAException
130         {
131             _timeout = seconds;
132             return (true);
133         }
134 
135         public void start(Xid xid, int flags) throws XAException
136         {
137             logger.debug("XA_START[" + xid + "] Flags=" + flags);
138         }
139 
140         protected int _timeout = 0;
141     }
142 }