001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: TestExceptionHandleBase.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.transaction.containermanaged.base;
025:
026: import static org.testng.Assert.assertTrue;
027: import static org.testng.Assert.fail;
028:
029: import java.sql.SQLException;
030:
031: import javax.ejb.EJBException;
032: import javax.transaction.UserTransaction;
033:
034: import org.ow2.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
035: import org.ow2.easybeans.tests.common.exception.AppException;
036: import org.ow2.easybeans.tests.common.exception.AppRuntimeException;
037: import org.ow2.easybeans.tests.common.exception.RollbackAppRuntimeException;
038: import org.ow2.easybeans.tests.common.exception.RollbackApplicationException;
039: import org.ow2.easybeans.tests.common.helper.EJBHelper;
040: import org.ow2.easybeans.tests.common.helper.ExceptionHelper;
041: import org.ow2.util.log.Log;
042: import org.ow2.util.log.LogFactory;
043:
044: /**
045: * Access different beans that have the same behaviour, but the beans throw
046: * different types of exception.
047: * @author Gisele Pinheiro Souza
048: * @author Eduardo Studzinski Estima de Castro
049: */
050: public abstract class TestExceptionHandleBase {
051:
052: /**
053: * Constant that defines the first database name.
054: */
055: protected static final String DATABASE_1 = "jdbc_1";
056:
057: /**
058: * Constant that defines the second database name.
059: */
060: protected static final String DATABASE_2 = "jdbc_2";
061:
062: /**
063: * Bean used during the tests that throws an application exception with
064: * rollback = false.
065: */
066: private ItfContainerTransaction sfsbContainerTransactionApp = null;
067:
068: /**
069: * Bean used during the tests that throws an application exception with
070: * rollback = true.
071: */
072: private ItfContainerTransaction sfsbContainerTransactionRollback = null;
073:
074: /**
075: * Bean used during the tests that throws an application exception(that
076: * extends a runtime exception) with rollback = false.
077: */
078: private ItfContainerTransaction sfsbContainerTransactionApp01 = null;
079:
080: /**
081: * Bean used during the tests that throws an application exception(that
082: * extends a runtime exception) with rollback = true.
083: */
084: private ItfContainerTransaction sfsbContainerTransactionApp02 = null;
085:
086: /**
087: * Bean used during the tests that throws a runtime exception.
088: */
089: private ItfContainerTransaction sfsbContainerTransactionRuntime = null;
090:
091: /**
092: * Logger.
093: */
094: private static Log logger = LogFactory
095: .getLog(TestExceptionHandleBase.class);
096:
097: /**
098: * Initiates all things needed to execute the tests.
099: * @throws Exception if an error occurs.
100: */
101: public void setup() throws Exception {
102: // cleans the transaction active in other test
103: ExceptionHandleUtil.cleanTransaction();
104: // creates all beans
105: createBeanApp();
106: createBeanApp01();
107: createBeanApp02();
108: createBeanRollback();
109: createBeanRuntime();
110: // cleans the database
111: deleteTable();
112: }
113:
114: /**
115: * Creates a bean that throws always an application exception that extends
116: * Exception and has a the rollback element as false.
117: * @throws Exception if a lookup error occurs.
118: */
119: public abstract void createBeanApp() throws Exception;
120:
121: /**
122: * Creates a bean that throws always an application exception that extends
123: * RuntimeException and has a the rollback element as false.
124: * @throws Exception if a lookup error occurs.
125: */
126: public abstract void createBeanApp01() throws Exception;
127:
128: /**
129: * Creates a bean that throws always a application exception that extends
130: * RuntimeException and has a the rollback element as true.
131: * @throws Exception if a lookup error occurs.
132: */
133: public abstract void createBeanApp02() throws Exception;
134:
135: /**
136: * Creates a bean that throws always an application exception that extends
137: * Exception and has a the rollback element as true.
138: * @throws Exception if a lookup error occurs.
139: */
140: public abstract void createBeanRollback() throws Exception;
141:
142: /**
143: * Creates a bean that throws always a runtime exception.
144: * @throws Exception if a lookup error occurs.
145: */
146: public abstract void createBeanRuntime() throws Exception;
147:
148: /**
149: * Gets a new instance of the bean used in the application exception (with
150: * rollback=false) tests.
151: * @param beanClass the bean class name used to make the lookup.
152: * @throws Exception if a lookup error occurs.
153: */
154: public void createBeanApp(final Class beanClass) throws Exception {
155: sfsbContainerTransactionApp = EJBHelper.getBeanRemoteInstance(
156: beanClass, ItfContainerTransaction.class);
157: }
158:
159: /**
160: * Gets a new instance of the bean used in the application exception that
161: * extends a runtime exception (with rollback=false) tests.
162: * @param beanClass the bean class name used to make the lookup.
163: * @throws Exception if a lookup error occurs.
164: */
165: public void createBeanApp01(final Class beanClass) throws Exception {
166: sfsbContainerTransactionApp01 = EJBHelper
167: .getBeanRemoteInstance(beanClass,
168: ItfContainerTransaction.class);
169: }
170:
171: /**
172: * Gets a new instance of the bean used in the application exception that
173: * extends a runtime exception (with rollback=true) tests.
174: * @param beanClass the bean class name used to make the lookup.
175: * @throws Exception if a lookup error occurs.
176: */
177: public void createBeanApp02(final Class beanClass) throws Exception {
178: sfsbContainerTransactionApp02 = EJBHelper
179: .getBeanRemoteInstance(beanClass,
180: ItfContainerTransaction.class);
181: }
182:
183: /**
184: * Gets a new instance of the bean used in the application exception (with
185: * rollback=true) tests.
186: * @param beanClass the bean class name used to make the lookup.
187: * @throws Exception if a lookup error occurs.
188: */
189: public void createBeanRollback(final Class beanClass)
190: throws Exception {
191: sfsbContainerTransactionRollback = EJBHelper
192: .getBeanRemoteInstance(beanClass,
193: ItfContainerTransaction.class);
194: }
195:
196: /**
197: * Gets a new instance of the bean used in the runtime exception tests.
198: * @param beanClass the bean class name used to make the lookup.
199: * @throws Exception if a lookup error occurs.
200: */
201: public void createBeanRuntime(final Class beanClass)
202: throws Exception {
203: sfsbContainerTransactionRuntime = EJBHelper
204: .getBeanRemoteInstance(beanClass,
205: ItfContainerTransaction.class);
206: }
207:
208: /**
209: * Verifies if the container re-throws the application exception that
210: * extends Exception (with rollback = true) and marks the transaction for
211: * rollback if there is transaction context.
212: * @throws Exception if an error during the tests occurs.
213: */
214: public void testNotUsingClientTransWithAppRollbackException()
215: throws Exception {
216: try {
217: sfsbContainerTransactionRollback
218: .insertCorrectFirstErrorSecond(DATABASE_1,
219: DATABASE_2);
220: fail("The container did not re-throw the application exception.");
221: } catch (RollbackApplicationException e) {
222: logger
223: .debug(
224: "The bean threw an expected error during the execution {0}",
225: e);
226: }
227: // verifies if the table was created in the DATABASE_1
228: ExceptionHandleUtil.verifyTable(DATABASE_1,
229: ItfContainerTransaction.TABLE);
230: }
231:
232: /**
233: * Verifies if the container re-throws the application exception that
234: * extends Exception(with rollback = true) and marks the transaction for
235: * rollback in some cases.This test uses a client transaction.
236: * @param canCommit true if the container does not need to mark the
237: * transaction as rollback(transaction attribute: NotSupported,
238: * Never). False if the container must mark the transaction for
239: * rollback(transaction attribute:Required, Mandatory and Supports).
240: * @throws Exception if an error during the tests occurs.
241: */
242: public void testUsingClientTransWithAppRollbackException(
243: final boolean canCommit) throws Exception {
244: UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
245: utx.begin();
246: try {
247: sfsbContainerTransactionRollback
248: .insertCorrectFirstErrorSecond(DATABASE_1,
249: DATABASE_2);
250: fail("The container did not re-throw the application exception.");
251: } catch (RollbackApplicationException e) {
252: logger
253: .debug(
254: "The bean threw an expected error during the execution {0}",
255: e);
256: }
257: // tries to commit
258: try {
259: utx.commit();
260: if (!canCommit) {
261: fail("The transaction was marked as rolled back, the client cannot make the commit.");
262: }
263: } catch (Exception e) {
264: logger
265: .debug(
266: "The bean threw an expected error during the execution {0}",
267: e);
268: }
269: // verifies if the table was created in the DATABASE_1
270: ExceptionHandleUtil.verifyTable(DATABASE_1,
271: ItfContainerTransaction.TABLE);
272: }
273:
274: /**
275: * Verifies if the container re-throws the application exception that
276: * extends Exception(with rollback = false) and does not do the rollback in
277: * this case.This test uses a client transaction.
278: * @throws Exception if an error during the tests occurs.
279: */
280: public void testUsingClientTransWithAppException() throws Exception {
281: UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
282: utx.begin();
283: try {
284: sfsbContainerTransactionApp.insertCorrectFirstErrorSecond(
285: DATABASE_1, DATABASE_2);
286: fail("The container did not re-throw the application exception.");
287: } catch (AppException e) {
288: logger
289: .debug(
290: "The bean threw an expected error during the execution {0}",
291: e);
292: }
293: // tries to commit
294: try {
295: utx.commit();
296: } catch (Exception e) {
297: logger
298: .debug(
299: "The bean threw an expected error during the execution {0}",
300: e);
301: }
302: // verifies if the table was created in the DATABASE_1
303: ExceptionHandleUtil.verifyTable(DATABASE_1,
304: ItfContainerTransaction.TABLE);
305: }
306:
307: /**
308: * Verifies if the container re-throws the application exception that
309: * extends Exception(with rollback = false) and does not do the rollback in
310: * this case.
311: * @throws Exception if an error during the tests occurs.
312: */
313: public void testNotUsingClientTransWithAppException()
314: throws Exception {
315: try {
316: sfsbContainerTransactionApp.insertCorrectFirstErrorSecond(
317: DATABASE_1, DATABASE_2);
318: fail("The container did not re-throw the application exception.");
319: } catch (AppException e) {
320: logger
321: .debug(
322: "The bean threw an expected error during the execution {0}",
323: e);
324: }
325: // verifies if the table was created in the DATABASE_1
326: ExceptionHandleUtil.verifyTable(DATABASE_1,
327: ItfContainerTransaction.TABLE);
328: }
329:
330: /**
331: * Verifies if the container re-throws the application exception that
332: * extends a runtime exception (with rollback = true) and marks the
333: * transaction for rollback in this case.
334: * @throws Exception if an error during the tests occurs.
335: */
336: public void testNotUsingClientTransWithAppRuntimeRollbackException()
337: throws Exception {
338: try {
339: sfsbContainerTransactionApp02
340: .insertCorrectFirstErrorSecond(DATABASE_1,
341: DATABASE_2);
342: fail("The container did not re-throw the application exception.");
343: } catch (RollbackAppRuntimeException e) {
344: logger
345: .debug(
346: "The bean threw an expected error during the execution {0}",
347: e);
348: }
349: // verifies if the table was created in the DATABASE_1
350: ExceptionHandleUtil.verifyTable(DATABASE_1,
351: ItfContainerTransaction.TABLE);
352: }
353:
354: /**
355: * Verifies if the container re-throws the application exception that
356: * extends a runtime exception (with rollback = true) and marks the
357: * transaction for rollback in this case. This test uses a client
358: * transaction.
359: * @param canCommit says if the bean is able to commit the client
360: * transaction. The client is able to commit when the bean does not
361: * use the client transaction.
362: * @throws Exception if an error during the tests occurs.
363: */
364: public void testUsingClientTransWithAppRuntimeRollbackException(
365: final boolean canCommit) throws Exception {
366: UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
367: utx.begin();
368: try {
369: sfsbContainerTransactionApp02
370: .insertCorrectFirstErrorSecond(DATABASE_1,
371: DATABASE_2);
372: fail("The container did not re-throw the application exception.");
373: } catch (RollbackAppRuntimeException e) {
374: logger
375: .debug(
376: "The bean threw an expected error during the execution {0}",
377: e);
378: }
379: // tries to commit
380: try {
381: utx.commit();
382: if (!canCommit) {
383: fail("The transaction was marked as rolled back, the client cannot make the commit.");
384: }
385: } catch (Exception e) {
386: logger
387: .debug(
388: "The bean threw an expected error during the execution {0}",
389: e);
390: }
391: // verifies if the table was created in the DATABASE_1
392: ExceptionHandleUtil.verifyTable(DATABASE_1,
393: ItfContainerTransaction.TABLE);
394: }
395:
396: /**
397: * Verifies if the container re-throws the application exception that
398: * extends a runtime exception (with rollback = false) and does not do the
399: * rollback in this case.
400: * @throws Exception if an error during the tests occurs.
401: */
402: public void testNotUsingClientTransWithAppRuntimeException()
403: throws Exception {
404: try {
405: sfsbContainerTransactionApp01
406: .insertCorrectFirstErrorSecond(DATABASE_1,
407: DATABASE_2);
408: fail("The container did not re-throw the application exception.");
409: } catch (AppRuntimeException e) {
410: logger
411: .debug(
412: "The bean threw an expected error during the execution {0}",
413: e);
414: }
415: // verifies if the table was created in the DATABASE_1
416: ExceptionHandleUtil.verifyTable(DATABASE_1,
417: ItfContainerTransaction.TABLE);
418: }
419:
420: /**
421: * Verifies if the container re-throws the application exception that
422: * extends a runtime exception (with rollback = false) and does not do the
423: * rollback in this case. The test uses a client transaction.
424: * @throws Exception if an error during the tests occurs.
425: */
426: public void testUsingClientTransWithAppRuntimeException()
427: throws Exception {
428: UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
429: utx.begin();
430: try {
431: sfsbContainerTransactionApp01
432: .insertCorrectFirstErrorSecond(DATABASE_1,
433: DATABASE_2);
434: fail("The container did not re-throw the application exception.");
435: } catch (AppRuntimeException e) {
436: logger
437: .debug(
438: "The bean threw an expected error during the execution {0}",
439: e);
440: }
441: // tries to commit
442: try {
443: utx.commit();
444: } catch (Exception e) {
445: logger
446: .debug(
447: "The bean threw an expected error during the execution {0}",
448: e);
449: }
450: // verifies if the table was created in the DATABASE_1
451: ExceptionHandleUtil.verifyTable(DATABASE_1,
452: ItfContainerTransaction.TABLE);
453: }
454:
455: /**
456: * Verifies if the container throws the EJBException when the bean throws a
457: * RuntimeException, discards the bean and does the rollback in this case.
458: * @throws Exception if an error during the tests occurs.
459: */
460: public void testNotUsingClientTransWithRuntimeException()
461: throws Exception {
462: // verifies if the container threw the correct exception
463: try {
464: sfsbContainerTransactionRuntime
465: .insertCorrectFirstErrorSecond(DATABASE_1,
466: DATABASE_2);
467: fail("The container did not throw the EJBException.");
468: } catch (EJBException e) {
469: logger
470: .debug(
471: "The bean threw an expected error during the execution {0}",
472: e);
473: }
474:
475: // verifies if the bean was discarded
476: assertTrue(ExceptionHandleUtil
477: .isDiscarded(sfsbContainerTransactionRuntime),
478: "There was a runtime exception and the container did not discarded the bean.");
479:
480: // TODO - verifies if the container log the error.
481:
482: // verifies if the table was created in the DATABASE_1
483: ExceptionHandleUtil.verifyTable(DATABASE_1,
484: ItfContainerTransaction.TABLE);
485:
486: }
487:
488: /**
489: * Verifies if the container throws the correct exception (EJBException for
490: * RequiresNew and NotSupports, and EJBTransactionRolledbackException for
491: * Require, Supports and Mandatory) when the bean throws a RuntimeException,
492: * discards the bean and does the rollback if the bean uses the client transaction.
493: * @param expectedException EJBException for
494: * RequiresNew and NotSupports, and EJBTransactionRolledbackException for
495: * Require, Supports and Mandatory
496: * @param canCommit true if the bean does not use the client transaction.
497: * @throws Exception if an error during the tests occurs.
498: */
499: public void testUsingClientTransWithRuntimeException(
500: final Class expectedException, final boolean canCommit)
501: throws Exception {
502: UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
503: utx.begin();
504: // verifies if the exception thrown is correct
505: try {
506: sfsbContainerTransactionRuntime
507: .insertCorrectFirstErrorSecond(DATABASE_1,
508: DATABASE_2);
509: } catch (Exception e) {
510: assertTrue(ExceptionHelper.isEquals(e, expectedException),
511: "The container did not throw the correct exception, the expected exception is "
512: + expectedException.getName()
513: + ", but the container threw "
514: + e.getClass().getName());
515: }
516: // tries to commit
517: try {
518: utx.commit();
519: if (!canCommit) {
520: fail("The transaction was marked as rolled back, the client cannot make the commit.");
521: }
522: } catch (Exception e) {
523: logger
524: .debug(
525: "The bean threw an expected error during the execution {0}",
526: e);
527: }
528: // verifies if the bean was discarded
529: assertTrue(ExceptionHandleUtil
530: .isDiscarded(sfsbContainerTransactionRuntime),
531: "There was a runtime exception and the container did not discarded the bean.");
532: // verifies if the transaction was rolled back
533: if (!canCommit) {
534: try {
535: // verifies if the table was created in the DATABASE_1
536: ExceptionHandleUtil.verifyTable(DATABASE_1,
537: ItfContainerTransaction.TABLE);
538: fail("There was an error during the commit and the transaction was not rolled back.");
539: } catch (SQLException e) {
540: logger
541: .debug(
542: "The bean threw an expected error during the execution {0}",
543: e);
544: }
545: }
546:
547: // TODO - verifies if the container log the error.
548:
549: }
550:
551: /**
552: * Deletes the tables created during test.
553: * @throws Exception if a lookup error occurs.
554: */
555: public void deleteTable() throws Exception {
556: // deletes the table after each test to avoid errors.
557: ExceptionHandleUtil.deleteTable(DATABASE_1,
558: ItfContainerTransaction.TABLE);
559: ExceptionHandleUtil.deleteTable(DATABASE_2,
560: ItfContainerTransaction.TABLE);
561: }
562:
563: }
|