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: TestContainerTransactionBase.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.transaction.containermanaged.base;
025:
026: import java.sql.SQLException;
027:
028: import javax.naming.NamingException;
029: import javax.transaction.Status;
030: import javax.transaction.UserTransaction;
031:
032: import org.ow2.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
033: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
034: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
035: import org.ow2.easybeans.tests.common.exception.TransactionException;
036: import org.ow2.easybeans.tests.common.helper.EJBHelper;
037: import org.ow2.easybeans.tests.common.helper.EmbeddedHelper;
038: import org.ow2.easybeans.tests.common.helper.TransactionHelper;
039: import org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged;
040: import org.ow2.util.log.Log;
041: import org.ow2.util.log.LogFactory;
042:
043: /**
044: * Contains all base methods for using during the tests about the
045: * container-managed.
046: * @author Gisele Pinheiro Souza
047: * @author Eduardo Studzinski Estima de Castro
048: */
049: public class TestContainerTransactionBase implements
050: ItfTestContainerManaged {
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.
064: */
065: private ItfContainerTransaction sfsbContainerTransaction = null;
066:
067: /**
068: * Bean used to manage the database in the server side.
069: */
070: private ItfDatabaseManager slsbDatabaseManager;
071:
072: /**
073: * Logger.
074: */
075: private static Log logger = LogFactory
076: .getLog(TestContainerTransactionBase.class);
077:
078: /**
079: * Verifies if the table was created in the database.
080: * @param database the database name.
081: * @param tableName the table name verified.
082: * @throws NamingException if a lookup error occurs.
083: * @throws SQLException if a database error occurs.
084: */
085: protected void verifyTable(final String database,
086: final String tableName) throws NamingException,
087: SQLException {
088: slsbDatabaseManager.verifyTable(database, tableName);
089: }
090:
091: /**
092: * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged#setup()
093: * @throws Exception if a database error occurs.
094: */
095: public void setup() throws Exception {
096: // Inserts all database before execute the test
097: // used because the container does not provide this feature yet
098: EmbeddedHelper.bindDatasource();
099: // creates the bean used to manages the databse in the server site.
100: slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(
101: SLSBDatabaseManager.class, ItfDatabaseManager.class);
102: //cleans the transaction active in other test
103: cleanTransaction();
104: }
105:
106: /**
107: * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged#createBean(java.lang.Class)
108: * @param beanClass the bean class name used to make the lookup.
109: * @throws Exception if a lookup error occurs.
110: */
111: public void createBean(final Class beanClass) throws Exception {
112: sfsbContainerTransaction = EJBHelper.getBeanRemoteInstance(
113: beanClass, ItfContainerTransaction.class);
114: }
115:
116: /**
117: * Creates a transaction, executes a method in the bean that creates a
118: * database and after forces a rollback. The specification says that the
119: * bean and the client, in this case, must to use the same transaction. So,
120: * the rollback must to be done in the bean also.
121: * @throws Exception if an error during the transaction occurs.
122: */
123: private void executeErrorTransaction() throws Exception {
124: UserTransaction utx = getUserTransaction();
125: // starts the transaction
126: utx.begin();
127: // call the method that must have the same transaction context
128: sfsbContainerTransaction.insertCorrectTableInBothDB(DATABASE_1,
129: DATABASE_2);
130: // rollback the transaction
131: utx.rollback();
132: }
133:
134: /**
135: * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged#testSetRollbackOnly()
136: * @throws Exception if an error during the tests occurs.
137: */
138: public void testSetRollbackOnly() throws Exception {
139: // calls the setRollbackOnly
140: try {
141: sfsbContainerTransaction.setRollbackOnly(DATABASE_1,
142: DATABASE_2);
143: } catch (TransactionException e) {
144: throw e.getParentException();
145: }
146: /*
147: * Verifies if the tables were inserted. The transaction attributes,
148: * that supports the setRollbackOnly, must to make a roll back when the
149: * method invocation completes.
150: */
151: verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
152: verifyTable(DATABASE_2, ItfContainerTransaction.TABLE);
153: }
154:
155: /**
156: * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged
157: * @throws Exception if an error during the tests occurs.
158: */
159: public void testGetRollbackOnly() throws Exception {
160: try {
161: sfsbContainerTransaction.getRollbackOnly();
162: } catch (TransactionException e) {
163: throw e.getParentException();
164: }
165: }
166:
167: /**
168: * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged#testUsingClientTrans()
169: * @throws Exception if an error during the tests occurs.
170: */
171: public void testUsingClientTrans() throws Exception {
172: executeErrorTransaction();
173:
174: // verifies if the table was created in the DATABASE_1
175: verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
176: // verifies if the table was created in the DATABASE_2
177: verifyTable(DATABASE_2, ItfContainerTransaction.TABLE);
178: }
179:
180: /**
181: * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged
182: */
183: public void deleteTable() {
184: // deletes the table after each test to avoid errors.
185: deleteTable(DATABASE_1, ItfContainerTransaction.TABLE);
186: deleteTable(DATABASE_2, ItfContainerTransaction.TABLE);
187: }
188:
189: /**
190: * Deletes the table in the database.
191: * @param dbName the database name in the registry.
192: * @param tableName the table name.
193: */
194: protected void deleteTable(final String dbName,
195: final String tableName) {
196: // deletes the table after each test to avoid errors.
197: try {
198: slsbDatabaseManager.deleteTable(dbName, tableName);
199: } catch (SQLException e) {
200: logger
201: .debug(
202: "The table delete threw an error during the execution {0}",
203: e);
204: } catch (NamingException e) {
205: logger
206: .debug(
207: "The table delete threw an error during the execution {0}",
208: e);
209: }
210: }
211:
212: /**
213: * Returns the bean used during the tests.
214: * @return the bean.
215: */
216: protected ItfContainerTransaction getBean() {
217: return sfsbContainerTransaction;
218: }
219:
220: /**
221: * Verifies if the container throws an IllegalStateException when a bean with
222: * container-managed transaction tries to get an UserTransaction. The bean
223: * gets the UserTransaction by using a EJBContext.
224: * @input -
225: * @output an IllegalStateException.
226: * @throws Exception if an error during the tests occurs.
227: */
228: public void testGetUserTransactionWithLookup() throws Exception {
229: try {
230: sfsbContainerTransaction.getUserTransactionWithLookup();
231: } catch (TransactionException e) {
232: throw e.getParentException();
233: }
234: }
235:
236: /**
237: * Verifies if the container throws a NameNotFoundException when a bean with
238: * container-managed transaction tries to get an UserTransaction. The bean
239: * gets the UserTransaction by using a lookup.
240: * @input -
241: * @output an IllegalStateException.
242: * @throws Exception if an error during the tests occurs.
243: */
244: public void testGetUserTransactionWithEJBContext() throws Exception {
245: try {
246: sfsbContainerTransaction.getUserTransactionWithEJBContext();
247: } catch (TransactionException e) {
248: throw e.getParentException();
249: }
250: }
251:
252: /**
253: * Gets a new transaction.
254: * @return the class UserTransaction.
255: * @throws Exception if a lookup error occurs.
256: */
257: protected UserTransaction getUserTransaction() throws Exception {
258: UserTransaction utx = TransactionHelper
259: .getInternalUserTransaction();
260: logger.debug("Transaction status after get = {0} ",
261: new Integer(utx.getStatus()));
262: return utx;
263: }
264:
265: /**
266: * Makes a rollback in the transaction that is active.
267: * @throws Exception if an error occurs during the rollback.
268: */
269: public void cleanTransaction() throws Exception {
270: UserTransaction utx = TransactionHelper
271: .getInternalUserTransaction();
272: try {
273: if (transactionIsActive()) {
274: utx.rollback();
275: }
276: } catch (Exception e) {
277: throw new Exception(
278: "Cannot clean the transaction. The test cannot be started",
279: e);
280: }
281: }
282:
283: /**
284: * Verifies if the transaction in the client side is active.
285: * @return true if the transaction is active, false otherwise.
286: * @throws Exception id a lookup error occurs
287: */
288: protected boolean transactionIsActive() throws Exception {
289: UserTransaction utx = TransactionHelper
290: .getInternalUserTransaction();
291: boolean bolResult = false;
292: if (utx != null) {
293: if (utx.getStatus() == Status.STATUS_ACTIVE) {
294: bolResult = true;
295: }
296: }
297: return bolResult;
298: }
299:
300: }
|