1   /*
2    * $Id: JmsXAAlwaysBeginTestCase.java 10524 2008-01-24 19:20:11Z akuzmin $
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.test.integration.providers.jms.functional;
11  
12  import org.mule.MuleManager;
13  
14  import java.util.List;
15  
16  import javax.jms.JMSException;
17  import javax.jms.Message;
18  import javax.jms.MessageConsumer;
19  import javax.jms.MessageProducer;
20  import javax.jms.Session;
21  import javax.jms.TextMessage;
22  import javax.transaction.Transaction;
23  import javax.transaction.xa.XAException;
24  import javax.transaction.xa.XAResource;
25  import javax.transaction.xa.Xid;
26  
27  import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  public class JmsXAAlwaysBeginTestCase extends AbstractJmsFunctionalTestCase
33  {
34  
35      private static final List committedTx = new CopyOnWriteArrayList();
36      private static final List rolledbackTx = new CopyOnWriteArrayList();
37      protected static final Log logger = LogFactory.getLog(JmsXAAlwaysBeginTestCase.class);
38  
39      protected String getConfigResources()
40      {
41          return "providers/activemq/jms-xa-tx-ALWAYS_BEGIN.xml";
42      }
43  
44      public void testAlwaysBeginTx() throws Exception
45      {
46          CurrentScenario scenarioNoTx = new CurrentScenario(DEFAULT_INPUT_MQ_QUEUE_NAME, DEFAULT_OUTPUT_MQ_QUEUE_NAME, true);
47          send(scenarioNoTx);
48          receive(scenarioNoTx);
49          receive(scenarioNoTx);
50  
51          scenarioNoTx.setRecieve(false);
52          receive(scenarioNoTx);
53          assertEquals(committedTx.size(), 0);
54          assertEquals(rolledbackTx.size(), 2);
55      }
56  
57  
58      public static class TestRollbackComponent
59      {
60  
61          public Object processObject(Object a) throws Exception
62          {
63              logger.debug("TestRollbackComponent " + a);
64              TestResource res = new TestResource();
65              Transaction currentTrans = MuleManager.getInstance().getTransactionManager().getTransaction();
66              currentTrans.enlistResource(res);
67              currentTrans.setRollbackOnly();
68              return DEFAULT_OUTPUT_MESSAGE;
69          }
70      }
71  
72      public static class TestResource implements XAResource
73      {
74  
75          public void commit(Xid id, boolean onePhase) throws XAException
76          {
77              committedTx.add(id);
78              logger.debug("XA_COMMIT[" + id + "]");
79          }
80  
81          public void end(Xid xid, int flags) throws XAException
82          {
83              logger.debug("XA_END[" + xid + "] Flags=" + flags);
84          }
85  
86          public void forget(Xid xid) throws XAException
87          {
88              logger.debug("XA_FORGET[" + xid + "]");
89          }
90  
91          public int getTransactionTimeout() throws XAException
92          {
93              return (_timeout);
94          }
95  
96          public boolean isSameRM(XAResource xares) throws XAException
97          {
98              return (xares.equals(this));
99          }
100 
101         public int prepare(Xid xid) throws XAException
102         {
103             logger.debug("XA_PREPARE[" + xid + "]");
104 
105             return (XA_OK);
106         }
107 
108         public Xid[] recover(int flag) throws XAException
109         {
110             logger.debug("RECOVER[" + flag + "]");
111             return (null);
112         }
113 
114         public void rollback(Xid xid) throws XAException
115         {
116             rolledbackTx.add(xid);
117             logger.debug("XA_ROLLBACK[" + xid + "]");
118         }
119 
120         public boolean setTransactionTimeout(int seconds) throws XAException
121         {
122             _timeout = seconds;
123             return (true);
124         }
125 
126         public void start(Xid xid, int flags) throws XAException
127         {
128             logger.debug("XA_START[" + xid + "] Flags=" + flags);
129         }
130 
131         protected int _timeout = 0;
132     }
133 
134     class CurrentScenario extends AbstractScenario
135     {
136 
137         private boolean recieve = true;
138 
139         public CurrentScenario(String inputQueue, String outputQueue, boolean recieve)
140         {
141             setInputQueue(inputQueue);
142             setOutputQueue(outputQueue);
143         }
144 
145         public boolean isRecieve()
146         {
147             return recieve;
148         }
149 
150         public void setRecieve(boolean recieve)
151         {
152             this.recieve = recieve;
153         }
154 
155         public void send(Session session, MessageProducer producer) throws JMSException
156         {
157             producer.send(session.createTextMessage(DEFAULT_INPUT_MESSAGE));
158         }
159 
160         public Message receive(Session session, MessageConsumer consumer) throws JMSException
161         {
162             Message message = consumer.receive(TIMEOUT);
163             if (isRecieve())
164             {
165                 assertNotNull(message);
166                 assertTrue(TextMessage.class.isAssignableFrom(message.getClass()));
167                 assertEquals(((TextMessage) message).getText(), DEFAULT_OUTPUT_MESSAGE);
168             }
169             else
170             {
171                 assertNull(message);
172             }
173             return message;
174         }
175     }
176 
177 }