KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > ref > TestCascadeDeleteSimpleRef


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.runtime.ref;
19
20 import javax.jdo.JDOUserException;
21 import javax.jdo.PersistenceManager;
22
23 import junit.framework.Assert;
24
25 import org.objectweb.speedo.SpeedoTestHelper;
26 import org.objectweb.speedo.pobjects.ref.Group;
27 import org.objectweb.speedo.pobjects.ref.Member;
28 import org.objectweb.speedo.pobjects.ref.President;
29
30 /**
31  *
32  * @author S.Col
33  */

34 public class TestCascadeDeleteSimpleRef extends SpeedoTestHelper {
35
36     public TestCascadeDeleteSimpleRef(String JavaDoc s) {
37         super(s);
38     }
39
40     protected String JavaDoc getLoggerName() {
41         return LOG_NAME + "rt.ref.CascadeDeleteSimpleRef";
42     }
43
44
45     /**
46      * This method is responsible for the test of the delete of an object that has one-one and one-to-many
47      * relationships with other classes, but these relationships are empty.
48      */

49     public void testCascadeDeleteOneToNone() {
50         PersistenceManager pm = pmf.getPersistenceManager();
51         
52         // We create a group. A group can have a president and many members, but not this one.
53
String JavaDoc gName = "SpeedoCascade";
54         Group grp = new Group(gName);
55         
56         // We make the group persistant
57
pm.makePersistent(grp);
58         
59         // Get the id
60
Object JavaDoc gId = pm.getObjectId(grp);
61
62         pm.close();
63
64         grp = null;
65         
66         // Check the creation of the group
67
pm = pmf.getPersistenceManager();
68         grp = (Group) pm.getObjectById(gId, true);
69         Assert.assertNotNull("null instance returned for group", grp);
70         Assert.assertEquals("Bad group name", gName, grp.getName());
71         
72         /* Delete the group.
73          */

74         pm.currentTransaction().begin();
75         pm.deletePersistent(grp);
76         pm.currentTransaction().commit();
77         pm.close();
78        
79         // Check that the group has been deleted
80
pm = pmf.getPersistenceManager();
81         try {
82             grp = (Group) pm.getObjectById(gId, true);
83             Assert.fail("Should have thrown a JDOUserException : the group has not been deleted");
84         }
85         catch (JDOUserException jdoEx) {
86             // The test has passed
87
Assert.assertTrue(true);
88         }
89         
90         pm.close();
91     }
92
93     
94     /**
95      * This method is responsible for the test of the cascade delete of a simple reference. A group has a president and if we delete the group,
96      * the president should be deleted too.
97      */

98     public void testCascadeDeleteOneToOne() {
99         PersistenceManager pm = pmf.getPersistenceManager();
100         
101         // A president
102
String JavaDoc pName = "Boss";
103         President pres = new President(pName);
104
105         // A group
106
String JavaDoc gName = "SpeedoCascade";
107         Group grp = new Group(gName, pres);
108         
109         // We make the group persistant
110
pm.makePersistent(grp);
111         
112         // Get the ids
113
Object JavaDoc gId = pm.getObjectId(grp);
114         Object JavaDoc pId = pm.getObjectId(grp.getPresident());
115
116         pm.close();
117
118         grp = null;
119         pres = null;
120         
121         // Check the creation of the group and its president
122
pm = pmf.getPersistenceManager();
123         grp = (Group) pm.getObjectById(gId, true);
124         pres = (President) pm.getObjectById(pId, true);
125         Assert.assertNotNull("null instance returned for group", grp);
126         Assert.assertEquals("Bad group name", gName, grp.getName());
127         Assert.assertNotNull("null instance returned for president", pres);
128         Assert.assertEquals("Bad president name", pName, pres.getName());
129         
130         /* Delete the group :
131          * this should delete the president.
132          */

133         pm.currentTransaction().begin();
134         pm.deletePersistent(grp);
135         pm.currentTransaction().commit();
136         pm.close();
137        
138         // Check that the president has been deleted
139
pm = pmf.getPersistenceManager();
140         try {
141             pres = (President) pm.getObjectById(pId, true);
142             Assert.fail("Should have thrown a JDOUserException : the president has not been deleted");
143         }
144         catch (JDOUserException jdoEx) {
145             // The test has passed
146
Assert.assertTrue(true);
147         }
148         
149         pm.close();
150     }
151
152     
153     /**
154      * This method is responsible for the test of the cascade delete of a simple reference. The group has 2 members.
155      */

156     public void testCascadeDeleteOneToTwo() {
157         PersistenceManager pm = pmf.getPersistenceManager();
158         
159         // A group
160
String JavaDoc gName = "SpeedoCascade";
161         Group grp = new Group(gName);
162         
163         // Two members
164
String JavaDoc m1Name = "Jean";
165         String JavaDoc m2Name = "Sophie";
166         Member mem1 = new Member(m1Name, grp);
167         Member mem2 = new Member(m2Name, grp);
168         
169         // We make the members persistant
170
pm.makePersistent(mem1);
171         pm.makePersistent(mem2);
172         
173         // Get the ids
174
Object JavaDoc m1Id = pm.getObjectId(mem1);
175         Object JavaDoc m2Id = pm.getObjectId(mem2);
176         Object JavaDoc gId = pm.getObjectId(grp);
177
178         pm.close();
179
180         mem1 = null;
181         mem2 = null;
182         grp = null;
183         
184         // Check the creation of the group and its members
185
pm = pmf.getPersistenceManager();
186         mem1 = (Member) pm.getObjectById(m1Id, true);
187         mem2 = (Member) pm.getObjectById(m2Id, true);
188         grp = (Group) pm.getObjectById(gId, true);
189         Assert.assertNotNull("null instance for the member 1", mem1);
190         Assert.assertEquals("Bad member 1 name", m1Name, mem1.getName());
191         Assert.assertNotNull("null instance for the member 2", mem2);
192         Assert.assertEquals("Bad member 2 name", m2Name, mem2.getName());
193         Assert.assertNotNull("null instance returned for group", grp);
194         Assert.assertEquals("Bad group name", gName, grp.getName());
195         
196         /* Delete the group :
197          * this should delete both members.
198          */

199         pm.currentTransaction().begin();
200         pm.deletePersistent(grp);
201         pm.currentTransaction().commit();
202         pm.close();
203        
204         // Check that the members have been deleted
205
pm = pmf.getPersistenceManager();
206         try {
207             mem1 = (Member) pm.getObjectById(m1Id, true);
208             
209             // Delete both members
210
pm.currentTransaction().begin();
211             pm.deletePersistent(mem1);
212             pm.deletePersistent(mem2);
213             pm.currentTransaction().commit();
214             Assert.fail("Should have thrown a JDOUserException : member 1 has not been deleted");
215         }
216         catch (JDOUserException jdoEx) {
217             // The test has passed
218
Assert.assertTrue(true);
219         }
220         try {
221             mem2 = (Member) pm.getObjectById(m2Id, true);
222             Assert.fail("Should have thrown a JDOUserException : member 2 has not been deleted");
223         }
224         catch (JDOUserException jdoEx) {
225             // The test has passed
226
Assert.assertTrue(true);
227         }
228         
229         pm.close();
230     }
231
232
233     /**
234      * This method is responsible for the test of the cascade delete of a simple reference. A group with 10 members.
235      */

236     public void testCascadeDeleteOneToMany() {
237         PersistenceManager pm = pmf.getPersistenceManager();
238         
239         // A group
240
String JavaDoc gName = "SpeedoCascade";
241         Group grp = new Group(gName);
242         
243         // Two members
244
String JavaDoc m1Name = "Jean";
245         String JavaDoc m2Name = "Sophie";
246         String JavaDoc m3Name = "Alain";
247         String JavaDoc m4Name = "Sylvie";
248         String JavaDoc m5Name = "Jeanne";
249         String JavaDoc m6Name = "Rose";
250         String JavaDoc m7Name = "Baptiste";
251         String JavaDoc m8Name = "Alexandre";
252         String JavaDoc m9Name = "Noemie";
253         String JavaDoc m10Name = "Eve";
254         Member mem1 = new Member(m1Name, grp);
255         Member mem2 = new Member(m2Name, grp);
256         Member mem3 = new Member(m3Name, grp);
257         Member mem4 = new Member(m4Name, grp);
258         Member mem5 = new Member(m5Name, grp);
259         Member mem6 = new Member(m6Name, grp);
260         Member mem7 = new Member(m7Name, grp);
261         Member mem8 = new Member(m8Name, grp);
262         Member mem9 = new Member(m9Name, grp);
263         Member mem10 = new Member(m10Name, grp);
264         
265         // We make the members persistant
266
pm.makePersistent(mem1);
267         pm.makePersistent(mem2);
268         pm.makePersistent(mem3);
269         pm.makePersistent(mem4);
270         pm.makePersistent(mem5);
271         pm.makePersistent(mem6);
272         pm.makePersistent(mem7);
273         pm.makePersistent(mem8);
274         pm.makePersistent(mem9);
275         pm.makePersistent(mem10);
276         
277         // Get the ids
278
Object JavaDoc m1Id = pm.getObjectId(mem1);
279         Object JavaDoc m2Id = pm.getObjectId(mem2);
280         Object JavaDoc m3Id = pm.getObjectId(mem3);
281         Object JavaDoc m4Id = pm.getObjectId(mem4);
282         Object JavaDoc m5Id = pm.getObjectId(mem5);
283         Object JavaDoc m6Id = pm.getObjectId(mem6);
284         Object JavaDoc m7Id = pm.getObjectId(mem7);
285         Object JavaDoc m8Id = pm.getObjectId(mem8);
286         Object JavaDoc m9Id = pm.getObjectId(mem9);
287         Object JavaDoc m10Id = pm.getObjectId(mem10);
288         Object JavaDoc gId = pm.getObjectId(grp);
289
290         pm.close();
291
292         mem1 = null;
293         mem2 = null;
294         mem3 = null;
295         mem4 = null;
296         mem5 = null;
297         mem6 = null;
298         mem7 = null;
299         mem8 = null;
300         mem9 = null;
301         mem10 = null;
302         grp = null;
303         
304         // Check the creation of the employee and the department
305
pm = pmf.getPersistenceManager();
306         mem1 = (Member) pm.getObjectById(m1Id, true);
307         mem2 = (Member) pm.getObjectById(m2Id, true);
308         mem3 = (Member) pm.getObjectById(m3Id, true);
309         mem4 = (Member) pm.getObjectById(m4Id, true);
310         mem5 = (Member) pm.getObjectById(m5Id, true);
311         mem6 = (Member) pm.getObjectById(m6Id, true);
312         mem7 = (Member) pm.getObjectById(m7Id, true);
313         mem8 = (Member) pm.getObjectById(m8Id, true);
314         mem9 = (Member) pm.getObjectById(m9Id, true);
315         mem10 = (Member) pm.getObjectById(m10Id, true);
316         grp = (Group) pm.getObjectById(gId, true);
317         Assert.assertNotNull("null instance for the member 1", mem1);
318         Assert.assertEquals("Bad member 1 name", m1Name, mem1.getName());
319         Assert.assertNotNull("null instance for the member 2", mem2);
320         Assert.assertEquals("Bad member 2 name", m2Name, mem2.getName());
321         Assert.assertNotNull("null instance for the member 3", mem3);
322         Assert.assertEquals("Bad member 3 name", m3Name, mem3.getName());
323         Assert.assertNotNull("null instance for the member 4", mem4);
324         Assert.assertEquals("Bad member 4 name", m4Name, mem4.getName());
325         Assert.assertNotNull("null instance for the member 5", mem5);
326         Assert.assertEquals("Bad member 5 name", m5Name, mem5.getName());
327         Assert.assertNotNull("null instance for the member 6", mem6);
328         Assert.assertEquals("Bad member 6 name", m6Name, mem6.getName());
329         Assert.assertNotNull("null instance for the member 7", mem7);
330         Assert.assertEquals("Bad member 7 name", m7Name, mem7.getName());
331         Assert.assertNotNull("null instance for the member 8", mem8);
332         Assert.assertEquals("Bad member 8 name", m8Name, mem8.getName());
333         Assert.assertNotNull("null instance for the member 9", mem9);
334         Assert.assertEquals("Bad member 9 name", m9Name, mem9.getName());
335         Assert.assertNotNull("null instance for the member 10", mem10);
336         Assert.assertEquals("Bad member 10 name", m10Name, mem10.getName());
337         Assert.assertNotNull("null instance returned for group", grp);
338         Assert.assertEquals("Bad group name", gName, grp.getName());
339         
340         /* Delete the group :
341          * this should delete its members too.
342          */

343         pm.currentTransaction().begin();
344         pm.deletePersistent(grp);
345         pm.currentTransaction().commit();
346         pm.close();
347        
348         // Check that the members have been deleted too
349
pm = pmf.getPersistenceManager();
350         try {
351             mem1 = (Member) pm.getObjectById(m1Id, true);
352             
353             // Delete all members
354
pm.currentTransaction().begin();
355             pm.deletePersistent(mem1);
356             pm.deletePersistent(mem2);
357             pm.deletePersistent(mem3);
358             pm.deletePersistent(mem4);
359             pm.deletePersistent(mem5);
360             pm.deletePersistent(mem6);
361             pm.deletePersistent(mem7);
362             pm.deletePersistent(mem8);
363             pm.deletePersistent(mem9);
364             pm.deletePersistent(mem10);
365             pm.currentTransaction().commit();
366             Assert.fail("Should have thrown a JDOUserException : member 1 has not been deleted");
367         }
368         catch (JDOUserException jdoEx) {
369             // The test has passed
370
}
371
372         // Check that the members have been deleted too
373
try {
374             mem2 = (Member) pm.getObjectById(m2Id, true);
375             
376             // Delete all members
377
pm.currentTransaction().begin();
378             pm.deletePersistent(mem2);
379             pm.deletePersistent(mem3);
380             pm.deletePersistent(mem4);
381             pm.deletePersistent(mem5);
382             pm.deletePersistent(mem6);
383             pm.deletePersistent(mem7);
384             pm.deletePersistent(mem8);
385             pm.deletePersistent(mem9);
386             pm.deletePersistent(mem10);
387             pm.currentTransaction().commit();
388             Assert.fail("Should have thrown a JDOUserException : member 2 has not been deleted");
389         }
390         catch (JDOUserException jdoEx) {
391             // The test has passed
392
}
393
394         // Check that the members have been deleted too
395
try {
396             mem3 = (Member) pm.getObjectById(m3Id, true);
397             
398             // Delete all members
399
pm.currentTransaction().begin();
400             pm.deletePersistent(mem3);
401             pm.deletePersistent(mem4);
402             pm.deletePersistent(mem5);
403             pm.deletePersistent(mem6);
404             pm.deletePersistent(mem7);
405             pm.deletePersistent(mem8);
406             pm.deletePersistent(mem9);
407             pm.deletePersistent(mem10);
408             pm.currentTransaction().commit();
409             Assert.fail("Should have thrown a JDOUserException : member 3 has not been deleted");
410         }
411         catch (JDOUserException jdoEx) {
412             // The test has passed
413
}
414
415         // Check that the members have been deleted too
416
try {
417             mem4 = (Member) pm.getObjectById(m4Id, true);
418             
419             // Delete all members
420
pm.currentTransaction().begin();
421             pm.deletePersistent(mem4);
422             pm.deletePersistent(mem5);
423             pm.deletePersistent(mem6);
424             pm.deletePersistent(mem7);
425             pm.deletePersistent(mem8);
426             pm.deletePersistent(mem9);
427             pm.deletePersistent(mem10);
428             pm.currentTransaction().commit();
429             Assert.fail("Should have thrown a JDOUserException : member 4 has not been deleted");
430         }
431         catch (JDOUserException jdoEx) {
432             // The test has passed
433
}
434
435         // Check that the members have been deleted too
436
try {
437             mem5 = (Member) pm.getObjectById(m5Id, true);
438             
439             // Delete all members
440
pm.currentTransaction().begin();
441             pm.deletePersistent(mem5);
442             pm.deletePersistent(mem6);
443             pm.deletePersistent(mem7);
444             pm.deletePersistent(mem8);
445             pm.deletePersistent(mem9);
446             pm.deletePersistent(mem10);
447             pm.currentTransaction().commit();
448             Assert.fail("Should have thrown a JDOUserException : member 5 has not been deleted");
449         }
450         catch (JDOUserException jdoEx) {
451             // The test has passed
452
}
453
454         // Check that the members have been deleted too
455
try {
456             mem6 = (Member) pm.getObjectById(m6Id, true);
457             
458             // Delete all members
459
pm.currentTransaction().begin();
460             pm.deletePersistent(mem6);
461             pm.deletePersistent(mem7);
462             pm.deletePersistent(mem8);
463             pm.deletePersistent(mem9);
464             pm.deletePersistent(mem10);
465             pm.currentTransaction().commit();
466             Assert.fail("Should have thrown a JDOUserException : member 6 has not been deleted");
467         }
468         catch (JDOUserException jdoEx) {
469             // The test has passed
470
}
471
472         // Check that the members have been deleted too
473
try {
474             mem7 = (Member) pm.getObjectById(m7Id, true);
475             
476             // Delete all members
477
pm.currentTransaction().begin();
478             pm.deletePersistent(mem7);
479             pm.deletePersistent(mem8);
480             pm.deletePersistent(mem9);
481             pm.deletePersistent(mem10);
482             pm.currentTransaction().commit();
483             Assert.fail("Should have thrown a JDOUserException : member 7 has not been deleted");
484         }
485         catch (JDOUserException jdoEx) {
486             // The test has passed
487
}
488
489         // Check that the members have been deleted too
490
try {
491             mem8 = (Member) pm.getObjectById(m8Id, true);
492             
493             // Delete all members
494
pm.currentTransaction().begin();
495             pm.deletePersistent(mem8);
496             pm.deletePersistent(mem9);
497             pm.deletePersistent(mem10);
498             pm.currentTransaction().commit();
499             Assert.fail("Should have thrown a JDOUserException : member 8 has not been deleted");
500         }
501         catch (JDOUserException jdoEx) {
502             // The test has passed
503
}
504
505         // Check that the members have been deleted too
506
try {
507             mem9 = (Member) pm.getObjectById(m9Id, true);
508             
509             // Delete all members
510
pm.currentTransaction().begin();
511             pm.deletePersistent(mem9);
512             pm.deletePersistent(mem10);
513             pm.currentTransaction().commit();
514             Assert.fail("Should have thrown a JDOUserException : member 9 has not been deleted");
515         }
516         catch (JDOUserException jdoEx) {
517             // The test has passed
518
}
519
520
521         // Check that the members have been deleted too
522
try {
523             mem10 = (Member) pm.getObjectById(m10Id, true);
524             
525             // Delete all members
526
pm.currentTransaction().begin();
527             pm.deletePersistent(mem10);
528             pm.currentTransaction().commit();
529             Assert.fail("Should have thrown a JDOUserException : member 10 has not been deleted");
530         }
531         catch (JDOUserException jdoEx) {
532             // The test has passed
533
Assert.assertTrue(true);
534         }
535
536         pm.close();
537     }
538 }
539
Popular Tags