KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestContainerTransactionException.java 893 2006-07-19 14:19:23Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.transaction.containermanaged.base;
26
27 import static org.testng.Assert.fail;
28
29 import javax.ejb.EJBException JavaDoc;
30
31 import org.objectweb.easybeans.log.JLog;
32 import org.objectweb.easybeans.log.JLogFactory;
33 import org.objectweb.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
34 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.transaction.ItfTransactionMisc00;
35 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
36 import org.objectweb.easybeans.tests.common.helper.EmbeddedHelper;
37
38 /**
39  * Verifies if the container manages well the runtime exception and the transaction among the beans.
40  * @author Gisele Pinheiro Souza
41  * @author Eduardo Studzinski Estima de Castro
42  */

43 public abstract class TestContainerTransactionException {
44
45     /**
46      * Constant that defines the first database name.
47      */

48     protected static final String JavaDoc DATABASE_1 = "jdbc_1";
49
50     /**
51      * Constant that defines the second database name.
52      */

53     protected static final String JavaDoc DATABASE_2 = "jdbc_2";
54
55     /**
56      * Bean used during the tests that throws a runtime exception.
57      */

58     private ItfContainerTransaction sfsbContainerTransactionRuntime = null;
59
60
61     /**
62      * Logger.
63      */

64     private static JLog logger = JLogFactory.getLog(TestContainerTransactionException.class);
65
66
67     /**
68      * Initiates all things needed to execute the tests.
69      * @throws Exception if an error occurs.
70      */

71     public void setup() throws Exception JavaDoc {
72         // Inserts all database before execute the test
73
// used because the container does not provide this feature yet
74
EmbeddedHelper.bindDatasource();
75         // creates the bean used to manages the databse in the server site.
76
//cleans the transaction active in other test
77
ExceptionHandleUtil.cleanTransaction();
78         //creates the bean used during the tests
79
createBeanRuntime();
80         //deletes all tables
81
deleteTable();
82     }
83
84
85     /**
86      * Gets a new instance of the bean used in the runtime exception tests.
87      * @param beanClass the bean class name used to make the lookup.
88      * @throws Exception if a lookup error occurs.
89      */

90     public void createBeanRuntime(final Class JavaDoc beanClass) throws Exception JavaDoc {
91         sfsbContainerTransactionRuntime = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class);
92     }
93
94     /**
95      * Gets a new instance of the bean used in the runtime exception tests.
96      * @throws Exception if a lookup error occurs.
97      */

98     public abstract void createBeanRuntime() throws Exception JavaDoc;
99
100
101     /**
102      * Verifies if the container manages the transaction among divers beans.The
103      * first bean creates a table in the first database and calls other
104      * bean(with transaction attribute REQUIRED) to insert a table in the second
105      * database. After the both insertions, the first bean throws a RuntimeException
106      * exception. Also, verifies if the container throws an EJBException, discards the bean and does the rollback in
107      * this case(if the transaction atttribute of the first bean has a
108      * transaction context).
109      * @throws Exception if an error during the tests occurs.
110      */

111     public void testCallOtherBeanReq() throws Exception JavaDoc {
112         try {
113             sfsbContainerTransactionRuntime.insertTablesUsingAuxBeanReq(DATABASE_1, DATABASE_2);
114             fail("The container did not throw the EJBException.");
115         } catch (EJBException JavaDoc e) {
116             logger.debug("The bean threw an expected error during the execution {0}", e);
117         }
118         // verifies if the container discarded the instance.
119
if (!ExceptionHandleUtil.isDiscarded(sfsbContainerTransactionRuntime)) {
120             fail("The bean was not discarded.");
121         }
122
123         // TODO - verifies if the container log the error.
124
}
125
126     /**
127      * Verifies if the container manages the transaction among divers beans.The
128      * first bean creates a table in the first database and calls other
129      * bean(with transaction attribute NOT_SUPPORTS) to insert a table in the second
130      * database. After the both insertions, the first bean throws a RuntimeException
131      * exception. Also, verifies if the container throws an EJBException, discards the bean and does the rollback in
132      * this case(if the transaction atttribute of the first bean has a
133      * transaction context).
134      * @throws Exception if an error during the tests occurs.
135      */

136     public void testCallOtherBeanNotSup() throws Exception JavaDoc {
137         try {
138             sfsbContainerTransactionRuntime.insertTablesUsingAuxBeanNotSup(DATABASE_1, DATABASE_2);
139             fail("The container did not throw the EJBException.");
140         } catch (EJBException JavaDoc e) {
141             logger.debug("The bean threw an expected error during the execution {0}", e);
142         }
143         // verifies if the container discarded the instance.
144
if (!ExceptionHandleUtil.isDiscarded(sfsbContainerTransactionRuntime)) {
145             fail("The bean was not discarded.");
146         }
147
148         // TODO - verifies if the container log the error.
149
}
150
151     /**
152      * Returns the bean that throws a runtime exception.
153      * @return the bean.
154      */

155     public ItfContainerTransaction getRuntimeBean(){
156         return sfsbContainerTransactionRuntime;
157     }
158
159     /**
160      * Deletes the tables created during test.
161      * @throws Exception if a lookup error occurs.
162      *
163      */

164     public void deleteTable() throws Exception JavaDoc {
165         // deletes the table after each test to avoid errors.
166
ExceptionHandleUtil.deleteTable(DATABASE_1, ItfContainerTransaction.TABLE);
167         ExceptionHandleUtil.deleteTable(DATABASE_2, ItfContainerTransaction.TABLE);
168         // deletes the auxiliar bean table
169
ExceptionHandleUtil.deleteTable(DATABASE_1, ItfTransactionMisc00.TABLE);
170         ExceptionHandleUtil.deleteTable(DATABASE_2, ItfTransactionMisc00.TABLE);
171     }
172
173
174 }
175
Popular Tags