KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > deploymentdesc > TestCMTDefinition


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: TestCMTDefinition.java 977 2006-07-28 13:18:26Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.deploymentdesc;
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.ejb.EJBException JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.transaction.UserTransaction JavaDoc;
35
36 import org.objectweb.easybeans.log.JLog;
37 import org.objectweb.easybeans.log.JLogFactory;
38 import org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.xmldescriptor.ItfCMTDeployDesc;
39 import org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.xmldescriptor.SFSBCMTDeployDesc;
40 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
41 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
42 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
43 import org.objectweb.easybeans.tests.common.helper.TransactionHelper;
44 import org.testng.annotations.BeforeMethod;
45 import org.testng.annotations.Test;
46
47 /**
48  * Verifies if the container can deploy an bean with the transaction attributes
49  * defined in the deployment description. Item 13.3.7.2.
50  * @reference JSR 220 - FINAL RELEASE
51  * @requirement the bean SFSBCMTDeployDesc and the SLSBDatabaseManager must be
52  * deployed to make the tests, and, the deployment descriptors must
53  * be used too.
54  * @setup gets an instance of the SFSBCMTDeployDesc and the SLSBDatabaseManager.
55  * @author Gisele Pinheiro Souza
56  * @author Eduardo Studzinski Estima de Castro
57  */

58 public class TestCMTDefinition {
59
60     /**
61      * The database used during the tests.
62      */

63     public static final String JavaDoc DB_NAME = "jdbc_1";
64
65     /**
66      * The table name.
67      */

68     public static final String JavaDoc TABLE_NAME = "test";
69
70     /**
71      * Bean used to verify the local access.
72      */

73     private ItfCMTDeployDesc bean;
74
75     /**
76      * Logger.
77      */

78     private static JLog logger = JLogFactory.getLog(TestCMTDefinition.class);
79
80     /**
81      * Bean used to manage the database in the server side.
82      */

83     private ItfDatabaseManager slsbDatabaseManager;
84
85     /**
86      * Creates the stateful bean used during the tests.
87      * @throws Exception if an error occurs during the lookup.
88      */

89     @BeforeMethod
90     public void setup() throws Exception JavaDoc {
91         bean = EJBHelper.getBeanRemoteInstance(SFSBCMTDeployDesc.class, ItfCMTDeployDesc.class);
92         // creates the bean used to manages the databse in the server site.
93
slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class, ItfDatabaseManager.class);
94         deleteTable(DB_NAME, TABLE_NAME);
95     }
96
97     /**
98      * Verifies if the definition of trasaction attribute for all methods work.
99      * The deployment descriptor is:<br> <container-transaction> <br> <method>
100      * <br> <ejb-name>SFSBCMTDeployDescRemote</ejb-name> <br> <method-name>*</method-name><br>
101      * </method> <br> <trans-attribute>Required</trans-attribute> <br>
102      * </container-transaction><br>
103      * @input -
104      * @output the correct method execution.
105      * @throws Exception if an error occurs.
106      */

107     @Test
108     public void verifyAllDefinition() throws Exception JavaDoc {
109         // the transaction attribute defined for all methods is required, so the
110
// method must work with or without a client transaction.
111

112         // the method is working with transaction...
113
UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
114         utx.begin();
115         bean.insertTable01(DB_NAME, TABLE_NAME);
116         utx.commit();
117
118         // cleans the db to avoid error
119
deleteTable(DB_NAME, TABLE_NAME);
120
121         // the method is working without transaction
122
bean.insertTable01(DB_NAME, TABLE_NAME);
123     }
124
125     /**
126      * Verifies if the definition of the transaction for all method with the
127      * same name works, as well as verifies the override of the wildcard.
128      * @input -
129      * @output the correct method execution.
130      */

131     @Test
132     public void verifyMethodWithoutParameterDefinition() {
133         // The transaction attribute is defined for all class methods that has
134
// the name insertTable02. The parameters are not defined, so all
135
// methods with the name insertTable02 have the same transaction
136
// attribute.
137

138         // The transaction attribute for the method with 2 parameters. The
139
// deployment descriptor defines this method as mandatory, so an
140
// exception must be throw when the method is called without a
141
// transactional context.
142
try {
143             bean.insertTable02(DB_NAME, TABLE_NAME);
144             fail("A method with transaction attribute mandatory is not throwing an EJBException"
145                     + " when it is called without transaction context");
146         } catch (Exception JavaDoc e) {
147             assertTrue(e instanceof EJBException JavaDoc, "A method with transaction attribute "
148                     + "mandatory is not throwing an EJBException when it is called without transaction context");
149         }
150         // cleans the db to avoid error
151
deleteTable(DB_NAME, TABLE_NAME);
152
153         // The transaction attribute for the method with 3 parameters. The
154
// deployment descriptor defines this method as mandatory, so an
155
// exception must be throw when the method is called withou a
156
// transactional context.
157

158         try {
159             bean.insertTable02(DB_NAME, TABLE_NAME, 1);
160             fail("A method with transaction attribute mandatory is not throwing an "
161                     + "EJBException when it is called without transaction context");
162         } catch (Exception JavaDoc e) {
163             assertTrue(
164                     e instanceof EJBException JavaDoc,
165                     "A method with transaction attribute mandatory is not throwing an "
166                     + "EJBException when it is called without transaction context");
167         }
168     }
169
170     /**
171      * Verifies if the definition of the transaction for a specific method, as
172      * well as verifies the override of the wildcard.
173      * @input -
174      * @output the correct method execution.
175      * @throws Exception if an error occurs.
176      */

177     @Test
178     public void verifyMethodWithParameterDefinition() throws Exception JavaDoc {
179         // The transaction attribute is defined for a method with the parameters
180
// dbName and tableName, so only this method will have the transaction
181
// attribute never. The other method with the same name, but with
182
// diferent attributes, has the general tarsnaction attribute(REQUIRED).
183

184         // The transaction attribute is never, so when the method is called with
185
// a transactional context, an exception must be throw.
186
UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
187         try {
188             utx.begin();
189             bean.insertTable03(DB_NAME, TABLE_NAME);
190             fail("A method with transaction attribute never is not throwing an "
191                     + "EJBException when it is called with transaction context");
192         } catch (Exception JavaDoc e) {
193             assertTrue(e instanceof EJBException JavaDoc,
194                     "A method with transaction attribute never is not throwing an "
195                     + "EJBException when it is called with transaction context");
196         } finally {
197             utx.rollback();
198         }
199
200         // cleans the db to avoid error
201
deleteTable(DB_NAME, TABLE_NAME);
202
203         // the transaction attribute defined for all methods is required, so the
204
// method must work with or without a client transaction.
205

206         // the method is working with transaction...
207
utx.begin();
208         bean.insertTable03(DB_NAME, TABLE_NAME, 1);
209         utx.commit();
210
211         // cleans the db to avoid error
212
deleteTable(DB_NAME, TABLE_NAME);
213
214         // the method is working without transaction
215
bean.insertTable03(DB_NAME, TABLE_NAME, 1);
216     }
217
218     /**
219      * Deletes the table in the database.
220      * @param dbName the database name in the registry.
221      * @param tableName the table name.
222      */

223     protected void deleteTable(final String JavaDoc dbName, final String JavaDoc tableName) {
224         // deletes the table after each test to avoid errors.
225
try {
226             slsbDatabaseManager.deleteTable(dbName, tableName);
227         } catch (SQLException JavaDoc e) {
228             logger.debug("The table delete threw an error during the execution {0}", e);
229         } catch (NamingException JavaDoc e) {
230             logger.debug("The table delete threw an error during the execution {0}", e);
231         }
232     }
233 }
234
Popular Tags