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