1 package org.apache.ojb.odmg; 2 3 17 18 import java.io.Serializable ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Date ; 22 import java.util.List ; 23 import java.util.Iterator ; 24 25 import org.apache.commons.lang.SerializationUtils; 26 import org.apache.commons.lang.SystemUtils; 27 import org.apache.commons.lang.builder.EqualsBuilder; 28 import org.apache.ojb.broker.Identity; 29 import org.apache.ojb.broker.query.Criteria; 30 import org.apache.ojb.broker.query.Query; 31 import org.apache.ojb.broker.query.QueryFactory; 32 import org.apache.ojb.broker.core.proxy.CollectionProxy; 33 import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl; 34 import org.apache.ojb.broker.core.proxy.CollectionProxyListener; 35 import org.apache.ojb.broker.core.proxy.IndirectionHandler; 36 import org.apache.ojb.broker.core.proxy.MaterializationListener; 37 import org.apache.ojb.broker.core.proxy.ProxyHelper; 38 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor; 39 import org.apache.ojb.junit.ODMGTestCase; 40 import org.odmg.OQLQuery; 41 import org.odmg.Transaction; 42 43 56 public class ObjectImageTest extends ODMGTestCase 57 { 58 static final int NONE = ObjectReferenceDescriptor.CASCADE_NONE; 59 static final int LINK = ObjectReferenceDescriptor.CASCADE_LINK; 60 static final int OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT; 61 static final String EOL = SystemUtils.LINE_SEPARATOR; 62 63 public static void main(String [] args) 64 { 65 junit.textui.TestRunner.main(new String []{ObjectImageTest.class.getName()}); 66 } 67 68 public void testReplaceOneToOneReference() throws Exception 69 { 70 String prefix = "testReplaceOneToOneReference_" + System.currentTimeMillis(); 71 ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, false); 72 73 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 74 tx.begin(); 75 Book book = new Book(prefix, null, null); 76 Publisher p_1 = new PublisherImpl(prefix); 77 Publisher p_2 = new PublisherImpl(prefix + "_replaced"); 78 book.setPublisher(p_1); 79 database.makePersistent(book); 80 database.makePersistent(p_1); 81 database.makePersistent(p_2); 82 tx.commit(); 83 84 Integer book_version = book.getVersion(); 85 Integer p1_version = p_1.getVersion(); 86 Integer p2_version = p_2.getVersion(); 87 88 tx.begin(); 89 tx.lock(book, Transaction.WRITE); 90 tx.lock(book.getPublisher(), Transaction.READ); 91 tx.lock(p_2, Transaction.READ); 92 book.setPublisher(p_2); 93 tx.commit(); 94 95 assertEquals(book_version.intValue() + 1, book.getVersion().intValue()); 96 assertEquals(p1_version, p_1.getVersion()); 97 assertEquals(p2_version, p_2.getVersion()); 98 } 99 100 public void testAddCollectionObjectToExistingObject() throws Exception 101 { 102 String prefix = "testAddCollectionObjectToExistingObject_" + System.currentTimeMillis(); 103 ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, false); 104 105 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 106 tx.begin(); 107 Book book = new Book(prefix, null, null); 108 Review r1 = new Review(prefix + "_1"); 109 database.makePersistent(book); 110 database.makePersistent(r1); 111 tx.commit(); 112 113 Integer book_version = book.getVersion(); 114 Integer r_1_version = r1.getVersion(); 115 116 Review r2 = new Review(prefix + "_2"); 117 tx.begin(); 118 tx.lock(r1, Transaction.WRITE); 119 tx.lock(book, Transaction.READ); 120 book.addReview(r1); 121 book.addReview(r2); 122 database.makePersistent(r2); 123 tx.commit(); 124 125 assertEquals(book_version, book.getVersion()); 126 assertTrue(book.getId() != null); 127 128 Integer r_2_version = r2.getVersion(); 129 130 tx.begin(); 131 tx.getBroker().clearCache(); 132 Book loadedCopy = (Book) tx.getBroker().getObjectByIdentity( 133 tx.getBroker().serviceIdentity().buildIdentity(Book.class, book.getId())); 134 assertNotNull(loadedCopy); 135 assertNotNull(loadedCopy.getReviews()); 136 assertEquals(2, loadedCopy.getReviews().size()); 137 assertEquals(book_version, loadedCopy.getVersion()); 138 Review new_r1 = (Review) loadedCopy.getReviews().get(0); 139 Review new_r2 = (Review) loadedCopy.getReviews().get(1); 140 assertEquals(r_1_version.intValue() + 1, new_r1.getVersion().intValue()); 141 assertEquals(r_2_version, new_r2.getVersion()); 142 143 tx.getBroker().clearCache(); 144 Criteria criteria = new Criteria(); 145 criteria.addLike("title", prefix); 146 Query q = QueryFactory.newQuery(Book.class, criteria); 147 Collection books = tx.getBroker().getCollectionByQuery(q); 148 assertNotNull(books); 149 assertEquals(1, books.size()); 150 Book new_book = (Book) books.iterator().next(); 151 tx.commit(); 152 assertEquals(book_version, new_book.getVersion()); 153 154 tx.begin(); 155 tx.lock(loadedCopy, Transaction.WRITE); 156 Review removed = (Review) loadedCopy.getReviews().remove(0); 157 Review stayed = (Review) loadedCopy.getReviews().get(0); 158 tx.commit(); 159 assertEquals(r_2_version, stayed.getVersion()); 161 164 tx.begin(); 165 OQLQuery query = odmg.newOQLQuery(); 166 query.create("select books from " + Book.class.getName() + " where title like $1"); 167 query.bind(prefix); 168 Collection result = (Collection ) query.execute(); 169 assertEquals(1, result.size()); 170 Book b = (Book) result.iterator().next(); 171 tx.commit(); 172 173 assertEquals(1, b.getReviews().size()); 174 Review r = (Review) b.getReviews().get(0); 175 if(!r.equals(r1) && !r.equals(r2)) 176 { 177 fail("Wrong object or wrong object version returned. Returned obj was " 178 + EOL + r + " expected object was " + EOL + r1 + " or " + EOL + r2); 179 } 180 } 181 182 185 public void testPersistenceByReachability_1() throws Exception 186 { 187 String name = "testPersistenceByReachability_1_" + System.currentTimeMillis(); 188 ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, true); 189 190 Date date = new Date (); 191 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 192 Book book = new Book(name, date, cover); 193 Review r1 = new Review(name); 194 Review r2 = new Review(name); 195 Review r3 = new Review(name + "_not_persistent"); 196 ArrayList reviews = new ArrayList (); 197 reviews.add(r1); 198 reviews.add(r2); 199 reviews.add(r3); 200 book.setReviews(reviews); 201 202 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 203 tx.begin(); 204 database.makePersistent(book); 205 Review r4 = new Review(name + "_new_added"); 206 book.addReview(r4); 208 tx.setCascadingDelete(Book.class, true); 209 book.removeReview(r3); 211 tx.commit(); 212 214 tx.begin(); 215 tx.getBroker().clearCache(); 216 217 OQLQuery query = odmg.newOQLQuery(); 218 query.create("select books from " + Book.class.getName() + " where title like $1"); 219 query.bind(name); 220 Collection result = (Collection ) query.execute(); 221 assertEquals(1, result.size()); 222 Book b = (Book) result.iterator().next(); 223 assertNotNull(b.getReviews()); 224 assertEquals(3, b.getReviews().size()); 225 226 query = odmg.newOQLQuery(); 227 query.create("select reviews from " + Review.class.getName() + " where summary like $1"); 228 query.bind(name + "_new_added"); 229 result = (Collection ) query.execute(); 230 assertEquals(1, result.size()); 232 233 query = odmg.newOQLQuery(); 234 query.create("select reviews from " + Review.class.getName() + " where summary like $1"); 235 query.bind(name + "_not_persistent"); 236 result = (Collection ) query.execute(); 237 assertEquals(0, result.size()); 239 tx.commit(); 240 } 241 242 245 public void testPersistenceByReachability_2() throws Exception 246 { 247 String name = "testPersistenceByReachability_2_" + System.currentTimeMillis(); 248 ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, true); 249 250 Date date = new Date (); 251 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 252 Book book = new Book(name, date, cover); 253 Review r1 = new Review(name); 254 Review r2 = new Review(name); 255 Review r3 = new Review(name + "_not_persistent"); 256 ArrayList reviews = new ArrayList (); 257 reviews.add(r1); 258 reviews.add(r2); 259 reviews.add(r3); 260 book.setReviews(reviews); 261 262 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 263 tx.begin(); 264 database.makePersistent(book); 265 Review r4 = new Review(name + "_new_added"); 266 book.addReview(r4); 268 tx.setCascadingDelete(Book.class, true); 269 book.removeReview(r3); 271 tx.checkpoint(); 272 273 tx.getBroker().clearCache(); 275 276 OQLQuery query = odmg.newOQLQuery(); 277 query.create("select books from " + Book.class.getName() + " where title like $1"); 278 query.bind(name); 279 Collection result = (Collection ) query.execute(); 280 assertEquals(1, result.size()); 281 Book b = (Book) result.iterator().next(); 282 assertNotNull(b.getReviews()); 283 assertEquals(3, b.getReviews().size()); 284 285 query = odmg.newOQLQuery(); 286 query.create("select reviews from " + Review.class.getName() + " where summary like $1"); 287 query.bind(name + "_new_added%"); 288 result = (Collection ) query.execute(); 289 assertEquals(1, result.size()); 291 292 query = odmg.newOQLQuery(); 293 query.create("select reviews from " + Review.class.getName() + " where summary like $1"); 294 query.bind(name + "_not_persistent"); 295 result = (Collection ) query.execute(); 296 assertEquals(0, result.size()); 298 299 b.setTitle(name + "_updated"); 300 tx.commit(); 301 302 query = odmg.newOQLQuery(); 303 query.create("select books from " + Book.class.getName() + " where title like $1"); 304 query.bind(name + "_updated"); 305 result = (Collection ) query.execute(); 306 assertEquals(1, result.size()); 307 Book b_updated = (Book) result.iterator().next(); 308 assertNotNull(b_updated.getReviews()); 309 assertEquals(3, b_updated.getReviews().size()); 310 assertEquals(name+"_updated", b_updated.getTitle()); 311 } 312 313 public void testAddPersistentObjectTo1toN() throws Exception 314 { 315 String name = "testAddPersistentObjectTo1toN_" + System.currentTimeMillis(); 316 Review review = new Review(name); 317 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 318 tx.begin(); 319 database.makePersistent(review); 320 tx.commit(); 321 322 Integer versionReview = review.getVersion(); 323 324 Date date = new Date (); 325 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 326 Book book = new Book(name, date, cover); 327 tx = (TransactionExt) odmg.newTransaction(); 328 tx.begin(); 329 database.makePersistent(book); 331 book.addReview(review); 332 tx.commit(); 333 334 assertEquals("expect that this object was updated", versionReview.intValue() + 1, review.getVersion().intValue()); 336 337 tx.begin(); 338 tx.getBroker().clearCache(); 339 OQLQuery query = odmg.newOQLQuery(); 340 query.create("select books from " + Book.class.getName() + " where title like $1"); 341 query.bind(name); 342 Collection result = (Collection ) query.execute(); 343 assertEquals(1, result.size()); 344 Book b = (Book) result.iterator().next(); 345 assertNotNull(b.getReviews()); 346 assertEquals(1, b.getReviews().size()); 347 tx.commit(); 348 } 349 350 public void testAddPersistentObjectToMtoN() throws Exception 351 { 352 String name = "testAddPersistentObjectToMtoN_" + System.currentTimeMillis(); 353 Author author = new Author(name, null); 354 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 355 tx.begin(); 356 database.makePersistent(author); 357 tx.commit(); 358 359 Integer versionReview = author.getVersion(); 360 361 Date date = new Date (); 362 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 363 Book book = new Book(name, date, cover); 364 tx = (TransactionExt) odmg.newTransaction(); 365 tx.begin(); 366 database.makePersistent(book); 367 book.addAuthor(author); 368 author.addBook(book); 369 tx.commit(); 370 371 assertEquals("expect that this object wasn't updated", versionReview.intValue(), author.getVersion().intValue()); 373 374 tx.begin(); 375 tx.getBroker().clearCache(); 376 OQLQuery query = odmg.newOQLQuery(); 377 query.create("select books from " + Book.class.getName() + " where title like $1"); 378 query.bind(name); 379 Collection result = (Collection ) query.execute(); 380 assertEquals(1, result.size()); 381 Book b = (Book) result.iterator().next(); 382 assertNotNull(b.getAuthors()); 383 assertEquals(1, b.getAuthors().size()); 384 tx.commit(); 385 } 386 387 390 public void testChangeMainFields() throws Exception 391 { 392 String name = "testChangeMainFields_" + System.currentTimeMillis(); 393 Date date = new Date (); 394 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 395 Book book = new Book(name, date, cover); 396 397 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 398 tx.begin(); 399 database.makePersistent(book); 400 tx.commit(); 401 402 Integer version = book.getVersion(); 403 405 tx.begin(); 406 tx.lock(book, Transaction.WRITE); 407 tx.commit(); 408 409 assertEquals(version, book.getVersion()); 411 412 tx.begin(); 413 tx.lock(book, Transaction.WRITE); 414 book.setPublicationDate(new Date (date.getTime())); 416 tx.commit(); 417 418 assertEquals(version, book.getVersion()); 420 421 tx.begin(); 422 tx.lock(book, Transaction.WRITE); 423 Date d = new Date (1111); 425 book.setPublicationDate(d); 426 tx.commit(); 427 assertFalse(date.equals(book.getPublicationDate())); 429 assertFalse(version.equals(book.getVersion())); 430 } 431 432 435 public void testChangeMainFields_2() throws Exception 436 { 437 String name = "testChangeMainFields_2_" + System.currentTimeMillis(); 438 Date date = new Date (); 439 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 440 Book book = new Book(name, date, cover); 441 442 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 443 tx.begin(); 444 database.makePersistent(book); 445 tx.commit(); 446 447 Integer version = book.getVersion(); 448 449 tx.begin(); 450 tx.lock(book, Transaction.WRITE); 451 book.setCover(new byte[]{2,3,4,5,6,7,8,8}); 452 tx.commit(); 453 454 assertFalse(version.equals(book.getVersion())); 455 tx.begin(); 456 tx.getBroker().clearCache(); 457 OQLQuery query = odmg.newOQLQuery(); 458 query.create("select books from " + Book.class.getName() + " where title like $1"); 459 query.bind(name); 460 Collection result = (Collection ) query.execute(); 461 assertEquals(1, result.size()); 462 Book b = (Book) result.iterator().next(); 463 assertFalse(version.equals(b.getVersion())); 464 tx.commit(); 465 } 466 467 470 public void testChangeMainFields_3() throws Exception 471 { 472 String name = "testChangeMainFields_3_" + System.currentTimeMillis(); 473 Date date = new Date (); 474 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 475 Book book = new Book(name, date, cover); 476 477 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 478 tx.begin(); 479 database.makePersistent(book); 480 tx.commit(); 481 482 Integer version = book.getVersion(); 483 484 tx.begin(); 485 tx.lock(book, Transaction.WRITE); 486 book = (Book) SerializationUtils.clone(book); 487 tx.lock(book, Transaction.WRITE); 488 tx.commit(); 489 490 assertEquals(version, book.getVersion()); 491 } 492 493 496 public void testChangeOneToOneReference_1() throws Exception 497 { 498 String name = "testChangeOneToOneReference_1_" + System.currentTimeMillis(); 499 Date date = new Date (); 500 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 501 Book book = new Book(name, date, cover); 502 Publisher publisher = new PublisherImpl(name); 503 book.setPublisher(publisher); 504 505 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 506 tx.begin(); 507 database.makePersistent(book); 508 tx.commit(); 509 511 Integer versionBook = book.getVersion(); 512 Integer versionPublisher = book.getPublisher().getVersion(); 513 514 tx.begin(); 515 tx.lock(book, Transaction.WRITE); 516 tx.lock(book, Transaction.WRITE); 517 tx.commit(); 518 520 assertEquals(versionBook, book.getVersion()); 521 assertEquals(versionPublisher, book.getVersion()); 522 } 523 524 527 public void testChangeOneToOneReference_1b() throws Exception 528 { 529 String name = "testChangeOneToOneReference_1b_" + System.currentTimeMillis(); 530 ojbChangeReferenceSetting(Book.class, "publisher", true, NONE, NONE, true); 531 532 Date date = new Date (); 533 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 534 Book book = new Book(name, date, cover); 535 Publisher publisher = new PublisherImpl(name); 536 book.setPublisher(publisher); 537 538 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 539 tx.begin(); 540 database.makePersistent(book); 541 tx.commit(); 542 544 Integer versionBook = book.getVersion(); 545 Integer versionPublisher = book.getPublisher().getVersion(); 546 547 tx.begin(); 548 tx.lock(book, Transaction.WRITE); 549 tx.lock(book, Transaction.WRITE); 550 tx.commit(); 551 553 assertEquals(versionBook, book.getVersion()); 554 assertEquals(versionPublisher, book.getVersion()); 555 } 556 557 560 public void testChangeOneToOneReference_2() throws Exception 561 { 562 String name = "testChangeOneToOneReference_2_" + System.currentTimeMillis(); 563 Date date = new Date (); 564 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 565 Book book = new Book(name, date, cover); 566 Publisher publisher = new PublisherImpl(name); 567 book.setPublisher(publisher); 568 569 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 570 tx.begin(); 571 database.makePersistent(book); 572 tx.commit(); 573 575 Integer versionBook = book.getVersion(); 576 Integer versionPublisher = book.getPublisher().getVersion(); 577 578 tx.begin(); 579 tx.lock(book, Transaction.WRITE); 580 book = (Book) SerializationUtils.clone(book); 582 tx.lock(book, Transaction.WRITE); 583 tx.commit(); 584 585 assertEquals(versionBook, book.getVersion()); 586 assertEquals(versionPublisher, book.getVersion()); 587 } 588 589 592 public void testChangeOneToOneReference_3() throws Exception 593 { 594 String name = "testChangeOneToOneReference_2_" + System.currentTimeMillis(); 595 Date date = new Date (); 596 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 597 Book book = new Book(name, date, cover); 598 Publisher publisher = new PublisherImpl(name); 599 book.setPublisher(publisher); 600 601 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 602 tx.begin(); 603 database.makePersistent(book); 604 tx.commit(); 605 607 Integer versionBook = book.getVersion(); 608 Integer versionPublisher = book.getPublisher().getVersion(); 609 610 tx.begin(); 611 tx.lock(book, Transaction.WRITE); 612 book = (Book) SerializationUtils.clone(book); 614 Publisher p = book.getPublisher(); 615 p.setName(name + "_updated"); 616 tx.lock(book, Transaction.WRITE); 619 tx.lock(p, Transaction.WRITE); 621 tx.commit(); 622 623 assertEquals(versionBook, book.getVersion()); 625 assertEquals(new Integer (versionPublisher.intValue() + 1), p.getVersion()); 627 } 628 629 632 public void testReplaceOneToOneReference_1() throws Exception 633 { 634 String name = "testChangeOneToOneReference_2_" + System.currentTimeMillis(); 635 Date date = new Date (); 636 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 637 Book book = new Book(name, date, cover); 638 Publisher publisher = new PublisherImpl(name); 639 book.setPublisher(publisher); 640 641 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 642 tx.begin(); 643 database.makePersistent(book); 644 tx.commit(); 645 647 Integer versionBook = book.getVersion(); 648 Integer versionPublisher = book.getPublisher().getVersion(); 649 650 tx.begin(); 651 tx.lock(book, Transaction.WRITE); 652 Publisher p = new PublisherImpl(name + "_new"); 653 book.setPublisher(p); 655 tx.lock(p, Transaction.WRITE); 656 tx.commit(); 657 658 assertEquals(new Integer (versionBook.intValue() + 1), book.getVersion()); 660 assertEquals(versionPublisher, p.getVersion()); 662 } 663 664 667 public void testReplaceOneToOneReference_2() throws Exception 668 { 669 String name = "testChangeOneToOneReference_2_" + System.currentTimeMillis(); 670 Date date = new Date (); 671 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 672 Book book = new Book(name, date, cover); 673 Publisher publisher = new PublisherImpl(name); 674 book.setPublisher(publisher); 675 676 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 677 tx.begin(); 678 database.makePersistent(book); 679 tx.commit(); 680 682 Integer versionBook = book.getVersion(); 683 Integer versionPublisher = book.getPublisher().getVersion(); 684 685 tx.begin(); 686 tx.lock(book, Transaction.WRITE); 687 book = (Book) SerializationUtils.clone(book); 688 Publisher p = new PublisherImpl(name + "_new"); 689 book.setPublisher(p); 691 tx.lock(book, Transaction.WRITE); 694 tx.lock(p, Transaction.WRITE); 696 tx.commit(); 697 698 assertEquals(new Integer (versionBook.intValue() + 1), book.getVersion()); 700 assertEquals(versionPublisher, p.getVersion()); 702 } 703 704 707 public void testChangeOneToOneReference_4() throws Exception 708 { 709 ojbChangeReferenceSetting(Book.class, "publisher", true, NONE, NONE, true); 710 String name = "testChangeOneToOneReference_4_" + System.currentTimeMillis(); 711 Date date = new Date (); 712 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 713 Book book = new Book(name, date, cover); 714 Publisher publisher = new PublisherImpl(name); 715 book.setPublisher(publisher); 716 717 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 718 tx.begin(); 719 database.makePersistent(book); 720 tx.commit(); 721 722 Integer versionBook = book.getVersion(); 723 724 tx.begin(); 725 tx.getBroker().clearCache(); 726 OQLQuery query = odmg.newOQLQuery(); 727 query.create("select books from " + Book.class.getName() + " where title like $1"); 728 query.bind(name); 729 Collection result = (Collection ) query.execute(); 730 assertEquals(1, result.size()); 731 Book b = (Book) result.iterator().next(); 732 IndirectionHandler handler = ProxyHelper.getIndirectionHandler(b.getPublisher()); 733 assertNotNull(handler); 734 assertFalse(handler.alreadyMaterialized()); 735 handler.addListener( 736 new MaterializationListener() 737 { 738 public void beforeMaterialization(IndirectionHandler handler, Identity oid) 739 { 740 fail("Reference shall not materialize while locking"); 741 } 742 743 public void afterMaterialization(IndirectionHandler handler, Object materializedObject) 744 { 745 } 746 } 747 ); 748 tx.lock(b, Transaction.WRITE); 749 tx.commit(); 750 751 assertEquals(versionBook, b.getVersion()); 752 } 753 754 757 public void testChangeOneToOneReference_5() throws Exception 758 { 759 ojbChangeReferenceSetting(Book.class, "publisher", true, NONE, NONE, true); 760 String name = "testChangeOneToOneReference_5_" + System.currentTimeMillis(); 761 Date date = new Date (); 762 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 763 Book book = new Book(name, date, cover); 764 Publisher publisher = new PublisherImpl(name); 765 book.setPublisher(publisher); 766 767 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 768 tx.begin(); 769 database.makePersistent(book); 770 tx.commit(); 771 773 774 Integer versionBook = book.getVersion(); 775 776 tx.begin(); 777 tx.getBroker().clearCache(); 778 OQLQuery query = odmg.newOQLQuery(); 779 query.create("select books from " + Book.class.getName() + " where title like $1"); 780 query.bind(name); 781 Collection result = (Collection ) query.execute(); 782 assertEquals(1, result.size()); 783 Book b = (Book) result.iterator().next(); 784 IndirectionHandler handler = ProxyHelper.getIndirectionHandler(b.getPublisher()); 785 assertNotNull(handler); 786 assertFalse(handler.alreadyMaterialized()); 787 handler.addListener( 788 new MaterializationListener() 789 { 790 public void beforeMaterialization(IndirectionHandler handler, Identity oid) 791 { 792 fail("Reference shall not materialize while locking"); 793 } 794 795 public void afterMaterialization(IndirectionHandler handler, Object materializedObject) 796 { 797 } 798 } 799 ); 800 tx.lock(b, Transaction.WRITE); 802 Publisher p = new PublisherImpl(name+"_new"); 804 b.setPublisher(p); 805 tx.commit(); 807 808 assertEquals(new Integer (versionBook.intValue() + 1), b.getVersion()); 810 assertFalse(publisher.getName().equals(p.getName())); 812 assertNotNull(p.getVersion()); 813 814 tx.begin(); 815 tx.getBroker().clearCache(); 816 query = odmg.newOQLQuery(); 817 query.create("select books from " + Book.class.getName() + " where title like $1"); 818 query.bind(name); 819 result = (Collection ) query.execute(); 820 tx.commit(); 821 assertEquals(1, result.size()); 822 b = (Book) result.iterator().next(); 823 Publisher newP = b.getPublisher(); 824 assertNotNull(newP); 825 assertEquals(name+"_new", newP.getName()); 826 } 827 828 831 public void testChangeOneToOneReference_6() throws Exception 832 { 833 String name = "testChangeOneToOneReference_6_" + System.currentTimeMillis(); 834 Date date = new Date (); 835 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 836 Book book = new Book(name, date, cover); 837 Publisher publisher = new PublisherImpl(name); 838 book.setPublisher(publisher); 839 840 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 841 tx.begin(); 842 database.makePersistent(book); 843 tx.commit(); 844 845 Integer versionBook = book.getVersion(); 846 Integer versionPublisher = book.getPublisher().getVersion(); 847 848 tx.begin(); 849 tx.getBroker().clearCache(); 850 OQLQuery query = odmg.newOQLQuery(); 851 query.create("select books from " + Book.class.getName() + " where title like $1"); 852 query.bind(name); 853 Collection result = (Collection ) query.execute(); 854 assertEquals(1, result.size()); 855 Book b = (Book) result.iterator().next(); 856 tx.lock(b, Transaction.WRITE); 857 b.getPublisher().setName("updated_" + b.getPublisher().getName()); 858 tx.commit(); 859 860 assertEquals(versionBook, b.getVersion()); 862 assertFalse(versionPublisher.equals(b.getPublisher().getVersion())); 864 assertEquals("updated_" + name, b.getPublisher().getName()); 865 } 866 867 870 public void testChangeOneToOneReference_7() throws Exception 871 { 872 String name = "testChangeOneToOneReference_7_" + System.currentTimeMillis(); 873 Date date = new Date (); 874 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 875 Book book = new Book(name, date, cover); 876 877 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 878 tx.begin(); 879 database.makePersistent(book); 880 tx.commit(); 881 882 Integer versionBook = book.getVersion(); 883 884 tx.begin(); 885 tx.getBroker().clearCache(); 886 OQLQuery query = odmg.newOQLQuery(); 887 query.create("select books from " + Book.class.getName() + " where title like $1"); 888 query.bind(name); 889 Collection result = (Collection ) query.execute(); 890 assertEquals(1, result.size()); 891 Book b = (Book) result.iterator().next(); 892 893 tx.lock(b, Transaction.WRITE); 894 assertNull(b.getPublisher()); 895 Publisher publisher = new PublisherImpl(name); 896 b.setPublisher(publisher); 897 tx.commit(); 899 900 tx.begin(); 901 tx.getBroker().clearCache(); 902 query = odmg.newOQLQuery(); 903 query.create("select books from " + Book.class.getName() + " where title like $1"); 904 query.bind(name); 905 result = (Collection ) query.execute(); 906 assertEquals(1, result.size()); 907 b = (Book) result.iterator().next(); 908 tx.commit(); 909 910 assertEquals(new Integer (versionBook.intValue() + 1), b.getVersion()); 912 assertNotNull(b.getPublisher()); 914 assertNotNull(b.getPublisher().getVersion()); 915 assertEquals(name, b.getPublisher().getName()); 916 } 917 918 921 public void testChangeOneToOneReference_8() throws Exception 922 { 923 String name = "testChangeOneToOneReference_8_" + System.currentTimeMillis(); 924 Date date = new Date (); 925 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 926 Book book = new Book(name, date, cover); 927 Publisher publisher = new PublisherImpl(name); 928 book.setPublisher(publisher); 929 930 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 931 tx.begin(); 932 database.makePersistent(book); 933 tx.commit(); 934 935 Integer versionBook = book.getVersion(); 936 Integer versionPublisher = book.getPublisher().getVersion(); 937 938 tx.begin(); 939 tx.getBroker().clearCache(); 940 OQLQuery query = odmg.newOQLQuery(); 941 query.create("select books from " + Book.class.getName() + " where title like $1"); 942 query.bind(name); 943 Collection result = (Collection ) query.execute(); 944 assertEquals(1, result.size()); 945 Book b = (Book) result.iterator().next(); 946 947 tx.lock(b, Transaction.WRITE); 948 b.setPublisher(null); 949 tx.commit(); 950 951 tx.begin(); 952 tx.getBroker().clearCache(); 953 query = odmg.newOQLQuery(); 954 query.create("select books from " + Book.class.getName() + " where title like $1"); 955 query.bind(name); 956 result = (Collection ) query.execute(); 957 assertEquals(1, result.size()); 958 b = (Book) result.iterator().next(); 959 tx.commit(); 960 961 assertEquals(new Integer (versionBook.intValue() + 1), b.getVersion()); 963 assertNull(b.getPublisher()); 964 965 tx.begin(); 966 tx.getBroker().clearCache(); 967 query = odmg.newOQLQuery(); 968 query.create("select publishers from " + Publisher.class.getName() + " where name like $1"); 969 query.bind(name); 970 result = (Collection ) query.execute(); 971 assertEquals(1, result.size()); 973 Publisher p = (Publisher) result.iterator().next(); 974 assertEquals(name, p.getName()); 975 assertEquals(versionPublisher, p.getVersion()); 977 tx.commit(); 978 } 979 980 983 public void testChangeOneToOneReference_8b() throws Exception 984 { 985 String name = "testChangeOneToOneReference_8b_" + System.currentTimeMillis(); 986 Date date = new Date (); 987 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 988 Book book = new Book(name, date, cover); 989 Publisher publisher = new PublisherImpl(name); 990 book.setPublisher(publisher); 991 992 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 993 tx.begin(); 994 database.makePersistent(book); 995 tx.commit(); 996 997 Integer versionBook = book.getVersion(); 998 999 tx.begin(); 1000 tx.getBroker().clearCache(); 1001 OQLQuery query = odmg.newOQLQuery(); 1002 query.create("select books from " + Book.class.getName() + " where title like $1"); 1003 query.bind(name); 1004 Collection result = (Collection ) query.execute(); 1005 assertEquals(1, result.size()); 1006 Book b = (Book) result.iterator().next(); 1007 1008 tx.lock(b, Transaction.WRITE); 1009 database.deletePersistent(b.getPublisher()); 1010 b.setPublisher(null); 1011 tx.commit(); 1012 1013 tx.begin(); 1014 tx.getBroker().clearCache(); 1015 query = odmg.newOQLQuery(); 1016 query.create("select books from " + Book.class.getName() + " where title like $1"); 1017 query.bind(name); 1018 result = (Collection ) query.execute(); 1019 assertEquals(1, result.size()); 1020 b = (Book) result.iterator().next(); 1021 tx.commit(); 1022 1023 assertEquals(new Integer (versionBook.intValue() + 1), b.getVersion()); 1025 assertNull(b.getPublisher()); 1026 1027 tx.begin(); 1028 tx.getBroker().clearCache(); 1029 query = odmg.newOQLQuery(); 1030 query.create("select publishers from " + Publisher.class.getName() + " where name like $1"); 1031 query.bind(name); 1032 result = (Collection ) query.execute(); 1033 assertEquals(0, result.size()); 1035 tx.commit(); 1036 } 1037 1038 1041 public void testCollectionReference_1() throws Exception 1042 { 1043 String name = "testCollectionReference_1_" + System.currentTimeMillis(); 1044 ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, true); 1045 1046 Date date = new Date (); 1047 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 1048 Book book = new Book(name, date, cover); 1049 Review r1 = new Review(name); 1050 Review r2 = new Review(name); 1051 Review r3 = new Review(name); 1052 ArrayList reviews = new ArrayList (); 1053 reviews.add(r1); 1054 reviews.add(r2); 1055 reviews.add(r3); 1056 book.setReviews(reviews); 1057 1058 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 1059 tx.begin(); 1060 database.makePersistent(book); 1061 tx.commit(); 1062 1064 tx.begin(); 1065 tx.getBroker().clearCache(); 1066 OQLQuery query = odmg.newOQLQuery(); 1067 query.create("select books from " + Book.class.getName() + " where title like $1"); 1068 query.bind(name); 1069 Collection result = (Collection ) query.execute(); 1070 assertEquals(1, result.size()); 1071 Book b = (Book) result.iterator().next(); 1072 CollectionProxy handler = ProxyHelper.getCollectionProxy(b.getReviews()); 1073 assertFalse("Don't expect an materialized collection proxy", handler.isLoaded()); 1074 handler.addListener(new CollectionProxyListener() 1075 { 1076 public void beforeLoading(CollectionProxyDefaultImpl colProxy) 1077 { 1078 fail("Collection proxy shouldn't be materialized"); 1079 } 1080 1081 public void afterLoading(CollectionProxyDefaultImpl colProxy) 1082 { 1083 } 1084 }); 1085 assertNotNull(b.getReviews()); 1086 assertEquals(3, b.getReviews().size()); 1087 1088 tx.lock(b, Transaction.WRITE); 1089 tx.commit(); 1090 } 1091 1092 1095 public void testCollectionReference_2a() throws Exception 1096 { 1097 String name = "testCollectionReference_2a_" + System.currentTimeMillis(); 1098 Date date = new Date (); 1099 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 1100 Book book = new Book(name, date, cover); 1101 Review r1 = new Review(name); 1102 Review r2 = new Review(name); 1103 Review r3 = new Review(name); 1104 ArrayList reviews = new ArrayList (); 1105 reviews.add(r1); 1106 reviews.add(r2); 1107 reviews.add(r3); 1108 book.setReviews(reviews); 1109 1110 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 1111 tx.begin(); 1112 database.makePersistent(book); 1113 tx.commit(); 1114 1116 Integer versionBook = book.getVersion(); 1117 Integer versionR1 = ((Review) book.getReviews().get(0)).getVersion(); 1118 1119 tx.begin(); 1120 tx.getBroker().clearCache(); 1121 OQLQuery query = odmg.newOQLQuery(); 1122 query.create("select books from " + Book.class.getName() + " where title like $1"); 1123 query.bind(name); 1124 Collection result = (Collection ) query.execute(); 1125 assertEquals(1, result.size()); 1126 Book b = (Book) result.iterator().next(); 1127 assertEquals(versionBook, b.getVersion()); 1128 assertNotNull(b.getReviews()); 1129 assertEquals(3, b.getReviews().size()); 1130 1131 tx.lock(b, Transaction.WRITE); 1132 Review newR2 = (Review) b.getReviews().get(1); 1133 newR2.setSummary("updated" + name); 1134 final int newR2id = newR2.getId().intValue(); 1135 tx.commit(); 1136 1137 assertEquals(versionBook, b.getVersion()); 1138 Integer versionR1New = ((Review) b.getReviews().get(0)).getVersion(); 1140 assertEquals(versionR1, versionR1New); 1141 1142 tx.begin(); 1143 tx.getBroker().clearCache(); 1144 query = odmg.newOQLQuery(); 1145 query.create("select books from " + Book.class.getName() + " where title like $1"); 1146 query.bind(name); 1147 result = (Collection ) query.execute(); 1148 assertEquals(1, result.size()); 1149 b = (Book) result.iterator().next(); 1150 assertNotNull(b.getReviews()); 1151 assertEquals(3, b.getReviews().size()); 1152 1153 final List updatedReviews = b.getReviews(); 1155 for (int i = 0; i < updatedReviews.size(); i++) 1156 { 1157 newR2 = (Review) updatedReviews.get(i); 1158 if (newR2id == newR2.getId().intValue()) { 1159 break; 1160 } 1161 } 1162 assertEquals("Could not find the updated review in the returned results", 1163 newR2id, newR2.getId().intValue()); 1164 assertEquals("updated" + name, newR2.getSummary()); 1165 assertEquals(versionBook, b.getVersion()); 1166 versionR1New = ((Review) b.getReviews().get(0)).getVersion(); 1168 assertEquals(versionR1, versionR1New); 1169 } 1170 1171 1174 public void testCollectionReference_2b() throws Exception 1175 { 1176 String name = "testCollectionReference_2b_" + System.currentTimeMillis(); 1177 1178 ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, true); 1179 1180 Date date = new Date (); 1181 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 1182 Book book = new Book(name, date, cover); 1183 Review r1 = new Review(name); 1184 Review r2 = new Review(name); 1185 Review r3 = new Review(name); 1186 ArrayList reviews = new ArrayList (); 1187 reviews.add(r1); 1188 reviews.add(r2); 1189 reviews.add(r3); 1190 book.setReviews(reviews); 1191 1192 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 1193 tx.begin(); 1194 database.makePersistent(book); 1195 tx.commit(); 1196 1198 Integer versionBook = book.getVersion(); 1199 Integer versionR0 = ((Review) book.getReviews().get(0)).getVersion(); 1200 Integer versionR1 = ((Review) book.getReviews().get(1)).getVersion(); 1201 1202 tx.begin(); 1203 tx.getBroker().clearCache(); 1204 OQLQuery query = odmg.newOQLQuery(); 1206 query.create("select books from " + Book.class.getName() + " where title like $1"); 1207 query.bind(name); 1208 Collection result = (Collection ) query.execute(); 1209 assertEquals(1, result.size()); 1210 Book b = (Book) result.iterator().next(); 1211 assertEquals(versionBook, b.getVersion()); 1212 assertNotNull(b.getReviews()); 1213 assertEquals(3, b.getReviews().size()); 1214 assertEquals(versionR0, ((Review) b.getReviews().get(0)).getVersion()); 1215 assertEquals(versionR1, ((Review) b.getReviews().get(1)).getVersion()); 1216 1217 tx.lock(b, Transaction.WRITE); 1219 Review newR1 = (Review) b.getReviews().get(1); 1221 newR1.setSummary("updated" + name); 1222 tx.commit(); 1224 1226 assertEquals(versionBook, b.getVersion()); 1227 assertEquals(3, b.getReviews().size()); 1228 1229 Integer versionR1New = ((Review) b.getReviews().get(1)).getVersion(); 1231 assertEquals(new Integer (versionR1.intValue() + 1), versionR1New); 1232 Integer versionR0New = ((Review) b.getReviews().get(0)).getVersion(); 1234 assertEquals(versionR0, versionR0New); 1235 1236 tx.begin(); 1237 tx.getBroker().clearCache(); 1238 query = odmg.newOQLQuery(); 1239 query.create("select books from " + Book.class.getName() + " where title like $1"); 1240 query.bind(name); 1241 result = (Collection ) query.execute(); 1242 assertEquals(1, result.size()); 1243 b = (Book) result.iterator().next(); 1244 assertNotNull(b.getReviews()); 1245 assertEquals(3, b.getReviews().size()); 1246 tx.commit(); 1247 1248 tx.begin(); 1250 tx.getBroker().clearCache(); 1251 query = odmg.newOQLQuery(); 1252 query.create("select reviews from " + Review.class.getName() + " where summary like $1"); 1253 query.bind("updated" + name); 1254 result = (Collection ) query.execute(); 1255 tx.commit(); 1256 assertEquals(1, result.size()); 1258 tx.begin(); 1260 tx.getBroker().clearCache(); 1261 query = odmg.newOQLQuery(); 1262 query.create("select reviews from " + Review.class.getName() + " where summary like $1"); 1263 query.bind(name); 1264 result = (Collection ) query.execute(); 1265 tx.commit(); 1266 1267 assertEquals(2, result.size()); 1268 assertEquals(versionR0, ((Review) new ArrayList (result).get(0)).getVersion()); 1269 1270 } 1271 1272 1277 public void testCollectionReference_3() throws Exception 1278 { 1279 String name = "testCollectionReference_3_" + System.currentTimeMillis(); 1280 Date date = new Date (); 1281 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 1282 Book book = new Book(name, date, cover); 1283 Review r1 = new Review(name); 1284 ArrayList reviews = new ArrayList (); 1285 reviews.add(r1); 1286 book.setReviews(reviews); 1287 1288 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 1289 tx.begin(); 1290 database.makePersistent(book); 1291 tx.commit(); 1292 1293 Integer versionBook = book.getVersion(); 1294 Integer versionR1 = ((Review) book.getReviews().get(0)).getVersion(); 1295 1296 tx.begin(); 1297 tx.getBroker().clearCache(); 1298 OQLQuery query = odmg.newOQLQuery(); 1299 query.create("select books from " + Book.class.getName() + " where title like $1"); 1300 query.bind(name); 1301 Collection result = (Collection ) query.execute(); 1302 assertEquals(1, result.size()); 1303 Book b = (Book) result.iterator().next(); 1304 assertEquals(versionBook, b.getVersion()); 1305 assertNotNull(b.getReviews()); 1306 assertEquals(1, b.getReviews().size()); 1307 1308 tx.lock(b, Transaction.WRITE); 1310 tx.setCascadingDelete(Book.class, "reviews", false); 1312 Review newR1 = (Review) b.getReviews().remove(0); 1313 tx.commit(); 1314 1316 assertEquals(versionBook, b.getVersion()); 1318 assertEquals(new Integer (versionR1.intValue() + 1), newR1.getVersion()); 1320 1321 tx.begin(); 1322 tx.getBroker().clearCache(); 1323 query = odmg.newOQLQuery(); 1324 query.create("select reviews from " + Review.class.getName() + " where summary like $1"); 1325 query.bind(name); 1326 result = (Collection ) query.execute(); 1327 tx.commit(); 1328 1329 assertEquals(1, result.size()); 1331 Review r = (Review) result.iterator().next(); 1332 assertEquals(new Integer (versionR1.intValue() + 1), r.getVersion()); 1334 1335 tx.begin(); 1336 query = odmg.newOQLQuery(); 1337 query.create("select books from " + Book.class.getName() + " where title like $1"); 1338 query.bind(name); 1339 result = (Collection ) query.execute(); 1340 tx.commit(); 1341 1342 assertEquals(1, result.size()); 1343 b = (Book) result.iterator().next(); 1344 assertEquals(versionBook, b.getVersion()); 1345 assertEquals(0, b.getReviews().size()); 1347 } 1348 1349 1352 public void testCollectionReference_3b() throws Exception 1353 { 1354 String name = "testCollectionReference_3b_" + System.currentTimeMillis(); 1355 Date date = new Date (); 1356 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 1357 Book book = new Book(name, date, cover); 1358 Review r1 = new Review(name); 1359 ArrayList reviews = new ArrayList (); 1360 reviews.add(r1); 1361 book.setReviews(reviews); 1362 1363 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 1364 tx.begin(); 1365 database.makePersistent(book); 1366 tx.commit(); 1367 1368 Integer versionBook = book.getVersion(); 1369 1370 tx.begin(); 1371 tx.getBroker().clearCache(); 1372 OQLQuery query = odmg.newOQLQuery(); 1373 query.create("select books from " + Book.class.getName() + " where title like $1"); 1374 query.bind(name); 1375 Collection result = (Collection ) query.execute(); 1376 assertEquals(1, result.size()); 1377 Book b = (Book) result.iterator().next(); 1378 assertEquals(versionBook, b.getVersion()); 1379 assertNotNull(b.getReviews()); 1380 assertEquals(1, b.getReviews().size()); 1381 1382 tx.lock(b, Transaction.WRITE); 1383 Review newR1 = (Review) b.getReviews().remove(0); 1385 database.deletePersistent(newR1); 1386 tx.commit(); 1387 1388 assertEquals(versionBook, b.getVersion()); 1390 1391 tx.begin(); 1392 query = odmg.newOQLQuery(); 1393 query.create("select reviews from " + Review.class.getName() + " where summary like $1"); 1394 query.bind(name); 1395 result = (Collection ) query.execute(); 1396 tx.commit(); 1397 1398 assertEquals(0, result.size()); 1399 1400 tx.begin(); 1401 query = odmg.newOQLQuery(); 1402 query.create("select books from " + Book.class.getName() + " where title like $1"); 1403 query.bind(name); 1404 result = (Collection ) query.execute(); 1405 tx.commit(); 1406 1407 assertEquals(1, result.size()); 1408 b = (Book) result.iterator().next(); 1409 assertEquals(versionBook, b.getVersion()); 1410 assertEquals(0, b.getReviews().size()); 1411 } 1412 1413 1416 public void testCollectionReference_3c() throws Exception 1417 { 1418 String name = "testCollectionReference_3c_" + System.currentTimeMillis(); 1419 Date date = new Date (); 1420 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 1421 Book book = new Book(name, date, cover); 1422 Review r1 = new Review(name); 1423 ArrayList reviews = new ArrayList (); 1424 reviews.add(r1); 1425 book.setReviews(reviews); 1426 1427 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 1428 tx.begin(); 1429 database.makePersistent(book); 1430 tx.commit(); 1431 1432 Integer versionBook = book.getVersion(); 1433 1434 tx.begin(); 1435 tx.getBroker().clearCache(); 1436 OQLQuery query = odmg.newOQLQuery(); 1437 query.create("select books from " + Book.class.getName() + " where title like $1"); 1438 query.bind(name); 1439 Collection result = (Collection ) query.execute(); 1440 assertEquals(1, result.size()); 1441 Book b = (Book) result.iterator().next(); 1442 assertEquals(versionBook, b.getVersion()); 1443 assertNotNull(b.getReviews()); 1444 assertEquals(1, b.getReviews().size()); 1445 1446 tx.lock(b, Transaction.WRITE); 1447 tx.setCascadingDelete(Book.class, "reviews", true); 1448 b.getReviews().remove(0); 1450 tx.commit(); 1451 1452 assertEquals(versionBook, b.getVersion()); 1454 1455 tx.begin(); 1456 tx.getBroker().clearCache(); 1457 query = odmg.newOQLQuery(); 1458 query.create("select reviews from " + Review.class.getName() + " where summary like $1"); 1459 query.bind(name); 1460 result = (Collection ) query.execute(); 1461 tx.commit(); 1462 1463 assertEquals(0, result.size()); 1466 1467 1468 tx.begin(); 1469 query = odmg.newOQLQuery(); 1470 query.create("select books from " + Book.class.getName() + " where title like $1"); 1471 query.bind(name); 1472 result = (Collection ) query.execute(); 1473 tx.commit(); 1474 1475 assertEquals(1, result.size()); 1476 b = (Book) result.iterator().next(); 1477 assertEquals(versionBook, b.getVersion()); 1478 assertEquals(0, b.getReviews().size()); 1480 } 1481 1482 1485 public void testCollectionReference_4a() throws Exception 1486 { 1487 String name = "testCollectionReference_4_" + System.currentTimeMillis(); 1488 Date date = new Date (); 1489 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 1490 Book book = new Book(name, date, cover); 1491 Review r1 = new Review(name); 1492 Review r2 = new Review(name); 1493 Review r3 = new Review(name); 1494 Author a1 = new Author(name, null); 1495 Author a2 = new Author(name, null); 1496 r1.setAuthor(a1); 1497 r2.setAuthor(a1); 1498 r3.setAuthor(a2); 1499 ArrayList reviews = new ArrayList (); 1500 reviews.add(r1); 1501 reviews.add(r2); 1502 reviews.add(r3); 1503 book.setReviews(reviews); 1504 1505 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 1506 tx.begin(); 1507 database.makePersistent(book); 1508 tx.commit(); 1509 1510 Integer versionBook = book.getVersion(); 1511 1512 tx.begin(); 1513 tx.getBroker().clearCache(); 1514 OQLQuery query = odmg.newOQLQuery(); 1515 query.create("select books from " + Book.class.getName() + " where title like $1"); 1516 query.bind(name); 1517 Collection result = (Collection ) query.execute(); 1518 assertEquals(1, result.size()); 1519 Book b = (Book) result.iterator().next(); 1520 assertEquals(versionBook, b.getVersion()); 1521 assertNotNull(b.getReviews()); 1522 assertEquals(3, b.getReviews().size()); 1523 assertNotNull(((Review) b.getReviews().get(0)).getAuthor()); 1524 assertNotNull(((Review) b.getReviews().get(1)).getAuthor()); 1525 assertNotNull(((Review) b.getReviews().get(2)).getAuthor()); 1526 1527 tx.setCascadingDelete(Book.class, "reviews", false); 1531 database.deletePersistent(b); 1532 tx.commit(); 1533 1535 tx.begin(); 1536 tx.getBroker().clearCache(); 1537 query = odmg.newOQLQuery(); 1538 query.create("select reviews from " + Review.class.getName() + " where summary like $1"); 1539 query.bind(name); 1540 result = (Collection ) query.execute(); 1541 tx.commit(); 1542 assertEquals(3, result.size()); 1544 List list = new ArrayList (result); 1545 assertNotNull(((Review) list.get(0)).getAuthor()); 1546 assertNotNull(((Review) list.get(1)).getAuthor()); 1547 assertNotNull(((Review) list.get(2)).getAuthor()); 1548 Review newR1 = (Review) list.get(0); 1549 assertNull(newR1.getFkBook()); 1551 1552 1553 tx.begin(); 1554 query = odmg.newOQLQuery(); 1555 query.create("select books from " + Book.class.getName() + " where title like $1"); 1556 query.bind(name); 1557 result = (Collection ) query.execute(); 1558 tx.commit(); 1559 assertEquals(0, result.size()); 1560 } 1561 1562 1565 public void testCollectionReference_4b() throws Exception 1566 { 1567 String name = "testCollectionReference_4_" + System.currentTimeMillis(); 1568 Date date = new Date (); 1569 byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; 1570 Book book = new Book(name, date, cover); 1571 Review r1 = new Review(name); 1572 Review r2 = new Review(name); 1573 Review r3 = new Review(name); 1574 Author a1 = new Author(name, null); 1575 Author a2 = new Author(name, null); 1576 r1.setAuthor(a1); 1577 r2.setAuthor(a1); 1578 r3.setAuthor(a2); 1579 ArrayList reviews = new ArrayList (); 1580 reviews.add(r1); 1581 reviews.add(r2); 1582 reviews.add(r3); 1583 book.setReviews(reviews); 1584 ArrayList authors = new ArrayList (); 1585 authors.add(a1); 1586 authors.add(a2); 1587 book.setAuthors(authors); 1588 1589 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 1590 tx.begin(); 1591 database.makePersistent(book); 1592 tx.commit(); 1593 1594 Integer versionBook = book.getVersion(); 1595 1596 tx.begin(); 1597 tx.getBroker().clearCache(); 1598 OQLQuery query = odmg.newOQLQuery(); 1599 query.create("select books from " + Book.class.getName() + " where title like $1"); 1600 query.bind(name); 1601 Collection result = (Collection ) query.execute(); 1602 assertEquals(1, result.size()); 1603 Book b = (Book) result.iterator().next(); 1604 assertEquals(versionBook, b.getVersion()); 1605 assertNotNull(b.getReviews()); 1606 assertEquals(3, b.getReviews().size()); 1607 assertNotNull(((Review) b.getReviews().get(0)).getAuthor()); 1608 assertNotNull(((Review) b.getReviews().get(1)).getAuthor()); 1609 assertNotNull(((Review) b.getReviews().get(2)).getAuthor()); 1610 assertNotNull(b.getAuthors()); 1611 assertEquals(2, b.getAuthors().size()); 1612 Author newA = (Author) b.getAuthors().get(0); 1613 boolean failed = true; 1614 for(Iterator iterator = b.getReviews().iterator(); iterator.hasNext();) 1615 { 1616 Review review = (Review) iterator.next(); 1617 if(newA.equals(review.getAuthor())) 1618 { 1619 assertSame(newA, review.getAuthor()); 1621 failed = false; 1622 } 1623 } 1624 if(failed) fail("Expect the same object instance, but not found for " + newA); 1625 1626 tx.setCascadingDelete(Book.class, "reviews", false); 1630 database.deletePersistent(b); 1631 tx.commit(); 1632 1633 tx.begin(); 1634 tx.getBroker().clearCache(); 1635 query = odmg.newOQLQuery(); 1636 query.create("select reviews from " + Review.class.getName() + " where summary like $1"); 1637 query.bind(name); 1638 result = (Collection ) query.execute(); 1639 tx.commit(); 1640 assertEquals(3, result.size()); 1642 List list = new ArrayList (result); 1643 assertNotNull(((Review) list.get(0)).getAuthor()); 1644 assertNotNull(((Review) list.get(1)).getAuthor()); 1645 assertNotNull(((Review) list.get(2)).getAuthor()); 1646 Review newR1 = (Review) list.get(0); 1647 assertNull(newR1.getFkBook()); 1649 newA = ((Review) list.get(0)).getAuthor(); 1650 assertEquals(0, newA.getBooks().size()); 1651 1652 1653 tx.begin(); 1654 query = odmg.newOQLQuery(); 1655 query.create("select books from " + Book.class.getName() + " where title like $1"); 1656 query.bind(name); 1657 result = (Collection ) query.execute(); 1658 tx.commit(); 1659 assertEquals(0, result.size()); 1660 } 1661 1662 public static final class Book implements Serializable 1666 { 1667 private Integer id; 1668 private String title; 1669 private Date publicationDate; 1670 private byte[] cover; 1671 private Integer version; 1672 1673 private List authors; 1674 private List reviews; 1675 private Publisher publisher; 1676 1677 public Book() 1678 { 1679 } 1680 1681 public Book(String title, Date publicationDate, byte[] cover) 1682 { 1683 this.title = title; 1684 this.publicationDate = publicationDate; 1685 this.cover = cover; 1686 } 1687 1688 public void addAuthor(Author author) 1689 { 1690 if(authors == null) 1691 { 1692 authors = new ArrayList (); 1693 } 1694 authors.add(author); 1695 } 1696 1697 public void addReview(Review review) 1698 { 1699 if(reviews == null) 1700 { 1701 reviews = new ArrayList (); 1702 } 1703 reviews.add(review); 1704 } 1705 1706 public boolean removeReview(Review review) 1707 { 1708 if(reviews != null) return reviews.remove(review); 1709 else return false; 1710 } 1711 1712 public Integer getId() 1713 { 1714 return id; 1715 } 1716 1717 public void setId(Integer id) 1718 { 1719 this.id = id; 1720 } 1721 1722 public String getTitle() 1723 { 1724 return title; 1725 } 1726 1727 public void setTitle(String title) 1728 { 1729 this.title = title; 1730 } 1731 1732 public Date getPublicationDate() 1733 { 1734 return publicationDate; 1735 } 1736 1737 public void setPublicationDate(Date publicationDate) 1738 { 1739 this.publicationDate = publicationDate; 1740 } 1741 1742 public byte[] getCover() 1743 { 1744 return cover; 1745 } 1746 1747 public void setCover(byte[] cover) 1748 { 1749 this.cover = cover; 1750 } 1751 1752 public Integer getVersion() 1753 { 1754 return version; 1755 } 1756 1757 public void setVersion(Integer version) 1758 { 1759 this.version = version; 1760 } 1761 1762 public List getAuthors() 1763 { 1764 return authors; 1765 } 1766 1767 public void setAuthors(List authors) 1768 { 1769 this.authors = authors; 1770 } 1771 1772 public List getReviews() 1773 { 1774 return reviews; 1775 } 1776 1777 public void setReviews(List reviews) 1778 { 1779 this.reviews = reviews; 1780 } 1781 1782 public Publisher getPublisher() 1783 { 1784 return publisher; 1785 } 1786 1787 public void setPublisher(Publisher publisher) 1788 { 1789 this.publisher = publisher; 1790 } 1791 } 1792 1793 public static final class Author implements Serializable 1794 { 1795 private Integer id; 1796 private String name; 1797 private List books; 1798 private Integer version; 1799 1800 public Author() 1801 { 1802 } 1803 1804 public Author(String name, List books) 1805 { 1806 this.name = name; 1807 this.books = books; 1808 } 1809 1810 public void addBook(Book book) 1811 { 1812 if(books == null) 1813 { 1814 books = new ArrayList (); 1815 } 1816 books.add(book); 1817 } 1818 1819 public Integer getId() 1820 { 1821 return id; 1822 } 1823 1824 public void setId(Integer id) 1825 { 1826 this.id = id; 1827 } 1828 1829 public String getName() 1830 { 1831 return name; 1832 } 1833 1834 public void setName(String name) 1835 { 1836 this.name = name; 1837 } 1838 1839 public List getBooks() 1840 { 1841 return books; 1842 } 1843 1844 public void setBooks(List books) 1845 { 1846 this.books = books; 1847 } 1848 1849 public Integer getVersion() 1850 { 1851 return version; 1852 } 1853 1854 public void setVersion(Integer version) 1855 { 1856 this.version = version; 1857 } 1858 } 1859 1860 public static interface Publisher extends Serializable 1861 { 1862 public Integer getId(); 1863 public void setId(Integer id); 1864 public String getName(); 1865 public void setName(String name); 1866 public Integer getVersion(); 1867 public void setVersion(Integer version); 1868 } 1869 1870 public static final class PublisherImpl implements Publisher 1871 { 1872 private Integer id; 1873 private String name; 1874 private Integer version; 1875 1876 public PublisherImpl() 1877 { 1878 } 1879 1880 public PublisherImpl(String name) 1881 { 1882 this.name = name; 1883 } 1884 1885 public Integer getId() 1886 { 1887 return id; 1888 } 1889 1890 public void setId(Integer id) 1891 { 1892 this.id = id; 1893 } 1894 1895 public String getName() 1896 { 1897 return name; 1898 } 1899 1900 public void setName(String name) 1901 { 1902 this.name = name; 1903 } 1904 1905 public Integer getVersion() 1906 { 1907 return version; 1908 } 1909 1910 public void setVersion(Integer version) 1911 { 1912 this.version = version; 1913 } 1914 } 1915 1916 public static final class Review implements Serializable 1917 { 1918 private Integer id; 1919 private String summary; 1920 private Integer fkBook; 1921 private Integer version; 1922 private Author author; 1923 1924 public Review() 1925 { 1926 } 1927 1928 public Review(String summary) 1929 { 1930 this.summary = summary; 1931 } 1932 1933 public Integer getId() 1934 { 1935 return id; 1936 } 1937 1938 public void setId(Integer id) 1939 { 1940 this.id = id; 1941 } 1942 1943 public Integer getFkBook() 1944 { 1945 return fkBook; 1946 } 1947 1948 public void setFkBook(Integer fkBook) 1949 { 1950 this.fkBook = fkBook; 1951 } 1952 1953 public String getSummary() 1954 { 1955 return summary; 1956 } 1957 1958 public void setSummary(String summary) 1959 { 1960 this.summary = summary; 1961 } 1962 1963 public Integer getVersion() 1964 { 1965 return version; 1966 } 1967 1968 public void setVersion(Integer version) 1969 { 1970 this.version = version; 1971 } 1972 1973 public Author getAuthor() 1974 { 1975 return author; 1976 } 1977 1978 public void setAuthor(Author author) 1979 { 1980 this.author = author; 1981 } 1982 1983 public boolean equals(Object obj) 1984 { 1985 boolean result = false; 1986 if(obj instanceof Review) 1987 { 1988 Review other = (Review) obj; 1989 result = new EqualsBuilder() 1990 .append(id, other.id) 1991 .append(summary, other.summary) 1992 .append(version, other.version) 1993 .append(fkBook, other.fkBook) 1994 .append(author, other.author) 1995 .isEquals(); 1996 } 1997 return result; 1998 } 1999 } 2000 2001} 2002 | Popular Tags |