001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: A_CatcherEntity.java 6769 2005-05-16 12:29:14Z benoitf $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.exception;
027:
028: import java.rmi.RemoteException;
029:
030: import javax.ejb.FinderException;
031: import javax.ejb.RemoveException;
032: import javax.transaction.RollbackException;
033: import javax.transaction.Status;
034: import javax.transaction.TransactionRolledbackException;
035:
036: import junit.framework.Assert;
037:
038: import org.objectweb.jonas.jtests.beans.beanexc.AccountE;
039: import org.objectweb.jonas.jtests.beans.beanexc.AccountEHome;
040: import org.objectweb.jonas.jtests.beans.beanexc.AccountPK;
041: import org.objectweb.jonas.jtests.beans.beanexc.AccountS;
042: import org.objectweb.jonas.jtests.beans.beanexc.AppException;
043:
044: public abstract class A_CatcherEntity extends A_Catcher {
045:
046: public A_CatcherEntity(String name) {
047: super (name);
048: }
049:
050: /**
051: * return an Account
052: */
053: public AccountE getAccount(int i) {
054: AccountE ac = null;
055: try {
056: ac = getHome().findByNoAccount(i);
057: } catch (FinderException e) {
058: fail("FinderException in getAccount :" + e);
059: } catch (RemoteException e) {
060: fail("RemoteException in getAccount :" + e);
061: }
062: return ac;
063: }
064:
065: /**
066: * return an AccountS
067: * must be defined for common tests (A_Catcher.java)
068: */
069: public AccountS getAccountS(int i) {
070: AccountS acs = null;
071: try {
072: acs = getHome().findByNoAccount(i);
073: } catch (FinderException e) {
074: fail("FinderException in getAccountS :" + e);
075: } catch (RemoteException e) {
076: fail("RemoteException in getAccountS :" + e);
077: }
078: return acs;
079: }
080:
081: /**
082: * return AccontHome, that can be either BMP or CMP bean.
083: */
084: public abstract AccountEHome getHome();
085:
086: // -----------------------------------------------------
087: // ApplicationException
088: // -----------------------------------------------------
089:
090: /**
091: * Test business method throwing an Application exception.
092: * It must be caught by the client.
093: * Methods runs in the context of the caller's transaction
094: * The business method has Mandatory attribute.
095: * Test that the transaction has not been marked rollback.
096: *
097: * Spec 2.0 page 375 (table15)
098: */
099: public void testApplicationCallerTx2() throws Exception {
100: AccountE ac = getAccount(87);
101: long oldbalance = ac.getBalance();
102: utx.begin();
103: try {
104: // the method add 5000 to the balance before throwing AppException
105: ac.doAppException_1();
106: fail("No AppException");
107: } catch (AppException e) {
108: assertTrue(utx.getStatus() == Status.STATUS_ACTIVE);
109: assertEquals("On AppException", 5000, ac.getBalance());
110: } finally {
111: utx.commit();
112: assertEquals("After commit", 5000, ac.getBalance());
113: }
114: }
115:
116: /**
117: * Test business method throwing an Application exception.
118: * It must be caught by the client.
119: * A transaction is started in the container. (RequiresNew)
120: * Bean does NOT call setRollbackOnly
121: * Test that the transaction has not been marked rollback.
122: *
123: * Spec 2.0 page 376 (table15)
124: */
125: public void testApplicationContTx2() throws Exception {
126: AccountE ac = getAccount(82);
127: long oldbalance = ac.getBalance();
128: try {
129: // the method add 50 to the balance
130: // the instance doesn't call setRollbackOnly
131: ac.doAppException_2(false);
132: fail("No AppException");
133: } catch (AppException e) {
134: assertEquals(50, ac.getBalance());
135: }
136: }
137:
138: /**
139: * Test business method throwing an Application exception.
140: * It must be caught by the client.
141: * A transaction is started in the container. (RequiresNew)
142: * Bean calls setRollbackOnly
143: * Test that the transaction has been rolledback.
144: *
145: * Spec 2.0 page 376 (table15)
146: */
147: public void testApplicationContTxRb2() throws Exception {
148: AccountE ac = getAccount(82);
149: long oldbalance = ac.getBalance();
150: try {
151: // the method add 50 to the balance
152: // the instance calls setRollbackOnly
153: ac.doAppException_2(true);
154: fail("No AppException");
155: } catch (AppException e) {
156: assertEquals(oldbalance, ac.getBalance());
157: }
158: }
159:
160: /**
161: * Test Create method throwing an Application exception.
162: * It must be caught by the client.
163: * Test that the transaction has not been marked rollback.
164: *
165: * equivalent to testApplicationContTx but the businessmethod is a create method
166: *
167: * Spec 2.0 page 376 (table15)
168: */
169: public void testApplicationHomeUserTx() throws Exception {
170: utx.begin();
171: try {
172: // throw AppException
173: getHome().create(true);
174: fail("No AppException");
175: } catch (AppException e) {
176: } finally {
177: utx.commit();
178: }
179: try {
180: getHome().findByNoAccount(1951);
181: fail("bean has been created");
182: } catch (FinderException e) {
183: }
184: }
185:
186: /**
187: * Test create method throwing an Application exception.
188: * It must be caught by the client.
189: * instance call setRollbackOnly
190: * The test verify the ac is not created
191: *
192: * equivalent to testApplicationContTxRb1 but for create method.
193: *
194: * Spec 2.0 page 376 (table15)
195: */
196: public void testApplicationHomeContTx() throws Exception {
197: try {
198: // throw AppException
199: getHome().create(0);
200: fail("No AppException");
201: } catch (AppException e) {
202: }
203: try {
204: getHome().findByNoAccount(1951);
205: fail("bean has been created");
206: } catch (FinderException e) {
207: }
208: }
209:
210: /**
211: * Test home method throwing an Application exception.
212: * It must be caught by the client.
213: *
214: * Spec 2.0 page 376 (table15)
215: */
216: public void testApplicationHomeNoTx() throws Exception {
217: try {
218: // throw AppException
219: // create(boolean) has the NotSupported trans. attribute.
220: // the instance is created with the pk 1951
221: getHome().create(true);
222: fail("No AppException");
223: } catch (AppException e) {
224: }
225: // No transaction involved -> the bean should be created.
226: // Actually, this is not the case. Why should it be created ?
227: // AccountE ac = getHome().findByNoAccount(1951);
228: // ac.remove();
229: }
230:
231: /**
232: * Test remove(pk) method throwing an Application exception (RemoveException).
233: * It must be caught by the client.
234: * A transaction is started in the container. (Required)
235: *
236: * Spec 2.0 page 376 (table15)
237: */
238: public void testApplicationRemovePkContTx() throws Exception {
239: int nac = 999990;
240: try {
241: AccountPK pk = new AccountPK(nac);
242: // throw AppException
243: getHome().remove(pk);
244: fail("No RemoveException");
245: } catch (RemoveException e) {
246: }
247: // Check if the account nac already exists and had correct values
248: AccountE ac = getAccount(nac);
249: Assert.assertEquals("For exception in ejbRemove", ac
250: .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 {
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 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
275: .getCustomer());
276: }
277:
278: /**
279: * Test remove() method throwing an Application exception (RemoveException).
280: * It must be caught by the client.
281: * A transaction is started in the container. (Required)
282: * equivalent to testApplicationRemoveHomeContTx but for obj.remove() method.
283: *
284: * Spec 2.0 page 376 (table15)
285: */
286: public void testApplicationRemoveThisContTx() throws Exception {
287: int nac = 999992;
288: AccountE ac;
289: try {
290: ac = getAccount(nac);
291: // throw AppException
292: ac.remove();
293: fail("No RemoveException");
294: } catch (RemoveException e) {
295: }
296: // Check if the account nac already exists and had correct values
297: ac = getAccount(nac);
298: Assert.assertEquals("For exception in ejbRemove", ac
299: .getCustomer());
300: }
301:
302: /**
303: * Test remove() method throwing an Application exception (RemoveException).
304: * It must be caught by the client.
305: * A transaction is started in the client. (Required)
306: * equivalent to testApplicationRemoveHomeUserTx but for obj.remove() method.
307: * BUG #100265
308: * Spec 2.0 page 400 (table15)
309: */
310: public void testApplicationRemoveThisUserTx() throws Exception {
311: int nac = 999993;
312: AccountE ac;
313: utx.begin();
314: try {
315: ac = getAccount(nac);
316: // throw AppException
317: ac.remove();
318: fail("No RemoveException");
319: } catch (RemoveException e) {
320: } finally {
321: utx.commit();
322: }
323: // Check if the account nac already exists and had correct values
324: ac = getAccount(nac);
325: Assert.assertEquals("For exception in ejbRemove", ac
326: .getCustomer());
327: }
328:
329: // -----------------------------------------------------
330: // RuntimeException
331: // -----------------------------------------------------
332:
333: /**
334: * Test RuntimeException in a container-invoked callback (ejbStore)
335: * The bean has the TransationRequired attribute.
336: * -> Exception must be logged, instance discarded, and the client should
337: * receive a RemoteException.
338: *
339: * See EJB2.0 specs 8.3.3 page 379
340: */
341: public void testRuntimeContTx() throws Exception {
342: AccountE ac = getAccount(81);
343: try {
344: ac.doFailedEjbStore_2();
345: fail("ejbStore should raise TransactionRolledbackException");
346: } catch (RemoteException e) {
347: assertTransactionRolledback(e);
348: }
349: }
350:
351: /**
352: * Test RuntimeException in a container-invoked callback (ejbStore)
353: * Methods runs in the context of the caller's transaction
354: * -> RollbackException should be received by the client on commit.
355: *
356: * See EJB2.0 specs 8.3.3 page 379
357: */
358: public void testRuntimeCallerTx() throws Exception {
359: AccountE ac = getAccount(81);
360: utx.begin();
361: ac.doFailedEjbStore_1();
362: try {
363: utx.commit();
364: fail("commit should raise RollbackException");
365: } catch (RollbackException e) {
366: }
367: }
368:
369: // -----------------------------------------------------
370: // EJBException
371: // -----------------------------------------------------
372:
373: /**
374: * Test business method throwing an EJBException Called inside a
375: * containertransaction (Requires).
376: * The client must receive RemoteException
377: * See EJB 2.1 specs page 400 (table 15).
378: * Test also if the instance has been discarded, ie means that the container
379: * must not invoke any bussiness methods or container callbacks on this instance.
380: * To reproduce the bug #300421.
381: */
382: public void testDestroyedAfterEJBContTx() throws Exception {
383: AccountS ac = getAccountS(101);
384: try {
385: ac.doEJBException_2();
386: fail("No RemoteException");
387: } catch (RemoteException e) {
388: }
389: assertFalse("instance hasn't been destroyed", ac.iAmDestroyed());
390: }
391:
392: /**
393: * Test business method throwing an EJBException Called inside a
394: * client transaction (Supports).
395: * The client must receive RemoteException
396: * See EJB 2.1 specs page 400 (table 15).
397: * Test also if the instance has been discarded, ie means that the container
398: * must not invoke any bussiness methods or container callbacks on this instance.
399: * To reproduce the bug #300421.
400: */
401: public void testDestroyedAfterEJBUserTx() throws Exception {
402: AccountS ac = getAccountS(101);
403: utx.begin();
404: try {
405: try {
406: ac.doEJBException_sup();
407: fail("No RemoteException");
408: } catch (RemoteException e) {
409: assertTransactionRolledback(e);
410: }
411: try {
412: assertTrue("instance should be destroyed", ac
413: .iAmDestroyed());
414: fail("business method on discarded instance succeeded");
415: } catch (RemoteException e) {
416: }
417: } finally {
418: utx.rollback();
419: }
420: }
421:
422: /**
423: * Test business method throwing an EJBException Called outside transaction.
424: * The client must receive RemoteException
425: * See EJB 2.1 specs page 401 (table 15).
426: * Test also if the instance has been discarded, ie means that the container
427: * must not invoke any bussiness methods or container callbacks on this instance.
428: * To reproduce the bug #300421.
429: */
430: public void testDestroyedAfterEJBNoTx() throws Exception {
431: AccountS ac = getAccountS(101);
432: try {
433: ac.doEJBException_sup();
434: fail("No RemoteException");
435: } catch (RemoteException e) {
436: }
437: assertFalse("instance hasn't been destroyed", ac.iAmDestroyed());
438: }
439:
440: public void testDiscardedInstances() throws Exception {
441: AccountS ac = getAccountS(101);
442: utx.begin();
443: try {
444: try {
445: ac.doEJBException_sup();
446: fail("No RemoteException");
447: } catch (RemoteException e) {
448: assertTransactionRolledback(e);
449: }
450: try {
451: assertTrue("instance should be destroyed", ac
452: .iAmDestroyed());
453: fail("business method on discarded instance succeeded");
454: } catch (RemoteException e) {
455: }
456: } finally {
457: utx.rollback();
458: }
459: try {
460: ac.doEJBException_2();
461: fail("No RemoteException");
462: } catch (RemoteException e) {
463: }
464: assertFalse("instance hasn't been destroyed (cont tx)", ac
465: .iAmDestroyed());
466: }
467:
468: // -----------------------------------------------------
469: // UncheckedException
470: // -----------------------------------------------------
471:
472: /**
473: * Test ejbCreate method throwing an unchecked exception.
474: * Called inside a containertransaction.
475: * The client must receive RemoteException
476: *
477: * See EJB2.0 specs page 376 (table 15)
478: */
479: public void testUncheckedHomeContTx() throws Exception {
480: try {
481: getHome().create(1); // force UncheckedException
482: fail("No RemoteException");
483: } catch (RemoteException e) {
484: }
485: // check rollback was effective.
486: try {
487: AccountE ac = getHome().findByNoAccount(1951);
488: fail("rollback should have removed bean");
489: } catch (FinderException e) {
490: }
491: }
492:
493: /**
494: * Test ejbCreate method throwing an unchecked exception.
495: * Methods runs in the context of the caller's transaction
496: * A transaction has been started by the client.
497: * Called inside a client transaction.
498: * TransactionRolledbackException must be caught by the client.
499: * The business method has Mandatory attribute.
500: *
501: * See EJB2.0 specs page 375 (table 15)
502: */
503: public void testUncheckedHomeUserTx() throws Exception {
504: utx.begin();
505: try {
506: getHome().create(1); // force UncheckedException
507: fail("No RemoteException");
508: } catch (RemoteException e) {
509: assertTransactionRolledback(e);
510: } finally {
511: utx.rollback();
512: }
513: // check rollback was effective.
514: try {
515: getHome().findByNoAccount(1951);
516: fail("rollback should have removed bean");
517: } catch (FinderException e) {
518: }
519: }
520:
521: private void assertTransactionRolledback(RemoteException e)
522: throws RemoteException {
523: // throw exception if not TransactionRolledBack exception or if inner exception is not TransactionRolledbackException
524: if ((!(e instanceof java.rmi.ServerException) || !(e.detail instanceof TransactionRolledbackException))
525: && !(e instanceof TransactionRolledbackException)) {
526: throw e;
527: }
528: }
529: }
|