0001: package org.apache.ojb.broker;
0002:
0003: import java.io.Serializable;
0004: import java.util.Collection;
0005: import java.util.Iterator;
0006: import java.util.List;
0007: import java.util.ArrayList;
0008:
0009: import org.apache.ojb.broker.metadata.ClassDescriptor;
0010: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
0011: import org.apache.ojb.broker.query.Criteria;
0012: import org.apache.ojb.broker.query.Query;
0013: import org.apache.ojb.broker.query.QueryFactory;
0014: import org.apache.ojb.junit.PBTestCase;
0015: import org.apache.commons.lang.builder.ToStringBuilder;
0016:
0017: /**
0018: * Test case for checking the management of references.
0019: *
0020: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
0021: * @version $Id: ReferenceTest.java,v 1.17.2.4 2005/10/06 15:21:41 arminw Exp $
0022: */
0023: public class ReferenceTest extends PBTestCase {
0024: private static String REF_TEST_STRING = "refTest";
0025:
0026: public static void main(String[] args) {
0027: String[] arr = { ReferenceTest.class.getName() };
0028: junit.textui.TestRunner.main(arr);
0029: }
0030:
0031: public void tearDown() {
0032: if (broker != null) {
0033: changeRepositoryAutoSetting("ref", true,
0034: ObjectReferenceDescriptor.CASCADE_OBJECT,
0035: ObjectReferenceDescriptor.CASCADE_OBJECT);
0036: changeRepositoryAutoSetting("refA", true,
0037: ObjectReferenceDescriptor.CASCADE_OBJECT,
0038: ObjectReferenceDescriptor.CASCADE_OBJECT);
0039: changeRepositoryAutoSetting("refB", true,
0040: ObjectReferenceDescriptor.CASCADE_OBJECT,
0041: ObjectReferenceDescriptor.CASCADE_OBJECT);
0042: changeRepositoryAutoSetting("animal", true,
0043: ObjectReferenceDescriptor.CASCADE_OBJECT,
0044: ObjectReferenceDescriptor.CASCADE_OBJECT);
0045: broker.close();
0046: }
0047: }
0048:
0049: /**
0050: * Test for OJB-49
0051: */
0052: public void testQueryExtentsWithAutoRefreshEnabled()
0053: throws Exception {
0054: String name = "testQueryExtentsWithAutoRefreshEnabled_"
0055: + System.currentTimeMillis();
0056:
0057: int cascadeObject = ObjectReferenceDescriptor.CASCADE_OBJECT;
0058: ojbChangeReferenceSetting(ObjA.class, "ref", true,
0059: cascadeObject, cascadeObject, false);
0060: ojbChangeReferenceSetting(ObjB.class, "ref", true,
0061: cascadeObject, cascadeObject, false);
0062:
0063: ClassDescriptor cldA = broker.getClassDescriptor(ObjA.class);
0064: ClassDescriptor cldB = broker.getClassDescriptor(ObjB.class);
0065: boolean oldRefreshA = cldA.isAlwaysRefresh();
0066: boolean oldRefreshB = cldB.isAlwaysRefresh();
0067: cldA.setAlwaysRefresh(true);
0068: cldB.setAlwaysRefresh(true);
0069:
0070: try {
0071: ObjA objA = new ObjA();
0072: objA.setName(name);
0073: ObjB objB = new ObjB();
0074: objB.setName(name);
0075: ObjC objC = new ObjC();
0076: objC.setName(name);
0077:
0078: ObjA objA2 = new ObjA();
0079: objA2.setName(name);
0080: ObjB objB2 = new ObjB();
0081: objB2.setName(name);
0082: ObjC objC2 = new ObjC();
0083: objC2.setName(name);
0084:
0085: List refs = new ArrayList();
0086: refs.add(objA2);
0087: refs.add(objB2);
0088:
0089: objA.setRef(objB);
0090: objC2.setReferences(refs);
0091: objB.setRef(objC);
0092: objC2.setRef(objA);
0093:
0094: broker.beginTransaction();
0095: broker.store(objA);
0096: broker.store(objC2);
0097: broker.commitTransaction();
0098:
0099: Criteria crit = new Criteria();
0100: crit.addLike("name", name);
0101: Query q = QueryFactory.newQuery(RefObject.class, crit);
0102: Collection result = broker.getCollectionByQuery(q);
0103: assertEquals(6, result.size());
0104: } finally {
0105: cldA.setAlwaysRefresh(oldRefreshA);
0106: cldB.setAlwaysRefresh(oldRefreshB);
0107: }
0108: }
0109:
0110: /**
0111: * Test the usage of interface as class-ref in collection-descriptor
0112: * when using inheritance.
0113: */
0114: public void testInterfaceAsCollectionRef_1() {
0115: // if(skipKnownIssueProblem("query using path via reference, like 'ref1.ref2.name'")) return;
0116: String name = "testQueryWithCollectionRef_"
0117: + System.currentTimeMillis();
0118: RefObject a = new ObjA();
0119: RefObject a2 = new ObjA();
0120: RefObject b = new ObjB();
0121: RefObject b2 = new ObjB();
0122: // this object has a 1:n relation
0123: ObjC c1 = new ObjC();
0124: // only used in the c object reference collection
0125: RefObject d1 = new ObjC();
0126: RefObject d2 = new ObjC();
0127: ObjC c2 = new ObjC();
0128:
0129: c1.setName(name + "_third");
0130: b.setName(name + "_second_1");
0131: b2.setName(name + "_second_2");
0132: a.setName(name + "_first_1");
0133: a2.setName(name + "_first_2");
0134: d1.setName(name + "_d1");
0135: d2.setName(name + "_d2");
0136: c2.setName(name + "_c2");
0137:
0138: c1.setNameC(name + "_1");
0139: c2.setNameC(name + "_2");
0140:
0141: a.setRef(b);
0142: b.setRef(c1);
0143: a2.setRef(b2);
0144:
0145: List refList = new ArrayList();
0146: refList.add(a);
0147: refList.add(b2);
0148: refList.add(d1);
0149: c1.setReferences(refList);
0150: List refList2 = new ArrayList();
0151: refList2.add(d2);
0152: c2.setReferences(refList2);
0153:
0154: broker.beginTransaction();
0155: broker.store(a);
0156: broker.store(a2);
0157: broker.store(c2);
0158: broker.commitTransaction();
0159:
0160: // check existence of objects
0161: Criteria crit = new Criteria();
0162: crit.addLike("name", name + "%");
0163: Query q = QueryFactory.newQuery(RefObject.class, crit);
0164: Collection result = broker.getCollectionByQuery(q);
0165: assertEquals(8, result.size());
0166:
0167: // expect all 'C' objects with 1:n reference object
0168: // with name '..._d1' --> 'c1'
0169: crit = new Criteria();
0170: crit.addEqualTo("references.name", name + "_d1");
0171: q = QueryFactory.newQuery(ObjC.class, crit);
0172: result = broker.getCollectionByQuery(q);
0173: assertEquals(1, result.size());
0174: ObjC newC = (ObjC) result.iterator().next();
0175: assertEquals(name + "_1", newC.getNameC());
0176:
0177: // expect all 'C' objects with 1:n reference object
0178: // with nameC '..._%' --> 'c1' 'c2'
0179: crit = new Criteria();
0180: crit.addLike("nameC", name + "_%");
0181: q = QueryFactory.newQuery(ObjC.class, crit);
0182: result = broker.getCollectionByQuery(q);
0183: assertEquals(2, result.size());
0184:
0185: // expect all 'B' objects with 1:1 to an RefObject which
0186: // has an 1:n reference object
0187: // with name '..._d1' --> 'b'
0188: crit = new Criteria();
0189: crit.addEqualTo("ref.references.name", name + "_d1");
0190: // add this because only 'C' objects have a 1:n reference
0191: crit.addPathClass("ref", ObjC.class);
0192: q = QueryFactory.newQuery(ObjB.class, crit);
0193: result = broker.getCollectionByQuery(q);
0194: assertEquals(1, result.size());
0195: ObjB newB = (ObjB) result.iterator().next();
0196: assertNotNull(newB.getRef());
0197: assertTrue(newB.getRef() instanceof ObjC);
0198: newC = (ObjC) newB.getRef();
0199: assertEquals(3, newC.getReferences().size());
0200:
0201: // expect all 'B' objects with 1:1 to an RefObject which
0202: // has an 1:n reference object
0203: // with name '..._d1' --> 'b'
0204: crit = new Criteria();
0205: crit.addLike("ref.nameC", name + "_%");
0206: // add this because only 'C' objects have a 1:n reference
0207: crit.addPathClass("ref", ObjC.class);
0208: q = QueryFactory.newQuery(ObjB.class, crit);
0209: result = broker.getCollectionByQuery(q);
0210: assertEquals(1, result.size());
0211: newB = (ObjB) result.iterator().next();
0212: assertNotNull(newB.getRef());
0213: assertTrue(newB.getRef() instanceof ObjC);
0214: newC = (ObjC) newB.getRef();
0215: assertEquals(3, newC.getReferences().size());
0216:
0217: // expect all A's which have a B called '_second_1'
0218: crit = new Criteria();
0219: crit.addLike("name", name + "_%");
0220: crit.addEqualTo("ref.name", name + "_second_1");
0221: crit.addPathClass("ref", ObjB.class);
0222: q = QueryFactory.newQuery(ObjA.class, crit);
0223: result = broker.getCollectionByQuery(q);
0224: assertEquals(1, result.size());
0225:
0226: // expect all A's which have a B called '_second_1' and
0227: // a C called '_third'
0228: crit = new Criteria();
0229: crit.addLike("name", name + "_%");
0230: crit.addEqualTo("ref.name", name + "_second_1");
0231: crit.addEqualTo("ref.ref.name", name + "_third");
0232: crit.addPathClass("ref", ObjB.class);
0233: crit.addPathClass("ref.ref", ObjC.class);
0234: q = QueryFactory.newQuery(ObjA.class, crit);
0235: result = broker.getCollectionByQuery(q);
0236: assertEquals(1, result.size());
0237:
0238: // expect all A's which third level 'ref' has a 'references'
0239: // field collection, this is only valid for 'C' class objects
0240: // and references contain '..._d1' object --> 'a'
0241: crit = new Criteria();
0242: crit.addLike("name", name + "_%");
0243: crit.addEqualTo("ref.ref.references.name", name + "_d1");
0244: crit.addPathClass("ref", ObjB.class);
0245: crit.addPathClass("ref.ref", ObjC.class);
0246: q = QueryFactory.newQuery(ObjA.class, crit);
0247: result = broker.getCollectionByQuery(q);
0248: assertEquals(1, result.size());
0249: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0250: RefObject ref = (RefObject) iterator.next();
0251: assertTrue(ref instanceof ObjA);
0252: String refName = ref.getName();
0253: assertTrue(!(refName.indexOf(name) < 0));
0254: }
0255:
0256: // expect all A's with reference object named '_second%' and
0257: // which third level 'ref' has a 'references'
0258: // field collection, this is only valid for 'C' class objects
0259: // and references contain '..._second%' objects --> 'a'
0260: crit = new Criteria();
0261: crit.addLike("name", name + "_%");
0262: crit.addLike("ref.name", name + "_second%");
0263: crit.addLike("ref.ref.references.name", name + "_second%");
0264: crit.addPathClass("ref", ObjB.class);
0265: crit.addPathClass("ref.ref", ObjC.class);
0266: q = QueryFactory.newQuery(ObjA.class, crit);
0267: result = broker.getCollectionByQuery(q);
0268: assertEquals(1, result.size());
0269: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0270: RefObject ref = (RefObject) iterator.next();
0271: assertTrue(ref instanceof ObjA);
0272: String refName = ref.getName();
0273: assertTrue(!(refName.indexOf(name) < 0));
0274: }
0275: }
0276:
0277: /**
0278: * Test the usage of interface as class-ref in collection-descriptor
0279: * when using inheritance.
0280: */
0281: public void testInterfaceAsCollectionRef_2() {
0282: // if(skipKnownIssueProblem("query using path via reference, like 'ref1.ref2.name'")) return;
0283: String name = "testQueryWithCollectionRef_"
0284: + System.currentTimeMillis();
0285: RefObject a = new ObjA();
0286: RefObject a2 = new ObjA();
0287: RefObject b = new ObjB();
0288: RefObject b2 = new ObjB();
0289: // this object has a 1:n relation
0290: ObjC c = new ObjC();
0291: // only used in the c object reference collection
0292: RefObject d = new ObjC();
0293:
0294: c.setName(name + "_third");
0295: b.setName(name + "_second_1");
0296: b2.setName(name + "_second_2");
0297: a.setName(name + "_first_1");
0298: a2.setName(name + "_first_2");
0299: d.setName(name + "_none");
0300:
0301: a.setRef(b);
0302: b.setRef(c);
0303: a2.setRef(b2);
0304:
0305: List refList = new ArrayList();
0306: refList.add(a);
0307: refList.add(b2);
0308: refList.add(d);
0309: c.setReferences(refList);
0310:
0311: broker.beginTransaction();
0312: broker.store(a);
0313: broker.store(a2);
0314: broker.commitTransaction();
0315:
0316: // check existence of objects
0317: Criteria crit = new Criteria();
0318: crit.addEqualTo("name", name + "_third");
0319: Query q = QueryFactory.newQuery(RefObject.class, crit);
0320: Collection result = broker.getCollectionByQuery(q);
0321: assertEquals(1, result.size());
0322: ObjC newC = (ObjC) result.iterator().next();
0323: assertNotNull(newC.getReferences());
0324: assertEquals(3, newC.getReferences().size());
0325:
0326: // test n-level depth
0327: //*****************************************
0328: crit = new Criteria();
0329: crit.addEqualTo("ref.ref.name", name + "_third");
0330: q = QueryFactory.newQuery(ObjA.class, crit);
0331: result = broker.getCollectionByQuery(q);
0332: assertEquals(1, result.size());
0333: //*****************************************
0334:
0335: crit = new Criteria();
0336: crit.addLike("references.name", name + "_first%");
0337: q = QueryFactory.newQuery(ObjC.class, crit);
0338: result = broker.getCollectionByQuery(q);
0339: assertEquals(1, result.size());
0340:
0341: // expect all A's with name "_first_2" or with second
0342: // level 'ref' "_third" in this case object 'a' and 'a2'
0343: crit = new Criteria();
0344: crit.addEqualTo("name", name + "_first_2");
0345: Criteria critOr = new Criteria();
0346: critOr.addEqualTo("ref.ref.name", name + "_third");
0347: crit.addOrCriteria(critOr);
0348: q = QueryFactory.newQuery(ObjA.class, crit);
0349: result = broker.getCollectionByQuery(q);
0350: assertEquals(2, result.size());
0351: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0352: RefObject ref = (RefObject) iterator.next();
0353: assertTrue(ref instanceof ObjA);
0354: String refName = ref.getName();
0355: assertTrue(!(refName.indexOf(name) < 0));
0356: }
0357:
0358: // expect all A's which second level 'ref' is "_third"
0359: // in this case object 'a'
0360: crit = new Criteria();
0361: crit.addLike("name", name + "_%");
0362: Criteria critAnd = new Criteria();
0363: critAnd.addEqualTo("ref.ref.name", name + "_third");
0364: crit.addAndCriteria(critAnd);
0365: q = QueryFactory.newQuery(ObjA.class, crit);
0366: result = broker.getCollectionByQuery(q);
0367: assertEquals(1, result.size());
0368: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0369: RefObject ref = (RefObject) iterator.next();
0370: assertTrue(ref instanceof ObjA);
0371: String refName = ref.getName();
0372: assertTrue(!(refName.indexOf(name) < 0));
0373: }
0374:
0375: // expect all A's with first level 'ref' "_second%"
0376: // in this case object 'a' and 'a2'
0377: crit = new Criteria();
0378: crit.addLike("ref.name", name + "_second%");
0379: critAnd = new Criteria();
0380: critAnd.addLike("name", name + "%");
0381: crit.addAndCriteria(critAnd);
0382: q = QueryFactory.newQuery(ObjA.class, crit);
0383: result = broker.getCollectionByQuery(q);
0384: assertEquals(2, result.size());
0385: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0386: RefObject ref = (RefObject) iterator.next();
0387: assertTrue(ref instanceof ObjA);
0388: String refName = ref.getName();
0389: assertTrue(!(refName.indexOf(name) < 0));
0390: }
0391: }
0392:
0393: public void testDeepPathQuery() {
0394: // if(skipKnownIssueProblem("query using path via reference, like 'ref1.ref2.name'")) return;
0395: String name = "testDeepPathQuery_" + System.currentTimeMillis();
0396: RefObject a = new ObjA();
0397: RefObject a2 = new ObjA();
0398: RefObject b = new ObjB();
0399: RefObject b2 = new ObjB();
0400: ObjC c = new ObjC();
0401:
0402: c.setName(name + "_third");
0403: b.setName(name + "_second_1");
0404: b2.setName(name + "_second_2");
0405: a.setName(name + "_first_1");
0406: a2.setName(name + "_first_2");
0407:
0408: a.setRef(b);
0409: b.setRef(c);
0410: a2.setRef(b2);
0411:
0412: List refList = new ArrayList();
0413: refList.add(a);
0414: refList.add(b2);
0415: c.setReferences(refList);
0416:
0417: broker.beginTransaction();
0418: broker.store(a);
0419: broker.store(a2);
0420: broker.commitTransaction();
0421:
0422: // check existence of objects
0423: Criteria crit = new Criteria();
0424: crit.addLike("name", name + "%");
0425: Query q = QueryFactory.newQuery(ObjA.class, crit);
0426: Collection result = broker.getCollectionByQuery(q);
0427: assertEquals(2, result.size());
0428:
0429: // check existence of object
0430: crit = new Criteria();
0431: crit.addLike("name", name + "_third%");
0432: q = QueryFactory.newQuery(ObjC.class, crit);
0433: result = broker.getCollectionByQuery(q);
0434: assertEquals(1, result.size());
0435:
0436: // check existence of object
0437: crit = new Criteria();
0438: crit.addLike("name", name + "%");
0439: q = QueryFactory.newQuery(ObjC.class, crit);
0440: result = broker.getCollectionByQuery(q);
0441: assertEquals(1, result.size());
0442:
0443: // test one level depth
0444: crit = new Criteria();
0445: crit.addLike("ref.name", name + "_second%");
0446: q = QueryFactory.newQuery(ObjA.class, crit);
0447: result = broker.getCollectionByQuery(q);
0448: assertEquals(2, result.size());
0449:
0450: // check existence of objects
0451: crit = new Criteria();
0452: crit.addLike("name", name + "%");
0453: q = QueryFactory.newQuery(RefObject.class, crit);
0454: result = broker.getCollectionByQuery(q);
0455: assertEquals(5, result.size());
0456:
0457: // test n-level depth
0458: //*****************************************
0459: crit = new Criteria();
0460: crit.addEqualTo("ref.ref.name", name + "_third");
0461: q = QueryFactory.newQuery(ObjA.class, crit);
0462: result = broker.getCollectionByQuery(q);
0463: assertEquals(1, result.size());
0464: //*****************************************
0465:
0466: // similar but more complex query
0467: crit = new Criteria();
0468: crit.addEqualTo("name", name + "_first_2");
0469: Criteria critOr = new Criteria();
0470: critOr.addEqualTo("ref.ref.name", name + "_third");
0471: crit.addOrCriteria(critOr);
0472: q = QueryFactory.newQuery(ObjA.class, crit);
0473: result = broker.getCollectionByQuery(q);
0474: assertEquals(2, result.size());
0475: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0476: RefObject ref = (RefObject) iterator.next();
0477: assertTrue(ref instanceof ObjA);
0478: String refName = ref.getName();
0479: assertTrue(!(refName.indexOf(name) < 0));
0480: }
0481: }
0482:
0483: public void testAutoUpdateDeleteSettings() {
0484: changeRepositoryAutoSetting("ref", true, false, false);
0485: ObjectReferenceDescriptor ord = broker.getClassDescriptor(
0486: Repository.class).getObjectReferenceDescriptorByName(
0487: "ref");
0488: assertEquals(ObjectReferenceDescriptor.CASCADE_LINK, ord
0489: .getCascadingStore());
0490: assertEquals(ObjectReferenceDescriptor.CASCADE_NONE, ord
0491: .getCascadingDelete());
0492: assertEquals(false, ord.getCascadeStore());
0493: assertEquals(false, ord.getCascadeDelete());
0494:
0495: changeRepositoryAutoSetting("ref", true, true, true);
0496: ord = broker.getClassDescriptor(Repository.class)
0497: .getObjectReferenceDescriptorByName("ref");
0498: assertEquals(ObjectReferenceDescriptor.CASCADE_OBJECT, ord
0499: .getCascadingStore());
0500: assertEquals(ObjectReferenceDescriptor.CASCADE_OBJECT, ord
0501: .getCascadingDelete());
0502: assertEquals(true, ord.getCascadeStore());
0503: assertEquals(true, ord.getCascadeDelete());
0504: }
0505:
0506: /**
0507: * not really a reference test, here we check handling of objects
0508: * with multiple PK fields. Such an object was used in following
0509: * reference tests.
0510: */
0511: public void testHandlingOfMultiplePKFields() throws Exception {
0512: String timestamp = "testLookupWithMultiplePK_"
0513: + System.currentTimeMillis();
0514: String regionName = "baden_" + timestamp;
0515: String countryName = "germany_" + timestamp;
0516: /*
0517: Wine has a 1:1 reference with Region, we set the reference object in
0518: Wine class. We don't set the FK fields in Wine, this should be done by OJB
0519: automatic
0520: */
0521: Region region = new Region(regionName, countryName, "original");
0522:
0523: broker.beginTransaction();
0524: broker.store(region);
0525: broker.commitTransaction();
0526:
0527: Identity oid = new Identity(region, broker);
0528: broker.clearCache();
0529: Region loadedRegion = (Region) broker.getObjectByIdentity(oid);
0530:
0531: assertNotNull(loadedRegion);
0532: assertEquals(region.getName(), loadedRegion.getName());
0533:
0534: loadedRegion.setDescription("update_1");
0535: broker.beginTransaction();
0536: broker.store(loadedRegion);
0537: broker.commitTransaction();
0538: broker.clearCache();
0539: loadedRegion = (Region) broker.getObjectByIdentity(oid);
0540: assertNotNull(loadedRegion);
0541: assertEquals("update_1", loadedRegion.getDescription());
0542:
0543: loadedRegion.setDescription("update_2");
0544: broker.beginTransaction();
0545: broker.store(loadedRegion);
0546: broker.commitTransaction();
0547: broker.clearCache();
0548: loadedRegion = (Region) broker.getObjectByIdentity(oid);
0549: assertNotNull(loadedRegion);
0550: assertEquals("update_2", loadedRegion.getDescription());
0551:
0552: Criteria crit = new Criteria();
0553: crit.addLike("name", regionName);
0554: Query q = QueryFactory.newQuery(Region.class, crit);
0555: Collection result = broker.getCollectionByQuery(q);
0556: assertEquals(1, result.size());
0557: }
0558:
0559: public void testStoreWithMultiplePK_1() throws Exception {
0560: String timestamp = "testStoreWithMultiplePK_1_"
0561: + System.currentTimeMillis();
0562: String regionName = "baden_1" + timestamp;
0563: String countryName = "germany_1" + timestamp;
0564: Region region = new Region(regionName, countryName, "brrr");
0565: Wine wine = new Wine(timestamp, "silvaner", "2003", regionName,
0566: countryName);
0567:
0568: broker.beginTransaction();
0569: broker.store(region);
0570: broker.commitTransaction();
0571:
0572: /*
0573: class Wine has a 1:1 reference with Region, we set set the FK in Wine but don't
0574: set the Region reference object in Wine. But retriveAllReferences materialize
0575: the reference object before store.
0576: */
0577: broker.beginTransaction();
0578: broker.retrieveAllReferences(wine);
0579: broker.store(wine);
0580: broker.commitTransaction();
0581:
0582: Identity oid = new Identity(wine, broker);
0583: broker.clearCache();
0584: Wine loadedWine = (Wine) broker.getObjectByIdentity(oid);
0585: assertNotNull(loadedWine);
0586: assertEquals(wine.getGrape(), loadedWine.getGrape());
0587: assertNotNull(loadedWine.getRegion());
0588: assertEquals(wine.getRegion().getCountry(), loadedWine
0589: .getRegion().getCountry());
0590: }
0591:
0592: public void testStoreWithMultiplePK_2() throws Exception {
0593: String timestamp = "testStoreWithMultiplePK_2_"
0594: + System.currentTimeMillis();
0595: String regionName = "baden_2" + timestamp;
0596: String countryName = "germany_2" + timestamp;
0597: /*
0598: Wine has a 1:1 reference with Region, we set the reference object in
0599: Wine class. We don't set the FK fields in Wine, this should be done by OJB
0600: automatic
0601: */
0602: Region region = new Region(regionName, countryName, "brrr");
0603: Wine wine = new Wine(timestamp, "silvaner", "2003", null, null);
0604: wine.setRegion(region);
0605:
0606: broker.beginTransaction();
0607: broker.store(region);
0608: broker.commitTransaction();
0609:
0610: broker.beginTransaction();
0611: broker.store(wine);
0612: broker.commitTransaction();
0613:
0614: Identity oid = new Identity(wine, broker);
0615: broker.clearCache();
0616: Wine loadedWine = (Wine) broker.getObjectByIdentity(oid);
0617: assertNotNull(loadedWine);
0618: assertEquals(wine.getGrape(), loadedWine.getGrape());
0619: assertNotNull(loadedWine.getRegion());
0620: assertEquals(wine.getRegion().getCountry(), loadedWine
0621: .getRegion().getCountry());
0622: }
0623:
0624: public void testDeleteWithMultiplePK() {
0625: String timestamp = "testDeleteWithMultiplePK_"
0626: + System.currentTimeMillis();
0627: String regionName = "baden_2" + timestamp;
0628: String countryName = "germany_2" + timestamp;
0629:
0630: /*
0631: Wine has a 1:1 reference with Region, we set the reference object in
0632: Wine class. We don't set the FK fields in Wine, this should be done by OJB
0633: automatic
0634: */
0635: Region region = new Region(regionName, countryName, "brrr");
0636: Wine wine = new Wine(timestamp, "silvaner", "2003", null, null);
0637: wine.setRegion(region);
0638:
0639: broker.beginTransaction();
0640: broker.store(region);
0641: broker.commitTransaction();
0642:
0643: broker.beginTransaction();
0644: broker.store(wine);
0645: broker.commitTransaction();
0646:
0647: Identity oid = new Identity(wine, broker);
0648: Identity oidRegion = new Identity(region, broker);
0649: broker.clearCache();
0650: Wine loadedWine = (Wine) broker.getObjectByIdentity(oid);
0651: assertNotNull(loadedWine);
0652: assertEquals(wine.getGrape(), loadedWine.getGrape());
0653: assertNotNull(loadedWine.getRegion());
0654: assertEquals(wine.getRegion().getCountry(), loadedWine
0655: .getRegion().getCountry());
0656:
0657: broker.beginTransaction();
0658: broker.delete(wine);
0659: broker.commitTransaction();
0660:
0661: loadedWine = (Wine) broker.getObjectByIdentity(oid);
0662: assertNull(loadedWine);
0663: Region loadedregion = (Region) broker
0664: .getObjectByIdentity(oidRegion);
0665: assertNotNull(loadedregion);
0666:
0667: broker.clearCache();
0668: loadedWine = (Wine) broker.getObjectByIdentity(oid);
0669: assertNull(loadedWine);
0670: loadedregion = (Region) broker.getObjectByIdentity(oidRegion);
0671: assertNotNull(loadedregion);
0672: }
0673:
0674: public void testStoreWithMultiplePK_3() throws Exception {
0675: String timestamp = "testStoreWithMultiplePK_3_"
0676: + System.currentTimeMillis();
0677: String regionName = "baden_3" + timestamp;
0678: String countryName = "germany_3" + timestamp;
0679: /*
0680: Wine has a 1:1 reference with Region, we set set the FK fields
0681: of an existing Region object in Wine
0682: but don't set the Region reference object itself in Wine object
0683: */
0684: Region region = new Region(regionName, countryName, "brrr");
0685: Wine wine = new Wine(timestamp, "silvaner", "2003", regionName,
0686: countryName);
0687: wine.setRegion(region);
0688:
0689: broker.beginTransaction();
0690: broker.store(region);
0691: broker.commitTransaction();
0692:
0693: broker.beginTransaction();
0694: broker.store(wine);
0695: broker.commitTransaction();
0696:
0697: Identity oid = new Identity(wine, broker);
0698: broker.clearCache();
0699: Wine loadedWine = (Wine) broker.getObjectByIdentity(oid);
0700: assertNotNull(loadedWine);
0701: assertEquals(wine.getGrape(), loadedWine.getGrape());
0702: assertNotNull(loadedWine.getRegion());
0703: assertEquals(wine.getRegion().getCountry(), loadedWine
0704: .getRegion().getCountry());
0705: }
0706:
0707: public void testStoreReferencesMappedToSameTable() {
0708: String referenceNamePrefix = "testStoreReferencesMappedToSameTable"
0709: + System.currentTimeMillis();
0710: Repository[] repository = prepareRepository(referenceNamePrefix);
0711:
0712: broker.beginTransaction();
0713: broker.store(repository[0]);
0714: broker.store(repository[1]);
0715: broker.store(repository[2]);
0716: broker.commitTransaction();
0717:
0718: broker.clearCache();
0719: Identity oid = new Identity(repository[0], broker);
0720:
0721: Repository rep = (Repository) broker.getObjectByIdentity(oid);
0722: assertNotNull(rep.getRef());
0723: assertNotNull(rep.getRefA());
0724: assertNotNull(rep.getRefB());
0725: // lookup reference name, set in prepareRepository method
0726: assertEquals(rep.getRefB().getRefNameB(), REF_TEST_STRING);
0727: }
0728:
0729: public void testGetReferencesByIdentityMappedToSameTable() {
0730: String referenceNamePrefix = "testGetReferencesByIdentityMappedToSameTable"
0731: + System.currentTimeMillis();
0732: Repository[] repository = prepareRepository(referenceNamePrefix);
0733:
0734: broker.beginTransaction();
0735: broker.store(repository[0]);
0736: broker.store(repository[1]);
0737: broker.store(repository[2]);
0738: broker.commitTransaction();
0739:
0740: assertNotNull(repository[0].getRef());
0741: assertNotNull(repository[0].getRefA());
0742: assertNotNull(repository[0].getRefB());
0743:
0744: Identity oid_ref = new Identity(repository[0].getRef(), broker);
0745: Identity oid_refA = new Identity(repository[0].getRefA(),
0746: broker);
0747: Identity oid_refB = new Identity(repository[0].getRefB(),
0748: broker);
0749:
0750: broker.clearCache();
0751: Object result;
0752: result = broker.getObjectByIdentity(oid_ref);
0753: assertTrue(result instanceof Reference);
0754: result = broker.getObjectByIdentity(oid_refA);
0755: assertTrue(result instanceof ReferenceA);
0756: result = broker.getObjectByIdentity(oid_refB);
0757: assertTrue(result instanceof ReferenceB);
0758:
0759: broker.clearCache();
0760: Identity repOID = new Identity(repository[0], broker);
0761: Repository repositoryObj = (Repository) broker
0762: .getObjectByIdentity(repOID);
0763: assertNotNull(repositoryObj);
0764: ReferenceBIF refB = repositoryObj.getRefB();
0765: assertNotNull(refB);
0766: assertEquals(refB.getRefNameB(), REF_TEST_STRING);
0767: }
0768:
0769: public void testQueryReferencesMappedToSameTable() {
0770: String referenceNamePrefix = "testQueryReferencesMappedToSameTable"
0771: + System.currentTimeMillis();
0772: Repository[] repository = prepareRepository(referenceNamePrefix);
0773:
0774: broker.beginTransaction();
0775: broker.store(repository[0]);
0776: broker.store(repository[1]);
0777: broker.store(repository[2]);
0778: broker.commitTransaction();
0779:
0780: broker.clearCache();
0781: Criteria criteria = new Criteria();
0782: criteria.addLike("name", referenceNamePrefix + "%");
0783: Query query = QueryFactory
0784: .newQuery(ReferenceIF.class, criteria);
0785: Collection result = broker.getCollectionByQuery(query);
0786:
0787: assertEquals("Wrong number of References", 9, result.size());
0788: int ref_count = 0;
0789: int refA_count = 0;
0790: int refB_count = 0;
0791: Iterator it = result.iterator();
0792: Object obj;
0793: while (it.hasNext()) {
0794: obj = it.next();
0795: if (obj instanceof ReferenceA)
0796: refA_count++;
0797: else if (obj instanceof ReferenceB)
0798: refB_count++;
0799: else if (obj instanceof Reference)
0800: ref_count++;
0801: }
0802: assertEquals("Wrong number of RefernceA", 3, refA_count);
0803: assertEquals("Wrong number of RefernceB", 3, refB_count);
0804: assertEquals("Wrong number of Refernce", 3, ref_count);
0805:
0806: result = broker.getCollectionByQuery(query);
0807: it = result.iterator();
0808: while (it.hasNext()) {
0809: obj = it.next();
0810: if (obj instanceof ReferenceA) {
0811: assertNotNull(((ReferenceA) obj).getRefNameA());
0812: assertNotNull(((ReferenceA) obj).getName());
0813: } else if (obj instanceof ReferenceB) {
0814: assertNotNull(((ReferenceB) obj).getRefNameB());
0815: assertNotNull(((ReferenceB) obj).getName());
0816: } else if (obj instanceof Reference) {
0817: assertNotNull(((Reference) obj).getName());
0818: }
0819: }
0820: }
0821:
0822: public void testDeleteReferencesMappedToSameTable() {
0823: String referenceNamePrefix = "testDeleteReferencesMappedToSameTable"
0824: + System.currentTimeMillis();
0825: Repository[] repository = prepareRepository(referenceNamePrefix);
0826:
0827: broker.beginTransaction();
0828: broker.store(repository[0]);
0829: broker.store(repository[1]);
0830: broker.store(repository[2]);
0831: broker.commitTransaction();
0832:
0833: Criteria criteria = new Criteria();
0834: criteria.addLike("name", referenceNamePrefix + "%");
0835: Query query = QueryFactory
0836: .newQuery(ReferenceIF.class, criteria);
0837: Collection result = broker.getCollectionByQuery(query);
0838:
0839: assertEquals("Wrong number of References", 9, result.size());
0840:
0841: broker.beginTransaction();
0842: broker.delete(repository[0]);
0843: broker.delete(repository[1]);
0844: broker.delete(repository[2]);
0845: broker.commitTransaction();
0846:
0847: result = broker.getCollectionByQuery(query);
0848: assertEquals("Wrong number of References", 0, result.size());
0849: }
0850:
0851: public void testDeleteReferencesMappedToSameTable_2() {
0852: String referenceNamePrefix = "testDeleteReferencesMappedToSameTable_2"
0853: + System.currentTimeMillis();
0854: changeRepositoryAutoSetting("ref", true, true, false);
0855: changeRepositoryAutoSetting("refA", true, true, false);
0856: changeRepositoryAutoSetting("refB", true, true, false);
0857:
0858: Repository[] repository = prepareRepository(referenceNamePrefix);
0859:
0860: broker.beginTransaction();
0861: broker.store(repository[0]);
0862: broker.store(repository[1]);
0863: broker.store(repository[2]);
0864: broker.commitTransaction();
0865:
0866: Criteria criteria = new Criteria();
0867: criteria.addLike("name", referenceNamePrefix + "%");
0868: Query query = QueryFactory
0869: .newQuery(ReferenceIF.class, criteria);
0870: Collection result = broker.getCollectionByQuery(query);
0871:
0872: assertEquals("Wrong number of References", 9, result.size());
0873:
0874: broker.beginTransaction();
0875: broker.delete(repository[0]);
0876: broker.delete(repository[1]);
0877: broker.delete(repository[2]);
0878: broker.commitTransaction();
0879:
0880: result = broker.getCollectionByQuery(query);
0881: assertEquals("Wrong number of References", 9, result.size());
0882: }
0883:
0884: public void testDeleteReferencesMappedToSameTable_3() {
0885: String referenceNamePrefix = "testDeleteReferencesMappedToSameTable_3"
0886: + System.currentTimeMillis();
0887: changeRepositoryAutoSetting("ref", true, true, false);
0888: changeRepositoryAutoSetting("refA", true, true, true);
0889: changeRepositoryAutoSetting("refB", true, true, false);
0890:
0891: Repository[] repository = prepareRepository(referenceNamePrefix);
0892:
0893: broker.beginTransaction();
0894: broker.store(repository[0]);
0895: broker.store(repository[1]);
0896: broker.store(repository[2]);
0897: broker.commitTransaction();
0898:
0899: Criteria criteria = new Criteria();
0900: criteria.addLike("name", referenceNamePrefix + "%");
0901: Query query = QueryFactory
0902: .newQuery(ReferenceIF.class, criteria);
0903: Collection result = broker.getCollectionByQuery(query);
0904:
0905: assertEquals("Wrong number of References", 9, result.size());
0906:
0907: broker.beginTransaction();
0908: broker.delete(repository[0]);
0909: broker.delete(repository[1]);
0910: broker.delete(repository[2]);
0911: broker.commitTransaction();
0912:
0913: result = broker.getCollectionByQuery(query);
0914: assertEquals("Wrong number of References", 6, result.size());
0915: }
0916:
0917: private void changeRepositoryAutoSetting(String attributeName,
0918: boolean retrieve, int update, int delete) {
0919: ClassDescriptor cld = broker
0920: .getClassDescriptor(Repository.class);
0921: ObjectReferenceDescriptor ord = cld
0922: .getObjectReferenceDescriptorByName(attributeName);
0923: ord.setCascadeRetrieve(retrieve);
0924: ord.setCascadingStore(update);
0925: ord.setCascadingDelete(delete);
0926: }
0927:
0928: private void changeRepositoryAutoSetting(String attributeName,
0929: boolean retrieve, boolean update, boolean delete) {
0930: ClassDescriptor cld = broker
0931: .getClassDescriptor(Repository.class);
0932: ObjectReferenceDescriptor ord = cld
0933: .getObjectReferenceDescriptorByName(attributeName);
0934: ord.setCascadeRetrieve(retrieve);
0935: ord.setCascadeStore(update);
0936: ord.setCascadeDelete(delete);
0937: }
0938:
0939: /**
0940: * This test does the same as the {@link #testRepositoryFKStore},
0941: * but the used mapping data differ.
0942: * {@link RepositoryFK} defines all the reference fields as
0943: * primary key in field-descriptors. Further on the used
0944: * database table declares the reference fields as PK too.
0945: * Based on a user post:
0946: * > The following fails to be stored by PersistenceBroker:
0947: >
0948: > I have a class ACL which has two primary keys: objectId and userFK, and
0949: > userFK is also a foreign key tied to a reference of type User. If I do
0950: this:
0951: >
0952: > persistentBroker.beginTransaction();
0953: > ACL acl = new ACL();
0954: > acl.setObjectId( 100 );
0955: > acl.setUser( currentUser );
0956: > persistentBroker.store(acl);
0957: > persistentBroker.commitTransaction();
0958: >
0959: > Acl will not be saved. The reason seems to be because in the storeToDb()
0960: > method of the PersistentBroker, there first comes an assertion of the
0961: > PrimaryKeys and afterwards comes the assignment of all the foreign keys.
0962: In
0963: > the scenario above the assertion of the primary keys will fail, because
0964: the
0965: > userFK has not been assigned yet, so we have an incomplete set of primary
0966: > keys. This does work with the ODMG layer, probably because of a different
0967: > sequence of events during the storing of the object.
0968: >
0969: > I wonder if there should be a check whether a primary key is shared by the
0970: > foreign key and allow that assignment before the assertion of the primary
0971: > keys is performed. Any ideas?
0972: >
0973: > Cheers,
0974: > --Bill.
0975: */
0976: public void testRepositoryFKStore() {
0977: String referenceNamePrefix = "testReferenceStore"
0978: + System.currentTimeMillis();
0979: RepositoryFK[] repository = prepareRepositoryFK(referenceNamePrefix);
0980:
0981: broker.beginTransaction();
0982: broker.store(repository[0]);
0983: broker.store(repository[1]);
0984: broker.store(repository[2]);
0985: broker.commitTransaction();
0986:
0987: Identity oid = new Identity(repository[0], broker);
0988: RepositoryFK repFK = (RepositoryFK) broker
0989: .getObjectByIdentity(oid);
0990:
0991: assertNotNull(
0992: "We should found a RepositoryFK object, but doesn't.",
0993: repFK);
0994: assertNotNull(repFK.getRef());
0995: assertNotNull(repFK.getRefA());
0996: assertNotNull(repFK.getRefB());
0997: }
0998:
0999: /**
1000: * this test case use an abstract class as reference
1001: * @throws Exception
1002: */
1003: public void testAbstractReferenceStore() throws Exception {
1004: String name = "testAbstractReferenceStore_"
1005: + System.currentTimeMillis();
1006: /*
1007: create some animals
1008: */
1009: Bird bird_1 = new Bird();
1010: bird_1.setName(name);
1011: bird_1.setWingspan(new Double(2.33));
1012: Bird bird_2 = new Bird();
1013: bird_2.setName(name);
1014: bird_2.setWingspan(new Double(0.99));
1015:
1016: Mammal mammal_1 = new Mammal();
1017: mammal_1.setName(name);
1018: mammal_1.setHeight(new Double(1.88));
1019: Mammal mammal_2 = new Mammal();
1020: mammal_2.setName(name);
1021: mammal_2.setHeight(new Double(19.13));
1022:
1023: Fish fish_1 = new Fish();
1024: fish_1.setName(name);
1025: fish_1.setLength(new Double(0.033));
1026: Fish fish_2 = new Fish();
1027: fish_2.setName(name);
1028: fish_2.setLength(new Double(37.89));
1029:
1030: Repository rep_1 = new Repository();
1031: rep_1.setAnimal(mammal_1);
1032: Repository rep_2 = new Repository();
1033: rep_2.setAnimal(bird_1);
1034: Repository rep_3 = new Repository();
1035: rep_3.setAnimal(fish_1);
1036:
1037: /*
1038: store Repository instances and dummy animals
1039: */
1040: broker.beginTransaction();
1041: // store some dummy objects
1042: broker.store(bird_2);
1043: broker.store(mammal_2);
1044: broker.store(fish_2);
1045: // now store the main objects
1046: broker.store(rep_1);
1047: broker.store(rep_2);
1048: broker.store(rep_3);
1049: broker.commitTransaction();
1050:
1051: Identity oid_mammal = new Identity(mammal_1, broker);
1052: Identity oid_bird = new Identity(bird_1, broker);
1053: Identity oid_fish = new Identity(fish_1, broker);
1054: Identity oid_rep_1 = new Identity(rep_1, broker);
1055: Identity oid_rep_2 = new Identity(rep_2, broker);
1056: Identity oid_rep_3 = new Identity(rep_3, broker);
1057:
1058: broker.clearCache();
1059: // check the references
1060: Mammal lookup_mammal = (Mammal) broker
1061: .getObjectByIdentity(oid_mammal);
1062: Bird lookup_bird = (Bird) broker.getObjectByIdentity(oid_bird);
1063: Fish lookup_fish = (Fish) broker.getObjectByIdentity(oid_fish);
1064: assertEquals(mammal_1, lookup_mammal);
1065: assertEquals(bird_1, lookup_bird);
1066: assertEquals(fish_1, lookup_fish);
1067:
1068: broker.clearCache();
1069: // check the main objects
1070: Repository lookup_rep_1 = (Repository) broker
1071: .getObjectByIdentity(oid_rep_1);
1072: Repository lookup_rep_2 = (Repository) broker
1073: .getObjectByIdentity(oid_rep_2);
1074: Repository lookup_rep_3 = (Repository) broker
1075: .getObjectByIdentity(oid_rep_3);
1076:
1077: assertNotNull(lookup_rep_1.getAnimal());
1078: assertTrue("Expected instance of Mammal, found "
1079: + lookup_rep_1.getAnimal(),
1080: lookup_rep_1.getAnimal() instanceof Mammal);
1081: assertEquals(mammal_1, lookup_rep_1.getAnimal());
1082:
1083: assertNotNull(lookup_rep_2.getAnimal());
1084: assertTrue("Expected instance of Bird, found "
1085: + lookup_rep_2.getAnimal(),
1086: lookup_rep_2.getAnimal() instanceof Bird);
1087: assertEquals(bird_1, lookup_rep_2.getAnimal());
1088:
1089: assertNotNull(lookup_rep_3.getAnimal());
1090: assertTrue("Expected instance of Fish, found "
1091: + lookup_rep_3.getAnimal(),
1092: lookup_rep_3.getAnimal() instanceof Fish);
1093: assertEquals(fish_1, lookup_rep_3.getAnimal());
1094: }
1095:
1096: public void testAbstractReferenceQuery() throws Exception {
1097: String name = "testAbstractReferenceQuery_"
1098: + System.currentTimeMillis();
1099: /*
1100: create some animals
1101: */
1102: Bird bird_1 = new Bird();
1103: bird_1.setName(name);
1104: bird_1.setWingspan(new Double(2.33));
1105: Bird bird_2 = new Bird();
1106: bird_2.setName(name);
1107: bird_2.setWingspan(new Double(0.99));
1108:
1109: Mammal mammal_1 = new Mammal();
1110: mammal_1.setName(name);
1111: mammal_1.setHeight(new Double(1.88));
1112: Mammal mammal_2 = new Mammal();
1113: mammal_2.setName(name);
1114: mammal_2.setHeight(new Double(19.13));
1115:
1116: Fish fish_1 = new Fish();
1117: fish_1.setName(name);
1118: fish_1.setLength(new Double(0.033));
1119: Fish fish_2 = new Fish();
1120: fish_2.setName(name);
1121: fish_2.setLength(new Double(37.89));
1122:
1123: Repository rep_1 = new Repository();
1124: rep_1.setAnimal(mammal_1);
1125: Repository rep_2 = new Repository();
1126: rep_2.setAnimal(bird_1);
1127: Repository rep_3 = new Repository();
1128: rep_3.setAnimal(fish_1);
1129:
1130: /*
1131: store Repository instances and dummy animals
1132: */
1133: broker.beginTransaction();
1134: // store some dummy objects
1135: broker.store(bird_2);
1136: broker.store(mammal_2);
1137: broker.store(fish_2);
1138: // now store the main objects
1139: broker.store(rep_1);
1140: broker.store(rep_2);
1141: broker.store(rep_3);
1142: broker.commitTransaction();
1143:
1144: Identity oid_rep_1 = new Identity(rep_1, broker);
1145: Identity oid_rep_2 = new Identity(rep_2, broker);
1146: Identity oid_rep_3 = new Identity(rep_3, broker);
1147:
1148: broker.clearCache();
1149: // check the main objects
1150: Repository lookup_rep_1 = (Repository) broker
1151: .getObjectByIdentity(oid_rep_1);
1152: Repository lookup_rep_2 = (Repository) broker
1153: .getObjectByIdentity(oid_rep_2);
1154: Repository lookup_rep_3 = (Repository) broker
1155: .getObjectByIdentity(oid_rep_3);
1156:
1157: assertNotNull(lookup_rep_1.getAnimal());
1158: assertTrue("Expected instance of Mammal, found "
1159: + lookup_rep_1.getAnimal(),
1160: lookup_rep_1.getAnimal() instanceof Mammal);
1161: assertEquals(mammal_1, lookup_rep_1.getAnimal());
1162:
1163: assertNotNull(lookup_rep_2.getAnimal());
1164: assertTrue("Expected instance of Bird, found "
1165: + lookup_rep_2.getAnimal(),
1166: lookup_rep_2.getAnimal() instanceof Bird);
1167: assertEquals(bird_1, lookup_rep_2.getAnimal());
1168:
1169: assertNotNull(lookup_rep_3.getAnimal());
1170: assertTrue("Expected instance of Fish, found "
1171: + lookup_rep_3.getAnimal(),
1172: lookup_rep_3.getAnimal() instanceof Fish);
1173: assertEquals(fish_1, lookup_rep_3.getAnimal());
1174:
1175: broker.clearCache();
1176: // query the references
1177: Criteria crit = new Criteria();
1178: crit.addEqualTo("name", name);
1179: Query query = QueryFactory.newQuery(Animal.class, crit);
1180: Collection result = broker.getCollectionByQuery(query);
1181: assertNotNull(result);
1182: int[] mammalBirdFish = new int[3];
1183: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
1184: Object o = iterator.next();
1185: if (o instanceof Mammal)
1186: ++mammalBirdFish[0];
1187: if (o instanceof Bird)
1188: ++mammalBirdFish[1];
1189: if (o instanceof Fish)
1190: ++mammalBirdFish[2];
1191: }
1192: assertEquals(2, mammalBirdFish[0]);
1193: assertEquals(2, mammalBirdFish[1]);
1194: assertEquals(2, mammalBirdFish[2]);
1195: }
1196:
1197: /**
1198: * this test case use an abstract class as reference
1199: * @throws Exception
1200: */
1201: public void testAbstractReferenceDelete() throws Exception {
1202: String name = "testAbstractReferenceDelete_"
1203: + System.currentTimeMillis();
1204: /*
1205: create some animals
1206: */
1207: Bird bird_1 = new Bird();
1208: bird_1.setName(name);
1209: bird_1.setWingspan(new Double(2.33));
1210: Bird bird_2 = new Bird();
1211: bird_2.setName(name);
1212: bird_2.setWingspan(new Double(0.99));
1213:
1214: Mammal mammal_1 = new Mammal();
1215: mammal_1.setName(name);
1216: mammal_1.setHeight(new Double(1.88));
1217: Mammal mammal_2 = new Mammal();
1218: mammal_2.setName(name);
1219: mammal_2.setHeight(new Double(19.13));
1220:
1221: Fish fish_1 = new Fish();
1222: fish_1.setName(name);
1223: fish_1.setLength(new Double(0.033));
1224: Fish fish_2 = new Fish();
1225: fish_2.setName(name);
1226: fish_2.setLength(new Double(37.89));
1227:
1228: Repository rep_1 = new Repository();
1229: rep_1.setAnimal(mammal_1);
1230: Repository rep_2 = new Repository();
1231: rep_2.setAnimal(bird_1);
1232: Repository rep_3 = new Repository();
1233: rep_3.setAnimal(fish_1);
1234:
1235: /*
1236: store Repository instances and dummy animals
1237: */
1238: broker.beginTransaction();
1239: // store some dummy objects
1240: broker.store(bird_2);
1241: broker.store(mammal_2);
1242: broker.store(fish_2);
1243: // now store the main objects
1244: broker.store(rep_1);
1245: broker.store(rep_2);
1246: broker.store(rep_3);
1247: broker.commitTransaction();
1248:
1249: Identity oid_rep_1 = new Identity(rep_1, broker);
1250: Identity oid_rep_2 = new Identity(rep_2, broker);
1251: Identity oid_rep_3 = new Identity(rep_3, broker);
1252:
1253: broker.clearCache();
1254: // check the main objects
1255: Repository lookup_rep_1 = (Repository) broker
1256: .getObjectByIdentity(oid_rep_1);
1257: Repository lookup_rep_2 = (Repository) broker
1258: .getObjectByIdentity(oid_rep_2);
1259: Repository lookup_rep_3 = (Repository) broker
1260: .getObjectByIdentity(oid_rep_3);
1261:
1262: assertNotNull(lookup_rep_1.getAnimal());
1263: assertTrue("Expected instance of Mammal, found "
1264: + lookup_rep_1.getAnimal(),
1265: lookup_rep_1.getAnimal() instanceof Mammal);
1266: assertEquals(mammal_1, lookup_rep_1.getAnimal());
1267:
1268: assertNotNull(lookup_rep_2.getAnimal());
1269: assertTrue("Expected instance of Bird, found "
1270: + lookup_rep_2.getAnimal(),
1271: lookup_rep_2.getAnimal() instanceof Bird);
1272: assertEquals(bird_1, lookup_rep_2.getAnimal());
1273:
1274: assertNotNull(lookup_rep_3.getAnimal());
1275: assertTrue("Expected instance of Fish, found "
1276: + lookup_rep_3.getAnimal(),
1277: lookup_rep_3.getAnimal() instanceof Fish);
1278: assertEquals(fish_1, lookup_rep_3.getAnimal());
1279:
1280: broker.clearCache();
1281:
1282: broker.beginTransaction();
1283: broker.delete(rep_1);
1284: broker.delete(rep_2);
1285: broker.delete(rep_3);
1286: broker.commitTransaction();
1287:
1288: lookup_rep_1 = (Repository) broker
1289: .getObjectByIdentity(oid_rep_1);
1290: lookup_rep_2 = (Repository) broker
1291: .getObjectByIdentity(oid_rep_2);
1292: lookup_rep_3 = (Repository) broker
1293: .getObjectByIdentity(oid_rep_3);
1294: assertNull(lookup_rep_1);
1295: assertNull(lookup_rep_2);
1296: assertNull(lookup_rep_3);
1297:
1298: }
1299:
1300: private Repository[] prepareRepository(String referenceNamePrefix) {
1301: Reference[] ref = new Reference[] {
1302: new Reference(referenceNamePrefix + "ref_1"),
1303: new Reference(referenceNamePrefix + "ref_2"),
1304: new Reference(referenceNamePrefix + "ref_3") };
1305: ReferenceA[] refA = new ReferenceA[] {
1306: new ReferenceA(referenceNamePrefix + "refA_1", "a1"),
1307: new ReferenceA(referenceNamePrefix + "refA_2", "a2"),
1308: new ReferenceA(referenceNamePrefix + "refA_3", "a3") };
1309: ReferenceB[] refB = new ReferenceB[] {
1310: new ReferenceB(referenceNamePrefix + "refB_1",
1311: REF_TEST_STRING),
1312: new ReferenceB(referenceNamePrefix + "refB_2", "b2"),
1313: new ReferenceB(referenceNamePrefix + "refB_3", "b3") };
1314:
1315: Repository repository = new Repository();
1316: repository.setRef(ref[0]);
1317: repository.setRefA(refA[0]);
1318: repository.setRefB(refB[0]);
1319:
1320: Repository repository2 = new Repository();
1321: repository2.setRef(ref[1]);
1322: repository2.setRefA(refA[1]);
1323: repository2.setRefB(refB[1]);
1324:
1325: Repository repository3 = new Repository();
1326: repository3.setRef(ref[2]);
1327: repository3.setRefA(refA[2]);
1328: repository3.setRefB(refB[2]);
1329:
1330: return new Repository[] { repository, repository2, repository3 };
1331: }
1332:
1333: private RepositoryFK[] prepareRepositoryFK(
1334: String referenceNamePrefix) {
1335: Reference[] ref = new Reference[] {
1336: new Reference(referenceNamePrefix + "ref_1"),
1337: new Reference(referenceNamePrefix + "ref_2"),
1338: new Reference(referenceNamePrefix + "ref_3") };
1339: ReferenceA[] refA = new ReferenceA[] {
1340: new ReferenceA(referenceNamePrefix + "refA_1", "a1"),
1341: new ReferenceA(referenceNamePrefix + "refA_2", "a2"),
1342: new ReferenceA(referenceNamePrefix + "refA_3", "a3") };
1343: ReferenceB[] refB = new ReferenceB[] {
1344: new ReferenceB(referenceNamePrefix + "refB_1",
1345: REF_TEST_STRING),
1346: new ReferenceB(referenceNamePrefix + "refB_2", "b2"),
1347: new ReferenceB(referenceNamePrefix + "refB_3", "b3") };
1348:
1349: RepositoryFK repository = new RepositoryFK();
1350: repository.setRef(ref[0]);
1351: repository.setRefA(refA[0]);
1352: repository.setRefB(refB[0]);
1353:
1354: RepositoryFK repository2 = new RepositoryFK();
1355: repository2.setRef(ref[1]);
1356: repository2.setRefA(refA[1]);
1357: repository2.setRefB(refB[1]);
1358:
1359: RepositoryFK repository3 = new RepositoryFK();
1360: repository3.setRef(ref[2]);
1361: repository3.setRefA(refA[2]);
1362: repository3.setRefB(refB[2]);
1363:
1364: return new RepositoryFK[] { repository, repository2,
1365: repository3 };
1366: }
1367:
1368: public void testMassOperations() {
1369: broker.beginTransaction();
1370: for (int i = 1; i < 100; i++) {
1371:
1372: ProductGroup pg = new ProductGroup();
1373: pg.setGroupName("1-1 test productgroup_" + i);
1374: broker.store(pg);
1375:
1376: Article article = Article.createInstance();
1377: article.setArticleName("1-1 test article_" + i);
1378: article.setProductGroupId(pg.getGroupId());
1379:
1380: broker.retrieveReference(article, "productGroup");
1381: broker.store(article);
1382: }
1383: broker.commitTransaction();
1384: }
1385:
1386: //***************************************************************************
1387: // Inner classes used by the test case
1388: //***************************************************************************
1389:
1390: public static class Repository implements Serializable {
1391: private Integer repId;
1392:
1393: private Integer refId;
1394: private Integer refAId;
1395: private Integer refBId;
1396:
1397: private ReferenceIF ref;
1398: private ReferenceAIF refA;
1399: private ReferenceBIF refB;
1400:
1401: private Animal animal;
1402: private Integer animalId;
1403:
1404: public Repository() {
1405: }
1406:
1407: public Integer getRefId() {
1408: return refId;
1409: }
1410:
1411: public void setRefId(Integer refId) {
1412: this .refId = refId;
1413: }
1414:
1415: public Integer getRefAId() {
1416: return refAId;
1417: }
1418:
1419: public void setRefAId(Integer refAId) {
1420: this .refAId = refAId;
1421: }
1422:
1423: public Integer getRefBId() {
1424: return refBId;
1425: }
1426:
1427: public void setRefBId(Integer refBId) {
1428: this .refBId = refBId;
1429: }
1430:
1431: public Animal getAnimal() {
1432: return animal;
1433: }
1434:
1435: public void setAnimal(Animal animal) {
1436: this .animal = animal;
1437: }
1438:
1439: public Integer getAnimalId() {
1440: return animalId;
1441: }
1442:
1443: public void setAnimalId(Integer animalId) {
1444: this .animalId = animalId;
1445: }
1446:
1447: public ReferenceIF getRef() {
1448: return ref;
1449: }
1450:
1451: public void setRef(ReferenceIF ref) {
1452: this .ref = ref;
1453: }
1454:
1455: public ReferenceAIF getRefA() {
1456: return refA;
1457: }
1458:
1459: public void setRefA(ReferenceAIF refA) {
1460: this .refA = refA;
1461: }
1462:
1463: public ReferenceBIF getRefB() {
1464: return refB;
1465: }
1466:
1467: public void setRefB(ReferenceBIF refB) {
1468: this .refB = refB;
1469: }
1470:
1471: public Integer getRepId() {
1472: return repId;
1473: }
1474:
1475: public void setRepId(Integer repId) {
1476: this .repId = repId;
1477: }
1478: }
1479:
1480: //***************************************************************
1481: // classes mapped to one table
1482: //***************************************************************
1483:
1484: public static class RepositoryFK extends Repository {
1485: }
1486:
1487: public static interface ReferenceIF extends Serializable {
1488: Integer getRefId();
1489:
1490: void setRefId(Integer refId);
1491:
1492: String getName();
1493:
1494: void setName(String name);
1495: }
1496:
1497: public static interface ReferenceAIF extends Serializable {
1498: String getRefNameA();
1499:
1500: void setRefNameA(String name);
1501: }
1502:
1503: public static interface ReferenceBIF extends Serializable {
1504: String getRefNameB();
1505:
1506: void setRefNameB(String name);
1507: }
1508:
1509: public static class Reference implements ReferenceIF {
1510: protected String ojbConcreteClass;
1511: private Integer refId;
1512: private String name;
1513:
1514: public Reference() {
1515: this (null);
1516: }
1517:
1518: public Reference(String name) {
1519: this .name = name;
1520: ojbConcreteClass = this .getClass().getName();
1521: }
1522:
1523: public String getOjbConcreteClass() {
1524: return ojbConcreteClass;
1525: }
1526:
1527: public void setOjbConcreteClass(String ojbConcreteClass) {
1528: this .ojbConcreteClass = ojbConcreteClass;
1529: }
1530:
1531: public Integer getRefId() {
1532: return refId;
1533: }
1534:
1535: public void setRefId(Integer refId) {
1536: this .refId = refId;
1537: }
1538:
1539: public String getName() {
1540: return name;
1541: }
1542:
1543: public void setName(String name) {
1544: this .name = name;
1545: }
1546: }
1547:
1548: public static class ReferenceA extends Reference implements
1549: ReferenceAIF {
1550: private String refNameA;
1551:
1552: public ReferenceA() {
1553: super ();
1554: }
1555:
1556: public ReferenceA(String name, String refNameA) {
1557: super (name);
1558: this .refNameA = refNameA;
1559: }
1560:
1561: public String getRefNameA() {
1562: return refNameA;
1563: }
1564:
1565: public void setRefNameA(String refName) {
1566: this .refNameA = refName;
1567: }
1568: }
1569:
1570: public static class ReferenceB extends Reference implements
1571: ReferenceBIF {
1572: private String refNameB;
1573:
1574: public ReferenceB() {
1575: super ();
1576: }
1577:
1578: public ReferenceB(String name, String refNameB) {
1579: super (name);
1580: this .refNameB = refNameB;
1581: }
1582:
1583: public String getRefNameB() {
1584: return refNameB;
1585: }
1586:
1587: public void setRefNameB(String refName) {
1588: this .refNameB = refName;
1589: }
1590: }
1591:
1592: //***************************************************************
1593: // classes mapped to multiple tables
1594: //***************************************************************
1595:
1596: public static abstract class Animal {
1597: private Integer id;
1598: private String name;
1599:
1600: public Integer getId() {
1601: return id;
1602: }
1603:
1604: public void setId(Integer id) {
1605: this .id = id;
1606: }
1607:
1608: public String getName() {
1609: return name;
1610: }
1611:
1612: public void setName(String name) {
1613: this .name = name;
1614: }
1615:
1616: public boolean equals(Object obj) {
1617: if (!(obj instanceof Animal))
1618: return false;
1619: Animal animal = (Animal) obj;
1620: boolean result;
1621: result = name == null ? (animal.getName() == null) : name
1622: .equals(animal.getName());
1623: result = result
1624: && (id == null ? animal.getId() == null : id
1625: .equals(animal.getId()));
1626: return result;
1627: }
1628: }
1629:
1630: public static class Mammal extends Animal {
1631: private Double height;
1632: private String ojbConcreteClass;
1633:
1634: public Mammal() {
1635: ojbConcreteClass = Mammal.class.getName();
1636: }
1637:
1638: public Double getHeight() {
1639: return height;
1640: }
1641:
1642: public void setHeight(Double height) {
1643: this .height = height;
1644: }
1645:
1646: public String getOjbConcreteClass() {
1647: return ojbConcreteClass;
1648: }
1649:
1650: public void setOjbConcreteClass(String ojbConcreteClass) {
1651: this .ojbConcreteClass = ojbConcreteClass;
1652: }
1653:
1654: public boolean equals(Object obj) {
1655: if (!(obj instanceof Mammal))
1656: return false;
1657: Mammal m = (Mammal) obj;
1658: boolean result = super .equals(obj);
1659: result = result
1660: && (height == null ? m.getHeight() == null : height
1661: .equals(m.getHeight()));
1662: return result;
1663: }
1664: }
1665:
1666: public static class Bird extends Animal {
1667: private Double wingspan;
1668: private String ojbConcreteClass;
1669:
1670: public Bird() {
1671: ojbConcreteClass = Bird.class.getName();
1672: }
1673:
1674: public boolean equals(Object obj) {
1675: if (!(obj instanceof Bird))
1676: return false;
1677: Bird m = (Bird) obj;
1678: boolean result = super .equals(obj);
1679: result = result
1680: && (wingspan == null ? m.getWingspan() == null
1681: : wingspan.equals(m.getWingspan()));
1682: return result;
1683: }
1684:
1685: public Double getWingspan() {
1686: return wingspan;
1687: }
1688:
1689: public void setWingspan(Double wingspan) {
1690: this .wingspan = wingspan;
1691: }
1692:
1693: public String getOjbConcreteClass() {
1694: return ojbConcreteClass;
1695: }
1696:
1697: public void setOjbConcreteClass(String ojbConcreteClass) {
1698: this .ojbConcreteClass = ojbConcreteClass;
1699: }
1700: }
1701:
1702: public static class Fish extends Animal {
1703: private Double length;
1704: private String ojbConcreteClass;
1705:
1706: public Fish() {
1707: ojbConcreteClass = Fish.class.getName();
1708: }
1709:
1710: public boolean equals(Object obj) {
1711: if (!(obj instanceof Fish))
1712: return false;
1713: Fish m = (Fish) obj;
1714: boolean result = super .equals(obj);
1715: result = result
1716: && (length == null ? m.getLength() == null : length
1717: .equals(m.getLength()));
1718: return result;
1719: }
1720:
1721: public String getOjbConcreteClass() {
1722: return ojbConcreteClass;
1723: }
1724:
1725: public void setOjbConcreteClass(String ojbConcreteClass) {
1726: this .ojbConcreteClass = ojbConcreteClass;
1727: }
1728:
1729: public Double getLength() {
1730: return length;
1731: }
1732:
1733: public void setLength(Double length) {
1734: this .length = length;
1735: }
1736: }
1737:
1738: //***************************************************************
1739: // classes for test with multiple, non-autoincrement PK
1740: //***************************************************************
1741: public static class Wine {
1742: private String id;
1743: private String grape;
1744: private String year;
1745: private String regionName;
1746: private String regionCountry;
1747: private Region region;
1748:
1749: public Wine() {
1750: }
1751:
1752: public Wine(String id, String grape, String year,
1753: String regionName, String regionCountry) {
1754: this .id = id;
1755: this .grape = grape;
1756: this .year = year;
1757: this .regionName = regionName;
1758: this .regionCountry = regionCountry;
1759: }
1760:
1761: public Region getRegion() {
1762: return region;
1763: }
1764:
1765: public void setRegion(Region region) {
1766: this .region = region;
1767: }
1768:
1769: public String getId() {
1770: return id;
1771: }
1772:
1773: public void setId(String id) {
1774: this .id = id;
1775: }
1776:
1777: public String getGrape() {
1778: return grape;
1779: }
1780:
1781: public void setGrape(String grape) {
1782: this .grape = grape;
1783: }
1784:
1785: public String getYear() {
1786: return year;
1787: }
1788:
1789: public void setYear(String year) {
1790: this .year = year;
1791: }
1792:
1793: public String getRegionName() {
1794: return regionName;
1795: }
1796:
1797: public void setRegionName(String regionName) {
1798: this .regionName = regionName;
1799: }
1800:
1801: public String getRegionCountry() {
1802: return regionCountry;
1803: }
1804:
1805: public void setRegionCountry(String regionCountry) {
1806: this .regionCountry = regionCountry;
1807: }
1808:
1809: public String toString() {
1810: return new ToStringBuilder(this ).append("id", id).append(
1811: "grape", grape).append("regionCountry",
1812: regionCountry).append("regionName", regionName)
1813: .append("year", year).append("region", region)
1814: .toString();
1815: }
1816: }
1817:
1818: public static class Region {
1819: private String name;
1820: private String country;
1821: private String description;
1822:
1823: public Region() {
1824: }
1825:
1826: public Region(String name, String country, String description) {
1827: this .name = name;
1828: this .country = country;
1829: this .description = description;
1830: }
1831:
1832: public String getName() {
1833: return name;
1834: }
1835:
1836: public void setName(String name) {
1837: this .name = name;
1838: }
1839:
1840: public String getCountry() {
1841: return country;
1842: }
1843:
1844: public void setCountry(String country) {
1845: this .country = country;
1846: }
1847:
1848: public String getDescription() {
1849: return description;
1850: }
1851:
1852: public void setDescription(String description) {
1853: this .description = description;
1854: }
1855:
1856: public String toString() {
1857: return new ToStringBuilder(this ).append("country", country)
1858: .append("name", name).append("description",
1859: description).toString();
1860: }
1861: }
1862:
1863: public static interface RefObject {
1864: Integer getId();
1865:
1866: void setId(Integer id);
1867:
1868: String getName();
1869:
1870: void setName(String name);
1871:
1872: RefObject getRef();
1873:
1874: void setRef(RefObject ref);
1875:
1876: Integer getFkColRef();
1877:
1878: void setFkColRef(Integer id);
1879: }
1880:
1881: public static class ObjA implements RefObject {
1882: Integer id;
1883: String name;
1884: RefObject ref;
1885: Integer fkColRef;
1886:
1887: public Integer getId() {
1888: return id;
1889: }
1890:
1891: public void setId(Integer id) {
1892: this .id = id;
1893: }
1894:
1895: public String getName() {
1896: return name;
1897: }
1898:
1899: public void setName(String name) {
1900: this .name = name;
1901: }
1902:
1903: public Integer getFkColRef() {
1904: return fkColRef;
1905: }
1906:
1907: public void setFkColRef(Integer fkColRef) {
1908: this .fkColRef = fkColRef;
1909: }
1910:
1911: public RefObject getRef() {
1912: return ref;
1913: }
1914:
1915: public void setRef(RefObject ref) {
1916: this .ref = ref;
1917: }
1918: }
1919:
1920: public static class ObjB extends ObjA {
1921:
1922: }
1923:
1924: public static class ObjC extends ObjA {
1925: String nameC;
1926: List references;
1927:
1928: public String getNameC() {
1929: return nameC;
1930: }
1931:
1932: public void setNameC(String nameC) {
1933: this .nameC = nameC;
1934: }
1935:
1936: public List getReferences() {
1937: return references;
1938: }
1939:
1940: public void setReferences(List references) {
1941: this.references = references;
1942: }
1943: }
1944: }
|