1
2
3
4
5
6
7 package org.mule.transport.jms.integration;
8
9 import java.util.List;
10
11 import javax.transaction.Transaction;
12 import javax.transaction.xa.XAException;
13 import javax.transaction.xa.XAResource;
14 import javax.transaction.xa.Xid;
15
16 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.junit.Ignore;
20 import org.junit.Test;
21
22 import static org.junit.Assert.assertEquals;
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 @Override
32 protected String getConfigResources()
33 {
34 return "integration/jms-xa-tx-ALWAYS_BEGIN.xml";
35 }
36
37 @Override
38 protected void doSetUp() throws Exception
39 {
40 super.doSetUp();
41 purge(getInboundQueueName());
42 purge(getOutboundQueueName());
43 }
44
45 @Override
46 protected void doTearDown() throws Exception
47 {
48 super.doTearDown();
49 purge(getInboundQueueName());
50 purge(getOutboundQueueName());
51 }
52
53 @Test
54 public void testAlwaysBeginTx() throws Exception
55 {
56 send(scenarioNoTx);
57 receive(scenarioNoTx);
58 receive(scenarioNoTx);
59 receive(scenarioNotReceive);
60 assertEquals(committedTx.size(), 0);
61 assertEquals(rolledbackTx.size(), 2);
62 }
63
64 @Ignore
65 public static class TestRollbackComponent
66 {
67
68 public Object processObject(Object a) throws Exception
69 {
70 logger.debug("TestRollbackComponent " + a);
71 TestResource res = new TestResource();
72 Transaction currentTrans = muleContext.getTransactionManager().getTransaction();
73 currentTrans.enlistResource(res);
74 currentTrans.setRollbackOnly();
75 return DEFAULT_OUTPUT_MESSAGE;
76 }
77 }
78
79 @Ignore
80 public static class TestResource implements XAResource
81 {
82
83 public void commit(Xid id, boolean onePhase) throws XAException
84 {
85 committedTx.add(id);
86 logger.debug("XA_COMMIT[" + id + "]");
87 }
88
89 public void end(Xid xid, int flags) throws XAException
90 {
91 logger.debug("XA_END[" + xid + "] Flags=" + flags);
92 }
93
94 public void forget(Xid xid) throws XAException
95 {
96 logger.debug("XA_FORGET[" + xid + "]");
97 }
98
99 public int getTransactionTimeout() throws XAException
100 {
101 return (_timeout);
102 }
103
104 public boolean isSameRM(XAResource xares) throws XAException
105 {
106 return (xares.equals(this));
107 }
108
109 public int prepare(Xid xid) throws XAException
110 {
111 logger.debug("XA_PREPARE[" + xid + "]");
112
113 return (XA_OK);
114 }
115
116 public Xid[] recover(int flag) throws XAException
117 {
118 logger.debug("RECOVER[" + flag + "]");
119 return (null);
120 }
121
122 public void rollback(Xid xid) throws XAException
123 {
124 rolledbackTx.add(xid);
125 logger.debug("XA_ROLLBACK[" + xid + "]");
126 }
127
128 public boolean setTransactionTimeout(int seconds) throws XAException
129 {
130 _timeout = seconds;
131 return (true);
132 }
133
134 public void start(Xid xid, int flags) throws XAException
135 {
136 logger.debug("XA_START[" + xid + "] Flags=" + flags);
137 }
138
139 protected int _timeout = 0;
140 }
141 }