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: TestRequiredNewException.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.transaction.containermanaged.base;
025:
026: import static org.testng.Assert.fail;
027:
028: import java.sql.SQLException;
029:
030: import javax.ejb.EJBException;
031: import javax.transaction.UserTransaction;
032:
033: import org.ow2.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
034: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
035: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
036: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.transaction.ItfTransactionMisc00;
037: import org.ow2.easybeans.tests.common.helper.EJBHelper;
038: import org.ow2.util.log.Log;
039: import org.ow2.util.log.LogFactory;
040:
041: /**
042: * Verifies if the container-managed transaction in the session bean is
043: * following the JSR 220. The container must handle the different types of
044: * exception in the transaction context. The items covered in this test are:
045: * 13.3
046: * @reference JSR 220-PROPOSED FINAL
047: * @requirement Application Server must be running; the bean .
048: * SFSBContainerTransactionRequiredNewApp,
049: * SFSBContainerTransactionRequiredNewRollback and
050: * SFSBContainerTransactionRequiredNewRuntime must be deployed for
051: * testing stateful session bean. And, the bean
052: * SLSBContainerTransactionRequiredNewApp,
053: * SLSBContainerTransactionRequiredNewRollback and
054: * SLSBContainerTransactionRequiredNewRuntime must be deployed for
055: * testing stateless session bean.
056: * @setup gets the reference of the bean and binds the databases specified in
057: * the EmbeddedTest.
058: * @author Gisele Pinheiro Souza
059: * @author Eduardo Studzinski Estima de Castro
060: */
061: public abstract class TestRequiredNewException extends
062: TestContainerTransactionException {
063:
064: /**
065: * Logger.
066: */
067: private static Log logger = LogFactory
068: .getLog(TestRequiredNewException.class);
069:
070: /**
071: * Verifies if the container does not use the same transaction when the
072: * other bean has the transaction attribute NOT_SUPPORTED.The bean method throws a runtime exception.
073: * @input -
074: * @output the method execution without error
075: * @throws Exception if an error during the tests occurs.
076: */
077: @Override
078: public void testCallOtherBeanNotSup() throws Exception {
079: super .testCallOtherBeanNotSup();
080: //verifies if the transaction in the bean was rolled back.
081: try {
082: ExceptionHandleUtil.verifyTable(DATABASE_1,
083: ItfContainerTransaction.TABLE);
084: fail("The container did not make a rollback in the transaction.");
085: } catch (SQLException e) {
086: logger.debug("The test threw an expected exception {0}", e);
087: }
088: // verifies if the table in the second bean was destroyed. This table
089: // must not to be destroyed because the other bean has the transaction
090: // attribute NOT_SUPPORTED.
091: try {
092: ExceptionHandleUtil.verifyTable(DATABASE_2,
093: ItfTransactionMisc00.TABLE);
094: } catch (SQLException e) {
095: fail("The container made a rollback in the transaction.");
096: }
097: }
098:
099: /**
100: * Verifies if the container uses the same transaction when the other bean
101: * has the transaction attribute REQUIRED.The method called
102: * throws a runtime exception.
103: * @input -
104: * @output the method execution without error
105: * @throws Exception if an error during the tests occurs.
106: */
107: @Override
108: public void testCallOtherBeanReq() throws Exception {
109: super .testCallOtherBeanReq();
110: //verifies if the transaction in the bean was rolled back.
111: try {
112: ExceptionHandleUtil.verifyTable(DATABASE_1,
113: ItfContainerTransaction.TABLE);
114: fail("The container did not make a rollback in the transaction.");
115: } catch (SQLException e) {
116: logger.debug("The test threw an expected exception {0}", e);
117: }
118: // verifies if the table in the second bean was destroyed. This table
119: // must to be destroyed because the two beans are using the same
120: // transaction.
121: try {
122: ExceptionHandleUtil.verifyTable(DATABASE_2,
123: ItfTransactionMisc00.TABLE);
124: fail("The container did not make a rollback in the transaction.");
125: } catch (SQLException e) {
126: logger.debug("The test threw an expected exception {0}", e);
127: }
128: }
129:
130: /**
131: * Verifies if the container uses the same transaction that the client. The
132: * @input -
133: * @output the table created by the client must to be correctly commited.
134: * @throws Exception if an error during the tests occurs.
135: */
136: public void testUsingClientTrans() throws Exception {
137: // gets the transaction
138: UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
139: // starts the transaction
140: utx.begin();
141: // creates a table in the second database
142: ItfDatabaseManager slsbDatabaseManager = EJBHelper
143: .getBeanRemoteInstance(SLSBDatabaseManager.class,
144: ItfDatabaseManager.class);
145: slsbDatabaseManager.insertTable(DATABASE_2,
146: ItfContainerTransaction.TABLE);
147: /*
148: * Calls a bean method that forces an error and makes a roll back in the
149: * method. The bean is using a required new attribute, so the bean
150: * cannot use the same transaction that the client.
151: */
152: try {
153: getRuntimeBean().insertCorrectFirstErrorSecond(DATABASE_1,
154: DATABASE_2);
155: } catch (EJBException e) {
156: logger.debug("The bean threw an expected exception {0}", e);
157: }
158: // commits the transaction
159: utx.commit();
160: /*
161: * the bean roll back must not to make any influence in the user
162: * transaction context. So, the client table must be create correctly,
163: * and consequently the "select" query in the table must not to throw
164: * exception.
165: */
166: ExceptionHandleUtil.verifyTable(DATABASE_2,
167: ItfContainerTransaction.TABLE);
168: }
169:
170: }
|