001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
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 of the License, or (at your option) 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 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Col.
025: *
026: */package org.objectweb.speedo.runtime.relations;
027:
028: import javax.jdo.JDOUserException;
029: import javax.jdo.PersistenceManager;
030:
031: import junit.framework.Assert;
032:
033: import org.objectweb.speedo.SpeedoTestHelper;
034: import org.objectweb.speedo.pobjects.relations.A;
035: import org.objectweb.speedo.pobjects.relations.B;
036:
037: public class TestCascadeDeleteRelations extends SpeedoTestHelper {
038:
039: public TestCascadeDeleteRelations(String s) {
040: super (s);
041: }
042:
043: protected String getLoggerName() {
044: return LOG_NAME + "rt.relations.CascadeDeleteRelations";
045: }
046:
047: /**
048: * This method tests the cascade delete in a one-one relationship.
049: *
050: */
051: public void testOneOne() {
052: A a1 = new A("testOneOne_a1");
053: A a2 = new A("testOneOne_a2");
054: B b1 = new B("testOneOne_b1");
055: B b2 = new B("testOneOne_b2");
056:
057: // a1 references b1 (so b1 references a1)
058: a1.setB(b1);
059: assertTrue(b1.getA() == a1);
060:
061: // a2 references b2 (so b2 references a2)
062: a2.setB(b2);
063: assertTrue(b2.getA() == a2);
064:
065: // We make the As persistent
066: PersistenceManager pm = pmf.getPersistenceManager();
067: pm.makePersistent(a1);
068: pm.makePersistent(a2);
069:
070: // We get the ids
071: Object a1Id = pm.getObjectId(a1);
072: Object a2Id = pm.getObjectId(a2);
073: Object b1Id = pm.getObjectId(b1);
074: Object b2Id = pm.getObjectId(b2);
075: Assert.assertNotNull("null object identifier for a1", a1Id);
076: Assert.assertNotNull("null object identifier for a2", a2Id);
077: Assert.assertNotNull("null object identifier for b1", b1Id);
078: Assert.assertNotNull("null object identifier for b2", b2Id);
079:
080: pm.close();
081:
082: // Now, we are going to get a1 and delete it. Then we'll check that b1 has been deleted too
083: pm = pmf.getPersistenceManager();
084: a1 = (A) pm.getObjectById(a1Id, true);
085: b2 = (B) pm.getObjectById(b2Id, true);
086: pm.currentTransaction().begin();
087: pm.deletePersistent(a1);
088: pm.deletePersistent(b2);
089: pm.currentTransaction().commit();
090:
091: try {
092: b1 = (B) pm.getObjectById(b1Id, true);
093: Assert
094: .fail("Should have thrown a JDOUser Exception : b1 has not been deleted");
095: } catch (JDOUserException jdoEx) {
096: // The test has passed
097: Assert.assertTrue(true);
098: }
099:
100: try {
101: a2 = (A) pm.getObjectById(a2Id, true);
102: Assert
103: .fail("Should have thrown a JDOUser Exception : a2 has not been deleted");
104: } catch (JDOUserException jdoEx) {
105: // The test has passed
106: Assert.assertTrue(true);
107: }
108:
109: pm.close();
110: }
111:
112: public void testManyManyDelete() {
113: A a1 = new A("testManyManyDelete_a1");
114: A a2 = new A("testManyManyDelete_a2");
115: A a3 = new A("testManyManyDelete_a3");
116:
117: B b1 = new B("testManyManyDelete_b1");
118: B b2 = new B("testManyManyDelete_b2");
119: B b3 = new B("testManyManyDelete_b3");
120:
121: PersistenceManager pm = pmf.getPersistenceManager();
122: pm.makePersistent(a1);
123: pm.makePersistent(a2);
124: pm.makePersistent(a3);
125: pm.makePersistent(b1);
126: pm.makePersistent(b2);
127: pm.makePersistent(b3);
128: pm.close();
129:
130: try {
131: pm = pmf.getPersistenceManager();
132:
133: // We add b1 and b2 to a1's Bs
134: a1.getBs().add(b1);
135: assertTrue(b1.getAs().contains(a1));
136: a1.getBs().add(b2);
137: assertTrue(b2.getAs().contains(a1));
138:
139: // We add b1, b2 and b3 to a2's Bs
140: a2.getBs().add(b1);
141: assertTrue(b1.getAs().contains(a2));
142: a2.getBs().add(b2);
143: assertTrue(b2.getAs().contains(a2));
144: a2.getBs().add(b3);
145: assertTrue(b3.getAs().contains(a2));
146:
147: // We add b2 and b3 to a3's Bs
148: a3.getBs().add(b2);
149: assertTrue(b2.getAs().contains(a3));
150: a3.getBs().add(b3);
151: assertTrue(b3.getAs().contains(a3));
152:
153: // We get some ids to try to get deleted objects
154: Object a3Id = pm.getObjectId(a3);
155: Object b2Id = pm.getObjectId(b2);
156: Object b3Id = pm.getObjectId(b3);
157:
158: pm.close();
159:
160: // We delete a3 : as bs relationship in A has a cascade delete extension with the true value,
161: // b2 and b3 should be delete.
162: pm = pmf.getPersistenceManager();
163: pm.currentTransaction().begin();
164: pm.deletePersistent(a3);
165: pm.currentTransaction().commit();
166: pm.close();
167:
168: pm = pmf.getPersistenceManager();
169:
170: // a1 should only have b1
171: assertTrue("a1.getBs() has " + a1.getBs().size()
172: + " elements, expected 1", a1.getBs().size() == 1);
173: assertTrue("a1 should contain b1", a1.getBs().contains(b1));
174: assertTrue("a1 should not contain b2", !a1.getBs()
175: .contains(b2));
176:
177: // a2 should only have b1
178: assertTrue("a2.getBs() has " + a2.getBs().size()
179: + " 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()
182: .contains(b3));
183:
184: // a3 should not exist anymore
185: try {
186: a3 = (A) pm.getObjectById(a3Id, true);
187: Assert
188: .fail("Should have thrown a JDOUserException : a3 has not been deleted");
189: } catch (JDOUserException jdoEx) {
190: // The test has passed
191: }
192:
193: // b2 should not exist anymore
194: try {
195: b2 = (B) pm.getObjectById(b2Id, true);
196: Assert
197: .fail("Should have thrown a JDOUserException : b2 has not been deleted");
198: } catch (JDOUserException jdoEx) {
199: // The test has passed
200: }
201:
202: // b3 should not exist anymore
203: try {
204: b3 = (B) pm.getObjectById(b3Id, true);
205: Assert
206: .fail("Should have thrown a JDOUserException : b3 has not been deleted");
207: } catch (JDOUserException jdoEx) {
208: // The test has passed
209: }
210:
211: // b1 should still contain a1 and a2, but no more a3
212: assertTrue("b1 should contain a1", b1.getAs().contains(a1));
213: assertTrue("b1 should contain a2", b1.getAs().contains(a2));
214: assertTrue("b1 should not contain a3", !b1.getAs()
215: .contains(a3));
216:
217: pm.close();
218:
219: } finally {
220: pm = pmf.getPersistenceManager();
221: pm.currentTransaction().begin();
222: pm.deletePersistent(a1);
223: pm.deletePersistent(a2);
224: pm.deletePersistent(b1);
225: pm.currentTransaction().commit();
226: pm.close();
227: }
228: }
229: }
|