KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestContainerTransactionBase.java 808 2006-07-03 13:58:29Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.transaction.containermanaged.base;
26
27 import java.sql.SQLException JavaDoc;
28
29 import javax.naming.NamingException JavaDoc;
30 import javax.transaction.Status JavaDoc;
31 import javax.transaction.UserTransaction JavaDoc;
32
33 import org.objectweb.easybeans.log.JLog;
34 import org.objectweb.easybeans.log.JLogFactory;
35 import org.objectweb.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
36 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
37 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
38 import org.objectweb.easybeans.tests.common.exception.TransactionException;
39 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
40 import org.objectweb.easybeans.tests.common.helper.EmbeddedHelper;
41 import org.objectweb.easybeans.tests.common.helper.TransactionHelper;
42 import org.objectweb.easybeans.tests.common.interfaces.ItfTestContainerManaged;
43
44 /**
45  * Contains all base methods for using during the tests about the
46  * container-managed.
47  * @author Gisele Pinheiro Souza
48  * @author Eduardo Studzinski Estima de Castro
49  */

50 public class TestContainerTransactionBase implements ItfTestContainerManaged {
51
52     /**
53      * Constant that defines the first database name.
54      */

55     protected static final String JavaDoc DATABASE_1 = "jdbc_1";
56
57     /**
58      * Constant that defines the second database name.
59      */

60     protected static final String JavaDoc DATABASE_2 = "jdbc_2";
61
62     /**
63      * Bean used during the tests.
64      */

65     private ItfContainerTransaction sfsbContainerTransaction = null;
66
67     /**
68      * Bean used to manage the database in the server side.
69      */

70     private ItfDatabaseManager slsbDatabaseManager;
71
72
73     /**
74      * Logger.
75      */

76     private static JLog logger = JLogFactory.getLog(TestContainerTransactionBase.class);
77
78     /**
79      * Verifies if the table was created in the database.
80      * @param database the database name.
81      * @param tableName the table name verified.
82      * @throws NamingException if a lookup error occurs.
83      * @throws SQLException if a database error occurs.
84      */

85     protected void verifyTable(final String JavaDoc database, final String JavaDoc tableName) throws NamingException JavaDoc, SQLException JavaDoc {
86         slsbDatabaseManager.verifyTable(database, tableName);
87     }
88
89     /**
90      * @see org.objectweb.easybeans.tests.common.interfaces.ItfTestContainerManaged#setup()
91      * @throws Exception if a database error occurs.
92      */

93     public void setup() throws Exception JavaDoc {
94         // Inserts all database before execute the test
95
// used because the container does not provide this feature yet
96
EmbeddedHelper.bindDatasource();
97         // creates the bean used to manages the databse in the server site.
98
slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class, ItfDatabaseManager.class);
99         //cleans the transaction active in other test
100
cleanTransaction();
101     }
102
103     /**
104      * @see org.objectweb.easybeans.tests.common.interfaces.ItfTestContainerManaged#createBean(java.lang.Class)
105      * @param beanClass the bean class name used to make the lookup.
106      * @throws Exception if a lookup error occurs.
107      */

108     public void createBean(final Class JavaDoc beanClass) throws Exception JavaDoc {
109         sfsbContainerTransaction = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class);
110     }
111
112     /**
113      * Creates a transaction, executes a method in the bean that creates a
114      * database and after forces a rollback. The specification says that the
115      * bean and the client, in this case, must to use the same transaction. So,
116      * the rollback must to be done in the bean also.
117      * @throws Exception if an error during the transaction occurs.
118      */

119     private void executeErrorTransaction() throws Exception JavaDoc {
120         UserTransaction JavaDoc utx = getUserTransaction();
121         // starts the transaction
122
utx.begin();
123         // call the method that must have the same transaction context
124
sfsbContainerTransaction.insertCorrectTableInBothDB(DATABASE_1, DATABASE_2);
125         // rollback the transaction
126
utx.rollback();
127     }
128
129     /**
130      * @see org.objectweb.easybeans.tests.common.interfaces.ItfTestContainerManaged#testSetRollbackOnly()
131      * @throws Exception if an error during the tests occurs.
132      */

133     public void testSetRollbackOnly() throws Exception JavaDoc {
134         // calls the setRollbackOnly
135
try {
136             sfsbContainerTransaction.setRollbackOnly(DATABASE_1, DATABASE_2);
137         } catch (TransactionException e) {
138             throw e.getParentException();
139         }
140         /*
141          * Verifies if the tables were inserted. The transaction attributes,
142          * that supports the setRollbackOnly, must to make a roll back when the
143          * method invocation completes.
144          */

145         verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
146         verifyTable(DATABASE_2, ItfContainerTransaction.TABLE);
147     }
148
149     /**
150      * @see org.objectweb.easybeans.tests.common.interfaces.ItfTestContainerManaged
151      * @throws Exception if an error during the tests occurs.
152      */

153     public void testGetRollbackOnly() throws Exception JavaDoc {
154         try {
155             sfsbContainerTransaction.getRollbackOnly();
156         } catch (TransactionException e) {
157             throw e.getParentException();
158         }
159     }
160
161     /**
162      * @see org.objectweb.easybeans.tests.common.interfaces.ItfTestContainerManaged#testUsingClientTrans()
163      * @throws Exception if an error during the tests occurs.
164      */

165     public void testUsingClientTrans() throws Exception JavaDoc {
166         executeErrorTransaction();
167
168         // verifies if the table was created in the DATABASE_1
169
verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
170         // verifies if the table was created in the DATABASE_2
171
verifyTable(DATABASE_2, ItfContainerTransaction.TABLE);
172     }
173
174     /**
175      * @see org.objectweb.easybeans.tests.common.interfaces.ItfTestContainerManaged
176      */

177     public void deleteTable() {
178         // deletes the table after each test to avoid errors.
179
deleteTable(DATABASE_1, ItfContainerTransaction.TABLE);
180         deleteTable(DATABASE_2, ItfContainerTransaction.TABLE);
181     }
182
183     /**
184      * Deletes the table in the database.
185      * @param dbName the database name in the registry.
186      * @param tableName the table name.
187      */

188     protected void deleteTable(final String JavaDoc dbName, final String JavaDoc tableName) {
189         // deletes the table after each test to avoid errors.
190
try {
191             slsbDatabaseManager.deleteTable(dbName, tableName);
192         } catch (SQLException JavaDoc e) {
193             logger.debug("The table delete threw an error during the execution {0}", e);
194         } catch (NamingException JavaDoc e) {
195             logger.debug("The table delete threw an error during the execution {0}", e);
196         }
197     }
198
199     /**
200      * Returns the bean used during the tests.
201      * @return the bean.
202      */

203     protected ItfContainerTransaction getBean() {
204         return sfsbContainerTransaction;
205     }
206
207
208     /**
209      * Verifies if the container throws an IllegalStateException when a bean with
210      * container-managed transaction tries to get an UserTransaction. The bean
211      * gets the UserTransaction by using a EJBContext.
212      * @input -
213      * @output an IllegalStateException.
214      * @throws Exception if an error during the tests occurs.
215      */

216     public void testGetUserTransactionWithLookup() throws Exception JavaDoc {
217         try {
218             sfsbContainerTransaction.getUserTransactionWithLookup();
219         } catch (TransactionException e) {
220             throw e.getParentException();
221         }
222     }
223
224     /**
225      * Verifies if the container throws a NameNotFoundException when a bean with
226      * container-managed transaction tries to get an UserTransaction. The bean
227      * gets the UserTransaction by using a lookup.
228      * @input -
229      * @output an IllegalStateException.
230      * @throws Exception if an error during the tests occurs.
231      */

232     public void testGetUserTransactionWithEJBContext() throws Exception JavaDoc {
233         try {
234             sfsbContainerTransaction.getUserTransactionWithEJBContext();
235         } catch (TransactionException e) {
236             throw e.getParentException();
237         }
238     }
239
240     /**
241      * Gets a new transaction.
242      * @return the class UserTransaction.
243      * @throws Exception if a lookup error occurs.
244      */

245     protected UserTransaction JavaDoc getUserTransaction() throws Exception JavaDoc {
246         UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
247         logger.debug("Transaction status after get = {0} ", new Integer JavaDoc(utx.getStatus()));
248         return utx;
249     }
250
251     /**
252      * Makes a rollback in the transaction that is active.
253      * @throws Exception if an error occurs during the rollback.
254      */

255     public void cleanTransaction() throws Exception JavaDoc {
256         UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
257         try {
258             if (transactionIsActive()) {
259                 utx.rollback();
260            }
261         } catch (Exception JavaDoc e) {
262             throw new Exception JavaDoc("Cannot clean the transaction. The test cannot be started", e);
263         }
264     }
265
266     /**
267      * Verifies if the transaction in the client side is active.
268      * @return true if the transaction is active, false otherwise.
269      * @throws Exception id a lookup error occurs
270      */

271     protected boolean transactionIsActive() throws Exception JavaDoc {
272         UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
273         boolean bolResult = false;
274         if (utx != null) {
275             if (utx.getStatus() == Status.STATUS_ACTIVE) {
276                 bolResult = true;
277             }
278         }
279         return bolResult;
280     }
281
282 }
283
Popular Tags