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: TestSupportsException.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.EJBTransactionRolledbackException;
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.transaction.ItfTransactionMisc00;
035: import org.ow2.util.log.Log;
036: import org.ow2.util.log.LogFactory;
037:
038: /**
039: * Verifies if the container-managed transaction in the session bean is
040: * following the JSR 220. The container must handle the different types of
041: * exception in the transaction context. The items covered in this test are:
042: * 13.3
043: * @reference JSR 220-PROPOSED FINAL
044: * @requirement Application Server must be running; the bean .
045: * SFSBContainerTransactionSupportsNewApp,
046: * SFSBContainerTransactionSupportsNewRollback and
047: * SFSBContainerTransactionSupportsNewRuntime must be deployed for
048: * testing stateful session bean. And, the bean
049: * SLSBContainerTransactionSupportsNewApp,
050: * SLSBContainerTransactionSupportsNewRollback and
051: * SLSBContainerTransactionSupportsNewRuntime must be deployed for
052: * testing stateless session bean.
053: * @setup gets the reference of the bean and binds the databases specified in
054: * the EmbeddedTest.
055: * @author Gisele Pinheiro Souza
056: * @author Eduardo Studzinski Estima de Castro
057: */
058: public abstract class TestSupportsException extends
059: TestContainerTransactionException {
060:
061: /**
062: * Logger.
063: */
064: private static Log logger = LogFactory
065: .getLog(TestSupportsException.class);
066:
067: /**
068: * Verifies if the container does not use the same transaction when the
069: * other bean has the transaction attribute NOT_SUPPORTED. In this case, the
070: * client has a transaction, so the bean performs in the same way than the
071: * REQUIRED.
072: * @input -
073: * @output the method execution without error
074: * @throws Exception if an error during the tests occurs.
075: */
076: public void testCallOtherBeanNotSupWithTrans() throws Exception {
077: UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
078: utx.begin();
079:
080: try {
081: getRuntimeBean().insertTablesUsingAuxBeanNotSup(DATABASE_1,
082: DATABASE_2);
083: fail("The container did not throw the EJBTransactionRolledbackException.");
084: } catch (EJBTransactionRolledbackException e) {
085: logger
086: .debug(
087: "The bean threw an expected error during the execution {0}",
088: e);
089: }
090: // verifies if the container discarded the instance.
091: if (!ExceptionHandleUtil.isDiscarded(getRuntimeBean())) {
092: fail("The bean was not discarded.");
093: }
094:
095: // tries to commit the transaction
096: try {
097: utx.commit();
098: fail("The transaction is marked as rollback. The client cannot make the commit.");
099: } catch (Exception e) {
100: logger.debug("Expected exception {0}", e);
101: }
102:
103: // verifies if the transaction in the bean was rolled back.
104: try {
105: ExceptionHandleUtil.verifyTable(DATABASE_1,
106: ItfContainerTransaction.TABLE);
107: fail("The container did not make a rollback in the transaction.");
108: } catch (SQLException e) {
109: logger.debug("The test threw an expected exception {0}", e);
110: }
111: // verifies if the table in the second bean was destroyed. This table
112: // must not to be destroyed because the other bean has the transaction
113: // atttribute NOT_SUPPORTED.
114: try {
115: ExceptionHandleUtil.verifyTable(DATABASE_2,
116: ItfTransactionMisc00.TABLE);
117: } catch (SQLException e) {
118: fail("The container made a rollback in the transaction.");
119: }
120: }
121:
122: /**
123: * Verifies if the container uses the same transaction when the other bean
124: * has the transaction attribute REQUIRED. In this case, the client has a
125: * transaction, so the bean performs in the same way than the REQUIRED.
126: * @input -
127: * @output the method execution without error
128: * @throws Exception if an error during the tests occurs.
129: */
130: public void testCallOtherBeanReqWithTrans() throws Exception {
131: UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
132: utx.begin();
133: try {
134: getRuntimeBean().insertTablesUsingAuxBeanReq(DATABASE_1,
135: DATABASE_2);
136: fail("The container did not throw the EJBTransactionRolledbackException.");
137: } catch (EJBTransactionRolledbackException e) {
138: logger
139: .debug(
140: "The bean threw an expected error during the execution {0}",
141: e);
142: }
143: // verifies if the container discarded the instance.
144: if (!ExceptionHandleUtil.isDiscarded(getRuntimeBean())) {
145: fail("The bean was not discarded.");
146: }
147: // tries to commit the transaction
148: try {
149: utx.commit();
150: fail("The transaction is marked as rollback. The client cannot make the commit.");
151: } catch (Exception e) {
152: logger.debug("Expected exception {0}", e);
153: }
154:
155: // verifies if the transaction in the bean was rolled back.
156: try {
157: ExceptionHandleUtil.verifyTable(DATABASE_1,
158: ItfContainerTransaction.TABLE);
159: fail("The container did not make a rollback in the transaction.");
160: } catch (SQLException e) {
161: logger.debug("The test threw an expected excpetion {0}", e);
162: }
163: // verifies if the table in the second bean was destroyed. This table
164: // must to be destroyed because the two beans are using the same
165: // transaction.
166: try {
167: ExceptionHandleUtil.verifyTable(DATABASE_2,
168: ItfTransactionMisc00.TABLE);
169: fail("The container did not make a rollback in the transaction.");
170: } catch (SQLException e) {
171: logger.debug("The test threw an expected exception {0}", e);
172: }
173: }
174:
175: /**
176: * Verifies if the container does not use the same transaction when the
177: * other bean has the transaction attribute NOT_SUPPORTED. The attribute
178: * transaction SUPPORTS has the same behavior that the attribute
179: * NOT_SUPPORTED that does not have a transaction context, so there is not
180: * transaction to be managed.The bean method throws a runtime exception.
181: * @input -
182: * @output the method execution without error
183: * @throws Exception if an error during the tests occurs.
184: */
185: public void testCallOtherBeanNotSupWithoutTrans() throws Exception {
186: super .testCallOtherBeanNotSup();
187: verifyCallOtherBean();
188: }
189:
190: /**
191: * Verifies if the container uses the same transaction when the other bean
192: * has the transaction attribute REQUIRED.The bean method throws a runtime
193: * exception.
194: * @input -
195: * @output the method execution without error
196: * @throws Exception if an error during the tests occurs.
197: */
198: public void testCallOtherBeanReqWithoutTrans() throws Exception {
199: super .testCallOtherBeanReq();
200: verifyCallOtherBean();
201: }
202:
203: /**
204: * Verifies if the bean did not made a rollback, because there is not a
205: * transaction context in this case.
206: * @throws Exception if a problem occurs.
207: */
208: private void verifyCallOtherBean() throws Exception {
209: //verifies if the transaction in the bean was rolled back.
210: try {
211: ExceptionHandleUtil.verifyTable(DATABASE_1,
212: ItfContainerTransaction.TABLE);
213: } catch (SQLException e) {
214: fail("The container made a rollback in the transaction.");
215: }
216: // verifies if the table in the second bean was destroyed. This table
217: // must not to be destroyed because the two beans are not using the same
218: // transaction.
219: try {
220: ExceptionHandleUtil.verifyTable(DATABASE_2,
221: ItfTransactionMisc00.TABLE);
222: } catch (SQLException e) {
223: fail("The container made a rollback in the transaction.");
224: }
225: }
226:
227: }
|