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: TestContainerTransactionException.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.fail;
027:
028: import javax.ejb.EJBException;
029:
030: import org.ow2.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
031: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.transaction.ItfTransactionMisc00;
032: import org.ow2.easybeans.tests.common.helper.EJBHelper;
033: import org.ow2.easybeans.tests.common.helper.EmbeddedHelper;
034: import org.ow2.util.log.Log;
035: import org.ow2.util.log.LogFactory;
036:
037: /**
038: * Verifies if the container manages well the runtime exception and the transaction among the beans.
039: * @author Gisele Pinheiro Souza
040: * @author Eduardo Studzinski Estima de Castro
041: */
042: public abstract class TestContainerTransactionException {
043:
044: /**
045: * Constant that defines the first database name.
046: */
047: protected static final String DATABASE_1 = "jdbc_1";
048:
049: /**
050: * Constant that defines the second database name.
051: */
052: protected static final String DATABASE_2 = "jdbc_2";
053:
054: /**
055: * Bean used during the tests that throws a runtime exception.
056: */
057: private ItfContainerTransaction sfsbContainerTransactionRuntime = null;
058:
059: /**
060: * Logger.
061: */
062: private static Log logger = LogFactory
063: .getLog(TestContainerTransactionException.class);
064:
065: /**
066: * Initiates all things needed to execute the tests.
067: * @throws Exception if an error occurs.
068: */
069: public void setup() throws Exception {
070: // Inserts all database before execute the test
071: // used because the container does not provide this feature yet
072: EmbeddedHelper.bindDatasource();
073: // creates the bean used to manages the databse in the server site.
074: //cleans the transaction active in other test
075: ExceptionHandleUtil.cleanTransaction();
076: //creates the bean used during the tests
077: createBeanRuntime();
078: //deletes all tables
079: deleteTable();
080: }
081:
082: /**
083: * Gets a new instance of the bean used in the runtime exception tests.
084: * @param beanClass the bean class name used to make the lookup.
085: * @throws Exception if a lookup error occurs.
086: */
087: public void createBeanRuntime(final Class beanClass)
088: throws Exception {
089: sfsbContainerTransactionRuntime = EJBHelper
090: .getBeanRemoteInstance(beanClass,
091: ItfContainerTransaction.class);
092: }
093:
094: /**
095: * Gets a new instance of the bean used in the runtime exception tests.
096: * @throws Exception if a lookup error occurs.
097: */
098: public abstract void createBeanRuntime() throws Exception;
099:
100: /**
101: * Verifies if the container manages the transaction among divers beans.The
102: * first bean creates a table in the first database and calls other
103: * bean(with transaction attribute REQUIRED) to insert a table in the second
104: * database. After the both insertions, the first bean throws a RuntimeException
105: * exception. Also, verifies if the container throws an EJBException, discards the bean and does the rollback in
106: * this case(if the transaction atttribute of the first bean has a
107: * transaction context).
108: * @throws Exception if an error during the tests occurs.
109: */
110: public void testCallOtherBeanReq() throws Exception {
111: try {
112: sfsbContainerTransactionRuntime
113: .insertTablesUsingAuxBeanReq(DATABASE_1, DATABASE_2);
114: fail("The container did not throw the EJBException.");
115: } catch (EJBException e) {
116: logger
117: .debug(
118: "The bean threw an expected error during the execution {0}",
119: e);
120: }
121: // verifies if the container discarded the instance.
122: if (!ExceptionHandleUtil
123: .isDiscarded(sfsbContainerTransactionRuntime)) {
124: fail("The bean was not discarded.");
125: }
126:
127: // TODO - verifies if the container log the error.
128: }
129:
130: /**
131: * Verifies if the container manages the transaction among divers beans.The
132: * first bean creates a table in the first database and calls other
133: * bean(with transaction attribute NOT_SUPPORTS) to insert a table in the second
134: * database. After the both insertions, the first bean throws a RuntimeException
135: * exception. Also, verifies if the container throws an EJBException, discards the bean and does the rollback in
136: * this case(if the transaction atttribute of the first bean has a
137: * transaction context).
138: * @throws Exception if an error during the tests occurs.
139: */
140: public void testCallOtherBeanNotSup() throws Exception {
141: try {
142: sfsbContainerTransactionRuntime
143: .insertTablesUsingAuxBeanNotSup(DATABASE_1,
144: DATABASE_2);
145: fail("The container did not throw the EJBException.");
146: } catch (EJBException e) {
147: logger
148: .debug(
149: "The bean threw an expected error during the execution {0}",
150: e);
151: }
152: // verifies if the container discarded the instance.
153: if (!ExceptionHandleUtil
154: .isDiscarded(sfsbContainerTransactionRuntime)) {
155: fail("The bean was not discarded.");
156: }
157:
158: // TODO - verifies if the container log the error.
159: }
160:
161: /**
162: * Returns the bean that throws a runtime exception.
163: * @return the bean.
164: */
165: public ItfContainerTransaction getRuntimeBean() {
166: return sfsbContainerTransactionRuntime;
167: }
168:
169: /**
170: * Deletes the tables created during test.
171: * @throws Exception if a lookup error occurs.
172: *
173: */
174: public void deleteTable() throws Exception {
175: // deletes the table after each test to avoid errors.
176: ExceptionHandleUtil.deleteTable(DATABASE_1,
177: ItfContainerTransaction.TABLE);
178: ExceptionHandleUtil.deleteTable(DATABASE_2,
179: ItfContainerTransaction.TABLE);
180: // deletes the auxiliar bean table
181: ExceptionHandleUtil.deleteTable(DATABASE_1,
182: ItfTransactionMisc00.TABLE);
183: ExceptionHandleUtil.deleteTable(DATABASE_2,
184: ItfTransactionMisc00.TABLE);
185: }
186:
187: }
|