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: TestBeanManagedException.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.transaction.beanmanaged;
025:
026: import static org.testng.Assert.fail;
027:
028: import java.sql.SQLException;
029:
030: import javax.ejb.EJBException;
031: import javax.ejb.NoSuchEJBException;
032: import javax.naming.NamingException;
033:
034: import org.ow2.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.ItfBeanManagedException;
035: import org.ow2.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.SFSBBeanManagedException;
036: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
037: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
038: import org.ow2.easybeans.tests.common.exception.RollbackAppRuntimeException;
039: import org.ow2.easybeans.tests.common.helper.EJBHelper;
040: import org.ow2.util.log.Log;
041: import org.ow2.util.log.LogFactory;
042: import org.testng.annotations.BeforeMethod;
043: import org.testng.annotations.Test;
044:
045: /**
046: * Verifies if the bean-managed transaction and the exceptions are correctly
047: * managed by the container.The item couvered in this test is: 14.3.1(table 15).
048: * @reference JSR 220- FINAL RELEASE
049: * @requirement Application Server must be running; the bean
050: * SFSBBeanManagedException and SLSBDatabaseManager. must be
051: * deployed.
052: * @setup gets the reference of SFSBBeanManagedException and
053: * SLSBDatabaseManager, cleans the database.
054: * @author Gisele Pinheiro Souza
055: * @author Eduardo Studzinski Estima de Castro
056: */
057: public class TestBeanManagedException {
058:
059: /**
060: * Bean used during the tests.
061: */
062: private ItfBeanManagedException bean;
063:
064: /**
065: * Bean used to manage the database in the server side.
066: */
067: private ItfDatabaseManager slsbDatabaseManager;
068:
069: /**
070: * The database used during the tests.
071: */
072: private static final String DATABASE = "jdbc_1";
073:
074: /**
075: * Logger.
076: */
077: private static Log logger = LogFactory
078: .getLog(TestBeanManagedException.class);
079:
080: /**
081: * Creates the bean used during the tests and binds the databases used.
082: * @throws Exception if an error during the test startup occurs.
083: */
084: @BeforeMethod
085: public void setup() throws Exception {
086: // creates the bean used to manages the databse in the server site.
087: slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(
088: SLSBDatabaseManager.class, ItfDatabaseManager.class);
089:
090: //deletes the table
091: deleteTable();
092:
093: //creates a bean
094: bean = EJBHelper.getBeanRemoteInstance(
095: SFSBBeanManagedException.class,
096: ItfBeanManagedException.class);
097: bean.startup(DATABASE);
098: }
099:
100: /**
101: * Verifies if the container only throws the application exception when it
102: * occurs in a bean-managed exception.
103: * @throws Exception if an error occurs.
104: * @input -
105: * @output the correct method execution
106: */
107: @Test
108: public void insertTableWithAppException() throws Exception {
109: try {
110: bean.insertTableWithAppException();
111: fail("The container did not throw the application exception");
112: } catch (RollbackAppRuntimeException e) {
113: logger
114: .debug(
115: "The table delete threw an error during the execution {0}",
116: e);
117: }
118: //The transaction must not be rolled back.
119: slsbDatabaseManager.verifyTable(DATABASE,
120: ItfBeanManagedException.TABLE);
121: }
122:
123: /**
124: * Verifies if the container manages a runtime exception in a bean-management transaction. The container must:
125: * <li>Log the exception</li>
126: * <li>Mark the transaction for rollback</li>
127: * <li>Discard the bean</li>
128: * <li>Throw EJBEXception</li>
129: * @throws Exception if an error occurs.
130: */
131: @Test
132: public void insertTableWithRuntimeException() throws Exception {
133: try {
134: bean.insertTableWithRuntimeException();
135: fail("The container did not throw EJBEexception");
136: } catch (EJBException e) {
137: logger
138: .debug(
139: "The table delete threw an error during the execution {0}",
140: e);
141: }
142: //verifies if the instance is discarded
143: try {
144: bean.emptyMethod();
145: fail("The instance was not discarded");
146: } catch (NoSuchEJBException e) {
147: logger
148: .debug(
149: "The bean threw an expected error during the execution {0}",
150: e);
151: }
152:
153: //TODO - verify log
154:
155: //verifies if the transaction was rolled back
156: try {
157: slsbDatabaseManager.verifyTable(DATABASE,
158: ItfBeanManagedException.TABLE);
159: fail("The transaction was not rolled back");
160: } catch (SQLException e) {
161: logger
162: .debug(
163: "The bean threw an expected error during the execution {0}",
164: e);
165: }
166:
167: }
168:
169: /**
170: * Deletes the table used during the test.
171: *
172: */
173: public void deleteTable() {
174: // deletes the table after each test to avoid errors.
175: try {
176: // verifies if the table was created
177: slsbDatabaseManager.deleteTable(DATABASE,
178: ItfBeanManagedException.TABLE);
179: } catch (SQLException e) {
180: logger
181: .debug(
182: "The table delete threw an error during the execution {0}",
183: e);
184: } catch (NamingException e) {
185: logger
186: .debug(
187: "The table delete threw an error during the execution {0}",
188: e);
189: }
190: }
191: }
|