KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestTransactionStatus.java 980 2006-07-28 13:20:32Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.transaction.beanmanaged;
26
27 import static org.testng.Assert.assertEquals;
28
29 import java.sql.SQLException JavaDoc;
30
31 import javax.naming.NamingException JavaDoc;
32 import javax.transaction.Status JavaDoc;
33
34 import org.objectweb.easybeans.log.JLog;
35 import org.objectweb.easybeans.log.JLogFactory;
36 import org.objectweb.easybeans.tests.common.asserts.Assert;
37 import org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.ItfBeanManagedTransaction;
38 import org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.SFSBBeanManagedTransaction;
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.BeforeClass;
44 import org.testng.annotations.BeforeMethod;
45 import org.testng.annotations.Test;
46
47 /**
48  * Verifies if the container manages correctly the user transaction for stateful
49  * bean.
50  * @reference JSR 220-PROPOSED FINAL
51  * @requirement Application Server must be running; the bean
52  * org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.SFSBBeanManagedTransaction
53  * must be deployed.
54  * @setup gets the reference of SFSBBeanManagedTransaction and binds the
55  * databases specified in the EmbeddedTest.
56  * @author Gisele Pinheiro Souza
57  * @author Eduardo Studzinski Estima de Castro
58  */

59 public class TestTransactionStatus {
60
61     /**
62      * Bean used during the tests.
63      */

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

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

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

79     private static final String JavaDoc DATABASE = "jdbc_1";
80
81     /**
82      * Creates the bean used during the tests.
83      * @throws Exception if an error during the test startup occurs.
84      */

85     @BeforeClass
86     public void setup() throws Exception JavaDoc {
87         // Inserts all database before execute the test
88
// used because the container does not provide this feature yet
89
// Is defined in each test to allows run each test separately.
90
EmbeddedHelper.bindDatasource();
91         // creates the bean used to manages the databse in the server site.
92
slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class, ItfDatabaseManager.class);
93
94     }
95
96     /**
97      * Verifies if the status is no transaction before the bean uses the
98      * transaction.
99      * @input -
100      * @output the transaction status is no transaction.
101      * @throws Exception if an error during the tests occurs.
102      */

103     @Test
104     public void testStatusBeforeBegin() throws Exception JavaDoc {
105         int intTransactionStatus = sfsbBeanManagedTransaction.getTransactionStatus();
106         assertEquals(Status.STATUS_NO_TRANSACTION, intTransactionStatus);
107
108     }
109
110     /**
111      * Verifies if the status changes after a begin.
112      * @input -
113      * @output the transaction status that must be active.
114      * @throws Exception if an error during the tests occurs.
115      */

116     @Test(dependsOnMethods = "testStatusBeforeBegin")
117     public void testStatusAfterBegin() throws Exception JavaDoc {
118         // starts the transaction without close
119
sfsbBeanManagedTransaction.insertTableWithoutCommitTransaction();
120         // verifies if the transaction is openned
121
int intTransactionStatus = sfsbBeanManagedTransaction.getTransactionStatus();
122         assertEquals(Status.STATUS_ACTIVE, intTransactionStatus);
123     }
124
125     /**
126      * Verifies if the status is commited after makes a commit.
127      * @input -
128      * @output the table inserted and commited.
129      * @throws Exception if an error during the tests occurs.
130      */

131     @Test
132     public void testStatusAfterCommit() throws Exception JavaDoc {
133         // starts and commit the transaction
134
sfsbBeanManagedTransaction.insertTableWithBeginCommitTransaction();
135         // verifies if the transaction is committed
136
int intTransactionStatus = sfsbBeanManagedTransaction.getTransactionStatus();
137         Integer JavaDoc[] expected = {new Integer JavaDoc(Status.STATUS_COMMITTED), new Integer JavaDoc(Status.STATUS_NO_TRANSACTION)};
138         Assert.assertEquals(new Integer JavaDoc(intTransactionStatus), expected,
139                 "The transaction status must be commited or no_transaction");
140
141     }
142
143     /**
144      * Verifies if the status is marked_rollback after makes a rollback.
145      * @input -
146      * @output the table inserted and rolled back.
147      * @throws Exception if an error during the tests occurs.
148      */

149     @Test
150     public void testStatusAfterRollback() throws Exception JavaDoc {
151         // starts and rollback the transaction
152
sfsbBeanManagedTransaction.insertTableWithBeginRollback();
153         // verifies if the transaction is committed
154
int intTransactionStatus = sfsbBeanManagedTransaction.getTransactionStatus();
155         Integer JavaDoc[] expected = {new Integer JavaDoc(Status.STATUS_ROLLEDBACK), new Integer JavaDoc(Status.STATUS_NO_TRANSACTION)};
156         Assert.assertEquals(new Integer JavaDoc(intTransactionStatus), expected,
157                 "The transaction status must be rolledback or no_transaction");
158
159     }
160
161     /**
162      * Deletes the table to avoid errors in each test.
163      */

164     @BeforeMethod
165     public void deletesTable() {
166         // deletes the table after each test to avoid errors.
167
try {
168             slsbDatabaseManager.deleteTable(DATABASE, ItfBeanManagedTransaction.TABLE);
169         } catch (SQLException JavaDoc e) {
170             logger.debug("The table delete threw an error during the execution {0}", e);
171         } catch (NamingException JavaDoc e) {
172             logger.debug("The table delete threw an error during the execution {0}", e);
173         }
174     }
175
176     /**
177      * Creates a new bean instance before each method.
178      * @throws Exception if an error during the bean creation occurs.
179      */

180     @BeforeMethod
181     public void createBean() throws Exception JavaDoc {
182         // creates the bean
183
sfsbBeanManagedTransaction = EJBHelper.getBeanRemoteInstance(SFSBBeanManagedTransaction.class,
184                 ItfBeanManagedTransaction.class);
185         sfsbBeanManagedTransaction.startup(ItfBeanManagedTransaction.CREATE_TABLE, DATABASE);
186     }
187
188 }
189
Popular Tags