db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/odmg CollectionsTest.java
Date Sat, 24 Jan 2004 14:15:55 GMT
arminw      2004/01/24 06:15:55

  Modified:    src/test/org/apache/ojb/odmg CollectionsTest.java
  Log:
  add new tests show unsolved problems
  
  Revision  Changes    Path
  1.9       +295 -7    db-ojb/src/test/org/apache/ojb/odmg/CollectionsTest.java
  
  Index: CollectionsTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/CollectionsTest.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- CollectionsTest.java	9 Jan 2004 17:36:41 -0000	1.8
  +++ CollectionsTest.java	24 Jan 2004 14:15:55 -0000	1.9
  @@ -2,6 +2,11 @@
   
   import junit.framework.TestCase;
   import org.apache.ojb.broker.TestHelper;
  +import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.PersistenceBrokerFactory;
  +import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryFactory;
   import org.odmg.Database;
   import org.odmg.Implementation;
   import org.odmg.OQLQuery;
  @@ -62,7 +67,7 @@
   
       public void testStoreFetchDeleteCollectionWithBackReference() throws Exception
       {
  -        String prefix = "testInsertDeleteCollectionWithBackReference" + System.currentTimeMillis();
  +        String prefix = "testStoreFetchDeleteCollectionWithBackReference_" + System.currentTimeMillis();
           String queryStr = "select gatherer from " + Gatherer.class.getName() + " where
gatId=$1";
   
           // prepare test case
  @@ -133,7 +138,7 @@
   
       public void testStoreUpdateCollectionWithBackReference() throws Exception
       {
  -        String prefix = "testInsertDeleteCollectionWithBackReference" + System.currentTimeMillis();
  +        String prefix = "testStoreUpdateCollectionWithBackReference_" + System.currentTimeMillis();
           String queryStr = "select gatherer from " + Gatherer.class.getName() + " where
gatId=$1";
   
           TransactionExt tx = (TransactionExt) odmg.newTransaction();
  @@ -331,11 +336,15 @@
       }
   
       /**
  -     * we creat two objects with one object in collection:
  +     * we create two objects with one object in collection:
        * gat1{collC1} and gat2{collC2}
        * then we exchange the collections
        * gat1{collC2} and gat2{collC1}
  -     * and commit.
  +     * and commit. So the size of the collection
  +     * hold by the main object doesn't change and current
  +     * implementation dosen't mark main object as dirty
  +     *
  +     * see ...odmg.ObjectEnvelope line 311
        */
       public void testUpdateCollection_2() throws Exception
       {
  @@ -345,7 +354,7 @@
           // prepare test case
           Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
           Gatherer gat2 = new Gatherer(null, prefix + "_Gatherer2");
  -        // set List of CollectiblesA objects
  +        // set List of CollectiblesC objects
           CollectibleC collC_1 = new CollectibleC(prefix + "NO_1", null, "nothing1");
           CollectibleC collC_2 = new CollectibleC(prefix + "NO_2", null, "nothing2");
           gat.setCollectiblesC(Arrays.asList(new CollectibleC[]{collC_1}));
  @@ -431,7 +440,7 @@
       {
           // String queryColl = "select colls from " + CollectibleC.class.getName() + " where
name=$1";
           String queryGat = "select gatherer from " + Gatherer.class.getName() + " where
gatId=$1";
  -        String prefix = "testStoreUpdateCollectionWithoutBackReference" + System.currentTimeMillis();
  +        String prefix = "testStoreUpdateCollectionWithoutBackReference_" + System.currentTimeMillis();
   
           // prepare test case
           Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
  @@ -502,6 +511,285 @@
   
           colC.get(0);
       }
  +
  +    /**
  +     * Create an main object Gatherer with a collection of objects CollectiblesC.
  +     * CollectiblesC hasn't a reference back to the main object, thus we don't have to
set
  +     * the main object in the collection objects. Further we can't set the object id of
the
  +     * main object, because we don't know it at creation time.
  +     * Then we ADD a new object to the collection
  +     */
  +    public void testAddCollectionElementWithoutBackReference() throws Exception
  +    {
  +        // String queryColl = "select colls from " + CollectibleC.class.getName() + " where
name=$1";
  +        String queryGat = "select gatherer from " + Gatherer.class.getName() + " where
gatId=$1";
  +        String prefix = "testAddCollectionElementWithoutBackReference_" + System.currentTimeMillis();
  +
  +        // prepare test case
  +        Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
  +        // we don't set the gatId in CollectiblesC, because we don't have one
  +        // Set List of CollectiblesC objects
  +        gat.setCollectiblesC(Arrays.asList(prepareCollectibleC(null, prefix)));
  +        TransactionExt tx = (TransactionExt)odmg.newTransaction();
  +        tx.begin();
  +        db.makePersistent(gat);
  +        tx.commit();
  +
  +        // check if gatherer was stored
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +        assertNotNull(gat.getGatId());
  +
  +        OQLQuery query = odmg.newOQLQuery();
  +        query.create(queryGat);
  +        query.bind(gat.getGatId());
  +
  +        Collection result = (Collection) query.execute();
  +        tx.commit();
  +        assertEquals("Wrong number of objects found", 1, result.size());
  +        Gatherer fetchedGat = (Gatherer) result.iterator().next();
  +        assertNotNull(fetchedGat);
  +
  +
  +        tx.begin();
  +        // check if gatherer contains list of CollectibleBase
  +        List colC = fetchedGat.getCollectiblesC();
  +        assertEquals("Wrong number of CollectiblesC", 3, colC.size());
  +
  +        //*************************************
  +        // Now add a new collection object
  +        CollectibleC newC = new CollectibleC(prefix, null, "new added");
  +        fetchedGat.getCollectiblesC().add(newC);
  +        //*************************************
  +
  +        tx.commit();
  +
  +        // check if the Collectibles were really deleted from DB
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +
  +        query = odmg.newOQLQuery();
  +        query.create("select colls from " + CollectibleC.class.getName() +
  +                " where name like $1");
  +        query.bind(prefix + "%");
  +        result = (Collection) query.execute();
  +        assertEquals("Wrong number of objects found", 3, result.size());
  +        tx.commit();
  +
  +        // check if the gatherer now contains a CollectibleBase list
  +        // increased by the added
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +        query = odmg.newOQLQuery();
  +        query.create(queryGat);
  +        query.bind(gat.getGatId());
  +        result = (Collection) query.execute();
  +        assertEquals("Wrong number of objects found", 1, result.size());
  +        fetchedGat = (Gatherer) result.iterator().next();
  +        colC = fetchedGat.getCollectiblesC();
  +        assertEquals("Wrong number of CollectiblesA found in Gatherer", 3, colC.size());
  +        tx.commit();
  +
  +        colC.get(0);
  +    }
  +
  +    /**
  +     * Create an main object Gatherer with a collection of objects CollectiblesC
  +     * (CollectiblesC has a reference back to the main object).
  +     * Then we ADD a new object to the collection
  +     */
  +    public void testAddCollectionElementWithBackReference() throws Exception
  +    {
  +        String queryGat = "select gatherer from " + Gatherer.class.getName() + " where
gatId=$1";
  +        String prefix = "testAddCollectionElementWithBackReference_" + System.currentTimeMillis();
  +
  +        /*
  +        prepare test case
  +        If the back reference was not set, the test doesn't pass
  +        */
  +        Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
  +        // Set List of CollectiblesB objects
  +        gat.setCollectiblesB(Arrays.asList(prepareCollectibleB(gat, prefix)));
  +
  +        TransactionExt tx = (TransactionExt)odmg.newTransaction();
  +        tx.begin();
  +        db.makePersistent(gat);
  +        tx.commit();
  +
  +        // check if gatherer was stored
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +        assertNotNull(gat.getGatId());
  +
  +        OQLQuery query = odmg.newOQLQuery();
  +        query.create(queryGat);
  +        query.bind(gat.getGatId());
  +
  +        Collection result = (Collection) query.execute();
  +        tx.commit();
  +        assertEquals("Wrong number of objects found", 1, result.size());
  +        Gatherer fetchedGat = (Gatherer) result.iterator().next();
  +        assertNotNull(fetchedGat);
  +
  +
  +        tx.begin();
  +        // check if gatherer contains list of CollectibleBase
  +        List colB = fetchedGat.getCollectiblesB();
  +        assertEquals("Wrong number of CollectiblesB", 3, colB.size());
  +
  +        //*************************************
  +        tx.lock(fetchedGat, Transaction.WRITE);
  +        // Now add a new collection object
  +        CollectibleB newB = new CollectibleB(prefix);
  +        newB.setGatherer(fetchedGat);
  +        fetchedGat.getCollectiblesB().add(newB);
  +        //*************************************
  +
  +        tx.commit();
  +
  +        // check
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +
  +        query = odmg.newOQLQuery();
  +        query.create("select colls from " + CollectibleB.class.getName() +
  +                " where name like $1");
  +        query.bind(prefix + "%");
  +        result = (Collection) query.execute();
  +        assertEquals("Wrong number of objects found", 4, result.size());
  +        tx.commit();
  +
  +        // check if the gatherer now contains a CollectibleBase list
  +        // increased by the added
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +        query = odmg.newOQLQuery();
  +        query.create(queryGat);
  +        query.bind(gat.getGatId());
  +        result = (Collection) query.execute();
  +        assertEquals("Wrong number of objects found", 1, result.size());
  +        fetchedGat = (Gatherer) result.iterator().next();
  +        colB = fetchedGat.getCollectiblesB();
  +        assertEquals("Wrong number of CollectiblesA found in Gatherer", 4, colB.size());
  +        tx.commit();
  +
  +        colB.get(0);
  +    }
  +
  +/*
  +    User test case from user-list:
  +> A contains a collection of B.  B has a reference back to its parent A.
  +> Calling A.addB(B) sets B's reference to A.
  +>
  +> Create new A.
  +> Create new B.
  +> Add B to A.
  +> Make A persistent.
  +> A and B successfully persisted to the DB.
  +> Clear the OJB cache
  +> Retrieve A (using PB)
  +> Start Tx (ODMG)
  +> Lock A for writing (ODMG)
  +> Create a new B2.
  +> Add B2 to A.
  +> Commit Tx.
  +> Clear Cache.
  +> Retrieve A (using PB)
  +> Assert(A count Bs == 2) FAIL.  B2 was never persisted.
  +>
  +> ODMG's Persistence by reachability should have persisted B2, should it not?
  +> I thought that using DList might fix this.
  +     */
  +    public void testAddCollectionElementCrossAPI() throws Exception
  +    {
  +        // Lookup current odmg-collection class
  +        Class oqlCollectionClass = ((OdmgConfiguration) PersistenceBrokerFactory.getConfigurator().
  +                getConfigurationFor(null)).getOqlCollectionClass();
  +
  +        String name = "testAddCollectionElementCrossAPI_"+System.currentTimeMillis();
  +        // prepare test case
  +        Gatherer gat = new Gatherer(null, name);
  +        CollectibleB B = new CollectibleB(name);
  +        B.setGatherer(gat);
  +        ArrayList cols = new ArrayList();
  +        cols.add(B);
  +        gat.setCollectiblesB(cols);
  +
  +        TransactionExt tx = (TransactionExt) odmg.newTransaction();
  +        tx.begin();
  +        db.makePersistent(gat);
  +        tx.commit();
  +
  +        // now cross ODMG with PB api
  +        Gatherer fetchedGatherer = null;
  +        PersistenceBroker pb = null;
  +        try
  +        {
  +            pb = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            pb.clearCache();
  +            Criteria crit = new Criteria();
  +            crit.addLike("name", name);
  +            Query q = QueryFactory.newQuery(Gatherer.class, crit);
  +            fetchedGatherer = (Gatherer) pb.getObjectByQuery(q);
  +        }
  +        finally
  +        {
  +            if(pb != null) pb.close();
  +        }
  +
  +        // check queried result
  +        assertNotNull(fetchedGatherer);
  +        assertEquals(gat.getGatId(), fetchedGatherer.getGatId());
  +        assertNotNull(fetchedGatherer.getCollectiblesB());
  +        assertEquals(1, fetchedGatherer.getCollectiblesB().size());
  +        CollectibleB fetched_B = (CollectibleB) fetchedGatherer.getCollectiblesB().iterator().next();
  +        assertNotNull(fetched_B);
  +
  +        // Now work with queried result
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +        //*************************************
  +        tx.lock(fetchedGatherer, Transaction.WRITE);
  +        CollectibleB newB = new CollectibleB(name);
  +        newB.setGatherer(fetchedGatherer);
  +        fetchedGatherer.getCollectiblesB().add(newB);
  +        //*************************************
  +        assertEquals(2, fetchedGatherer.getCollectiblesB().size());
  +        tx.commit();
  +
  +        // now cross again ODMG with PB api
  +        fetchedGatherer = null;
  +        pb = null;
  +        try
  +        {
  +            pb = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            pb.clearCache();
  +            Criteria crit = new Criteria();
  +            crit.addLike("name", name);
  +            Query q = QueryFactory.newQuery(Gatherer.class, crit);
  +            fetchedGatherer = (Gatherer) pb.getObjectByQuery(q);
  +        }
  +        finally
  +        {
  +            if(pb != null) pb.close();
  +        }
  +
  +        // check queried result
  +        assertNotNull(fetchedGatherer);
  +        assertEquals(gat.getGatId(), fetchedGatherer.getGatId());
  +        assertNotNull(fetchedGatherer.getCollectiblesB());
  +        assertEquals(2, fetchedGatherer.getCollectiblesB().size());
  +        CollectibleB fetched_B_1 = (CollectibleB) fetchedGatherer.getCollectiblesB().iterator().next();
  +        CollectibleB fetched_B_2 = (CollectibleB) fetchedGatherer.getCollectiblesB().iterator().next();
  +        assertNotNull(fetched_B_1);
  +        assertNotNull(fetched_B_2);
  +    }
  +
  +
  +
  +    //**********************************************************
  +    // helper methods
  +    //**********************************************************
   
       private CollectibleA[] prepareCollectibleA(Gatherer gat, String namePrefix)
       {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message