Return-Path: Delivered-To: apmail-db-ojb-dev-archive@db.apache.org Received: (qmail 95669 invoked by uid 500); 9 Aug 2003 10:04:09 -0000 Mailing-List: contact ojb-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "OJB Developers List" Reply-To: "OJB Developers List" Delivered-To: mailing list ojb-dev@db.apache.org Received: (qmail 95653 invoked by uid 500); 9 Aug 2003 10:04:09 -0000 Received: (qmail 95645 invoked from network); 9 Aug 2003 10:04:09 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 9 Aug 2003 10:04:09 -0000 Received: (qmail 17227 invoked by uid 1510); 9 Aug 2003 10:04:35 -0000 Date: 9 Aug 2003 10:04:35 -0000 Message-ID: <20030809100435.17226.qmail@minotaur.apache.org> From: arminw@apache.org To: db-ojb-cvs@apache.org Subject: cvs commit: db-ojb/src/test/org/apache/ojb/broker CollectionTest.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N arminw 2003/08/09 03:04:35 Modified: src/test/org/apache/ojb repository_junit.xml src/schema ojbtest-schema.xml src/test/org/apache/ojb/broker CollectionTest.java Log: - add new tests, update tests one test does not pass Can anyone shed some light on this? Revision Changes Path 1.83 +98 -25 db-ojb/src/test/org/apache/ojb/repository_junit.xml Index: repository_junit.xml =================================================================== RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit.xml,v retrieving revision 1.82 retrieving revision 1.83 diff -u -r1.82 -r1.83 --- repository_junit.xml 25 Jul 2003 00:19:12 -0000 1.82 +++ repository_junit.xml 9 Aug 2003 10:04:34 -0000 1.83 @@ -4010,7 +4010,31 @@ proxy="true" auto-retrieve="true" auto-update="true" - auto-delete="false" + auto-delete="true" + > + + + + + + + + + + @@ -4026,8 +4050,8 @@ --> - + table="COLLECTION_COLLECTIBLE_BASE"> + - - - + table="COLLECTION_COLLECTIBLE_B"> - + - - - - + table="COLLECTION_COLLECTIBLE_C"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.45 +20 -1 db-ojb/src/schema/ojbtest-schema.xml Index: ojbtest-schema.xml =================================================================== RCS file: /home/cvs/db-ojb/src/schema/ojbtest-schema.xml,v retrieving revision 1.44 retrieving revision 1.45 diff -u -r1.44 -r1.45 --- ojbtest-schema.xml 25 Jul 2003 00:19:12 -0000 1.44 +++ ojbtest-schema.xml 9 Aug 2003 10:04:34 -0000 1.45 @@ -683,11 +683,30 @@ - +
+ + + +
+ + + + + +
+ + +
+ + + + + +
1.3 +446 -40 db-ojb/src/test/org/apache/ojb/broker/CollectionTest.java Index: CollectionTest.java =================================================================== RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/CollectionTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CollectionTest.java 7 Jun 2003 10:13:28 -0000 1.2 +++ CollectionTest.java 9 Aug 2003 10:04:35 -0000 1.3 @@ -1,6 +1,8 @@ package org.apache.ojb.broker; import junit.framework.TestCase; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; import org.apache.ojb.broker.query.Criteria; import org.apache.ojb.broker.query.Query; import org.apache.ojb.broker.query.QueryByCriteria; @@ -9,9 +11,43 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; -import java.util.Vector; /** + * Test case for collection handling. + * + * Main class Gatherer have five collections of type CollectibleBase, + * CollectibleB, CollectibleC, CollectibleD, CollectibleDD + * + * Class hierarchy: + * [CollectibleBaseIF <--] CollectibleBase <-- CollectibleB <-- CollectibleC + * | + * CollectibleD <-- CollectibleDD + * + * in repository interface CollectibleBaseIF was declared with five + * extents (CollectibleBase, CollectibleB, CollectibleC, CollectibleD, CollectibleDD) + * + * CollectibleBase + * auto-retrieve, auto-update, auto-delete all true + * proxy false + * + * CollectibleB + * auto-retrieve, auto-update set true, auto-delete false + * proxy true + * + * CollectibleC + * auto-retrieve, auto-update, auto-delete set true, + * proxy true + * CollectibleC has a reference back to the Gatherer object + * (auto-retrieve, auto-update, auto-delete set false to avoid circular + * object creation) + * + * CollectibleD + * auto-retrieve, auto-update, auto-delete all true + * proxy false + * + * CollectibleDD + * auto-retrieve, auto-update, auto-delete all true + * proxy true * * @author Armin Waibel * @version $Id$ @@ -42,21 +78,42 @@ } } - public void testCollectionCreation() + /** + * generate main object with collections and store + * main object to make all persistent + */ + public void testStoreDeleteSimpleCollections() { long timestamp = System.currentTimeMillis(); String colPrefix = "col_" + timestamp; - String name = timestamp + "_test gatherer"; + String name = timestamp + "_testStoreDeleteSimpleCollections"; // create gatherer with collections Gatherer gatherer = new Gatherer(null, name); gatherer.setCollectiblesBase(Arrays.asList(prepareCollectibleBase(colPrefix))); gatherer.setCollectiblesB(Arrays.asList(prepareCollectibleB(colPrefix))); - gatherer.setCollectiblesC(Arrays.asList(prepareCollectibleC(colPrefix))); broker.beginTransaction(); broker.store(gatherer); broker.commitTransaction(); + assertEquals("CollectibleBase objects", 3, gatherer.getCollectiblesBase().size()); + assertEquals(gatherer.getGatId(), ((CollectibleBaseIF) gatherer.getCollectiblesBase().get(0)).getGathererId()); + assertEquals("CollectibleB objects", 4, gatherer.getCollectiblesB().size()); + assertEquals(gatherer.getGatId(), ((CollectibleBIF) gatherer.getCollectiblesB().get(0)).getGathererId()); + + Identity oid = new Identity(gatherer, broker); + broker.clearCache(); + Gatherer new_gatherer = (Gatherer) broker.getObjectByIdentity(oid); + + assertNotNull(new_gatherer); + assertNotNull(new_gatherer.getCollectiblesBase()); + assertNotNull(new_gatherer.getCollectiblesB()); + assertEquals("CollectibleBase objects", 3, new_gatherer.getCollectiblesBase().size()); + assertEquals("CollectibleB objects", 4, new_gatherer.getCollectiblesB().size()); + assertEquals(new_gatherer.getGatId(), ((CollectibleBaseIF) new_gatherer.getCollectiblesBase().get(0)).getGathererId()); + assertEquals(new_gatherer.getGatId(), ((CollectibleBIF) new_gatherer.getCollectiblesB().get(0)).getGathererId()); + + broker.clearCache(); Criteria criteria = new Criteria(); criteria.addLike("name", colPrefix + "_colBase*"); @@ -73,10 +130,264 @@ assertEquals("Wrong number of queried objects", 4, result.size()); criteria = new Criteria(); - criteria.addLike("name", colPrefix + "_colC*"); - q = new QueryByCriteria(CollectibleC.class, criteria); + criteria.addLike("name", colPrefix + "*"); + q = new QueryByCriteria(CollectibleBaseIF.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + assertEquals("Wrong number of queried objects", 7, result.size()); + + // now we delete the main object + // and see what's going on with the dependend objects + broker.beginTransaction(); + broker.delete(new_gatherer); + broker.commitTransaction(); + + broker.clearCache(); + new_gatherer = (Gatherer) broker.getObjectByIdentity(oid); + + assertNull(new_gatherer); + + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "_colBase*"); + q = new QueryByCriteria(CollectibleBase.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + // auto-delete is set true + assertEquals("Wrong number of queried objects", 0, result.size()); + + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "_colB*"); + q = new QueryByCriteria(CollectibleB.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + // auto-delete is set false + assertEquals("Wrong number of queried objects", 4, result.size()); + + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "*"); + q = new QueryByCriteria(CollectibleBaseIF.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + assertEquals("Wrong number of queried objects", 4, result.size()); + } + + /** + * generate main object with collections and store + * main object to make all persistent + * using ojbConcreteClass feature to map different + * objects to same table + */ + public void testStoreDeleteSimpleCollections_2() + { + long timestamp = System.currentTimeMillis(); + String colPrefix = "col_" + timestamp; + String name = timestamp + "_testStoreDeleteSimpleCollections"; + + // create gatherer with collections + Gatherer gatherer = new Gatherer(null, name); + gatherer.setCollectiblesD(Arrays.asList(prepareCollectibleD(colPrefix))); + gatherer.setCollectiblesDD(Arrays.asList(prepareCollectibleDD(colPrefix))); + + broker.beginTransaction(); + broker.store(gatherer); + broker.commitTransaction(); + assertEquals("CollectibleD objects", 2, gatherer.getCollectiblesD().size()); + assertEquals(gatherer.getGatId(), ((CollectibleDIF) gatherer.getCollectiblesD().get(0)).getGathererId()); + assertEquals("CollectibleDD objects", 3, gatherer.getCollectiblesDD().size()); + assertEquals(gatherer.getGatId(), ((CollectibleDDIF) gatherer.getCollectiblesDD().get(0)).getGathererId()); + + Identity oid = new Identity(gatherer, broker); + broker.clearCache(); + Gatherer new_gatherer = (Gatherer) broker.getObjectByIdentity(oid); + + assertNotNull(new_gatherer); + assertNotNull(new_gatherer.getCollectiblesD()); + assertNotNull(new_gatherer.getCollectiblesDD()); + assertEquals("CollectibleD objects", 2, new_gatherer.getCollectiblesD().size()); + assertEquals("CollectibleDD objects", 3, new_gatherer.getCollectiblesDD().size()); + assertEquals(new_gatherer.getGatId(), ((CollectibleDIF) new_gatherer.getCollectiblesD().get(0)).getGathererId()); + assertEquals(new_gatherer.getGatId(), ((CollectibleDDIF) new_gatherer.getCollectiblesDD().get(0)).getGathererId()); + + broker.clearCache(); + + Criteria criteria = new Criteria(); + criteria.addLike("name", colPrefix + "_colD*"); + criteria.addLike("name", colPrefix + "*"); + Query q = new QueryByCriteria(CollectibleD.class, criteria); + Collection result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + assertEquals("Wrong number of queried objects", 2, result.size()); + + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "_colDD*"); + q = new QueryByCriteria(CollectibleDD.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + assertEquals("Wrong number of queried objects", 3, result.size()); + + // now test objConcreteClass feature + // should only return CollectibleD class instances + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "*"); + q = new QueryByCriteria(CollectibleD.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + assertEquals("Wrong number of queried objects", 2, result.size()); + // now test objConcreteClass feature + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "*"); + q = new QueryByCriteria(CollectibleDD.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + assertEquals("Wrong number of queried objects", 3, result.size()); + + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "*"); + q = new QueryByCriteria(CollectibleBaseIF.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + assertEquals("Wrong number of queried objects", 5, result.size()); + + // now we delete the main object + // and see what's going on with the dependend objects + broker.beginTransaction(); + broker.delete(new_gatherer); + broker.commitTransaction(); + + broker.clearCache(); + new_gatherer = (Gatherer) broker.getObjectByIdentity(oid); + + assertNull(new_gatherer); + + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "_colD*"); + q = new QueryByCriteria(CollectibleD.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + // auto-delete is set true + assertEquals("Wrong number of queried objects", 0, result.size()); + + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "_colDD*"); + q = new QueryByCriteria(CollectibleDD.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + // auto-delete is set true + assertEquals("Wrong number of queried objects", 0, result.size()); + + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "*"); + q = new QueryByCriteria(CollectibleBaseIF.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + assertEquals("Wrong number of queried objects", 0, result.size()); + } + + public void testStoreSimpleCollections() + { + long timestamp = System.currentTimeMillis(); + String colPrefix = "col_" + timestamp; + String name = timestamp + "_testStoreSimpleCollections"; + + // create gatherer with collections + Gatherer gatherer = new Gatherer(null, name); + + broker.beginTransaction(); + broker.store(gatherer); + gatherer.setCollectiblesBase(Arrays.asList(prepareCollectibleBase(colPrefix))); + gatherer.setCollectiblesB(Arrays.asList(prepareCollectibleB(colPrefix))); + broker.store(gatherer); + broker.commitTransaction(); + + Identity oid = new Identity(gatherer, broker); + broker.clearCache(); + Gatherer new_gatherer = (Gatherer) broker.getObjectByIdentity(oid); + + assertNotNull(new_gatherer); + assertNotNull(new_gatherer.getCollectiblesBase()); + assertNotNull(new_gatherer.getCollectiblesB()); + assertEquals("CollectibleBase objects", 3, new_gatherer.getCollectiblesBase().size()); + assertEquals("CollectibleB objects", 4, new_gatherer.getCollectiblesB().size()); + Integer gatId = ((CollectibleBaseIF)new_gatherer.getCollectiblesBase().get(0)).getGathererId(); + assertNotNull(gatId); + assertEquals(new_gatherer.gatId, gatId); + broker.clearCache(); + + Criteria criteria = new Criteria(); + criteria.addLike("name", colPrefix + "_colBase*"); + Query q = new QueryByCriteria(CollectibleBase.class, criteria); + Collection result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + assertEquals("Wrong number of queried objects", 3, result.size()); + + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "_colB*"); + q = new QueryByCriteria(CollectibleB.class, criteria); + result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); + assertEquals("Wrong number of queried objects", 4, result.size()); + + criteria = new Criteria(); + criteria.addLike("name", colPrefix + "*"); + q = new QueryByCriteria(CollectibleBaseIF.class, criteria); result = (Collection) broker.getCollectionByQuery(q); assertNotNull(result); + assertEquals("Wrong number of queried objects", 7, result.size()); + } + + /** + * generate main object with collections and store + * main object to make all persistent. + * same like {@link #testStoreSimpleCollections} but now the + * collection objects have a reference back to main object. + * + * Curious but this test does not pass + */ + public void testStoreCollectionObjectsWithBackReference() + { + long timestamp = System.currentTimeMillis(); + String colPrefix = "col_" + timestamp; + String name = timestamp + "_testStoreCollectionObjectsWithBackReference"; + + // create gatherer with collections + Gatherer gatherer = new Gatherer(null, name); + gatherer.setCollectiblesC(Arrays.asList(prepareCollectibleC(colPrefix))); + + broker.beginTransaction(); + broker.store(gatherer); + broker.commitTransaction(); + assertEquals("CollectibleC objects", 5, gatherer.getCollectiblesC().size()); + /* + TODO: does not pass. the same test with an object that + doesn't have a reference back to main object (Gatherer) + pass without problems. + */ + assertEquals(gatherer.getGatId(), ((CollectibleCIF) gatherer.getCollectiblesC().get(0)).getGathererId()); + + Identity oid = new Identity(gatherer, broker); + broker.clearCache(); +// System.out.println("## stored "+gatherer); + Gatherer new_gatherer = (Gatherer) broker.getObjectByIdentity(oid); +// System.out.println("## after lookup "+new_gatherer); +// System.out.println("## after "+new_gatherer.getCollectiblesC().size()); + + assertNotNull(new_gatherer); + assertNotNull(new_gatherer.getCollectiblesC()); + /* + TODO: fix this. When dependend object has a reference to the main object, + the test fails. After store, all is ok. Seems something going wrong + on lookup. + */ + assertEquals("CollectibleC objects", 5, gatherer.getCollectiblesC().size()); + assertEquals(gatherer.getGatId(), ((CollectibleCIF) gatherer.getCollectiblesC().get(0)).getGathererId()); + + broker.clearCache(); + + Criteria criteria = new Criteria(); + criteria.addLike("name", colPrefix + "_colC*"); + Query q = new QueryByCriteria(CollectibleC.class, criteria); + Collection result = (Collection) broker.getCollectionByQuery(q); + assertNotNull(result); assertEquals("Wrong number of queried objects", 5, result.size()); criteria = new Criteria(); @@ -84,7 +395,7 @@ q = new QueryByCriteria(CollectibleBaseIF.class, criteria); result = (Collection) broker.getCollectionByQuery(q); assertNotNull(result); - assertEquals("Wrong number of queried objects", 12, result.size()); + assertEquals("Wrong number of queried objects", 5, result.size()); } private CollectibleBase[] prepareCollectibleBase(String namePrefix) @@ -111,15 +422,34 @@ private CollectibleB[] prepareCollectibleC(String namePrefix) { CollectibleC[] colC = new CollectibleC[]{ - new CollectibleC(null, namePrefix + "_colC_1", null, "ext1"), - new CollectibleC(null, namePrefix + "_colC_2", null, "ext2"), - new CollectibleC(null, namePrefix + "_colC_3", null, "ext3"), - new CollectibleC(null, namePrefix + "_colC_4", null, "ext4"), - new CollectibleC(null, namePrefix + "_colC_5", null, "ext5") + new CollectibleC(namePrefix + "_colC_1", "ext1"), + new CollectibleC(namePrefix + "_colC_2", "ext2"), + new CollectibleC(namePrefix + "_colC_3", "ext3"), + new CollectibleC(namePrefix + "_colC_4", "ext4"), + new CollectibleC(namePrefix + "_colC_5", "ext5") }; return colC; } + private CollectibleD[] prepareCollectibleD(String namePrefix) + { + CollectibleD[] colD = new CollectibleD[]{ + new CollectibleD(namePrefix + "_colD_1"), + new CollectibleD(namePrefix + "_colD_2"), + }; + return colD; + } + + private CollectibleDD[] prepareCollectibleDD(String namePrefix) + { + CollectibleDD[] colDD = new CollectibleDD[]{ + new CollectibleDD(namePrefix + "_colDD_1"), + new CollectibleDD(namePrefix + "_colDD_2"), + new CollectibleDD(namePrefix + "_colDD_3") + }; + return colDD; + } + //********************************************************************* // inner class - persistent object @@ -128,20 +458,16 @@ { private Integer gatId; private String name; - private List collectiblesBase = new Vector(); - private List collectiblesB = new Vector(); - private List collectiblesC = new Vector(); + private List collectiblesBase; + private List collectiblesB; + private List collectiblesC; + private List collectiblesD; + private List collectiblesDD; public Gatherer() { } - public void addCollectibleA(CollectibleBase colA) - { - if (collectiblesBase == null) collectiblesBase = new Vector(); - collectiblesBase.add(colA); - } - public Gatherer(Integer gatId, String name) { this.gatId = gatId; @@ -197,6 +523,39 @@ { this.collectiblesC = collectiblesC; } + + public List getCollectiblesD() + { + return collectiblesD; + } + + public void setCollectiblesD(List collectiblesD) + { + this.collectiblesD = collectiblesD; + } + + public List getCollectiblesDD() + { + return collectiblesDD; + } + + public void setCollectiblesDD(List collectiblesDD) + { + this.collectiblesDD = collectiblesDD; + } + + public String toString() + { + ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE); + buf.append("gatId", gatId); + buf.append("name", name); + buf.append("collectiblesBase", collectiblesBase); + buf.append("collectiblesB", collectiblesB); + buf.append("collectiblesC", collectiblesC); + buf.append("collectiblesD", collectiblesD); + buf.append("collectiblesDD", collectiblesDD); + return buf.toString(); + } } public static interface CollectibleBaseIF extends Serializable @@ -224,27 +583,27 @@ private String name; private Integer gathererId; private Gatherer gatherer; + // protected String ojbConcreteClass; public CollectibleBase() { + // ojbConcreteClass = CollectibleBase.class.getName(); } - public CollectibleBase(Integer colId, String name, Integer gathererId) - { - this.colId = colId; - this.name = name; - this.gathererId = gathererId; - } - - public CollectibleBase(String name, Integer gathererId) + public CollectibleBase(String name) { + // ojbConcreteClass = CollectibleBase.class.getName(); this.name = name; - this.gathererId = gathererId; } - public CollectibleBase(String name) + public String toString() { - this.name = name; + ToStringBuilder buf = new ToStringBuilder(this); + buf.append("colId", colId); + buf.append("name", name); + buf.append("gathererId", gathererId); +// buf.append("ojbConcreteClass", ojbConcreteClass); + return buf.toString(); } public Gatherer getGatherer() @@ -297,16 +656,13 @@ { public CollectibleB() { +// ojbConcreteClass = CollectibleB.class.getName(); } public CollectibleB(String name) { super(name); - } - - public CollectibleB(Integer colId, String name, Integer gathererId) - { - super(colId, name, gathererId); +// ojbConcreteClass = CollectibleB.class.getName(); } } @@ -323,11 +679,19 @@ public CollectibleC() { +// ojbConcreteClass = CollectibleC.class.getName(); } - public CollectibleC(Integer colId, String name, Integer gathererId, String extentName) + public CollectibleC(String name) { - super(colId, name, gathererId); + super(name); +// ojbConcreteClass = CollectibleC.class.getName(); + } + + public CollectibleC(String name, String extentName) + { + super(name); +// ojbConcreteClass = CollectibleC.class.getName(); this.extentName = extentName; } @@ -339,6 +703,48 @@ public void setExtentName(String extentName) { this.extentName = extentName; + } + } + + public static interface CollectibleDIF extends CollectibleBaseIF + { + + } + + public static class CollectibleD extends CollectibleBase implements CollectibleDIF + { + protected String ojbConcreteClass; + + public CollectibleD() + { + ojbConcreteClass = CollectibleD.class.getName(); + } + + public CollectibleD(String name) + { + super(name); + ojbConcreteClass = CollectibleD.class.getName(); + } + } + + public static interface CollectibleDDIF extends CollectibleBaseIF + { + + } + + public static class CollectibleDD extends CollectibleBase implements CollectibleDDIF + { + protected String ojbConcreteClass; + + public CollectibleDD() + { + ojbConcreteClass = CollectibleDD.class.getName(); + } + + public CollectibleDD(String name) + { + super(name); + ojbConcreteClass = CollectibleDD.class.getName(); } } } --------------------------------------------------------------------- To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org For additional commands, e-mail: ojb-dev-help@db.apache.org