KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > transaction > beanmanaged > TestTransactionCommitSLSB


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: TestTransactionCommitSLSB.java 1166 2006-10-18 13:44:31Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.transaction.beanmanaged;
26
27 import static org.objectweb.easybeans.tests.common.helper.ExceptionHelper.checkCause;
28 import static org.testng.Assert.fail;
29
30 import java.sql.SQLException JavaDoc;
31
32 import javax.ejb.EJBException JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34
35 import org.objectweb.easybeans.log.JLog;
36 import org.objectweb.easybeans.log.JLogFactory;
37 import org.objectweb.easybeans.tests.common.ejbs.stateless.beanmanaged.transaction.ItfBeanManagedTransaction;
38 import org.objectweb.easybeans.tests.common.ejbs.stateless.beanmanaged.transaction.SLSBBeanManagedTransaction;
39 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
40 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
41 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
42 import org.objectweb.easybeans.tests.common.helper.EmbeddedHelper;
43 import org.testng.annotations.AfterClass;
44 import org.testng.annotations.BeforeClass;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
47
48 /**
49  * Verifies if the container manages correctly the beand-managed transaction for
50  * stateless bean.
51  * @reference JSR 220-PROPOSED FINAL
52  * @requirement Application Server must be running; the bean
53  * org.objectweb.easybeans.tests.common.ejbs.stateless.beanmanaged.SLSBBeanManagedTransaction
54  * must be deployed.
55  * @setup gets the reference of SLSBBeanManagedTransaction and binds the
56  * databases specified in the EmbeddedTest.
57  * @author Gisele Pinheiro Souza
58  * @author Eduardo Studzinski Estima de Castro
59  */

60 public class TestTransactionCommitSLSB {
61
62     /**
63      * Bean used during the tests.
64      */

65     private ItfBeanManagedTransaction slsbBeanManagedTransaction;
66
67     /**
68      * Bean used to manage the database in the server side.
69      */

70     private ItfDatabaseManager slsbDatabaseManager;
71
72     /**
73      * Logger.
74      */

75     private static JLog logger = JLogFactory.getLog(TestTransactionCommitSLSB.class);
76
77     /**
78      * The database used during the tests.
79      */

80     private static final String JavaDoc DATABASE = "jdbc_1";
81
82     /**
83      * Publishes the databases.
84      * @throws Exception if an error during the test startup occurs.
85      */

86     @BeforeClass
87     public void setup() throws Exception JavaDoc {
88         // Inserts all database before execute the test
89
// used because the container does not provide this feature yet
90
// Is defined in each test to allows run each test separately.
91
EmbeddedHelper.bindDatasource();
92         // creates the bean used to manages the databse in the server site.
93
slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class, ItfDatabaseManager.class);
94     }
95
96     /**
97      * Tests if the container supports the bean does not close the transaction
98      * in the same method that the transaction was opened. The container must
99      * to:
100      * <ul>
101      * <li>Makes a log to alert the system administrator.</li>
102      * <li>Rollbacks the transaction.</li>
103      * <li>Discard the session bean instance</li>
104      * <li>Throws the javax.ejb.EJBException if using EJB3 client</li>
105      * </ul>
106      * @input -
107      * @output all actions listed above.
108      * @throws Exception if an error during the test occurs.
109      */

110     @Test
111     public void testBeginWithoutCommit() throws Exception JavaDoc {
112         try {
113             // inserts a table without making the commit.
114
slsbBeanManagedTransaction.insertTableWithoutCommit(ItfBeanManagedTransaction.CREATE_TABLE, DATABASE);
115             fail("The container must to throw the javax.ejb.EJBException, if the stateless bean does not make the commit. "
116                     + "However the container did not throw any exception.");
117         } catch (Exception JavaDoc e) {
118             if (!(e instanceof javax.ejb.EJBException JavaDoc)) {
119                 fail("The container must to throw the javax.ejb.EJBException, if the stateless bean does not makes the commit. "
120                         + "However the container did not throw the correct exception.");
121             }
122         }
123         // verifies if the container makes the roll back
124
try {
125             slsbDatabaseManager.verifyTable(DATABASE, ItfBeanManagedTransaction.TABLE);
126             fail("The container must to make the rollback, if the stateless bean does not make the commit.");
127         } catch (SQLException JavaDoc e) {
128             logger.debug("the container made the rollback {0}", e);
129         }
130         //TODO: verifies if the container discard the bean
131

132         // test if the application log the error
133
//TODO: verify how easybeans makes the log
134
}
135
136     /**
137      * Calls userTransaction.begin(), inserts the table and calls
138      * userTransaction.commit().
139      * @input -
140      * @output the table created.
141      * @throws Exception if an error during the tests occurs.
142      */

143     @Test
144     public void testTransInSameMethod() throws Exception JavaDoc {
145         slsbBeanManagedTransaction.insertTableWithBeginCommit(ItfBeanManagedTransaction.CREATE_TABLE, DATABASE);
146         // verifies if the table was created
147
slsbDatabaseManager.verifyTable(DATABASE, ItfBeanManagedTransaction.TABLE);
148     }
149
150     /**
151      * Tests if the container does not allow the bean to use the
152      * setRollbackOnly.
153      * @input -
154      * @output an IllegalStateException
155      * @throws Exception if an error during the tests occurs.
156      */

157     @Test
158     public void testSetRollbackOnly() throws Exception JavaDoc {
159         try {
160             // calls the setRollbackOnly that must to throw exception
161
slsbBeanManagedTransaction.setRollbackOnly();
162         } catch (Exception JavaDoc e) {
163             checkCause(e, IllegalStateException JavaDoc.class);
164         }
165     }
166
167     /**
168      * Tests if the container does not allow the bean to use the
169      * getRollbackOnly.
170      * @input -
171      * @output an IllegalStateException wrapped in EJBException
172      */

173     @Test
174     public void testGetRollbackOnly() {
175         try {
176             // calls the setRollbackOnly that must to throw exception
177
slsbBeanManagedTransaction.getRollbackOnly();
178         } catch (EJBException JavaDoc e) {
179             checkCause(e, IllegalStateException JavaDoc.class);
180         } catch (NamingException JavaDoc e) {
181             fail("Shouldn't throw Naming Exception" + e.getMessage());
182         }
183     }
184
185     /**
186      * Deletes the databases entries from the registry.
187      * @throws Exception if an error occurs during the unbind.
188      */

189     @AfterClass
190     public void tierDown() throws Exception JavaDoc {
191         // Remove all database after execute the test
192
// used because the container does not provide this feature yet.
193
EmbeddedHelper.unbindDatasource();
194     }
195
196     /**
197      * Deletes the table to avoid errors in each test.
198      */

199     @BeforeMethod
200     public void deletesTable() {
201         // deletes the table after each test to avoid errors.
202
try {
203             slsbDatabaseManager.deleteTable(DATABASE, ItfBeanManagedTransaction.TABLE);
204         } catch (SQLException JavaDoc e) {
205             logger.debug("The table delete threw an error during the execution {0}", e);
206         } catch (NamingException JavaDoc e) {
207             logger.debug("The table delete threw an error during the execution {0}", e);
208         }
209     }
210
211     /**
212      * Creates the bean before each test, because there are test that discard
213      * the bean.
214      * @throws Exception if an error during the bea lookup occurs.
215      */

216     @BeforeMethod
217     public void createBean() throws Exception JavaDoc {
218         // creates the bean
219
slsbBeanManagedTransaction = EJBHelper.getBeanRemoteInstance(SLSBBeanManagedTransaction.class,
220                 ItfBeanManagedTransaction.class);
221     }
222
223 }
224
Popular Tags