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: TestAppException.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.deploymentdesc;
025:
026: import static org.testng.Assert.fail;
027:
028: import java.sql.SQLException;
029:
030: import javax.naming.NamingException;
031: import javax.transaction.UserTransaction;
032:
033: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
034: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
035: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.exception.ItfExceptionXML;
036: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.exception.SLSBExceptionXML;
037: import org.ow2.easybeans.tests.common.exception.CustomException00;
038: import org.ow2.easybeans.tests.common.exception.CustomException01;
039: import org.ow2.easybeans.tests.common.exception.IllegalException;
040: import org.ow2.easybeans.tests.common.exception.RollbackApplicationException;
041: import org.ow2.easybeans.tests.common.helper.EJBHelper;
042: import org.ow2.easybeans.tests.common.helper.TransactionHelper;
043: import org.ow2.util.log.Log;
044: import org.ow2.util.log.LogFactory;
045: import org.testng.annotations.BeforeMethod;
046: import org.testng.annotations.Test;
047:
048: /**
049: * Verifies if the container can set the application exception defined by
050: * deployment descriptor.
051: * @reference JSR 220 - FINAL RELEASE
052: * @requirement the bean SLSBExceptionXML and SLSBDatabaseManager must be
053: * deployed to make the tests, and, the deployment descriptor in
054: * the SLSBExceptionXML package must be used too.
055: * @setup gets an instance of the SLSBExceptionXML and SLSBDatabaseManager.
056: * Also, it cleans the database(it drops the table)
057: * @author Gisele Pinheiro Souza
058: * @author Eduardo Studzinski Estima de Castro
059: */
060: public class TestAppException {
061:
062: /**
063: * The database used during the tests.
064: */
065: public static final String DB_NAME = "jdbc_1";
066:
067: /**
068: * Bean used to verify the local access.
069: */
070: private ItfExceptionXML bean;
071:
072: /**
073: * Logger.
074: */
075: private static Log logger = LogFactory
076: .getLog(TestAppException.class);
077:
078: /**
079: * Bean used to manage the database in the server side.
080: */
081: private ItfDatabaseManager slsbDatabaseManager;
082:
083: /**
084: * Creates the stateful bean used during the tests.
085: * @throws Exception if an error occurs during the lookup.
086: */
087: @BeforeMethod
088: public void setup() throws Exception {
089: bean = EJBHelper.getBeanRemoteInstance(SLSBExceptionXML.class,
090: ItfExceptionXML.class);
091: // creates the bean used to manages the databse in the server site.
092: slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(
093: SLSBDatabaseManager.class, ItfDatabaseManager.class);
094: deleteTable(DB_NAME);
095: }
096:
097: /**
098: * Verifies if the definition if an application exception(with rollback =
099: * false) in the deployment descriptor works properly. When an application
100: * exception with rollback = false occurs, the container must not mark the
101: * transaction for rollback. Also, the container must re-throw the
102: * exception.
103: * @input -
104: * @output the correct method execution without exceptions.
105: * @throws Exception if some error occurs during the tests.
106: */
107: @Test
108: public void testAppExceptionWithoutRollback() throws Exception {
109: //verifies if the application exception is re-throw
110: //the exception is not a checked exception
111: UserTransaction utx = TransactionHelper
112: .getInternalUserTransaction();
113: try {
114: utx.begin();
115: bean.createTableWithAppException(DB_NAME);
116: fail("The container did not re-throw the application exception.");
117: } catch (CustomException01 e) {
118: logger.debug(
119: "The container threw an expected exception {0}", e);
120: } finally {
121: // the container must not mark the transaction for rollback, so the
122: // commit should work.
123: utx.commit();
124: }
125: //verifies if the commit worked
126: verifyTable(DB_NAME);
127: }
128:
129: /**
130: * Verifies if the definition if an application exception overrided in a
131: * deployment descriptor works properly. The annotation defines the
132: * rollback as true, however the deployment descriptor defines
133: * the transaction attribute as false. The deployment descriptor must
134: * override the annotation value for the rollback. When an application
135: * exception with rollback = false occurs, the container must not mark the
136: * transaction for rollback. Also, the container must re-throw the
137: * exception.
138: * @input -
139: * @output the correct method execution without exceptions.
140: * @throws Exception if some error occurs during the tests.
141: */
142: @Test
143: public void testAppExceptionWithRollbackOverride() throws Exception {
144: //verifies if the application exception is re-throw
145: //the exception is not a checked exception
146: UserTransaction utx = TransactionHelper
147: .getInternalUserTransaction();
148: try {
149: utx.begin();
150: bean.createTableWithAppExceptionOverride(DB_NAME);
151: fail("The container did not re-throw the application exception.");
152: } catch (RollbackApplicationException e) {
153: logger.debug(
154: "The container threw an expected exception {0}", e);
155: } finally {
156: // the container must not mark the transaction for rollback, so the
157: // commit should work.
158: utx.commit();
159: }
160: //verifies if the commit worked
161: verifyTable(DB_NAME);
162: }
163:
164: /**
165: * Verifies if the definition if an application exception(with rollback =
166: * true) in the deployment descriptor works properly. When an application
167: * exception with rollback = true occurs, the container must mark the
168: * transaction for rollback. Also, the container must re-throw the
169: * exception.
170: * @input -
171: * @output the correct method execution without exceptions.
172: * @throws Exception if some error occurs during the tests.
173: */
174: @Test
175: public void testAppExceptionWithRollback() throws Exception {
176: //verifies if the application exception is re-throw
177: //the exception is a checked exception
178: UserTransaction utx = TransactionHelper
179: .getInternalUserTransaction();
180: try {
181: utx.begin();
182: bean.createTableWithAppExceptionAndRollback(DB_NAME);
183: fail("The container did not re-throw the application exception.");
184: } catch (CustomException00 e) {
185: logger.debug(
186: "The container threw an expected exception {0}", e);
187: } finally {
188: // the container must mark the transaction for rollback, so the
189: // commit should not work.
190: try {
191: utx.commit();
192: fail("The container did not mark the transaction for rollback");
193: } catch (Exception e) {
194: logger
195: .debug(
196: "The container threw an expected exception {0}",
197: e);
198: }
199: }
200: }
201:
202: /**
203: * Verifies if the definition if an application exception(with the dafault rollback
204: * value) in the deployment descriptor works properly. When an application
205: * exception with rollback defult value(false) occurs, the container must not mark the
206: * transaction for rollback. Also, the container must re-throw the
207: * exception.
208: * @input -
209: * @output the correct method execution without exceptions.
210: * @throws Exception if some error occurs during the tests.
211: */
212: @Test
213: public void testAppExceptionWithRollbackDefault() throws Exception {
214: //verifies if the application exception is re-throw
215: //the exception is not a checked exception
216: UserTransaction utx = TransactionHelper
217: .getInternalUserTransaction();
218: try {
219: utx.begin();
220: bean.createTableWithAppExceptionDefault(DB_NAME);
221: fail("The container did not re-throw the application exception.");
222: } catch (IllegalException e) {
223: logger.debug(
224: "The container threw an expected exception {0}", e);
225: } finally {
226: // the container must not mark the transaction for rollback, so the
227: // commit should work.
228: utx.commit();
229: }
230: //verifies if the commit worked
231: verifyTable(DB_NAME);
232: }
233:
234: /**
235: * Verifies if the table was created in the database.
236: * @param database the database name.
237: * @throws NamingException if a lookup error occurs.
238: * @throws SQLException if a database error occurs.
239: */
240: protected void verifyTable(final String database)
241: throws NamingException, SQLException {
242: slsbDatabaseManager
243: .verifyTable(database, ItfExceptionXML.TABLE);
244: }
245:
246: /**
247: * Deletes the table in the database.
248: * @param dbName the database name in the registry.
249: */
250: protected void deleteTable(final String dbName) {
251: // deletes the table after each test to avoid errors.
252: try {
253: slsbDatabaseManager.deleteTable(dbName,
254: ItfExceptionXML.TABLE);
255: } catch (SQLException e) {
256: logger
257: .debug(
258: "The table delete threw an error during the execution {0}",
259: e);
260: } catch (NamingException e) {
261: logger
262: .debug(
263: "The table delete threw an error during the execution {0}",
264: e);
265: }
266: }
267:
268: }
|