Source code: org/activemq/service/impl/XATransactionCommandTest.java
1 /**
2 *
3 * Copyright 2004 Michael Gaffney
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.activemq.service.impl;
19
20 import org.jmock.cglib.MockObjectTestCase;
21
22 /**
23 * @author <a href="mailto:michael.gaffney@panacya.com">Michael Gaffney </a>
24 */
25 public class XATransactionCommandTest extends MockObjectTestCase {
26 /*
27 private XATransactionCommand txnCmd;
28 private ActiveMQXid stubXid;
29 private Mock mockBroker;
30 private Mock mockXaTxnMap;
31 private Mock mockTxnStore;
32 private Mock mockTxnTask;
33 private Mock stubTxnTask;
34
35 public XATransactionCommandTest(String name) {
36 setName(name);
37 }
38
39 public void testRollbackFromPreparedStateWithPostRollbackFailure() {
40 putTxnCmdInPreparedState();
41 addPostRollbackFailure();
42 expectRemoveXidCalled();
43 expectTxnStorageRemoveCalled();
44 doRollbackExpectException(XAException.XAER_RMERR);
45 endInFinishedState();
46 }
47
48 public void testRollbackFromPreparedState() {
49 putTxnCmdInPreparedState();
50 expectPostRollbackCalled();
51 expectRemoveXidCalled();
52 expectTxnStorageRemoveCalled();
53 doRollbackExpectSuccess();
54 endInFinishedState();
55 }
56
57 public void testRollbackFromInUseStateWithPostRollbackFailure() {
58 putTxnCmdInUseState();
59 addPostRollbackFailure();
60 expectRemoveXidCalled();
61 doRollbackExpectException(XAException.XAER_RMERR);
62 endInFinishedState();
63 }
64
65 public void testRollbackFromInUseState() {
66 putTxnCmdInUseState();
67 expectPostRollbackCalled();
68 expectRemoveXidCalled();
69 doRollbackExpectSuccess();
70 endInFinishedState();
71 }
72
73 public void testRollbackFromStartState() {
74 putTxnCmdInStartState();
75 expectRemoveXidCalled();
76 doRollbackExpectSuccess();
77 endInFinishedState();
78 }
79
80 public void testPrepareFromPrepareState() {
81 putTxnCmdInPreparedState();
82 expectRemoveXidNeverCalled();
83 doPrepareExpectException(XAException.XAER_PROTO);
84 endInPreparedState();
85 }
86
87 public void testPrepareFromInUseStateWithPrePrepareFailure() {
88 putTxnCmdInUseState();
89 addPrePrepareFailure();
90 expectPostRollbackCalled();
91 expectRemoveXidCalled();
92 doPrepareExpectException(XAException.XA_RBOTHER);
93 endInFinishedState();
94 }
95
96 public void testPrepareFromInUseState() {
97 putTxnCmdInUseState();
98 expectPrePrepareCalled();
99 expectTxnStoragePutCalled();
100 doPrepareExpectSuccess(XAResource.XA_OK);
101 endInPreparedState();
102 }
103
104 public void testPrepareFromStartState() {
105 putTxnCmdInStartState();
106 expectRemoveXidCalled();
107 doPrepareExpectSuccess(XAResource.XA_RDONLY);
108 endInFinishedState();
109 }
110
111 public void testCommitTwoPhaseFromPreparedStateWithPostCommitFailure() {
112 putTxnCmdInPreparedState();
113 doPostCommitFailure(false);
114 }
115
116 public void testCommitTwoPhaseFromPreparedState() {
117 putTxnCmdInPreparedState();
118 expectPostCommitCalled();
119 doCommitExpectSuccess(false);
120 endInFinishedState();
121 }
122
123 public void testCommitOnePhaseFromInUseStateWithPostCommitFailure() {
124 putTxnCmdInUseState();
125 expectPrePrepareCalled();
126 doPostCommitFailure(true);
127 }
128
129 public void testCommitOnePhaseFromInUseStateWithPrePrepareFailure() {
130 putTxnCmdInUseState();
131 addPrePrepareFailure();
132 expectPostRollbackCalled();
133 expectRemoveXidCalled();
134 doCommitExpectException(true, XAException.XA_RBOTHER);
135 endInFinishedState();
136 }
137
138 public void testCommitOnePhaseFromInUseState() {
139 putTxnCmdInUseState();
140 expectPrePrepareCalled();
141 expectPostCommitCalled();
142 doCommitExpectSuccess(true);
143 endInFinishedState();
144 }
145
146 public void testCommitTwoPhaseFromInUseState() {
147 putTxnCmdInUseState();
148 expectRemoveXidNeverCalled();
149 doCommitExpectException(false, XAException.XAER_PROTO);
150 endInUseState();
151 }
152
153 public void testCommitOnePhaseFromStartState() {
154 putTxnCmdInStartState();
155 doCommitExpectSuccess(true);
156 endInFinishedState();
157 }
158
159 public void testCommitTwoPhaseFromStartState() {
160 putTxnCmdInStartState();
161 expectRemoveXidNeverCalled();
162 doCommitExpectException(false, XAException.XAER_PROTO);
163 endInStartState();
164 }
165
166 public void testCommitTwoPhaseFromFinishedState() {
167 putTxnCmdInFinishedState();
168 expectRemoveXidNeverCalled();
169 doCommitExpectException(false, XAException.XAER_PROTO);
170 endInFinishedState();
171 }
172
173 public void testPrePrepare() throws Throwable {
174 putTxnCmdInStartState();
175 expectPrePrepareCalled();
176 assertTxnCmdInUseState();
177 txnCmd.prePrepare();
178 endInUseState();
179 }
180
181 public void testAddPrePrepare() throws Throwable {
182 putTxnCmdInStartState();
183 txnCmd.addPrePrepareTask((TransactionTask) mockTxnTask.proxy());
184 endInUseState();
185 }
186
187 public void testAddPostCommitTask() throws Throwable {
188 putTxnCmdInStartState();
189 txnCmd.addPostCommitTask((TransactionTask) mockTxnTask.proxy());
190 endInUseState();
191 }
192
193 public void testAddPostRollbackTask() throws Throwable {
194 putTxnCmdInStartState();
195 txnCmd.addPostRollbackTask((TransactionTask) mockTxnTask.proxy());
196 endInUseState();
197 }
198
199 protected void setUp() throws Exception {
200 setupStubs();
201 setupMocks();
202 setupTxnCommand();
203 }
204
205 private void setupStubs() throws JMSException {
206 stubXid = new ActiveMQXid("1:2:3");
207 stubTxnTask = new Mock(TransactionTask.class);
208 }
209
210 private void setupTxnCommand() {
211 txnCmd = new XATransactionCommand((Broker) mockBroker.proxy(),
212 stubXid, (Map) mockXaTxnMap.proxy(),
213 (TransactionStore) mockTxnStore.proxy());
214 }
215
216 private void setupMocks() {
217 mockBroker = new Mock(Broker.class);
218 mockXaTxnMap = new Mock(Map.class);
219 mockTxnStore = new Mock(TransactionStore.class);
220 mockTxnTask = new Mock(TransactionTask.class);
221 }
222
223 private void verifyMocks() {
224 mockBroker.verify();
225 mockXaTxnMap.verify();
226 mockTxnStore.verify();
227 mockTxnTask.verify();
228 }
229
230 private void doRollbackExpectSuccess() {
231 try {
232 txnCmd.rollback();
233 } catch (XAException e) {
234 fail("No XAException should be thrown");
235 }
236 }
237
238 private void doRollbackExpectException(final int expectedErrorCode) {
239 try {
240 txnCmd.rollback();
241 fail("An XAException should have been thrown");
242 } catch (XAException e) {
243 assertEquals("Incorrect XAException error code", expectedErrorCode, e.errorCode);
244 }
245 }
246
247 private void doPrepareExpectException(final int expectedErrorCode) {
248 try {
249 txnCmd.prepare();
250 fail("An XAException should have been thrown");
251 } catch (XAException e) {
252 assertEquals("Incorrect XAException error code", expectedErrorCode, e.errorCode);
253 }
254 }
255
256 private void doPrepareExpectSuccess(final int expectedXaCode) {
257 try {
258 assertEquals("Incorrect XAResource code returned.", expectedXaCode, txnCmd.prepare());
259 } catch (XAException e) {
260 fail("No XAException should be thrown");
261 }
262 }
263
264 private void doPostCommitFailure(final boolean onePhase) {
265 addPostCommitFailure();
266 expectRemoveXidCalled();
267 doCommitExpectException(onePhase, XAException.XAER_RMERR);
268 endInFinishedState();
269 }
270
271 private void doCommitExpectSuccess(final boolean onePhase) {
272 expectRemoveXidCalled();
273 try {
274 txnCmd.commit(onePhase);
275 } catch (XAException e) {
276 fail("No XAException should be thrown");
277 }
278 }
279
280 private void doCommitExpectException(final boolean onePhase, final int expectedErrorCode) {
281 try {
282 txnCmd.commit(onePhase);
283 fail("An XAException should have been thrown");
284 } catch (XAException e) {
285 assertEquals("Incorrect XAException error code", expectedErrorCode, e.errorCode);
286 }
287 }
288
289 private void addPostRollbackFailure() {
290 stubTxnTask.expects(once()).method("execute").will(throwException(new Throwable("Stub")));
291 txnCmd.addPostRollbackTask((TransactionTask) stubTxnTask.proxy());
292 }
293
294 private void addPostCommitFailure() {
295 stubTxnTask.expects(once()).method("execute").will(throwException(new Throwable("Stub")));
296 txnCmd.addPostCommitTask((TransactionTask) stubTxnTask.proxy());
297 }
298
299 private void addPrePrepareFailure() {
300 stubTxnTask.expects(once()).method("execute").will(throwException(new Throwable("Stub")));
301 txnCmd.addPrePrepareTask((TransactionTask) stubTxnTask.proxy());
302 }
303
304 private void expectPrePrepareCalled() {
305 mockTxnTask.expects(once()).method("execute").with(same(mockBroker.proxy()));
306 txnCmd.addPrePrepareTask((TransactionTask) mockTxnTask.proxy());
307 }
308
309 private void expectPostCommitCalled() {
310 mockTxnTask.expects(once()).method("execute").with(same(mockBroker.proxy()));
311 txnCmd.addPostCommitTask((TransactionTask) mockTxnTask.proxy());
312 }
313
314 private void expectPostRollbackCalled() {
315 mockTxnTask.expects(once()).method("execute").with(same(mockBroker.proxy()));
316 txnCmd.addPostRollbackTask((TransactionTask) mockTxnTask.proxy());
317 }
318
319 private void expectRemoveXidCalled() {
320 mockXaTxnMap.expects(once()).method("remove").with(same(stubXid));
321 }
322
323 private void expectRemoveXidNeverCalled() {
324 mockXaTxnMap.expects(never()).method("remove");
325 }
326
327 private void expectTxnStoragePutCalled() {
328 mockTxnStore.expects(once()).method("put").with(same(stubXid), same(txnCmd));
329 }
330
331 private void expectTxnStorageRemoveCalled() {
332 mockTxnStore.expects(once()).method("remove").with(same(stubXid));
333 }
334
335 private void putTxnCmdInStartState() {
336 txnCmd.setState(AbstractTransaction.START_STATE);
337 assertTxnCmdInStartState();
338 }
339
340 private void putTxnCmdInUseState() {
341 txnCmd.setState(AbstractTransaction.IN_USE_STATE);
342 assertTxnCmdInUseState();
343 }
344
345 private void putTxnCmdInPreparedState() {
346 txnCmd.setState(AbstractTransaction.PREPARED_STATE);
347 assertTxnCmdInPreparedState();
348 }
349
350 private void putTxnCmdInFinishedState() {
351 txnCmd.setState(AbstractTransaction.FINISHED_STATE);
352 assertTxnCmdInFinishedState();
353 }
354
355 private void assertTxnCmdInStartState() {
356 assertEquals("Not in START_STATE", AbstractTransaction.START_STATE, txnCmd.getState());
357 }
358
359 private void assertTxnCmdInFinishedState() {
360 assertEquals("Not in FINISHED_STATE", AbstractTransaction.FINISHED_STATE, txnCmd.getState());
361 }
362
363 private void assertTxnCmdInUseState() {
364 assertEquals("Not in IN_USE_STATE", AbstractTransaction.IN_USE_STATE, txnCmd.getState());
365 }
366
367 private void assertTxnCmdInPreparedState() {
368 assertEquals("Not in PREPARED_STATE", AbstractTransaction.PREPARED_STATE, txnCmd.getState());
369 }
370
371 private void endInFinishedState() {
372 assertTxnCmdInFinishedState();
373 verifyMocks();
374 }
375
376 private void endInUseState() {
377 assertTxnCmdInUseState();
378 verifyMocks();
379 }
380
381 private void endInStartState() {
382 assertTxnCmdInStartState();
383 verifyMocks();
384 }
385
386 private void endInPreparedState() {
387 assertTxnCmdInPreparedState();
388 verifyMocks();
389 }
390 */
391 public void testX() {}
392
393 }