KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > transaction > beanmanaged > TestTransactionCommitSFSB


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: TestTransactionCommitSFSB.java 980 2006-07-28 13:20:32Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.transaction.beanmanaged;
26
27 import java.sql.SQLException JavaDoc;
28
29 import javax.ejb.EJBException JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.transaction.Status JavaDoc;
32 import javax.transaction.UserTransaction JavaDoc;
33
34 import org.objectweb.easybeans.log.JLog;
35 import org.objectweb.easybeans.log.JLogFactory;
36 import org.objectweb.easybeans.tests.common.asserts.Assert;
37 import org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.ItfBeanManagedTransaction;
38 import org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.SFSBBeanManagedTransaction;
39 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
40 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
41 import org.objectweb.easybeans.tests.common.exception.TransactionException;
42 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
43 import org.objectweb.easybeans.tests.common.helper.EmbeddedHelper;
44 import org.objectweb.easybeans.tests.common.helper.ExceptionHelper;
45 import org.objectweb.easybeans.tests.common.helper.TransactionHelper;
46 import org.testng.annotations.AfterClass;
47 import org.testng.annotations.BeforeClass;
48 import org.testng.annotations.BeforeMethod;
49 import org.testng.annotations.Test;
50
51 /**
52  * Verifies if the bean-managed transaction in the stateful bean is following
53  * the JSR 220.The items couvered in this test are: 12.2.3 and 12.3.3 spec.
54  * @reference JSR 220-PROPOSED FINAL
55  * @requirement Application Server must be running; the bean
56  * org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.SFSBBeanManagedTransaction
57  * must be deployed.
58  * @setup gets the reference of SFSBBeanManagedTransaction and binds the
59  * databases specified in the EmbeddedTest.
60  * @author Gisele Pinheiro Souza
61  * @author Eduardo Studzinski Estima de Castro
62  */

63 public class TestTransactionCommitSFSB {
64
65     /**
66      * Bean used during the tests.
67      */

68     private ItfBeanManagedTransaction sfsbBeanManagedTransaction;
69
70     /**
71      * Bean used to manage the database in the server side.
72      */

73     private ItfDatabaseManager slsbDatabaseManager;
74
75     /**
76      * The database used during the tests.
77      */

78     private static final String JavaDoc DATABASE = "jdbc_1";
79
80     /**
81      * Logger.
82      */

83     private static JLog logger = JLogFactory.getLog(TestTransactionCommitSFSB.class);
84
85     /**
86      * Creates the bean used during the tests and binds the databases used.
87      * @throws Exception if an error during the test startup occurs.
88      */

89     @BeforeClass
90     public void setup() throws Exception JavaDoc {
91         // Inserts all database before execute the test
92
// used because the container does not provide this feature yet
93
EmbeddedHelper.bindDatasource();
94
95         // creates the bean used to manages the databse in the server site.
96
slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class, ItfDatabaseManager.class);
97     }
98
99     /**
100      * Tests if the container supports the bean that does not close the
101      * transaction in the same method that the transaction was opened.
102      * @input -
103      * @output SQLException because the insert was not commited.
104      * @throws Exception if an error during the test occurs.
105      */

106     @Test
107     public void testTransInTwoMethods() throws Exception JavaDoc {
108         try {
109             // inserts a table without making the commit.
110
sfsbBeanManagedTransaction.insertTableWithoutCommitTransaction();
111             // tries to delete the table in other connection.
112
sfsbBeanManagedTransaction.dropTableWithoutBeginTransaction();
113         } catch (TransactionException e) {
114             throw e.getParentException();
115         }
116     }
117
118     /**
119      * Tests if the container allows the bean open a transaction without close
120      * the other one, i.e., if the container allows nested transactions. The
121      * transaction is opened twice, but in differents methods.
122      * @input -
123      * @output a NotSupportedException
124      * @throws Exception if an error during the tests occurs.
125      */

126     @Test(expectedExceptions = javax.transaction.NotSupportedException JavaDoc.class)
127     public void testBeginTwiceSameTrans() throws Exception JavaDoc {
128         try {
129             // call an userTransaction.begin()
130
sfsbBeanManagedTransaction.insertTableWithoutCommitTransaction();
131             // call other userTransaction without finish the first one
132
sfsbBeanManagedTransaction.dropTableWithBeginCommitTransaction();
133         } catch (TransactionException e) {
134             throw e.getParentException();
135         }
136
137     }
138
139     /**
140      * Tests if the container allows the bean open a transaction without close
141      * the other one, i.e., if the container allows nested transactions. There
142      * are two transactions where the second one is started before the first one
143      * makes the commit. The both transaction are in the same method.
144      * @input -
145      * @output a NotSupportedException.
146      * @throws Exception if an error durong the tests occurs.
147      */

148     @Test(expectedExceptions = javax.transaction.NotSupportedException JavaDoc.class)
149     public void testBeginTwoTransSameMethod() throws Exception JavaDoc {
150         try {
151             // call an userTransaction.begin()
152
sfsbBeanManagedTransaction.insertTableWithNestedTrans();
153         } catch (TransactionException e) {
154             throw e.getParentException();
155         }
156     }
157
158     /**
159      * Tests if the container allows the bean open a transaction without close
160      * the other one, i.e., if the container allows nested transactions. There
161      * are two transactions where the second one is started before the first one
162      * makes the commit. Each transaction is in a different method.
163      * @input -
164      * @output a NotSupportedException
165      * @throws Exception if an error during the tests occurs.
166      */

167     @Test(expectedExceptions = javax.transaction.NotSupportedException JavaDoc.class)
168     public void testBeginTwoTransDifMethod() throws Exception JavaDoc {
169         try {
170             // call an userTransaction.begin()
171
sfsbBeanManagedTransaction.insertTableWithoutCommitTransaction();
172             // call other userTransaction without finish the first one
173
sfsbBeanManagedTransaction.insertTableWithNewTransaction();
174         } catch (TransactionException e) {
175             throw e.getParentException();
176         }
177     }
178
179     /**
180      * If the bean has a transaction active and the client too, the client
181      * transaction must be resumed when the bean is called.
182      * @input -
183      * @output the bean transaction will make a rollback and the client
184      * transaction must be active.
185      * @throws Exception if a erro during the tests occurs.
186      */

187     @Test
188     public void testUsingClientTransaction() throws Exception JavaDoc {
189         // calls a method that starts a transaction
190
sfsbBeanManagedTransaction.insertTableWithoutCommitTransaction();
191         // gets the transaction
192
UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
193         // starts the transaction
194
utx.begin();
195         // makes a rollback in the bean transaction, the container
196
// must to resume the user transaction and execute the
197
// bean transaction in other transaction
198
sfsbBeanManagedTransaction.setRollback();
199         // tries to commit the transaction to avoid a nested transaction
200
try {
201             utx.commit();
202         } catch (Exception JavaDoc e) {
203             logger.debug("Error when the transaction made a rollback {0}", e);
204         }
205         Integer JavaDoc[] expected = {new Integer JavaDoc(Status.STATUS_COMMITTED), new Integer JavaDoc(Status.STATUS_NO_TRANSACTION) };
206
207         // the user transaction must be active yet.
208
Assert.assertEquals(new Integer JavaDoc(utx.getStatus()), expected,
209                 "After the commit the transaction must be commited or not_transaction");
210
211     }
212
213     /**
214      * Tests if the container does not allow the bean uses the setRollbackOnly.
215      * @input -
216      * @output an IllegalStateException
217      * @throws Exception if an error during the tests occurs.
218      */

219     @Test
220     public void testSetRollbackOnly() throws Exception JavaDoc {
221         // calls the setrollbackonly that must to throw exception
222
try{
223             sfsbBeanManagedTransaction.setRollbackOnly();
224         }catch(EJBException JavaDoc e){
225             ExceptionHelper.checkCause(e, java.lang.IllegalStateException JavaDoc.class);
226         }
227     }
228     /**
229      * Tests if the container does not allow the bean uses the getRollbackOnly.
230      * @input -
231      * @output an IllegalStateException
232      * @throws Exception if an error during the tests occurs.
233      */

234     @Test
235     public void testgetRollbackOnly() throws Exception JavaDoc {
236         // calls the setrollbackonly that must to throw exception
237
try{
238             sfsbBeanManagedTransaction.getRollbackOnly();
239         }catch(EJBException JavaDoc e){
240             ExceptionHelper.checkCause(e, java.lang.IllegalStateException JavaDoc.class);
241         }
242     }
243     /**
244      * Tests if the transaction begin and the transaction commit work well.
245      * Tries to create a table and verifies if the table was created correctly.
246      * @input -
247      * @output method executed without exception.
248      * @throws Exception if an error during the test occurs.
249      */

250     @Test
251     public void testTransInSameMethod() throws Exception JavaDoc {
252         try {
253             sfsbBeanManagedTransaction.insertTableWithBeginCommitTransaction();
254         } catch (TransactionException e) {
255             throw e.getParentException();
256         }
257         // verifies if the table was created
258
slsbDatabaseManager.verifyTable(DATABASE, ItfBeanManagedTransaction.TABLE);
259
260     }
261
262     /**
263      * Tests if the rollback works properly. The method calls an
264      * UserTransaction.begin(), inserts a table and makes a rollback in the
265      * transaction.
266      * @input -
267      * @output the select * from test must return an exception, because the
268      * table does not exists.
269      * @throws Exception if an error during the test occurs.
270      */

271     @Test(expectedExceptions = SQLException JavaDoc.class)
272     public void testRollbackInSameMethod() throws Exception JavaDoc {
273         sfsbBeanManagedTransaction.insertTableWithBeginRollback();
274
275         // verifies if the table was created, the expected is the table not
276
// exists.
277
slsbDatabaseManager.verifyTable(DATABASE, ItfBeanManagedTransaction.TABLE);
278
279     }
280
281     /**
282      * Deletes the databases entries from the registry.
283      * @throws Exception if an error occurs during the unbind.
284      */

285     @AfterClass
286     public void tierDown() throws Exception JavaDoc {
287         // Removes all database registry references after execute the test
288
// it's used because the container does not provide this feature yet.
289
// It's run after each test to allow run each test separately.
290
EmbeddedHelper.unbindDatasource();
291     }
292
293     /**
294      * Deletes the table to avoid errors in each test.
295      */

296     @BeforeMethod
297     public void deletesTable() {
298         // deletes the table after each test to avoid errors.
299
try {
300             // verifies if the table was created
301
slsbDatabaseManager.deleteTable(DATABASE, ItfBeanManagedTransaction.TABLE);
302         } catch (SQLException JavaDoc e) {
303             logger.debug("The table delete threw an error during the execution {0}", e);
304         } catch (NamingException JavaDoc e) {
305             logger.debug("The table delete threw an error during the execution {0}", e);
306         }
307     }
308
309     /**
310      * Gets a new instance of the bean before each test.
311      * @throws Exception if an error during the bean creation occurs.
312      */

313     @BeforeMethod
314     public void createBean() throws Exception JavaDoc {
315         sfsbBeanManagedTransaction = EJBHelper.getBeanRemoteInstance(SFSBBeanManagedTransaction.class,
316                 ItfBeanManagedTransaction.class);
317         sfsbBeanManagedTransaction.startup(ItfBeanManagedTransaction.CREATE_TABLE, DATABASE);
318     }
319 }
320
Popular Tags