KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > deploymentdesc > TestAppException


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: TestAppException.java 977 2006-07-28 13:18:26Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.deploymentdesc;
26
27 import static org.testng.Assert.fail;
28
29 import java.sql.SQLException JavaDoc;
30
31 import javax.naming.NamingException 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.stateless.containermanaged.ItfDatabaseManager;
37 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
38 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.exception.ItfExceptionXML;
39 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.exception.SLSBExceptionXML;
40 import org.objectweb.easybeans.tests.common.exception.CustomException00;
41 import org.objectweb.easybeans.tests.common.exception.CustomException01;
42 import org.objectweb.easybeans.tests.common.exception.IllegalException;
43 import org.objectweb.easybeans.tests.common.exception.RollbackApplicationException;
44 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
45 import org.objectweb.easybeans.tests.common.helper.TransactionHelper;
46 import org.testng.annotations.BeforeMethod;
47 import org.testng.annotations.Test;
48
49 /**
50  * Verifies if the container can set the application exception defined by
51  * deployment descriptor.
52  * @reference JSR 220 - FINAL RELEASE
53  * @requirement the bean SLSBExceptionXML and SLSBDatabaseManager must be
54  * deployed to make the tests, and, the deployment descriptor in
55  * the SLSBExceptionXML package must be used too.
56  * @setup gets an instance of the SLSBExceptionXML and SLSBDatabaseManager.
57  * Also, it cleans the database(it drops the table)
58  * @author Gisele Pinheiro Souza
59  * @author Eduardo Studzinski Estima de Castro
60  */

61 public class TestAppException {
62
63     /**
64      * The database used during the tests.
65      */

66     public static final String JavaDoc DB_NAME = "jdbc_1";
67
68    /**
69      * Bean used to verify the local access.
70      */

71     private ItfExceptionXML bean;
72
73     /**
74      * Logger.
75      */

76     private static JLog logger = JLogFactory.getLog(TestAppException.class);
77
78     /**
79      * Bean used to manage the database in the server side.
80      */

81     private ItfDatabaseManager slsbDatabaseManager;
82
83     /**
84      * Creates the stateful bean used during the tests.
85      * @throws Exception if an error occurs during the lookup.
86      */

87     @BeforeMethod
88     public void setup() throws Exception JavaDoc {
89         bean = EJBHelper.getBeanRemoteInstance(SLSBExceptionXML.class, ItfExceptionXML.class);
90         // creates the bean used to manages the databse in the server site.
91
slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class, ItfDatabaseManager.class);
92         deleteTable(DB_NAME);
93     }
94
95     /**
96      * Verifies if the definition if an application exception(with rollback =
97      * false) in the deployment descriptor works properly. When an application
98      * exception with rollback = false occurs, the container must not mark the
99      * transaction for rollback. Also, the container must re-throw the
100      * exception.
101      * @input -
102      * @output the correct method execution without exceptions.
103      * @throws Exception if some error occurs during the tests.
104      */

105     @Test
106     public void testAppExceptionWithoutRollback() throws Exception JavaDoc{
107         //verifies if the application exception is re-throw
108
//the exception is not a checked exception
109
UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
110         try{
111             utx.begin();
112             bean.createTableWithAppException(DB_NAME);
113             fail("The container did not re-throw the application exception.");
114         }catch(CustomException01 e){
115            logger.debug("The container threw an expected exception {0}", e);
116         }finally{
117             // the container must not mark the transaction for rollback, so the
118
// commit should work.
119
utx.commit();
120         }
121         //verifies if the commit worked
122
verifyTable(DB_NAME);
123     }
124
125
126     /**
127      * Verifies if the definition if an application exception overrided in a
128      * deployment descriptor works properly. The annotation defines the
129      * rollback as true, however the deployment descriptor defines
130      * the transaction attribute as false. The deployment descriptor must
131      * override the annotation value for the rollback. When an application
132      * exception with rollback = false occurs, the container must not mark the
133      * transaction for rollback. Also, the container must re-throw the
134      * exception.
135      * @input -
136      * @output the correct method execution without exceptions.
137      * @throws Exception if some error occurs during the tests.
138      */

139     @Test
140     public void testAppExceptionWithRollbackOverride() throws Exception JavaDoc{
141         //verifies if the application exception is re-throw
142
//the exception is not a checked exception
143
UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
144         try{
145             utx.begin();
146             bean.createTableWithAppExceptionOverride(DB_NAME);
147             fail("The container did not re-throw the application exception.");
148         }catch(RollbackApplicationException e){
149            logger.debug("The container threw an expected exception {0}", e);
150         }finally{
151             // the container must not mark the transaction for rollback, so the
152
// commit should work.
153
utx.commit();
154         }
155         //verifies if the commit worked
156
verifyTable(DB_NAME);
157     }
158     /**
159      * Verifies if the definition if an application exception(with rollback =
160      * true) in the deployment descriptor works properly. When an application
161      * exception with rollback = true occurs, the container must mark the
162      * transaction for rollback. Also, the container must re-throw the
163      * exception.
164      * @input -
165      * @output the correct method execution without exceptions.
166      * @throws Exception if some error occurs during the tests.
167      */

168     @Test
169     public void testAppExceptionWithRollback() throws Exception JavaDoc{
170         //verifies if the application exception is re-throw
171
//the exception is a checked exception
172
UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
173         try{
174             utx.begin();
175             bean.createTableWithAppExceptionAndRollback(DB_NAME);
176             fail("The container did not re-throw the application exception.");
177         }catch(CustomException00 e){
178            logger.debug("The container threw an expected exception {0}", e);
179         }finally{
180             // the container must mark the transaction for rollback, so the
181
// commit should not work.
182
try{
183                 utx.commit();
184                 fail("The container did not mark the transaction for rollback");
185             }catch(Exception JavaDoc e){
186                 logger.debug("The container threw an expected exception {0}", e);
187             }
188         }
189     }
190
191
192     /**
193      * Verifies if the definition if an application exception(with the dafault rollback
194      * value) in the deployment descriptor works properly. When an application
195      * exception with rollback defult value(false) occurs, the container must not mark the
196      * transaction for rollback. Also, the container must re-throw the
197      * exception.
198      * @input -
199      * @output the correct method execution without exceptions.
200      * @throws Exception if some error occurs during the tests.
201      */

202     @Test
203     public void testAppExceptionWithRollbackDefault() throws Exception JavaDoc{
204         //verifies if the application exception is re-throw
205
//the exception is not a checked exception
206
UserTransaction JavaDoc utx = TransactionHelper.getInternalUserTransaction();
207         try{
208             utx.begin();
209             bean.createTableWithAppExceptionDefault(DB_NAME);
210             fail("The container did not re-throw the application exception.");
211         }catch(IllegalException e){
212            logger.debug("The container threw an expected exception {0}", e);
213         }finally{
214             // the container must not mark the transaction for rollback, so the
215
// commit should work.
216
utx.commit();
217         }
218         //verifies if the commit worked
219
verifyTable(DB_NAME);
220     }
221
222
223     /**
224      * Verifies if the table was created in the database.
225      * @param database the database name.
226      * @throws NamingException if a lookup error occurs.
227      * @throws SQLException if a database error occurs.
228      */

229     protected void verifyTable(final String JavaDoc database) throws NamingException JavaDoc, SQLException JavaDoc {
230         slsbDatabaseManager.verifyTable(database, ItfExceptionXML.TABLE);
231     }
232
233     /**
234      * Deletes the table in the database.
235      * @param dbName the database name in the registry.
236      */

237     protected void deleteTable(final String JavaDoc dbName) {
238         // deletes the table after each test to avoid errors.
239
try {
240             slsbDatabaseManager.deleteTable(dbName, ItfExceptionXML.TABLE);
241         } catch (SQLException JavaDoc e) {
242             logger.debug("The table delete threw an error during the execution {0}", e);
243         } catch (NamingException JavaDoc e) {
244             logger.debug("The table delete threw an error during the execution {0}", e);
245         }
246     }
247
248
249 }
250
Popular Tags