KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > exception > A_CatcherEntity


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@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: A_CatcherEntity.java,v 1.20 2005/05/16 12:29:14 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.exception;
27
28 import java.rmi.RemoteException JavaDoc;
29
30 import javax.ejb.FinderException JavaDoc;
31 import javax.ejb.RemoveException JavaDoc;
32 import javax.transaction.RollbackException JavaDoc;
33 import javax.transaction.Status JavaDoc;
34 import javax.transaction.TransactionRolledbackException JavaDoc;
35
36 import junit.framework.Assert;
37
38 import org.objectweb.jonas.jtests.beans.beanexc.AccountE;
39 import org.objectweb.jonas.jtests.beans.beanexc.AccountEHome;
40 import org.objectweb.jonas.jtests.beans.beanexc.AccountPK;
41 import org.objectweb.jonas.jtests.beans.beanexc.AccountS;
42 import org.objectweb.jonas.jtests.beans.beanexc.AppException;
43
44 public abstract class A_CatcherEntity extends A_Catcher {
45
46     public A_CatcherEntity(String JavaDoc name) {
47         super(name);
48     }
49
50     /**
51      * return an Account
52      */

53     public AccountE getAccount(int i) {
54         AccountE ac = null;
55         try {
56             ac = getHome().findByNoAccount(i);
57         } catch (FinderException JavaDoc e) {
58             fail("FinderException in getAccount :" + e);
59         } catch (RemoteException JavaDoc e) {
60             fail("RemoteException in getAccount :" + e);
61         }
62         return ac;
63     }
64
65     /**
66      * return an AccountS
67      * must be defined for common tests (A_Catcher.java)
68      */

69     public AccountS getAccountS(int i) {
70         AccountS acs = null;
71         try {
72             acs = getHome().findByNoAccount(i);
73         } catch (FinderException JavaDoc e) {
74             fail("FinderException in getAccountS :" + e);
75         } catch (RemoteException JavaDoc e) {
76             fail("RemoteException in getAccountS :" + e);
77         }
78         return acs;
79     }
80
81     /**
82      * return AccontHome, that can be either BMP or CMP bean.
83      */

84     public abstract AccountEHome getHome();
85
86
87     // -----------------------------------------------------
88
// ApplicationException
89
// -----------------------------------------------------
90

91     /**
92      * Test business method throwing an Application exception.
93      * It must be caught by the client.
94      * Methods runs in the context of the caller's transaction
95      * The business method has Mandatory attribute.
96      * Test that the transaction has not been marked rollback.
97      *
98      * Spec 2.0 page 375 (table15)
99      */

100     public void testApplicationCallerTx2() throws Exception JavaDoc {
101         AccountE ac = getAccount(87);
102         long oldbalance = ac.getBalance();
103         utx.begin();
104         try {
105             // the method add 5000 to the balance before throwing AppException
106
ac.doAppException_1();
107             fail("No AppException");
108         } catch (AppException e) {
109             assertTrue(utx.getStatus() == Status.STATUS_ACTIVE);
110             assertEquals("On AppException", 5000, ac.getBalance());
111         } finally {
112             utx.commit();
113             assertEquals("After commit", 5000, ac.getBalance());
114         }
115     }
116
117     /**
118      * Test business method throwing an Application exception.
119      * It must be caught by the client.
120      * A transaction is started in the container. (RequiresNew)
121      * Bean does NOT call setRollbackOnly
122      * Test that the transaction has not been marked rollback.
123      *
124      * Spec 2.0 page 376 (table15)
125      */

126     public void testApplicationContTx2() throws Exception JavaDoc {
127         AccountE ac = getAccount(82);
128         long oldbalance = ac.getBalance();
129         try {
130             // the method add 50 to the balance
131
// the instance doesn't call setRollbackOnly
132
ac.doAppException_2(false);
133             fail("No AppException");
134         } catch (AppException e) {
135             assertEquals(50, ac.getBalance());
136         }
137     }
138
139     /**
140      * Test business method throwing an Application exception.
141      * It must be caught by the client.
142      * A transaction is started in the container. (RequiresNew)
143      * Bean calls setRollbackOnly
144      * Test that the transaction has been rolledback.
145      *
146      * Spec 2.0 page 376 (table15)
147      */

148     public void testApplicationContTxRb2() throws Exception JavaDoc {
149         AccountE ac = getAccount(82);
150         long oldbalance = ac.getBalance();
151         try {
152             // the method add 50 to the balance
153
// the instance calls setRollbackOnly
154
ac.doAppException_2(true);
155             fail("No AppException");
156         } catch (AppException e) {
157             assertEquals(oldbalance, ac.getBalance());
158         }
159     }
160
161     /**
162      * Test Create method throwing an Application exception.
163      * It must be caught by the client.
164      * Test that the transaction has not been marked rollback.
165      *
166      * equivalent to testApplicationContTx but the businessmethod is a create method
167      *
168      * Spec 2.0 page 376 (table15)
169      */

170     public void testApplicationHomeUserTx() throws Exception JavaDoc {
171         utx.begin();
172         try {
173             // throw AppException
174
getHome().create(true);
175             fail("No AppException");
176         } catch (AppException e) {
177         } finally {
178             utx.commit();
179         }
180         try {
181             getHome().findByNoAccount(1951);
182             fail("bean has been created");
183         } catch (FinderException JavaDoc e) {
184         }
185     }
186
187     /**
188      * Test create method throwing an Application exception.
189      * It must be caught by the client.
190      * instance call setRollbackOnly
191      * The test verify the ac is not created
192      *
193      * equivalent to testApplicationContTxRb1 but for create method.
194      *
195      * Spec 2.0 page 376 (table15)
196      */

197     public void testApplicationHomeContTx() throws Exception JavaDoc {
198         try {
199             // throw AppException
200
getHome().create(0);
201             fail("No AppException");
202         } catch (AppException e) {
203         }
204         try {
205             getHome().findByNoAccount(1951);
206             fail("bean has been created");
207         } catch (FinderException JavaDoc e) {
208         }
209     }
210
211     /**
212      * Test home method throwing an Application exception.
213      * It must be caught by the client.
214      *
215      * Spec 2.0 page 376 (table15)
216      */

217     public void testApplicationHomeNoTx() throws Exception JavaDoc {
218         try {
219             // throw AppException
220
// create(boolean) has the NotSupported trans. attribute.
221
// the instance is created with the pk 1951
222
getHome().create(true);
223             fail("No AppException");
224         } catch (AppException e) {
225         }
226         // No transaction involved -> the bean should be created.
227
// Actually, this is not the case. Why should it be created ?
228
// AccountE ac = getHome().findByNoAccount(1951);
229
// ac.remove();
230
}
231
232     /**
233      * Test remove(pk) method throwing an Application exception (RemoveException).
234      * It must be caught by the client.
235      * A transaction is started in the container. (Required)
236      *
237      * Spec 2.0 page 376 (table15)
238      */

239     public void testApplicationRemovePkContTx() throws Exception JavaDoc {
240         int nac = 999990;
241         try {
242             AccountPK pk = new AccountPK(nac);
243             // throw AppException
244
getHome().remove(pk);
245             fail("No RemoveException");
246         } catch (RemoveException JavaDoc e) {
247         }
248         // Check if the account nac already exists and had correct values
249
AccountE ac = getAccount(nac);
250         Assert.assertEquals("For exception in ejbRemove", ac.getCustomer());
251     }
252
253     /**
254      * Test remove(pk) method throwing an Application exception (RemoveException).
255      * It must be caught by the client.
256      * A transaction is started in the client. (Required)
257      * bug #100265
258      * Spec 2.1 page 400 (table15)
259      */

260     public void testApplicationRemovePkUserTx() throws Exception JavaDoc {
261         int nac = 999991;
262         utx.begin();
263         try {
264             AccountPK pk = new AccountPK(nac);
265             // throw AppException
266
getHome().remove(pk);
267             fail("No RemoveException");
268         } catch (RemoveException JavaDoc e) {
269         } finally {
270             utx.commit();
271         }
272         // Check if the account nac already exists and had correct values
273
AccountE ac = getAccount(nac);
274         Assert.assertEquals("For exception in ejbRemove", ac.getCustomer());
275     }
276
277     /**
278      * Test remove() method throwing an Application exception (RemoveException).
279      * It must be caught by the client.
280      * A transaction is started in the container. (Required)
281      * equivalent to testApplicationRemoveHomeContTx but for obj.remove() method.
282      *
283      * Spec 2.0 page 376 (table15)
284      */

285     public void testApplicationRemoveThisContTx() throws Exception JavaDoc {
286         int nac = 999992;
287         AccountE ac;
288         try {
289             ac = getAccount(nac);
290             // throw AppException
291
ac.remove();
292             fail("No RemoveException");
293         } catch (RemoveException JavaDoc e) {
294         }
295         // Check if the account nac already exists and had correct values
296
ac = getAccount(nac);
297         Assert.assertEquals("For exception in ejbRemove", ac.getCustomer());
298     }
299
300     /**
301      * Test remove() method throwing an Application exception (RemoveException).
302      * It must be caught by the client.
303      * A transaction is started in the client. (Required)
304      * equivalent to testApplicationRemoveHomeUserTx but for obj.remove() method.
305      * BUG #100265
306      * Spec 2.0 page 400 (table15)
307      */

308     public void testApplicationRemoveThisUserTx() throws Exception JavaDoc {
309         int nac = 999993;
310         AccountE ac;
311         utx.begin();
312         try {
313             ac = getAccount(nac);
314             // throw AppException
315
ac.remove();
316             fail("No RemoveException");
317         } catch (RemoveException JavaDoc e) {
318         } finally {
319             utx.commit();
320         }
321         // Check if the account nac already exists and had correct values
322
ac = getAccount(nac);
323         Assert.assertEquals("For exception in ejbRemove", ac.getCustomer());
324     }
325
326     // -----------------------------------------------------
327
// RuntimeException
328
// -----------------------------------------------------
329

330     /**
331      * Test RuntimeException in a container-invoked callback (ejbStore)
332      * The bean has the TransationRequired attribute.
333      * -> Exception must be logged, instance discarded, and the client should
334      * receive a RemoteException.
335      *
336      * See EJB2.0 specs 8.3.3 page 379
337      */

338     public void testRuntimeContTx() throws Exception JavaDoc {
339         AccountE ac = getAccount(81);
340         try {
341             ac.doFailedEjbStore_2();
342             fail("ejbStore should raise TransactionRolledbackException");
343         } catch (RemoteException JavaDoc e) {
344             assertTransactionRolledback(e);
345         }
346     }
347
348     /**
349      * Test RuntimeException in a container-invoked callback (ejbStore)
350      * Methods runs in the context of the caller's transaction
351      * -> RollbackException should be received by the client on commit.
352      *
353      * See EJB2.0 specs 8.3.3 page 379
354      */

355     public void testRuntimeCallerTx() throws Exception JavaDoc {
356         AccountE ac = getAccount(81);
357         utx.begin();
358         ac.doFailedEjbStore_1();
359         try {
360             utx.commit();
361             fail("commit should raise RollbackException");
362         } catch (RollbackException JavaDoc e) {
363         }
364     }
365
366     // -----------------------------------------------------
367
// EJBException
368
// -----------------------------------------------------
369

370     /**
371      * Test business method throwing an EJBException Called inside a
372      * containertransaction (Requires).
373      * The client must receive RemoteException
374      * See EJB 2.1 specs page 400 (table 15).
375      * Test also if the instance has been discarded, ie means that the container
376      * must not invoke any bussiness methods or container callbacks on this instance.
377      * To reproduce the bug #300421.
378      */

379     public void testDestroyedAfterEJBContTx() throws Exception JavaDoc {
380         AccountS ac = getAccountS(101);
381         try {
382             ac.doEJBException_2();
383             fail("No RemoteException");
384         } catch (RemoteException JavaDoc e) {
385         }
386         assertFalse("instance hasn't been destroyed", ac.iAmDestroyed());
387     }
388
389     /**
390      * Test business method throwing an EJBException Called inside a
391      * client transaction (Supports).
392      * The client must receive RemoteException
393      * See EJB 2.1 specs page 400 (table 15).
394      * Test also if the instance has been discarded, ie means that the container
395      * must not invoke any bussiness methods or container callbacks on this instance.
396      * To reproduce the bug #300421.
397      */

398     public void testDestroyedAfterEJBUserTx() throws Exception JavaDoc {
399         AccountS ac = getAccountS(101);
400         utx.begin();
401         try {
402             try {
403                 ac.doEJBException_sup();
404                 fail("No RemoteException");
405             } catch (RemoteException JavaDoc e) {
406                 assertTransactionRolledback(e);
407             }
408             try {
409                 assertTrue("instance should be destroyed", ac.iAmDestroyed());
410                 fail("business method on discarded instance succeeded");
411             } catch (RemoteException JavaDoc e) {
412             }
413         } finally {
414             utx.rollback();
415         }
416     }
417
418     /**
419      * Test business method throwing an EJBException Called outside transaction.
420      * The client must receive RemoteException
421      * See EJB 2.1 specs page 401 (table 15).
422      * Test also if the instance has been discarded, ie means that the container
423      * must not invoke any bussiness methods or container callbacks on this instance.
424      * To reproduce the bug #300421.
425      */

426     public void testDestroyedAfterEJBNoTx() throws Exception JavaDoc {
427         AccountS ac = getAccountS(101);
428         try {
429             ac.doEJBException_sup();
430             fail("No RemoteException");
431         } catch (RemoteException JavaDoc e) {
432         }
433         assertFalse("instance hasn't been destroyed", ac.iAmDestroyed());
434     }
435
436     public void testDiscardedInstances() throws Exception JavaDoc {
437         AccountS ac = getAccountS(101);
438         utx.begin();
439         try {
440             try {
441                 ac.doEJBException_sup();
442                 fail("No RemoteException");
443             } catch (RemoteException JavaDoc e) {
444                 assertTransactionRolledback(e);
445             }
446             try {
447                 assertTrue("instance should be destroyed", ac.iAmDestroyed());
448                 fail("business method on discarded instance succeeded");
449             } catch (RemoteException JavaDoc e) {
450             }
451         } finally {
452             utx.rollback();
453         }
454         try {
455             ac.doEJBException_2();
456             fail("No RemoteException");
457         } catch (RemoteException JavaDoc e) {
458         }
459         assertFalse("instance hasn't been destroyed (cont tx)", ac.iAmDestroyed());
460     }
461
462     // -----------------------------------------------------
463
// UncheckedException
464
// -----------------------------------------------------
465

466     /**
467      * Test ejbCreate method throwing an unchecked exception.
468      * Called inside a containertransaction.
469      * The client must receive RemoteException
470      *
471      * See EJB2.0 specs page 376 (table 15)
472      */

473     public void testUncheckedHomeContTx() throws Exception JavaDoc {
474         try {
475             getHome().create(1); // force UncheckedException
476
fail("No RemoteException");
477         } catch (RemoteException JavaDoc e) {
478         }
479         // check rollback was effective.
480
try {
481             AccountE ac = getHome().findByNoAccount(1951);
482             fail("rollback should have removed bean");
483         } catch (FinderException JavaDoc e) {
484         }
485     }
486
487     /**
488      * Test ejbCreate method throwing an unchecked exception.
489      * Methods runs in the context of the caller's transaction
490      * A transaction has been started by the client.
491      * Called inside a client transaction.
492      * TransactionRolledbackException must be caught by the client.
493      * The business method has Mandatory attribute.
494      *
495      * See EJB2.0 specs page 375 (table 15)
496      */

497     public void testUncheckedHomeUserTx() throws Exception JavaDoc {
498         utx.begin();
499         try {
500             getHome().create(1); // force UncheckedException
501
fail("No RemoteException");
502         } catch (RemoteException JavaDoc e) {
503             assertTransactionRolledback(e);
504         } finally {
505             utx.rollback();
506         }
507         // check rollback was effective.
508
try {
509             getHome().findByNoAccount(1951);
510             fail("rollback should have removed bean");
511         } catch (FinderException JavaDoc e) {
512         }
513     }
514
515     private void assertTransactionRolledback(RemoteException JavaDoc e)
516         throws RemoteException JavaDoc {
517         // throw exception if not TransactionRolledBack exception or if inner exception is not TransactionRolledbackException
518
if ((!(e instanceof java.rmi.ServerException JavaDoc)
519             || !(e.detail instanceof TransactionRolledbackException JavaDoc)) && !(e instanceof TransactionRolledbackException JavaDoc)) {
520             throw e;
521         }
522     }
523 }
524
Popular Tags