1 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 s) { 42 super(s); 43 } 44 45 protected String getLoggerName() { 46 return LOG_NAME + "rt.relations.CascadeDeleteRelations"; 47 } 48 49 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.setB(b1); 61 assertTrue(b1.getA() == a1); 62 63 a2.setB(b2); 65 assertTrue(b2.getA() == a2); 66 67 PersistenceManager pm = pmf.getPersistenceManager(); 69 pm.makePersistent(a1); 70 pm.makePersistent(a2); 71 72 Object a1Id = pm.getObjectId(a1); 74 Object a2Id = pm.getObjectId(a2); 75 Object b1Id = pm.getObjectId(b1); 76 Object 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 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 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 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 a1.getBs().add(b1); 138 assertTrue(b1.getAs().contains(a1)); 139 a1.getBs().add(b2); 140 assertTrue(b2.getAs().contains(a1)); 141 142 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 a3.getBs().add(b2); 152 assertTrue(b2.getAs().contains(a3)); 153 a3.getBs().add(b3); 154 assertTrue(b3.getAs().contains(a3)); 155 156 Object a3Id = pm.getObjectId(a3); 158 Object b2Id = pm.getObjectId(b2); 159 Object b3Id = pm.getObjectId(b3); 160 161 pm.close(); 162 163 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 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 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 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 } 191 192 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 } 200 201 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 } 209 210 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 |