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: TestTransactionCommitSFSB.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.transaction.beanmanaged;
025:
026: import java.sql.SQLException;
027:
028: import javax.ejb.EJBException;
029: import javax.naming.NamingException;
030: import javax.transaction.Status;
031: import javax.transaction.UserTransaction;
032:
033: import org.ow2.easybeans.tests.common.asserts.Assert;
034: import org.ow2.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.ItfBeanManagedTransaction;
035: import org.ow2.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.SFSBBeanManagedTransaction;
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.TransactionException;
039: import org.ow2.easybeans.tests.common.helper.EJBHelper;
040: import org.ow2.easybeans.tests.common.helper.EmbeddedHelper;
041: import org.ow2.easybeans.tests.common.helper.ExceptionHelper;
042: import org.ow2.easybeans.tests.common.helper.TransactionHelper;
043: import org.ow2.util.log.Log;
044: import org.ow2.util.log.LogFactory;
045: import org.testng.annotations.AfterClass;
046: import org.testng.annotations.BeforeClass;
047: import org.testng.annotations.BeforeMethod;
048: import org.testng.annotations.Test;
049:
050: /**
051: * Verifies if the bean-managed transaction in the stateful bean is following
052: * the JSR 220.The items couvered in this test are: 12.2.3 and 12.3.3 spec.
053: * @reference JSR 220-PROPOSED FINAL
054: * @requirement Application Server must be running; the bean
055: * org.ow2.easybeans.tests.common.ejbs.stateful.beanmanaged.SFSBBeanManagedTransaction
056: * must be deployed.
057: * @setup gets the reference of SFSBBeanManagedTransaction and binds the
058: * databases specified in the EmbeddedTest.
059: * @author Gisele Pinheiro Souza
060: * @author Eduardo Studzinski Estima de Castro
061: */
062: public class TestTransactionCommitSFSB {
063:
064: /**
065: * Bean used during the tests.
066: */
067: private ItfBeanManagedTransaction sfsbBeanManagedTransaction;
068:
069: /**
070: * Bean used to manage the database in the server side.
071: */
072: private ItfDatabaseManager slsbDatabaseManager;
073:
074: /**
075: * The database used during the tests.
076: */
077: private static final String DATABASE = "jdbc_1";
078:
079: /**
080: * Logger.
081: */
082: private static Log logger = LogFactory
083: .getLog(TestTransactionCommitSFSB.class);
084:
085: /**
086: * Creates the bean used during the tests and binds the databases used.
087: * @throws Exception if an error during the test startup occurs.
088: */
089: @BeforeClass
090: public void setup() throws Exception {
091: // Inserts all database before execute the test
092: // used because the container does not provide this feature yet
093: EmbeddedHelper.bindDatasource();
094:
095: // creates the bean used to manages the databse in the server site.
096: slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(
097: SLSBDatabaseManager.class, ItfDatabaseManager.class);
098: }
099:
100: /**
101: * Tests if the container supports the bean that does not close the
102: * transaction in the same method that the transaction was opened.
103: * @input -
104: * @output SQLException because the insert was not commited.
105: * @throws Exception if an error during the test occurs.
106: */
107: @Test
108: public void testTransInTwoMethods() throws Exception {
109: try {
110: // inserts a table without making the commit.
111: sfsbBeanManagedTransaction
112: .insertTableWithoutCommitTransaction();
113: // tries to delete the table in other connection.
114: sfsbBeanManagedTransaction
115: .dropTableWithoutBeginTransaction();
116: } catch (TransactionException e) {
117: throw e.getParentException();
118: }
119: }
120:
121: /**
122: * Tests if the container allows the bean open a transaction without close
123: * the other one, i.e., if the container allows nested transactions. The
124: * transaction is opened twice, but in differents methods.
125: * @input -
126: * @output a NotSupportedException
127: * @throws Exception if an error during the tests occurs.
128: */
129: @Test(expectedExceptions=javax.transaction.NotSupportedException.class)
130: public void testBeginTwiceSameTrans() throws Exception {
131: try {
132: // call an userTransaction.begin()
133: sfsbBeanManagedTransaction
134: .insertTableWithoutCommitTransaction();
135: // call other userTransaction without finish the first one
136: sfsbBeanManagedTransaction
137: .dropTableWithBeginCommitTransaction();
138: } catch (TransactionException e) {
139: throw e.getParentException();
140: }
141:
142: }
143:
144: /**
145: * Tests if the container allows the bean open a transaction without close
146: * the other one, i.e., if the container allows nested transactions. There
147: * are two transactions where the second one is started before the first one
148: * makes the commit. The both transaction are in the same method.
149: * @input -
150: * @output a NotSupportedException.
151: * @throws Exception if an error durong the tests occurs.
152: */
153: @Test(expectedExceptions=javax.transaction.NotSupportedException.class)
154: public void testBeginTwoTransSameMethod() throws Exception {
155: try {
156: // call an userTransaction.begin()
157: sfsbBeanManagedTransaction.insertTableWithNestedTrans();
158: } catch (TransactionException e) {
159: throw e.getParentException();
160: }
161: }
162:
163: /**
164: * Tests if the container allows the bean open a transaction without close
165: * the other one, i.e., if the container allows nested transactions. There
166: * are two transactions where the second one is started before the first one
167: * makes the commit. Each transaction is in a different method.
168: * @input -
169: * @output a NotSupportedException
170: * @throws Exception if an error during the tests occurs.
171: */
172: @Test(expectedExceptions=javax.transaction.NotSupportedException.class)
173: public void testBeginTwoTransDifMethod() throws Exception {
174: try {
175: // call an userTransaction.begin()
176: sfsbBeanManagedTransaction
177: .insertTableWithoutCommitTransaction();
178: // call other userTransaction without finish the first one
179: sfsbBeanManagedTransaction.insertTableWithNewTransaction();
180: } catch (TransactionException e) {
181: throw e.getParentException();
182: }
183: }
184:
185: /**
186: * If the bean has a transaction active and the client too, the client
187: * transaction must be resumed when the bean is called.
188: * @input -
189: * @output the bean transaction will make a rollback and the client
190: * transaction must be active.
191: * @throws Exception if a erro during the tests occurs.
192: */
193: @Test
194: public void testUsingClientTransaction() throws Exception {
195: // calls a method that starts a transaction
196: sfsbBeanManagedTransaction
197: .insertTableWithoutCommitTransaction();
198: // gets the transaction
199: UserTransaction utx = TransactionHelper
200: .getInternalUserTransaction();
201: // starts the transaction
202: utx.begin();
203: // makes a rollback in the bean transaction, the container
204: // must to resume the user transaction and execute the
205: // bean transaction in other transaction
206: sfsbBeanManagedTransaction.setRollback();
207: // tries to commit the transaction to avoid a nested transaction
208: try {
209: utx.commit();
210: } catch (Exception e) {
211: logger
212: .debug(
213: "Error when the transaction made a rollback {0}",
214: e);
215: }
216: Integer[] expected = { new Integer(Status.STATUS_COMMITTED),
217: new Integer(Status.STATUS_NO_TRANSACTION) };
218:
219: // the user transaction must be active yet.
220: Assert
221: .assertEquals(new Integer(utx.getStatus()), expected,
222: "After the commit the transaction must be commited or not_transaction");
223:
224: }
225:
226: /**
227: * Tests if the container does not allow the bean uses the setRollbackOnly.
228: * @input -
229: * @output an IllegalStateException
230: * @throws Exception if an error during the tests occurs.
231: */
232: @Test
233: public void testSetRollbackOnly() throws Exception {
234: // calls the setrollbackonly that must to throw exception
235: try {
236: sfsbBeanManagedTransaction.setRollbackOnly();
237: } catch (EJBException e) {
238: ExceptionHelper.checkCause(e,
239: java.lang.IllegalStateException.class);
240: }
241: }
242:
243: /**
244: * Tests if the container does not allow the bean uses the getRollbackOnly.
245: * @input -
246: * @output an IllegalStateException
247: * @throws Exception if an error during the tests occurs.
248: */
249: @Test
250: public void testgetRollbackOnly() throws Exception {
251: // calls the setrollbackonly that must to throw exception
252: try {
253: sfsbBeanManagedTransaction.getRollbackOnly();
254: } catch (EJBException e) {
255: ExceptionHelper.checkCause(e,
256: java.lang.IllegalStateException.class);
257: }
258: }
259:
260: /**
261: * Tests if the transaction begin and the transaction commit work well.
262: * Tries to create a table and verifies if the table was created correctly.
263: * @input -
264: * @output method executed without exception.
265: * @throws Exception if an error during the test occurs.
266: */
267: @Test
268: public void testTransInSameMethod() throws Exception {
269: try {
270: sfsbBeanManagedTransaction
271: .insertTableWithBeginCommitTransaction();
272: } catch (TransactionException e) {
273: throw e.getParentException();
274: }
275: // verifies if the table was created
276: slsbDatabaseManager.verifyTable(DATABASE,
277: ItfBeanManagedTransaction.TABLE);
278:
279: }
280:
281: /**
282: * Tests if the rollback works properly. The method calls an
283: * UserTransaction.begin(), inserts a table and makes a rollback in the
284: * transaction.
285: * @input -
286: * @output the select * from test must return an exception, because the
287: * table does not exists.
288: * @throws Exception if an error during the test occurs.
289: */
290: @Test(expectedExceptions=SQLException.class)
291: public void testRollbackInSameMethod() throws Exception {
292: sfsbBeanManagedTransaction.insertTableWithBeginRollback();
293:
294: // verifies if the table was created, the expected is the table not
295: // exists.
296: slsbDatabaseManager.verifyTable(DATABASE,
297: ItfBeanManagedTransaction.TABLE);
298:
299: }
300:
301: /**
302: * Deletes the databases entries from the registry.
303: * @throws Exception if an error occurs during the unbind.
304: */
305: @AfterClass
306: public void tierDown() throws Exception {
307: // Removes all database registry references after execute the test
308: // it's used because the container does not provide this feature yet.
309: // It's run after each test to allow run each test separately.
310: EmbeddedHelper.unbindDatasource();
311: }
312:
313: /**
314: * Deletes the table to avoid errors in each test.
315: */
316: @BeforeMethod
317: public void deletesTable() {
318: // deletes the table after each test to avoid errors.
319: try {
320: // verifies if the table was created
321: slsbDatabaseManager.deleteTable(DATABASE,
322: ItfBeanManagedTransaction.TABLE);
323: } catch (SQLException e) {
324: logger
325: .debug(
326: "The table delete threw an error during the execution {0}",
327: e);
328: } catch (NamingException e) {
329: logger
330: .debug(
331: "The table delete threw an error during the execution {0}",
332: e);
333: }
334: }
335:
336: /**
337: * Gets a new instance of the bean before each test.
338: * @throws Exception if an error during the bean creation occurs.
339: */
340: @BeforeMethod
341: public void createBean() throws Exception {
342: sfsbBeanManagedTransaction = EJBHelper.getBeanRemoteInstance(
343: SFSBBeanManagedTransaction.class,
344: ItfBeanManagedTransaction.class);
345: sfsbBeanManagedTransaction.startup(
346: ItfBeanManagedTransaction.CREATE_TABLE, DATABASE);
347: }
348: }
|