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: TestSessionSynchronization.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.transaction.containermanaged.stateful;
025:
026: import static org.testng.Assert.assertTrue;
027: import static org.testng.Assert.fail;
028:
029: import java.sql.SQLException;
030:
031: import javax.transaction.UserTransaction;
032:
033: import org.ow2.easybeans.tests.common.ejbs.stateful.containermanaged.transaction.ItfSessionSync;
034: import org.ow2.easybeans.tests.common.ejbs.stateful.containermanaged.transaction.SFSBSessionSync;
035: import org.ow2.easybeans.tests.common.helper.EJBHelper;
036: import org.ow2.easybeans.tests.transaction.containermanaged.base.TestContainerTransactionBase;
037: import org.ow2.util.log.Log;
038: import org.ow2.util.log.LogFactory;
039: import org.testng.annotations.BeforeClass;
040: import org.testng.annotations.BeforeMethod;
041: import org.testng.annotations.Test;
042:
043: /**
044: * Verifies if the container-managed transaction in the stateful bean is
045: * following the JSR 220.The test verifies if the SessionSynchronization Callbacks are working properly.
046: * The items covered in this test are: 12.6.2.11.
047: * @reference JSR 220-PROPOSED FINAL
048: * @requirement Application Server must be running; the bean
049: * org.ow2.easybeans.tests.common.ejbs.stateful.containermanaged.SFSBSessionSync
050: * must be deployed.
051: * @setup gets the reference of SFSBSessionSync and binds the
052: * databases specified in the EmbeddedTest.
053: * @author Gisele Pinheiro Souza
054: * @author Eduardo Studzinski Estima de Castro
055: */
056: public class TestSessionSynchronization extends
057: TestContainerTransactionBase {
058:
059: /**
060: * Bean used during the tests.
061: */
062: private ItfSessionSync sfsbSessionSync;
063:
064: /**
065: * Logger.
066: */
067: private static Log logger = LogFactory
068: .getLog(TestSessionSynchronization.class);
069:
070: /**
071: * Creates the bean before each test to avoid errors during the tests.
072: * @throws Exception if an error during the bean creation occurs.
073: */
074: @BeforeMethod
075: public void createBean() throws Exception {
076: sfsbSessionSync = EJBHelper.getBeanRemoteInstance(
077: SFSBSessionSync.class, ItfSessionSync.class);
078: sfsbSessionSync.startup(DATABASE_1, DATABASE_2);
079: }
080:
081: /**
082: * Binds the databases used.
083: * @throws Exception if an error during the test startup occurs.
084: */
085: @BeforeClass
086: @Override
087: public void setup() throws Exception {
088: super .setup();
089: }
090:
091: /**
092: * Verifies if the callback methods were called correctly. The afterbegin,
093: * before completion must stay in the same transaction that the method
094: * called. The beforeCompletation calls a setRollbackOnly, so the table
095: * created in the afterBegin must to be deleted and the afterCompletation
096: * must receive that the transaction was not commited.
097: * @throws Exception if an unexpected error occurs during the test.
098: */
099: private void verifyCallbacks() throws Exception {
100: // verifies if the table was created in the DATABASE_1
101: // the afterBegin method inserts the table in this database.
102: try {
103: verifyTable(DATABASE_1, ItfSessionSync.TABLE);
104: fail("The beforeCompletion method was not called "
105: + " or the afterBegin method and the method called were not in the same transaction.");
106: } catch (SQLException e) {
107: logger.debug("The method threw an expected exception {0}",
108: e);
109: }
110:
111: // verifies if the table was created in the DATABASE_2
112: // the insertTableRequired method inserts the table in this database.
113: try {
114: verifyTable(DATABASE_2, ItfSessionSync.TABLE);
115: fail("The beforeCompletion method was not called.");
116: } catch (SQLException e) {
117: logger.debug("The method threw an expected exception {0}",
118: e);
119: }
120: // the after completion method inserts the value in this variable,
121: // so it tests if the afterCompletion is called.
122: assertTrue(sfsbSessionSync.isRolledback());
123: }
124:
125: /**
126: * Verifies if the table created by the afterBegin method is created when a
127: * method that does not support transaction is called.Also, this method
128: * verifies if the table in the method was create properly.
129: * @throws Exception if an unexpected error occurs during the test.
130: */
131: private void verifyTablesWithouCallbacks() throws Exception {
132: // verifies if the table was created in the DATABASE_1
133: // the afterTransaction method inserts the table in this database.
134: try {
135: verifyTable(DATABASE_1, ItfSessionSync.TABLE);
136: fail("The method afterbegin was called, but it is not correct in this case.");
137: } catch (SQLException e) {
138: logger.debug("The method threw an expected exception {0}",
139: e);
140: }
141: // verifies if the table was created in the DATABASE_?
142: // the method CALLED inserts the table in this database.
143: try {
144: verifyTable(DATABASE_2, ItfSessionSync.TABLE);
145: logger.debug("The table was inserted correctly.");
146: } catch (SQLException e) {
147: fail("The container rolled back the transaction, but there is not transaction associated.");
148: }
149: }
150:
151: /**
152: * Calls the method that uses the required attribute.The bean implements the
153: * SessionSynchronization interface, so the methods afterBegin,
154: * beforeCompletion and afterCompletion must be called. The method
155: * beforeCompletion calls setRollbackOnly, so the transaction must be rolled
156: * back.
157: * @input -
158: * @output the tables not created because the transaction rolled back.
159: * @throws Exception if an error occurs during the tests.
160: */
161: @Test(dependsOnMethods={"testSessionSyncWithUserTransAndSupports"})
162: public void testSessionSyncWithRequired() throws Exception {
163: sfsbSessionSync.insertTableRequired();
164: verifyCallbacks();
165: }
166:
167: /**
168: * Calls the method that uses the requires_new attribute.The bean implements the
169: * SessionSynchronization interface, so the methods afterBegin,
170: * beforeCompletion and afterCompletion must be called. The method
171: * beforeCompletion calls setRollbackOnly, so the transaction must be
172: * rolled back.
173: * @input -
174: * @output the tables not created because the transaction rolled back.
175: * @throws Exception if an error occurs during the tests.
176: */
177: @Test
178: public void testSessionSyncWithRequiresNew() throws Exception {
179: sfsbSessionSync.insertTableRequiredNew();
180: verifyCallbacks();
181: }
182:
183: /**
184: * Calls the method that uses the mandatory attribute.The bean implements the
185: * SessionSynchronization interface, so the methods afterBegin,
186: * beforeCompletion and afterCompletion must be called. The method
187: * beforeCompletion calls setRollbackOnly, so the transaction must be
188: * rolled back.
189: * @input -
190: * @output the tables not created because the transaction rolled back.
191: * @throws Exception if an error occurs during the tests.
192: */
193: @Test
194: public void testSessionSyncWithMandatory() throws Exception {
195: UserTransaction utx = getUserTransaction();
196: utx.begin();
197: sfsbSessionSync.insertTableMandatory();
198: try {
199: utx.commit();
200: } catch (Exception e) {
201: logger.debug("The method threw an expected exception {e}",
202: e);
203: }
204: verifyCallbacks();
205: }
206:
207: /**
208: * Calls the method with transaction attribute supports and verifies if the
209: * methods callback are called. The transaction attribute supports must to
210: * have the same behaviour that the required.
211: * @input -
212: * @output the databases without tables, because the container made a
213: * rollback.
214: * @throws Exception if an error occurs during the tests.
215: */
216: @Test
217: public void testSessionSyncWithUserTransAndSupports()
218: throws Exception {
219: UserTransaction utx = getUserTransaction();
220: utx.begin();
221: sfsbSessionSync.insertTableSupports();
222: try {
223: utx.commit();
224: } catch (Exception e) {
225: logger.debug("The method threw an expected exception {e}",
226: e);
227: }
228: verifyCallbacks();
229: }
230:
231: /**
232: * Calls the method with transaction attribute supports and verifies if the
233: * container did not call the callbacks methdos from SessionSynchronization
234: * interface. The container must no call this methods, because the method is
235: * not in a transaction context.
236: * @throws Exception if an error occurs during the tests.
237: */
238: @Test
239: public void testSessionSyncWithOnlySupports() throws Exception {
240: sfsbSessionSync.insertTableSupports();
241: verifyTablesWithouCallbacks();
242: }
243:
244: /**
245: * Calls the method with transaction attribute not_supported and verifies if the
246: * container did not call the callbacks methdos from SessionSynchronization
247: * interface. The container must no call this methods, because the method is
248: * not in a transaction context.
249: * @throws Exception if an error occurs during the tests.
250: */
251: @Test
252: public void testSessionSyncWithNotSupported() throws Exception {
253: sfsbSessionSync.insertTableNotSupported();
254: verifyTablesWithouCallbacks();
255: }
256:
257: /**
258: * Calls the method with transaction attribute never and verifies if the
259: * container did not call the callbacks methdos from SessionSynchronization
260: * interface. The container must no call this methods, because the method is
261: * not in a transaction context.
262: * @throws Exception if an error occurs during the tests.
263: */
264: @Test
265: public void testSessionSyncWithNever() throws Exception {
266: sfsbSessionSync.insertTableNever();
267: verifyTablesWithouCallbacks();
268: }
269:
270: /**
271: * Deletes the table before each test to avoid errors in each test.
272: */
273: @BeforeMethod
274: @Override
275: public void deleteTable() {
276: deleteTable(DATABASE_1, ItfSessionSync.TABLE);
277: deleteTable(DATABASE_2, ItfSessionSync.TABLE);
278: }
279:
280: /**
281: * Makes a rollback in the transaction actives.
282: * @throws Exception if a transaction exception occurs.
283: */
284: @BeforeMethod
285: @Override
286: public void cleanTransaction() throws Exception {
287: super.cleanTransaction();
288: }
289:
290: }
|