1 18 package org.objectweb.speedo.runtime.relations; 19 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 25 import javax.jdo.PersistenceManager; 26 import javax.jdo.Query; 27 28 import org.objectweb.speedo.SpeedoTestHelper; 29 import org.objectweb.speedo.pobjects.relations.C; 30 import org.objectweb.speedo.pobjects.relations.D; 31 import org.objectweb.util.monolog.api.BasicLevel; 32 33 37 public class TestRelationsSeqId extends SpeedoTestHelper { 38 39 public TestRelationsSeqId(String s) { 40 super(s); 41 } 42 43 protected String getLoggerName() { 44 return LOG_NAME + "rt.relations.Relations"; 45 } 46 47 public void testOneOne() { 48 C c1 = new C("testOneOne_c1"); 49 C c2 = new C("testOneOne_c2"); 50 D d1 = new D("testOneOne_d1"); 51 D d2 = new D("testOneOne_d2"); 52 53 c1.setD(d1); 54 assertTrue(d1.getC() == c1); 55 56 c2.setD(d2); 57 assertTrue(d2.getC() == c2); 58 59 c1.setD(d2); 60 assertTrue(d1.getC() == null); 61 assertTrue(d2.getC() == c1); 62 assertTrue(c2.getD() == null); 63 64 d1.setC(c2); 65 assertTrue(c2.getD() == d1); 66 67 d1.setC(c1); 68 assertTrue(c1.getD() == d1); 69 assertTrue(d2.getC() == null); 70 assertTrue(c2.getD() == null); 71 } 72 73 public void testOneOne2() { 74 C c1 = new C("testOneOne_c1"); 75 PersistenceManager pm = pmf.getPersistenceManager(); 76 77 pm.currentTransaction().begin(); 78 pm.makePersistent(c1); 79 Object c1oid = pm.getObjectId(c1); 80 c1 = null; 81 pm.currentTransaction().commit(); 82 83 pm.evictAll(); 84 85 pm.currentTransaction().begin(); 86 c1 = (C) pm.getObjectById(c1oid, false); 87 pm.deletePersistent(c1); 88 pm.currentTransaction().commit(); 89 90 pm.close(); 91 } 92 93 public void testOneMany() { C c1 = new C("testOneMany_c1"); 95 C c2 = new C("testOneMany_c2"); 96 97 D d1 = new D("testOneMany_d1"); 98 D d2 = new D("testOneMany_d2"); 99 D d3 = new D("testOneMany_d3"); 100 D d4 = new D("testOneMany_d4"); 101 102 PersistenceManager pm = pmf.getPersistenceManager(); 103 pm.makePersistent(c1); 104 pm.makePersistent(c2); 105 pm.makePersistent(d1); 106 pm.makePersistent(d2); 107 pm.makePersistent(d3); 108 pm.makePersistent(d4); 109 pm.close(); 110 111 try { 112 pm = pmf.getPersistenceManager(); 113 114 c1.getNds().add(d1); 115 assertTrue(d1.getNc() == c1); 116 c1.getNds().add(d2); 117 assertTrue(d2.getNc() == c1); 118 119 c2.getNds().add(d3); 120 assertTrue(d3.getNc() == c2); 121 c2.getNds().add(d4); 122 assertTrue(d4.getNc() == c2); 123 124 c1.getNds().add(d3); 125 assertTrue(d3.getNc() == c1); 126 assertTrue(!c2.getNds().contains(d3)); 127 128 c1.getNds().remove(d1); 129 assertTrue(d1.getNc() == null); 130 131 d3.setNc(c2); 132 assertTrue(!c1.getNds().contains(d3)); 133 assertTrue(c2.getNds().contains(d3)); 134 135 c1.setNds(c2.getNds()); 136 pm.close(); 138 pm = pmf.getPersistenceManager(); 139 140 assertTrue(!c1.getNds().contains(d1)); 141 assertTrue(!c1.getNds().contains(d2)); 142 assertTrue(c1.getNds().contains(d3)); 143 assertTrue(c1.getNds().contains(d4)); 144 145 assertTrue(!c2.getNds().contains(d1)); 146 assertTrue(!c2.getNds().contains(d2)); 147 assertTrue(!c2.getNds().contains(d3)); 148 assertTrue(!c2.getNds().contains(d4)); 149 150 assertTrue(d1.getNc() == null); 151 assertTrue(d2.getNc() == null); 152 assertTrue(d3.getNc() == c1); 153 assertTrue(d4.getNc() == c1); 154 155 pm.close(); 156 } finally { 157 pm = pmf.getPersistenceManager(); 158 pm.currentTransaction().begin(); 159 pm.deletePersistent(c1); 160 pm.deletePersistent(c2); 161 pm.deletePersistent(d1); 162 pm.deletePersistent(d2); 163 pm.deletePersistent(d3); 164 pm.deletePersistent(d4); 165 pm.currentTransaction().commit(); 166 pm.close(); 167 } 168 } 169 170 public void testOneMany2() { C c1 = new C("testOneMany2_c1"); 172 C c2 = new C("testOneMany2_c2"); 173 174 D d1 = new D("testOneMany2_d1"); 175 D d2 = new D("testOneMany2_d2"); 176 D d3 = new D("testOneMany2_d3"); 177 D d4 = new D("testOneMany2_d4"); 178 179 PersistenceManager pm = pmf.getPersistenceManager(); 180 181 d1.setNc(c1); 182 assertTrue(c1.getNds() != null); 184 assertTrue(c1.getNds().contains(d1)); 185 186 d2.setNc(c1); 187 assertTrue(c1.getNds().contains(d2)); 188 189 d3.setNc(c2); 190 assertTrue(c2.getNds() != null); 191 assertTrue(c2.getNds().contains(d3)); 192 193 d4.setNc(c2); 194 assertTrue(c2.getNds().contains(d4)); 195 196 pm.makePersistent(c1); 197 pm.makePersistent(c2); 198 199 pm.close(); 200 201 try { 202 pm = pmf.getPersistenceManager(); 203 204 Collection c = new ArrayList (); 205 c.addAll(c2.getNds()); 206 c1.setNds(c); 207 208 pm.close(); 209 210 pm = pmf.getPersistenceManager(); 211 212 assertTrue(!c1.getNds().contains(d1)); 213 assertTrue(!c1.getNds().contains(d2)); 214 assertTrue(c1.getNds().contains(d3)); 215 assertTrue(c1.getNds().contains(d4)); 216 217 assertTrue(c2.getNds().size() == 0); 218 219 assertTrue(d1.getNc() == null); 220 assertTrue(d2.getNc() == null); 221 assertTrue(d3.getNc() == c1); 222 assertTrue(d4.getNc() == c1); 223 224 pm.close(); 225 } finally { 226 pm = pmf.getPersistenceManager(); 227 pm.currentTransaction().begin(); 228 pm.deletePersistent(c1); 229 pm.deletePersistent(c2); 230 pm.deletePersistent(d1); 231 pm.deletePersistent(d2); 232 pm.deletePersistent(d3); 233 pm.deletePersistent(d4); 234 pm.currentTransaction().commit(); 235 pm.close(); 236 } 237 } 238 239 public void testOneMany3() { C c1 = new C("testOneMany3_c1"); 241 242 D d1 = new D("testOneMany3_d1"); 243 D d2 = new D("testOneMany3_d2"); 244 245 PersistenceManager pm = pmf.getPersistenceManager(); 246 247 ArrayList l = new ArrayList (); 248 l.add(d1); 249 l.add(d2); 250 c1.setNds(l); 251 252 d1.setNc(c1); 253 d2.setNc(c1); 254 255 assertTrue(c1.getNds().size() == 2); 256 257 pm.makePersistent(c1); 258 pm.close(); 259 260 try { 261 pm = pmf.getPersistenceManager(); 262 assertTrue(c1.getNds().size() == 2); 263 pm.close(); 264 } finally { 265 pm = pmf.getPersistenceManager(); 266 pm.currentTransaction().begin(); 267 pm.deletePersistent(c1); 268 pm.deletePersistent(d1); 269 pm.deletePersistent(d2); 270 pm.currentTransaction().commit(); 271 pm.close(); 272 } 273 } 274 275 public void testOneMany4() { PersistenceManager pm = pmf.getPersistenceManager(); 277 C c1 = new C("testOneMany4_c1"); 278 D d1 = new D("testOneMany4_d1"); 279 ArrayList l = new ArrayList (); 280 l.add(d1); 281 c1.setNds(l); 282 d1.setNc(c1); 283 assertTrue(c1.getNds().size() == 1); 284 285 pm.currentTransaction().begin(); 286 pm.makePersistent(c1); 287 pm.currentTransaction().commit(); 288 try { 289 assertNotNull("c1 not written", found(C.class, "testOneMany4_c1", pm)); 290 assertNotNull("d1 not written", found(D.class, "testOneMany4_d1", pm)); 291 } finally { 292 pm.close(); 293 } 294 295 pm = pmf.getPersistenceManager(); 296 D d2 = new D("testOneMany4_d2"); 297 d2.setNc(c1); 298 pm.close(); 299 300 pm = pmf.getPersistenceManager(); 301 pm.currentTransaction().begin(); 302 pm.deletePersistent(c1); 303 pm.deletePersistent(d1); 304 pm.deletePersistent(d2); 305 pm.currentTransaction().commit(); 306 pm.close(); 307 pm = pmf.getPersistenceManager(); 308 try { 309 assertNull("c1 was not removed", found(C.class, "testOneMany4_c1", pm)); 310 assertNull("d1 was not removed", found(D.class, "testOneMany4_d1", pm)); 311 assertNull("d2 was not removed", found(D.class, "testOneMany4_d2", pm)); 312 } finally { 313 pm.close(); 314 } 315 } 316 317 public void testManyMany() { C c1 = new C("testManyMany_c1"); 319 C c2 = new C("testManyMany_c2"); 320 C c3 = new C("testManyMany_c3"); 321 322 D d1 = new D("testManyMany_d1"); 323 D d2 = new D("testManyMany_d2"); 324 D d3 = new D("testManyMany_d3"); 325 326 PersistenceManager pm = pmf.getPersistenceManager(); 327 pm.makePersistent(c1); 328 pm.makePersistent(c2); 329 pm.makePersistent(c3); 330 pm.makePersistent(d1); 331 pm.makePersistent(d2); 332 pm.makePersistent(d3); 333 pm.close(); 334 335 try { 336 pm = pmf.getPersistenceManager(); 337 c1.getDs().add(d1); 338 assertTrue(d1.getCs().contains(c1)); 339 c1.getDs().add(d2); 340 assertTrue(d2.getCs().contains(c1)); 341 342 c2.getDs().add(d1); 343 assertTrue(d1.getCs().contains(c2)); 344 c2.getDs().add(d2); 345 assertTrue(d2.getCs().contains(c2)); 346 c2.getDs().add(d3); 347 assertTrue(d3.getCs().contains(c2)); 348 349 c3.getDs().add(d2); 350 assertTrue(d2.getCs().contains(c3)); 351 c3.getDs().add(d3); 352 assertTrue(d3.getCs().contains(c3)); 353 354 c1.getDs().add(d3); 355 assertTrue(d3.getCs().contains(c1)); 356 357 c1.getDs().remove(d1); 358 assertTrue(!d1.getCs().contains(c1)); 359 360 d2.getCs().remove(c2); 361 assertTrue(!c2.getDs().contains(d2)); 362 363 c1.setDs(c2.getDs()); 364 pm.close(); 366 pm = pmf.getPersistenceManager(); 367 assertTrue(c1.getDs().contains(d1)); 368 assertTrue(!c1.getDs().contains(d2)); 369 assertTrue(c1.getDs().contains(d3)); 370 371 assertTrue(c2.getDs().contains(d1)); 372 assertTrue(!c2.getDs().contains(d2)); 373 assertTrue(c2.getDs().contains(d3)); 374 375 assertTrue(!c3.getDs().contains(d1)); 376 assertTrue(c3.getDs().contains(d2)); 377 assertTrue(c3.getDs().contains(d3)); 378 379 assertTrue(d1.getCs().contains(c1)); 380 assertTrue(d1.getCs().contains(c2)); 381 assertTrue(!d1.getCs().contains(c3)); 382 383 assertTrue(!d2.getCs().contains(c1)); 384 assertTrue(!d2.getCs().contains(c2)); 385 assertTrue(d2.getCs().contains(c3)); 386 387 assertTrue(d3.getCs().contains(c1)); 388 assertTrue(d3.getCs().contains(c2)); 389 assertTrue(d3.getCs().contains(c3)); 390 391 pm.close(); 392 } finally { 393 pm = pmf.getPersistenceManager(); 394 pm.currentTransaction().begin(); 395 pm.deletePersistent(c1); 396 pm.deletePersistent(c2); 397 pm.deletePersistent(c3); 398 pm.deletePersistent(d1); 399 pm.deletePersistent(d2); 400 pm.deletePersistent(d3); 401 pm.currentTransaction().commit(); 402 pm.close(); 403 } 404 } 405 406 public void testManyMany2() { C c1 = new C("testManyMany2_c1"); 408 C c2 = new C("testManyMany2_c2"); 409 C c3 = new C("testManyMany2_c3"); 410 411 D d1 = new D("testManyMany2_d1"); 412 D d2 = new D("testManyMany2_d2"); 413 D d3 = new D("testManyMany2_d3"); 414 415 PersistenceManager pm = pmf.getPersistenceManager(); 416 417 c1.getDs().add(d1); 418 c1.getDs().add(d2); 419 420 c2.getDs().add(d1); 421 c2.getDs().add(d2); 422 c2.getDs().add(d3); 423 424 c3.getDs().add(d2); 425 c3.getDs().add(d3); 426 427 pm.makePersistent(c1); 428 pm.makePersistent(c2); 429 pm.makePersistent(c3); 430 pm.close(); 431 432 try { 433 pm = pmf.getPersistenceManager(); 434 assertTrue(d1.getCs().contains(c1)); 435 assertTrue(d2.getCs().contains(c1)); 436 437 assertTrue(d1.getCs().contains(c2)); 438 assertTrue(d2.getCs().contains(c2)); 439 assertTrue(d3.getCs().contains(c2)); 440 441 assertTrue(d2.getCs().contains(c3)); 442 assertTrue(d3.getCs().contains(c3)); 443 444 Collection c = new ArrayList (); 445 c.addAll(c2.getDs()); 446 c1.setDs(c); 447 448 pm.close(); 449 450 pm = pmf.getPersistenceManager(); 451 assertTrue(c1.getDs().contains(d1)); 452 assertTrue(c1.getDs().contains(d2)); 453 assertTrue(c1.getDs().contains(d3)); 454 455 assertTrue(c2.getDs().contains(d1)); 456 assertTrue(c2.getDs().contains(d2)); 457 assertTrue(c2.getDs().contains(d3)); 458 459 assertTrue(!c3.getDs().contains(d1)); 460 assertTrue(c3.getDs().contains(d2)); 461 assertTrue(c3.getDs().contains(d3)); 462 463 assertTrue(d1.getCs().contains(c1)); 464 assertTrue(d1.getCs().contains(c2)); 465 assertTrue(!d1.getCs().contains(c3)); 466 467 assertTrue(d2.getCs().contains(c1)); 468 assertTrue(d2.getCs().contains(c2)); 469 assertTrue(d2.getCs().contains(c3)); 470 471 assertTrue(d3.getCs().contains(c1)); 472 assertTrue(d3.getCs().contains(c2)); 473 assertTrue(d3.getCs().contains(c3)); 474 475 pm.close(); 476 } finally { 477 pm = pmf.getPersistenceManager(); 478 pm.currentTransaction().begin(); 479 pm.deletePersistent(c1); 480 pm.deletePersistent(c2); 481 pm.deletePersistent(c3); 482 pm.deletePersistent(d1); 483 pm.deletePersistent(d2); 484 pm.deletePersistent(d3); 485 pm.currentTransaction().commit(); 486 pm.close(); 487 } 488 } 489 490 public void testManyMany3() { C c1 = new C("testManyMany3_c1"); 492 C c2 = new C("testManyMany3_c2"); 493 C c3 = new C("testManyMany3_c3"); 494 495 D d1 = new D("testManyMany3_d1"); 496 D d2 = new D("testManyMany3_d2"); 497 D d3 = new D("testManyMany3_d3"); 498 499 PersistenceManager pm = pmf.getPersistenceManager(); 500 501 c1.setDs(Arrays.asList(new Object [] { d1, d2 })); 502 c2.setDs(Arrays.asList(new Object [] { d1, d2, d3 })); 503 c3.setDs(Arrays.asList(new Object [] { d2, d3 })); 504 505 d1.setCs(Arrays.asList(new Object [] { c1, c2 })); 506 d2.setCs(Arrays.asList(new Object [] { c1, c2, c3 })); 507 d3.setCs(Arrays.asList(new Object [] { c2, c3 })); 508 509 pm.makePersistent(c1); 510 pm.makePersistent(c2); 511 pm.makePersistent(c3); 512 pm.close(); 513 514 try { 515 pm = pmf.getPersistenceManager(); 516 assertTrue(c1.getDs().size() == 2); 517 assertTrue(c1.getDs().contains(d1)); 518 assertTrue(c1.getDs().contains(d2)); 519 520 assertTrue(c2.getDs().size() == 3); 521 assertTrue(c2.getDs().contains(d1)); 522 assertTrue(c2.getDs().contains(d2)); 523 assertTrue(c2.getDs().contains(d3)); 524 525 assertTrue(c3.getDs().size() == 2); 526 assertTrue(c3.getDs().contains(d2)); 527 assertTrue(c3.getDs().contains(d3)); 528 529 assertTrue(d1.getCs().size() == 2); 530 assertTrue(d1.getCs().contains(c1)); 531 assertTrue(d1.getCs().contains(c2)); 532 533 assertTrue(d2.getCs().size() == 3); 534 assertTrue(d2.getCs().contains(c1)); 535 assertTrue(d2.getCs().contains(c2)); 536 assertTrue(d2.getCs().contains(c3)); 537 538 assertTrue(d3.getCs().size() == 2); 539 assertTrue(d3.getCs().contains(c2)); 540 assertTrue(d3.getCs().contains(c3)); 541 542 pm.close(); 543 } finally { 544 pm = pmf.getPersistenceManager(); 545 pm.currentTransaction().begin(); 546 pm.deletePersistent(c1); 547 pm.deletePersistent(c2); 548 pm.deletePersistent(c3); 549 pm.deletePersistent(d1); 550 pm.deletePersistent(d2); 551 pm.deletePersistent(d3); 552 pm.currentTransaction().commit(); 553 pm.close(); 554 } 555 } 556 557 public void testManyManyDelete() { 558 C c1 = new C("testManyManyDelete_c1"); 559 C c2 = new C("testManyManyDelete_c2"); 560 C c3 = new C("testManyManyDelete_c3"); 561 562 D d1 = new D("testManyManyDelete_d1"); 563 D d2 = new D("testManyManyDelete_d2"); 564 D d3 = new D("testManyManyDelete_d3"); 565 566 PersistenceManager pm = pmf.getPersistenceManager(); 567 pm.makePersistent(c1); 568 pm.makePersistent(c2); 569 pm.makePersistent(c3); 570 pm.makePersistent(d1); 571 pm.makePersistent(d2); 572 pm.makePersistent(d3); 573 logger.log(BasicLevel.DEBUG, "testManyManyDelete:after makePersistent"); 574 pm.close(); 575 logger.log(BasicLevel.DEBUG, "testManyManyDelete:after close"); 576 577 try { 578 pm = pmf.getPersistenceManager(); 579 580 c1.getDs().add(d1); 581 582 c1.getDs().add(d2); 583 584 c2.getDs().add(d1); 585 c2.getDs().add(d2); 586 c2.getDs().add(d3); 587 588 c3.getDs().add(d2); 589 c3.getDs().add(d3); 590 591 logger.log(BasicLevel.DEBUG, "testManyManyDelete:after ad collection content"); 592 593 pm.close(); 594 logger.log(BasicLevel.DEBUG, "testManyManyDelete:after close"); 595 596 pm = pmf.getPersistenceManager(); 597 pm.currentTransaction().begin(); 598 assertTrue(d1.getCs().contains(c1)); 599 assertTrue(d2.getCs().contains(c1)); 600 assertTrue(d1.getCs().contains(c2)); 601 assertTrue(d2.getCs().contains(c2)); 602 assertTrue(d3.getCs().contains(c2)); 603 assertTrue(d2.getCs().contains(c3)); 604 assertTrue(d3.getCs().contains(c3)); 605 606 pm.deletePersistent(d2); 607 logger.log(BasicLevel.DEBUG, "testManyManyDelete:after delete d2"); 608 pm.currentTransaction().commit(); 609 pm.close(); 610 logger.log(BasicLevel.DEBUG, "testManyManyDelete:after close"); 611 612 pm = pmf.getPersistenceManager(); 613 614 assertTrue(c1.getDs().size() == 1); 615 logger.log(BasicLevel.DEBUG, "testManyManyDelete:c1.nds.size()==1 : " + (c1.getDs().size()==1)); 616 assertTrue(c1.getDs().contains(d1)); 617 logger.log(BasicLevel.DEBUG, "testManyManyDelete:c1.nds.contains(d1) : " + (c1.getDs().contains(d1))); 618 619 assertTrue(c2.getDs().size() == 2); 620 logger.log(BasicLevel.DEBUG, "testManyManyDelete:c2.nds.size()==1 : " + (c2.getDs().size()==1)); 621 assertTrue(c2.getDs().contains(d1)); 622 logger.log(BasicLevel.DEBUG, "testManyManyDelete:c2.nds.contains(d1) : " + (c2.getDs().contains(d1))); 623 assertTrue(c2.getDs().contains(d3)); 624 logger.log(BasicLevel.DEBUG, "testManyManyDelete:c2.nds.contains(d3) : " + (c2.getDs().contains(d3))); 625 626 assertTrue(c3.getDs().size() == 1); 627 logger.log(BasicLevel.DEBUG, "testManyManyDelete:c3.nds.size()==1 : " + (c3.getDs().size()==1)); 628 assertTrue(c3.getDs().contains(d3)); 629 logger.log(BasicLevel.DEBUG, "testManyManyDelete:c3.nds.contains(d3) : " + (c3.getDs().contains(d3))); 630 631 assertTrue(d1.getCs().contains(c1)); 632 logger.log(BasicLevel.DEBUG, "testManyManyDelete:d1.cs.contains(c1) : " + (d1.getCs().contains(c1))); 633 assertTrue(d1.getCs().contains(c2)); 634 logger.log(BasicLevel.DEBUG, "testManyManyDelete:d1.cs.contains(c2) : " + (d1.getCs().contains(c2))); 635 assertTrue(!d1.getCs().contains(c3)); 636 logger.log(BasicLevel.DEBUG, "testManyManyDelete:!d1.cs.contains(c3) : " + (!d1.getCs().contains(c3))); 637 638 assertTrue(!d3.getCs().contains(c1)); 639 logger.log(BasicLevel.DEBUG, "testManyManyDelete:!d3.cs.contains(c1) : " + (!d3.getCs().contains(c1))); 640 assertTrue(d3.getCs().contains(c2)); 641 logger.log(BasicLevel.DEBUG, "testManyManyDelete:d3.cs.contains(c2) : " + (d3.getCs().contains(c2))); 642 assertTrue(d3.getCs().contains(c3)); 643 logger.log(BasicLevel.DEBUG, "testManyManyDelete:d3.cs.contains(c3) : " + (d3.getCs().contains(c3))); 644 645 pm.close(); 646 logger.log(BasicLevel.DEBUG, "testManyManyDelete:after close()"); 647 } finally { 648 pm = pmf.getPersistenceManager(); 649 pm.currentTransaction().begin(); 650 pm.deletePersistent(c1); 651 pm.deletePersistent(c2); 652 pm.deletePersistent(c3); 653 pm.deletePersistent(d1); 654 pm.deletePersistent(d3); 655 logger.log(BasicLevel.DEBUG, "testManyManyDelete:after deletes()"); 656 pm.currentTransaction().commit(); 657 pm.close(); 658 logger.log(BasicLevel.DEBUG, "testManyManyDelete:after close()"); 659 } 660 logger.log(BasicLevel.DEBUG, "testManyManyDelete:end"); 661 } 662 663 private Object found(Class clazz, String name, PersistenceManager pm) { 664 Query q = pm.newQuery(clazz); 665 q.setFilter("(name == p1)"); 666 q.declareParameters("String p1"); 667 Collection res = (Collection ) q.execute(name); 668 Iterator it = res.iterator(); 669 try { 670 if (it.hasNext()) { 671 return it.next(); 672 } else { 673 return null; 674 } 675 } finally { 676 q.closeAll(); 677 } 678 } 679 } 680 | Popular Tags |