KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestRequiredNewException.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.EJBException 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.ItfDatabaseManager;
38 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
39 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.transaction.ItfTransactionMisc00;
40 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
41
42 /**
43  * Verifies if the container-managed transaction in the session bean is
44  * following the JSR 220. The container must handle the different types of
45  * exception in the transaction context. The items covered in this test are:
46  * 13.3
47  * @reference JSR 220-PROPOSED FINAL
48  * @requirement Application Server must be running; the bean .
49  * SFSBContainerTransactionRequiredNewApp,
50  * SFSBContainerTransactionRequiredNewRollback and
51  * SFSBContainerTransactionRequiredNewRuntime must be deployed for
52  * testing stateful session bean. And, the bean
53  * SLSBContainerTransactionRequiredNewApp,
54  * SLSBContainerTransactionRequiredNewRollback and
55  * SLSBContainerTransactionRequiredNewRuntime must be deployed for
56  * testing stateless session bean.
57  * @setup gets the reference of the bean and binds the databases specified in
58  * the EmbeddedTest.
59  * @author Gisele Pinheiro Souza
60  * @author Eduardo Studzinski Estima de Castro
61  */

62 public abstract class TestRequiredNewException extends TestContainerTransactionException {
63
64     /**
65      * Logger.
66      */

67     private static JLog logger = JLogFactory.getLog(TestRequiredNewException.class);
68
69     /**
70      * Verifies if the container does not use the same transaction when the
71      * other bean has the transaction attribute NOT_SUPPORTED.The bean method throws a runtime exception.
72      * @input -
73      * @output the method execution without error
74      * @throws Exception if an error during the tests occurs.
75      */

76     @Override JavaDoc
77     public void testCallOtherBeanNotSup() throws Exception JavaDoc {
78         super.testCallOtherBeanNotSup();
79         //verifies if the transaction in the bean was rolled back.
80
try {
81             ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
82             fail("The container did not make a rollback in the transaction.");
83         } catch (SQLException JavaDoc e) {
84             logger.debug("The test threw an expected exception {0}", e);
85         }
86         // verifies if the table in the second bean was destroyed. This table
87
// must not to be destroyed because the other bean has the transaction
88
// attribute NOT_SUPPORTED.
89
try {
90             ExceptionHandleUtil.verifyTable(DATABASE_2, ItfTransactionMisc00.TABLE);
91         } catch (SQLException JavaDoc e) {
92             fail("The container made a rollback in the transaction.");
93         }
94     }
95
96     /**
97      * Verifies if the container uses the same transaction when the other bean
98      * has the transaction attribute REQUIRED.The method called
99      * throws a runtime exception.
100      * @input -
101      * @output the method execution without error
102      * @throws Exception if an error during the tests occurs.
103      */

104     @Override JavaDoc
105     public void testCallOtherBeanReq() throws Exception JavaDoc {
106         super.testCallOtherBeanReq();
107         //verifies if the transaction in the bean was rolled back.
108
try {
109             ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
110             fail("The container did not make a rollback in the transaction.");
111         } catch (SQLException JavaDoc e) {
112             logger.debug("The test threw an expected exception {0}", e);
113         }
114         // verifies if the table in the second bean was destroyed. This table
115
// must to be destroyed because the two beans are using the same
116
// transaction.
117
try {
118             ExceptionHandleUtil.verifyTable(DATABASE_2, ItfTransactionMisc00.TABLE);
119             fail("The container did not make a rollback in the transaction.");
120         } catch (SQLException JavaDoc e) {
121             logger.debug("The test threw an expected exception {0}", e);
122         }
123     }
124
125     /**
126      * Verifies if the container uses the same transaction that the client. The
127      * @input -
128      * @output the table created by the client must to be correctly commited.
129      * @throws Exception if an error during the tests occurs.
130      */

131     public void testUsingClientTrans() throws Exception JavaDoc {
132         // gets the transaction
133
UserTransaction JavaDoc utx = ExceptionHandleUtil.getUserTransaction();
134         // starts the transaction
135
utx.begin();
136         // creates a table in the second database
137
ItfDatabaseManager slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class,
138                 ItfDatabaseManager.class);
139         slsbDatabaseManager.insertTable(DATABASE_2, ItfContainerTransaction.TABLE);
140         /*
141          * Calls a bean method that forces an error and makes a roll back in the
142          * method. The bean is using a required new attribute, so the bean
143          * cannot use the same transaction that the client.
144          */

145         try {
146             getRuntimeBean().insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
147         } catch (EJBException JavaDoc e) {
148             logger.debug("The bean threw an expected exception {0}", e);
149         }
150         // commits the transaction
151
utx.commit();
152         /*
153          * the bean roll back must not to make any influence in the user
154          * transaction context. So, the client table must be create correctly,
155          * and consequently the "select" query in the table must not to throw
156          * exception.
157          */

158         ExceptionHandleUtil.verifyTable(DATABASE_2, ItfContainerTransaction.TABLE);
159     }
160
161 }
162
Popular Tags