1 package org.apache.ojb.odmg; 2 3 import java.io.Serializable ; 4 import java.util.ArrayList ; 5 import java.util.Arrays ; 6 import java.util.Collection ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.Vector ; 10 11 import org.apache.ojb.broker.PersistenceBroker; 12 import org.apache.ojb.broker.PersistenceBrokerFactory; 13 import org.apache.ojb.broker.metadata.CollectionDescriptor; 14 import org.apache.ojb.broker.query.Criteria; 15 import org.apache.ojb.broker.query.Query; 16 import org.apache.ojb.broker.query.QueryFactory; 17 import org.apache.ojb.junit.ODMGTestCase; 18 import org.apache.commons.lang.builder.ToStringBuilder; 19 import org.apache.commons.lang.builder.ToStringStyle; 20 import org.odmg.OQLQuery; 21 import org.odmg.Transaction; 22 23 29 public class CollectionsTest extends ODMGTestCase 30 { 31 public CollectionsTest(String s) 32 { 33 super(s); 34 } 35 36 public static void main(String [] args) 37 { 38 String [] arr = {CollectionsTest.class.getName()}; 39 junit.textui.TestRunner.main(arr); 40 } 41 42 public void setUp() throws Exception 43 { 44 super.setUp(); 45 } 46 47 protected void tearDown() throws Exception 48 { 49 super.tearDown(); 50 } 51 52 53 public void testStoreDeleteCascadeDelete() throws Exception 54 { 55 String prefix = "testStoreDeleteCascadeDelete_" + System.currentTimeMillis(); 56 String queryStr = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1"; 57 58 ojbChangeReferenceSetting(Gatherer.class, "collectiblesA", true, 59 CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_OBJECT, false); 60 61 Gatherer gat = new Gatherer(null, prefix + "_Gatherer"); 63 CollectibleA[] cols = prepareCollectibleA(gat, prefix); 64 List colList = Arrays.asList(cols); 65 gat.setCollectiblesA(colList); 67 TransactionExt tx = (TransactionExt)odmg.newTransaction(); 68 tx.begin(); 69 database.makePersistent(gat); 70 tx.commit(); 71 72 tx.begin(); 74 tx.getBroker().clearCache(); 75 OQLQuery query = odmg.newOQLQuery(); 76 query.create(queryStr); 77 Integer gatId = gat.getGatId(); 78 assertNotNull(gatId); 79 query.bind(gatId); 80 Collection result = (Collection ) query.execute(); 81 tx.commit(); 82 assertEquals("Wrong number of objects found", 1, result.size()); 83 Gatherer fetchedGat = (Gatherer) result.iterator().next(); 84 85 List colsA = fetchedGat.getCollectiblesA(); 86 assertEquals("Wrong number of CollectiblesA", 3, colsA.size()); 87 tx.begin(); 89 database.deletePersistent(fetchedGat); 91 tx.commit(); 93 94 tx.begin(); 96 query = odmg.newOQLQuery(); 97 query.create("select allCollectibleA from " + CollectibleA.class.getName() + 98 " where name like $1"); 99 query.bind(prefix + "%"); 100 result = (Collection ) query.execute(); 101 assertEquals("Wrong number of objects found", 0, result.size()); 102 tx.commit(); 103 104 tx.begin(); 107 query = odmg.newOQLQuery(); 108 query.create(queryStr); 109 query.bind(gatId); 110 result = (Collection ) query.execute(); 111 assertEquals("Wrong number of objects found", 0, result.size()); 112 tx.commit(); 113 } 114 115 public void testStoreCollectionElementWithoutBackReference() throws Exception 116 { 117 String queryGat = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1"; 119 String prefix = "testStoreCollectionElementWithoutBackReference_" + System.currentTimeMillis(); 120 121 Gatherer gat = new Gatherer(null, prefix + "_Gatherer"); 123 TransactionExt tx = (TransactionExt)odmg.newTransaction(); 124 tx.begin(); 125 database.makePersistent(gat); 126 tx.commit(); 127 tx.begin(); 129 tx.getBroker().clearCache(); 130 assertNotNull(gat.getGatId()); 131 OQLQuery query = odmg.newOQLQuery(); 132 query.create(queryGat); 133 query.bind(gat.getGatId()); 134 Collection result = (Collection ) query.execute(); 135 tx.commit(); 136 assertEquals("Wrong number of objects found", 1, result.size()); 137 gat = (Gatherer) result.iterator().next(); 138 assertNotNull(gat); 139 CollectibleC child = new CollectibleC(prefix, null, "a new CollectibleC"); 141 tx.begin(); 142 tx.lock(gat, Transaction.WRITE); 143 tx.lock(child, Transaction.WRITE); 144 List childs = new ArrayList (); 145 childs.add(child); 146 gat.setCollectiblesC(childs); 147 tx.commit(); 148 tx.begin(); 151 tx.getBroker().clearCache(); 152 assertNotNull(gat.getGatId()); 153 query = odmg.newOQLQuery(); 154 query.create(queryGat); 155 query.bind(gat.getGatId()); 156 result = (Collection ) query.execute(); 157 tx.commit(); 158 assertEquals("Wrong number of objects found", 1, result.size()); 159 gat = (Gatherer) result.iterator().next(); 160 assertNotNull(gat); 161 assertNotNull(gat.getCollectiblesC()); 162 assertEquals(1, gat.getCollectiblesC().size()); 163 } 164 165 170 public void testRemoveCollectionElementWithoutBackReference() throws Exception 171 { 172 String queryGat = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1"; 174 String prefix = "testDeleteCollectionElementWithoutBackReference_" + System.currentTimeMillis(); 175 176 Gatherer gat = new Gatherer(null, prefix + "_Gatherer"); 178 gat.setCollectiblesC(Arrays.asList(prepareCollectibleC(null, prefix))); 181 TransactionExt tx = (TransactionExt)odmg.newTransaction(); 182 tx.begin(); 183 database.makePersistent(gat); 184 tx.commit(); 185 186 tx.begin(); 188 tx.getBroker().clearCache(); 189 assertNotNull(gat.getGatId()); 190 191 OQLQuery query = odmg.newOQLQuery(); 192 query.create(queryGat); 193 query.bind(gat.getGatId()); 194 195 Collection result = (Collection ) query.execute(); 196 tx.commit(); 197 assertEquals("Wrong number of objects found", 1, result.size()); 198 Gatherer fetchedGat = (Gatherer) result.iterator().next(); 199 assertNotNull(fetchedGat); 200 201 List colC = fetchedGat.getCollectiblesC(); 203 assertEquals("Wrong number of CollectiblesC", 3, colC.size()); 204 205 tx.begin(); 206 tx.lock(fetchedGat, Transaction.WRITE); 208 Object toDelete = fetchedGat.getCollectiblesC().remove(0); 210 database.deletePersistent(toDelete); 212 tx.commit(); 215 216 tx.begin(); 218 tx.getBroker().clearCache(); 219 220 query = odmg.newOQLQuery(); 221 query.create("select colls from " + CollectibleC.class.getName() + 222 " where name like $1"); 223 query.bind(prefix + "%"); 224 result = (Collection ) query.execute(); 225 226 assertEquals("Wrong number of objects found", 2, result.size()); 227 tx.commit(); 228 229 tx.begin(); 232 tx.getBroker().clearCache(); 233 query = odmg.newOQLQuery(); 234 query.create(queryGat); 235 query.bind(gat.getGatId()); 236 result = (Collection ) query.execute(); 237 assertEquals("Wrong number of objects found", 1, result.size()); 238 fetchedGat = (Gatherer) result.iterator().next(); 239 colC = fetchedGat.getCollectiblesC(); 240 assertEquals("Wrong number of CollectiblesA found in Gatherer", 2, colC.size()); 241 tx.commit(); 242 assertNotNull(colC.get(0)); 243 } 244 245 public void testStoreFetchDeleteCollectionWithBackReference() throws Exception 246 { 247 String prefix = "testStoreFetchDeleteCollectionWithBackReference_" + System.currentTimeMillis(); 248 String queryStr = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1"; 249 250 Gatherer gat = new Gatherer(null, prefix + "_Gatherer"); 252 CollectibleA[] cols = prepareCollectibleA(gat, prefix); 253 List colList = Arrays.asList(cols); 254 gat.setCollectiblesA(colList); 256 TransactionExt tx = (TransactionExt)odmg.newTransaction(); 257 tx.begin(); 258 database.makePersistent(gat); 259 tx.commit(); 260 261 tx.begin(); 263 tx.getBroker().clearCache(); 264 OQLQuery query = odmg.newOQLQuery(); 265 query.create(queryStr); 266 Integer gatId = gat.getGatId(); 267 assertNotNull(gatId); 268 query.bind(gatId); 269 Collection result = (Collection ) query.execute(); 270 tx.commit(); 271 assertEquals("Wrong number of objects found", 1, result.size()); 272 Gatherer fetchedGat = (Gatherer) result.iterator().next(); 273 274 List colsA = fetchedGat.getCollectiblesA(); 275 assertEquals("Wrong number of CollectiblesA", 3, colsA.size()); 276 tx.begin(); 278 List newCols = new ArrayList (); 283 newCols.add(colsA.get(1)); 284 newCols.add(colsA.get(2)); 285 fetchedGat.setCollectiblesA(newCols); 286 tx.lock(fetchedGat, Transaction.WRITE); 287 database.deletePersistent(colsA.get(0)); 289 tx.commit(); 291 292 tx.begin(); 294 query = odmg.newOQLQuery(); 295 query.create("select allCollectibleA from " + CollectibleA.class.getName() + 296 " where name like $1"); 297 query.bind(prefix + "%"); 298 result = (Collection ) query.execute(); 299 assertEquals("Wrong number of objects found", 2, result.size()); 300 tx.commit(); 301 302 tx.begin(); 305 query = odmg.newOQLQuery(); 306 query.create(queryStr); 307 query.bind(gatId); 308 result = (Collection ) query.execute(); 309 assertEquals("Wrong number of objects found", 1, result.size()); 310 fetchedGat = (Gatherer) result.iterator().next(); 311 colsA = fetchedGat.getCollectiblesA(); 312 assertEquals("Wrong number of CollectiblesA found in Gatherer", 2, colsA.size()); 313 tx.commit(); 314 315 colsA.get(0); 316 } 317 318 public void testWithBackReference_1() throws Exception 319 { 320 String prefix = "testWithBackReference_1_" + System.currentTimeMillis(); 321 String queryStr = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1"; 322 323 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 324 tx.begin(); 325 Gatherer gat = new Gatherer(null, prefix + "_Gatherer"); 327 CollectibleA[] colsA = prepareCollectibleA(gat, prefix); 328 CollectibleB[] colsB = prepareCollectibleB(gat, prefix); 329 List colListA = Arrays.asList(colsA); 330 List colListB = Arrays.asList(colsB); 331 gat.setCollectiblesA(colListA); 333 gat.setCollectiblesB(colListB); 334 335 database.makePersistent(gat); 336 tx.commit(); 337 338 tx.begin(); 340 tx.getBroker().clearCache(); 341 OQLQuery query = odmg.newOQLQuery(); 342 query.create(queryStr); 343 Integer gatId = gat.getGatId(); 344 assertNotNull(gatId); 345 query.bind(gatId); 346 Collection result = (Collection ) query.execute(); 347 348 assertEquals("Wrong number of objects found", 1, result.size()); 349 Gatherer fetchedGat = (Gatherer) result.iterator().next(); 350 tx.lock(fetchedGat, Transaction.WRITE); 352 assertNotNull(fetchedGat.getCollectiblesA()); 353 assertNotNull(fetchedGat.getCollectiblesB()); 354 assertEquals(3, fetchedGat.getCollectiblesA().size()); 355 assertEquals(3, fetchedGat.getCollectiblesB().size()); 356 assertEquals(0, fetchedGat.getCollectiblesC().size()); 357 assertNotNull(fetchedGat.getCollectiblesA().get(0)); 358 assertNotNull(fetchedGat.getCollectiblesB().get(0)); 359 360 fetchedGat.getCollectiblesA().remove(0); 361 fetchedGat.getCollectiblesB().remove(0); 362 tx.commit(); 365 366 tx.begin(); 367 tx.getBroker().clearCache(); 368 query = odmg.newOQLQuery(); 369 query.create(queryStr); 370 gatId = gat.getGatId(); 371 assertNotNull(gatId); 372 query.bind(gatId); 373 result = (Collection ) query.execute(); 374 tx.commit(); 375 assertEquals("Wrong number of objects found", 1, result.size()); 376 fetchedGat = (Gatherer) result.iterator().next(); 377 378 assertNotNull(fetchedGat.getCollectiblesA()); 379 assertNotNull(fetchedGat.getCollectiblesB()); 380 assertEquals(2, fetchedGat.getCollectiblesA().size()); 381 assertEquals(2, fetchedGat.getCollectiblesB().size()); 382 assertNotNull(fetchedGat.getCollectiblesA().get(0)); 383 assertNotNull(fetchedGat.getCollectiblesB().get(0)); 384 } 385 386 390 public void testWithBackReference_2() throws Exception 391 { 392 if(ojbSkipKnownIssueProblem("Issue using proxies with circular references and a non-global-shared cache")) return; 393 394 420 421 String prefix = "testWithBackReference_2_" + System.currentTimeMillis(); 422 String queryStr = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1"; 423 424 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 425 tx.begin(); 426 Gatherer gat = new Gatherer(null, prefix + "_Gatherer"); 428 CollectibleA[] colsA = prepareCollectibleA(gat, prefix); 429 CollectibleB[] colsB = prepareCollectibleB(gat, prefix); 430 List colListA = Arrays.asList(colsA); 431 List colListB = Arrays.asList(colsB); 432 gat.setCollectiblesA(colListA); 434 gat.setCollectiblesB(colListB); 435 436 database.makePersistent(gat); 437 tx.commit(); 438 442 tx.begin(); 444 tx.getBroker().clearCache(); 445 OQLQuery query = odmg.newOQLQuery(); 446 query.create(queryStr); 447 Integer gatId = gat.getGatId(); 448 assertNotNull(gatId); 449 query.bind(gatId); 450 Collection result = (Collection ) query.execute(); 451 tx.commit(); 452 453 assertEquals("Wrong number of objects found", 1, result.size()); 454 Gatherer fetchedGat = (Gatherer) result.iterator().next(); 455 assertNotNull(fetchedGat.getCollectiblesA()); 456 assertNotNull(fetchedGat.getCollectiblesB()); 457 assertEquals(3, fetchedGat.getCollectiblesA().size()); 458 assertEquals(3, fetchedGat.getCollectiblesB().size()); 459 assertEquals(0, fetchedGat.getCollectiblesC().size()); 460 assertNotNull(fetchedGat.getCollectiblesA().get(0)); 461 assertNotNull(fetchedGat.getCollectiblesB().get(0)); 462 tx.begin(); 472 tx.getBroker().clearCache(); 473 tx.lock(fetchedGat, Transaction.WRITE); 475 fetchedGat.getCollectiblesA().remove(0); 479 fetchedGat.getCollectiblesB().remove(0); 480 tx.getBroker().serviceObjectCache().cache(tx.getBroker().serviceIdentity().buildIdentity(fetchedGat), fetchedGat); 481 tx.commit(); 489 493 tx.begin(); 494 tx.getBroker().clearCache(); 495 query = odmg.newOQLQuery(); 496 query.create(queryStr); 497 gatId = gat.getGatId(); 498 assertNotNull(gatId); 499 query.bind(gatId); 500 result = (Collection ) query.execute(); 501 tx.commit(); 502 assertEquals("Wrong number of objects found", 1, result.size()); 503 fetchedGat = (Gatherer) result.iterator().next(); 504 505 assertNotNull(fetchedGat.getCollectiblesA()); 506 assertNotNull(fetchedGat.getCollectiblesB()); 507 assertEquals(2, fetchedGat.getCollectiblesA().size()); 508 assertEquals(2, fetchedGat.getCollectiblesB().size()); 509 assertNotNull(fetchedGat.getCollectiblesA().get(0)); 510 assertNotNull(fetchedGat.getCollectiblesB().get(0)); 511 } 512 513 public void testUpdateCollectionWithBackReference() throws Exception 514 { 515 String name = "testUpdateCollectionWithBackReference" + System.currentTimeMillis(); 516 String queryStr = "select colls from " + CollectibleA.class.getName() + " where name=$1"; 517 518 Gatherer gat_1 = new Gatherer(null, "Gatherer_" + name); 520 CollectibleA coll_1 = new CollectibleA(name); 521 Gatherer gat_2 = new Gatherer(null, "Gatherer_" + name); 522 CollectibleA coll_2 = new CollectibleA(name); 523 524 coll_1.setGatherer(gat_1); 525 ArrayList alist = new ArrayList (); 526 alist.add(coll_1); 527 gat_1.setCollectiblesA(alist); 528 529 coll_2.setGatherer(gat_2); 530 ArrayList blist = new ArrayList (); 531 blist.add(coll_2); 532 gat_2.setCollectiblesA(blist); 533 534 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 535 tx.begin(); 536 database.makePersistent(coll_1); 537 database.makePersistent(coll_2); 538 tx.commit(); 539 540 tx.begin(); 541 tx.getBroker().clearCache(); 542 OQLQuery query = odmg.newOQLQuery(); 543 query.create(queryStr); 544 query.bind(name); 545 Collection result = (Collection ) query.execute(); 546 assertNotNull(result); 547 assertEquals(2, result.size()); 548 549 for (Iterator iterator = result.iterator(); iterator.hasNext();) 550 { 551 CollectibleA collectible = (CollectibleA) iterator.next(); 552 Gatherer gat = collectible.getGatherer(); 553 assertNotNull(gat); 554 assertEquals("Gatherer_"+name, gat.getName()); 555 tx.lock(collectible, Transaction.WRITE); 556 collectible.getGatherer().setName("New_"+name); 557 } 558 tx.commit(); 559 560 tx.begin(); 561 tx.getBroker().clearCache(); 562 query = odmg.newOQLQuery(); 563 query.create(queryStr); 564 query.bind(name); 565 result = (Collection ) query.execute(); 566 assertNotNull(result); 567 assertEquals(2, result.size()); 568 569 tx.setCascadingDelete(Gatherer.class, false); 571 for (Iterator iterator = result.iterator(); iterator.hasNext();) 572 { 573 CollectibleA collectible = (CollectibleA) iterator.next(); 574 Gatherer gat = collectible.getGatherer(); 575 assertNotNull(gat); 576 assertEquals("New_"+name, gat.getName()); 577 tx.lock(collectible, Transaction.WRITE); 578 collectible.setGatherer(null); 579 gat.getCollectiblesA().remove(collectible); 580 } 581 tx.commit(); 582 583 tx.begin(); 584 tx.getBroker().clearCache(); 585 query = odmg.newOQLQuery(); 586 query.create(queryStr); 587 query.bind(name); 588 result = (Collection ) query.execute(); 589 assertNotNull(result); 590 assertEquals(2, result.size()); 591 592 for (Iterator iterator = result.iterator(); iterator.hasNext();) 593 { 594 CollectibleA collectible = (CollectibleA) iterator.next(); 595 Gatherer gat = collectible.getGatherer(); 596 assertNull(gat); 597 } 598 tx.commit(); 599 } 600 601 605 public void testUpdateCollection() throws Exception 606 { 607 String prefix = "testUpdateCollection" + System.currentTimeMillis(); 608 String queryStr = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1"; 609 String modifiedName = "modified_name_" + System.currentTimeMillis(); 610 String queryMod = "select coll from " + CollectibleA.class.getName() + " where name like $1"; 611 612 Gatherer gat = new Gatherer(null, prefix + "_Gatherer"); 614 gat.setCollectiblesA(Arrays.asList(prepareCollectibleA(gat, prefix + "_1_"))); 616 TransactionExt tx = (TransactionExt)odmg.newTransaction(); 617 tx.begin(); 618 database.makePersistent(gat); 619 tx.commit(); 620 621 tx.begin(); 622 tx.getBroker().clearCache(); 623 OQLQuery query = odmg.newOQLQuery(); 625 query.create(queryStr); 626 assertNotNull(gat.getGatId()); 627 query.bind(gat.getGatId()); 628 Collection result = (Collection ) query.execute(); 629 tx.commit(); 630 assertEquals("Wrong number of objects found", 1, result.size()); 631 Gatherer fetchedGat = (Gatherer) result.iterator().next(); 632 633 tx.begin(); 634 tx.lock(fetchedGat, Transaction.WRITE); 635 List collsA = fetchedGat.getCollectiblesA(); 636 assertEquals(3, collsA.size()); 637 ((CollectibleA)collsA.get(0)).setName(modifiedName); 639 tx.commit(); 640 641 tx.begin(); 642 tx.getBroker().clearCache(); 643 query = odmg.newOQLQuery(); 645 query.create(queryMod); 646 query.bind(modifiedName); 647 result = (Collection ) query.execute(); 648 tx.commit(); 649 assertEquals("Wrong number of objects found", 1, result.size()); 650 CollectibleA collA = (CollectibleA) result.iterator().next(); 651 assertNotNull(collA); 652 assertEquals(modifiedName, collA.getName()); 653 } 654 655 663 public void testUpdateWhenExchangeObjectsInCollection() throws Exception 664 { 665 final String prefix = "testUpdateWhenExchangeObjectsInCollection" + System.currentTimeMillis(); 666 final String queryStr = "select gatherer from " + Gatherer.class.getName() + 667 " where gatId=$1 or gatId=$2 order by gatId asc"; 668 669 final String gat1Name = prefix + "_Gatherer"; 671 final String gat2Name = prefix + "_Gatherer2"; 672 Gatherer gat = new Gatherer(null, gat1Name); 673 Gatherer gat2 = new Gatherer(null, gat2Name); 674 CollectibleC collC_1 = new CollectibleC(prefix + "NO_1", null, "nothing1"); 676 CollectibleC collC_2 = new CollectibleC(prefix + "NO_2", null, "nothing2"); 677 gat.setCollectiblesC(Arrays.asList(new CollectibleC[]{collC_1})); 678 gat2.setCollectiblesC(Arrays.asList(new CollectibleC[]{collC_2})); 679 TransactionExt tx = (TransactionExt)odmg.newTransaction(); 680 tx.begin(); 681 database.makePersistent(gat); 682 database.makePersistent(gat2); 683 tx.commit(); 684 685 tx.begin(); 687 tx.getBroker().clearCache(); 688 OQLQuery query = odmg.newOQLQuery(); 689 query.create(queryStr); 690 assertNotNull(gat.getGatId()); 691 query.bind(gat.getGatId()); 692 query.bind(gat2.getGatId()); 693 Collection result = (Collection ) query.execute(); 694 tx.commit(); 695 assertEquals("Wrong number of objects found", 2, result.size()); 696 Iterator it = result.iterator(); 697 Gatherer fetchedGat = (Gatherer) it.next(); 698 Gatherer fetchedGat2 = (Gatherer) it.next(); 699 assertNotNull(fetchedGat); 700 assertNotNull(fetchedGat2); 701 assertEquals("Wrong gatherer returned: fetchedGat should be first Gatherer", 702 gat1Name, fetchedGat.getName()); 703 assertEquals("Wrong gatherer returned: fetchedGat2 should be second Gatherer", 704 gat2Name, fetchedGat2.getName()); 705 assertNotNull(fetchedGat.collectiblesC); 706 assertNotNull(fetchedGat2.collectiblesC); 707 assertEquals(1, fetchedGat.getCollectiblesC().size()); 708 assertEquals(1, fetchedGat2.getCollectiblesC().size()); 709 710 collC_1 = (CollectibleC) fetchedGat.getCollectiblesC().get(0); 711 collC_2 = (CollectibleC) fetchedGat2.getCollectiblesC().get(0); 712 assertEquals(prefix + "NO_1", collC_1.getName()); 713 assertEquals(prefix + "NO_2", collC_2.getName()); 714 715 List list1 = fetchedGat.getCollectiblesC(); 717 List list2 = fetchedGat2.getCollectiblesC(); 718 tx.begin(); 720 tx.lock(fetchedGat, Transaction.WRITE); 721 tx.lock(fetchedGat2, Transaction.WRITE); 722 fetchedGat.setCollectiblesC(list2); 723 fetchedGat2.setCollectiblesC(list1); 724 tx.commit(); 726 729 tx.begin(); 731 tx.getBroker().clearCache(); 732 query = odmg.newOQLQuery(); 733 query.create(queryStr); 734 assertNotNull(gat.getGatId()); 735 query.bind(gat.getGatId()); 736 query.bind(gat2.getGatId()); 737 result = (Collection ) query.execute(); 738 tx.commit(); 739 assertEquals("Wrong number of objects found", 2, result.size()); 740 it = result.iterator(); 741 fetchedGat = (Gatherer) it.next(); 742 fetchedGat2 = (Gatherer) it.next(); 743 assertNotNull(fetchedGat); 744 assertNotNull(fetchedGat2); 745 assertNotNull(fetchedGat.getCollectiblesC()); 746 assertNotNull(fetchedGat2.getCollectiblesC()); 747 assertEquals(1, fetchedGat.getCollectiblesC().size()); 748 assertEquals(1, fetchedGat2.getCollectiblesC().size()); 749 750 collC_1 = (CollectibleC) fetchedGat.getCollectiblesC().get(0); 751 collC_2 = (CollectibleC) fetchedGat2.getCollectiblesC().get(0); 752 assertEquals(prefix + "NO_2", collC_1.getName()); 754 assertEquals(prefix + "NO_1", collC_2.getName()); 755 } 756 757 764 public void testRemoveCollectionElementWithoutBackReference_2() throws Exception 765 { 766 String queryGat = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1"; 768 String prefix = "testRemoveCollectionObjectWithoutBackReference_2_" + System.currentTimeMillis(); 769 770 Gatherer gat = new Gatherer(null, prefix + "_Gatherer"); 772 gat.setCollectiblesC(Arrays.asList(prepareCollectibleC(null, prefix))); 775 TransactionExt tx = (TransactionExt)odmg.newTransaction(); 776 tx.begin(); 777 database.makePersistent(gat); 778 tx.commit(); 779 780 tx.begin(); 782 tx.getBroker().clearCache(); 783 assertNotNull(gat.getGatId()); 784 785 OQLQuery query = odmg.newOQLQuery(); 786 query.create(queryGat); 787 query.bind(gat.getGatId()); 788 789 Collection result = (Collection ) query.execute(); 790 tx.commit(); 791 assertEquals("Wrong number of objects found", 1, result.size()); 792 Gatherer fetchedGat = (Gatherer) result.iterator().next(); 793 assertNotNull(fetchedGat); 794 795 List colC = fetchedGat.getCollectiblesC(); 796 assertEquals("Wrong number of CollectiblesC", 3, colC.size()); 797 tx.begin(); 799 List newCols = new ArrayList (colC); 803 Object toDelete = newCols.remove(2); 804 tx.lock(fetchedGat, Transaction.WRITE); 806 fetchedGat.setCollectiblesC(newCols); 807 database.deletePersistent(toDelete); 809 tx.commit(); 811 812 tx.begin(); 814 tx.getBroker().clearCache(); 815 816 query = odmg.newOQLQuery(); 817 query.create("select colls from " + CollectibleC.class.getName() + 818 " where name like $1"); 819 query.bind(prefix + "%"); 820 result = (Collection ) query.execute(); 821 assertEquals("Wrong number of objects found", 2, result.size()); 822 tx.commit(); 823 824 tx.begin(); 827 query = odmg.newOQLQuery(); 828 query.create(queryGat); 829 query.bind(gat.getGatId()); 830 result = (Collection ) query.execute(); 831 assertEquals("Wrong number of objects found", 1, result.size()); 832 fetchedGat = (Gatherer) result.iterator().next(); 833 colC = fetchedGat.getCollectiblesC(); 834 assertEquals("Wrong number of CollectiblesA found in Gatherer", 2, colC.size()); 835 tx.commit(); 836 837 colC.get(0); 838 } 839 840 847 public void testAddCollectionElementWithoutBackReference() throws Exception 848 { 849 String queryGat = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1"; 851 String prefix = "testAddCollectionElementWithoutBackReference_" + System.currentTimeMillis(); 852 853 Gatherer gat = new Gatherer(null, prefix + "_Gatherer"); 855 gat.setCollectiblesC(Arrays.asList(prepareCollectibleC(null, prefix))); 858 TransactionExt tx = (TransactionExt)odmg.newTransaction(); 859 tx.begin(); 860 database.makePersistent(gat); 861 tx.commit(); 862 863 tx.begin(); 865 tx.getBroker().clearCache(); 866 assertNotNull(gat.getGatId()); 867 868 OQLQuery query = odmg.newOQLQuery(); 869 query.create(queryGat); 870 query.bind(gat.getGatId()); 871 872 Collection result = (Collection ) query.execute(); 873 tx.commit(); 874 assertEquals("Wrong number of objects found", 1, result.size()); 875 Gatherer fetchedGat = (Gatherer) result.iterator().next(); 876 assertNotNull(fetchedGat); 877 878 List colC = fetchedGat.getCollectiblesC(); 880 assertEquals("Wrong number of CollectiblesC", 3, colC.size()); 881 882 tx.begin(); 883 tx.lock(fetchedGat, Transaction.WRITE); 885 CollectibleC newC = new CollectibleC(prefix, null, "### new added ###"); 887 fetchedGat.getCollectiblesC().add(newC); 888 newC.setGathererId(fetchedGat.getGatId()); 889 tx.lock(newC, Transaction.WRITE); 890 tx.commit(); 892 893 tx.begin(); 895 tx.getBroker().clearCache(); 896 897 query = odmg.newOQLQuery(); 898 query.create("select colls from " + CollectibleC.class.getName() + 899 " where name like $1"); 900 query.bind(prefix + "%"); 901 result = (Collection ) query.execute(); 902 assertEquals("Wrong number of objects found", 4, result.size()); 903 tx.commit(); 904 905 tx.begin(); 908 tx.getBroker().clearCache(); 909 query = odmg.newOQLQuery(); 910 query.create(queryGat); 911 query.bind(gat.getGatId()); 912 result = (Collection ) query.execute(); 913 assertEquals("Wrong number of objects found", 1, result.size()); 914 fetchedGat = (Gatherer) result.iterator().next(); 915 colC = fetchedGat.getCollectiblesC(); 916 assertEquals("Wrong number of CollectiblesA found in Gatherer", 4, colC.size()); 917 tx.commit(); 918 919 colC.get(0); 920 } 921 922 927 public void testAddCollectionElementWithBackReference() throws Exception 928 { 929 String queryGat = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1"; 930 String prefix = "testAddCollectionElementWithBackReference_" + System.currentTimeMillis(); 931 932 936 Gatherer gat = new Gatherer(null, prefix + "_Gatherer"); 937 gat.setCollectiblesB(Arrays.asList(prepareCollectibleB(gat, prefix))); 939 940 TransactionExt tx = (TransactionExt)odmg.newTransaction(); 941 tx.begin(); 942 database.makePersistent(gat); 943 tx.commit(); 944 945 tx.begin(); 947 tx.getBroker().clearCache(); 948 assertNotNull(gat.getGatId()); 949 950 OQLQuery query = odmg.newOQLQuery(); 951 query.create(queryGat); 952 query.bind(gat.getGatId()); 953 954 Collection result = (Collection ) query.execute(); 955 tx.commit(); 956 assertEquals("Wrong number of objects found", 1, result.size()); 957 Gatherer fetchedGat = (Gatherer) result.iterator().next(); 958 assertNotNull(fetchedGat); 959 960 961 tx.begin(); 962 List colB = fetchedGat.getCollectiblesB(); 964 assertEquals("Wrong number of CollectiblesB", 3, colB.size()); 965 966 tx.lock(fetchedGat, Transaction.WRITE); 968 CollectibleB newB = new CollectibleB(prefix); 970 newB.setGatherer(fetchedGat); 971 fetchedGat.getCollectiblesB().add(newB); 972 tx.lock(newB, Transaction.WRITE); 974 976 tx.commit(); 977 978 tx.begin(); 980 tx.getBroker().clearCache(); 981 982 query = odmg.newOQLQuery(); 983 query.create("select colls from " + CollectibleB.class.getName() + 984 " where name like $1"); 985 query.bind(prefix + "%"); 986 result = (Collection ) query.execute(); 987 assertEquals("Wrong number of objects found", 4, result.size()); 988 tx.commit(); 989 990 tx.begin(); 993 tx.getBroker().clearCache(); 994 query = odmg.newOQLQuery(); 995 query.create(queryGat); 996 query.bind(gat.getGatId()); 997 result = (Collection ) query.execute(); 998 assertEquals("Wrong number of objects found", 1, result.size()); 999 fetchedGat = (Gatherer) result.iterator().next(); 1000 colB = fetchedGat.getCollectiblesB(); 1001 assertEquals("Wrong number of CollectiblesA found in Gatherer", 4, colB.size()); 1002 tx.commit(); 1003 1004 colB.get(0); 1005 } 1006 1007 1031 public void testAddCollectionElementCrossAPI() throws Exception 1032 { 1033 String name = "testAddCollectionElementCrossAPI_"+System.currentTimeMillis(); 1034 Gatherer gat = new Gatherer(null, name); 1036 CollectibleB B = new CollectibleB(name); 1037 B.setGatherer(gat); 1038 ArrayList cols = new ArrayList (); 1039 cols.add(B); 1040 gat.setCollectiblesB(cols); 1041 1042 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 1043 tx.begin(); 1044 database.makePersistent(gat); 1045 tx.commit(); 1046 1047 Gatherer fetchedGatherer = null; 1049 PersistenceBroker pb = null; 1050 try 1051 { 1052 pb = PersistenceBrokerFactory.defaultPersistenceBroker(); 1053 pb.clearCache(); 1054 Criteria crit = new Criteria(); 1055 crit.addLike("name", name); 1056 Query q = QueryFactory.newQuery(Gatherer.class, crit); 1057 fetchedGatherer = (Gatherer) pb.getObjectByQuery(q); 1058 } 1059 finally 1060 { 1061 if(pb != null) pb.close(); 1062 } 1063 1064 assertNotNull(fetchedGatherer); 1066 assertEquals(gat.getGatId(), fetchedGatherer.getGatId()); 1067 assertNotNull(fetchedGatherer.getCollectiblesB()); 1068 assertEquals(1, fetchedGatherer.getCollectiblesB().size()); 1069 CollectibleB fetched_B = (CollectibleB) fetchedGatherer.getCollectiblesB().iterator().next(); 1070 assertNotNull(fetched_B); 1071 1072 tx.begin(); 1074 tx.getBroker().clearCache(); 1075 tx.lock(fetchedGatherer, Transaction.WRITE); 1077 CollectibleB newB = new CollectibleB(name); 1078 newB.setGatherer(fetchedGatherer); 1079 fetchedGatherer.getCollectiblesB().add(newB); 1080 tx.lock(newB, Transaction.WRITE); 1081 assertEquals(2, fetchedGatherer.getCollectiblesB().size()); 1083 tx.commit(); 1084 1085 fetchedGatherer = null; 1087 pb = null; 1088 try 1089 { 1090 pb = PersistenceBrokerFactory.defaultPersistenceBroker(); 1091 pb.clearCache(); 1092 Criteria crit = new Criteria(); 1093 crit.addLike("name", name); 1094 Query q = QueryFactory.newQuery(Gatherer.class, crit); 1095 fetchedGatherer = (Gatherer) pb.getObjectByQuery(q); 1096 } 1097 finally 1098 { 1099 if(pb != null) pb.close(); 1100 } 1101 1102 assertNotNull(fetchedGatherer); 1104 assertEquals(gat.getGatId(), fetchedGatherer.getGatId()); 1105 assertNotNull(fetchedGatherer.getCollectiblesB()); 1106 assertEquals(2, fetchedGatherer.getCollectiblesB().size()); 1107 CollectibleB fetched_B_1 = (CollectibleB) fetchedGatherer.getCollectiblesB().iterator().next(); 1108 CollectibleB fetched_B_2 = (CollectibleB) fetchedGatherer.getCollectiblesB().iterator().next(); 1109 assertNotNull(fetched_B_1); 1110 assertNotNull(fetched_B_2); 1111 } 1112 1113 1114 1115 1119 private CollectibleA[] prepareCollectibleA(Gatherer gat, String namePrefix) 1120 { 1121 CollectibleA[] colA = new CollectibleA[]{ 1122 new CollectibleA(namePrefix + " colA_1"), 1123 new CollectibleA(namePrefix + " colA_2"), 1124 new CollectibleA(namePrefix + " colA_3") 1125 }; 1126 for (int i = 0; i < colA.length; i++) 1127 { 1128 CollectibleA collectibleA = colA[i]; 1129 collectibleA.setGatherer(gat); 1130 } 1131 return colA; 1132 } 1133 1134 private CollectibleB[] prepareCollectibleB(Gatherer gat, String namePrefix) 1135 { 1136 CollectibleB[] colB = new CollectibleB[]{ 1137 new CollectibleB(namePrefix + " colB_1"), 1138 new CollectibleB(namePrefix + " colB_2"), 1139 new CollectibleB(namePrefix + " colB_3") 1140 }; 1141 for (int i = 0; i < colB.length; i++) 1142 { 1143 CollectibleB collectibleB = colB[i]; 1144 collectibleB.setGatherer(gat); 1145 } 1146 return colB; 1147 } 1148 1149 private CollectibleC[] prepareCollectibleC(Gatherer gat, String namePrefix) 1150 { 1151 CollectibleC[] colC = new CollectibleC[]{ 1152 new CollectibleC(namePrefix + " colC_1", null, "ext1"), 1153 new CollectibleC(namePrefix + " colC_2", null, "ext2"), 1154 new CollectibleC(namePrefix + " colC_3", null, "ext3") 1155 }; 1156 for (int i = 0; i < colC.length; i++) 1157 { 1158 CollectibleC collectibleC = colC[i]; 1159 collectibleC.setGathererId(gat != null ? gat.gatId : null); 1160 } 1161 return colC; 1162 } 1163 1164 1165 public static class Gatherer implements Serializable 1169 { 1170 private Integer gatId; 1171 private String name; 1172 private List collectiblesA = new Vector (); 1173 private List collectiblesB = new Vector (); 1174 private List collectiblesC = new Vector (); 1175 1176 public Gatherer() 1177 { 1178 } 1179 1180 public String toString() 1181 { 1182 return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) 1183 .append("gatId", gatId) 1184 .append("name", name) 1185 .append("colA", collectiblesA) 1186 .append("colB", collectiblesB) 1187 .append("colc", collectiblesC) 1188 .toString(); 1189 } 1190 1191 public void addCollectibleA(CollectibleA colA) 1192 { 1193 if (collectiblesA == null) collectiblesA = new Vector (); 1194 collectiblesA.add(colA); 1195 } 1196 1197 public Gatherer(Integer gatId, String name) 1198 { 1199 this.gatId = gatId; 1200 this.name = name; 1201 } 1202 1203 public Integer getGatId() 1204 { 1205 return gatId; 1206 } 1207 1208 public void setGatId(Integer gatId) 1209 { 1210 this.gatId = gatId; 1211 } 1212 1213 public String getName() 1214 { 1215 return name; 1216 } 1217 1218 public void setName(String name) 1219 { 1220 this.name = name; 1221 } 1222 1223 public List getCollectiblesA() 1224 { 1225 return collectiblesA; 1226 } 1227 1228 public void setCollectiblesA(List collectiblesA) 1229 { 1230 this.collectiblesA = collectiblesA; 1231 } 1232 1233 public List getCollectiblesB() 1234 { 1235 return collectiblesB; 1236 } 1237 1238 public void setCollectiblesB(List collectiblesB) 1239 { 1240 this.collectiblesB = collectiblesB; 1241 } 1242 1243 public List getCollectiblesC() 1244 { 1245 return collectiblesC; 1246 } 1247 1248 public void setCollectiblesC(List collectiblesC) 1249 { 1250 this.collectiblesC = collectiblesC; 1251 } 1252 } 1253 1254 public static interface CollectibleBIF extends Serializable 1255 { 1256 Integer getColId(); 1257 void setColId(Integer colId); 1258 String getName(); 1259 void setName(String name); 1260 Integer getGathererId(); 1261 void setGathererId(Integer colId); 1262 Gatherer getGatherer(); 1263 void setGatherer(Gatherer gatherer); 1264 } 1265 1266 public static class CollectibleB implements CollectibleBIF 1267 { 1268 private Integer colId; 1269 private String name; 1270 private Integer gathererId; 1271 private Gatherer gatherer; 1272 1273 public CollectibleB() 1274 { 1275 } 1276 1277 public CollectibleB(String name) 1278 { 1279 this.name = name; 1280 } 1281 1282 public CollectibleB(String name, Integer gathererId) 1283 { 1284 this.name = name; 1285 this.gathererId = gathererId; 1286 } 1287 1288 public CollectibleB(Integer colId, String name, Integer gathererId) 1289 { 1290 this.colId = colId; 1291 this.name = name; 1292 this.gathererId = gathererId; 1293 } 1294 1295 public Gatherer getGatherer() 1296 { 1297 return gatherer; 1298 } 1299 1300 public void setGatherer(Gatherer gatherer) 1301 { 1302 this.gatherer = gatherer; 1303 } 1304 1305 public Integer getGathererId() 1306 { 1307 return gathererId; 1308 } 1309 1310 public void setGathererId(Integer gathererId) 1311 { 1312 this.gathererId = gathererId; 1313 } 1314 1315 public Integer getColId() 1316 { 1317 return colId; 1318 } 1319 1320 public void setColId(Integer colId) 1321 { 1322 this.colId = colId; 1323 } 1324 1325 public String getName() 1326 { 1327 return name; 1328 } 1329 1330 public void setName(String name) 1331 { 1332 this.name = name; 1333 } 1334 } 1335 1336 public static interface CollectibleAIF extends Serializable 1337 { 1338 Integer getColId(); 1339 1340 void setColId(Integer colId); 1341 1342 String getName(); 1343 1344 void setName(String name); 1345 1346 Integer getGathererId(); 1347 1348 void setGathererId(Integer colId); 1349 1350 Gatherer getGatherer(); 1351 1352 void setGatherer(Gatherer gatherer); 1353 } 1354 1355 public static class CollectibleA implements CollectibleAIF 1356 { 1357 private Integer colId; 1358 private String name; 1359 private Integer gathererId; 1360 private Gatherer gatherer; 1361 1362 public CollectibleA() 1363 { 1364 } 1365 1366 public CollectibleA(Integer colId, String name, Integer gathererId) 1367 { 1368 this.colId = colId; 1369 this.name = name; 1370 this.gathererId = gathererId; 1371 } 1372 1373 public CollectibleA(String name, Integer gathererId) 1374 { 1375 this.name = name; 1376 this.gathererId = gathererId; 1377 } 1378 1379 public CollectibleA(String name) 1380 { 1381 this.name = name; 1382 } 1383 1384 public Gatherer getGatherer() 1385 { 1386 return gatherer; 1387 } 1388 1389 public void setGatherer(Gatherer gatherer) 1390 { 1391 this.gatherer = gatherer; 1392 } 1393 1394 public Integer getGathererId() 1395 { 1396 return gathererId; 1397 } 1398 1399 public void setGathererId(Integer gathererId) 1400 { 1401 this.gathererId = gathererId; 1402 } 1403 1404 public Integer getColId() 1405 { 1406 return colId; 1407 } 1408 1409 public void setColId(Integer colId) 1410 { 1411 this.colId = colId; 1412 } 1413 1414 public String getName() 1415 { 1416 return name; 1417 } 1418 1419 public void setName(String name) 1420 { 1421 this.name = name; 1422 } 1423 } 1424 1425 public static interface CollectibleCIF extends Serializable 1426 { 1427 Integer getColId(); 1428 void setColId(Integer colId); 1429 String getName(); 1430 void setName(String name); 1431 Integer getGathererId(); 1432 void setGathererId(Integer colId); 1433 Gatherer getGatherer(); 1434 void setGatherer(Gatherer gatherer); 1435 String getExtentName(); 1436 void setExtentName(String extentName); 1437 } 1438 1439 public static class CollectibleC 1440 { 1441 private Integer colId; 1442 private String name; 1443 private Integer gathererId; 1444 private String extentName; 1445 1446 public CollectibleC() 1447 { 1448 } 1449 1450 public CollectibleC(String name, Integer gathererId, String extentName) 1451 { 1452 this.name = name; 1453 this.gathererId = gathererId; 1454 this.extentName = extentName; 1455 } 1456 1457 public String getExtentName() 1458 { 1459 return extentName; 1460 } 1461 1462 public void setExtentName(String extentName) 1463 { 1464 this.extentName = extentName; 1465 } 1466 1467 public Integer getColId() 1468 { 1469 return colId; 1470 } 1471 1472 public void setColId(Integer colId) 1473 { 1474 this.colId = colId; 1475 } 1476 1477 public String getName() 1478 { 1479 return name; 1480 } 1481 1482 public void setName(String name) 1483 { 1484 this.name = name; 1485 } 1486 1487 public Integer getGathererId() 1488 { 1489 return gathererId; 1490 } 1491 1492 public void setGathererId(Integer gathererId) 1493 { 1494 this.gathererId = gathererId; 1495 } 1496 } 1497} 1498 | Popular Tags |