KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > transaction > containermanaged > base > TestSupportsException


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

25 package org.objectweb.easybeans.tests.transaction.containermanaged.base;
26
27 import static org.testng.Assert.fail;
28
29 import java.sql.SQLException JavaDoc;
30
31 import javax.ejb.EJBTransactionRolledbackException JavaDoc;
32 import javax.transaction.UserTransaction JavaDoc;
33
34 import org.objectweb.easybeans.log.JLog;
35 import org.objectweb.easybeans.log.JLogFactory;
36 import org.objectweb.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
37 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.transaction.ItfTransactionMisc00;
38
39 /**
40  * Verifies if the container-managed transaction in the session bean is
41  * following the JSR 220. The container must handle the different types of
42  * exception in the transaction context. The items covered in this test are:
43  * 13.3
44  * @reference JSR 220-PROPOSED FINAL
45  * @requirement Application Server must be running; the bean .
46  * SFSBContainerTransactionSupportsNewApp,
47  * SFSBContainerTransactionSupportsNewRollback and
48  * SFSBContainerTransactionSupportsNewRuntime must be deployed for
49  * testing stateful session bean. And, the bean
50  * SLSBContainerTransactionSupportsNewApp,
51  * SLSBContainerTransactionSupportsNewRollback and
52  * SLSBContainerTransactionSupportsNewRuntime must be deployed for
53  * testing stateless session bean.
54  * @setup gets the reference of the bean and binds the databases specified in
55  * the EmbeddedTest.
56  * @author Gisele Pinheiro Souza
57  * @author Eduardo Studzinski Estima de Castro
58  */

59 public abstract class TestSupportsException extends TestContainerTransactionException {
60
61     /**
62      * Logger.
63      */

64     private static JLog logger = JLogFactory.getLog(TestSupportsException.class);
65
66     /**
67      * Verifies if the container does not use the same transaction when the
68      * other bean has the transaction attribute NOT_SUPPORTED. In this case, the
69      * client has a transaction, so the bean performs in the same way than the
70      * REQUIRED.
71      * @input -
72      * @output the method execution without error
73      * @throws Exception if an error during the tests occurs.
74      */

75     public void testCallOtherBeanNotSupWithTrans() throws Exception JavaDoc {
76         UserTransaction JavaDoc utx = ExceptionHandleUtil.getUserTransaction();
77         utx.begin();
78
79         try {
80             getRuntimeBean().insertTablesUsingAuxBeanNotSup(DATABASE_1, DATABASE_2);
81             fail("The container did not throw the EJBTransactionRolledbackException.");
82         } catch (EJBTransactionRolledbackException JavaDoc e) {
83             logger.debug("The bean threw an expected error during the execution {0}", e);
84         }
85         // verifies if the container discarded the instance.
86
if (!ExceptionHandleUtil.isDiscarded(getRuntimeBean())) {
87             fail("The bean was not discarded.");
88         }
89
90         // tries to commit the transaction
91
try {
92             utx.commit();
93             fail("The transaction is marked as rollback. The client cannot make the commit.");
94         } catch (Exception JavaDoc e) {
95             logger.debug("Expected exception {0}", e);
96         }
97
98         // verifies if the transaction in the bean was rolled back.
99
try {
100             ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
101             fail("The container did not make a rollback in the transaction.");
102         } catch (SQLException JavaDoc e) {
103             logger.debug("The test threw an expected exception {0}", e);
104         }
105         // verifies if the table in the second bean was destroyed. This table
106
// must not to be destroyed because the other bean has the transaction
107
// atttribute NOT_SUPPORTED.
108
try {
109             ExceptionHandleUtil.verifyTable(DATABASE_2, ItfTransactionMisc00.TABLE);
110         } catch (SQLException JavaDoc e) {
111             fail("The container made a rollback in the transaction.");
112         }
113     }
114
115     /**
116      * Verifies if the container uses the same transaction when the other bean
117      * has the transaction attribute REQUIRED. In this case, the client has a
118      * transaction, so the bean performs in the same way than the REQUIRED.
119      * @input -
120      * @output the method execution without error
121      * @throws Exception if an error during the tests occurs.
122      */

123     public void testCallOtherBeanReqWithTrans() throws Exception JavaDoc {
124         UserTransaction JavaDoc utx = ExceptionHandleUtil.getUserTransaction();
125         utx.begin();
126         try {
127             getRuntimeBean().insertTablesUsingAuxBeanReq(DATABASE_1, DATABASE_2);
128             fail("The container did not throw the EJBTransactionRolledbackException.");
129         } catch (EJBTransactionRolledbackException JavaDoc e) {
130             logger.debug("The bean threw an expected error during the execution {0}", e);
131         }
132         // verifies if the container discarded the instance.
133
if (!ExceptionHandleUtil.isDiscarded(getRuntimeBean())) {
134             fail("The bean was not discarded.");
135         }
136         // tries to commit the transaction
137
try {
138             utx.commit();
139             fail("The transaction is marked as rollback. The client cannot make the commit.");
140         } catch (Exception JavaDoc e) {
141             logger.debug("Expected exception {0}", e);
142         }
143
144         // verifies if the transaction in the bean was rolled back.
145
try {
146             ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
147             fail("The container did not make a rollback in the transaction.");
148         } catch (SQLException JavaDoc e) {
149             logger.debug("The test threw an expected excpetion {0}", e);
150         }
151         // verifies if the table in the second bean was destroyed. This table
152
// must to be destroyed because the two beans are using the same
153
// transaction.
154
try {
155             ExceptionHandleUtil.verifyTable(DATABASE_2, ItfTransactionMisc00.TABLE);
156             fail("The container did not make a rollback in the transaction.");
157         } catch (SQLException JavaDoc e) {
158             logger.debug("The test threw an expected exception {0}", e);
159         }
160     }
161
162     /**
163      * Verifies if the container does not use the same transaction when the
164      * other bean has the transaction attribute NOT_SUPPORTED. The attribute
165      * transaction SUPPORTS has the same behavior that the attribute
166      * NOT_SUPPORTED that does not have a transaction context, so there is not
167      * transaction to be managed.The bean method throws a runtime exception.
168      * @input -
169      * @output the method execution without error
170      * @throws Exception if an error during the tests occurs.
171      */

172     public void testCallOtherBeanNotSupWithoutTrans() throws Exception JavaDoc {
173         super.testCallOtherBeanNotSup();
174         verifyCallOtherBean();
175     }
176
177     /**
178      * Verifies if the container uses the same transaction when the other bean
179      * has the transaction attribute REQUIRED.The bean method throws a runtime
180      * exception.
181      * @input -
182      * @output the method execution without error
183      * @throws Exception if an error during the tests occurs.
184      */

185     public void testCallOtherBeanReqWithoutTrans() throws Exception JavaDoc {
186         super.testCallOtherBeanReq();
187         verifyCallOtherBean();
188     }
189
190     /**
191      * Verifies if the bean did not made a rollback, because there is not a
192      * transaction context in this case.
193      * @throws Exception if a problem occurs.
194      */

195     private void verifyCallOtherBean() throws Exception JavaDoc {
196         //verifies if the transaction in the bean was rolled back.
197
try {
198             ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
199         } catch (SQLException JavaDoc e) {
200             fail("The container made a rollback in the transaction.");
201         }
202         // verifies if the table in the second bean was destroyed. This table
203
// must not to be destroyed because the two beans are not using the same
204
// transaction.
205
try {
206             ExceptionHandleUtil.verifyTable(DATABASE_2, ItfTransactionMisc00.TABLE);
207         } catch (SQLException JavaDoc e) {
208             fail("The container made a rollback in the transaction.");
209         }
210     }
211
212 }
213
Popular Tags