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: TestTransactionCommitSLSB.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.transaction.beanmanaged;
025:
026: import static org.ow2.easybeans.tests.common.helper.ExceptionHelper.checkCause;
027: import static org.testng.Assert.fail;
028:
029: import java.sql.SQLException;
030:
031: import javax.ejb.EJBException;
032: import javax.naming.NamingException;
033:
034: import org.ow2.easybeans.tests.common.ejbs.stateless.beanmanaged.transaction.ItfBeanManagedTransaction;
035: import org.ow2.easybeans.tests.common.ejbs.stateless.beanmanaged.transaction.SLSBBeanManagedTransaction;
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.helper.EJBHelper;
039: import org.ow2.easybeans.tests.common.helper.EmbeddedHelper;
040: import org.ow2.util.log.Log;
041: import org.ow2.util.log.LogFactory;
042: import org.testng.annotations.AfterClass;
043: import org.testng.annotations.BeforeClass;
044: import org.testng.annotations.BeforeMethod;
045: import org.testng.annotations.Test;
046:
047: /**
048: * Verifies if the container manages correctly the beand-managed transaction for
049: * stateless bean.
050: * @reference JSR 220-PROPOSED FINAL
051: * @requirement Application Server must be running; the bean
052: * org.ow2.easybeans.tests.common.ejbs.stateless.beanmanaged.SLSBBeanManagedTransaction
053: * must be deployed.
054: * @setup gets the reference of SLSBBeanManagedTransaction and binds the
055: * databases specified in the EmbeddedTest.
056: * @author Gisele Pinheiro Souza
057: * @author Eduardo Studzinski Estima de Castro
058: */
059: public class TestTransactionCommitSLSB {
060:
061: /**
062: * Bean used during the tests.
063: */
064: private ItfBeanManagedTransaction slsbBeanManagedTransaction;
065:
066: /**
067: * Bean used to manage the database in the server side.
068: */
069: private ItfDatabaseManager slsbDatabaseManager;
070:
071: /**
072: * Logger.
073: */
074: private static Log logger = LogFactory
075: .getLog(TestTransactionCommitSLSB.class);
076:
077: /**
078: * The database used during the tests.
079: */
080: private static final String DATABASE = "jdbc_1";
081:
082: /**
083: * Publishes the databases.
084: * @throws Exception if an error during the test startup occurs.
085: */
086: @BeforeClass
087: public void setup() throws Exception {
088: // Inserts all database before execute the test
089: // used because the container does not provide this feature yet
090: // Is defined in each test to allows run each test separately.
091: EmbeddedHelper.bindDatasource();
092: // creates the bean used to manages the databse in the server site.
093: slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(
094: SLSBDatabaseManager.class, ItfDatabaseManager.class);
095: }
096:
097: /**
098: * Tests if the container supports the bean does not close the transaction
099: * in the same method that the transaction was opened. The container must
100: * to:
101: * <ul>
102: * <li>Makes a log to alert the system administrator.</li>
103: * <li>Rollbacks the transaction.</li>
104: * <li>Discard the session bean instance</li>
105: * <li>Throws the javax.ejb.EJBException if using EJB3 client</li>
106: * </ul>
107: * @input -
108: * @output all actions listed above.
109: * @throws Exception if an error during the test occurs.
110: */
111: @Test
112: public void testBeginWithoutCommit() throws Exception {
113: try {
114: // inserts a table without making the commit.
115: slsbBeanManagedTransaction.insertTableWithoutCommit(
116: ItfBeanManagedTransaction.CREATE_TABLE, DATABASE);
117: fail("The container must to throw the javax.ejb.EJBException, if the stateless bean does not make the commit. "
118: + "However the container did not throw any exception.");
119: } catch (Exception e) {
120: if (!(e instanceof javax.ejb.EJBException)) {
121: fail("The container must to throw the javax.ejb.EJBException, if the stateless bean does not makes the commit. "
122: + "However the container did not throw the correct exception.");
123: }
124: }
125: // verifies if the container makes the roll back
126: try {
127: slsbDatabaseManager.verifyTable(DATABASE,
128: ItfBeanManagedTransaction.TABLE);
129: fail("The container must to make the rollback, if the stateless bean does not make the commit.");
130: } catch (SQLException e) {
131: logger.debug("the container made the rollback {0}", e);
132: }
133: //TODO: verifies if the container discard the bean
134:
135: // test if the application log the error
136: //TODO: verify how easybeans makes the log
137: }
138:
139: /**
140: * Calls userTransaction.begin(), inserts the table and calls
141: * userTransaction.commit().
142: * @input -
143: * @output the table created.
144: * @throws Exception if an error during the tests occurs.
145: */
146: @Test
147: public void testTransInSameMethod() throws Exception {
148: slsbBeanManagedTransaction.insertTableWithBeginCommit(
149: ItfBeanManagedTransaction.CREATE_TABLE, DATABASE);
150: // verifies if the table was created
151: slsbDatabaseManager.verifyTable(DATABASE,
152: ItfBeanManagedTransaction.TABLE);
153: }
154:
155: /**
156: * Tests if the container does not allow the bean to use the
157: * setRollbackOnly.
158: * @input -
159: * @output an IllegalStateException
160: * @throws Exception if an error during the tests occurs.
161: */
162: @Test
163: public void testSetRollbackOnly() throws Exception {
164: try {
165: // calls the setRollbackOnly that must to throw exception
166: slsbBeanManagedTransaction.setRollbackOnly();
167: } catch (Exception e) {
168: checkCause(e, IllegalStateException.class);
169: }
170: }
171:
172: /**
173: * Tests if the container does not allow the bean to use the
174: * getRollbackOnly.
175: * @input -
176: * @output an IllegalStateException wrapped in EJBException
177: */
178: @Test
179: public void testGetRollbackOnly() {
180: try {
181: // calls the setRollbackOnly that must to throw exception
182: slsbBeanManagedTransaction.getRollbackOnly();
183: } catch (EJBException e) {
184: checkCause(e, IllegalStateException.class);
185: } catch (NamingException e) {
186: fail("Shouldn't throw Naming Exception" + e.getMessage());
187: }
188: }
189:
190: /**
191: * Deletes the databases entries from the registry.
192: * @throws Exception if an error occurs during the unbind.
193: */
194: @AfterClass
195: public void tierDown() throws Exception {
196: // Remove all database after execute the test
197: // used because the container does not provide this feature yet.
198: EmbeddedHelper.unbindDatasource();
199: }
200:
201: /**
202: * Deletes the table to avoid errors in each test.
203: */
204: @BeforeMethod
205: public void deletesTable() {
206: // deletes the table after each test to avoid errors.
207: try {
208: slsbDatabaseManager.deleteTable(DATABASE,
209: ItfBeanManagedTransaction.TABLE);
210: } catch (SQLException e) {
211: logger
212: .debug(
213: "The table delete threw an error during the execution {0}",
214: e);
215: } catch (NamingException e) {
216: logger
217: .debug(
218: "The table delete threw an error during the execution {0}",
219: e);
220: }
221: }
222:
223: /**
224: * Creates the bean before each test, because there are test that discard
225: * the bean.
226: * @throws Exception if an error during the bea lookup occurs.
227: */
228: @BeforeMethod
229: public void createBean() throws Exception {
230: // creates the bean
231: slsbBeanManagedTransaction = EJBHelper.getBeanRemoteInstance(
232: SLSBBeanManagedTransaction.class,
233: ItfBeanManagedTransaction.class);
234: }
235:
236: }
|