001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cts.interfaces;
023:
024: import javax.ejb.*;
025:
026: import javax.transaction.Status;
027: import javax.transaction.UserTransaction;
028: import javax.transaction.RollbackException;
029:
030: import org.jboss.test.cts.keys.AccountPK;
031:
032: /**
033: * A utility class for testing UserTransaction in a stand-alone
034: * client, or a BMT enterprise bean.
035: * This is not part of any interface, just shared code used in
036: * client and server.
037: * This does not depend on JUnit, as JUnit is not available on the server.
038: *
039: * @author <a href="mailto:osh@sparre.dk">Ole Husgaard</a>
040: * @version $Revision: 57211 $
041: */
042: public class UserTransactionTester {
043: static org.apache.log4j.Category log = org.apache.log4j.Category
044: .getInstance(UserTransactionTester.class);
045:
046: /**
047: * Home of entity used as a resource for testing.
048: */
049: private CtsBmpHome home;
050:
051: /**
052: * UserTransaction to test.
053: */
054: private UserTransaction ut;
055:
056: /**
057: * First resource for testing.
058: */
059: private CtsBmp bean1;
060:
061: /**
062: * Second resource for testing.
063: */
064: private CtsBmp bean2;
065:
066: /**
067: * Create a new UserTransaction test instance.
068: */
069: public UserTransactionTester(CtsBmpHome home,
070: UserTransaction userTransaction) {
071: this .home = home;
072: this .ut = userTransaction;
073: }
074:
075: /**
076: * Run all the UserTransaction tests.
077: */
078: public boolean runAllTests() {
079: // No resource tests
080: if (!testBeginRollback())
081: return false;
082: if (!testBeginCommit())
083: return false;
084: if (!testBeginSetrollbackonlyRollback())
085: return false;
086: if (!testBeginSetrollbackonlyCommit())
087: return false;
088:
089: // Create first instance
090: try {
091: bean1 = home.create(new AccountPK("UT_TestBean1"), "Ole1");
092: } catch (Exception ex) {
093: log.debug("failed", ex);
094: return false;
095: }
096:
097: // Single resource tests
098: if (!testSingleRollback())
099: return false;
100: if (!testSingleCommit())
101: return false;
102: if (!testSingleSetrollbackonlyCommit())
103: return false;
104:
105: // Can second instance be created in a tx that is rolled back?
106: try {
107: ut.begin();
108: bean2 = home.create(new AccountPK("UT_TestBean2"), "Ole2");
109: ut.rollback();
110:
111: // Should no longer exist
112: boolean gotException = false;
113: try {
114: bean2.setPersonsName("Ole");
115: } catch (Exception e) {
116: log
117: .info("IGNORE PREVIOUS NoSuchEntityException - it is intentional");
118: gotException = true;
119: }
120: if (!gotException)
121: throw new RuntimeException(
122: "Rollback didn't rollback create.");
123: } catch (Exception ex) {
124: log.debug("failed", ex);
125: return false;
126: }
127:
128: // Create second instance
129: try {
130: bean2 = home.create(new AccountPK("UT_TestBean2"), "Ole2");
131: } catch (Exception ex) {
132: log.debug("failed", ex);
133: return false;
134: }
135:
136: return true;
137: }
138:
139: //
140: // No resource tests.
141: //
142:
143: /**
144: * Simple begin/rollback test.
145: */
146: private boolean testBeginRollback() {
147: try {
148: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
149: throw new RuntimeException("No tx should be active.");
150:
151: ut.begin();
152: if (ut.getStatus() != Status.STATUS_ACTIVE)
153: throw new RuntimeException("New tx not active.");
154: ut.rollback();
155:
156: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
157: throw new RuntimeException("No tx should be active.");
158: } catch (Exception ex) {
159: log.debug("failed", ex);
160: return false;
161: }
162: return true;
163: }
164:
165: /**
166: * Simple begin/commit test.
167: */
168: private boolean testBeginCommit() {
169: try {
170: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
171: throw new RuntimeException("No tx should be active.");
172:
173: ut.begin();
174: if (ut.getStatus() != Status.STATUS_ACTIVE)
175: throw new RuntimeException("New tx not active.");
176: ut.commit();
177:
178: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
179: throw new RuntimeException("No tx should be active.");
180: } catch (Exception ex) {
181: log.debug("failed", ex);
182: return false;
183: }
184: return true;
185: }
186:
187: /**
188: * Simple begin/setRollbackOnly/rollback test.
189: */
190: private boolean testBeginSetrollbackonlyRollback() {
191: try {
192: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
193: throw new RuntimeException("No tx should be active.");
194:
195: ut.begin();
196: if (ut.getStatus() != Status.STATUS_ACTIVE)
197: throw new RuntimeException("New tx not active.");
198: ut.setRollbackOnly();
199: if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK)
200: throw new RuntimeException(
201: "Tx not marked for rollback.");
202: ut.rollback();
203:
204: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
205: throw new RuntimeException("No tx should be active.");
206: } catch (Exception ex) {
207: log.debug("failed", ex);
208: return false;
209: }
210: return true;
211: }
212:
213: /**
214: * Simple begin/setRollbackOnly/commit test.
215: */
216: private boolean testBeginSetrollbackonlyCommit() {
217: try {
218: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
219: throw new RuntimeException("No tx should be active.");
220:
221: ut.begin();
222: if (ut.getStatus() != Status.STATUS_ACTIVE)
223: throw new RuntimeException("New tx not active.");
224: ut.setRollbackOnly();
225: if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK)
226: throw new RuntimeException(
227: "Tx not marked for rollback.");
228:
229: boolean gotException = false;
230: try {
231: ut.commit();
232: } catch (RollbackException rbe) {
233: gotException = true;
234: }
235: if (!gotException)
236: throw new RuntimeException(
237: "Didn't get expected RollbackException.");
238:
239: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
240: throw new RuntimeException("No tx should be active.");
241: } catch (Exception ex) {
242: log.debug("failed", ex);
243: return false;
244: }
245: return true;
246: }
247:
248: //
249: // Single resource tests.
250: //
251:
252: /**
253: * Tests if a rollback really rolls back.
254: */
255: private boolean testSingleRollback() {
256: try {
257: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
258: throw new RuntimeException("No tx should be active.");
259:
260: bean1.setPersonsName("Ole");
261: if (!bean1.getPersonsName().equals("Ole"))
262: throw new RuntimeException("Unable to set property.");
263:
264: ut.begin();
265: if (ut.getStatus() != Status.STATUS_ACTIVE)
266: throw new RuntimeException("New tx not active.");
267: if (!bean1.getPersonsName().equals("Ole"))
268: throw new RuntimeException(
269: "Property changes after begin.");
270:
271: bean1.setPersonsName("Peter");
272: if (!bean1.getPersonsName().equals("Peter"))
273: throw new RuntimeException("Unable to set property.");
274:
275: ut.rollback();
276: if (!bean1.getPersonsName().equals("Ole"))
277: throw new RuntimeException("Rollback doesn't work.");
278:
279: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
280: throw new RuntimeException("No tx should be active.");
281: } catch (Exception ex) {
282: log.debug("failed", ex);
283: return false;
284: }
285: return true;
286: }
287:
288: /**
289: * Tests if a commit really commits.
290: */
291: private boolean testSingleCommit() {
292: try {
293: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
294: throw new RuntimeException("No tx should be active.");
295:
296: bean1.setPersonsName("Ole");
297: if (!bean1.getPersonsName().equals("Ole"))
298: throw new RuntimeException("Unable to set property.");
299:
300: ut.begin();
301: if (ut.getStatus() != Status.STATUS_ACTIVE)
302: throw new RuntimeException("New tx not active.");
303: if (!bean1.getPersonsName().equals("Ole"))
304: throw new RuntimeException(
305: "Property changes after begin.");
306:
307: bean1.setPersonsName("Peter");
308: if (!bean1.getPersonsName().equals("Peter"))
309: throw new RuntimeException("Unable to set property.");
310:
311: ut.commit();
312: if (!bean1.getPersonsName().equals("Peter"))
313: throw new RuntimeException(
314: "Property not set after commit.");
315:
316: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
317: throw new RuntimeException("No tx should be active.");
318: } catch (Exception ex) {
319: log.debug("failed", ex);
320: return false;
321: }
322: return true;
323: }
324:
325: /**
326: * Tests if a setRollbackOnly really makes the transaction rollback.
327: */
328: private boolean testSingleSetrollbackonlyCommit() {
329: try {
330: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
331: throw new RuntimeException("No tx should be active.");
332:
333: bean1.setPersonsName("Ole");
334: if (!bean1.getPersonsName().equals("Ole"))
335: throw new RuntimeException("Unable to set property.");
336:
337: ut.begin();
338: if (ut.getStatus() != Status.STATUS_ACTIVE)
339: throw new RuntimeException("New tx not active.");
340: if (!bean1.getPersonsName().equals("Ole"))
341: throw new RuntimeException(
342: "Property changes after begin.");
343:
344: bean1.setPersonsName("Peter");
345: if (!bean1.getPersonsName().equals("Peter"))
346: throw new RuntimeException("Unable to set property.");
347:
348: ut.setRollbackOnly();
349: if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK)
350: throw new RuntimeException(
351: "Tx not marked for rollback.");
352:
353: boolean gotException = false;
354: try {
355: ut.commit();
356: } catch (RollbackException rbe) {
357: gotException = true;
358: }
359: if (!gotException)
360: throw new RuntimeException(
361: "Didn't get expected RollbackException.");
362:
363: if (!bean1.getPersonsName().equals("Ole"))
364: throw new RuntimeException("Didn't roll back.");
365:
366: if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
367: throw new RuntimeException("No tx should be active.");
368: } catch (Exception ex) {
369: log.debug("failed", ex);
370: return false;
371: }
372: return true;
373: }
374: }
|