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 Fri, 09 Jan 2004 17:36:42 GMT
arminw      2004/01/09 09:36:41

  Modified:    src/test/org/apache/ojb/odmg CollectionsTest.java
  Log:
  fix tests
  add new test
  
  Revision  Changes    Path
  1.8       +159 -13   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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- CollectionsTest.java	9 Jan 2004 13:23:59 -0000	1.7
  +++ CollectionsTest.java	9 Jan 2004 17:36:41 -0000	1.8
  @@ -277,10 +277,155 @@
       }
   
       /**
  -     * Store/update an main object Gatherer with a collection of objects CollectiblesC.
  +     * Create object with 3 objects in associated collection.
  +     * We change one object of the collection
  +     */
  +    public void testUpdateCollection() throws Exception
  +    {
  +        String prefix = "testUpdateCollection" + System.currentTimeMillis();
  +        String queryStr = "select gatherer from " + Gatherer.class.getName() + " where
gatId=$1";
  +        String modifiedName = "modified_name_" + System.currentTimeMillis();
  +        String queryMod = "select coll from " + CollectibleA.class.getName() + " where
name like $1";
  +
  +        // prepare test case
  +        Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
  +        // set List of CollectiblesA objects
  +        gat.setCollectiblesA(Arrays.asList(prepareCollectibleA(gat, prefix + "_1_")));
  +        TransactionExt tx = (TransactionExt)odmg.newTransaction();
  +        tx.begin();
  +        db.makePersistent(gat);
  +        tx.commit();
  +
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +        // check if gatherer was stored
  +        OQLQuery query = odmg.newOQLQuery();
  +        query.create(queryStr);
  +        assertNotNull(gat.getGatId());
  +        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();
  +
  +        tx.begin();
  +        tx.lock(fetchedGat, Transaction.WRITE);
  +        List collsA = fetchedGat.getCollectiblesA();
  +        assertEquals(3, collsA.size());
  +        // now we change an object of the collection
  +        ((CollectibleA)collsA.get(0)).setName(modifiedName);
  +        tx.commit();
  +
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +        // now check if the modification was stored
  +        query = odmg.newOQLQuery();
  +        query.create(queryMod);
  +        query.bind(modifiedName);
  +        result = (Collection) query.execute();
  +        tx.commit();
  +        assertEquals("Wrong number of objects found", 1, result.size());
  +        CollectibleA collA = (CollectibleA) result.iterator().next();
  +        assertNotNull(collA);
  +        assertEquals(modifiedName, collA.getName());
  +    }
  +
  +    /**
  +     * we creat two objects with one object in collection:
  +     * gat1{collC1} and gat2{collC2}
  +     * then we exchange the collections
  +     * gat1{collC2} and gat2{collC1}
  +     * and commit.
  +     */
  +    public void testUpdateCollection_2() throws Exception
  +    {
  +        final String prefix = "testUpdateCollection_2" + System.currentTimeMillis();
  +        final String queryStr = "select gatherer from " + Gatherer.class.getName() + "
where gatId=$1 or gatId=$2";
  +
  +        // prepare test case
  +        Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
  +        Gatherer gat2 = new Gatherer(null, prefix + "_Gatherer2");
  +        // set List of CollectiblesA 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}));
  +        gat2.setCollectiblesC(Arrays.asList(new CollectibleC[]{collC_2}));
  +        TransactionExt tx = (TransactionExt)odmg.newTransaction();
  +        tx.begin();
  +        db.makePersistent(gat);
  +        db.makePersistent(gat2);
  +        tx.commit();
  +
  +        // query and check the result
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +        OQLQuery query = odmg.newOQLQuery();
  +        query.create(queryStr);
  +        assertNotNull(gat.getGatId());
  +        query.bind(gat.getGatId());
  +        query.bind(gat2.getGatId());
  +        Collection result = (Collection) query.execute();
  +        tx.commit();
  +        assertEquals("Wrong number of objects found", 2, result.size());
  +        Iterator it = result.iterator();
  +        Gatherer fetchedGat = (Gatherer) it.next();
  +        Gatherer fetchedGat2 = (Gatherer) it.next();
  +        assertNotNull(fetchedGat);
  +        assertNotNull(fetchedGat2);
  +        assertNotNull(fetchedGat.collectiblesC);
  +        assertNotNull(fetchedGat2.collectiblesC);
  +        assertEquals(1, fetchedGat.getCollectiblesC().size());
  +        assertEquals(1, fetchedGat2.getCollectiblesC().size());
  +
  +        collC_1 = (CollectibleC) fetchedGat.getCollectiblesC().get(0);
  +        collC_2 = (CollectibleC) fetchedGat2.getCollectiblesC().get(0);
  +        assertEquals(prefix + "NO_1", collC_1.getName());
  +        assertEquals(prefix + "NO_2", collC_2.getName());
  +
  +        List list1 = fetchedGat.getCollectiblesC();
  +        List list2 = fetchedGat2.getCollectiblesC();
  +        // now exchange the lists
  +        tx.begin();
  +        tx.lock(fetchedGat, Transaction.WRITE);
  +        tx.lock(fetchedGat2, Transaction.WRITE);
  +        fetchedGat.setCollectiblesC(list2);
  +        fetchedGat2.setCollectiblesC(list1);
  +        tx.commit();
  +
  +        // now we do same procedure to query and check
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +        query = odmg.newOQLQuery();
  +        query.create(queryStr);
  +        assertNotNull(gat.getGatId());
  +        query.bind(gat.getGatId());
  +        query.bind(gat2.getGatId());
  +        result = (Collection) query.execute();
  +        tx.commit();
  +        assertEquals("Wrong number of objects found", 2, result.size());
  +        it = result.iterator();
  +        fetchedGat = (Gatherer) it.next();
  +        fetchedGat2 = (Gatherer) it.next();
  +        assertNotNull(fetchedGat);
  +        assertNotNull(fetchedGat2);
  +        assertNotNull(fetchedGat.getCollectiblesC());
  +        assertNotNull(fetchedGat2.getCollectiblesC());
  +        assertEquals(1, fetchedGat.getCollectiblesC().size());
  +        assertEquals(1, fetchedGat2.getCollectiblesC().size());
  +
  +        collC_1 = (CollectibleC) fetchedGat.getCollectiblesC().get(0);
  +        collC_2 = (CollectibleC) fetchedGat2.getCollectiblesC().get(0);
  +        // we exchange the lists, thus we expect exchanged names
  +        assertEquals(prefix + "NO_2", collC_1.getName());
  +        assertEquals(prefix + "NO_1", collC_2.getName());
  +    }
  +
  +    /**
  +     * 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 remove one object of the collection
        */
       public void testStoreUpdateCollectionWithoutBackReference() throws Exception
       {
  @@ -290,11 +435,9 @@
   
           // prepare test case
           Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
  -        // we don't set the gatId, because we don't have one
  -        CollectibleC[] cols = prepareCollectibleC(null, prefix);
  -        List colList = Arrays.asList(cols);
  -        // set List of CollectiblesC objects
  -        gat.setCollectiblesC(colList);
  +        // 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);
  @@ -303,19 +446,21 @@
           // check if gatherer was stored
           tx.begin();
           tx.getBroker().clearCache();
  +        assertNotNull(gat.getGatId());
  +
           OQLQuery query = odmg.newOQLQuery();
           query.create(queryGat);
  -        Integer gatId = gat.getGatId();
  -        assertNotNull(gatId);
  -        query.bind(gatId);
  +        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);
   
           // check if gatherer contains list of CollectibleBase
           tx.begin();
  -        List colC = fetchedGat.getCollectiblesA();
  +        List colC = fetchedGat.getCollectiblesC();
           assertEquals("Wrong number of CollectiblesC", 3, colC.size());
   
           // delete one of the CollectibleBase
  @@ -324,8 +469,9 @@
           List newCols = new ArrayList();
           newCols.add(colC.get(1));
           newCols.add(colC.get(2));
  -        fetchedGat.setCollectiblesA(newCols);
  +        // lock object before do changes
           tx.lock(fetchedGat, Transaction.WRITE);
  +        fetchedGat.setCollectiblesA(newCols);
           db.deletePersistent(colC.get(0));
           tx.commit();
   
  @@ -346,7 +492,7 @@
           tx.begin();
           query = odmg.newOQLQuery();
           query.create(queryGat);
  -        query.bind(gatId);
  +        query.bind(gat.getGatId());
           result = (Collection) query.execute();
           assertEquals("Wrong number of objects found", 1, result.size());
           fetchedGat = (Gatherer) result.iterator().next();
  
  
  

---------------------------------------------------------------------
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