KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > entity > A_AdvancedHomeEC


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_AdvancedHomeEC.java,v 1.17 2004/10/07 11:52:42 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.util.Enumeration JavaDoc;
30
31 import javax.ejb.DuplicateKeyException JavaDoc;
32 import javax.ejb.EJBException JavaDoc;
33 import javax.ejb.FinderException JavaDoc;
34 import javax.ejb.Handle JavaDoc;
35 import javax.ejb.RemoveException JavaDoc;
36 import javax.rmi.PortableRemoteObject JavaDoc;
37 import javax.transaction.RollbackException JavaDoc;
38
39 import org.objectweb.jonas.jtests.beans.annuaire.Personne;
40 import org.objectweb.jonas.jtests.beans.annuaire.PersonneHome;
41 import org.objectweb.jonas.jtests.util.JTestCase;
42
43 /**
44  * This is an advanced test suite for home interface on entity bean CMP.
45  * Beans used: annuaire
46  * @author Philippe Coq, Philippe Durieux, Helene Joanin (jonas team)
47  */

48 public abstract class A_AdvancedHomeEC extends JTestCase {
49
50     String JavaDoc mynum = "638";
51     String JavaDoc myname = "Philippe Durieux";
52
53     public A_AdvancedHomeEC(String JavaDoc name) {
54         super(name);
55     }
56
57     protected void setUp() {
58         super.setUp();
59     }
60
61     /**
62      * return PersonneHome, that can be either CMP v1 or CMP v2 bean.
63      */

64     abstract public PersonneHome getHome();
65
66     /**
67      * test du findAll, returning an Enumeration.
68      * Some of the beans are already in the database, some other are created.
69      */

70     public void testFindAllEnum() throws Exception JavaDoc {
71         Enumeration JavaDoc e = null;
72         e = getHome().findAll();
73         int count = 0;
74         while (e.hasMoreElements()) {
75             count++;
76             Personne p = (Personne)
77                 PortableRemoteObject.narrow(e.nextElement(), Personne.class);
78         }
79         assertEquals("Wrong number of bean (case 1)", 10, count);
80         Personne p1 = getHome().create("Paul Morgan", "1234256");
81         Personne p2 = getHome().create("Jean Richard", "1234356");
82         Personne p3 = getHome().create("Jean-Paul Landry", "1234556");
83         e = getHome().findAll();
84         count = 0;
85         while (e.hasMoreElements()) {
86             count++;
87             Personne p = (Personne)
88                 PortableRemoteObject.narrow(e.nextElement(), Personne.class);
89         }
90         assertEquals("Wrong number of bean (case 2)", 13, count);
91         // cleaning
92
p1.remove();
93         p2.remove();
94         p3.remove();
95         e = getHome().findAll();
96         count = 0;
97         while (e.hasMoreElements()) {
98             count++;
99             Personne p = (Personne) PortableRemoteObject.narrow(e.nextElement(), Personne.class);
100         }
101         assertEquals("Wrong number of bean (case 3)", 10, count);
102     }
103
104     /**
105      * test Create inside a Transaction.
106      */

107     public void testCreateInTx() throws Exception JavaDoc {
108         utx.begin();
109         try {
110             getHome().create("Duke Ellington", "12235446");
111             utx.commit();
112             Personne p = getHome().findByNom("Duke Ellington");
113             p.remove();
114             utx.begin();
115             getHome().create("Louis Armstrong", "12035446");
116         } catch (Exception JavaDoc e) {
117             fail(e.getMessage());
118         } finally {
119             utx.rollback();
120         }
121         try {
122             getHome().findByNom("Louis Armstrong");
123             fail("Should not find it");
124         } catch (FinderException JavaDoc e) {
125         }
126     }
127
128     /**
129      * test a simple Create inside a Transaction.
130      */

131     public void testSimpleCreateInTx() throws Exception JavaDoc {
132         utx.begin();
133         try {
134             getHome().create("Mike", "12235846");
135         } catch (Exception JavaDoc e) {
136             fail(e.getMessage());
137         } finally {
138             utx.commit();
139         }
140         Personne p = getHome().findByNom("Mike");
141         p.remove();
142     }
143
144     /**
145      * test a simple Remove
146      * Find an instance already in database and remove it.
147      */

148     public void testSimpleRemove() throws Exception JavaDoc {
149         Personne p = getHome().findByNom("Adriana Danes");
150         p.remove();
151         getHome().create("Adriana Danes", "777"); // cleaning
152
}
153
154     /**
155      * test remove twice
156      */

157     public void testRemoveTwice() throws Exception JavaDoc {
158         Personne p = getHome().create("Andre", "77710"); // first create a new element
159
p.remove();
160         try {
161             p.remove();
162             fail("Should not be able to remove this object twice");
163         } catch (RemoteException JavaDoc e) {
164             // OK
165
} catch (Exception JavaDoc e) {
166             fail("Bad exception raised: " + e);
167         }
168     }
169
170     /**
171      * test remove twice in same transaction
172      */

173     public void testRemoveTwiceTx() throws Exception JavaDoc {
174         Personne p = getHome().create("Andre", "77710"); // first create a new element
175
utx.begin();
176         try {
177             p.remove();
178             p.remove();
179             fail("Should not be able to remove this object twice");
180         } catch (RemoteException JavaDoc e) {
181             // OK: normal case because 2nd remove must fail
182
} catch (Exception JavaDoc e) {
183             fail("Bad exception raised: " + e);
184         } finally {
185             try {
186                 utx.commit();
187             } catch (RollbackException JavaDoc e) {
188                 // Possibly go here, since 2nd remove failed.
189
// clean object (1st remove has been rolled back!)
190
// Don't use p because instance should have been discarded.
191
getHome().remove("Andre");
192             }
193         }
194     }
195
196     /**
197      * Test Create + Business Method inside the same Tx
198      */

199     public void testCreateAndBusiness() throws Exception JavaDoc {
200         String JavaDoc num = "00000001";
201         Personne p = null;
202         utx.begin();
203         try {
204             p = getHome().create("Lionel Hampton", "92235446");
205             p.setNumero(num);
206         }
207         catch (Exception JavaDoc e) {
208             fail(e.getMessage());
209         }
210         finally {
211             utx.commit();
212         }
213         assertTrue(num.equals(p.getNumero()));
214         p.remove(); // cleaning
215
}
216
217     /**
218      * Test remove(pk): create / find / remove(pk) / find
219      */

220     public void testRemovePk() throws Exception JavaDoc {
221         Personne p = null;
222         String JavaDoc newman = "Xavier Spengler";
223         p = getHome().create(newman, "6");
224         getHome().findByPrimaryKey(newman);
225         getHome().remove(newman);
226         try {
227             getHome().findByPrimaryKey(newman);
228             fail("Should not exist anymore");
229         }
230         catch (FinderException JavaDoc e) {
231         }
232     }
233     
234     /**
235      * Test remove(handle): create / find / remove(handle) / find
236      * CMP2: bug #300419
237      */

238     public void testRemoveHandle() throws Exception JavaDoc {
239         Personne p = null;
240         String JavaDoc newman = "Xavier Spengler";
241         p = getHome().create(newman, "6");
242         Handle JavaDoc h = p.getHandle();
243         getHome().remove(h);
244         try {
245             getHome().findByPrimaryKey(newman);
246             fail("Should not exist anymore");
247         } catch (FinderException JavaDoc e) {
248         }
249     }
250
251     /**
252      * Test remove(pk) inside transaction:
253      * create / find / remove(pk) / find
254      */

255     public void testRemovePkInTx() throws Exception JavaDoc {
256         Personne p = null;
257         String JavaDoc newman = "Xavier Spengler";
258         p = getHome().create(newman, "6");
259         getHome().findByPrimaryKey(newman);
260         utx.begin();
261         try {
262             getHome().remove(newman);
263         }
264         catch (Exception JavaDoc e) {
265             fail(e.getMessage());
266         }
267         finally {
268             utx.commit();
269         }
270         try {
271             getHome().findByPrimaryKey(newman);
272             fail("Should not exist anymore");
273         }
274         catch (FinderException JavaDoc e) {
275         }
276     }
277
278     /**
279      * Test remove(pk) of a non-existent pk.
280      */

281     public void testRemovePkNonExistent() throws Exception JavaDoc {
282         String JavaDoc man = "NonExistent";
283         try {
284             getHome().remove(man);
285             fail("No RemoteException");
286         }
287         catch (RemoteException JavaDoc e) {
288         }
289     }
290
291     /**
292      * Test remove(pk) of a non-existent pk inside transaction.
293      * See ejb2.1 18.3.1
294      */

295     public void testRemovePkNonExistentInTx() throws Exception JavaDoc {
296         String JavaDoc man = "NonExistent";
297         utx.begin();
298         try {
299             getHome().remove(man);
300             fail("No RemoteException");
301         }
302         catch (RemoteException JavaDoc e) {
303         } finally {
304             try {
305                 utx.commit();
306                 fail("transaction should be marked as rollback");
307             } catch (RollbackException JavaDoc e) {
308             }
309         }
310     }
311
312     /**
313      * Test bean.remove :
314      * create / find / remove / find
315      */

316     public void testRemove() throws Exception JavaDoc {
317         Personne p = null;
318         String JavaDoc newman = "Jeannot";
319         p = getHome().create(newman, "5");
320         getHome().findByPrimaryKey(newman);
321         p.remove();
322         try {
323             getHome().findByPrimaryKey(newman);
324             fail("Should not exist anymore");
325         }
326         catch (FinderException JavaDoc e) {
327         }
328     }
329     
330     /**
331      * Test bean.remove :
332      * create / find / remove / bussiness method
333      * Ensure the javax.ejb.EJBException is thrown when trying
334      * to invoke an accessor method on a deleted entitybean object
335      */

336     public void testRemove2() throws Exception JavaDoc {
337         Personne p = null;
338         String JavaDoc newman = "Jeannot2";
339         p = getHome().create(newman, "5");
340         getHome().findByPrimaryKey(newman);
341         p.remove();
342         try {
343             p.getNumero();
344             fail("Should not exist anymore");
345         }
346         catch (Exception JavaDoc e) {
347         }
348     }
349
350     /**
351      * Test remove by PK in a tx rolled back
352      * we modify the bean instance outside tx, then we check that
353      * the bean state has not been changed by the rolled back remove
354      */

355     public void testRemovePKRB() throws Exception JavaDoc {
356         String JavaDoc newnum = "344";
357         String JavaDoc name = "remove-by-pk";
358         Personne p = getHome().create(name, "420");
359         p.setNumero(newnum);
360         utx.begin();
361         getHome().remove(name);
362         utx.rollback();
363         try {
364             assertEquals("Lost modified value", newnum, p.getNumero());
365         } finally {
366             // cleaning
367
getHome().remove(name);
368         }
369     }
370
371     /**
372      * Test remove in a tx rolled back
373      * we modify the bean instance outside tx, then we check that
374      * the bean state has not been changed by the rolled back remove
375      */

376     public void testRemoveRB() throws Exception JavaDoc {
377         String JavaDoc newnum = "345";
378         String JavaDoc name = "remove-instance";
379         Personne p = getHome().create(name, "421");
380         p.setNumero(newnum);
381         utx.begin();
382         p.remove();
383         utx.rollback();
384         try {
385             assertEquals("Lost modified value", newnum, p.getNumero());
386         } finally {
387             // cleaning
388
//p.remove();
389
getHome().remove(name);
390         }
391     }
392     
393     /**
394      * Test bean.remove inside a tx:
395      * create / find / remove / find
396      */

397     public void testRemoveInTx() throws Exception JavaDoc {
398         Personne p = null;
399         String JavaDoc newman = "Jeannot";
400         p = getHome().create(newman, "5");
401         getHome().findByPrimaryKey(newman);
402         utx.begin();
403         try {
404             p.remove();
405         }
406         catch (Exception JavaDoc e) {
407             fail(e.getMessage());
408         }
409         finally {
410             utx.commit();
411         }
412         try {
413             getHome().findByPrimaryKey(newman);
414             fail("Should not exist anymore");
415         }
416         catch (FinderException JavaDoc e) {
417         }
418     }
419
420     /**
421      * Test finder in transactions
422      */

423     public void testFinderInTx() throws Exception JavaDoc {
424         Personne p = null;
425
426         // FindByPrimaryKey
427
for (int i = 0; i < 5; i++) {
428             utx.begin();
429             try {
430                 p = getHome().findByPrimaryKey(myname);
431             }
432             catch (Exception JavaDoc e) {
433                 fail(e.getMessage());
434             }
435             finally {
436                 utx.commit();
437             }
438         }
439         assertTrue(p.getNumero().equals(mynum));
440
441         // FindByNum
442
for (int i = 0; i < 5; i++) {
443             utx.begin();
444             try {
445                 p = getHome().findByNom(myname);
446             }
447             catch (Exception JavaDoc e) {
448                 fail(e.getMessage());
449             }
450             finally {
451                 utx.commit();
452             }
453         }
454         assertTrue(p.getNumero().equals(mynum));
455
456         // FindAll
457
for (int i = 0; i < 5; i++) {
458             utx.begin();
459             try {
460                 getHome().findAll();
461             }
462             catch (Exception JavaDoc e) {
463                 fail(e.getMessage());
464             }
465             finally {
466                 utx.commit();
467             }
468         }
469     }
470
471     /**
472      * Test create + remove in transactions
473      */

474     public void testCreateRemoveInTx() throws Exception JavaDoc {
475         for (int i = 0; i < 20; i++) {
476             utx.begin();
477             try {
478                 getHome().create("Eric Paire", "500");
479                 getHome().remove("Eric Paire");
480             }
481             catch (Exception JavaDoc e) {
482                 fail(e.getMessage());
483             }
484             finally {
485                 utx.commit();
486             }
487         }
488     }
489
490     /**
491      * test 2 findByPrimaryKey in the same tx, with an update
492      * between the 2 calls. We shall see the modifications, even before
493      * the commit occurs.
494      */

495     public void testFindTwice() throws Exception JavaDoc {
496         String JavaDoc num = "0032";
497         String JavaDoc oldnum = "";
498         utx.begin();
499         Personne p = null;
500         try {
501             p = getHome().findByPrimaryKey(myname);
502             oldnum = p.getNumero();
503             p.setNumero(num);
504             Personne p2 = getHome().findByPrimaryKey(myname);
505             assertTrue(p2.getNumero().equals(num));
506         }
507         catch (Exception JavaDoc e) {
508             fail(e.getMessage());
509         }
510         finally {
511             utx.rollback();
512         }
513         p = getHome().findByPrimaryKey(myname);
514         assertTrue(p.getNumero().equals(oldnum));
515     }
516
517     /**
518      * Test DuplicateKeyException.
519      * Create in an implicit tx
520      * known bug #300836
521      */

522     public void testDuplicateKeyTx() throws Exception JavaDoc {
523         getHome().findByPrimaryKey(myname);
524         try {
525             getHome().create(myname, "700");
526             fail("DuplicateKeyException not raised");
527         } catch (DuplicateKeyException JavaDoc e) {
528         } catch (Exception JavaDoc e) {
529             fail("Bad Exception raised:" + e);
530         }
531     }
532
533     /**
534      * Test DuplicateKeyException.
535      * Create outside tx
536      * known bug #300836
537      */

538     public void testDuplicateKey() throws Exception JavaDoc {
539         try {
540             getHome().create(myname, "700", true);
541             fail("DuplicateKeyException not raised");
542         } catch (DuplicateKeyException JavaDoc e) {
543         } catch (Exception JavaDoc e) {
544             fail("Bad exception raised: " + e);
545         }
546     }
547
548     public void testDuplicateKey2() throws Exception JavaDoc {
549         getHome().findByPrimaryKey(myname);
550         try {
551             getHome().create(myname, "700", true);
552             fail("DuplicateKeyException not raised");
553         } catch (DuplicateKeyException JavaDoc e) {
554             // should go here
555
} catch (Exception JavaDoc e) {
556             // go here in jonas-3-3-6
557
}
558         utx.begin();
559         Personne p0 = getHome().findByPrimaryKey(myname);
560         utx.commit();
561         String JavaDoc num = p0.getNumero();
562         assertTrue("bad value: " + num + ", should be: " + mynum, num.equals(mynum));
563     }
564
565     public void testRemoveCreateTx() throws Exception JavaDoc {
566         utx.begin();
567         try {
568             getHome().remove(myname);
569             getHome().create(myname, mynum, true);
570         } finally {
571             utx.commit();
572         }
573         getHome().findByPrimaryKey(myname);
574     }
575
576     /**
577      * Verify that we can create an entity with a null name
578      *
579      */

580     public void testCreateNull() throws Exception JavaDoc {
581         getHome().create("nullv", null);
582         getHome().remove("nullv"); // cleaning
583
}
584
585     /**
586      * Test many Create calls
587      */

588     public void testManyCreate() throws Exception JavaDoc {
589         int nbCreate = 20;
590         for (int i = 1; i <= nbCreate; i++) {
591             Personne p = getHome().create("manycreate" + i, "num" + i, true);
592         }
593         for (int i = 1; i <= nbCreate; i++) {
594             getHome().remove("manycreate" + i);
595         }
596     }
597
598     public void testRemoveCreate() throws Exception JavaDoc {
599         Personne p1 = null;
600         String JavaDoc man1 = "Count";
601         utx.begin();
602         getHome().create(man1, "1");
603         utx.commit();
604         p1 = getHome().findByNom(man1);
605         p1.remove();
606         getHome().create(man1, "1");
607         p1 = getHome().findByNom(man1);
608         p1.remove();
609     }
610
611     public void testRemoveCreate2() throws Exception JavaDoc {
612         Personne p1 = null;
613         String JavaDoc man1 = "Duke";
614         p1 = getHome().create(man1, "1");
615         p1.remove();
616         p1 = getHome().create(man1, "1");
617         p1.remove();
618     }
619
620     public void testRemoveCreate3() throws Exception JavaDoc {
621         Personne p4 = null;
622         String JavaDoc man4 = "Dexter";
623         p4 = getHome().create(man4, "4");
624         getHome().findByPrimaryKey(man4);
625         getHome().remove(man4);
626         p4 = getHome().create(man4, "44");
627         getHome().remove(man4);
628     }
629
630     /**
631      * Combination of other tests
632      * This test the transaction isolation in entity bean container
633      * this test must never hang
634      */

635     public void testIsolation() throws Exception JavaDoc {
636         Personne p0 = null;
637         Personne p1 = null;
638         Personne p2 = null;
639         Personne p3 = null;
640         Personne p4 = null;
641         Personne p5 = null;
642         String JavaDoc n = null;
643         String JavaDoc man1 = "Duke Ellington";
644         String JavaDoc man2 = "Louis Armstrong";
645         String JavaDoc man3 = "Lionel Hampton";
646         String JavaDoc man4 = "Dexter Gordon";
647         String JavaDoc man5 = "Bill Evans";
648
649         // man1
650
utx.begin();
651         try {
652             getHome().create(man1, "1");
653         }
654         catch (Exception JavaDoc e) {
655             fail(e.getMessage());
656         }
657         finally {
658             utx.commit();
659         }
660         p1 = getHome().findByNom(man1);
661
662         // man2
663
utx.begin();
664         try {
665             getHome().create(man2, "2");
666         }
667         catch (Exception JavaDoc e) {
668             fail(e.getMessage());
669         }
670         finally {
671             utx.rollback();
672         }
673
674         // man3
675
utx.begin();
676         try {
677             p3 = getHome().create(man3, "3");
678             p3.setNumero("33");
679         }
680         catch (Exception JavaDoc e) {
681             fail(e.getMessage());
682         }
683         finally {
684             utx.commit();
685         }
686         assertTrue(p3.getNumero().equals("33"));
687
688         // cleaning
689
p1.remove();
690         p3.remove();
691
692         // man4
693
p4 = getHome().create(man4, "4");
694         getHome().findByPrimaryKey(man4);
695         getHome().remove(man4);
696         p4 = getHome().create(man4, "44");
697         getHome().findByPrimaryKey(man4);
698         utx.begin();
699         try {
700             getHome().remove(man4);
701         }
702         catch (Exception JavaDoc e) {
703             fail(e.getMessage());
704         }
705         finally {
706             utx.commit();
707         }
708
709         // man5
710
p5 = getHome().create(man5, "5");
711         getHome().findByPrimaryKey(man5);
712         p5.remove();
713         p5 = getHome().create(man5, "5");
714         getHome().findByPrimaryKey(man5);
715         utx.begin();
716         try {
717             p5.remove();
718         }
719         catch (Exception JavaDoc e) {
720             fail(e.getMessage());
721         }
722         finally {
723             utx.commit();
724         }
725
726         // existing person
727
for (int i = 0; i < 5; i++) {
728             utx.begin();
729             try {
730                 p0 = getHome().findByPrimaryKey(myname);
731             }
732             catch (Exception JavaDoc e) {
733                 fail(e.getMessage());
734             }
735             finally {
736                 utx.commit();
737             }
738         }
739         assertTrue(p0.getNumero().equals(mynum));
740
741         // findall
742
for (int i = 0; i < 5; i++) {
743             utx.begin();
744             try {
745                 getHome().findAll();
746             }
747             catch (Exception JavaDoc e) {
748                 fail(e.getMessage());
749             }
750             finally {
751                 utx.commit();
752             }
753         }
754
755         // create + remove
756
for (int i = 0; i < 20; i++) {
757             utx.begin();
758             try {
759                 getHome().create(man2, "500");
760                 getHome().remove(man2);
761             }
762             catch (Exception JavaDoc e) {
763                 fail(e.getMessage());
764             }
765             finally {
766                 utx.commit();
767             }
768         }
769
770         // findByPrimaryKey twice
771
utx.begin();
772         try {
773             p0 = getHome().findByPrimaryKey(myname);
774             p0.setNumero("0");
775             p2 = getHome().findByPrimaryKey(myname);
776             assertTrue(p2.getNumero().equals("0"));
777         }
778         catch (Exception JavaDoc e) {
779             fail(e.getMessage());
780         }
781         finally {
782             utx.rollback();
783         }
784
785     }
786
787 }
788
Popular Tags