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_AdvancedHomeEC.java 10103 2007-03-28 08:07:47Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.entity;
027:
028: import java.rmi.RemoteException;
029: import java.util.Enumeration;
030:
031: import javax.ejb.DuplicateKeyException;
032: import javax.ejb.EJBException;
033: import javax.ejb.FinderException;
034: import javax.ejb.Handle;
035: import javax.ejb.RemoveException;
036: import javax.rmi.PortableRemoteObject;
037: import javax.transaction.RollbackException;
038:
039: import org.objectweb.jonas.jtests.beans.annuaire.Personne;
040: import org.objectweb.jonas.jtests.beans.annuaire.PersonneHome;
041: import org.objectweb.jonas.jtests.util.JTestCase;
042:
043: /**
044: * This is an advanced test suite for home interface on entity bean CMP.
045: * Beans used: annuaire
046: * @author Philippe Coq, Philippe Durieux, Helene Joanin (jonas team)
047: */
048: public abstract class A_AdvancedHomeEC extends JTestCase {
049:
050: String mynum = "638";
051: String myname = "Philippe Durieux";
052:
053: public A_AdvancedHomeEC(String name) {
054: super (name);
055: }
056:
057: protected void setUp() {
058: super .setUp();
059: }
060:
061: /**
062: * return PersonneHome, that can be either CMP v1 or CMP v2 bean.
063: */
064: abstract public PersonneHome getHome();
065:
066: /**
067: * test du findAll, returning an Enumeration.
068: * Some of the beans are already in the database, some other are created.
069: */
070: public void testFindAllEnum() throws Exception {
071: Enumeration e = null;
072: e = getHome().findAll();
073: int count = 0;
074: while (e.hasMoreElements()) {
075: count++;
076: Personne p = (Personne) PortableRemoteObject.narrow(e
077: .nextElement(), Personne.class);
078: }
079: assertEquals("Wrong number of bean (case 1)", 10, count);
080: Personne p1 = getHome().create("Paul Morgan", "1234256");
081: Personne p2 = getHome().create("Jean Richard", "1234356");
082: Personne p3 = getHome().create("Jean-Paul Landry", "1234556");
083: e = getHome().findAll();
084: count = 0;
085: while (e.hasMoreElements()) {
086: count++;
087: Personne p = (Personne) PortableRemoteObject.narrow(e
088: .nextElement(), Personne.class);
089: }
090: assertEquals("Wrong number of bean (case 2)", 13, count);
091: // cleaning
092: p1.remove();
093: p2.remove();
094: p3.remove();
095: e = getHome().findAll();
096: count = 0;
097: while (e.hasMoreElements()) {
098: count++;
099: Personne p = (Personne) PortableRemoteObject.narrow(e
100: .nextElement(), Personne.class);
101: }
102: assertEquals("Wrong number of bean (case 3)", 10, count);
103: }
104:
105: /**
106: * test Create inside a Transaction.
107: */
108: public void testCreateInTx() throws Exception {
109: utx.begin();
110: try {
111: getHome().create("Duke Ellington", "12235446");
112: utx.commit();
113: Personne p = getHome().findByNom("Duke Ellington");
114: p.remove();
115: utx.begin();
116: getHome().create("Louis Armstrong", "12035446");
117: } catch (Exception e) {
118: fail(e.getMessage());
119: } finally {
120: utx.rollback();
121: }
122: try {
123: getHome().findByNom("Louis Armstrong");
124: fail("Should not find it");
125: } catch (FinderException e) {
126: }
127: }
128:
129: /**
130: * test a simple Create inside a Transaction.
131: */
132: public void testSimpleCreateInTx() throws Exception {
133: utx.begin();
134: try {
135: getHome().create("Mike", "12235846");
136: } catch (Exception e) {
137: fail(e.getMessage());
138: } finally {
139: utx.commit();
140: }
141: Personne p = getHome().findByNom("Mike");
142: p.remove();
143: }
144:
145: /**
146: * test a simple Remove
147: * Find an instance already in database and remove it.
148: */
149: public void testSimpleRemove() throws Exception {
150: Personne p = getHome().findByNom("Adriana Danes");
151: p.remove();
152: getHome().create("Adriana Danes", "777"); // cleaning
153: }
154:
155: /**
156: * test remove twice
157: */
158: public void testRemoveTwice() throws Exception {
159: Personne p = getHome().create("Andre", "77710"); // first create a new element
160: p.remove();
161: try {
162: p.remove();
163: fail("Should not be able to remove this object twice");
164: } catch (RemoteException e) {
165: // OK
166: } catch (Exception e) {
167: fail("Bad exception raised: " + e);
168: }
169: }
170:
171: /**
172: * test remove twice in same transaction
173: */
174: public void testRemoveTwiceTx() throws Exception {
175: Personne p = getHome().create("Andre", "77710"); // first create a new element
176: utx.begin();
177: try {
178: p.remove();
179: p.remove();
180: fail("Should not be able to remove this object twice");
181: } catch (RemoteException e) {
182: // OK: normal case because 2nd remove must fail
183: } catch (Exception e) {
184: fail("Bad exception raised: " + e);
185: } finally {
186: try {
187: utx.commit();
188: } catch (RollbackException e) {
189: // Possibly go here, since 2nd remove failed.
190: // clean object (1st remove has been rolled back!)
191: // Don't use p because instance should have been discarded.
192: getHome().remove("Andre");
193: }
194: }
195: }
196:
197: /**
198: * Test Create + Business Method inside the same Tx
199: */
200: public void testCreateAndBusiness() throws Exception {
201: String num = "00000001";
202: Personne p = null;
203: utx.begin();
204: try {
205: p = getHome().create("Lionel Hampton", "92235446");
206: p.setNumero(num);
207: } catch (Exception e) {
208: fail(e.getMessage());
209: } finally {
210: utx.commit();
211: }
212: assertTrue(num.equals(p.getNumero()));
213: p.remove(); // cleaning
214: }
215:
216: /**
217: * Test remove(pk): create / find / remove(pk) / find
218: */
219: public void testRemovePk() throws Exception {
220: Personne p = null;
221: String newman = "Xavier Spengler";
222: p = getHome().create(newman, "6");
223: getHome().findByPrimaryKey(newman);
224: getHome().remove(newman);
225: try {
226: getHome().findByPrimaryKey(newman);
227: fail("Should not exist anymore");
228: } catch (FinderException e) {
229: }
230: }
231:
232: /**
233: * Test remove(handle): create / find / remove(handle) / find
234: * CMP2: bug #300419
235: */
236: public void testRemoveHandle() throws Exception {
237: Personne p = null;
238: String newman = "Xavier Spengler";
239: p = getHome().create(newman, "6");
240: Handle h = p.getHandle();
241: getHome().remove(h);
242: try {
243: getHome().findByPrimaryKey(newman);
244: fail("Should not exist anymore");
245: } catch (FinderException e) {
246: }
247: }
248:
249: /**
250: * Test remove(pk) inside transaction:
251: * create / find / remove(pk) / find
252: */
253: public void testRemovePkInTx() throws Exception {
254: Personne p = null;
255: String newman = "Xavier Spengler";
256: p = getHome().create(newman, "6");
257: getHome().findByPrimaryKey(newman);
258: utx.begin();
259: try {
260: getHome().remove(newman);
261: } catch (Exception e) {
262: fail(e.getMessage());
263: } finally {
264: utx.commit();
265: }
266: try {
267: getHome().findByPrimaryKey(newman);
268: fail("Should not exist anymore");
269: } catch (FinderException e) {
270: }
271: }
272:
273: /**
274: * Test remove(pk) of a non-existent pk.
275: */
276: public void testRemovePkNonExistent() throws Exception {
277: String man = "NonExistent";
278: try {
279: getHome().remove(man);
280: fail("No RemoteException");
281: } catch (RemoteException e) {
282: }
283: }
284:
285: /**
286: * Test remove(pk) of a non-existent pk inside transaction.
287: * See ejb2.1 18.3.1
288: */
289: public void testRemovePkNonExistentInTx() throws Exception {
290: String man = "NonExistent";
291: utx.begin();
292: try {
293: getHome().remove(man);
294: fail("No RemoteException");
295: } catch (RemoteException e) {
296: } finally {
297: try {
298: utx.commit();
299: fail("transaction should be marked as rollback");
300: } catch (RollbackException e) {
301: }
302: }
303: }
304:
305: /**
306: * Test bean.remove :
307: * create / find / remove / find
308: */
309: public void testRemove() throws Exception {
310: Personne p = null;
311: String newman = "Jeannot";
312: p = getHome().create(newman, "5");
313: getHome().findByPrimaryKey(newman);
314: p.remove();
315: try {
316: getHome().findByPrimaryKey(newman);
317: fail("Should not exist anymore");
318: } catch (FinderException e) {
319: }
320: }
321:
322: /**
323: * Test bean.remove :
324: * create / find / remove / bussiness method
325: * Ensure the javax.ejb.EJBException is thrown when trying
326: * to invoke an accessor method on a deleted entitybean object
327: */
328: public void testRemove2() throws Exception {
329: Personne p = null;
330: String newman = "Jeannot2";
331: p = getHome().create(newman, "5");
332: getHome().findByPrimaryKey(newman);
333: p.remove();
334: try {
335: p.getNumero();
336: fail("Should not exist anymore");
337: } catch (Exception e) {
338: }
339: }
340:
341: /**
342: * Test remove by PK in a tx rolled back
343: * we modify the bean instance outside tx, then we check that
344: * the bean state has not been changed by the rolled back remove
345: */
346: public void testRemovePKRB() throws Exception {
347: String newnum = "344";
348: String name = "remove-by-pk";
349: Personne p = getHome().create(name, "420");
350: p.setNumero(newnum);
351: utx.begin();
352: getHome().remove(name);
353: utx.rollback();
354: try {
355: assertEquals("Lost modified value", newnum, p.getNumero());
356: } finally {
357: // cleaning
358: getHome().remove(name);
359: }
360: }
361:
362: /**
363: * Test remove in a tx rolled back
364: * we modify the bean instance outside tx, then we check that
365: * the bean state has not been changed by the rolled back remove
366: */
367: public void testRemoveRB() throws Exception {
368: String newnum = "345";
369: String name = "remove-instance";
370: Personne p = getHome().create(name, "421");
371: p.setNumero(newnum);
372: utx.begin();
373: p.remove();
374: utx.rollback();
375: try {
376: assertEquals("Lost modified value", newnum, p.getNumero());
377: } finally {
378: // cleaning
379: //p.remove();
380: getHome().remove(name);
381: }
382: }
383:
384: /**
385: * Test bean.remove inside a tx:
386: * create / find / remove / find
387: */
388: public void testRemoveInTx() throws Exception {
389: Personne p = null;
390: String newman = "Jeannot";
391: p = getHome().create(newman, "5");
392: getHome().findByPrimaryKey(newman);
393: utx.begin();
394: try {
395: p.remove();
396: } catch (Exception e) {
397: fail(e.getMessage());
398: } finally {
399: utx.commit();
400: }
401: try {
402: getHome().findByPrimaryKey(newman);
403: fail("Should not exist anymore");
404: } catch (FinderException e) {
405: }
406: }
407:
408: /**
409: * Test finder in transactions
410: */
411: public void testFinderInTx() throws Exception {
412: Personne p = null;
413:
414: // FindByPrimaryKey
415: for (int i = 0; i < 5; i++) {
416: utx.begin();
417: try {
418: p = getHome().findByPrimaryKey(myname);
419: } catch (Exception e) {
420: fail(e.getMessage());
421: } finally {
422: utx.commit();
423: }
424: }
425: assertTrue(p.getNumero().equals(mynum));
426:
427: // FindByNum
428: for (int i = 0; i < 5; i++) {
429: utx.begin();
430: try {
431: p = getHome().findByNom(myname);
432: } catch (Exception e) {
433: fail(e.getMessage());
434: } finally {
435: utx.commit();
436: }
437: }
438: assertTrue(p.getNumero().equals(mynum));
439:
440: // FindAll
441: for (int i = 0; i < 5; i++) {
442: utx.begin();
443: try {
444: getHome().findAll();
445: } catch (Exception e) {
446: fail(e.getMessage());
447: } finally {
448: utx.commit();
449: }
450: }
451: }
452:
453: /**
454: * Test create + remove in transactions
455: */
456: public void testCreateRemoveInTx() throws Exception {
457: for (int i = 0; i < 20; i++) {
458: utx.begin();
459: try {
460: getHome().create("Eric Paire", "500");
461: getHome().remove("Eric Paire");
462: } catch (Exception e) {
463: fail(e.getMessage());
464: } finally {
465: utx.commit();
466: }
467: }
468: }
469:
470: /**
471: * test 2 findByPrimaryKey in the same tx, with an update
472: * between the 2 calls. We shall see the modifications, even before
473: * the commit occurs.
474: */
475: public void testFindTwice() throws Exception {
476: String num = "0032";
477: String oldnum = "";
478: utx.begin();
479: Personne p = null;
480: try {
481: p = getHome().findByPrimaryKey(myname);
482: oldnum = p.getNumero();
483: p.setNumero(num);
484: Personne p2 = getHome().findByPrimaryKey(myname);
485: assertTrue(p2.getNumero().equals(num));
486: } catch (Exception e) {
487: fail(e.getMessage());
488: } finally {
489: utx.rollback();
490: }
491: p = getHome().findByPrimaryKey(myname);
492: assertTrue(p.getNumero().equals(oldnum));
493: }
494:
495: /**
496: * Test DuplicateKeyException.
497: * Create in an implicit tx
498: * known bug #300836: remove it from the list
499: */
500: public void _testDuplicateKeyTx() throws Exception {
501: getHome().findByPrimaryKey(myname);
502: try {
503: getHome().create(myname, "700");
504: fail("DuplicateKeyException not raised");
505: } catch (DuplicateKeyException e) {
506: } catch (Exception e) {
507: fail("Bad Exception raised:" + e);
508: }
509: }
510:
511: /**
512: * Test DuplicateKeyException.
513: * Create outside tx
514: * known bug #300836: remove it from the list
515: */
516: public void _testDuplicateKey() throws Exception {
517: try {
518: getHome().create(myname, "700", true);
519: fail("DuplicateKeyException not raised");
520: } catch (DuplicateKeyException e) {
521: } catch (Exception e) {
522: fail("Bad exception raised: " + e);
523: }
524: }
525:
526: public void testDuplicateKey2() throws Exception {
527: getHome().findByPrimaryKey(myname);
528: try {
529: getHome().create(myname, "700", true);
530: fail("DuplicateKeyException not raised");
531: } catch (DuplicateKeyException e) {
532: // should go here
533: } catch (Exception e) {
534: // go here in jonas-3-3-6
535: }
536: utx.begin();
537: Personne p0 = getHome().findByPrimaryKey(myname);
538: utx.commit();
539: String num = p0.getNumero();
540: assertTrue("bad value: " + num + ", should be: " + mynum, num
541: .equals(mynum));
542: }
543:
544: public void testRemoveCreateTx() throws Exception {
545: utx.begin();
546: try {
547: getHome().remove(myname);
548: getHome().create(myname, mynum, true);
549: } finally {
550: utx.commit();
551: }
552: getHome().findByPrimaryKey(myname);
553: }
554:
555: /**
556: * Verify that we can create an entity with a null name
557: *
558: */
559: public void testCreateNull() throws Exception {
560: getHome().create("nullv", null);
561: getHome().remove("nullv"); // cleaning
562: }
563:
564: /**
565: * Test many Create calls
566: */
567: public void testManyCreate() throws Exception {
568: int nbCreate = 20;
569: for (int i = 1; i <= nbCreate; i++) {
570: Personne p = getHome().create("manycreate" + i, "num" + i,
571: true);
572: }
573: for (int i = 1; i <= nbCreate; i++) {
574: getHome().remove("manycreate" + i);
575: }
576: }
577:
578: public void testRemoveCreate() throws Exception {
579: Personne p1 = null;
580: String man1 = "Count";
581: utx.begin();
582: getHome().create(man1, "1");
583: utx.commit();
584: p1 = getHome().findByNom(man1);
585: p1.remove();
586: getHome().create(man1, "1");
587: p1 = getHome().findByNom(man1);
588: p1.remove();
589: }
590:
591: public void testRemoveCreate2() throws Exception {
592: Personne p1 = null;
593: String man1 = "Duke";
594: p1 = getHome().create(man1, "1");
595: p1.remove();
596: p1 = getHome().create(man1, "1");
597: p1.remove();
598: }
599:
600: public void testRemoveCreate3() throws Exception {
601: Personne p4 = null;
602: String man4 = "Dexter";
603: p4 = getHome().create(man4, "4");
604: getHome().findByPrimaryKey(man4);
605: getHome().remove(man4);
606: p4 = getHome().create(man4, "44");
607: getHome().remove(man4);
608: }
609:
610: /**
611: * Combination of other tests
612: * This test the transaction isolation in entity bean container
613: * this test must never hang
614: */
615: public void testIsolation() throws Exception {
616: Personne p0 = null;
617: Personne p1 = null;
618: Personne p2 = null;
619: Personne p3 = null;
620: Personne p4 = null;
621: Personne p5 = null;
622: String n = null;
623: String man1 = "Duke Ellington";
624: String man2 = "Louis Armstrong";
625: String man3 = "Lionel Hampton";
626: String man4 = "Dexter Gordon";
627: String man5 = "Bill Evans";
628:
629: // man1
630: utx.begin();
631: try {
632: getHome().create(man1, "1");
633: } catch (Exception e) {
634: fail(e.getMessage());
635: } finally {
636: utx.commit();
637: }
638: p1 = getHome().findByNom(man1);
639:
640: // man2
641: utx.begin();
642: try {
643: getHome().create(man2, "2");
644: } catch (Exception e) {
645: fail(e.getMessage());
646: } finally {
647: utx.rollback();
648: }
649:
650: // man3
651: utx.begin();
652: try {
653: p3 = getHome().create(man3, "3");
654: p3.setNumero("33");
655: } catch (Exception e) {
656: fail(e.getMessage());
657: } finally {
658: utx.commit();
659: }
660: assertTrue(p3.getNumero().equals("33"));
661:
662: // cleaning
663: p1.remove();
664: p3.remove();
665:
666: // man4
667: p4 = getHome().create(man4, "4");
668: getHome().findByPrimaryKey(man4);
669: getHome().remove(man4);
670: p4 = getHome().create(man4, "44");
671: getHome().findByPrimaryKey(man4);
672: utx.begin();
673: try {
674: getHome().remove(man4);
675: } catch (Exception e) {
676: fail(e.getMessage());
677: } finally {
678: utx.commit();
679: }
680:
681: // man5
682: p5 = getHome().create(man5, "5");
683: getHome().findByPrimaryKey(man5);
684: p5.remove();
685: p5 = getHome().create(man5, "5");
686: getHome().findByPrimaryKey(man5);
687: utx.begin();
688: try {
689: p5.remove();
690: } catch (Exception e) {
691: fail(e.getMessage());
692: } finally {
693: utx.commit();
694: }
695:
696: // existing person
697: for (int i = 0; i < 5; i++) {
698: utx.begin();
699: try {
700: p0 = getHome().findByPrimaryKey(myname);
701: } catch (Exception e) {
702: fail(e.getMessage());
703: } finally {
704: utx.commit();
705: }
706: }
707: assertTrue(p0.getNumero().equals(mynum));
708:
709: // findall
710: for (int i = 0; i < 5; i++) {
711: utx.begin();
712: try {
713: getHome().findAll();
714: } catch (Exception e) {
715: fail(e.getMessage());
716: } finally {
717: utx.commit();
718: }
719: }
720:
721: // create + remove
722: for (int i = 0; i < 20; i++) {
723: utx.begin();
724: try {
725: getHome().create(man2, "500");
726: getHome().remove(man2);
727: } catch (Exception e) {
728: fail(e.getMessage());
729: } finally {
730: utx.commit();
731: }
732: }
733:
734: // findByPrimaryKey twice
735: utx.begin();
736: try {
737: p0 = getHome().findByPrimaryKey(myname);
738: p0.setNumero("0");
739: p2 = getHome().findByPrimaryKey(myname);
740: assertTrue(p2.getNumero().equals("0"));
741: } catch (Exception e) {
742: fail(e.getMessage());
743: } finally {
744: utx.rollback();
745: }
746:
747: }
748:
749: }
|