KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > transaction > containermanaged > stateful > TestSessionSynchronization


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

25 package org.objectweb.easybeans.tests.transaction.containermanaged.stateful;
26
27 import static org.testng.Assert.assertTrue;
28 import static org.testng.Assert.fail;
29
30 import java.sql.SQLException JavaDoc;
31
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.ejbs.stateful.containermanaged.transaction.ItfSessionSync;
37 import org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.transaction.SFSBSessionSync;
38 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
39 import org.objectweb.easybeans.tests.transaction.containermanaged.base.TestContainerTransactionBase;
40 import org.testng.annotations.BeforeClass;
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
43
44 /**
45  * Verifies if the container-managed transaction in the stateful bean is
46  * following the JSR 220.The test verifies if the SessionSynchronization Callbacks are working properly.
47  * The items covered in this test are: 12.6.2.11.
48  * @reference JSR 220-PROPOSED FINAL
49  * @requirement Application Server must be running; the bean
50  * org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.SFSBSessionSync
51  * must be deployed.
52  * @setup gets the reference of SFSBSessionSync and binds the
53  * databases specified in the EmbeddedTest.
54  * @author Gisele Pinheiro Souza
55  * @author Eduardo Studzinski Estima de Castro
56  */

57 public class TestSessionSynchronization extends TestContainerTransactionBase {
58
59     /**
60      * Bean used during the tests.
61      */

62     private ItfSessionSync sfsbSessionSync;
63
64     /**
65      * Logger.
66      */

67     private static JLog logger = JLogFactory.getLog(TestSessionSynchronization.class);
68
69     /**
70      * Creates the bean before each test to avoid errors during the tests.
71      * @throws Exception if an error during the bean creation occurs.
72      */

73     @BeforeMethod
74     public void createBean() throws Exception JavaDoc {
75         sfsbSessionSync = EJBHelper.getBeanRemoteInstance(SFSBSessionSync.class, ItfSessionSync.class);
76         sfsbSessionSync.startup(DATABASE_1, DATABASE_2);
77     }
78
79     /**
80      * Binds the databases used.
81      * @throws Exception if an error during the test startup occurs.
82      */

83     @BeforeClass
84     @Override JavaDoc
85     public void setup() throws Exception JavaDoc {
86         super.setup();
87     }
88
89     /**
90      * Verifies if the callback methods were called correctly. The afterbegin,
91      * before completion must stay in the same transaction that the method
92      * called. The beforeCompletation calls a setRollbackOnly, so the table
93      * created in the afterBegin must to be deleted and the afterCompletation
94      * must receive that the transaction was not commited.
95      * @throws Exception if an unexpected error occurs during the test.
96      */

97     private void verifyCallbacks() throws Exception JavaDoc {
98         // verifies if the table was created in the DATABASE_1
99
// the afterBegin method inserts the table in this database.
100
try {
101             verifyTable(DATABASE_1, ItfSessionSync.TABLE);
102             fail("The beforeCompletion method was not called "
103                     + " or the afterBegin method and the method called were not in the same transaction.");
104         } catch (SQLException JavaDoc e) {
105             logger.debug("The method threw an expected exception {0}", e);
106         }
107
108         // verifies if the table was created in the DATABASE_2
109
// the insertTableRequired method inserts the table in this database.
110
try {
111             verifyTable(DATABASE_2, ItfSessionSync.TABLE);
112             fail("The beforeCompletion method was not called.");
113         } catch (SQLException JavaDoc e) {
114             logger.debug("The method threw an expected exception {0}", e);
115         }
116         // the after completion method inserts the value in this variable,
117
// so it tests if the afterCompletion is called.
118
assertTrue(sfsbSessionSync.isRolledback());
119     }
120
121     /**
122      * Verifies if the table created by the afterBegin method is created when a
123      * method that does not support transaction is called.Also, this method
124      * verifies if the table in the method was create properly.
125      * @throws Exception if an unexpected error occurs during the test.
126      */

127     private void verifyTablesWithouCallbacks() throws Exception JavaDoc {
128         // verifies if the table was created in the DATABASE_1
129
// the afterTransaction method inserts the table in this database.
130
try {
131             verifyTable(DATABASE_1, ItfSessionSync.TABLE);
132             fail("The method afterbegin was called, but it is not correct in this case.");
133         } catch (SQLException JavaDoc e) {
134             logger.debug("The method threw an expected exception {0}", e);
135         }
136         // verifies if the table was created in the DATABASE_?
137
// the method CALLED inserts the table in this database.
138
try {
139             verifyTable(DATABASE_2, ItfSessionSync.TABLE);
140             logger.debug("The table was inserted correctly.");
141         } catch (SQLException JavaDoc e) {
142             fail("The container rolled back the transaction, but there is not transaction associated.");
143         }
144     }
145
146     /**
147      * Calls the method that uses the required attribute.The bean implements the
148      * SessionSynchronization interface, so the methods afterBegin,
149      * beforeCompletion and afterCompletion must be called. The method
150      * beforeCompletion calls setRollbackOnly, so the transaction must be rolled
151      * back.
152      * @input -
153      * @output the tables not created because the transaction rolled back.
154      * @throws Exception if an error occurs during the tests.
155      */

156     @Test(dependsOnMethods = {"testSessionSyncWithUserTransAndSupports"})
157     public void testSessionSyncWithRequired() throws Exception JavaDoc {
158         sfsbSessionSync.insertTableRequired();
159         verifyCallbacks();
160     }
161
162     /**
163      * Calls the method that uses the requires_new attribute.The bean implements the
164      * SessionSynchronization interface, so the methods afterBegin,
165      * beforeCompletion and afterCompletion must be called. The method
166      * beforeCompletion calls setRollbackOnly, so the transaction must be
167      * rolled back.
168      * @input -
169      * @output the tables not created because the transaction rolled back.
170      * @throws Exception if an error occurs during the tests.
171      */

172     @Test
173     public void testSessionSyncWithRequiresNew() throws Exception JavaDoc {
174         sfsbSessionSync.insertTableRequiredNew();
175         verifyCallbacks();
176     }
177
178     /**
179      * Calls the method that uses the mandatory attribute.The bean implements the
180      * SessionSynchronization interface, so the methods afterBegin,
181      * beforeCompletion and afterCompletion must be called. The method
182      * beforeCompletion calls setRollbackOnly, so the transaction must be
183      * rolled back.
184      * @input -
185      * @output the tables not created because the transaction rolled back.
186      * @throws Exception if an error occurs during the tests.
187      */

188     @Test
189     public void testSessionSyncWithMandatory() throws Exception JavaDoc {
190         UserTransaction JavaDoc utx = getUserTransaction();
191         utx.begin();
192         sfsbSessionSync.insertTableMandatory();
193         try{
194             utx.commit();
195         }catch(Exception JavaDoc e){
196            logger.debug("The method threw an expected exception {e}", e);
197         }
198         verifyCallbacks();
199     }
200
201     /**
202      * Calls the method with transaction attribute supports and verifies if the
203      * methods callback are called. The transaction attribute supports must to
204      * have the same behaviour that the required.
205      * @input -
206      * @output the databases without tables, because the container made a
207      * rollback.
208      * @throws Exception if an error occurs during the tests.
209      */

210     @Test
211     public void testSessionSyncWithUserTransAndSupports() throws Exception JavaDoc {
212         UserTransaction JavaDoc utx = getUserTransaction();
213         utx.begin();
214         sfsbSessionSync.insertTableSupports();
215         try{
216             utx.commit();
217         }catch(Exception JavaDoc e){
218            logger.debug("The method threw an expected exception {e}", e);
219         }
220         verifyCallbacks();
221     }
222
223     /**
224      * Calls the method with transaction attribute supports and verifies if the
225      * container did not call the callbacks methdos from SessionSynchronization
226      * interface. The container must no call this methods, because the method is
227      * not in a transaction context.
228      * @throws Exception if an error occurs during the tests.
229      */

230     @Test
231     public void testSessionSyncWithOnlySupports() throws Exception JavaDoc {
232         sfsbSessionSync.insertTableSupports();
233         verifyTablesWithouCallbacks();
234     }
235
236     /**
237      * Calls the method with transaction attribute not_supported and verifies if the
238      * container did not call the callbacks methdos from SessionSynchronization
239      * interface. The container must no call this methods, because the method is
240      * not in a transaction context.
241      * @throws Exception if an error occurs during the tests.
242      */

243     @Test
244     public void testSessionSyncWithNotSupported() throws Exception JavaDoc {
245         sfsbSessionSync.insertTableNotSupported();
246         verifyTablesWithouCallbacks();
247     }
248
249     /**
250      * Calls the method with transaction attribute never and verifies if the
251      * container did not call the callbacks methdos from SessionSynchronization
252      * interface. The container must no call this methods, because the method is
253      * not in a transaction context.
254      * @throws Exception if an error occurs during the tests.
255      */

256     @Test
257     public void testSessionSyncWithNever() throws Exception JavaDoc {
258         sfsbSessionSync.insertTableNever();
259         verifyTablesWithouCallbacks();
260     }
261
262     /**
263      * Deletes the table before each test to avoid errors in each test.
264      */

265     @BeforeMethod
266     @Override JavaDoc
267     public void deleteTable() {
268         deleteTable(DATABASE_1, ItfSessionSync.TABLE);
269         deleteTable(DATABASE_2, ItfSessionSync.TABLE);
270     }
271
272     /**
273      * Makes a rollback in the transaction actives.
274      * @throws Exception if a transaction exception occurs.
275      */

276     @BeforeMethod
277     @Override JavaDoc
278     public void cleanTransaction() throws Exception JavaDoc{
279         super.cleanTransaction();
280     }
281
282 }
283
Popular Tags