0001: package org.apache.ojb.odmg;
0002:
0003: /* Copyright 2002-2005 The Apache Software Foundation
0004: *
0005: * Licensed under the Apache License, Version 2.0 (the "License");
0006: * you may not use this file except in compliance with the License.
0007: * You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: import java.io.Serializable;
0019: import java.util.ArrayList;
0020: import java.util.Collection;
0021: import java.util.Date;
0022: import java.util.List;
0023: import java.util.Iterator;
0024:
0025: import org.apache.commons.lang.SerializationUtils;
0026: import org.apache.commons.lang.SystemUtils;
0027: import org.apache.commons.lang.builder.EqualsBuilder;
0028: import org.apache.ojb.broker.Identity;
0029: import org.apache.ojb.broker.query.Criteria;
0030: import org.apache.ojb.broker.query.Query;
0031: import org.apache.ojb.broker.query.QueryFactory;
0032: import org.apache.ojb.broker.core.proxy.CollectionProxy;
0033: import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl;
0034: import org.apache.ojb.broker.core.proxy.CollectionProxyListener;
0035: import org.apache.ojb.broker.core.proxy.IndirectionHandler;
0036: import org.apache.ojb.broker.core.proxy.MaterializationListener;
0037: import org.apache.ojb.broker.core.proxy.ProxyHelper;
0038: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
0039: import org.apache.ojb.junit.ODMGTestCase;
0040: import org.odmg.OQLQuery;
0041: import org.odmg.Transaction;
0042:
0043: /**
0044: * Check the quality of object image comparison:
0045: * - new referenced objects
0046: * - deleted referenced objects
0047: * - deleted referenced objects
0048: * ...etc.
0049: *
0050: * Test cases for refactored odmg-api implementation. These tests asserts previously outstanding
0051: * ODMG-issues and proxy object handling in the ODMG API.
0052: *
0053: * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
0054: * @version $Id: ObjectImageTest.java,v 1.1.2.8 2005/12/13 18:18:25 arminw Exp $
0055: */
0056: public class ObjectImageTest extends ODMGTestCase {
0057: static final int NONE = ObjectReferenceDescriptor.CASCADE_NONE;
0058: static final int LINK = ObjectReferenceDescriptor.CASCADE_LINK;
0059: static final int OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT;
0060: static final String EOL = SystemUtils.LINE_SEPARATOR;
0061:
0062: public static void main(String[] args) {
0063: junit.textui.TestRunner
0064: .main(new String[] { ObjectImageTest.class.getName() });
0065: }
0066:
0067: public void testReplaceOneToOneReference() throws Exception {
0068: String prefix = "testReplaceOneToOneReference_"
0069: + System.currentTimeMillis();
0070: ojbChangeReferenceSetting(Book.class, "reviews", true, NONE,
0071: NONE, false);
0072:
0073: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0074: tx.begin();
0075: Book book = new Book(prefix, null, null);
0076: Publisher p_1 = new PublisherImpl(prefix);
0077: Publisher p_2 = new PublisherImpl(prefix + "_replaced");
0078: book.setPublisher(p_1);
0079: database.makePersistent(book);
0080: database.makePersistent(p_1);
0081: database.makePersistent(p_2);
0082: tx.commit();
0083:
0084: Integer book_version = book.getVersion();
0085: Integer p1_version = p_1.getVersion();
0086: Integer p2_version = p_2.getVersion();
0087:
0088: tx.begin();
0089: tx.lock(book, Transaction.WRITE);
0090: tx.lock(book.getPublisher(), Transaction.READ);
0091: tx.lock(p_2, Transaction.READ);
0092: book.setPublisher(p_2);
0093: tx.commit();
0094:
0095: assertEquals(book_version.intValue() + 1, book.getVersion()
0096: .intValue());
0097: assertEquals(p1_version, p_1.getVersion());
0098: assertEquals(p2_version, p_2.getVersion());
0099: }
0100:
0101: public void testAddCollectionObjectToExistingObject()
0102: throws Exception {
0103: String prefix = "testAddCollectionObjectToExistingObject_"
0104: + System.currentTimeMillis();
0105: ojbChangeReferenceSetting(Book.class, "reviews", true, NONE,
0106: NONE, false);
0107:
0108: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0109: tx.begin();
0110: Book book = new Book(prefix, null, null);
0111: Review r1 = new Review(prefix + "_1");
0112: database.makePersistent(book);
0113: database.makePersistent(r1);
0114: tx.commit();
0115:
0116: Integer book_version = book.getVersion();
0117: Integer r_1_version = r1.getVersion();
0118:
0119: Review r2 = new Review(prefix + "_2");
0120: tx.begin();
0121: tx.lock(r1, Transaction.WRITE);
0122: tx.lock(book, Transaction.READ);
0123: book.addReview(r1);
0124: book.addReview(r2);
0125: database.makePersistent(r2);
0126: tx.commit();
0127:
0128: assertEquals(book_version, book.getVersion());
0129: assertTrue(book.getId() != null);
0130:
0131: Integer r_2_version = r2.getVersion();
0132:
0133: tx.begin();
0134: tx.getBroker().clearCache();
0135: Book loadedCopy = (Book) tx.getBroker().getObjectByIdentity(
0136: tx.getBroker().serviceIdentity().buildIdentity(
0137: Book.class, book.getId()));
0138: assertNotNull(loadedCopy);
0139: assertNotNull(loadedCopy.getReviews());
0140: assertEquals(2, loadedCopy.getReviews().size());
0141: assertEquals(book_version, loadedCopy.getVersion());
0142: Review new_r1 = (Review) loadedCopy.getReviews().get(0);
0143: Review new_r2 = (Review) loadedCopy.getReviews().get(1);
0144: assertEquals(r_1_version.intValue() + 1, new_r1.getVersion()
0145: .intValue());
0146: assertEquals(r_2_version, new_r2.getVersion());
0147:
0148: tx.getBroker().clearCache();
0149: Criteria criteria = new Criteria();
0150: criteria.addLike("title", prefix);
0151: Query q = QueryFactory.newQuery(Book.class, criteria);
0152: Collection books = tx.getBroker().getCollectionByQuery(q);
0153: assertNotNull(books);
0154: assertEquals(1, books.size());
0155: Book new_book = (Book) books.iterator().next();
0156: tx.commit();
0157: assertEquals(book_version, new_book.getVersion());
0158:
0159: tx.begin();
0160: tx.lock(loadedCopy, Transaction.WRITE);
0161: Review removed = (Review) loadedCopy.getReviews().remove(0);
0162: Review stayed = (Review) loadedCopy.getReviews().get(0);
0163: tx.commit();
0164: // expect same version, nothing should be changed
0165: assertEquals(r_2_version, stayed.getVersion());
0166: //
0167: //assertEquals(r_1_version, removed.getVersion());
0168:
0169: tx.begin();
0170: OQLQuery query = odmg.newOQLQuery();
0171: query.create("select books from " + Book.class.getName()
0172: + " where title like $1");
0173: query.bind(prefix);
0174: Collection result = (Collection) query.execute();
0175: assertEquals(1, result.size());
0176: Book b = (Book) result.iterator().next();
0177: tx.commit();
0178:
0179: assertEquals(1, b.getReviews().size());
0180: Review r = (Review) b.getReviews().get(0);
0181: if (!r.equals(r1) && !r.equals(r2)) {
0182: fail("Wrong object or wrong object version returned. Returned obj was "
0183: + EOL
0184: + r
0185: + " expected object was "
0186: + EOL
0187: + r1
0188: + " or " + EOL + r2);
0189: }
0190: }
0191:
0192: /**
0193: * test persistence by reachability of collection reference objects
0194: */
0195: public void testPersistenceByReachability_1() throws Exception {
0196: String name = "testPersistenceByReachability_1_"
0197: + System.currentTimeMillis();
0198: ojbChangeReferenceSetting(Book.class, "reviews", true, NONE,
0199: NONE, true);
0200:
0201: Date date = new Date();
0202: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0203: Book book = new Book(name, date, cover);
0204: Review r1 = new Review(name);
0205: Review r2 = new Review(name);
0206: Review r3 = new Review(name + "_not_persistent");
0207: ArrayList reviews = new ArrayList();
0208: reviews.add(r1);
0209: reviews.add(r2);
0210: reviews.add(r3);
0211: book.setReviews(reviews);
0212:
0213: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0214: tx.begin();
0215: database.makePersistent(book);
0216: Review r4 = new Review(name + "_new_added");
0217: // add a new review after make persistent main object
0218: book.addReview(r4);
0219: tx.setCascadingDelete(Book.class, true);
0220: // remove object after make persistent main object
0221: book.removeReview(r3);
0222: tx.commit();
0223: // System.err.println("## Insert main object with 3 referecnes");
0224:
0225: tx.begin();
0226: tx.getBroker().clearCache();
0227:
0228: OQLQuery query = odmg.newOQLQuery();
0229: query.create("select books from " + Book.class.getName()
0230: + " where title like $1");
0231: query.bind(name);
0232: Collection result = (Collection) query.execute();
0233: assertEquals(1, result.size());
0234: Book b = (Book) result.iterator().next();
0235: assertNotNull(b.getReviews());
0236: assertEquals(3, b.getReviews().size());
0237:
0238: query = odmg.newOQLQuery();
0239: query.create("select reviews from " + Review.class.getName()
0240: + " where summary like $1");
0241: query.bind(name + "_new_added");
0242: result = (Collection) query.execute();
0243: // we expect the delayed added Review object
0244: assertEquals(1, result.size());
0245:
0246: query = odmg.newOQLQuery();
0247: query.create("select reviews from " + Review.class.getName()
0248: + " where summary like $1");
0249: query.bind(name + "_not_persistent");
0250: result = (Collection) query.execute();
0251: // we expect the removed Review object wasn't persistet
0252: assertEquals(0, result.size());
0253: tx.commit();
0254: }
0255:
0256: /**
0257: * test persistence by reachability of collection reference objects
0258: */
0259: public void testPersistenceByReachability_2() throws Exception {
0260: String name = "testPersistenceByReachability_2_"
0261: + System.currentTimeMillis();
0262: ojbChangeReferenceSetting(Book.class, "reviews", true, NONE,
0263: NONE, true);
0264:
0265: Date date = new Date();
0266: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0267: Book book = new Book(name, date, cover);
0268: Review r1 = new Review(name);
0269: Review r2 = new Review(name);
0270: Review r3 = new Review(name + "_not_persistent");
0271: ArrayList reviews = new ArrayList();
0272: reviews.add(r1);
0273: reviews.add(r2);
0274: reviews.add(r3);
0275: book.setReviews(reviews);
0276:
0277: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0278: tx.begin();
0279: database.makePersistent(book);
0280: Review r4 = new Review(name + "_new_added");
0281: // add a new review after make persistent main object
0282: book.addReview(r4);
0283: tx.setCascadingDelete(Book.class, true);
0284: // remove object after make persistent main object
0285: book.removeReview(r3);
0286: tx.checkpoint();
0287:
0288: //tx.begin();
0289: tx.getBroker().clearCache();
0290:
0291: OQLQuery query = odmg.newOQLQuery();
0292: query.create("select books from " + Book.class.getName()
0293: + " where title like $1");
0294: query.bind(name);
0295: Collection result = (Collection) query.execute();
0296: assertEquals(1, result.size());
0297: Book b = (Book) result.iterator().next();
0298: assertNotNull(b.getReviews());
0299: assertEquals(3, b.getReviews().size());
0300:
0301: query = odmg.newOQLQuery();
0302: query.create("select reviews from " + Review.class.getName()
0303: + " where summary like $1");
0304: query.bind(name + "_new_added%");
0305: result = (Collection) query.execute();
0306: // we expect the delayed added Review object
0307: assertEquals(1, result.size());
0308:
0309: query = odmg.newOQLQuery();
0310: query.create("select reviews from " + Review.class.getName()
0311: + " where summary like $1");
0312: query.bind(name + "_not_persistent");
0313: result = (Collection) query.execute();
0314: // we expect the removed Review object wasn't persistet
0315: assertEquals(0, result.size());
0316:
0317: b.setTitle(name + "_updated");
0318: tx.commit();
0319:
0320: query = odmg.newOQLQuery();
0321: query.create("select books from " + Book.class.getName()
0322: + " where title like $1");
0323: query.bind(name + "_updated");
0324: result = (Collection) query.execute();
0325: assertEquals(1, result.size());
0326: Book b_updated = (Book) result.iterator().next();
0327: assertNotNull(b_updated.getReviews());
0328: assertEquals(3, b_updated.getReviews().size());
0329: assertEquals(name + "_updated", b_updated.getTitle());
0330: }
0331:
0332: public void testAddPersistentObjectTo1toN() throws Exception {
0333: String name = "testAddPersistentObjectTo1toN_"
0334: + System.currentTimeMillis();
0335: Review review = new Review(name);
0336: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0337: tx.begin();
0338: database.makePersistent(review);
0339: tx.commit();
0340:
0341: Integer versionReview = review.getVersion();
0342:
0343: Date date = new Date();
0344: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0345: Book book = new Book(name, date, cover);
0346: tx = (TransactionExt) odmg.newTransaction();
0347: tx.begin();
0348: // tx.lock(review, Transaction.WRITE);
0349: database.makePersistent(book);
0350: book.addReview(review);
0351: tx.commit();
0352:
0353: // the Review object has to be linked
0354: assertEquals("expect that this object was updated",
0355: versionReview.intValue() + 1, review.getVersion()
0356: .intValue());
0357:
0358: tx.begin();
0359: tx.getBroker().clearCache();
0360: OQLQuery query = odmg.newOQLQuery();
0361: query.create("select books from " + Book.class.getName()
0362: + " where title like $1");
0363: query.bind(name);
0364: Collection result = (Collection) query.execute();
0365: assertEquals(1, result.size());
0366: Book b = (Book) result.iterator().next();
0367: assertNotNull(b.getReviews());
0368: assertEquals(1, b.getReviews().size());
0369: tx.commit();
0370: }
0371:
0372: public void testAddPersistentObjectToMtoN() throws Exception {
0373: String name = "testAddPersistentObjectToMtoN_"
0374: + System.currentTimeMillis();
0375: Author author = new Author(name, null);
0376: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0377: tx.begin();
0378: database.makePersistent(author);
0379: tx.commit();
0380:
0381: Integer versionReview = author.getVersion();
0382:
0383: Date date = new Date();
0384: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0385: Book book = new Book(name, date, cover);
0386: tx = (TransactionExt) odmg.newTransaction();
0387: tx.begin();
0388: database.makePersistent(book);
0389: book.addAuthor(author);
0390: author.addBook(book);
0391: tx.commit();
0392:
0393: // the Review object has to be linked
0394: assertEquals("expect that this object wasn't updated",
0395: versionReview.intValue(), author.getVersion()
0396: .intValue());
0397:
0398: tx.begin();
0399: tx.getBroker().clearCache();
0400: OQLQuery query = odmg.newOQLQuery();
0401: query.create("select books from " + Book.class.getName()
0402: + " where title like $1");
0403: query.bind(name);
0404: Collection result = (Collection) query.execute();
0405: assertEquals(1, result.size());
0406: Book b = (Book) result.iterator().next();
0407: assertNotNull(b.getAuthors());
0408: assertEquals(1, b.getAuthors().size());
0409: tx.commit();
0410: }
0411:
0412: /**
0413: * only lock object, no changes made
0414: */
0415: public void testChangeMainFields() throws Exception {
0416: String name = "testChangeMainFields_"
0417: + System.currentTimeMillis();
0418: Date date = new Date();
0419: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0420: Book book = new Book(name, date, cover);
0421:
0422: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0423: tx.begin();
0424: database.makePersistent(book);
0425: tx.commit();
0426:
0427: Integer version = book.getVersion();
0428: // System.err.println("### 1. commit, insert new object");
0429:
0430: tx.begin();
0431: tx.lock(book, Transaction.WRITE);
0432: tx.commit();
0433:
0434: // System.err.println("### 2. commit, no changes");
0435: assertEquals(version, book.getVersion());
0436:
0437: tx.begin();
0438: tx.lock(book, Transaction.WRITE);
0439: // we set the same date, so no reason to update
0440: book.setPublicationDate(new Date(date.getTime()));
0441: tx.commit();
0442:
0443: // System.err.println("### 3. commit, replace with same date");
0444: assertEquals(version, book.getVersion());
0445:
0446: tx.begin();
0447: tx.lock(book, Transaction.WRITE);
0448: // now we change the date
0449: Date d = new Date(1111);
0450: book.setPublicationDate(d);
0451: tx.commit();
0452: // System.err.println("### 4. commit, changed date");
0453: assertFalse(date.equals(book.getPublicationDate()));
0454: assertFalse(version.equals(book.getVersion()));
0455: }
0456:
0457: /**
0458: * modify field of main object
0459: */
0460: public void testChangeMainFields_2() throws Exception {
0461: String name = "testChangeMainFields_2_"
0462: + System.currentTimeMillis();
0463: Date date = new Date();
0464: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0465: Book book = new Book(name, date, cover);
0466:
0467: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0468: tx.begin();
0469: database.makePersistent(book);
0470: tx.commit();
0471:
0472: Integer version = book.getVersion();
0473:
0474: tx.begin();
0475: tx.lock(book, Transaction.WRITE);
0476: book.setCover(new byte[] { 2, 3, 4, 5, 6, 7, 8, 8 });
0477: tx.commit();
0478:
0479: assertFalse(version.equals(book.getVersion()));
0480: tx.begin();
0481: tx.getBroker().clearCache();
0482: OQLQuery query = odmg.newOQLQuery();
0483: query.create("select books from " + Book.class.getName()
0484: + " where title like $1");
0485: query.bind(name);
0486: Collection result = (Collection) query.execute();
0487: assertEquals(1, result.size());
0488: Book b = (Book) result.iterator().next();
0489: assertFalse(version.equals(b.getVersion()));
0490: tx.commit();
0491: }
0492:
0493: /**
0494: * lock object and lock serialized unmodified version again
0495: */
0496: public void testChangeMainFields_3() throws Exception {
0497: String name = "testChangeMainFields_3_"
0498: + System.currentTimeMillis();
0499: Date date = new Date();
0500: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0501: Book book = new Book(name, date, cover);
0502:
0503: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0504: tx.begin();
0505: database.makePersistent(book);
0506: tx.commit();
0507:
0508: Integer version = book.getVersion();
0509:
0510: tx.begin();
0511: tx.lock(book, Transaction.WRITE);
0512: book = (Book) SerializationUtils.clone(book);
0513: tx.lock(book, Transaction.WRITE);
0514: tx.commit();
0515:
0516: assertEquals(version, book.getVersion());
0517: }
0518:
0519: /**
0520: * Double lock object with reference
0521: */
0522: public void testChangeOneToOneReference_1() throws Exception {
0523: String name = "testChangeOneToOneReference_1_"
0524: + System.currentTimeMillis();
0525: Date date = new Date();
0526: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0527: Book book = new Book(name, date, cover);
0528: Publisher publisher = new PublisherImpl(name);
0529: book.setPublisher(publisher);
0530:
0531: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0532: tx.begin();
0533: database.makePersistent(book);
0534: tx.commit();
0535: // System.err.println("### 1. commit, insert new object");
0536:
0537: Integer versionBook = book.getVersion();
0538: Integer versionPublisher = book.getPublisher().getVersion();
0539:
0540: tx.begin();
0541: tx.lock(book, Transaction.WRITE);
0542: tx.lock(book, Transaction.WRITE);
0543: tx.commit();
0544: // System.err.println("### 2. commit, double lock call, no changes");
0545:
0546: assertEquals(versionBook, book.getVersion());
0547: assertEquals(versionPublisher, book.getVersion());
0548: }
0549:
0550: /**
0551: * Double lock object with reference and proxy reference
0552: */
0553: public void testChangeOneToOneReference_1b() throws Exception {
0554: String name = "testChangeOneToOneReference_1b_"
0555: + System.currentTimeMillis();
0556: ojbChangeReferenceSetting(Book.class, "publisher", true, NONE,
0557: NONE, true);
0558:
0559: Date date = new Date();
0560: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0561: Book book = new Book(name, date, cover);
0562: Publisher publisher = new PublisherImpl(name);
0563: book.setPublisher(publisher);
0564:
0565: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0566: tx.begin();
0567: database.makePersistent(book);
0568: tx.commit();
0569: // System.err.println("### 1. commit, insert new object");
0570:
0571: Integer versionBook = book.getVersion();
0572: Integer versionPublisher = book.getPublisher().getVersion();
0573:
0574: tx.begin();
0575: tx.lock(book, Transaction.WRITE);
0576: tx.lock(book, Transaction.WRITE);
0577: tx.commit();
0578: // System.err.println("### 2. commit, double lock call, no changes");
0579:
0580: assertEquals(versionBook, book.getVersion());
0581: assertEquals(versionPublisher, book.getVersion());
0582: }
0583:
0584: /**
0585: * lock object with reference and lock serialized version again
0586: */
0587: public void testChangeOneToOneReference_2() throws Exception {
0588: String name = "testChangeOneToOneReference_2_"
0589: + System.currentTimeMillis();
0590: Date date = new Date();
0591: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0592: Book book = new Book(name, date, cover);
0593: Publisher publisher = new PublisherImpl(name);
0594: book.setPublisher(publisher);
0595:
0596: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0597: tx.begin();
0598: database.makePersistent(book);
0599: tx.commit();
0600: // System.err.println("### 1. commit, insert new object");
0601:
0602: Integer versionBook = book.getVersion();
0603: Integer versionPublisher = book.getPublisher().getVersion();
0604:
0605: tx.begin();
0606: tx.lock(book, Transaction.WRITE);
0607: // nothing changed, so no need to update objects
0608: book = (Book) SerializationUtils.clone(book);
0609: tx.lock(book, Transaction.WRITE);
0610: tx.commit();
0611:
0612: assertEquals(versionBook, book.getVersion());
0613: assertEquals(versionPublisher, book.getVersion());
0614: }
0615:
0616: /**
0617: * lock object with reference, change reference only
0618: */
0619: public void testChangeOneToOneReference_3() throws Exception {
0620: String name = "testChangeOneToOneReference_2_"
0621: + System.currentTimeMillis();
0622: Date date = new Date();
0623: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0624: Book book = new Book(name, date, cover);
0625: Publisher publisher = new PublisherImpl(name);
0626: book.setPublisher(publisher);
0627:
0628: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0629: tx.begin();
0630: database.makePersistent(book);
0631: tx.commit();
0632: // System.err.println("### 1. commit, insert new object");
0633:
0634: Integer versionBook = book.getVersion();
0635: Integer versionPublisher = book.getPublisher().getVersion();
0636:
0637: tx.begin();
0638: tx.lock(book, Transaction.WRITE);
0639: // nothing changed, so no need to update objects
0640: book = (Book) SerializationUtils.clone(book);
0641: Publisher p = book.getPublisher();
0642: p.setName(name + "_updated");
0643: // not needed to re-lock, because nothing changed, but
0644: // if we lock Book no update should be done, because nothing changed
0645: tx.lock(book, Transaction.WRITE);
0646: // we have to re-lock the changed objects, because it was serialized
0647: tx.lock(p, Transaction.WRITE);
0648: tx.commit();
0649:
0650: // no changes made in Book
0651: assertEquals(versionBook, book.getVersion());
0652: // publisher should be updated
0653: assertEquals(new Integer(versionPublisher.intValue() + 1), p
0654: .getVersion());
0655: }
0656:
0657: /**
0658: * lock object with reference, replace reference only
0659: */
0660: public void testReplaceOneToOneReference_1() throws Exception {
0661: String name = "testChangeOneToOneReference_2_"
0662: + System.currentTimeMillis();
0663: Date date = new Date();
0664: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0665: Book book = new Book(name, date, cover);
0666: Publisher publisher = new PublisherImpl(name);
0667: book.setPublisher(publisher);
0668:
0669: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0670: tx.begin();
0671: database.makePersistent(book);
0672: tx.commit();
0673: // System.err.println("### 1. commit, insert new object");
0674:
0675: Integer versionBook = book.getVersion();
0676: Integer versionPublisher = book.getPublisher().getVersion();
0677:
0678: tx.begin();
0679: tx.lock(book, Transaction.WRITE);
0680: Publisher p = new PublisherImpl(name + "_new");
0681: // set new Publisher instance
0682: book.setPublisher(p);
0683: tx.lock(p, Transaction.WRITE);
0684: tx.commit();
0685:
0686: // changes made in Book
0687: assertEquals(new Integer(versionBook.intValue() + 1), book
0688: .getVersion());
0689: // publisher should not be updated, because it was replaced
0690: assertEquals(versionPublisher, p.getVersion());
0691: }
0692:
0693: /**
0694: * lock object with reference, replace reference only
0695: */
0696: public void testReplaceOneToOneReference_2() throws Exception {
0697: String name = "testChangeOneToOneReference_2_"
0698: + System.currentTimeMillis();
0699: Date date = new Date();
0700: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0701: Book book = new Book(name, date, cover);
0702: Publisher publisher = new PublisherImpl(name);
0703: book.setPublisher(publisher);
0704:
0705: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0706: tx.begin();
0707: database.makePersistent(book);
0708: tx.commit();
0709: // System.err.println("### 1. commit, insert new object");
0710:
0711: Integer versionBook = book.getVersion();
0712: Integer versionPublisher = book.getPublisher().getVersion();
0713:
0714: tx.begin();
0715: tx.lock(book, Transaction.WRITE);
0716: book = (Book) SerializationUtils.clone(book);
0717: Publisher p = new PublisherImpl(name + "_new");
0718: // set new Publisher instance
0719: book.setPublisher(p);
0720: // not needed to re-lock, because nothing changed, but
0721: // if we lock Book no update should be done, because nothing changed
0722: tx.lock(book, Transaction.WRITE);
0723: // we have to re-lock the changed objects, because it was serialized
0724: tx.lock(p, Transaction.WRITE);
0725: tx.commit();
0726:
0727: // changes made in Book
0728: assertEquals(new Integer(versionBook.intValue() + 1), book
0729: .getVersion());
0730: // publisher should not be updated, because it was replaced
0731: assertEquals(versionPublisher, p.getVersion());
0732: }
0733:
0734: /**
0735: * check materialization of proxy object
0736: */
0737: public void testChangeOneToOneReference_4() throws Exception {
0738: ojbChangeReferenceSetting(Book.class, "publisher", true, NONE,
0739: NONE, true);
0740: String name = "testChangeOneToOneReference_4_"
0741: + System.currentTimeMillis();
0742: Date date = new Date();
0743: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0744: Book book = new Book(name, date, cover);
0745: Publisher publisher = new PublisherImpl(name);
0746: book.setPublisher(publisher);
0747:
0748: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0749: tx.begin();
0750: database.makePersistent(book);
0751: tx.commit();
0752:
0753: Integer versionBook = book.getVersion();
0754:
0755: tx.begin();
0756: tx.getBroker().clearCache();
0757: OQLQuery query = odmg.newOQLQuery();
0758: query.create("select books from " + Book.class.getName()
0759: + " where title like $1");
0760: query.bind(name);
0761: Collection result = (Collection) query.execute();
0762: assertEquals(1, result.size());
0763: Book b = (Book) result.iterator().next();
0764: IndirectionHandler handler = ProxyHelper
0765: .getIndirectionHandler(b.getPublisher());
0766: assertNotNull(handler);
0767: assertFalse(handler.alreadyMaterialized());
0768: handler.addListener(new MaterializationListener() {
0769: public void beforeMaterialization(
0770: IndirectionHandler handler, Identity oid) {
0771: fail("Reference shall not materialize while locking");
0772: }
0773:
0774: public void afterMaterialization(
0775: IndirectionHandler handler,
0776: Object materializedObject) {
0777: }
0778: });
0779: tx.lock(b, Transaction.WRITE);
0780: tx.commit();
0781:
0782: assertEquals(versionBook, b.getVersion());
0783: }
0784:
0785: /**
0786: * replace proxy reference by new reference object
0787: */
0788: public void testChangeOneToOneReference_5() throws Exception {
0789: ojbChangeReferenceSetting(Book.class, "publisher", true, NONE,
0790: NONE, true);
0791: String name = "testChangeOneToOneReference_5_"
0792: + System.currentTimeMillis();
0793: Date date = new Date();
0794: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0795: Book book = new Book(name, date, cover);
0796: Publisher publisher = new PublisherImpl(name);
0797: book.setPublisher(publisher);
0798:
0799: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0800: tx.begin();
0801: database.makePersistent(book);
0802: tx.commit();
0803: // System.err.println("### 1. commit, insert new object");
0804:
0805: Integer versionBook = book.getVersion();
0806:
0807: tx.begin();
0808: tx.getBroker().clearCache();
0809: OQLQuery query = odmg.newOQLQuery();
0810: query.create("select books from " + Book.class.getName()
0811: + " where title like $1");
0812: query.bind(name);
0813: Collection result = (Collection) query.execute();
0814: assertEquals(1, result.size());
0815: Book b = (Book) result.iterator().next();
0816: IndirectionHandler handler = ProxyHelper
0817: .getIndirectionHandler(b.getPublisher());
0818: assertNotNull(handler);
0819: assertFalse(handler.alreadyMaterialized());
0820: handler.addListener(new MaterializationListener() {
0821: public void beforeMaterialization(
0822: IndirectionHandler handler, Identity oid) {
0823: fail("Reference shall not materialize while locking");
0824: }
0825:
0826: public void afterMaterialization(
0827: IndirectionHandler handler,
0828: Object materializedObject) {
0829: }
0830: });
0831: // no need to lock with default settings, because lock is done when query object
0832: tx.lock(b, Transaction.WRITE);
0833: // replace 1:1 reference
0834: Publisher p = new PublisherImpl(name + "_new");
0835: b.setPublisher(p);
0836: //tx.lock(p, Transaction.WRITE);
0837: tx.commit();
0838:
0839: // we expect increased version, because Book object needs update - changed FK
0840: assertEquals(new Integer(versionBook.intValue() + 1), b
0841: .getVersion());
0842: // should should differ, because new reference should be stored
0843: assertFalse(publisher.getName().equals(p.getName()));
0844: assertNotNull(p.getVersion());
0845:
0846: tx.begin();
0847: tx.getBroker().clearCache();
0848: query = odmg.newOQLQuery();
0849: query.create("select books from " + Book.class.getName()
0850: + " where title like $1");
0851: query.bind(name);
0852: result = (Collection) query.execute();
0853: tx.commit();
0854: assertEquals(1, result.size());
0855: b = (Book) result.iterator().next();
0856: Publisher newP = b.getPublisher();
0857: assertNotNull(newP);
0858: assertEquals(name + "_new", newP.getName());
0859: }
0860:
0861: /**
0862: * update referenced object
0863: */
0864: public void testChangeOneToOneReference_6() throws Exception {
0865: String name = "testChangeOneToOneReference_6_"
0866: + System.currentTimeMillis();
0867: Date date = new Date();
0868: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0869: Book book = new Book(name, date, cover);
0870: Publisher publisher = new PublisherImpl(name);
0871: book.setPublisher(publisher);
0872:
0873: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0874: tx.begin();
0875: database.makePersistent(book);
0876: tx.commit();
0877:
0878: Integer versionBook = book.getVersion();
0879: Integer versionPublisher = book.getPublisher().getVersion();
0880:
0881: tx.begin();
0882: tx.getBroker().clearCache();
0883: OQLQuery query = odmg.newOQLQuery();
0884: query.create("select books from " + Book.class.getName()
0885: + " where title like $1");
0886: query.bind(name);
0887: Collection result = (Collection) query.execute();
0888: assertEquals(1, result.size());
0889: Book b = (Book) result.iterator().next();
0890: tx.lock(b, Transaction.WRITE);
0891: b.getPublisher().setName(
0892: "updated_" + b.getPublisher().getName());
0893: tx.commit();
0894:
0895: // nothing changed
0896: assertEquals(versionBook, b.getVersion());
0897: // version should should differ, because reference should be updated
0898: assertFalse(versionPublisher.equals(b.getPublisher()
0899: .getVersion()));
0900: assertEquals("updated_" + name, b.getPublisher().getName());
0901: }
0902:
0903: /**
0904: * add new reference
0905: */
0906: public void testChangeOneToOneReference_7() throws Exception {
0907: String name = "testChangeOneToOneReference_7_"
0908: + System.currentTimeMillis();
0909: Date date = new Date();
0910: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0911: Book book = new Book(name, date, cover);
0912:
0913: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0914: tx.begin();
0915: database.makePersistent(book);
0916: tx.commit();
0917:
0918: Integer versionBook = book.getVersion();
0919:
0920: tx.begin();
0921: tx.getBroker().clearCache();
0922: OQLQuery query = odmg.newOQLQuery();
0923: query.create("select books from " + Book.class.getName()
0924: + " where title like $1");
0925: query.bind(name);
0926: Collection result = (Collection) query.execute();
0927: assertEquals(1, result.size());
0928: Book b = (Book) result.iterator().next();
0929:
0930: tx.lock(b, Transaction.WRITE);
0931: assertNull(b.getPublisher());
0932: Publisher publisher = new PublisherImpl(name);
0933: b.setPublisher(publisher);
0934: // tx.lock(publisher, Transaction.WRITE);
0935: tx.commit();
0936:
0937: tx.begin();
0938: tx.getBroker().clearCache();
0939: query = odmg.newOQLQuery();
0940: query.create("select books from " + Book.class.getName()
0941: + " where title like $1");
0942: query.bind(name);
0943: result = (Collection) query.execute();
0944: assertEquals(1, result.size());
0945: b = (Book) result.iterator().next();
0946: tx.commit();
0947:
0948: // we expect increased version, because Book object needs update
0949: assertEquals(new Integer(versionBook.intValue() + 1), b
0950: .getVersion());
0951: // version should differ from null, because new reference should be stored
0952: assertNotNull(b.getPublisher());
0953: assertNotNull(b.getPublisher().getVersion());
0954: assertEquals(name, b.getPublisher().getName());
0955: }
0956:
0957: /**
0958: * remove reference
0959: */
0960: public void testChangeOneToOneReference_8() throws Exception {
0961: String name = "testChangeOneToOneReference_8_"
0962: + System.currentTimeMillis();
0963: Date date = new Date();
0964: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
0965: Book book = new Book(name, date, cover);
0966: Publisher publisher = new PublisherImpl(name);
0967: book.setPublisher(publisher);
0968:
0969: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0970: tx.begin();
0971: database.makePersistent(book);
0972: tx.commit();
0973:
0974: Integer versionBook = book.getVersion();
0975: Integer versionPublisher = book.getPublisher().getVersion();
0976:
0977: tx.begin();
0978: tx.getBroker().clearCache();
0979: OQLQuery query = odmg.newOQLQuery();
0980: query.create("select books from " + Book.class.getName()
0981: + " where title like $1");
0982: query.bind(name);
0983: Collection result = (Collection) query.execute();
0984: assertEquals(1, result.size());
0985: Book b = (Book) result.iterator().next();
0986:
0987: tx.lock(b, Transaction.WRITE);
0988: b.setPublisher(null);
0989: tx.commit();
0990:
0991: tx.begin();
0992: tx.getBroker().clearCache();
0993: query = odmg.newOQLQuery();
0994: query.create("select books from " + Book.class.getName()
0995: + " where title like $1");
0996: query.bind(name);
0997: result = (Collection) query.execute();
0998: assertEquals(1, result.size());
0999: b = (Book) result.iterator().next();
1000: tx.commit();
1001:
1002: // we expect increased version, because Book object needs update
1003: assertEquals(new Integer(versionBook.intValue() + 1), b
1004: .getVersion());
1005: assertNull(b.getPublisher());
1006:
1007: tx.begin();
1008: tx.getBroker().clearCache();
1009: query = odmg.newOQLQuery();
1010: query.create("select publishers from "
1011: + Publisher.class.getName() + " where name like $1");
1012: query.bind(name);
1013: result = (Collection) query.execute();
1014: // we don't remove the reference object
1015: assertEquals(1, result.size());
1016: Publisher p = (Publisher) result.iterator().next();
1017: assertEquals(name, p.getName());
1018: // removed 1:1 reference, expect unchanged version
1019: assertEquals(versionPublisher, p.getVersion());
1020: tx.commit();
1021: }
1022:
1023: /**
1024: * delete reference
1025: */
1026: public void testChangeOneToOneReference_8b() throws Exception {
1027: String name = "testChangeOneToOneReference_8b_"
1028: + System.currentTimeMillis();
1029: Date date = new Date();
1030: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
1031: Book book = new Book(name, date, cover);
1032: Publisher publisher = new PublisherImpl(name);
1033: book.setPublisher(publisher);
1034:
1035: TransactionExt tx = (TransactionExt) odmg.newTransaction();
1036: tx.begin();
1037: database.makePersistent(book);
1038: tx.commit();
1039:
1040: Integer versionBook = book.getVersion();
1041:
1042: tx.begin();
1043: tx.getBroker().clearCache();
1044: OQLQuery query = odmg.newOQLQuery();
1045: query.create("select books from " + Book.class.getName()
1046: + " where title like $1");
1047: query.bind(name);
1048: Collection result = (Collection) query.execute();
1049: assertEquals(1, result.size());
1050: Book b = (Book) result.iterator().next();
1051:
1052: tx.lock(b, Transaction.WRITE);
1053: database.deletePersistent(b.getPublisher());
1054: b.setPublisher(null);
1055: tx.commit();
1056:
1057: tx.begin();
1058: tx.getBroker().clearCache();
1059: query = odmg.newOQLQuery();
1060: query.create("select books from " + Book.class.getName()
1061: + " where title like $1");
1062: query.bind(name);
1063: result = (Collection) query.execute();
1064: assertEquals(1, result.size());
1065: b = (Book) result.iterator().next();
1066: tx.commit();
1067:
1068: // we expect increased version, because Book object needs update
1069: assertEquals(new Integer(versionBook.intValue() + 1), b
1070: .getVersion());
1071: assertNull(b.getPublisher());
1072:
1073: tx.begin();
1074: tx.getBroker().clearCache();
1075: query = odmg.newOQLQuery();
1076: query.create("select publishers from "
1077: + Publisher.class.getName() + " where name like $1");
1078: query.bind(name);
1079: result = (Collection) query.execute();
1080: // we don't remove the reference object
1081: assertEquals(0, result.size());
1082: tx.commit();
1083: }
1084:
1085: /**
1086: * check materialzation of collection reference
1087: */
1088: public void testCollectionReference_1() throws Exception {
1089: String name = "testCollectionReference_1_"
1090: + System.currentTimeMillis();
1091: ojbChangeReferenceSetting(Book.class, "reviews", true, NONE,
1092: NONE, true);
1093:
1094: Date date = new Date();
1095: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
1096: Book book = new Book(name, date, cover);
1097: Review r1 = new Review(name);
1098: Review r2 = new Review(name);
1099: Review r3 = new Review(name);
1100: ArrayList reviews = new ArrayList();
1101: reviews.add(r1);
1102: reviews.add(r2);
1103: reviews.add(r3);
1104: book.setReviews(reviews);
1105:
1106: TransactionExt tx = (TransactionExt) odmg.newTransaction();
1107: tx.begin();
1108: database.makePersistent(book);
1109: tx.commit();
1110: // System.err.println("## Insert main object with 3 referecnes");
1111:
1112: tx.begin();
1113: tx.getBroker().clearCache();
1114: OQLQuery query = odmg.newOQLQuery();
1115: query.create("select books from " + Book.class.getName()
1116: + " where title like $1");
1117: query.bind(name);
1118: Collection result = (Collection) query.execute();
1119: assertEquals(1, result.size());
1120: Book b = (Book) result.iterator().next();
1121: CollectionProxy handler = ProxyHelper.getCollectionProxy(b
1122: .getReviews());
1123: assertFalse("Don't expect an materialized collection proxy",
1124: handler.isLoaded());
1125: handler.addListener(new CollectionProxyListener() {
1126: public void beforeLoading(
1127: CollectionProxyDefaultImpl colProxy) {
1128: fail("Collection proxy shouldn't be materialized");
1129: }
1130:
1131: public void afterLoading(CollectionProxyDefaultImpl colProxy) {
1132: }
1133: });
1134: assertNotNull(b.getReviews());
1135: assertEquals(3, b.getReviews().size());
1136:
1137: tx.lock(b, Transaction.WRITE);
1138: tx.commit();
1139: }
1140:
1141: /**
1142: * update collection reference object
1143: */
1144: public void testCollectionReference_2a() throws Exception {
1145: String name = "testCollectionReference_2a_"
1146: + System.currentTimeMillis();
1147: Date date = new Date();
1148: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
1149: Book book = new Book(name, date, cover);
1150: Review r1 = new Review(name);
1151: Review r2 = new Review(name);
1152: Review r3 = new Review(name);
1153: ArrayList reviews = new ArrayList();
1154: reviews.add(r1);
1155: reviews.add(r2);
1156: reviews.add(r3);
1157: book.setReviews(reviews);
1158:
1159: TransactionExt tx = (TransactionExt) odmg.newTransaction();
1160: tx.begin();
1161: database.makePersistent(book);
1162: tx.commit();
1163: // System.err.println("## Insert main object with 3 referecnes");
1164:
1165: Integer versionBook = book.getVersion();
1166: Integer versionR1 = ((Review) book.getReviews().get(0))
1167: .getVersion();
1168:
1169: tx.begin();
1170: tx.getBroker().clearCache();
1171: OQLQuery query = odmg.newOQLQuery();
1172: query.create("select books from " + Book.class.getName()
1173: + " where title like $1");
1174: query.bind(name);
1175: Collection result = (Collection) query.execute();
1176: assertEquals(1, result.size());
1177: Book b = (Book) result.iterator().next();
1178: assertEquals(versionBook, b.getVersion());
1179: assertNotNull(b.getReviews());
1180: assertEquals(3, b.getReviews().size());
1181:
1182: tx.lock(b, Transaction.WRITE);
1183: Review newR2 = (Review) b.getReviews().get(1);
1184: newR2.setSummary("updated" + name);
1185: final int newR2id = newR2.getId().intValue();
1186: tx.commit();
1187:
1188: assertEquals(versionBook, b.getVersion());
1189: // this referenced object was not updated
1190: Integer versionR1New = ((Review) b.getReviews().get(0))
1191: .getVersion();
1192: assertEquals(versionR1, versionR1New);
1193:
1194: tx.begin();
1195: tx.getBroker().clearCache();
1196: query = odmg.newOQLQuery();
1197: query.create("select books from " + Book.class.getName()
1198: + " where title like $1");
1199: query.bind(name);
1200: result = (Collection) query.execute();
1201: assertEquals(1, result.size());
1202: b = (Book) result.iterator().next();
1203: assertNotNull(b.getReviews());
1204: assertEquals(3, b.getReviews().size());
1205:
1206: // Search for the updated R2:
1207: final List updatedReviews = b.getReviews();
1208: for (int i = 0; i < updatedReviews.size(); i++) {
1209: newR2 = (Review) updatedReviews.get(i);
1210: if (newR2id == newR2.getId().intValue()) {
1211: break;
1212: }
1213: }
1214: assertEquals(
1215: "Could not find the updated review in the returned results",
1216: newR2id, newR2.getId().intValue());
1217: assertEquals("updated" + name, newR2.getSummary());
1218: assertEquals(versionBook, b.getVersion());
1219: // this referenced object was not updated
1220: versionR1New = ((Review) b.getReviews().get(0)).getVersion();
1221: assertEquals(versionR1, versionR1New);
1222: }
1223:
1224: /**
1225: * update proxy collection reference object
1226: */
1227: public void testCollectionReference_2b() throws Exception {
1228: String name = "testCollectionReference_2b_"
1229: + System.currentTimeMillis();
1230:
1231: ojbChangeReferenceSetting(Book.class, "reviews", true, NONE,
1232: NONE, true);
1233:
1234: Date date = new Date();
1235: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
1236: Book book = new Book(name, date, cover);
1237: Review r1 = new Review(name);
1238: Review r2 = new Review(name);
1239: Review r3 = new Review(name);
1240: ArrayList reviews = new ArrayList();
1241: reviews.add(r1);
1242: reviews.add(r2);
1243: reviews.add(r3);
1244: book.setReviews(reviews);
1245:
1246: TransactionExt tx = (TransactionExt) odmg.newTransaction();
1247: tx.begin();
1248: database.makePersistent(book);
1249: tx.commit();
1250: // System.err.println("## Insert main object with 3 referecnes");
1251:
1252: Integer versionBook = book.getVersion();
1253: Integer versionR0 = ((Review) book.getReviews().get(0))
1254: .getVersion();
1255: Integer versionR1 = ((Review) book.getReviews().get(1))
1256: .getVersion();
1257:
1258: tx.begin();
1259: tx.getBroker().clearCache();
1260: // System.err.println("## Started new tx");
1261: OQLQuery query = odmg.newOQLQuery();
1262: query.create("select books from " + Book.class.getName()
1263: + " where title like $1");
1264: query.bind(name);
1265: Collection result = (Collection) query.execute();
1266: assertEquals(1, result.size());
1267: Book b = (Book) result.iterator().next();
1268: assertEquals(versionBook, b.getVersion());
1269: assertNotNull(b.getReviews());
1270: assertEquals(3, b.getReviews().size());
1271: assertEquals(versionR0, ((Review) b.getReviews().get(0))
1272: .getVersion());
1273: assertEquals(versionR1, ((Review) b.getReviews().get(1))
1274: .getVersion());
1275:
1276: // System.err.println("## Query done");
1277: tx.lock(b, Transaction.WRITE);
1278: // System.err.println("## Lock object again");
1279: Review newR1 = (Review) b.getReviews().get(1);
1280: newR1.setSummary("updated" + name);
1281: // System.err.println("## Before commit");
1282: tx.commit();
1283: // System.err.println("## Commit Book with updated Review");
1284:
1285: assertEquals(versionBook, b.getVersion());
1286: assertEquals(3, b.getReviews().size());
1287:
1288: // the updated one
1289: Integer versionR1New = ((Review) b.getReviews().get(1))
1290: .getVersion();
1291: assertEquals(new Integer(versionR1.intValue() + 1),
1292: versionR1New);
1293: // this referenced object was not updated
1294: Integer versionR0New = ((Review) b.getReviews().get(0))
1295: .getVersion();
1296: assertEquals(versionR0, versionR0New);
1297:
1298: tx.begin();
1299: tx.getBroker().clearCache();
1300: query = odmg.newOQLQuery();
1301: query.create("select books from " + Book.class.getName()
1302: + " where title like $1");
1303: query.bind(name);
1304: result = (Collection) query.execute();
1305: assertEquals(1, result.size());
1306: b = (Book) result.iterator().next();
1307: assertNotNull(b.getReviews());
1308: assertEquals(3, b.getReviews().size());
1309: tx.commit();
1310:
1311: // query for updated Review
1312: tx.begin();
1313: tx.getBroker().clearCache();
1314: query = odmg.newOQLQuery();
1315: query.create("select reviews from " + Review.class.getName()
1316: + " where summary like $1");
1317: query.bind("updated" + name);
1318: result = (Collection) query.execute();
1319: tx.commit();
1320: // the update Review object
1321: assertEquals(1, result.size());
1322: // query for unchanged Review objects
1323: tx.begin();
1324: tx.getBroker().clearCache();
1325: query = odmg.newOQLQuery();
1326: query.create("select reviews from " + Review.class.getName()
1327: + " where summary like $1");
1328: query.bind(name);
1329: result = (Collection) query.execute();
1330: tx.commit();
1331:
1332: assertEquals(2, result.size());
1333: assertEquals(versionR0, ((Review) new ArrayList(result).get(0))
1334: .getVersion());
1335:
1336: }
1337:
1338: /**
1339: * remove collection reference object
1340: * this test expects that removed 1:n referenced objects only
1341: * be "unlinked" instead of deleted.
1342: */
1343: public void testCollectionReference_3() throws Exception {
1344: String name = "testCollectionReference_3_"
1345: + System.currentTimeMillis();
1346: Date date = new Date();
1347: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
1348: Book book = new Book(name, date, cover);
1349: Review r1 = new Review(name);
1350: ArrayList reviews = new ArrayList();
1351: reviews.add(r1);
1352: book.setReviews(reviews);
1353:
1354: TransactionExt tx = (TransactionExt) odmg.newTransaction();
1355: tx.begin();
1356: database.makePersistent(book);
1357: tx.commit();
1358:
1359: Integer versionBook = book.getVersion();
1360: Integer versionR1 = ((Review) book.getReviews().get(0))
1361: .getVersion();
1362:
1363: tx.begin();
1364: tx.getBroker().clearCache();
1365: OQLQuery query = odmg.newOQLQuery();
1366: query.create("select books from " + Book.class.getName()
1367: + " where title like $1");
1368: query.bind(name);
1369: Collection result = (Collection) query.execute();
1370: assertEquals(1, result.size());
1371: Book b = (Book) result.iterator().next();
1372: assertEquals(versionBook, b.getVersion());
1373: assertNotNull(b.getReviews());
1374: assertEquals(1, b.getReviews().size());
1375:
1376: //***********************************
1377: tx.lock(b, Transaction.WRITE);
1378: // remove from collection, but do not explicit delete
1379: tx.setCascadingDelete(Book.class, "reviews", false);
1380: Review newR1 = (Review) b.getReviews().remove(0);
1381: tx.commit();
1382: //***********************************
1383:
1384: // only the removed reference has changed
1385: assertEquals(versionBook, b.getVersion());
1386: // expect an "unlinked" new version
1387: assertEquals(new Integer(versionR1.intValue() + 1), newR1
1388: .getVersion());
1389:
1390: tx.begin();
1391: tx.getBroker().clearCache();
1392: query = odmg.newOQLQuery();
1393: query.create("select reviews from " + Review.class.getName()
1394: + " where summary like $1");
1395: query.bind(name);
1396: result = (Collection) query.execute();
1397: tx.commit();
1398:
1399: // we don't delete the Review object, only remove from reference collection
1400: assertEquals(1, result.size());
1401: Review r = (Review) result.iterator().next();
1402: // expect new object version, because the FK to main object was set null
1403: assertEquals(new Integer(versionR1.intValue() + 1), r
1404: .getVersion());
1405:
1406: tx.begin();
1407: query = odmg.newOQLQuery();
1408: query.create("select books from " + Book.class.getName()
1409: + " where title like $1");
1410: query.bind(name);
1411: result = (Collection) query.execute();
1412: tx.commit();
1413:
1414: assertEquals(1, result.size());
1415: b = (Book) result.iterator().next();
1416: assertEquals(versionBook, b.getVersion());
1417: // we have removed the Review object
1418: assertEquals(0, b.getReviews().size());
1419: }
1420:
1421: /**
1422: * remove collection reference object and explicit delete it
1423: */
1424: public void testCollectionReference_3b() throws Exception {
1425: String name = "testCollectionReference_3b_"
1426: + System.currentTimeMillis();
1427: Date date = new Date();
1428: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
1429: Book book = new Book(name, date, cover);
1430: Review r1 = new Review(name);
1431: ArrayList reviews = new ArrayList();
1432: reviews.add(r1);
1433: book.setReviews(reviews);
1434:
1435: TransactionExt tx = (TransactionExt) odmg.newTransaction();
1436: tx.begin();
1437: database.makePersistent(book);
1438: tx.commit();
1439:
1440: Integer versionBook = book.getVersion();
1441:
1442: tx.begin();
1443: tx.getBroker().clearCache();
1444: OQLQuery query = odmg.newOQLQuery();
1445: query.create("select books from " + Book.class.getName()
1446: + " where title like $1");
1447: query.bind(name);
1448: Collection result = (Collection) query.execute();
1449: assertEquals(1, result.size());
1450: Book b = (Book) result.iterator().next();
1451: assertEquals(versionBook, b.getVersion());
1452: assertNotNull(b.getReviews());
1453: assertEquals(1, b.getReviews().size());
1454:
1455: tx.lock(b, Transaction.WRITE);
1456: // remove from collection and delete
1457: Review newR1 = (Review) b.getReviews().remove(0);
1458: database.deletePersistent(newR1);
1459: tx.commit();
1460:
1461: // only the removed reference has changed
1462: assertEquals(versionBook, b.getVersion());
1463:
1464: tx.begin();
1465: query = odmg.newOQLQuery();
1466: query.create("select reviews from " + Review.class.getName()
1467: + " where summary like $1");
1468: query.bind(name);
1469: result = (Collection) query.execute();
1470: tx.commit();
1471:
1472: assertEquals(0, result.size());
1473:
1474: tx.begin();
1475: query = odmg.newOQLQuery();
1476: query.create("select books from " + Book.class.getName()
1477: + " where title like $1");
1478: query.bind(name);
1479: result = (Collection) query.execute();
1480: tx.commit();
1481:
1482: assertEquals(1, result.size());
1483: b = (Book) result.iterator().next();
1484: assertEquals(versionBook, b.getVersion());
1485: assertEquals(0, b.getReviews().size());
1486: }
1487:
1488: /**
1489: * remove collection reference object with enabled auto-delete
1490: */
1491: public void testCollectionReference_3c() throws Exception {
1492: String name = "testCollectionReference_3c_"
1493: + System.currentTimeMillis();
1494: Date date = new Date();
1495: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
1496: Book book = new Book(name, date, cover);
1497: Review r1 = new Review(name);
1498: ArrayList reviews = new ArrayList();
1499: reviews.add(r1);
1500: book.setReviews(reviews);
1501:
1502: TransactionExt tx = (TransactionExt) odmg.newTransaction();
1503: tx.begin();
1504: database.makePersistent(book);
1505: tx.commit();
1506:
1507: Integer versionBook = book.getVersion();
1508:
1509: tx.begin();
1510: tx.getBroker().clearCache();
1511: OQLQuery query = odmg.newOQLQuery();
1512: query.create("select books from " + Book.class.getName()
1513: + " where title like $1");
1514: query.bind(name);
1515: Collection result = (Collection) query.execute();
1516: assertEquals(1, result.size());
1517: Book b = (Book) result.iterator().next();
1518: assertEquals(versionBook, b.getVersion());
1519: assertNotNull(b.getReviews());
1520: assertEquals(1, b.getReviews().size());
1521:
1522: tx.lock(b, Transaction.WRITE);
1523: tx.setCascadingDelete(Book.class, "reviews", true);
1524: // remove from collection with cascading delete
1525: b.getReviews().remove(0);
1526: tx.commit();
1527:
1528: // only the removed reference has changed
1529: assertEquals(versionBook, b.getVersion());
1530:
1531: tx.begin();
1532: tx.getBroker().clearCache();
1533: query = odmg.newOQLQuery();
1534: query.create("select reviews from " + Review.class.getName()
1535: + " where summary like $1");
1536: query.bind(name);
1537: result = (Collection) query.execute();
1538: tx.commit();
1539:
1540: // cascading delete was used, so we don't expect an unlinked
1541: // version of the Review class
1542: assertEquals(0, result.size());
1543:
1544: tx.begin();
1545: query = odmg.newOQLQuery();
1546: query.create("select books from " + Book.class.getName()
1547: + " where title like $1");
1548: query.bind(name);
1549: result = (Collection) query.execute();
1550: tx.commit();
1551:
1552: assertEquals(1, result.size());
1553: b = (Book) result.iterator().next();
1554: assertEquals(versionBook, b.getVersion());
1555: // we have removed the Review object
1556: assertEquals(0, b.getReviews().size());
1557: }
1558:
1559: /**
1560: * delete Book object with existing references
1561: */
1562: public void testCollectionReference_4a() throws Exception {
1563: String name = "testCollectionReference_4_"
1564: + System.currentTimeMillis();
1565: Date date = new Date();
1566: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
1567: Book book = new Book(name, date, cover);
1568: Review r1 = new Review(name);
1569: Review r2 = new Review(name);
1570: Review r3 = new Review(name);
1571: Author a1 = new Author(name, null);
1572: Author a2 = new Author(name, null);
1573: r1.setAuthor(a1);
1574: r2.setAuthor(a1);
1575: r3.setAuthor(a2);
1576: ArrayList reviews = new ArrayList();
1577: reviews.add(r1);
1578: reviews.add(r2);
1579: reviews.add(r3);
1580: book.setReviews(reviews);
1581:
1582: TransactionExt tx = (TransactionExt) odmg.newTransaction();
1583: tx.begin();
1584: database.makePersistent(book);
1585: tx.commit();
1586:
1587: Integer versionBook = book.getVersion();
1588:
1589: tx.begin();
1590: tx.getBroker().clearCache();
1591: OQLQuery query = odmg.newOQLQuery();
1592: query.create("select books from " + Book.class.getName()
1593: + " where title like $1");
1594: query.bind(name);
1595: Collection result = (Collection) query.execute();
1596: assertEquals(1, result.size());
1597: Book b = (Book) result.iterator().next();
1598: assertEquals(versionBook, b.getVersion());
1599: assertNotNull(b.getReviews());
1600: assertEquals(3, b.getReviews().size());
1601: assertNotNull(((Review) b.getReviews().get(0)).getAuthor());
1602: assertNotNull(((Review) b.getReviews().get(1)).getAuthor());
1603: assertNotNull(((Review) b.getReviews().get(2)).getAuthor());
1604:
1605: // Book instance should be already locked
1606: // now mark Book for delete and disable cascading delete
1607: // for the 1:n relation to Review class
1608: tx.setCascadingDelete(Book.class, "reviews", false);
1609: database.deletePersistent(b);
1610: tx.commit();
1611: // System.out.println("## After commit");
1612:
1613: tx.begin();
1614: tx.getBroker().clearCache();
1615: query = odmg.newOQLQuery();
1616: query.create("select reviews from " + Review.class.getName()
1617: + " where summary like $1");
1618: query.bind(name);
1619: result = (Collection) query.execute();
1620: tx.commit();
1621: // we auto-delete the Review object, only remove from reference collection
1622: assertEquals(3, result.size());
1623: List list = new ArrayList(result);
1624: assertNotNull(((Review) list.get(0)).getAuthor());
1625: assertNotNull(((Review) list.get(1)).getAuthor());
1626: assertNotNull(((Review) list.get(2)).getAuthor());
1627: Review newR1 = (Review) list.get(0);
1628: // book was deleted so we expect unlink of FK
1629: assertNull(newR1.getFkBook());
1630:
1631: tx.begin();
1632: query = odmg.newOQLQuery();
1633: query.create("select books from " + Book.class.getName()
1634: + " where title like $1");
1635: query.bind(name);
1636: result = (Collection) query.execute();
1637: tx.commit();
1638: assertEquals(0, result.size());
1639: }
1640:
1641: /**
1642: * delete Book object with existing references and circular referencing objects
1643: */
1644: public void testCollectionReference_4b() throws Exception {
1645: String name = "testCollectionReference_4_"
1646: + System.currentTimeMillis();
1647: Date date = new Date();
1648: byte[] cover = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
1649: Book book = new Book(name, date, cover);
1650: Review r1 = new Review(name);
1651: Review r2 = new Review(name);
1652: Review r3 = new Review(name);
1653: Author a1 = new Author(name, null);
1654: Author a2 = new Author(name, null);
1655: r1.setAuthor(a1);
1656: r2.setAuthor(a1);
1657: r3.setAuthor(a2);
1658: ArrayList reviews = new ArrayList();
1659: reviews.add(r1);
1660: reviews.add(r2);
1661: reviews.add(r3);
1662: book.setReviews(reviews);
1663: ArrayList authors = new ArrayList();
1664: authors.add(a1);
1665: authors.add(a2);
1666: book.setAuthors(authors);
1667:
1668: TransactionExt tx = (TransactionExt) odmg.newTransaction();
1669: tx.begin();
1670: database.makePersistent(book);
1671: tx.commit();
1672:
1673: Integer versionBook = book.getVersion();
1674:
1675: tx.begin();
1676: tx.getBroker().clearCache();
1677: OQLQuery query = odmg.newOQLQuery();
1678: query.create("select books from " + Book.class.getName()
1679: + " where title like $1");
1680: query.bind(name);
1681: Collection result = (Collection) query.execute();
1682: assertEquals(1, result.size());
1683: Book b = (Book) result.iterator().next();
1684: assertEquals(versionBook, b.getVersion());
1685: assertNotNull(b.getReviews());
1686: assertEquals(3, b.getReviews().size());
1687: assertNotNull(((Review) b.getReviews().get(0)).getAuthor());
1688: assertNotNull(((Review) b.getReviews().get(1)).getAuthor());
1689: assertNotNull(((Review) b.getReviews().get(2)).getAuthor());
1690: assertNotNull(b.getAuthors());
1691: assertEquals(2, b.getAuthors().size());
1692: Author newA = (Author) b.getAuthors().get(0);
1693: boolean failed = true;
1694: for (Iterator iterator = b.getReviews().iterator(); iterator
1695: .hasNext();) {
1696: Review review = (Review) iterator.next();
1697: if (newA.equals(review.getAuthor())) {
1698: // as we have circular references we expect the same object instance
1699: assertSame(newA, review.getAuthor());
1700: failed = false;
1701: }
1702: }
1703: if (failed)
1704: fail("Expect the same object instance, but not found for "
1705: + newA);
1706:
1707: // Book instance should be already locked
1708: // now mark Book for delete and disable cascading delete
1709: // for the 1:n relation to Review class
1710: tx.setCascadingDelete(Book.class, "reviews", false);
1711: database.deletePersistent(b);
1712: tx.commit();
1713:
1714: tx.begin();
1715: tx.getBroker().clearCache();
1716: query = odmg.newOQLQuery();
1717: query.create("select reviews from " + Review.class.getName()
1718: + " where summary like $1");
1719: query.bind(name);
1720: result = (Collection) query.execute();
1721: tx.commit();
1722: // we auto-delete the Review object, only remove from reference collection
1723: assertEquals(3, result.size());
1724: List list = new ArrayList(result);
1725: assertNotNull(((Review) list.get(0)).getAuthor());
1726: assertNotNull(((Review) list.get(1)).getAuthor());
1727: assertNotNull(((Review) list.get(2)).getAuthor());
1728: Review newR1 = (Review) list.get(0);
1729: // book was deleted so we expect unlink of FK
1730: assertNull(newR1.getFkBook());
1731: newA = ((Review) list.get(0)).getAuthor();
1732: assertEquals(0, newA.getBooks().size());
1733:
1734: tx.begin();
1735: query = odmg.newOQLQuery();
1736: query.create("select books from " + Book.class.getName()
1737: + " where title like $1");
1738: query.bind(name);
1739: result = (Collection) query.execute();
1740: tx.commit();
1741: assertEquals(0, result.size());
1742: }
1743:
1744: //=======================================================
1745: // inner test classes
1746: //=======================================================
1747: public static final class Book implements Serializable {
1748: private Integer id;
1749: private String title;
1750: private Date publicationDate;
1751: private byte[] cover;
1752: private Integer version;
1753:
1754: private List authors;
1755: private List reviews;
1756: private Publisher publisher;
1757:
1758: public Book() {
1759: }
1760:
1761: public Book(String title, Date publicationDate, byte[] cover) {
1762: this .title = title;
1763: this .publicationDate = publicationDate;
1764: this .cover = cover;
1765: }
1766:
1767: public void addAuthor(Author author) {
1768: if (authors == null) {
1769: authors = new ArrayList();
1770: }
1771: authors.add(author);
1772: }
1773:
1774: public void addReview(Review review) {
1775: if (reviews == null) {
1776: reviews = new ArrayList();
1777: }
1778: reviews.add(review);
1779: }
1780:
1781: public boolean removeReview(Review review) {
1782: if (reviews != null)
1783: return reviews.remove(review);
1784: else
1785: return false;
1786: }
1787:
1788: public Integer getId() {
1789: return id;
1790: }
1791:
1792: public void setId(Integer id) {
1793: this .id = id;
1794: }
1795:
1796: public String getTitle() {
1797: return title;
1798: }
1799:
1800: public void setTitle(String title) {
1801: this .title = title;
1802: }
1803:
1804: public Date getPublicationDate() {
1805: return publicationDate;
1806: }
1807:
1808: public void setPublicationDate(Date publicationDate) {
1809: this .publicationDate = publicationDate;
1810: }
1811:
1812: public byte[] getCover() {
1813: return cover;
1814: }
1815:
1816: public void setCover(byte[] cover) {
1817: this .cover = cover;
1818: }
1819:
1820: public Integer getVersion() {
1821: return version;
1822: }
1823:
1824: public void setVersion(Integer version) {
1825: this .version = version;
1826: }
1827:
1828: public List getAuthors() {
1829: return authors;
1830: }
1831:
1832: public void setAuthors(List authors) {
1833: this .authors = authors;
1834: }
1835:
1836: public List getReviews() {
1837: return reviews;
1838: }
1839:
1840: public void setReviews(List reviews) {
1841: this .reviews = reviews;
1842: }
1843:
1844: public Publisher getPublisher() {
1845: return publisher;
1846: }
1847:
1848: public void setPublisher(Publisher publisher) {
1849: this .publisher = publisher;
1850: }
1851: }
1852:
1853: public static final class Author implements Serializable {
1854: private Integer id;
1855: private String name;
1856: private List books;
1857: private Integer version;
1858:
1859: public Author() {
1860: }
1861:
1862: public Author(String name, List books) {
1863: this .name = name;
1864: this .books = books;
1865: }
1866:
1867: public void addBook(Book book) {
1868: if (books == null) {
1869: books = new ArrayList();
1870: }
1871: books.add(book);
1872: }
1873:
1874: public Integer getId() {
1875: return id;
1876: }
1877:
1878: public void setId(Integer id) {
1879: this .id = id;
1880: }
1881:
1882: public String getName() {
1883: return name;
1884: }
1885:
1886: public void setName(String name) {
1887: this .name = name;
1888: }
1889:
1890: public List getBooks() {
1891: return books;
1892: }
1893:
1894: public void setBooks(List books) {
1895: this .books = books;
1896: }
1897:
1898: public Integer getVersion() {
1899: return version;
1900: }
1901:
1902: public void setVersion(Integer version) {
1903: this .version = version;
1904: }
1905: }
1906:
1907: public static interface Publisher extends Serializable {
1908: public Integer getId();
1909:
1910: public void setId(Integer id);
1911:
1912: public String getName();
1913:
1914: public void setName(String name);
1915:
1916: public Integer getVersion();
1917:
1918: public void setVersion(Integer version);
1919: }
1920:
1921: public static final class PublisherImpl implements Publisher {
1922: private Integer id;
1923: private String name;
1924: private Integer version;
1925:
1926: public PublisherImpl() {
1927: }
1928:
1929: public PublisherImpl(String name) {
1930: this .name = name;
1931: }
1932:
1933: public Integer getId() {
1934: return id;
1935: }
1936:
1937: public void setId(Integer id) {
1938: this .id = id;
1939: }
1940:
1941: public String getName() {
1942: return name;
1943: }
1944:
1945: public void setName(String name) {
1946: this .name = name;
1947: }
1948:
1949: public Integer getVersion() {
1950: return version;
1951: }
1952:
1953: public void setVersion(Integer version) {
1954: this .version = version;
1955: }
1956: }
1957:
1958: public static final class Review implements Serializable {
1959: private Integer id;
1960: private String summary;
1961: private Integer fkBook;
1962: private Integer version;
1963: private Author author;
1964:
1965: public Review() {
1966: }
1967:
1968: public Review(String summary) {
1969: this .summary = summary;
1970: }
1971:
1972: public Integer getId() {
1973: return id;
1974: }
1975:
1976: public void setId(Integer id) {
1977: this .id = id;
1978: }
1979:
1980: public Integer getFkBook() {
1981: return fkBook;
1982: }
1983:
1984: public void setFkBook(Integer fkBook) {
1985: this .fkBook = fkBook;
1986: }
1987:
1988: public String getSummary() {
1989: return summary;
1990: }
1991:
1992: public void setSummary(String summary) {
1993: this .summary = summary;
1994: }
1995:
1996: public Integer getVersion() {
1997: return version;
1998: }
1999:
2000: public void setVersion(Integer version) {
2001: this .version = version;
2002: }
2003:
2004: public Author getAuthor() {
2005: return author;
2006: }
2007:
2008: public void setAuthor(Author author) {
2009: this .author = author;
2010: }
2011:
2012: public boolean equals(Object obj) {
2013: boolean result = false;
2014: if (obj instanceof Review) {
2015: Review other = (Review) obj;
2016: result = new EqualsBuilder().append(id, other.id)
2017: .append(summary, other.summary).append(version,
2018: other.version).append(fkBook,
2019: other.fkBook).append(author,
2020: other.author).isEquals();
2021: }
2022: return result;
2023: }
2024: }
2025:
2026: }
|