KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > relations > TestCascadeDeleteRelations


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
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 of the License, or (at your option) 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 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Col.
25  *
26  */

27
28 package org.objectweb.speedo.runtime.relations;
29
30 import javax.jdo.JDOUserException;
31 import javax.jdo.PersistenceManager;
32
33 import junit.framework.Assert;
34
35 import org.objectweb.speedo.SpeedoTestHelper;
36 import org.objectweb.speedo.pobjects.relations.A;
37 import org.objectweb.speedo.pobjects.relations.B;
38
39 public class TestCascadeDeleteRelations extends SpeedoTestHelper {
40
41     public TestCascadeDeleteRelations(String JavaDoc s) {
42         super(s);
43     }
44
45     protected String JavaDoc getLoggerName() {
46         return LOG_NAME + "rt.relations.CascadeDeleteRelations";
47     }
48
49     /**
50      * This method tests the cascade delete in a one-one relationship.
51      *
52      */

53     public void testOneOne() {
54         A a1 = new A("testOneOne_a1");
55         A a2 = new A("testOneOne_a2");
56         B b1 = new B("testOneOne_b1");
57         B b2 = new B("testOneOne_b2");
58
59         // a1 references b1 (so b1 references a1)
60
a1.setB(b1);
61         assertTrue(b1.getA() == a1);
62
63         // a2 references b2 (so b2 references a2)
64
a2.setB(b2);
65         assertTrue(b2.getA() == a2);
66
67         // We make the As persistent
68
PersistenceManager pm = pmf.getPersistenceManager();
69         pm.makePersistent(a1);
70         pm.makePersistent(a2);
71         
72         // We get the ids
73
Object JavaDoc a1Id = pm.getObjectId(a1);
74         Object JavaDoc a2Id = pm.getObjectId(a2);
75         Object JavaDoc b1Id = pm.getObjectId(b1);
76         Object JavaDoc b2Id = pm.getObjectId(b2);
77         Assert.assertNotNull("null object identifier for a1", a1Id);
78         Assert.assertNotNull("null object identifier for a2", a2Id);
79         Assert.assertNotNull("null object identifier for b1", b1Id);
80         Assert.assertNotNull("null object identifier for b2", b2Id);
81         
82         pm.close();
83         
84         // Now, we are going to get a1 and delete it. Then we'll check that b1 has been deleted too
85
pm = pmf.getPersistenceManager();
86         a1 = (A)pm.getObjectById(a1Id, true);
87         b2 = (B)pm.getObjectById(b2Id, true);
88         pm.currentTransaction().begin();
89         pm.deletePersistent(a1);
90         pm.deletePersistent(b2);
91         pm.currentTransaction().commit();
92        
93         try {
94             b1 = (B)pm.getObjectById(b1Id, true);
95             Assert.fail("Should have thrown a JDOUser Exception : b1 has not been deleted");
96         }
97         catch (JDOUserException jdoEx) {
98             // The test has passed
99
Assert.assertTrue(true);
100         }
101
102         try {
103             a2 = (A)pm.getObjectById(a2Id, true);
104             Assert.fail("Should have thrown a JDOUser Exception : a2 has not been deleted");
105         }
106         catch (JDOUserException jdoEx) {
107             // The test has passed
108
Assert.assertTrue(true);
109         }
110
111         pm.close();
112     }
113
114     
115     public void testManyManyDelete() {
116         A a1 = new A("testManyManyDelete_a1");
117         A a2 = new A("testManyManyDelete_a2");
118         A a3 = new A("testManyManyDelete_a3");
119
120         B b1 = new B("testManyManyDelete_b1");
121         B b2 = new B("testManyManyDelete_b2");
122         B b3 = new B("testManyManyDelete_b3");
123
124         PersistenceManager pm = pmf.getPersistenceManager();
125         pm.makePersistent(a1);
126         pm.makePersistent(a2);
127         pm.makePersistent(a3);
128         pm.makePersistent(b1);
129         pm.makePersistent(b2);
130         pm.makePersistent(b3);
131         pm.close();
132
133         try {
134             pm = pmf.getPersistenceManager();
135
136             // We add b1 and b2 to a1's Bs
137
a1.getBs().add(b1);
138             assertTrue(b1.getAs().contains(a1));
139             a1.getBs().add(b2);
140             assertTrue(b2.getAs().contains(a1));
141
142             // We add b1, b2 and b3 to a2's Bs
143
a2.getBs().add(b1);
144             assertTrue(b1.getAs().contains(a2));
145             a2.getBs().add(b2);
146             assertTrue(b2.getAs().contains(a2));
147             a2.getBs().add(b3);
148             assertTrue(b3.getAs().contains(a2));
149
150             // We add b2 and b3 to a3's Bs
151
a3.getBs().add(b2);
152             assertTrue(b2.getAs().contains(a3));
153             a3.getBs().add(b3);
154             assertTrue(b3.getAs().contains(a3));
155
156             // We get some ids to try to get deleted objects
157
Object JavaDoc a3Id = pm.getObjectId(a3);
158             Object JavaDoc b2Id = pm.getObjectId(b2);
159             Object JavaDoc b3Id = pm.getObjectId(b3);
160             
161             pm.close();
162
163             // We delete a3 : as bs relationship in A has a cascade delete extension with the true value,
164
// b2 and b3 should be delete.
165
pm = pmf.getPersistenceManager();
166             pm.currentTransaction().begin();
167             pm.deletePersistent(a3);
168             pm.currentTransaction().commit();
169             pm.close();
170
171             pm = pmf.getPersistenceManager();
172
173             // a1 should only have b1
174
assertTrue("a1.getBs() has " + a1.getBs().size() + " elements, expected 1", a1.getBs().size() == 1);
175             assertTrue("a1 should contain b1", a1.getBs().contains(b1));
176             assertTrue("a1 should not contain b2", !a1.getBs().contains(b2));
177
178             // a2 should only have b1
179
assertTrue("a2.getBs() has " + a2.getBs().size() + " elements, expected 1", a2.getBs().size() == 1);
180             assertTrue("a2 should contain b1", a2.getBs().contains(b1));
181             assertTrue("a2 should not contain b3", !a2.getBs().contains(b3));
182
183             // a3 should not exist anymore
184
try {
185                 a3 = (A) pm.getObjectById(a3Id, true);
186                 Assert.fail("Should have thrown a JDOUserException : a3 has not been deleted");
187             }
188             catch (JDOUserException jdoEx) {
189                 // The test has passed
190
}
191
192             // b2 should not exist anymore
193
try {
194                 b2 = (B) pm.getObjectById(b2Id, true);
195                 Assert.fail("Should have thrown a JDOUserException : b2 has not been deleted");
196             }
197             catch (JDOUserException jdoEx) {
198                 // The test has passed
199
}
200
201             // b3 should not exist anymore
202
try {
203                 b3 = (B) pm.getObjectById(b3Id, true);
204                 Assert.fail("Should have thrown a JDOUserException : b3 has not been deleted");
205             }
206             catch (JDOUserException jdoEx) {
207                 // The test has passed
208
}
209
210             // b1 should still contain a1 and a2, but no more a3
211
assertTrue("b1 should contain a1", b1.getAs().contains(a1));
212             assertTrue("b1 should contain a2", b1.getAs().contains(a2));
213             assertTrue("b1 should not contain a3", !b1.getAs().contains(a3));
214
215             pm.close();
216             
217         } finally {
218             pm = pmf.getPersistenceManager();
219             pm.currentTransaction().begin();
220             pm.deletePersistent(a1);
221             pm.deletePersistent(a2);
222             pm.deletePersistent(b1);
223             pm.currentTransaction().commit();
224             pm.close();
225         }
226     }
227 }
228
Popular Tags