1 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 34 public class TestCascadeDeleteSimpleRef extends SpeedoTestHelper { 35 36 public TestCascadeDeleteSimpleRef(String s) { 37 super(s); 38 } 39 40 protected String getLoggerName() { 41 return LOG_NAME + "rt.ref.CascadeDeleteSimpleRef"; 42 } 43 44 45 49 public void testCascadeDeleteOneToNone() { 50 PersistenceManager pm = pmf.getPersistenceManager(); 51 52 String gName = "SpeedoCascade"; 54 Group grp = new Group(gName); 55 56 pm.makePersistent(grp); 58 59 Object gId = pm.getObjectId(grp); 61 62 pm.close(); 63 64 grp = null; 65 66 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 74 pm.currentTransaction().begin(); 75 pm.deletePersistent(grp); 76 pm.currentTransaction().commit(); 77 pm.close(); 78 79 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 Assert.assertTrue(true); 88 } 89 90 pm.close(); 91 } 92 93 94 98 public void testCascadeDeleteOneToOne() { 99 PersistenceManager pm = pmf.getPersistenceManager(); 100 101 String pName = "Boss"; 103 President pres = new President(pName); 104 105 String gName = "SpeedoCascade"; 107 Group grp = new Group(gName, pres); 108 109 pm.makePersistent(grp); 111 112 Object gId = pm.getObjectId(grp); 114 Object pId = pm.getObjectId(grp.getPresident()); 115 116 pm.close(); 117 118 grp = null; 119 pres = null; 120 121 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 133 pm.currentTransaction().begin(); 134 pm.deletePersistent(grp); 135 pm.currentTransaction().commit(); 136 pm.close(); 137 138 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 Assert.assertTrue(true); 147 } 148 149 pm.close(); 150 } 151 152 153 156 public void testCascadeDeleteOneToTwo() { 157 PersistenceManager pm = pmf.getPersistenceManager(); 158 159 String gName = "SpeedoCascade"; 161 Group grp = new Group(gName); 162 163 String m1Name = "Jean"; 165 String m2Name = "Sophie"; 166 Member mem1 = new Member(m1Name, grp); 167 Member mem2 = new Member(m2Name, grp); 168 169 pm.makePersistent(mem1); 171 pm.makePersistent(mem2); 172 173 Object m1Id = pm.getObjectId(mem1); 175 Object m2Id = pm.getObjectId(mem2); 176 Object gId = pm.getObjectId(grp); 177 178 pm.close(); 179 180 mem1 = null; 181 mem2 = null; 182 grp = null; 183 184 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 199 pm.currentTransaction().begin(); 200 pm.deletePersistent(grp); 201 pm.currentTransaction().commit(); 202 pm.close(); 203 204 pm = pmf.getPersistenceManager(); 206 try { 207 mem1 = (Member) pm.getObjectById(m1Id, true); 208 209 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 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 Assert.assertTrue(true); 227 } 228 229 pm.close(); 230 } 231 232 233 236 public void testCascadeDeleteOneToMany() { 237 PersistenceManager pm = pmf.getPersistenceManager(); 238 239 String gName = "SpeedoCascade"; 241 Group grp = new Group(gName); 242 243 String m1Name = "Jean"; 245 String m2Name = "Sophie"; 246 String m3Name = "Alain"; 247 String m4Name = "Sylvie"; 248 String m5Name = "Jeanne"; 249 String m6Name = "Rose"; 250 String m7Name = "Baptiste"; 251 String m8Name = "Alexandre"; 252 String m9Name = "Noemie"; 253 String 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 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 Object m1Id = pm.getObjectId(mem1); 279 Object m2Id = pm.getObjectId(mem2); 280 Object m3Id = pm.getObjectId(mem3); 281 Object m4Id = pm.getObjectId(mem4); 282 Object m5Id = pm.getObjectId(mem5); 283 Object m6Id = pm.getObjectId(mem6); 284 Object m7Id = pm.getObjectId(mem7); 285 Object m8Id = pm.getObjectId(mem8); 286 Object m9Id = pm.getObjectId(mem9); 287 Object m10Id = pm.getObjectId(mem10); 288 Object 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 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 343 pm.currentTransaction().begin(); 344 pm.deletePersistent(grp); 345 pm.currentTransaction().commit(); 346 pm.close(); 347 348 pm = pmf.getPersistenceManager(); 350 try { 351 mem1 = (Member) pm.getObjectById(m1Id, true); 352 353 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 } 371 372 try { 374 mem2 = (Member) pm.getObjectById(m2Id, true); 375 376 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 } 393 394 try { 396 mem3 = (Member) pm.getObjectById(m3Id, true); 397 398 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 } 414 415 try { 417 mem4 = (Member) pm.getObjectById(m4Id, true); 418 419 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 } 434 435 try { 437 mem5 = (Member) pm.getObjectById(m5Id, true); 438 439 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 } 453 454 try { 456 mem6 = (Member) pm.getObjectById(m6Id, true); 457 458 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 } 471 472 try { 474 mem7 = (Member) pm.getObjectById(m7Id, true); 475 476 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 } 488 489 try { 491 mem8 = (Member) pm.getObjectById(m8Id, true); 492 493 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 } 504 505 try { 507 mem9 = (Member) pm.getObjectById(m9Id, true); 508 509 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 } 519 520 521 try { 523 mem10 = (Member) pm.getObjectById(m10Id, true); 524 525 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 Assert.assertTrue(true); 534 } 535 536 pm.close(); 537 } 538 } 539 | Popular Tags |