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: ExceptionHandleUtil.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.ejb.NoSuchEJBException;
029: import javax.naming.NamingException;
030: import javax.transaction.Status;
031: import javax.transaction.SystemException;
032: import javax.transaction.UserTransaction;
033:
034: import org.ow2.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
035: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
036: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
037: import org.ow2.easybeans.tests.common.helper.EJBHelper;
038: import org.ow2.easybeans.tests.common.helper.TransactionHelper;
039: import org.ow2.util.log.Log;
040: import org.ow2.util.log.LogFactory;
041:
042: /**
043: * Has useful methods for the transaction tests.
044: * @author Gisele Pinheiro Souza
045: * @author Eduardo Studzinski Estima de Castro
046: *
047: */
048: public final class ExceptionHandleUtil {
049:
050: /**
051: * Logger.
052: */
053: private static Log logger = LogFactory
054: .getLog(ExceptionHandleUtil.class);
055:
056: /**
057: * Creates a new instance.
058: *
059: */
060: private ExceptionHandleUtil() {
061:
062: }
063:
064: /**
065: * Gets a bean instance.
066: * @throws Exception if a lookup error occurs.
067: * @return a new instance of the bean.
068: */
069: private static ItfDatabaseManager getBean() throws Exception {
070: return EJBHelper.getBeanRemoteInstance(
071: SLSBDatabaseManager.class, ItfDatabaseManager.class);
072:
073: }
074:
075: /**
076: * Verifies if the table was created in the database.
077: * @param database the database name.
078: * @param tableName the table name verified.
079: * @throws Exception if an error when the bean is goten occurs.
080: */
081: public static void verifyTable(final String database,
082: final String tableName) throws Exception {
083: getBean().verifyTable(database, tableName);
084: }
085:
086: /**
087: * Calls the bean method toString() to verify if the bean was discarded.
088: * @param bean the bean that is verified.
089: * @return true if the bean is discarded and false otherwise.
090: * @throws Exception if an error during the test occurs.
091: */
092: public static boolean isDiscarded(final ItfContainerTransaction bean)
093: throws Exception {
094: boolean bolResult = false;
095: try {
096: bean.getUserTransactionWithLookup();
097: } catch (NoSuchEJBException e) {
098: bolResult = true;
099: logger
100: .debug(
101: "The bean threw an expected error during the execution {0}",
102: e);
103: }
104: return bolResult;
105: }
106:
107: /**
108: * Deletes the table in the database.
109: * @param dbName the database name in the registry.
110: * @param tableName the table name.
111: * @throws Exception if a lookup error occurs.
112: */
113: public static void deleteTable(final String dbName,
114: final String tableName) throws Exception {
115: // deletes the table after each test to avoid errors.
116: try {
117: getBean().deleteTable(dbName, tableName);
118: } catch (SQLException e) {
119: logger
120: .debug(
121: "The table delete threw an error during the execution {0}",
122: e);
123: } catch (NamingException e) {
124: logger
125: .debug(
126: "The table delete threw an error during the execution {0}",
127: e);
128: }
129: }
130:
131: /**
132: * Makes a rollback in the transaction that is active.
133: * @throws Exception if an error occurs during the rollback.
134: */
135: public static void cleanTransaction() throws Exception {
136: UserTransaction utx = TransactionHelper
137: .getInternalUserTransaction();
138: try {
139: if (transactionIsActive()) {
140: utx.rollback();
141: }
142: } catch (Exception e) {
143: throw new Exception(
144: "Cannot clean the transaction. The test cannot be started",
145: e);
146: }
147: }
148:
149: /**
150: * Verifies if the transaction in the client side is active.
151: * @return true if the transaction is active, false otherwise.
152: * @throws SystemException if a transaction exception occurs.
153: * @throws NamingException if a lookup error occurs.
154: */
155: public static boolean transactionIsActive() throws SystemException,
156: NamingException {
157: UserTransaction utx = TransactionHelper
158: .getInternalUserTransaction();
159: boolean bolResult = false;
160: if (utx != null) {
161: if (utx.getStatus() == Status.STATUS_ACTIVE) {
162: bolResult = true;
163: }
164: }
165: return bolResult;
166: }
167:
168: /**
169: * Gets a new transaction.
170: * @throws NamingException if a lookup error occurs.
171: * @return the class UserTransaction.
172: */
173: public static UserTransaction getUserTransaction()
174: throws NamingException {
175: UserTransaction utx = TransactionHelper
176: .getInternalUserTransaction();
177: return utx;
178: }
179: }
|