001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: TestCMTDefinition.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.deploymentdesc;
025:
026: import static org.testng.Assert.assertTrue;
027: import static org.testng.Assert.fail;
028:
029: import java.sql.SQLException;
030:
031: import javax.ejb.EJBException;
032: import javax.naming.NamingException;
033: import javax.transaction.UserTransaction;
034:
035: import org.ow2.easybeans.tests.common.ejbs.stateful.containermanaged.xmldescriptor.ItfCMTDeployDesc;
036: import org.ow2.easybeans.tests.common.ejbs.stateful.containermanaged.xmldescriptor.SFSBCMTDeployDesc;
037: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
038: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
039: import org.ow2.easybeans.tests.common.helper.EJBHelper;
040: import org.ow2.easybeans.tests.common.helper.TransactionHelper;
041: import org.ow2.util.log.Log;
042: import org.ow2.util.log.LogFactory;
043: import org.testng.annotations.BeforeMethod;
044: import org.testng.annotations.Test;
045:
046: /**
047: * Verifies if the container can deploy an bean with the transaction attributes
048: * defined in the deployment description. Item 13.3.7.2.
049: * @reference JSR 220 - FINAL RELEASE
050: * @requirement the bean SFSBCMTDeployDesc and the SLSBDatabaseManager must be
051: * deployed to make the tests, and, the deployment descriptors must
052: * be used too.
053: * @setup gets an instance of the SFSBCMTDeployDesc and the SLSBDatabaseManager.
054: * @author Gisele Pinheiro Souza
055: * @author Eduardo Studzinski Estima de Castro
056: */
057: public class TestCMTDefinition {
058:
059: /**
060: * The database used during the tests.
061: */
062: public static final String DB_NAME = "jdbc_1";
063:
064: /**
065: * The table name.
066: */
067: public static final String TABLE_NAME = "test";
068:
069: /**
070: * Bean used to verify the local access.
071: */
072: private ItfCMTDeployDesc bean;
073:
074: /**
075: * Logger.
076: */
077: private static Log logger = LogFactory
078: .getLog(TestCMTDefinition.class);
079:
080: /**
081: * Bean used to manage the database in the server side.
082: */
083: private ItfDatabaseManager slsbDatabaseManager;
084:
085: /**
086: * Creates the stateful bean used during the tests.
087: * @throws Exception if an error occurs during the lookup.
088: */
089: @BeforeMethod
090: public void setup() throws Exception {
091: bean = EJBHelper.getBeanRemoteInstance(SFSBCMTDeployDesc.class,
092: ItfCMTDeployDesc.class);
093: // creates the bean used to manages the databse in the server site.
094: slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(
095: SLSBDatabaseManager.class, ItfDatabaseManager.class);
096: deleteTable(DB_NAME, TABLE_NAME);
097: }
098:
099: /**
100: * Verifies if the definition of trasaction attribute for all methods work.
101: * The deployment descriptor is:<br> <container-transaction> <br> <method>
102: * <br> <ejb-name>SFSBCMTDeployDescRemote</ejb-name> <br> <method-name>*</method-name><br>
103: * </method> <br> <trans-attribute>Required</trans-attribute> <br>
104: * </container-transaction><br>
105: * @input -
106: * @output the correct method execution.
107: * @throws Exception if an error occurs.
108: */
109: @Test
110: public void verifyAllDefinition() throws Exception {
111: // the transaction attribute defined for all methods is required, so the
112: // method must work with or without a client transaction.
113:
114: // the method is working with transaction...
115: UserTransaction utx = TransactionHelper
116: .getInternalUserTransaction();
117: utx.begin();
118: bean.insertTable01(DB_NAME, TABLE_NAME);
119: utx.commit();
120:
121: // cleans the db to avoid error
122: deleteTable(DB_NAME, TABLE_NAME);
123:
124: // the method is working without transaction
125: bean.insertTable01(DB_NAME, TABLE_NAME);
126: }
127:
128: /**
129: * Verifies if the definition of the transaction for all method with the
130: * same name works, as well as verifies the override of the wildcard.
131: * @input -
132: * @output the correct method execution.
133: */
134: @Test
135: public void verifyMethodWithoutParameterDefinition() {
136: // The transaction attribute is defined for all class methods that has
137: // the name insertTable02. The parameters are not defined, so all
138: // methods with the name insertTable02 have the same transaction
139: // attribute.
140:
141: // The transaction attribute for the method with 2 parameters. The
142: // deployment descriptor defines this method as mandatory, so an
143: // exception must be throw when the method is called without a
144: // transactional context.
145: try {
146: bean.insertTable02(DB_NAME, TABLE_NAME);
147: fail("A method with transaction attribute mandatory is not throwing an EJBException"
148: + " when it is called without transaction context");
149: } catch (Exception e) {
150: assertTrue(
151: e instanceof EJBException,
152: "A method with transaction attribute "
153: + "mandatory is not throwing an EJBException when it is called without transaction context");
154: }
155: // cleans the db to avoid error
156: deleteTable(DB_NAME, TABLE_NAME);
157:
158: // The transaction attribute for the method with 3 parameters. The
159: // deployment descriptor defines this method as mandatory, so an
160: // exception must be throw when the method is called withou a
161: // transactional context.
162:
163: try {
164: bean.insertTable02(DB_NAME, TABLE_NAME, 1);
165: fail("A method with transaction attribute mandatory is not throwing an "
166: + "EJBException when it is called without transaction context");
167: } catch (Exception e) {
168: assertTrue(
169: e instanceof EJBException,
170: "A method with transaction attribute mandatory is not throwing an "
171: + "EJBException when it is called without transaction context");
172: }
173: }
174:
175: /**
176: * Verifies if the definition of the transaction for a specific method, as
177: * well as verifies the override of the wildcard.
178: * @input -
179: * @output the correct method execution.
180: * @throws Exception if an error occurs.
181: */
182: @Test
183: public void verifyMethodWithParameterDefinition() throws Exception {
184: // The transaction attribute is defined for a method with the parameters
185: // dbName and tableName, so only this method will have the transaction
186: // attribute never. The other method with the same name, but with
187: // diferent attributes, has the general tarsnaction attribute(REQUIRED).
188:
189: // The transaction attribute is never, so when the method is called with
190: // a transactional context, an exception must be throw.
191: UserTransaction utx = TransactionHelper
192: .getInternalUserTransaction();
193: try {
194: utx.begin();
195: bean.insertTable03(DB_NAME, TABLE_NAME);
196: fail("A method with transaction attribute never is not throwing an "
197: + "EJBException when it is called with transaction context");
198: } catch (Exception e) {
199: assertTrue(
200: e instanceof EJBException,
201: "A method with transaction attribute never is not throwing an "
202: + "EJBException when it is called with transaction context");
203: } finally {
204: utx.rollback();
205: }
206:
207: // cleans the db to avoid error
208: deleteTable(DB_NAME, TABLE_NAME);
209:
210: // the transaction attribute defined for all methods is required, so the
211: // method must work with or without a client transaction.
212:
213: // the method is working with transaction...
214: utx.begin();
215: bean.insertTable03(DB_NAME, TABLE_NAME, 1);
216: utx.commit();
217:
218: // cleans the db to avoid error
219: deleteTable(DB_NAME, TABLE_NAME);
220:
221: // the method is working without transaction
222: bean.insertTable03(DB_NAME, TABLE_NAME, 1);
223: }
224:
225: /**
226: * Deletes the table in the database.
227: * @param dbName the database name in the registry.
228: * @param tableName the table name.
229: */
230: protected void deleteTable(final String dbName,
231: final String tableName) {
232: // deletes the table after each test to avoid errors.
233: try {
234: slsbDatabaseManager.deleteTable(dbName, tableName);
235: } catch (SQLException e) {
236: logger
237: .debug(
238: "The table delete threw an error during the execution {0}",
239: e);
240: } catch (NamingException e) {
241: logger
242: .debug(
243: "The table delete threw an error during the execution {0}",
244: e);
245: }
246: }
247: }
|