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: SFSBTransAttributeTester.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.stateful.containermanaged.xmldescriptor;
025:
026: import static org.testng.Assert.fail;
027:
028: import java.sql.SQLException;
029:
030: import javax.ejb.EJB;
031: import javax.ejb.EJBException;
032: import javax.ejb.Remote;
033: import javax.ejb.Stateful;
034: import javax.naming.NamingException;
035: import javax.transaction.UserTransaction;
036:
037: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
038: import org.ow2.easybeans.tests.common.helper.TransactionHelper;
039: import org.ow2.util.log.Log;
040: import org.ow2.util.log.LogFactory;
041:
042: /**
043: * Verifies if the container sets all types of transaction attributes that are
044: * defined by the deployment descriptor.
045: * @author Gisele Pinheiro Souza
046: * @author Eduardo Studzinski Estima de Castro
047: */
048: @Stateful
049: @Remote(ItfTransAttributeTester.class)
050: public class SFSBTransAttributeTester implements
051: ItfTransAttributeTester {
052:
053: /**
054: * Bean that has the transaction attribute mandatory defined in the
055: * deployment descriptor.
056: */
057: @EJB(beanName="SFSBTransAttributeDefMandatory")
058: private ItfTransAttributeDefinition beanMandatory;
059:
060: /**
061: * Bean that has the transaction attribute required defined in the
062: * deployment descriptor.
063: */
064: @EJB(beanName="SFSBTransAttributeDefRequired")
065: private ItfTransAttributeDefinition beanRequired;
066:
067: /**
068: * Bean that has the transaction attribute requires_new defined in the
069: * deployment descriptor.
070: */
071: @EJB(beanName="SFSBTransAttributeDefRequiresNew")
072: private ItfTransAttributeDefinition beanRequiresNew;
073:
074: /**
075: * Bean that has the transaction attribute supports defined in the
076: * deployment descriptor.
077: */
078: @EJB(beanName="SFSBTransAttributeDefSupports")
079: private ItfTransAttributeDefinition beanSupports;
080:
081: /**
082: * Bean that has the transaction attribute not supported defined in the
083: * deployment descriptor.
084: */
085: @EJB(beanName="SFSBTransAttributeDefNotSupported")
086: private ItfTransAttributeDefinition beanNotSupported;
087:
088: /**
089: * Bean that has the transaction attribute never defined in the
090: * deployment descriptor.
091: */
092: @EJB(beanName="SFSBTransAttributeDefNever")
093: private ItfTransAttributeDefinition beanNever;
094:
095: /**
096: * Used to manipulate the data in the database.
097: */
098: @EJB
099: private ItfDatabaseManager slsbDatabaseManager;
100:
101: /**
102: * Logger.
103: */
104: private static Log logger = LogFactory
105: .getLog(SFSBTransAttributeTester.class);
106:
107: /**
108: * Verifies if the client use the same transaction that the bean. Creates a
109: * transaction, call a bean method that acess the JDBC and after makes a
110: * rollback. If the bean uses the same transaction, the data in the database
111: * must be rolled back.
112: * @param bean the bean that is verified.
113: * @return true if the bean uses the same transaction that the client, false otherwise.
114: * @throws Exception if an error occurs.
115: */
116: private boolean usesClientTransaction(
117: final ItfTransAttributeDefinition bean) throws Exception {
118: UserTransaction utx = TransactionHelper
119: .getInternalUserTransaction();
120: try {
121: utx.begin();
122: bean.insertTableCorrectMandatory(DB_NAME, TABLE_NAME);
123: } finally {
124: utx.rollback();
125: }
126: try {
127: slsbDatabaseManager.verifyTable(DB_NAME, TABLE_NAME);
128: return false;
129: } catch (SQLException e) {
130: return true;
131: }
132: }
133:
134: /**
135: * Verifies if the bean has a transaction context.
136: * @param bean the bean that is verified.
137: * @return true if the bean uses a transaction, false otherwise.
138: * @throws Exception if an error occurs.
139: */
140: private boolean hasTransaction(
141: final ItfTransAttributeDefinition bean) throws Exception {
142: bean.insertTableErrorMandatory(DB_NAME, TABLE_NAME);
143: try {
144: slsbDatabaseManager.verifyTable(DB_NAME, TABLE_NAME);
145: return false;
146: } catch (SQLException e) {
147: return true;
148: }
149: }
150:
151: /**
152: * Verifies if the deployment descriptor overrides the default transaction
153: * attribute value. Also, verifies if the transaction attribute type is really mandatory.
154: * @throws Exception if an error occurs.
155: */
156: public void testMandatory() throws Exception {
157: //cleans the database to avoid errors.
158: deleteTable(DB_NAME, TABLE_NAME);
159:
160: UserTransaction utx = TransactionHelper.getUserTransaction();
161: try {
162: utx.begin();
163: beanMandatory.insertTableCorrect(DB_NAME, TABLE_NAME);
164: } finally {
165: utx.rollback();
166: }
167: try {
168: slsbDatabaseManager.verifyTable(DB_NAME, TABLE_NAME);
169: fail("The deployment descriptor did not override the "
170: + "annotation. The bean did not use the client transaction in mandatory mode.");
171: } catch (SQLException e) {
172: logger.debug("The bean thron an expected exception{0}", e);
173: }
174:
175: //cleans the database to avoid errors.
176: deleteTable(DB_NAME, TABLE_NAME);
177:
178: try {
179: beanMandatory.insertTableCorrect(DB_NAME, TABLE_NAME);
180: } catch (EJBException e) {
181: logger.debug("The bean thron an expected exception{0}", e);
182: fail("The deployment descriptor did not override the annotation. "
183: + "The bean was called withou a transaction context in a mandatory mode and there was not exception.");
184: }
185: }
186:
187: /**
188: * Verifies if the deployment descriptor overrides the transaction
189: * attribute mandatory. Also, verifies if the transaction attribute type is really required.
190: * @throws Exception if an error occurs.
191: */
192: public void testRequired() throws Exception {
193: //cleans the database to avoid errors.
194: deleteTable(DB_NAME, TABLE_NAME);
195: boolean bolClientTransaction = usesClientTransaction(beanRequired);
196: //cleans the database to avoid errors.
197: deleteTable(DB_NAME, TABLE_NAME);
198: boolean bolHasTransaction = hasTransaction(beanRequired);
199: if (!(bolClientTransaction && bolHasTransaction)) {
200: fail("The container did not override the transaction attribute for REQUIRED");
201: }
202: }
203:
204: /**
205: * Verifies if the deployment descriptor overrides the transaction
206: * attribute mandatory. Also, verifies if the transaction attribute type is really requireds new.
207: * @throws Exception if an error occurs.
208: */
209: public void testRequiresNew() throws Exception {
210: //cleans the database to avoid errors.
211: deleteTable(DB_NAME, TABLE_NAME);
212: boolean bolClientTransaction = usesClientTransaction(beanRequiresNew);
213: //cleans the database to avoid errors.
214: deleteTable(DB_NAME, TABLE_NAME);
215: boolean bolHasTransaction = hasTransaction(beanRequiresNew);
216: if (!(!bolClientTransaction && bolHasTransaction)) {
217: fail("The container did not override the transaction attribute for REQUIRES_NEW");
218: }
219: }
220:
221: /**
222: * Verifies if the deployment descriptor overrides the transaction
223: * attribute mandatory. Also, verifies if the transaction attribute type is really supports.
224: * @throws Exception if an error occurs.
225: */
226: public void testSupports() throws Exception {
227: //cleans the database to avoid errors.
228: deleteTable(DB_NAME, TABLE_NAME);
229: boolean bolClientTransaction = usesClientTransaction(beanSupports);
230: //cleans the database to avoid errors.
231: deleteTable(DB_NAME, TABLE_NAME);
232: boolean bolHasTransaction = hasTransaction(beanSupports);
233: if (!(bolClientTransaction && !bolHasTransaction)) {
234: fail("The container did not override the transaction attribute for SUPPORTS");
235: }
236: }
237:
238: /**
239: * Verifies if the deployment descriptor overrides the transaction
240: * attribute mandatory. Also, verifies if the transaction attribute type is really not supported.
241: * @throws Exception if an error occurs.
242: */
243: public void testNotSupported() throws Exception {
244: //cleans the database to avoid errors.
245: deleteTable(DB_NAME, TABLE_NAME);
246: boolean bolClientTransaction = usesClientTransaction(beanNotSupported);
247: //cleans the database to avoid errors.
248: deleteTable(DB_NAME, TABLE_NAME);
249: boolean bolHasTransaction = hasTransaction(beanNotSupported);
250: if (bolClientTransaction || bolHasTransaction) {
251: fail("The container did not override the transaction attribute for NOT_SUPPORTED");
252: }
253: }
254:
255: /**
256: * Verifies if the deployment descriptor overrides the transaction
257: * attribute mandatory. Also, verifies if the transaction attribute type is really never.
258: * @throws Exception if an error occurs.
259: */
260: public void testNever() throws Exception {
261: //cleans the database to avoid errors.
262: deleteTable(DB_NAME, TABLE_NAME);
263: boolean bolClientTransaction = true;
264: try {
265: usesClientTransaction(beanNever);
266: } catch (EJBException e) {
267: bolClientTransaction = false;
268: }
269: //cleans the database to avoid errors.
270: deleteTable(DB_NAME, TABLE_NAME);
271: boolean bolHasTransaction = hasTransaction(beanNever);
272: if (bolClientTransaction || bolHasTransaction) {
273: fail("The container did not override the transaction attribute for NEVER");
274: }
275: }
276:
277: /**
278: * Deletes the table in the database.
279: * @param dbName the database name in the registry.
280: * @param tableName the table name.
281: */
282: protected void deleteTable(final String dbName,
283: final String tableName) {
284: // deletes the table after each test to avoid errors.
285: try {
286: slsbDatabaseManager.deleteTable(dbName, tableName);
287: } catch (SQLException e) {
288: logger
289: .debug(
290: "The table delete threw an error during the execution {0}",
291: e);
292: } catch (NamingException e) {
293: logger
294: .debug(
295: "The table delete threw an error during the execution {0}",
296: e);
297: }
298: }
299:
300: }
|