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: TestMandatory.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.assertFalse;
027: import static org.testng.Assert.fail;
028:
029: import javax.transaction.UserTransaction;
030:
031: import org.ow2.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
032: import org.ow2.easybeans.tests.common.exception.TransactionException;
033: import org.ow2.util.log.Log;
034: import org.ow2.util.log.LogFactory;
035:
036: /**
037: * Verifies if the container-managed transaction in the session bean is
038: * following the JSR 220.The items covered in this test are: 12.6.2.5.
039: * @reference JSR 220-PROPOSED FINAL
040: * @requirement Application Server must be running; the bean .
041: * objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.SFSBContainerTransactionMandatory
042: * must be deployed for testing stateful session bean. And, the
043: * bean
044: * objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBContainerTransactionMandatory
045: * must be deployed for testing stateless session bean.
046: * @setup gets the reference of the bean and binds the databases specified in
047: * the EmbeddedTest.
048: * @author Gisele Pinheiro Souza
049: * @author Eduardo Studzinski Estima de Castro
050: */
051: public class TestMandatory extends TestContainerTransactionBase {
052:
053: /**
054: * Logger.
055: */
056: private static Log logger = LogFactory.getLog(TestMandatory.class);
057:
058: /**
059: * Verifies if the container uses the same transaction that the client. The
060: * client rollback must to force a bean rollback, because the both use the
061: * same transaction.
062: * @input -
063: * @output a SQLException because the table must not be created.
064: * @throws Exception if an error during the tests occurs.
065: */
066: @Override
067: public void testUsingClientTrans() throws Exception {
068: super .testUsingClientTrans();
069: }
070:
071: /**
072: * Calls a setRollbackOnly and after inserts two tables. The container must
073: * do a roll back before the method invocation completes. So, the tables
074: * must not be created and a SQLException must be thrown when the test tries
075: * to verify if the table exists.
076: * @input -
077: * @output an exception when the method verifies if the table exists.
078: * @throws Exception if an error occurs during the tests.
079: */
080: public void testSetRollbackOnlyWithUserTransaction()
081: throws Exception {
082: UserTransaction utx = getUserTransaction();
083: utx.begin();
084: try {
085: ItfContainerTransaction sfsbContainerTransaction = getBean();
086: // calls the setRollbackOnly
087: try {
088: sfsbContainerTransaction.setRollbackOnly(DATABASE_1,
089: DATABASE_2);
090: } catch (TransactionException e) {
091: throw e.getParentException();
092: }
093: // tries to commit the transaction
094: try {
095: utx.commit();
096: fail("The transaction is marked as rollback. The client cannot make the commit.");
097: } catch (Exception e) {
098: logger.debug("Expected exception {0}", e);
099: }
100: } finally {
101: assertFalse(
102: transactionIsActive(),
103: "There was an exception in the server side. The container is using the same transaction "
104: + "that the client, so the cointainer should rollback the transaction.");
105: }
106: }
107:
108: /**
109: * Calls the getRollbackOnly that must throw an exception for this type of
110: * transaction attribute.The EJBTransactionRequiredException is thrown,
111: * because the setRollbackOnly will be called without an transaction
112: * context.
113: * @input -
114: * @output an exception when the method call a method without a transaction
115: * context.
116: * @throws Exception if an error occurs during the tests.
117: */
118: public void testSetRollbackOnlyWithoutUserTransaction()
119: throws Exception {
120: try {
121: testSetRollbackOnly();
122: } finally {
123: assertFalse(
124: transactionIsActive(),
125: "There was an exception in the server side. The container is using the same transaction "
126: + "that the client, so the cointainer should rollback the transaction.");
127: }
128: }
129:
130: /**
131: * Calls the getRollbackOnly that must not throw any exception for this type
132: * of transaction attribute.
133: * @input -
134: * @output the method execution without error.
135: * @throws Exception if an error occurs during the tests.
136: */
137: public void testGetRollbackOnlyWithUserTransaction()
138: throws Exception {
139: UserTransaction utx = getUserTransaction();
140:
141: utx.begin();
142: testGetRollbackOnly();
143: utx.commit();
144: }
145:
146: /**
147: * Calls the getRollbackOnly that must throw an exception for this type of
148: * transaction attribute.The EJBTransactionRequiredException is thrown,
149: * because the setRollbackOnly will be called without an transaction
150: * context.
151: * @input -
152: * @output an exception when the method call a method without a transaction
153: * context.
154: * @throws Exception if an error occurs during the tests.
155: */
156: public void testGetRollbackOnlyWithoutUserTransaction()
157: throws Exception {
158: testGetRollbackOnly();
159:
160: }
161:
162: /**
163: * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged#testGetUserTransactionWithEJBContext()
164: * @throws Exception if an error occurs during the tests.
165: */
166: @Override
167: public void testGetUserTransactionWithEJBContext() throws Exception {
168: UserTransaction utx = getUserTransaction();
169: utx.begin();
170: try {
171: super .testGetUserTransactionWithEJBContext();
172: } finally {
173: utx.rollback();
174: }
175: }
176:
177: /**
178: * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged#testGetUserTransactionWithLookup()
179: * @throws Exception if an error occurs during the tests.
180: */
181: @Override
182: public void testGetUserTransactionWithLookup() throws Exception {
183: UserTransaction utx = getUserTransaction();
184: utx.begin();
185: try {
186: super.testGetUserTransactionWithLookup();
187: } finally {
188: utx.rollback();
189: }
190: }
191:
192: }
|