0001: package org.apache.ojb.broker;
0002:
0003: import java.util.ArrayList;
0004: import java.util.Collection;
0005: import java.util.Iterator;
0006: import java.util.List;
0007:
0008: import org.apache.commons.lang.ClassUtils;
0009: import org.apache.commons.lang.builder.EqualsBuilder;
0010: import org.apache.commons.lang.builder.HashCodeBuilder;
0011: import org.apache.commons.lang.builder.ToStringBuilder;
0012: import org.apache.commons.lang.time.StopWatch;
0013: import org.apache.ojb.broker.metadata.CollectionDescriptor;
0014: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
0015: import org.apache.ojb.broker.query.Criteria;
0016: import org.apache.ojb.broker.query.Query;
0017: import org.apache.ojb.broker.query.QueryFactory;
0018: import org.apache.ojb.broker.util.ObjectModification;
0019: import org.apache.ojb.junit.PBTestCase;
0020:
0021: /**
0022: * Test (non-decomposed) M:N relations.
0023: *
0024: * IMPORTANT NOTE: The global runtime metadata changes made by this test case
0025: * are NOT recommended in multithreaded environments, because they are global
0026: * and each thread will be affected.
0027: *
0028: * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
0029: * @version $Id: M2NTest.java,v 1.7.2.11 2005/11/26 02:37:03 arminw Exp $
0030: */
0031: public class M2NTest extends PBTestCase {
0032: static final int NONE = ObjectReferenceDescriptor.CASCADE_NONE;
0033: static final int LINK = ObjectReferenceDescriptor.CASCADE_LINK;
0034: static final int OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT;
0035:
0036: int actorCount = 200;
0037: int movieCount = 100;
0038:
0039: public static void main(String[] args) {
0040: junit.textui.TestRunner.main(new String[] { M2NTest.class
0041: .getName() });
0042: }
0043:
0044: public void tearDown() throws Exception {
0045: super .tearDown();
0046: }
0047:
0048: /**
0049: * Test for OJB-76
0050: */
0051: public void testStoreWithSharedIndirectionTableColumn() {
0052: ojbChangeReferenceSetting(MovieImpl.class, "producers", true,
0053: OBJECT, OBJECT, false);
0054: ojbChangeReferenceSetting(Producer.class, "movies", true,
0055: OBJECT, OBJECT, false);
0056: String timestamp = "" + System.currentTimeMillis();
0057: String postfix = "testStoreWithSharedIndirectionTableColumn_"
0058: + timestamp;
0059:
0060: Movie m_1 = new MovieImpl(postfix, postfix + "_1", null);
0061: Movie m_2 = new MovieImpl(postfix, postfix + "_2", null);
0062: Producer p_1 = new Producer(postfix, "producer_" + timestamp);
0063: m_1.addProducer(p_1);
0064: p_1.addMovie(m_1);
0065: broker.beginTransaction();
0066: broker.store(p_1, ObjectModification.INSERT);
0067: broker.commitTransaction();
0068:
0069: broker.clearCache();
0070: Criteria crit = new Criteria();
0071: crit.addLike("title", postfix + "%");
0072: Query q = QueryFactory.newQuery(Movie.class, crit);
0073: Movie new_m_1 = (Movie) broker.getObjectByQuery(q);
0074: assertNotNull(new_m_1);
0075: assertNotNull(new_m_1.getProducers());
0076: assertEquals(1, new_m_1.getProducers().size());
0077:
0078: broker.beginTransaction();
0079: p_1.addMovie(m_2);
0080: m_2.addProducer(p_1);
0081: broker.store(p_1, ObjectModification.UPDATE);
0082: // or (but this will cause more DB traffic)
0083: // broker.store(m_2, ObjectModification.INSERT);
0084: broker.commitTransaction();
0085:
0086: broker.clearCache();
0087: new_m_1 = (Movie) broker.getObjectByQuery(q);
0088: assertNotNull(new_m_1);
0089: assertNotNull(new_m_1.getProducers());
0090: assertEquals(1, new_m_1.getProducers().size());
0091: Producer new_p_1 = (Producer) new_m_1.getProducers().get(0);
0092: assertNotNull(new_p_1);
0093: assertNotNull(new_p_1.getMovies());
0094: assertEquals(2, new_p_1.getMovies().size());
0095:
0096: broker.beginTransaction();
0097: broker.delete(p_1);
0098: broker.commitTransaction();
0099:
0100: new_m_1 = (Movie) broker.getObjectByQuery(q);
0101: assertNull(new_m_1);
0102:
0103: crit = new Criteria();
0104: crit.addEqualTo("name", "producer_" + timestamp);
0105: q = QueryFactory.newQuery(Producer.class, crit);
0106: new_p_1 = (Producer) broker.getObjectByQuery(q);
0107: assertNull(new_p_1);
0108: }
0109:
0110: public void testSimpleStore_1() {
0111: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
0112: OBJECT, OBJECT, false);
0113: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
0114: OBJECT, OBJECT, false);
0115: ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT,
0116: OBJECT, false);
0117: String postfix = "testSimpleStore_1_"
0118: + System.currentTimeMillis();
0119: Movie m = new MovieImpl(postfix, postfix, null);
0120: Actor a = new Actor(postfix);
0121:
0122: broker.beginTransaction();
0123: broker.store(m);
0124: broker.store(a);
0125:
0126: m.addActor(a);
0127: broker.store(m);
0128: broker.commitTransaction();
0129:
0130: broker.retrieveAllReferences(a);
0131:
0132: assertNotNull(a.getMovies());
0133: assertEquals(1, a.getMovies().size());
0134: }
0135:
0136: public void testSimpleStore_2() {
0137: ojbChangeReferenceSetting(MovieImpl.class, "actors", false,
0138: OBJECT, OBJECT, false);
0139: ojbChangeReferenceSetting(MovieImpl.class, "actors2", false,
0140: OBJECT, OBJECT, false);
0141: ojbChangeReferenceSetting(Actor.class, "movies", false, OBJECT,
0142: OBJECT, false);
0143: String postfix = "testSimpleStore_2_"
0144: + System.currentTimeMillis();
0145: Movie m = new MovieImpl(postfix, postfix, null);
0146: Actor a = new Actor(postfix);
0147:
0148: broker.beginTransaction();
0149: broker.store(m);
0150: broker.store(a);
0151:
0152: m.addActor(a);
0153: broker.store(m);
0154: broker.commitTransaction();
0155:
0156: // needed, because autoretrieve is set false
0157: broker.retrieveAllReferences(a);
0158:
0159: assertNotNull(a.getMovies());
0160: assertEquals(1, a.getMovies().size());
0161: }
0162:
0163: /**
0164: * Test deprecated auto Settings
0165: */
0166: public void testAutoUpdateDeleteSettings() {
0167: ojbChangeReferenceSetting(Actor.class, "movies", false, false,
0168: false, false);
0169: CollectionDescriptor ord = broker.getClassDescriptor(
0170: Actor.class).getCollectionDescriptorByName("movies");
0171: assertEquals(LINK, ord.getCascadingStore());
0172: assertEquals(LINK, ord.getCascadingDelete());
0173: assertEquals(false, ord.getCascadeStore());
0174: assertEquals(false, ord.getCascadeDelete());
0175:
0176: ojbChangeReferenceSetting(Actor.class, "movies", false, true,
0177: true, false);
0178: ord = broker.getClassDescriptor(Actor.class)
0179: .getCollectionDescriptorByName("movies");
0180: assertEquals(OBJECT, ord.getCascadingStore());
0181: assertEquals(OBJECT, ord.getCascadingDelete());
0182: assertEquals(true, ord.getCascadeStore());
0183: assertEquals(true, ord.getCascadeDelete());
0184: }
0185:
0186: public void testMassStoreUpdateAutomatic() {
0187:
0188: long testPeriod = 0;
0189:
0190: String postfix = "testMassStoreUpdateAutomatic_"
0191: + System.currentTimeMillis();
0192: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
0193: OBJECT, OBJECT, false);
0194: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
0195: OBJECT, OBJECT, false);
0196: ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT,
0197: OBJECT, false);
0198:
0199: Movie movie = buildMovieWithActors(postfix, actorCount);
0200: Actor actor = buildActorWithMovies(postfix, movieCount);
0201:
0202: List actors = new ArrayList(movie.getActors());
0203: actors.add(actor);
0204: movie.setActors(actors);
0205:
0206: MovieManageableCollection movies = actor.getMovies();
0207: movies.add(movie);
0208:
0209: StopWatch watch = new StopWatch();
0210: watch.start();
0211: broker.beginTransaction();
0212: broker.store(movie);
0213: broker.commitTransaction();
0214: watch.stop();
0215: System.out.println("["
0216: + ClassUtils.getShortClassName(this .getClass())
0217: + "#testMassStoreUpdateAutomatic] Time to store "
0218: + (actorCount + movieCount) + " m:n objects="
0219: + watch.getTime());
0220: testPeriod += watch.getTime();
0221:
0222: watch.reset();
0223: watch.start();
0224: Query queryMovie = queryMovie(postfix);
0225: Collection resultMovie = broker
0226: .getCollectionByQuery(queryMovie);
0227: assertEquals(movieCount + 1, resultMovie.size());
0228: watch.stop();
0229: System.out.println("["
0230: + ClassUtils.getShortClassName(this .getClass())
0231: + "#testMassStoreUpdateAutomatic] Time to query "
0232: + movieCount + " m:n objects=" + watch.getTime());
0233: testPeriod += watch.getTime();
0234:
0235: watch.reset();
0236: watch.start();
0237: Query queryActor = queryActor(postfix);
0238: Collection resultActor = broker
0239: .getCollectionByQuery(queryActor);
0240: assertEquals(actorCount + 1, resultActor.size());
0241: watch.stop();
0242: System.out.println("["
0243: + ClassUtils.getShortClassName(this .getClass())
0244: + "#testMassStoreUpdateAutomatic] Time to query "
0245: + actorCount + " m:n objects=" + watch.getTime());
0246: testPeriod += watch.getTime();
0247:
0248: Query queryRole = roleQueryActorOrMovieMatch(actor, movie);
0249: Collection resultRole = broker.getCollectionByQuery(queryRole);
0250: assertEquals(actorCount + movieCount + 1, resultRole.size());
0251:
0252: //*****************************
0253: // update movie
0254: movie.setActors(new ArrayList());
0255: watch.reset();
0256: watch.start();
0257: broker.beginTransaction();
0258: broker.store(movie);
0259: broker.commitTransaction();
0260: watch.stop();
0261: System.out
0262: .println("["
0263: + ClassUtils.getShortClassName(this .getClass())
0264: + "#testMassStoreUpdateAutomatic] Time to update object with "
0265: + actorCount + " m:n objects="
0266: + watch.getTime());
0267: testPeriod += watch.getTime();
0268: //*****************************
0269:
0270: broker.clearCache();
0271: Identity oid = broker.serviceIdentity().buildIdentity(movie);
0272: movie = (Movie) broker.getObjectByIdentity(oid);
0273:
0274: resultMovie = broker.getCollectionByQuery(queryMovie);
0275: assertEquals(movieCount + 1, resultMovie.size());
0276:
0277: resultActor = broker.getCollectionByQuery(queryActor);
0278: assertEquals(actorCount + 1, resultActor.size());
0279:
0280: resultRole = broker.getCollectionByQuery(queryRole);
0281: assertEquals(movieCount, resultRole.size());
0282:
0283: assertNotNull(movie);
0284: assertEquals(0, movie.getActors().size());
0285:
0286: //*****************************
0287: // remove actor
0288: movie.setActors(new ArrayList());
0289: watch.reset();
0290: watch.start();
0291: broker.beginTransaction();
0292: broker.delete(actor);
0293: broker.commitTransaction();
0294: watch.stop();
0295: System.out
0296: .println("["
0297: + ClassUtils.getShortClassName(this .getClass())
0298: + "#testMassStoreUpdateAutomatic] Time to remove object with "
0299: + movieCount + " m:n objects="
0300: + watch.getTime());
0301: testPeriod += watch.getTime();
0302: //*****************************
0303:
0304: broker.clearCache();
0305: oid = broker.serviceIdentity().buildIdentity(actor);
0306: actor = (Actor) broker.getObjectByIdentity(oid);
0307:
0308: resultMovie = broker.getCollectionByQuery(queryMovie);
0309: assertEquals(0, resultMovie.size());
0310:
0311: // we never delete these actor objects
0312: resultActor = broker.getCollectionByQuery(queryActor);
0313: assertEquals(actorCount, resultActor.size());
0314:
0315: resultRole = broker.getCollectionByQuery(queryRole);
0316: assertEquals(0, resultRole.size());
0317:
0318: assertNull(actor);
0319:
0320: broker.beginTransaction();
0321: broker.delete(movie);
0322: broker.commitTransaction();
0323:
0324: resultMovie = broker.getCollectionByQuery(queryMovie);
0325: assertEquals(0, resultMovie.size());
0326: System.out.println("["
0327: + ClassUtils.getShortClassName(this .getClass())
0328: + "#testMassStoreUpdateAutomatic] Total test time is "
0329: + testPeriod + " ms");
0330: System.out.println("");
0331: }
0332:
0333: public void testMassStoreUpdateLinking() {
0334: long testPeriod = 0;
0335:
0336: String postfix = "testMassStoreUpdateLinking"
0337: + System.currentTimeMillis();
0338: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
0339: NONE, OBJECT, false);
0340: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
0341: NONE, OBJECT, false);
0342: ojbChangeReferenceSetting(Actor.class, "movies", true, NONE,
0343: OBJECT, false);
0344:
0345: Movie movie = buildMovieWithActors(postfix, actorCount);
0346: Actor actor = buildActorWithMovies(postfix, movieCount);
0347:
0348: List actors = new ArrayList(movie.getActors());
0349: actors.add(actor);
0350: movie.setActors(actors);
0351:
0352: MovieManageableCollection movies = actor.getMovies();
0353: movies.add(movie);
0354:
0355: StopWatch watch = new StopWatch();
0356: watch.start();
0357: broker.beginTransaction();
0358: broker.store(movie);
0359: for (int i = 0; i < actors.size(); i++) {
0360: broker.store(actors.get(i));
0361: }
0362: MovieManageableCollection actorMovies = actor.getMovies();
0363: for (int i = 0; i < actorMovies.size(); i++) {
0364: broker.store(actorMovies.get(i));
0365: }
0366: broker.serviceBrokerHelper().link(movie, true);
0367: broker.serviceBrokerHelper().link(actor, true);
0368: broker.commitTransaction();
0369: watch.stop();
0370: System.out.println("["
0371: + ClassUtils.getShortClassName(this .getClass())
0372: + "#testMassStoreUpdateLinking] Time to store "
0373: + (actorCount + movieCount) + " m:n objects="
0374: + watch.getTime());
0375: testPeriod += watch.getTime();
0376:
0377: watch.reset();
0378: watch.start();
0379: Query queryMovie = queryMovie(postfix);
0380: Collection resultMovie = broker
0381: .getCollectionByQuery(queryMovie);
0382: assertEquals(movieCount + 1, resultMovie.size());
0383: watch.stop();
0384: System.out.println("["
0385: + ClassUtils.getShortClassName(this .getClass())
0386: + "#testMassStoreUpdateLinking] Time to query "
0387: + movieCount + " m:n objects=" + watch.getTime());
0388: testPeriod += watch.getTime();
0389:
0390: watch.reset();
0391: watch.start();
0392: Query queryActor = queryActor(postfix);
0393: Collection resultActor = broker
0394: .getCollectionByQuery(queryActor);
0395: assertEquals(actorCount + 1, resultActor.size());
0396: watch.stop();
0397: System.out.println("["
0398: + ClassUtils.getShortClassName(this .getClass())
0399: + "#testMassStoreUpdateLinking] Time to query "
0400: + actorCount + " m:n objects=" + watch.getTime());
0401: testPeriod += watch.getTime();
0402:
0403: Query queryRole = roleQueryActorOrMovieMatch(actor, movie);
0404: Collection resultRole = broker.getCollectionByQuery(queryRole);
0405: assertEquals(actorCount + movieCount + 1, resultRole.size());
0406:
0407: //*****************************
0408: // update movie
0409: movie.setActors(new ArrayList());
0410: watch.reset();
0411: watch.start();
0412: broker.beginTransaction();
0413: broker.serviceBrokerHelper().unlink(movie);
0414: broker.store(movie);
0415: broker.serviceBrokerHelper().link(movie, false);
0416: broker.commitTransaction();
0417: watch.stop();
0418: System.out
0419: .println("["
0420: + ClassUtils.getShortClassName(this .getClass())
0421: + "#testMassStoreUpdateLinking] Time to update object with "
0422: + actorCount + " m:n objects="
0423: + watch.getTime());
0424: testPeriod += watch.getTime();
0425: //*****************************
0426:
0427: broker.clearCache();
0428: Identity oid = broker.serviceIdentity().buildIdentity(movie);
0429: movie = (Movie) broker.getObjectByIdentity(oid);
0430:
0431: resultMovie = broker.getCollectionByQuery(queryMovie);
0432: assertEquals(movieCount + 1, resultMovie.size());
0433:
0434: resultActor = broker.getCollectionByQuery(queryActor);
0435: assertEquals(actorCount + 1, resultActor.size());
0436:
0437: resultRole = broker.getCollectionByQuery(queryRole);
0438: assertEquals(movieCount, resultRole.size());
0439:
0440: assertNotNull(movie);
0441: assertEquals(0, movie.getActors().size());
0442:
0443: //*****************************
0444: // remove actor
0445: movie.setActors(new ArrayList());
0446: watch.reset();
0447: watch.start();
0448: broker.beginTransaction();
0449: broker.delete(actor);
0450: broker.commitTransaction();
0451: watch.stop();
0452: System.out
0453: .println("["
0454: + ClassUtils.getShortClassName(this .getClass())
0455: + "#testMassStoreUpdateLinking] Time to remove object with "
0456: + movieCount + " m:n objects="
0457: + watch.getTime());
0458: testPeriod += watch.getTime();
0459: //*****************************
0460:
0461: broker.clearCache();
0462: oid = broker.serviceIdentity().buildIdentity(actor);
0463: actor = (Actor) broker.getObjectByIdentity(oid);
0464:
0465: resultMovie = broker.getCollectionByQuery(queryMovie);
0466: assertEquals(0, resultMovie.size());
0467:
0468: // we never delete these actor objects
0469: resultActor = broker.getCollectionByQuery(queryActor);
0470: assertEquals(actorCount, resultActor.size());
0471:
0472: resultRole = broker.getCollectionByQuery(queryRole);
0473: assertEquals(0, resultRole.size());
0474:
0475: assertNull(actor);
0476:
0477: broker.beginTransaction();
0478: broker.delete(movie);
0479: broker.commitTransaction();
0480:
0481: resultMovie = broker.getCollectionByQuery(queryMovie);
0482: assertEquals(0, resultMovie.size());
0483: System.out.println("["
0484: + ClassUtils.getShortClassName(this .getClass())
0485: + "#testMassStoreUpdateLinking] Total test time is "
0486: + testPeriod + " ms");
0487: System.out.println("");
0488: }
0489:
0490: //============================================================
0491: // auto-retrieve true / auto-update/auto-delete LINK
0492: //============================================================
0493: // test needs refactoring, LINK settings doesn't allow to store
0494: // an object graph
0495: public void YYYtestStoreAddUpdateDeleteTLLF() {
0496: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
0497: LINK, LINK, false);
0498: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
0499: LINK, LINK, false);
0500: ojbChangeReferenceSetting(Actor.class, "movies", true, LINK,
0501: LINK, false);
0502: String postfix = "_testStoreTLLF_" + System.currentTimeMillis();
0503: Movie movie = buildMovieWithActorsAndBackReferences(postfix);
0504: doTestStoreAddUpdateDeleteTLLX(movie, postfix);
0505: }
0506:
0507: public void YYYtestStoreAddUpdateDeleteTLLT() {
0508: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
0509: LINK, LINK, true);
0510: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
0511: LINK, LINK, true);
0512: ojbChangeReferenceSetting(Actor.class, "movies", true, LINK,
0513: LINK, false);
0514: String postfix = "_testStoreTLLF_" + System.currentTimeMillis();
0515: Movie movie = buildMovieWithActorsAndBackReferences(postfix);
0516: doTestStoreAddUpdateDeleteTLLX(movie, postfix);
0517: }
0518:
0519: public void doTestStoreAddUpdateDeleteTLLX(Movie movie,
0520: String postfix) {
0521: broker.beginTransaction();
0522: broker.store(movie);
0523: broker.commitTransaction();
0524: Identity movieOID = broker.serviceIdentity().buildIdentity(
0525: movie);
0526:
0527: /*
0528: auto-update setting is LINK, but it behaves same way as OBJECT
0529: */
0530: broker.clearCache();
0531: Query queryMovie = queryMovie(postfix);
0532: Collection resultMovie = broker
0533: .getCollectionByQuery(queryMovie);
0534: assertEquals(3, resultMovie.size());
0535:
0536: Query queryActor = queryActor(postfix);
0537: Collection resultActor = broker
0538: .getCollectionByQuery(queryActor);
0539: assertEquals(3, resultActor.size());
0540:
0541: // first created actor
0542: Actor actor = (Actor) movie.getActors().iterator().next();
0543: Query queryRole = queryRole(actor, movie);
0544: Collection resultRole = broker.getCollectionByQuery(queryRole);
0545: assertEquals(5, resultRole.size());
0546:
0547: broker.clearCache();
0548: broker.beginTransaction();
0549: Movie newMovie = (Movie) broker.getObjectByIdentity(movieOID);
0550: newMovie.setTitle("updated_title_" + postfix);
0551: Iterator it = newMovie.getActors().iterator();
0552: while (it.hasNext()) {
0553: Actor a = (Actor) it.next();
0554: a.setName("updated_name_" + postfix);
0555: }
0556: ArrayList list = new ArrayList(newMovie.getActors());
0557: list.add(new Actor("updated_name_" + postfix));
0558: newMovie.setActors(list);
0559: broker.store(newMovie);
0560: broker.commitTransaction();
0561:
0562: broker.clearCache();
0563: queryMovie = queryMovie(postfix);
0564: resultMovie = broker.getCollectionByQuery(queryMovie);
0565: assertEquals(3, resultMovie.size());
0566:
0567: queryActor = queryActor(postfix);
0568: resultActor = broker.getCollectionByQuery(queryActor);
0569: assertEquals(4, resultActor.size());
0570:
0571: // first created actor
0572: actor = (Actor) movie.getActors().iterator().next();
0573: queryRole = queryRole(actor, movie);
0574: resultRole = broker.getCollectionByQuery(queryRole);
0575: assertEquals(6, resultRole.size());
0576:
0577: newMovie = (Movie) broker.getObjectByIdentity(movieOID);
0578: assertEquals("updated_title_" + postfix, newMovie.getTitle());
0579: it = newMovie.getActors().iterator();
0580: while (it.hasNext()) {
0581: Actor a = (Actor) it.next();
0582: assertEquals("updated_name_" + postfix, a.getName());
0583: }
0584:
0585: // now we delete the movie object
0586: broker.beginTransaction();
0587: broker.delete(newMovie);
0588: broker.commitTransaction();
0589:
0590: broker.clearCache();
0591: resultMovie = broker.getCollectionByQuery(queryMovie);
0592: assertEquals(2, resultMovie.size());
0593:
0594: resultActor = broker.getCollectionByQuery(queryActor);
0595: assertEquals(4, resultActor.size());
0596:
0597: resultRole = broker.getCollectionByQuery(queryRole);
0598: assertEquals(2, resultRole.size());
0599: }
0600:
0601: //============================================================
0602: // auto-update / auto-retrieve false
0603: //============================================================
0604:
0605: /**
0606: * All auto-xxx settings are false, thus expect that all thing must be
0607: * done by hand
0608: */
0609: public void testStoreFFFF() {
0610: ojbChangeReferenceSetting(MovieImpl.class, "actors", false,
0611: NONE, NONE, false);
0612: ojbChangeReferenceSetting(MovieImpl.class, "actors2", false,
0613: NONE, NONE, false);
0614: ojbChangeReferenceSetting(Actor.class, "movies", false, NONE,
0615: NONE, false);
0616: String postfix = "" + System.currentTimeMillis();
0617: Movie movie = buildMovieWithActors(postfix);
0618: doTestStoreFFFX(movie, postfix);
0619: }
0620:
0621: /**
0622: * All auto-xxx settings are false, thus expect that all thing must be
0623: * done by hand, proxy true should not affect anything
0624: */
0625: public void testStoreFFFT() {
0626: ojbChangeReferenceSetting(MovieImpl.class, "actors", false,
0627: NONE, NONE, true);
0628: ojbChangeReferenceSetting(MovieImpl.class, "actors2", false,
0629: NONE, NONE, true);
0630: ojbChangeReferenceSetting(Actor.class, "movies", false, NONE,
0631: NONE, false);
0632: String postfix = "" + System.currentTimeMillis();
0633: Movie movie = buildMovieWithActors(postfix);
0634: doTestStoreFFFX(movie, postfix);
0635: }
0636:
0637: /**
0638: * All auto-xxx settings are false, thus expect that all thing must be
0639: * done by hand. Actors has a collection of movies too - should be ignored
0640: * in this case.
0641: */
0642: public void testStoreFFFF_2() {
0643: ojbChangeReferenceSetting(MovieImpl.class, "actors", false,
0644: NONE, NONE, false);
0645: ojbChangeReferenceSetting(MovieImpl.class, "actors2", false,
0646: NONE, NONE, false);
0647: ojbChangeReferenceSetting(Actor.class, "movies", false, NONE,
0648: NONE, false);
0649: String postfix = "" + System.currentTimeMillis();
0650: Movie movie = buildMovieWithActorsAndBackReferences(postfix);
0651: doTestStoreFFFX(movie, postfix);
0652: }
0653:
0654: /**
0655: * All auto-xxx settings are false, thus expect that all thing must be
0656: * done by hand, proxy true should not affect anything. Actors has a
0657: * collection of movies too - should be ignored in this case.
0658: */
0659: public void testStoreFFFT_2() {
0660: ojbChangeReferenceSetting(MovieImpl.class, "actors", false,
0661: NONE, NONE, true);
0662: ojbChangeReferenceSetting(MovieImpl.class, "actors2", false,
0663: NONE, NONE, true);
0664: ojbChangeReferenceSetting(Actor.class, "movies", false, NONE,
0665: NONE, false);
0666: String postfix = "" + System.currentTimeMillis();
0667: Movie movie = buildMovieWithActorsAndBackReferences(postfix);
0668: doTestStoreFFFX(movie, postfix);
0669: }
0670:
0671: public void doTestStoreFFFX(Movie movie, String postfix) {
0672: broker.beginTransaction();
0673: broker.store(movie);
0674: broker.commitTransaction();
0675:
0676: /*
0677: all auto-xxx settings are false, so only the movie object should be
0678: stored - no other inserts!
0679: */
0680: Query queryMovie = queryMovie(postfix);
0681: Collection resultMovie = broker
0682: .getCollectionByQuery(queryMovie);
0683: assertEquals(1, resultMovie.size());
0684:
0685: Query queryActor = queryActor(postfix);
0686: Collection resultActor = broker
0687: .getCollectionByQuery(queryActor);
0688: // auto-update is false, thus we don't expect an Actor object
0689: assertEquals(0, resultActor.size());
0690:
0691: Query queryRole = queryRole(null, movie);
0692: Collection resultRole = broker.getCollectionByQuery(queryRole);
0693: // auto-update is false, thus we don't expect Role objects
0694: assertEquals(0, resultRole.size());
0695:
0696: broker.beginTransaction();
0697: /*
0698: now we store the right-side objects and the intermediary entries
0699: */
0700: Iterator it = movie.getActors().iterator();
0701: while (it.hasNext()) {
0702: Object actor = it.next();
0703: broker.store(actor);
0704: }
0705: // now both side exist and we can link the references
0706: broker.serviceBrokerHelper().link(movie, "actors", true);
0707: /*
0708: alternative call
0709: broker.serviceBrokerHelper().link(movie, true);
0710: */
0711: broker.commitTransaction();
0712:
0713: /*
0714: now we expect all stored objects
0715: */
0716: resultMovie = broker.getCollectionByQuery(queryMovie);
0717: assertEquals(1, resultMovie.size());
0718:
0719: resultActor = broker.getCollectionByQuery(queryActor);
0720: assertEquals(3, resultActor.size());
0721:
0722: resultRole = broker.getCollectionByQuery(queryRole);
0723: assertEquals(3, resultRole.size());
0724:
0725: broker.clearCache();
0726: Identity oid = broker.serviceIdentity().buildIdentity(movie);
0727: Movie readMovie = (Movie) broker.getObjectByIdentity(oid);
0728: assertNotNull(readMovie);
0729: // auto-retrieve false
0730: assertTrue(readMovie.getActors() == null
0731: || readMovie.getActors().size() == 0);
0732:
0733: broker.retrieveAllReferences(readMovie);
0734: assertEquals(3, readMovie.getActors().size());
0735:
0736: resultActor = broker.getCollectionByQuery(queryActor);
0737: assertEquals(3, resultActor.size());
0738: Actor readActor = (Actor) resultActor.iterator().next();
0739: // auto-retrieve false
0740: assertTrue(readActor.getMovies() == null
0741: || readActor.getMovies().size() == 0);
0742: broker.retrieveAllReferences(readActor);
0743: assertEquals(1, readActor.getMovies().size());
0744:
0745: // We try to delete all objects
0746: // first do unlink the m:n references
0747: broker.beginTransaction();
0748: broker.serviceBrokerHelper().unlink(readMovie, "actors");
0749: /*
0750: alternative call
0751: broker.serviceBrokerHelper().unlink(readMovie);
0752: */
0753: broker.commitTransaction();
0754:
0755: broker.clearCache();
0756: resultMovie = broker.getCollectionByQuery(queryMovie);
0757: assertEquals(1, resultMovie.size());
0758:
0759: resultActor = broker.getCollectionByQuery(queryActor);
0760: assertEquals(3, resultActor.size());
0761:
0762: resultRole = broker.getCollectionByQuery(queryRole);
0763: assertEquals(0, resultRole.size());
0764:
0765: // now we delete the n- and m-side objects
0766: broker.beginTransaction();
0767: Iterator iter = movie.getActors().iterator();
0768: while (iter.hasNext()) {
0769: broker.delete(iter.next());
0770: }
0771: broker.delete(movie);
0772: broker.commitTransaction();
0773:
0774: broker.clearCache();
0775: resultMovie = broker.getCollectionByQuery(queryMovie);
0776: assertEquals(0, resultMovie.size());
0777:
0778: resultActor = broker.getCollectionByQuery(queryActor);
0779: assertEquals(0, resultActor.size());
0780:
0781: resultRole = broker.getCollectionByQuery(queryRole);
0782: assertEquals(0, resultRole.size());
0783: }
0784:
0785: //============================================================
0786: // auto-retrieve true / auto-update/auto-delete false
0787: //============================================================
0788: /**
0789: * auto-retrieve is true, other false. proxy true/false should not affect
0790: * anything.
0791: */
0792: public void testStoreTFFF() {
0793: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
0794: NONE, NONE, false);
0795: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
0796: NONE, NONE, false);
0797: ojbChangeReferenceSetting(Actor.class, "movies", true, NONE,
0798: NONE, false);
0799: doTestStoreTFFX();
0800: }
0801:
0802: /**
0803: * auto-retrieve is true, other false. proxy true/false should not affect
0804: * anything.
0805: */
0806: public void testStoreTFFT() {
0807: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
0808: NONE, NONE, true);
0809: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
0810: NONE, NONE, true);
0811: ojbChangeReferenceSetting(Actor.class, "movies", true, NONE,
0812: NONE, false);
0813: doTestStoreTFFX();
0814: }
0815:
0816: public void doTestStoreTFFX() {
0817: String postfix = "" + System.currentTimeMillis();
0818: Movie movie = buildMovieWithActorsAndBackReferences(postfix);
0819:
0820: broker.beginTransaction();
0821: broker.store(movie);
0822: broker.commitTransaction();
0823: broker.clearCache();
0824:
0825: /*
0826: auto-update settings is false, so only the movie object should be
0827: stored - no other inserts!
0828: */
0829: Query queryMovie = queryMovie(postfix);
0830: Collection collMovie = broker.getCollectionByQuery(queryMovie);
0831: assertEquals(1, collMovie.size());
0832:
0833: Query queryActor = queryActor(postfix);
0834: Collection resultActor = broker
0835: .getCollectionByQuery(queryActor);
0836: // auto-update is false, thus we don't expect an Actor object
0837: assertEquals(0, resultActor.size());
0838:
0839: Query queryRole = queryRole(null, movie);
0840: Collection resultRole = broker.getCollectionByQuery(queryRole);
0841: // auto-update is false, thus we don't expect Role objects
0842: assertEquals(0, resultRole.size());
0843:
0844: broker.beginTransaction();
0845: /*
0846: now we store the right-side objects and the intermediary entries
0847: */
0848: Iterator it = movie.getActors().iterator();
0849: while (it.hasNext()) {
0850: Object actor = it.next();
0851: broker.store(actor);
0852: }
0853: // now both side exist and we can link the references
0854: broker.serviceBrokerHelper().link(movie, "actors", true);
0855: /*
0856: alternative call
0857: broker.serviceBrokerHelper().link(movie, true);
0858: */
0859: broker.commitTransaction();
0860:
0861: /*
0862: now we expect all stored objects
0863: */
0864: collMovie = broker.getCollectionByQuery(queryMovie);
0865: assertEquals(1, collMovie.size());
0866:
0867: resultActor = broker.getCollectionByQuery(queryActor);
0868: assertEquals(3, resultActor.size());
0869:
0870: resultRole = broker.getCollectionByQuery(queryRole);
0871: assertEquals(3, resultRole.size());
0872:
0873: broker.clearCache();
0874: Identity oid = broker.serviceIdentity().buildIdentity(movie);
0875: Movie readMovie = (Movie) broker.getObjectByIdentity(oid);
0876: assertNotNull(readMovie);
0877: // auto-retrieve true
0878: assertTrue(readMovie.getActors() != null);
0879: assertEquals(3, readMovie.getActors().size());
0880:
0881: // Now we want to add new objects
0882: Actor a1 = new Actor(postfix);
0883: Actor a2 = new Actor(postfix);
0884: readMovie.addActor(a1);
0885: readMovie.addActor(a2);
0886: broker.beginTransaction();
0887: broker.store(a1);
0888: broker.store(a2);
0889: broker.serviceBrokerHelper().unlink(readMovie, "actors");
0890: broker.serviceBrokerHelper().link(readMovie, "actors", true);
0891: broker.commitTransaction();
0892:
0893: collMovie = broker.getCollectionByQuery(queryMovie);
0894: assertEquals(1, collMovie.size());
0895: resultActor = broker.getCollectionByQuery(queryActor);
0896: assertEquals(5, resultActor.size());
0897: resultRole = broker.getCollectionByQuery(queryRole);
0898: assertEquals(5, resultRole.size());
0899: broker.clearCache();
0900: oid = broker.serviceIdentity().buildIdentity(movie);
0901: readMovie = (Movie) broker.getObjectByIdentity(oid);
0902: assertNotNull(readMovie);
0903: // auto-retrieve true
0904: assertTrue(readMovie.getActors() != null);
0905: assertEquals(5, readMovie.getActors().size());
0906:
0907: // We try to delete all objects
0908: // first do unlink the m:n references
0909: broker.beginTransaction();
0910: broker.serviceBrokerHelper().unlink(readMovie, "actors");
0911: /*
0912: alternative call
0913: broker.serviceBrokerHelper().unlink(readMovie);
0914: */
0915: broker.commitTransaction();
0916:
0917: broker.clearCache();
0918: // TODO: replace this with query below (when prefetching bug was solved)
0919: //Movie movieLookup = (Movie) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(movie));
0920: //assertNotNull(movieLookup);
0921: collMovie = broker.getCollectionByQuery(queryMovie);
0922: assertEquals(1, collMovie.size());
0923: readMovie = (Movie) collMovie.iterator().next();
0924: assertEquals(0, readMovie.getActors().size());
0925:
0926: resultActor = broker.getCollectionByQuery(queryActor);
0927: assertEquals(5, resultActor.size());
0928:
0929: resultRole = broker.getCollectionByQuery(queryRole);
0930: assertEquals(0, resultRole.size());
0931:
0932: // now we delete the n- and m-side objects
0933: broker.beginTransaction();
0934: Iterator iter = resultActor.iterator();
0935: while (iter.hasNext()) {
0936: broker.delete(iter.next());
0937: }
0938: broker.delete(readMovie);
0939: broker.commitTransaction();
0940:
0941: // broker.clearCache();
0942: collMovie = broker.getCollectionByQuery(queryMovie);
0943: assertEquals(0, collMovie.size());
0944:
0945: resultActor = broker.getCollectionByQuery(queryActor);
0946: assertEquals(0, resultActor.size());
0947:
0948: resultRole = broker.getCollectionByQuery(queryRole);
0949: assertEquals(0, resultRole.size());
0950: }
0951:
0952: //============================================================
0953: // auto-update / auto-retrieve true
0954: //============================================================
0955:
0956: public void testStoreTTFF() {
0957: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
0958: OBJECT, NONE, false);
0959: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
0960: OBJECT, NONE, false);
0961: ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT,
0962: NONE, false);
0963: doTestStoreTTXX();
0964: }
0965:
0966: public void testStoreTTFT() {
0967: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
0968: OBJECT, NONE, true);
0969: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
0970: OBJECT, NONE, true);
0971: ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT,
0972: NONE, false);
0973: doTestStoreTTXX();
0974: }
0975:
0976: public void doTestStoreTTXX() {
0977: String postfix = "" + System.currentTimeMillis();
0978: Movie movie = buildMovieWithActors(postfix);
0979:
0980: broker.beginTransaction();
0981: broker.store(movie);
0982: broker.commitTransaction();
0983: broker.clearCache();
0984:
0985: Query queryMovie = queryMovie(postfix);
0986: Collection resultMovie = broker
0987: .getCollectionByQuery(queryMovie);
0988: assertEquals(1, resultMovie.size());
0989:
0990: Query queryActor = queryActor(postfix);
0991: Collection resultActor = broker
0992: .getCollectionByQuery(queryActor);
0993: assertEquals(3 + 2, resultActor.size());
0994:
0995: Query queryRole = queryRole(null, movie);
0996: Collection resultRole = broker.getCollectionByQuery(queryRole);
0997: assertEquals(3, resultRole.size());
0998:
0999: broker.clearCache();
1000: Identity oid = broker.serviceIdentity().buildIdentity(movie);
1001: Movie readMovie = (Movie) broker.getObjectByIdentity(oid);
1002: assertNotNull(readMovie);
1003: assertEquals(3, readMovie.getActors().size());
1004: assertEquals(2, readMovie.getActors2().size());
1005: }
1006:
1007: /**
1008: * movies with back-references
1009: * auto-update = OBJECT
1010: */
1011: public void testStoreTTFT_2() {
1012: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
1013: OBJECT, NONE, true);
1014: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
1015: OBJECT, NONE, true);
1016: ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT,
1017: NONE, false);
1018: doTestStoreTTXX_2();
1019: }
1020:
1021: public void doTestStoreTTXX_2() {
1022: String postfix = "" + System.currentTimeMillis();
1023: Movie movie = buildMovieWithActorsAndBackReferences(postfix);
1024:
1025: broker.beginTransaction();
1026: broker.store(movie);
1027: broker.commitTransaction();
1028: broker.clearCache();
1029:
1030: Query queryMovie = queryMovie(postfix);
1031: Collection resultMovie = broker
1032: .getCollectionByQuery(queryMovie);
1033: assertEquals(3, resultMovie.size());
1034:
1035: Query queryActor = queryActor(postfix);
1036: Collection resultActor = broker
1037: .getCollectionByQuery(queryActor);
1038: assertEquals(3, resultActor.size());
1039:
1040: Query queryRole = queryRole(null, movie);
1041: Collection resultRole = broker.getCollectionByQuery(queryRole);
1042: assertEquals(3, resultRole.size());
1043:
1044: broker.clearCache();
1045: Identity oid = broker.serviceIdentity().buildIdentity(movie);
1046: Movie readMovie = (Movie) broker.getObjectByIdentity(oid);
1047: assertNotNull(readMovie);
1048: assertEquals(3, readMovie.getActors().size());
1049: }
1050:
1051: public void testStoreUpdateTTFF() {
1052: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
1053: OBJECT, NONE, false);
1054: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
1055: OBJECT, NONE, false);
1056: ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT,
1057: NONE, false);
1058: doTestStoreUpdateTTXX();
1059: }
1060:
1061: public void testStoreUpdateTTFF_2() {
1062: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
1063: OBJECT, NONE, true);
1064: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
1065: OBJECT, NONE, true);
1066: ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT,
1067: NONE, false);
1068: doTestStoreUpdateTTXX();
1069: }
1070:
1071: public void doTestStoreUpdateTTXX() {
1072: String postfix = "doTestStoreUpdateTTXX_"
1073: + System.currentTimeMillis();
1074: Movie movie = buildMovieWithActors(postfix);
1075:
1076: broker.beginTransaction();
1077: broker.store(movie);
1078: broker.commitTransaction();
1079: broker.clearCache();
1080:
1081: Query queryMovie = queryMovie(postfix);
1082: Collection resultMovie = broker
1083: .getCollectionByQuery(queryMovie);
1084: assertEquals(1, resultMovie.size());
1085:
1086: Query queryActor = queryActor(postfix);
1087: Collection resultActor = broker
1088: .getCollectionByQuery(queryActor);
1089: assertEquals(3 + 2, resultActor.size());
1090:
1091: Query queryRole = queryRole(null, movie);
1092: Collection resultRole = broker.getCollectionByQuery(queryRole);
1093: assertEquals(3, resultRole.size());
1094:
1095: //*****************************
1096: // remove all actors
1097: movie.setActors(new ArrayList());
1098: broker.beginTransaction();
1099: broker.store(movie);
1100: broker.commitTransaction();
1101: //*****************************
1102:
1103: broker.clearCache();
1104: Identity oid = broker.serviceIdentity().buildIdentity(movie);
1105: movie = (Movie) broker.getObjectByIdentity(oid);
1106:
1107: resultMovie = broker.getCollectionByQuery(queryMovie);
1108: assertEquals(1, resultMovie.size());
1109:
1110: resultActor = broker.getCollectionByQuery(queryActor);
1111: assertEquals(3 + 2, resultActor.size());
1112:
1113: resultRole = broker.getCollectionByQuery(queryRole);
1114: assertEquals(0, resultRole.size());
1115:
1116: assertNotNull(movie);
1117: assertEquals(0, movie.getActors().size());
1118: }
1119:
1120: public void testStoreUpdateActorTTFF() {
1121: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
1122: OBJECT, NONE, false);
1123: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
1124: OBJECT, NONE, false);
1125: ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT,
1126: NONE, false);
1127:
1128: String postfix = "" + System.currentTimeMillis();
1129: Actor actor = buildActorWithMovies(postfix);
1130:
1131: broker.beginTransaction();
1132: broker.store(actor);
1133: broker.commitTransaction();
1134:
1135: broker.clearCache();
1136: Query queryMovie = queryMovie(postfix);
1137: Collection resultMovie = broker
1138: .getCollectionByQuery(queryMovie);
1139: assertEquals(3, resultMovie.size());
1140:
1141: Query queryActor = queryActor(postfix);
1142: Collection resultActor = broker
1143: .getCollectionByQuery(queryActor);
1144: assertEquals(1, resultActor.size());
1145:
1146: Query queryRole = queryRole(actor, null);
1147: Collection resultRole = broker.getCollectionByQuery(queryRole);
1148: assertEquals(3, resultRole.size());
1149:
1150: broker.clearCache();
1151: Identity oid = broker.serviceIdentity().buildIdentity(actor);
1152: Actor loadedActor = (Actor) broker.getObjectByIdentity(oid);
1153: assertNotNull(loadedActor);
1154: MovieManageableCollection col = loadedActor.getMovies();
1155: assertNotNull(col);
1156: col.get(0);
1157: }
1158:
1159: public void testAddNewEntriesTTTF() {
1160: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
1161: OBJECT, OBJECT, false);
1162: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
1163: OBJECT, OBJECT, false);
1164: ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT,
1165: OBJECT, false);
1166: doTestAddNewEntries();
1167: }
1168:
1169: public void testAddNewEntriesTTTT() {
1170: ojbChangeReferenceSetting(MovieImpl.class, "actors", true,
1171: OBJECT, OBJECT, true);
1172: ojbChangeReferenceSetting(MovieImpl.class, "actors2", true,
1173: OBJECT, OBJECT, true);
1174: // default proxy does not work for user defined collection
1175: ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT,
1176: OBJECT, false);
1177: doTestAddNewEntries();
1178: }
1179:
1180: public void doTestAddNewEntries() {
1181: String postfix = "doTestAddNewEntries_"
1182: + System.currentTimeMillis();
1183:
1184: /*
1185: Returns 1 movie object with 3 actor objects and one actor object with
1186: back-reference to movie object + 2 new movies
1187: */
1188: Movie movie = buildMovieWithActorsAndBackReferences(postfix);
1189: Actor a_1 = new Actor("testAddNewEntries_" + postfix);
1190: Actor a_2 = new Actor("testAddNewEntries_" + postfix);
1191: Actor a_3 = new Actor("testAddNewEntries_" + postfix);
1192: Actor a_4 = new Actor("testAddNewEntries_" + postfix);
1193: /*
1194: all in all we expect 3 movie, 6 actor, 3 role entries after first
1195: store.
1196: */
1197:
1198: broker.beginTransaction();
1199: broker.store(movie);
1200: broker.store(a_1);
1201: broker.store(a_2);
1202: broker.store(a_3);
1203: broker.commitTransaction();
1204:
1205: broker.clearCache();
1206:
1207: Query queryMovie = queryMovie(postfix);
1208: Collection resultMovie = broker
1209: .getCollectionByQuery(queryMovie);
1210: assertEquals(3, resultMovie.size());
1211:
1212: Query queryActor = queryActor(postfix);
1213: Collection resultActor = broker
1214: .getCollectionByQuery(queryActor);
1215: assertEquals(6, resultActor.size());
1216:
1217: Query queryRole = queryRole(null, movie);
1218: Collection resultRole = broker.getCollectionByQuery(queryRole);
1219: assertEquals(3, resultRole.size());
1220:
1221: broker.clearCache();
1222: Identity oid = broker.serviceIdentity().buildIdentity(movie);
1223: Movie readMovie = (Movie) broker.getObjectByIdentity(oid);
1224: assertNotNull(readMovie);
1225: assertEquals(3, readMovie.getActors().size());
1226: assertEquals(0, readMovie.getActors2().size());
1227:
1228: /*
1229: we add 2 existing actor an movie object, thus we expect
1230: 3 movie, 6 actor, 5 role entries after store.
1231: And next lookup of movie we expect 5 dependend actor objects
1232: */
1233: movie.getActors().add(a_1);
1234: movie.getActors().add(a_2);
1235: // add new actor object
1236: movie.getActors().add(a_4);
1237: broker.beginTransaction();
1238: broker.store(movie);
1239: broker.commitTransaction();
1240:
1241: broker.clearCache();
1242:
1243: queryMovie = queryMovie(postfix);
1244: resultMovie = broker.getCollectionByQuery(queryMovie);
1245: assertEquals(3, resultMovie.size());
1246:
1247: queryActor = queryActor(postfix);
1248: resultActor = broker.getCollectionByQuery(queryActor);
1249: assertEquals(7, resultActor.size());
1250:
1251: queryRole = queryRole(null, movie);
1252: resultRole = broker.getCollectionByQuery(queryRole);
1253: assertEquals(6, resultRole.size());
1254:
1255: broker.clearCache();
1256: oid = broker.serviceIdentity().buildIdentity(movie);
1257: readMovie = (Movie) broker.getObjectByIdentity(oid);
1258: assertNotNull(readMovie);
1259: assertEquals(6, readMovie.getActors().size());
1260:
1261: /*
1262: on delete we expect that all entries are deleted except the single
1263: actor which have no references to any movie object
1264: */
1265: broker.beginTransaction();
1266: broker.delete(movie);
1267: broker.commitTransaction();
1268:
1269: broker.clearCache();
1270: resultMovie = broker.getCollectionByQuery(queryMovie);
1271: assertEquals(0, resultMovie.size());
1272:
1273: resultActor = broker.getCollectionByQuery(queryActor);
1274: assertEquals(1, resultActor.size());
1275:
1276: resultRole = broker.getCollectionByQuery(queryRole);
1277: assertEquals(0, resultRole.size());
1278: }
1279:
1280: //=======================================================================
1281: // helper methods
1282: //=======================================================================
1283: Query queryMovie(String postfix) {
1284: Criteria c = new Criteria();
1285: c.addLike("idStr", "%" + postfix + "%");
1286: return QueryFactory.newQuery(Movie.class, c);
1287: }
1288:
1289: Query queryActor(String postfix) {
1290: Criteria c = new Criteria();
1291: c.addLike("name", "%" + postfix + "%");
1292: return QueryFactory.newQuery(Actor.class, c);
1293: }
1294:
1295: Query queryRole(Actor actor, Movie movie) {
1296: Criteria c = new Criteria();
1297: if (actor != null)
1298: c.addEqualTo("actorId", actor.getId());
1299: if (movie != null && actor != null) {
1300: Criteria c2 = new Criteria();
1301: c2.addEqualTo("movieIntId", movie.getIdInt());
1302: c2.addEqualTo("movieStrId", movie.getIdStr());
1303: c.addOrCriteria(c2);
1304: } else if (movie != null) {
1305: c.addEqualTo("movieIntId", movie.getIdInt());
1306: c.addEqualTo("movieStrId", movie.getIdStr());
1307: }
1308: return QueryFactory.newQuery(Role.class, c);
1309: }
1310:
1311: Query roleQueryActorOrMovieMatch(Actor actor, Movie movie) {
1312: Criteria c_1 = new Criteria();
1313: Criteria c_2 = new Criteria();
1314: if (actor != null)
1315: c_1.addEqualTo("actorId", actor.getId());
1316: if (movie != null) {
1317: c_2.addEqualTo("movieIntId", movie.getIdInt());
1318: c_2.addEqualTo("movieStrId", movie.getIdStr());
1319: }
1320: if (actor != null) {
1321: c_2.addOrCriteria(c_1);
1322: } else {
1323: c_2 = c_1;
1324: }
1325: return QueryFactory.newQuery(Role.class, c_2);
1326: }
1327:
1328: /**
1329: * Returns 1 movie object with 3 actor objects in actors-collection
1330: * and 2 actor objects in actors2-collection
1331: */
1332: Movie buildMovieWithActors(String postfixId) {
1333: Movie m = new MovieImpl(
1334: postfixId,
1335: "Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb "
1336: + postfixId,
1337: "An insane general starts a process to nuclear holocaust that a war"
1338: + " room of politicians and generals frantically try to stop. "
1339: + postfixId);
1340:
1341: Actor a1 = new Actor("Peter Sellers " + postfixId);
1342: Actor a2 = new Actor("George C. Scott " + postfixId);
1343: Actor a3 = new Actor("Sterling Hayden " + postfixId);
1344: ArrayList list = new ArrayList();
1345: list.add(a1);
1346: list.add(a2);
1347: list.add(a3);
1348: m.setActors(list);
1349:
1350: Actor a4 = new Actor("Actor 2 A " + postfixId);
1351: Actor a5 = new Actor("Actor 2 B " + postfixId);
1352: ArrayList list2 = new ArrayList();
1353: list2.add(a4);
1354: list2.add(a5);
1355: m.setActors2(list2);
1356:
1357: return m;
1358: }
1359:
1360: /**
1361: * Returns 1 movie object with 3 actor objects
1362: */
1363: Movie buildMovieWithActors(String postfixId, int actorCount) {
1364: Movie m = new MovieImpl(postfixId, "Movie with " + actorCount
1365: + " actors_" + postfixId, "none");
1366:
1367: ArrayList list = new ArrayList();
1368: for (int i = 0; i < actorCount; i++) {
1369: Actor a = new Actor("A bad actor_" + postfixId);
1370: list.add(a);
1371: }
1372: m.setActors(list);
1373: return m;
1374: }
1375:
1376: /**
1377: * Returns 1 movie object with 3 actor objects
1378: */
1379: Actor buildActorWithMovies(String postfixId, int movieCount) {
1380: Actor a = new Actor(postfixId + "_Actor play in " + movieCount
1381: + " movies");
1382:
1383: MovieManageableCollection list = new MovieManageableCollection();
1384: for (int i = 0; i < movieCount; i++) {
1385: Movie m = new MovieImpl(postfixId, "A bad movie_"
1386: + postfixId, "none");
1387: list.add(m);
1388: }
1389: a.setMovies(list);
1390: return a;
1391: }
1392:
1393: /**
1394: * Returns 1 movie object with 3 actor objects and one actor object with
1395: * back-reference to movie object + 2 new movies
1396: */
1397: Movie buildMovieWithActorsAndBackReferences(String postfixId) {
1398: Movie m = new MovieImpl(
1399: postfixId,
1400: "Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb "
1401: + postfixId,
1402: "An insane general starts a process to nuclear holocaust that a war"
1403: + " room of politicians and generals frantically try to stop. "
1404: + postfixId);
1405: Actor a1 = new Actor("Peter Sellers " + postfixId);
1406: Actor a2 = new Actor("George C. Scott " + postfixId);
1407: Actor a3 = new Actor("Sterling Hayden " + postfixId);
1408: ArrayList list = new ArrayList();
1409: list.add(a1);
1410: list.add(a2);
1411: list.add(a3);
1412: m.setActors(list);
1413:
1414: Movie m1 = new MovieImpl(postfixId + "", "A Shot in the Dark",
1415: "As murder follows murder, beautiful Maria is the obvious suspect...");
1416: Movie m2 = new MovieImpl(
1417: postfixId + "",
1418: "The Pink Panther",
1419: " In the first movie starring Peter Sellers as the bumbling Inspector Clouseau...");
1420:
1421: MovieManageableCollection mlist1 = new MovieManageableCollection();
1422: mlist1.add(m);
1423: mlist1.add(m1);
1424: mlist1.add(m2);
1425: MovieManageableCollection mlist2 = new MovieManageableCollection();
1426: MovieManageableCollection mlist3 = new MovieManageableCollection();
1427: a1.setMovies(mlist1);
1428: a2.setMovies(mlist2);
1429: a3.setMovies(mlist3);
1430:
1431: return m;
1432: }
1433:
1434: Actor buildActorWithMovies(String postfixId) {
1435: Actor a = new Actor("John Cusack" + postfixId);
1436: MovieManageableCollection list = new MovieManageableCollection();
1437: list.add(new MovieImpl("a_" + postfixId, "High Fidelity",
1438: "A comedy about fear of commitment, hating your job..."
1439: + postfixId));
1440: list.add(new MovieImpl("b_" + postfixId, "Identity",
1441: "When a nasty storm hits a hotel, ten strangers are stranded within ..."
1442: + postfixId));
1443: list
1444: .add(new MovieImpl(
1445: "c_" + postfixId,
1446: "Grosse Pointe Blank",
1447: "Martin Blank is a professional assassin. He is sent on a mission to a small Detroit ..."
1448: + postfixId));
1449: a.setMovies(list);
1450: return a;
1451: }
1452:
1453: //=======================================================================
1454: // Inner classes, persistence capable test classes
1455: //=======================================================================
1456: public static class MovieManageableCollection implements
1457: ManageableCollection {
1458: private ArrayList list = new ArrayList();
1459:
1460: public void ojbAdd(Object anObject) {
1461: list.add(anObject);
1462: }
1463:
1464: public void ojbAddAll(ManageableCollection otherCollection) {
1465: Iterator it = otherCollection.ojbIterator();
1466: while (it.hasNext()) {
1467: list.add(it.next());
1468: }
1469: }
1470:
1471: public Iterator ojbIterator() {
1472: return list.iterator();
1473: }
1474:
1475: public void afterStore(PersistenceBroker broker)
1476: throws PersistenceBrokerException {
1477: }
1478:
1479: public Iterator iterator() {
1480: return list.iterator();
1481: }
1482:
1483: public int size() {
1484: return list.size();
1485: }
1486:
1487: public boolean isEmpty() {
1488: return list.isEmpty();
1489: }
1490:
1491: public void clear() {
1492: list.clear();
1493: }
1494:
1495: public boolean add(Movie movie) {
1496: return list.add(movie);
1497: }
1498:
1499: public boolean remove(Movie movie) {
1500: return list.remove(movie);
1501: }
1502:
1503: public boolean contains(Movie movie) {
1504: return list.contains(movie);
1505: }
1506:
1507: public Movie get(int index) {
1508: return (Movie) list.get(index);
1509: }
1510: }
1511:
1512: //===================================================================
1513: // inner class
1514: //===================================================================
1515: public static class Actor {
1516: private Integer id;
1517: private Integer id2;
1518: private String name;
1519: private MovieManageableCollection movies;
1520:
1521: public Actor() {
1522: }
1523:
1524: public Actor(String name) {
1525: this .name = name;
1526: }
1527:
1528: public MovieManageableCollection getMovies() {
1529: return movies;
1530: }
1531:
1532: public void setMovies(MovieManageableCollection movies) {
1533: this .movies = movies;
1534: }
1535:
1536: public void addMovie(Movie m) {
1537: if (movies == null) {
1538: movies = new MovieManageableCollection();
1539: }
1540: movies.add(m);
1541: }
1542:
1543: public Integer getId() {
1544: return id;
1545: }
1546:
1547: public void setId(Integer id) {
1548: this .id = id;
1549: }
1550:
1551: public Integer getId2() {
1552: return id2;
1553: }
1554:
1555: public void setId2(Integer id2) {
1556: this .id2 = id2;
1557: }
1558:
1559: public String getName() {
1560: return name;
1561: }
1562:
1563: public void setName(String name) {
1564: this .name = name;
1565: }
1566:
1567: public String toString() {
1568: return ToStringBuilder.reflectionToString(this );
1569: }
1570: }
1571:
1572: //===================================================================
1573: // inner class
1574: //===================================================================
1575: public static interface Movie {
1576: public Collection getActors();
1577:
1578: public void setActors(Collection actors);
1579:
1580: public void addActor(Actor a);
1581:
1582: public Collection getActors2();
1583:
1584: public void setActors2(Collection actors);
1585:
1586: public List getProducers();
1587:
1588: public void setProducers(List producers);
1589:
1590: public void addProducer(Producer p);
1591:
1592: public Integer getIdInt2();
1593:
1594: public Integer getIdInt();
1595:
1596: public void setIdInt2(Integer id2Int);
1597:
1598: public void setIdInt(Integer idInt);
1599:
1600: public String getIdStr();
1601:
1602: public void setIdStr(String idStr);
1603:
1604: public String getTitle();
1605:
1606: public void setTitle(String title);
1607:
1608: public String getDescription();
1609:
1610: public void setDescription(String description);
1611: }
1612:
1613: //===================================================================
1614: // inner class
1615: //===================================================================
1616: public static class MovieImpl implements Movie {
1617: private Integer idInt;
1618: private Integer idInt2;
1619: private String idStr;
1620:
1621: private String title;
1622: private String description;
1623: private Collection actors;
1624: private Collection actors2;
1625: private List producers;
1626:
1627: public MovieImpl() {
1628: }
1629:
1630: public MovieImpl(String idStr, String title, String description) {
1631: this .idStr = idStr;
1632: this .title = title;
1633: this .description = description;
1634: }
1635:
1636: public List getProducers() {
1637: return producers;
1638: }
1639:
1640: public void setProducers(List producers) {
1641: this .producers = producers;
1642: }
1643:
1644: public void addProducer(Producer p) {
1645: if (producers == null) {
1646: producers = new ArrayList();
1647: }
1648: producers.add(p);
1649: if (p.getMovies() == null || !p.getMovies().contains(this )) {
1650: p.addMovie(this );
1651: }
1652: }
1653:
1654: public Collection getActors() {
1655: return actors;
1656: }
1657:
1658: public void setActors(Collection actors) {
1659: this .actors = actors;
1660: }
1661:
1662: public void addActor(Actor a) {
1663: if (actors == null) {
1664: actors = new ArrayList();
1665: }
1666: actors.add(a);
1667: }
1668:
1669: public Collection getActors2() {
1670: return actors2;
1671: }
1672:
1673: public void setActors2(Collection actors) {
1674: this .actors2 = actors;
1675: }
1676:
1677: public Integer getIdInt() {
1678: return idInt;
1679: }
1680:
1681: public void setIdInt(Integer idInt) {
1682: this .idInt = idInt;
1683: }
1684:
1685: public Integer getIdInt2() {
1686: return idInt2;
1687: }
1688:
1689: public void setIdInt2(Integer idInt2) {
1690: this .idInt2 = idInt2;
1691: }
1692:
1693: public String getIdStr() {
1694: return idStr;
1695: }
1696:
1697: public void setIdStr(String idStr) {
1698: this .idStr = idStr;
1699: }
1700:
1701: public String getTitle() {
1702: return title;
1703: }
1704:
1705: public void setTitle(String title) {
1706: this .title = title;
1707: }
1708:
1709: public String getDescription() {
1710: return description;
1711: }
1712:
1713: public void setDescription(String description) {
1714: this .description = description;
1715: }
1716:
1717: public int hashCode() {
1718: return new HashCodeBuilder().append(idInt).append(idInt2)
1719: .append(idStr).hashCode();
1720: }
1721:
1722: public boolean equals(Object obj) {
1723: boolean result = false;
1724: if (obj instanceof MovieImpl) {
1725: MovieImpl other = (MovieImpl) obj;
1726: result = new EqualsBuilder().append(idInt, other.idInt)
1727: .append(idInt2, other.idInt2).append(idStr,
1728: other.idStr).isEquals();
1729: }
1730: return result;
1731: }
1732:
1733: public String toString() {
1734: return ToStringBuilder.reflectionToString(this );
1735: }
1736: }
1737:
1738: //===================================================================
1739: // inner class
1740: //===================================================================
1741: public static class Role {
1742: private Integer actorId;
1743: private Integer actorId2;
1744: private Integer movieIntId;
1745: private Integer movieIntId2;
1746: private String movieStrId;
1747:
1748: public Role() {
1749: }
1750:
1751: public Integer getActorId() {
1752: return actorId;
1753: }
1754:
1755: public void setActorId(Integer actorId) {
1756: this .actorId = actorId;
1757: }
1758:
1759: public Integer getMovieIntId() {
1760: return movieIntId;
1761: }
1762:
1763: public Integer getMovieIntId2() {
1764: return movieIntId2;
1765: }
1766:
1767: public void setMovieIntId2(Integer movieIntId2) {
1768: this .movieIntId2 = movieIntId2;
1769: }
1770:
1771: public Integer getActorId2() {
1772: return actorId2;
1773: }
1774:
1775: public void setActorId2(Integer actorId2) {
1776: this .actorId2 = actorId2;
1777: }
1778:
1779: public void setMovieIntId(Integer movieIntId) {
1780: this .movieIntId = movieIntId;
1781: }
1782:
1783: public String getMovieStrId() {
1784: return movieStrId;
1785: }
1786:
1787: public void setMovieStrId(String movieStrId) {
1788: this .movieStrId = movieStrId;
1789: }
1790:
1791: public String toString() {
1792: return ToStringBuilder.reflectionToString(this );
1793: }
1794: }
1795:
1796: //===================================================================
1797: // inner class
1798: //===================================================================
1799: /**
1800: * This class has a m:n relation with Movie and also use a composite
1801: * key.
1802: */
1803: public static class Producer {
1804: private Integer id;
1805: private String idStr;
1806: private String name;
1807: private List movies;
1808:
1809: public Producer() {
1810: }
1811:
1812: public Producer(String idStr, String name) {
1813: this .idStr = idStr;
1814: this .name = name;
1815: }
1816:
1817: public List getMovies() {
1818: return movies;
1819: }
1820:
1821: public void setMovies(List movies) {
1822: this .movies = movies;
1823: }
1824:
1825: public void addMovie(Movie movie) {
1826: if (movies == null) {
1827: movies = new ArrayList();
1828: }
1829: movies.add(movie);
1830: if (movie.getProducers() == null
1831: || !movie.getProducers().contains(this )) {
1832: movie.addProducer(this );
1833: }
1834: }
1835:
1836: public Integer getId() {
1837: return id;
1838: }
1839:
1840: public void setId(Integer id) {
1841: this .id = id;
1842: }
1843:
1844: public String getIdStr() {
1845: return idStr;
1846: }
1847:
1848: public void setIdStr(String idStr) {
1849: this .idStr = idStr;
1850: }
1851:
1852: public String getName() {
1853: return name;
1854: }
1855:
1856: public void setName(String name) {
1857: this .name = name;
1858: }
1859:
1860: public int hashCode() {
1861: return new HashCodeBuilder().append(id).append(idStr)
1862: .hashCode();
1863: }
1864:
1865: public boolean equals(Object obj) {
1866: boolean result = false;
1867: if (obj instanceof Producer) {
1868: Producer other = (Producer) obj;
1869: result = new EqualsBuilder().append(id, other.id)
1870: .append(idStr, other.idStr).isEquals();
1871: }
1872: return result;
1873: }
1874:
1875: public String toString() {
1876: return ToStringBuilder.reflectionToString(this);
1877: }
1878: }
1879: }
|