db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker ReferenceTest.java
Date Fri, 11 Mar 2005 20:44:56 GMT
brj         2005/03/11 12:44:56

  Modified:    src/schema ojbtest-schema.xml
               src/test/org/apache/ojb/broker ReferenceTest.java
  Log:
  extended reference test
  
  Revision  Changes    Path
  1.95      +7 -4      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.94
  retrieving revision 1.95
  diff -u -r1.94 -r1.95
  --- ojbtest-schema.xml	11 Mar 2005 19:44:40 -0000	1.94
  +++ ojbtest-schema.xml	11 Mar 2005 20:44:56 -0000	1.95
  @@ -848,22 +848,25 @@
       <table name="REF_OBJ_A">
           <column name="OBJ_ID" required="true" primaryKey="true" type="INTEGER"/>
           <column name="NAME" type="VARCHAR" size="150"/>
  +        <column name="FK_COL_REF" type="INTEGER"/>
           <column name="FK_REF" type="INTEGER"/>
       </table>
   
       <table name="REF_OBJ_B">
           <column name="OBJ_ID" required="true" primaryKey="true" type="INTEGER"/>
           <column name="NAME" type="VARCHAR" size="150"/>
  +        <column name="FK_COL_REF" type="INTEGER"/>
           <column name="FK_REF" type="INTEGER"/>
       </table>
  -
  + 
       <table name="REF_OBJ_C">
           <column name="OBJ_ID" required="true" primaryKey="true" type="INTEGER"/>
           <column name="NAME" type="VARCHAR" size="150"/>
  +        <column name="NAME_C" type="VARCHAR" size="150"/>
  +        <column name="FK_COL_REF" type="INTEGER"/>
           <column name="FK_REF" type="INTEGER"/>
       </table>
   
  -
       <!-- =================================================== -->
       <!-- PrimaryKeyForeignKeyTest - PB-api                   -->
       <!-- =================================================== -->
  @@ -1568,7 +1571,7 @@
               javaName="articleName"/>
       <column name="Lieferanten_Nr" type="INTEGER"
               javaName="supplierId"/>
  -    <column name="KatName" type="VARCHAR" size="150" 
  +    <column name="KatName" type="VARCHAR" size="150"
               javaName="productGroupName"/>
       <column name="Liefereinheit" type="VARCHAR" size="150"
               javaName="unit"/>
  
  
  
  1.20      +310 -53   db-ojb/src/test/org/apache/ojb/broker/ReferenceTest.java
  
  Index: ReferenceTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/ReferenceTest.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- ReferenceTest.java	20 Nov 2004 09:30:33 -0000	1.19
  +++ ReferenceTest.java	11 Mar 2005 20:44:56 -0000	1.20
  @@ -1,8 +1,10 @@
   package org.apache.ojb.broker;
   
   import java.io.Serializable;
  +import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Iterator;
  +import java.util.List;
   
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  @@ -39,6 +41,294 @@
           }
       }
   
  +    /**
  +     * Test the usage of interface as class-ref in collection-descriptor
  +     * when using inheritance.
  +     */
  +    public void testInterfaceAsCollectionRef_1()
  +    {
  +        // if(skipKnownIssueProblem("query using path via reference, like 'ref1.ref2.name'"))
return;
  +        String name = "testQueryWithCollectionRef_" + System.currentTimeMillis();
  +        RefObject a = new ObjA();
  +        RefObject a2 = new ObjA();
  +        RefObject b = new ObjB();
  +        RefObject b2 = new ObjB();
  +        // this object has a 1:n relation
  +        ObjC c1 = new ObjC();
  +        // only used in the c object reference collection
  +        RefObject d1 = new ObjC();
  +        RefObject d2 = new ObjC();
  +        ObjC c2 = new ObjC();
  +
  +        c1.setName(name+"_third");
  +        b.setName(name+"_second_1");
  +        b2.setName(name+"_second_2");
  +        a.setName(name+"_first_1");
  +        a2.setName(name+"_first_2");
  +        d1.setName(name+"_d1");
  +        d2.setName(name+"_d2");
  +        c2.setName(name + "_c2");
  +
  +        c1.setNameC(name + "_1");
  +        c2.setNameC(name + "_2");
  +
  +        a.setRef(b);
  +        b.setRef(c1);
  +        a2.setRef(b2);
  +
  +        List refList = new ArrayList();
  +        refList.add(a);
  +        refList.add(b2);
  +        refList.add(d1);
  +        c1.setReferences(refList);
  +        List refList2 = new ArrayList();
  +        refList2.add(d2);
  +        c2.setReferences(refList2);
  +
  +        broker.beginTransaction();
  +        broker.store(a);
  +        broker.store(a2);
  +        broker.store(c2);
  +        broker.commitTransaction();
  +
  +        // check existence of objects
  +        Criteria crit = new Criteria();
  +        crit.addLike("name", name + "%");
  +        Query q = QueryFactory.newQuery(RefObject.class, crit);
  +        Collection result = broker.getCollectionByQuery(q);
  +        assertEquals(8, result.size());
  +
  +        // expect all 'C' objects with 1:n reference object
  +        // with name '..._d1' --> 'c1'
  +        crit = new Criteria();
  +        crit.addEqualTo("references.name", name+"_d1");
  +        q = QueryFactory.newQuery(ObjC.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +        ObjC newC = (ObjC) result.iterator().next();
  +        assertEquals(name + "_1", newC.getNameC());
  +
  +        // expect all 'C' objects with 1:n reference object
  +        // with nameC '..._%' --> 'c1' 'c2'
  +        crit = new Criteria();
  +        crit.addLike("nameC", name+"_%");
  +        q = QueryFactory.newQuery(ObjC.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(2, result.size());
  +
  +        // expect all 'B' objects with 1:1 to an RefObject which
  +        // has an 1:n reference object
  +        // with name '..._d1' --> 'b'
  +        crit = new Criteria();
  +        crit.addEqualTo("ref.references.name", name+"_d1");
  +        // add this because only 'C' objects have a 1:n reference
  +        crit.addPathClass("ref", ObjC.class);
  +        q = QueryFactory.newQuery(ObjB.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +        ObjB newB = (ObjB) result.iterator().next();
  +        assertNotNull(newB.getRef());
  +        assertTrue(newB.getRef() instanceof ObjC);
  +        newC = (ObjC) newB.getRef();
  +        assertEquals(3, newC.getReferences().size());
  +
  +        // expect all 'B' objects with 1:1 to an RefObject which
  +        // has an 1:n reference object
  +        // with name '..._d1' --> 'b'
  +        crit = new Criteria();
  +        crit.addLike("ref.nameC", name+"_%");
  +        // add this because only 'C' objects have a 1:n reference
  +        crit.addPathClass("ref", ObjC.class);
  +        q = QueryFactory.newQuery(ObjB.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +        newB = (ObjB) result.iterator().next();
  +        assertNotNull(newB.getRef());
  +        assertTrue(newB.getRef() instanceof ObjC);
  +        newC = (ObjC) newB.getRef();
  +        assertEquals(3, newC.getReferences().size());
  +
  +        // expect all A's which have a B called '_second_1'
  +        crit = new Criteria();
  +        crit.addLike("name", name+"_%");
  +        crit.addEqualTo("ref.name", name+"_second_1");
  +        crit.addPathClass("ref", ObjB.class);
  +        q = QueryFactory.newQuery(ObjA.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +
  +        // expect all A's which have a B called '_second_1' and 
  +        // a C called '_third' 
  +        crit = new Criteria();
  +        crit.addLike("name", name+"_%");
  +        crit.addEqualTo("ref.name", name+"_second_1");
  +        crit.addEqualTo("ref.ref.name", name+"_third");
  +        crit.addPathClass("ref", ObjB.class);
  +        crit.addPathClass("ref.ref", ObjC.class);
  +        q = QueryFactory.newQuery(ObjA.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +
  +        // expect all A's which third level 'ref' has a 'references'
  +        // field collection, this is only valid for 'C' class objects
  +        // and references contain '..._d1' object --> 'a'
  +        crit = new Criteria();
  +        crit.addLike("name", name+"_%");
  +        crit.addEqualTo("ref.ref.references.name", name+"_d1");
  +        crit.addPathClass("ref", ObjB.class);
  +        crit.addPathClass("ref.ref", ObjC.class);
  +        q = QueryFactory.newQuery(ObjA.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +        for(Iterator iterator = result.iterator(); iterator.hasNext();)
  +        {
  +            RefObject ref = (RefObject) iterator.next();
  +            assertTrue(ref instanceof ObjA);
  +            String refName = ref.getName();
  +            assertTrue(!(refName.indexOf(name)<0));
  +        }
  +
  +        // expect all A's with reference object named '_second%' and
  +        // which third level 'ref' has a 'references'
  +        // field collection, this is only valid for 'C' class objects
  +        // and references contain '..._second%' objects --> 'a'
  +        crit = new Criteria();
  +        crit.addLike("name", name+"_%");
  +        crit.addLike("ref.name", name+"_second%");
  +        crit.addLike("ref.ref.references.name", name+"_second%");
  +        crit.addPathClass("ref", ObjB.class);
  +        crit.addPathClass("ref.ref", ObjC.class);
  +        q = QueryFactory.newQuery(ObjA.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +        for(Iterator iterator = result.iterator(); iterator.hasNext();)
  +        {
  +            RefObject ref = (RefObject) iterator.next();
  +            assertTrue(ref instanceof ObjA);
  +            String refName = ref.getName();
  +            assertTrue(!(refName.indexOf(name)<0));
  +        }
  +    }
  +
  +    /**
  +     * Test the usage of interface as class-ref in collection-descriptor
  +     * when using inheritance.
  +     */
  +    public void testInterfaceAsCollectionRef_2()
  +    {
  +        // if(skipKnownIssueProblem("query using path via reference, like 'ref1.ref2.name'"))
return;
  +        String name = "testQueryWithCollectionRef_" + System.currentTimeMillis();
  +        RefObject a = new ObjA();
  +        RefObject a2 = new ObjA();
  +        RefObject b = new ObjB();
  +        RefObject b2 = new ObjB();
  +        // this object has a 1:n relation
  +        ObjC c = new ObjC();
  +        // only used in the c object reference collection
  +        RefObject d = new ObjC();
  +
  +        c.setName(name+"_third");
  +        b.setName(name+"_second_1");
  +        b2.setName(name+"_second_2");
  +        a.setName(name+"_first_1");
  +        a2.setName(name+"_first_2");
  +        d.setName(name+"_none");
  +
  +        a.setRef(b);
  +        b.setRef(c);
  +        a2.setRef(b2);
  +
  +        List refList = new ArrayList();
  +        refList.add(a);
  +        refList.add(b2);
  +        refList.add(d);
  +        c.setReferences(refList);
  +
  +        broker.beginTransaction();
  +        broker.store(a);
  +        broker.store(a2);
  +        broker.commitTransaction();
  +
  +        // check existence of objects
  +        Criteria crit = new Criteria();
  +        crit.addEqualTo("name", name+"_third");
  +        Query q = QueryFactory.newQuery(RefObject.class, crit);
  +        Collection result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +        ObjC newC = (ObjC) result.iterator().next();
  +        assertNotNull(newC.getReferences());
  +        assertEquals(3, newC.getReferences().size());
  +
  +        // test n-level depth
  +        //*****************************************
  +        crit = new Criteria();
  +        crit.addEqualTo("ref.ref.name", name+"_third");
  +        q = QueryFactory.newQuery(ObjA.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +        //*****************************************
  +
  +        crit = new Criteria();
  +        crit.addLike("references.name", name+"_first%");
  +        q = QueryFactory.newQuery(ObjC.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +
  +        // expect all A's with name "_first_2" or with second
  +        // level 'ref' "_third" in this case object 'a' and 'a2'
  +        crit = new Criteria();
  +        crit.addEqualTo("name", name+"_first_2");
  +        Criteria critOr = new Criteria();
  +        critOr.addEqualTo("ref.ref.name", name+"_third");
  +        crit.addOrCriteria(critOr);
  +        q = QueryFactory.newQuery(ObjA.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(2, result.size());
  +        for(Iterator iterator = result.iterator(); iterator.hasNext();)
  +        {
  +            RefObject ref = (RefObject) iterator.next();
  +            assertTrue(ref instanceof ObjA);
  +            String refName = ref.getName();
  +            assertTrue(!(refName.indexOf(name)<0));
  +        }
  +
  +        // expect all A's which second level 'ref' is "_third"
  +        // in this case object 'a'
  +        crit = new Criteria();
  +        crit.addLike("name", name+"_%");
  +        Criteria critAnd = new Criteria();
  +        critAnd.addEqualTo("ref.ref.name", name+"_third");
  +        crit.addAndCriteria(critAnd);
  +        q = QueryFactory.newQuery(ObjA.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +        for(Iterator iterator = result.iterator(); iterator.hasNext();)
  +        {
  +            RefObject ref = (RefObject) iterator.next();
  +            assertTrue(ref instanceof ObjA);
  +            String refName = ref.getName();
  +            assertTrue(!(refName.indexOf(name)<0));
  +        }
  +
  +        // expect all A's with first level 'ref' "_second%"
  +        // in this case object 'a' and 'a2'
  +        crit = new Criteria();
  +        crit.addLike("ref.name", name+"_second%");
  +        critAnd = new Criteria();
  +        critAnd.addLike("name", name+"%");
  +        crit.addAndCriteria(critAnd);
  +        q = QueryFactory.newQuery(ObjA.class, crit);
  +        result = broker.getCollectionByQuery(q);
  +        assertEquals(2, result.size());
  +        for(Iterator iterator = result.iterator(); iterator.hasNext();)
  +        {
  +            RefObject ref = (RefObject) iterator.next();
  +            assertTrue(ref instanceof ObjA);
  +            String refName = ref.getName();
  +            assertTrue(!(refName.indexOf(name)<0));
  +        }
  +    }
  +    
       public void testDeepPathQuery()
       {
           // if(skipKnownIssueProblem("query using path via reference, like 'ref1.ref2.name'"))
return;
  @@ -1565,6 +1855,7 @@
           Integer id;
           String name;
           RefObject ref;
  +        Integer fkColRef;
   
           public Integer getId()
           {
  @@ -1586,41 +1877,14 @@
               this.name = name;
           }
   
  -        public RefObject getRef()
  -        {
  -            return ref;
  -        }
  -
  -        public void setRef(RefObject ref)
  -        {
  -            this.ref = ref;
  -        }
  -    }
  -
  -    public static class ObjB implements RefObject
  -    {
  -        Integer id;
  -        String name;
  -        RefObject ref;
  -
  -        public Integer getId()
  -        {
  -            return id;
  -        }
  -
  -        public void setId(Integer id)
  +        public Integer getFkColRef()
           {
  -            this.id = id;
  +            return fkColRef;
           }
   
  -        public String getName()
  +        public void setFkColRef(Integer fkColRef)
           {
  -            return name;
  -        }
  -
  -        public void setName(String name)
  -        {
  -            this.name = name;
  +            this.fkColRef = fkColRef;
           }
   
           public RefObject getRef()
  @@ -1634,40 +1898,33 @@
           }
       }
   
  -    public static class ObjC implements RefObject
  +    public static class ObjB extends ObjA
       {
  -        Integer id;
  -        String name;
  -        RefObject ref;
  -
  -        public Integer getId()
  -        {
  -            return id;
  -        }
  +    }
   
  -        public void setId(Integer id)
  -        {
  -            this.id = id;
  -        }
  +    public static class ObjC extends ObjA
  +    {
  +        String nameC;
  +        List references;
   
  -        public String getName()
  +        public String getNameC()
           {
  -            return name;
  +            return nameC;
           }
   
  -        public void setName(String name)
  +        public void setNameC(String nameC)
           {
  -            this.name = name;
  +            this.nameC = nameC;
           }
   
  -        public RefObject getRef()
  +        public List getReferences()
           {
  -            return ref;
  +            return references;
           }
   
  -        public void setRef(RefObject ref)
  +        public void setReferences(List references)
           {
  -            this.ref = ref;
  +            this.references = references;
           }
       }
   }
  
  
  

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