KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateful > containermanaged > xmldescriptor > SFSBTransAttributeTester


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: SFSBTransAttributeTester.java 659 2006-06-15 13:41:59Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.xmldescriptor;
26
27 import static org.testng.Assert.fail;
28
29 import java.sql.SQLException JavaDoc;
30
31 import javax.ejb.EJB JavaDoc;
32 import javax.ejb.EJBException JavaDoc;
33 import javax.ejb.Remote JavaDoc;
34 import javax.ejb.Stateful JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.transaction.UserTransaction JavaDoc;
37
38 import org.objectweb.easybeans.log.JLog;
39 import org.objectweb.easybeans.log.JLogFactory;
40 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
41 import org.objectweb.easybeans.tests.common.helper.TransactionHelper;
42
43 /**
44  * Verifies if the container sets all types of transaction attributes that are
45  * defined by the deployment descriptor.
46  * @author Gisele Pinheiro Souza
47  * @author Eduardo Studzinski Estima de Castro
48  */

49 @Stateful JavaDoc
50 @Remote JavaDoc(ItfTransAttributeTester.class)
51 public class SFSBTransAttributeTester implements ItfTransAttributeTester {
52
53     /**
54      * Bean that has the transaction attribute mandatory defined in the
55      * deployment descriptor.
56      */

57     @EJB JavaDoc(beanName = "SFSBTransAttributeDefMandatory")
58     private ItfTransAttributeDefinition beanMandatory;
59
60     /**
61      * Bean that has the transaction attribute required defined in the
62      * deployment descriptor.
63      */

64     @EJB JavaDoc(beanName = "SFSBTransAttributeDefRequired")
65     private ItfTransAttributeDefinition beanRequired;
66
67     /**
68      * Bean that has the transaction attribute requires_new defined in the
69      * deployment descriptor.
70      */

71     @EJB JavaDoc(beanName = "SFSBTransAttributeDefRequiresNew")
72     private ItfTransAttributeDefinition beanRequiresNew;
73
74     /**
75      * Bean that has the transaction attribute supports defined in the
76      * deployment descriptor.
77      */

78     @EJB JavaDoc(beanName = "SFSBTransAttributeDefSupports")
79     private ItfTransAttributeDefinition beanSupports;
80
81     /**
82      * Bean that has the transaction attribute not supported defined in the
83      * deployment descriptor.
84      */

85     @EJB JavaDoc(beanName = "SFSBTransAttributeDefNotSupported")
86     private ItfTransAttributeDefinition beanNotSupported;
87
88     /**
89      * Bean that has the transaction attribute never defined in the
90      * deployment descriptor.
91      */

92     @EJB JavaDoc(beanName = "SFSBTransAttributeDefNever")
93     private ItfTransAttributeDefinition beanNever;
94
95     /**
96      * Used to manipulate the data in the database.
97      */

98     @EJB JavaDoc
99     private ItfDatabaseManager slsbDatabaseManager;
100
101     /**
102      * Logger.
103      */

104     private static JLog logger = JLogFactory.getLog(SFSBTransAttributeTester.class);
105
106     /**
107      * Verifies if the client use the same transaction that the bean. Creates a
108      * transaction, call a bean method that acess the JDBC and after makes a
109      * rollback. If the bean uses the same transaction, the data in the database
110      * must be rolled back.
111      * @param bean the bean that is verified.
112      * @return true if the bean uses the same transaction that the client, false otherwise.
113      * @throws Exception if an error occurs.
114      */

115     private boolean usesClientTransaction(final ItfTransAttributeDefinition bean) throws Exception JavaDoc {
116         UserTransaction JavaDoc utx = TransactionHelper.getUserTransaction();
117         try {
118             utx.begin();
119             bean.insertTableCorrectMandatory(DB_NAME, TABLE_NAME);
120         } finally {
121             utx.rollback();
122         }
123         try {
124             slsbDatabaseManager.verifyTable(DB_NAME, TABLE_NAME);
125             return false;
126         } catch (SQLException JavaDoc e) {
127             return true;
128         }
129     }
130
131     /**
132      * Verifies if the bean has a transaction context.
133      * @param bean the bean that is verified.
134      * @return true if the bean uses a transaction, false otherwise.
135      * @throws Exception if an error occurs.
136      */

137     private boolean hasTransaction(final ItfTransAttributeDefinition bean) throws Exception JavaDoc {
138         bean.insertTableErrorMandatory(DB_NAME, TABLE_NAME);
139         try {
140             slsbDatabaseManager.verifyTable(DB_NAME, TABLE_NAME);
141             return false;
142         } catch (SQLException JavaDoc e) {
143             return true;
144         }
145     }
146
147     /**
148      * Verifies if the deployment descriptor overrides the default transaction
149      * attribute value. Also, verifies if the transaction attribute type is really mandatory.
150      * @throws Exception if an error occurs.
151      */

152     public void testMandatory() throws Exception JavaDoc {
153         //cleans the database to avoid errors.
154
deleteTable(DB_NAME, TABLE_NAME);
155
156         UserTransaction JavaDoc utx = TransactionHelper.getUserTransaction();
157         try {
158             utx.begin();
159             beanMandatory.insertTableCorrect(DB_NAME, TABLE_NAME);
160         } finally {
161             utx.rollback();
162         }
163         try {
164             slsbDatabaseManager.verifyTable(DB_NAME, TABLE_NAME);
165             fail("The deployment descriptor did not override the "
166                    + "annotation. The bean did not use the client transaction in mandatory mode.");
167         } catch (SQLException JavaDoc e) {
168             logger.debug("The bean thron an expected exception{0}", e);
169         }
170
171         //cleans the database to avoid errors.
172
deleteTable(DB_NAME, TABLE_NAME);
173
174         try {
175             beanMandatory.insertTableCorrect(DB_NAME, TABLE_NAME);
176         } catch (EJBException JavaDoc e) {
177             logger.debug("The bean thron an expected exception{0}", e);
178             fail("The deployment descriptor did not override the annotation. "
179                     + "The bean was called withou a transaction context in a mandatory mode and there was not exception.");
180         }
181     }
182
183     /**
184      * Verifies if the deployment descriptor overrides the transaction
185      * attribute mandatory. Also, verifies if the transaction attribute type is really required.
186      * @throws Exception if an error occurs.
187      */

188     public void testRequired() throws Exception JavaDoc {
189         //cleans the database to avoid errors.
190
deleteTable(DB_NAME, TABLE_NAME);
191         boolean bolClientTransaction = usesClientTransaction(beanRequired);
192         //cleans the database to avoid errors.
193
deleteTable(DB_NAME, TABLE_NAME);
194         boolean bolHasTransaction = hasTransaction(beanRequired);
195         if (!(bolClientTransaction && bolHasTransaction)) {
196             fail("The container did not override the transaction attribute for REQUIRED");
197         }
198     }
199
200     /**
201      * Verifies if the deployment descriptor overrides the transaction
202      * attribute mandatory. Also, verifies if the transaction attribute type is really requireds new.
203      * @throws Exception if an error occurs.
204      */

205     public void testRequiresNew() throws Exception JavaDoc {
206         //cleans the database to avoid errors.
207
deleteTable(DB_NAME, TABLE_NAME);
208         boolean bolClientTransaction = usesClientTransaction(beanRequiresNew);
209         //cleans the database to avoid errors.
210
deleteTable(DB_NAME, TABLE_NAME);
211         boolean bolHasTransaction = hasTransaction(beanRequiresNew);
212         if (!(!bolClientTransaction && bolHasTransaction)) {
213             fail("The container did not override the transaction attribute for REQUIRES_NEW");
214         }
215     }
216
217     /**
218      * Verifies if the deployment descriptor overrides the transaction
219      * attribute mandatory. Also, verifies if the transaction attribute type is really supports.
220      * @throws Exception if an error occurs.
221      */

222     public void testSupports() throws Exception JavaDoc {
223         //cleans the database to avoid errors.
224
deleteTable(DB_NAME, TABLE_NAME);
225         boolean bolClientTransaction = usesClientTransaction(beanSupports);
226         //cleans the database to avoid errors.
227
deleteTable(DB_NAME, TABLE_NAME);
228         boolean bolHasTransaction = hasTransaction(beanSupports);
229         if (!(bolClientTransaction && !bolHasTransaction)) {
230             fail("The container did not override the transaction attribute for SUPPORTS");
231         }
232     }
233
234     /**
235      * Verifies if the deployment descriptor overrides the transaction
236      * attribute mandatory. Also, verifies if the transaction attribute type is really not supported.
237      * @throws Exception if an error occurs.
238      */

239     public void testNotSupported() throws Exception JavaDoc {
240         //cleans the database to avoid errors.
241
deleteTable(DB_NAME, TABLE_NAME);
242         boolean bolClientTransaction = usesClientTransaction(beanNotSupported);
243         //cleans the database to avoid errors.
244
deleteTable(DB_NAME, TABLE_NAME);
245         boolean bolHasTransaction = hasTransaction(beanNotSupported);
246         if (bolClientTransaction || bolHasTransaction) {
247             fail("The container did not override the transaction attribute for NOT_SUPPORTED");
248         }
249     }
250
251     /**
252      * Verifies if the deployment descriptor overrides the transaction
253      * attribute mandatory. Also, verifies if the transaction attribute type is really never.
254      * @throws Exception if an error occurs.
255      */

256     public void testNever() throws Exception JavaDoc {
257         //cleans the database to avoid errors.
258
deleteTable(DB_NAME, TABLE_NAME);
259         boolean bolClientTransaction = true;
260         try {
261             usesClientTransaction(beanNever);
262         } catch (EJBException JavaDoc e) {
263             bolClientTransaction = false;
264         }
265         //cleans the database to avoid errors.
266
deleteTable(DB_NAME, TABLE_NAME);
267         boolean bolHasTransaction = hasTransaction(beanNever);
268         if (bolClientTransaction || bolHasTransaction) {
269             fail("The container did not override the transaction attribute for NEVER");
270         }
271     }
272
273     /**
274      * Deletes the table in the database.
275      * @param dbName the database name in the registry.
276      * @param tableName the table name.
277      */

278     protected void deleteTable(final String JavaDoc dbName, final String JavaDoc tableName) {
279         // deletes the table after each test to avoid errors.
280
try {
281             slsbDatabaseManager.deleteTable(dbName, tableName);
282         } catch (SQLException JavaDoc e) {
283             logger.debug("The table delete threw an error during the execution {0}", e);
284         } catch (NamingException JavaDoc e) {
285             logger.debug("The table delete threw an error during the execution {0}", e);
286         }
287     }
288
289 }
290
Popular Tags