KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestExceptionHandleBase.java 893 2006-07-19 14:19:23Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.transaction.containermanaged.base;
26
27 import static org.testng.Assert.assertTrue;
28 import static org.testng.Assert.fail;
29
30 import java.sql.SQLException JavaDoc;
31
32 import javax.ejb.EJBException JavaDoc;
33 import javax.transaction.UserTransaction JavaDoc;
34
35 import org.objectweb.easybeans.log.JLog;
36 import org.objectweb.easybeans.log.JLogFactory;
37 import org.objectweb.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
38 import org.objectweb.easybeans.tests.common.exception.AppException;
39 import org.objectweb.easybeans.tests.common.exception.AppRuntimeException;
40 import org.objectweb.easybeans.tests.common.exception.RollbackAppRuntimeException;
41 import org.objectweb.easybeans.tests.common.exception.RollbackApplicationException;
42 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
43 import org.objectweb.easybeans.tests.common.helper.ExceptionHelper;
44
45 /**
46  * Access different beans that have the same behaviour, but the beans throw
47  * different types of exception.
48  * @author Gisele Pinheiro Souza
49  * @author Eduardo Studzinski Estima de Castro
50  */

51 public abstract class TestExceptionHandleBase {
52
53     /**
54      * Constant that defines the first database name.
55      */

56     protected static final String JavaDoc DATABASE_1 = "jdbc_1";
57
58     /**
59      * Constant that defines the second database name.
60      */

61     protected static final String JavaDoc DATABASE_2 = "jdbc_2";
62
63     /**
64      * Bean used during the tests that throws an application exception with
65      * rollback = false.
66      */

67     private ItfContainerTransaction sfsbContainerTransactionApp = null;
68
69     /**
70      * Bean used during the tests that throws an application exception with
71      * rollback = true.
72      */

73     private ItfContainerTransaction sfsbContainerTransactionRollback = null;
74
75     /**
76      * Bean used during the tests that throws an application exception(that
77      * extends a runtime exception) with rollback = false.
78      */

79     private ItfContainerTransaction sfsbContainerTransactionApp01 = null;
80
81     /**
82      * Bean used during the tests that throws an application exception(that
83      * extends a runtime exception) with rollback = true.
84      */

85     private ItfContainerTransaction sfsbContainerTransactionApp02 = null;
86
87     /**
88      * Bean used during the tests that throws a runtime exception.
89      */

90     private ItfContainerTransaction sfsbContainerTransactionRuntime = null;
91
92     /**
93      * Logger.
94      */

95     private static JLog logger = JLogFactory.getLog(TestExceptionHandleBase.class);
96
97     /**
98      * Initiates all things needed to execute the tests.
99      * @throws Exception if an error occurs.
100      */

101     public void setup() throws Exception JavaDoc {
102         // cleans the transaction active in other test
103
ExceptionHandleUtil.cleanTransaction();
104         // creates all beans
105
createBeanApp();
106         createBeanApp01();
107         createBeanApp02();
108         createBeanRollback();
109         createBeanRuntime();
110         // cleans the database
111
deleteTable();
112     }
113
114     /**
115      * Creates a bean that throws always an application exception that extends
116      * Exception and has a the rollback element as false.
117      * @throws Exception if a lookup error occurs.
118      */

119     public abstract void createBeanApp() throws Exception JavaDoc;
120
121     /**
122      * Creates a bean that throws always an application exception that extends
123      * RuntimeException and has a the rollback element as false.
124      * @throws Exception if a lookup error occurs.
125      */

126     public abstract void createBeanApp01() throws Exception JavaDoc;
127
128     /**
129      * Creates a bean that throws always a application exception that extends
130      * RuntimeException and has a the rollback element as true.
131      * @throws Exception if a lookup error occurs.
132      */

133     public abstract void createBeanApp02() throws Exception JavaDoc;
134
135     /**
136      * Creates a bean that throws always an application exception that extends
137      * Exception and has a the rollback element as true.
138      * @throws Exception if a lookup error occurs.
139      */

140     public abstract void createBeanRollback() throws Exception JavaDoc;
141
142     /**
143      * Creates a bean that throws always a runtime exception.
144      * @throws Exception if a lookup error occurs.
145      */

146     public abstract void createBeanRuntime() throws Exception JavaDoc;
147
148     /**
149      * Gets a new instance of the bean used in the application exception (with
150      * rollback=false) tests.
151      * @param beanClass the bean class name used to make the lookup.
152      * @throws Exception if a lookup error occurs.
153      */

154     public void createBeanApp(final Class JavaDoc beanClass) throws Exception JavaDoc {
155         sfsbContainerTransactionApp = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class);
156     }
157
158     /**
159      * Gets a new instance of the bean used in the application exception that
160      * extends a runtime exception (with rollback=false) tests.
161      * @param beanClass the bean class name used to make the lookup.
162      * @throws Exception if a lookup error occurs.
163      */

164     public void createBeanApp01(final Class JavaDoc beanClass) throws Exception JavaDoc {
165         sfsbContainerTransactionApp01 = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class);
166     }
167
168     /**
169      * Gets a new instance of the bean used in the application exception that
170      * extends a runtime exception (with rollback=true) tests.
171      * @param beanClass the bean class name used to make the lookup.
172      * @throws Exception if a lookup error occurs.
173      */

174     public void createBeanApp02(final Class JavaDoc beanClass) throws Exception JavaDoc {
175         sfsbContainerTransactionApp02 = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class);
176     }
177
178     /**
179      * Gets a new instance of the bean used in the application exception (with
180      * rollback=true) tests.
181      * @param beanClass the bean class name used to make the lookup.
182      * @throws Exception if a lookup error occurs.
183      */

184     public void createBeanRollback(final Class JavaDoc beanClass) throws Exception JavaDoc {
185         sfsbContainerTransactionRollback = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class);
186     }
187
188     /**
189      * Gets a new instance of the bean used in the runtime exception tests.
190      * @param beanClass the bean class name used to make the lookup.
191      * @throws Exception if a lookup error occurs.
192      */

193     public void createBeanRuntime(final Class JavaDoc beanClass) throws Exception JavaDoc {
194         sfsbContainerTransactionRuntime = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class);
195     }
196
197     /**
198      * Verifies if the container re-throws the application exception that
199      * extends Exception (with rollback = true) and marks the transaction for
200      * rollback if there is transaction context.
201      * @throws Exception if an error during the tests occurs.
202      */

203     public void testNotUsingClientTransWithAppRollbackException() throws Exception JavaDoc {
204         try {
205             sfsbContainerTransactionRollback.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
206             fail("The container did not re-throw the application exception.");
207         } catch (RollbackApplicationException e) {
208             logger.debug("The bean threw an expected error during the execution {0}", e);
209         }
210         // verifies if the table was created in the DATABASE_1
211
ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
212     }
213
214     /**
215      * Verifies if the container re-throws the application exception that
216      * extends Exception(with rollback = true) and marks the transaction for
217      * rollback in some cases.This test uses a client transaction.
218      * @param canCommit true if the container does not need to mark the
219      * transaction as rollback(transaction attribute: NotSupported,
220      * Never). False if the container must mark the transaction for
221      * rollback(transaction attribute:Required, Mandatory and Supports).
222      * @throws Exception if an error during the tests occurs.
223      */

224     public void testUsingClientTransWithAppRollbackException(final boolean canCommit) throws Exception JavaDoc {
225         UserTransaction JavaDoc utx = ExceptionHandleUtil.getUserTransaction();
226         utx.begin();
227         try {
228             sfsbContainerTransactionRollback.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
229             fail("The container did not re-throw the application exception.");
230         } catch (RollbackApplicationException e) {
231             logger.debug("The bean threw an expected error during the execution {0}", e);
232         }
233         // tries to commit
234
try {
235             utx.commit();
236             if (!canCommit) {
237                 fail("The transaction was marked as rolled back, the client cannot make the commit.");
238             }
239         } catch (Exception JavaDoc e) {
240             logger.debug("The bean threw an expected error during the execution {0}", e);
241         }
242         // verifies if the table was created in the DATABASE_1
243
ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
244     }
245
246     /**
247      * Verifies if the container re-throws the application exception that
248      * extends Exception(with rollback = false) and does not do the rollback in
249      * this case.This test uses a client transaction.
250      * @throws Exception if an error during the tests occurs.
251      */

252     public void testUsingClientTransWithAppException() throws Exception JavaDoc {
253         UserTransaction JavaDoc utx = ExceptionHandleUtil.getUserTransaction();
254         utx.begin();
255         try {
256             sfsbContainerTransactionApp.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
257             fail("The container did not re-throw the application exception.");
258         } catch (AppException e) {
259             logger.debug("The bean threw an expected error during the execution {0}", e);
260         }
261         // tries to commit
262
try {
263             utx.commit();
264         } catch (Exception JavaDoc e) {
265             logger.debug("The bean threw an expected error during the execution {0}", e);
266         }
267         // verifies if the table was created in the DATABASE_1
268
ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
269     }
270
271     /**
272      * Verifies if the container re-throws the application exception that
273      * extends Exception(with rollback = false) and does not do the rollback in
274      * this case.
275      * @throws Exception if an error during the tests occurs.
276      */

277     public void testNotUsingClientTransWithAppException() throws Exception JavaDoc {
278         try {
279             sfsbContainerTransactionApp.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
280             fail("The container did not re-throw the application exception.");
281         } catch (AppException e) {
282             logger.debug("The bean threw an expected error during the execution {0}", e);
283         }
284         // verifies if the table was created in the DATABASE_1
285
ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
286     }
287
288     /**
289      * Verifies if the container re-throws the application exception that
290      * extends a runtime exception (with rollback = true) and marks the
291      * transaction for rollback in this case.
292      * @throws Exception if an error during the tests occurs.
293      */

294     public void testNotUsingClientTransWithAppRuntimeRollbackException() throws Exception JavaDoc {
295         try {
296             sfsbContainerTransactionApp02.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
297             fail("The container did not re-throw the application exception.");
298         } catch (RollbackAppRuntimeException e) {
299             logger.debug("The bean threw an expected error during the execution {0}", e);
300         }
301         // verifies if the table was created in the DATABASE_1
302
ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
303     }
304
305     /**
306      * Verifies if the container re-throws the application exception that
307      * extends a runtime exception (with rollback = true) and marks the
308      * transaction for rollback in this case. This test uses a client
309      * transaction.
310      * @param canCommit says if the bean is able to commit the client
311      * transaction. The client is able to commit when the bean does not
312      * use the client transaction.
313      * @throws Exception if an error during the tests occurs.
314      */

315     public void testUsingClientTransWithAppRuntimeRollbackException(final boolean canCommit) throws Exception JavaDoc {
316         UserTransaction JavaDoc utx = ExceptionHandleUtil.getUserTransaction();
317         utx.begin();
318         try {
319             sfsbContainerTransactionApp02.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
320             fail("The container did not re-throw the application exception.");
321         } catch (RollbackAppRuntimeException e) {
322             logger.debug("The bean threw an expected error during the execution {0}", e);
323         }
324         // tries to commit
325
try {
326             utx.commit();
327             if (!canCommit) {
328                 fail("The transaction was marked as rolled back, the client cannot make the commit.");
329             }
330         } catch (Exception JavaDoc e) {
331             logger.debug("The bean threw an expected error during the execution {0}", e);
332         }
333         // verifies if the table was created in the DATABASE_1
334
ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
335     }
336
337     /**
338      * Verifies if the container re-throws the application exception that
339      * extends a runtime exception (with rollback = false) and does not do the
340      * rollback in this case.
341      * @throws Exception if an error during the tests occurs.
342      */

343     public void testNotUsingClientTransWithAppRuntimeException() throws Exception JavaDoc {
344         try {
345             sfsbContainerTransactionApp01.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
346             fail("The container did not re-throw the application exception.");
347         } catch (AppRuntimeException e) {
348             logger.debug("The bean threw an expected error during the execution {0}", e);
349         }
350         // verifies if the table was created in the DATABASE_1
351
ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
352     }
353
354     /**
355      * Verifies if the container re-throws the application exception that
356      * extends a runtime exception (with rollback = false) and does not do the
357      * rollback in this case. The test uses a client transaction.
358      * @throws Exception if an error during the tests occurs.
359      */

360     public void testUsingClientTransWithAppRuntimeException() throws Exception JavaDoc {
361         UserTransaction JavaDoc utx = ExceptionHandleUtil.getUserTransaction();
362         utx.begin();
363         try {
364             sfsbContainerTransactionApp01.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
365             fail("The container did not re-throw the application exception.");
366         } catch (AppRuntimeException e) {
367             logger.debug("The bean threw an expected error during the execution {0}", e);
368         }
369         // tries to commit
370
try {
371             utx.commit();
372         } catch (Exception JavaDoc e) {
373             logger.debug("The bean threw an expected error during the execution {0}", e);
374         }
375         // verifies if the table was created in the DATABASE_1
376
ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
377     }
378
379     /**
380      * Verifies if the container throws the EJBException when the bean throws a
381      * RuntimeException, discards the bean and does the rollback in this case.
382      * @throws Exception if an error during the tests occurs.
383      */

384     public void testNotUsingClientTransWithRuntimeException() throws Exception JavaDoc {
385         // verifies if the container threw the correct exception
386
try {
387             sfsbContainerTransactionRuntime.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
388             fail("The container did not throw the EJBException.");
389         } catch (EJBException JavaDoc e) {
390             logger.debug("The bean threw an expected error during the execution {0}", e);
391         }
392
393         // verifies if the bean was discarded
394
assertTrue(ExceptionHandleUtil.isDiscarded(sfsbContainerTransactionRuntime),
395                 "There was a runtime exception and the container did not discarded the bean.");
396
397         // TODO - verifies if the container log the error.
398

399         // verifies if the table was created in the DATABASE_1
400
ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
401
402     }
403
404     /**
405      * Verifies if the container throws the correct exception (EJBException for
406      * RequiresNew and NotSupports, and EJBTransactionRolledbackException for
407      * Require, Supports and Mandatory) when the bean throws a RuntimeException,
408      * discards the bean and does the rollback if the bean uses the client transaction.
409      * @param expectedException EJBException for
410      * RequiresNew and NotSupports, and EJBTransactionRolledbackException for
411      * Require, Supports and Mandatory
412      * @param canCommit true if the bean does not use the client transaction.
413      * @throws Exception if an error during the tests occurs.
414      */

415     public void testUsingClientTransWithRuntimeException(final Class JavaDoc expectedException, final boolean canCommit)
416             throws Exception JavaDoc {
417         UserTransaction JavaDoc utx = ExceptionHandleUtil.getUserTransaction();
418         utx.begin();
419         // verifies if the exception thrown is correct
420
try {
421             sfsbContainerTransactionRuntime.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
422         } catch (Exception JavaDoc e) {
423             assertTrue(ExceptionHelper.isEquals(e, expectedException),
424                     "The container did not throw the correct exception, the expected exception is "
425                             + expectedException.getName() + ", but the container threw " + e.getClass().getName());
426         }
427         // tries to commit
428
try {
429             utx.commit();
430             if (!canCommit) {
431                 fail("The transaction was marked as rolled back, the client cannot make the commit.");
432             }
433         } catch (Exception JavaDoc e) {
434             logger.debug("The bean threw an expected error during the execution {0}", e);
435         }
436         // verifies if the bean was discarded
437
assertTrue(ExceptionHandleUtil.isDiscarded(sfsbContainerTransactionRuntime),
438                 "There was a runtime exception and the container did not discarded the bean.");
439         // verifies if the transaction was rolled back
440
if (!canCommit) {
441             try {
442                 // verifies if the table was created in the DATABASE_1
443
ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
444                 fail("There was an error during the commit and the transaction was not rolled back.");
445             } catch (SQLException JavaDoc e) {
446                 logger.debug("The bean threw an expected error during the execution {0}", e);
447             }
448         }
449
450         // TODO - verifies if the container log the error.
451

452     }
453
454     /**
455      * Deletes the tables created during test.
456      * @throws Exception if a lookup error occurs.
457      */

458     public void deleteTable() throws Exception JavaDoc {
459         // deletes the table after each test to avoid errors.
460
ExceptionHandleUtil.deleteTable(DATABASE_1, ItfContainerTransaction.TABLE);
461         ExceptionHandleUtil.deleteTable(DATABASE_2, ItfContainerTransaction.TABLE);
462     }
463
464 }
465
Popular Tags