KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > transaction > containermanaged > base > ExceptionHandleUtil


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ExceptionHandleUtil.java 923 2006-07-25 10:56:06Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.transaction.containermanaged.base;
26
27 import java.sql.SQLException JavaDoc;
28
29 import javax.ejb.NoSuchEJBException JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.transaction.Status JavaDoc;
32 import javax.transaction.SystemException JavaDoc;
33 import javax.transaction.UserTransaction JavaDoc;
34
35 import org.objectweb.easybeans.log.JLog;
36 import org.objectweb.easybeans.log.JLogFactory;
37 import org.objectweb.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
38 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
39 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
40 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
41 import org.objectweb.easybeans.tests.common.helper.TransactionHelper;
42
43 /**
44  * Has useful methods for the transaction tests.
45  * @author Gisele Pinheiro Souza
46  * @author Eduardo Studzinski Estima de Castro
47  *
48  */

49 public final class ExceptionHandleUtil {
50
51
52     /**
53      * Logger.
54      */

55     private static JLog logger = JLogFactory.getLog(ExceptionHandleUtil.class);
56
57     /**
58      * Creates a new instance.
59      *
60      */

61     private ExceptionHandleUtil(){
62
63     }
64
65     /**
66      * Gets a bean instance.
67      * @throws Exception if a lookup error occurs.
68      * @return a new instance of the bean.
69      */

70     private static ItfDatabaseManager getBean() throws Exception JavaDoc{
71         return EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class, ItfDatabaseManager.class);
72
73     }
74     /**
75      * Verifies if the table was created in the database.
76      * @param database the database name.
77      * @param tableName the table name verified.
78      * @throws Exception if an error when the bean is goten occurs.
79      */

80     public static void verifyTable(final String JavaDoc database, final String JavaDoc tableName) throws Exception JavaDoc {
81          getBean().verifyTable(database, tableName);
82     }
83
84     /**
85      * Calls the bean method toString() to verify if the bean was discarded.
86      * @param bean the bean that is verified.
87      * @return true if the bean is discarded and false otherwise.
88      * @throws Exception if an error during the test occurs.
89      */

90     public static boolean isDiscarded(final ItfContainerTransaction bean) throws Exception JavaDoc {
91         boolean bolResult = false;
92         try {
93             bean.getUserTransactionWithLookup();
94         } catch (NoSuchEJBException JavaDoc e) {
95             bolResult = true;
96             logger.debug("The bean threw an expected error during the execution {0}", e);
97         }
98         return bolResult;
99     }
100
101
102     /**
103      * Deletes the table in the database.
104      * @param dbName the database name in the registry.
105      * @param tableName the table name.
106      * @throws Exception if a lookup error occurs.
107      */

108     public static void deleteTable(final String JavaDoc dbName, final String JavaDoc tableName) throws Exception JavaDoc {
109         // deletes the table after each test to avoid errors.
110
try {
111             getBean().deleteTable(dbName, tableName);
112         } catch (SQLException JavaDoc e) {
113             logger.debug("The table delete threw an error during the execution {0}", e);
114         } catch (NamingException JavaDoc e) {
115             logger.debug("The table delete threw an error during the execution {0}", e);
116         }
117     }
118     /**
119      * Makes a rollback in the transaction that is active.
120      * @throws Exception if an error occurs during the rollback.
121      */

122     public static void cleanTransaction() throws Exception JavaDoc {
123         UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
124         try {
125             if (transactionIsActive()) {
126                 utx.rollback();
127             }
128         } catch (Exception JavaDoc e) {
129             throw new Exception JavaDoc("Cannot clean the transaction. The test cannot be started", e);
130         }
131     }
132
133     /**
134      * Verifies if the transaction in the client side is active.
135      * @return true if the transaction is active, false otherwise.
136      * @throws SystemException if a transaction exception occurs.
137      * @throws NamingException if a lookup error occurs.
138      */

139     public static boolean transactionIsActive() throws SystemException JavaDoc, NamingException JavaDoc {
140         UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
141         boolean bolResult = false;
142         if (utx != null) {
143             if (utx.getStatus() == Status.STATUS_ACTIVE) {
144                 bolResult = true;
145             }
146         }
147         return bolResult;
148     }
149
150     /**
151      * Gets a new transaction.
152      * @throws NamingException if a lookup error occurs.
153      * @return the class UserTransaction.
154      */

155     public static UserTransaction JavaDoc getUserTransaction() throws NamingException JavaDoc {
156         UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
157         return utx;
158     }
159 }
160
Popular Tags