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/schema ojbtest-schema.xml
Date Fri, 22 Aug 2003 21:37:48 GMT
arminw      2003/08/22 14:37:48

  Modified:    src/test/org/apache/ojb/broker ReferenceTest.java
               src/test/org/apache/ojb repository_junit.xml
               src/schema ojbtest-schema.xml
  Log:
  add new test cases using abstract
  classes as references
  
  Revision  Changes    Path
  1.6       +475 -1    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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ReferenceTest.java	22 May 2003 19:39:42 -0000	1.5
  +++ ReferenceTest.java	22 Aug 2003 21:37:48 -0000	1.6
  @@ -224,6 +224,283 @@
           assertNotNull(repFK.getRefB());
       }
   
  +    /**
  +     * this test case use an abstract class as reference
  +     * @throws Exception
  +     */
  +    public void testAbstractReferenceStore() throws Exception
  +    {
  +        String name = "testAbstractReferenceStore_" + System.currentTimeMillis();
  +        /*
  +        create some animals
  +        */
  +        Bird bird_1 = new Bird();
  +        bird_1.setName(name);
  +        bird_1.setWingspan(new Double(2.33));
  +        Bird bird_2 = new Bird();
  +        bird_2.setName(name);
  +        bird_2.setWingspan(new Double(0.99));
  +
  +        Mammal mammal_1 = new Mammal();
  +        mammal_1.setName(name);
  +        mammal_1.setHeight(new Double(1.88));
  +        Mammal mammal_2 = new Mammal();
  +        mammal_2.setName(name);
  +        mammal_2.setHeight(new Double(19.13));
  +
  +        Fish fish_1 = new Fish();
  +        fish_1.setName(name);
  +        fish_1.setLength(new Double(0.033));
  +        Fish fish_2 = new Fish();
  +        fish_2.setName(name);
  +        fish_2.setLength(new Double(37.89));
  +
  +        Repository rep_1 = new Repository();
  +        rep_1.setAnimal(mammal_1);
  +        Repository rep_2 = new Repository();
  +        rep_2.setAnimal(bird_1);
  +        Repository rep_3 = new Repository();
  +        rep_3.setAnimal(fish_1);
  +
  +        /*
  +        store Repository instances and dummy animals
  +        */
  +        broker.beginTransaction();
  +        // store some dummy objects
  +        broker.store(bird_2);
  +        broker.store(mammal_2);
  +        broker.store(fish_2);
  +        // now store the main objects
  +        broker.store(rep_1);
  +        broker.store(rep_2);
  +        broker.store(rep_3);
  +        broker.commitTransaction();
  +
  +        Identity oid_mammal = new Identity(mammal_1, broker);
  +        Identity oid_bird = new Identity(bird_1, broker);
  +        Identity oid_fish = new Identity(fish_1, broker);
  +        Identity oid_rep_1 = new Identity(rep_1, broker);
  +        Identity oid_rep_2 = new Identity(rep_2, broker);
  +        Identity oid_rep_3 = new Identity(rep_3, broker);
  +
  +        broker.clearCache();
  +        // check the references
  +        Mammal lookup_mammal = (Mammal) broker.getObjectByIdentity(oid_mammal);
  +        Bird lookup_bird = (Bird) broker.getObjectByIdentity(oid_bird);
  +        Fish lookup_fish = (Fish) broker.getObjectByIdentity(oid_fish);
  +        assertEquals(mammal_1, lookup_mammal);
  +        assertEquals(bird_1, lookup_bird);
  +        assertEquals(fish_1, lookup_fish);
  +
  +        broker.clearCache();
  +        // check the main objects
  +        Repository lookup_rep_1 = (Repository) broker.getObjectByIdentity(oid_rep_1);
  +        Repository lookup_rep_2 = (Repository) broker.getObjectByIdentity(oid_rep_2);
  +        Repository lookup_rep_3 = (Repository) broker.getObjectByIdentity(oid_rep_3);
  +
  +        assertNotNull(lookup_rep_1.getAnimal());
  +        assertTrue("Expected instance of Mammal, found "+lookup_rep_1.getAnimal(),
  +                lookup_rep_1.getAnimal() instanceof Mammal);
  +        assertEquals(mammal_1, lookup_rep_1.getAnimal());
  +
  +        assertNotNull(lookup_rep_2.getAnimal());
  +        assertTrue("Expected instance of Bird, found "+lookup_rep_2.getAnimal(),
  +                lookup_rep_2.getAnimal() instanceof Bird);
  +        assertEquals(bird_1, lookup_rep_2.getAnimal());
  +
  +        assertNotNull(lookup_rep_3.getAnimal());
  +        assertTrue("Expected instance of Fish, found "+lookup_rep_3.getAnimal(),
  +                lookup_rep_3.getAnimal() instanceof Fish);
  +        assertEquals(fish_1, lookup_rep_3.getAnimal());
  +    }
  +
  +    public void testAbstractReferenceQuery() throws Exception
  +    {
  +        String name = "testAbstractReferenceQuery_" + System.currentTimeMillis();
  +        /*
  +        create some animals
  +        */
  +        Bird bird_1 = new Bird();
  +        bird_1.setName(name);
  +        bird_1.setWingspan(new Double(2.33));
  +        Bird bird_2 = new Bird();
  +        bird_2.setName(name);
  +        bird_2.setWingspan(new Double(0.99));
  +
  +        Mammal mammal_1 = new Mammal();
  +        mammal_1.setName(name);
  +        mammal_1.setHeight(new Double(1.88));
  +        Mammal mammal_2 = new Mammal();
  +        mammal_2.setName(name);
  +        mammal_2.setHeight(new Double(19.13));
  +
  +        Fish fish_1 = new Fish();
  +        fish_1.setName(name);
  +        fish_1.setLength(new Double(0.033));
  +        Fish fish_2 = new Fish();
  +        fish_2.setName(name);
  +        fish_2.setLength(new Double(37.89));
  +
  +        Repository rep_1 = new Repository();
  +        rep_1.setAnimal(mammal_1);
  +        Repository rep_2 = new Repository();
  +        rep_2.setAnimal(bird_1);
  +        Repository rep_3 = new Repository();
  +        rep_3.setAnimal(fish_1);
  +
  +        /*
  +        store Repository instances and dummy animals
  +        */
  +        broker.beginTransaction();
  +        // store some dummy objects
  +        broker.store(bird_2);
  +        broker.store(mammal_2);
  +        broker.store(fish_2);
  +        // now store the main objects
  +        broker.store(rep_1);
  +        broker.store(rep_2);
  +        broker.store(rep_3);
  +        broker.commitTransaction();
  +
  +        Identity oid_rep_1 = new Identity(rep_1, broker);
  +        Identity oid_rep_2 = new Identity(rep_2, broker);
  +        Identity oid_rep_3 = new Identity(rep_3, broker);
  +
  +        broker.clearCache();
  +        // check the main objects
  +        Repository lookup_rep_1 = (Repository) broker.getObjectByIdentity(oid_rep_1);
  +        Repository lookup_rep_2 = (Repository) broker.getObjectByIdentity(oid_rep_2);
  +        Repository lookup_rep_3 = (Repository) broker.getObjectByIdentity(oid_rep_3);
  +
  +        assertNotNull(lookup_rep_1.getAnimal());
  +        assertTrue("Expected instance of Mammal, found "+lookup_rep_1.getAnimal(),
  +                lookup_rep_1.getAnimal() instanceof Mammal);
  +        assertEquals(mammal_1, lookup_rep_1.getAnimal());
  +
  +        assertNotNull(lookup_rep_2.getAnimal());
  +        assertTrue("Expected instance of Bird, found "+lookup_rep_2.getAnimal(),
  +                lookup_rep_2.getAnimal() instanceof Bird);
  +        assertEquals(bird_1, lookup_rep_2.getAnimal());
  +
  +        assertNotNull(lookup_rep_3.getAnimal());
  +        assertTrue("Expected instance of Fish, found "+lookup_rep_3.getAnimal(),
  +                lookup_rep_3.getAnimal() instanceof Fish);
  +        assertEquals(fish_1, lookup_rep_3.getAnimal());
  +
  +        broker.clearCache();
  +        // query the references
  +        Criteria crit = new Criteria();
  +        crit.addEqualTo("name", name);
  +        Query query = QueryFactory.newQuery(Animal.class, crit);
  +        Collection result = broker.getCollectionByQuery(query);
  +        assertNotNull(result);
  +        int[] mammalBirdFish = new int[3];
  +        for (Iterator iterator = result.iterator(); iterator.hasNext();)
  +        {
  +            Object o = iterator.next();
  +            if(o instanceof Mammal) ++mammalBirdFish[0];
  +            if(o instanceof Bird) ++mammalBirdFish[1];
  +            if(o instanceof Fish) ++mammalBirdFish[2];
  +        }
  +        assertEquals(2, mammalBirdFish[0]);
  +        assertEquals(2, mammalBirdFish[1]);
  +        assertEquals(2, mammalBirdFish[2]);
  +    }
  +
  +    /**
  +     * this test case use an abstract class as reference
  +     * @throws Exception
  +     */
  +    public void testAbstractReferenceDelete() throws Exception
  +    {
  +        String name = "testAbstractReferenceDelete_" + System.currentTimeMillis();
  +        /*
  +        create some animals
  +        */
  +        Bird bird_1 = new Bird();
  +        bird_1.setName(name);
  +        bird_1.setWingspan(new Double(2.33));
  +        Bird bird_2 = new Bird();
  +        bird_2.setName(name);
  +        bird_2.setWingspan(new Double(0.99));
  +
  +        Mammal mammal_1 = new Mammal();
  +        mammal_1.setName(name);
  +        mammal_1.setHeight(new Double(1.88));
  +        Mammal mammal_2 = new Mammal();
  +        mammal_2.setName(name);
  +        mammal_2.setHeight(new Double(19.13));
  +
  +        Fish fish_1 = new Fish();
  +        fish_1.setName(name);
  +        fish_1.setLength(new Double(0.033));
  +        Fish fish_2 = new Fish();
  +        fish_2.setName(name);
  +        fish_2.setLength(new Double(37.89));
  +
  +        Repository rep_1 = new Repository();
  +        rep_1.setAnimal(mammal_1);
  +        Repository rep_2 = new Repository();
  +        rep_2.setAnimal(bird_1);
  +        Repository rep_3 = new Repository();
  +        rep_3.setAnimal(fish_1);
  +
  +        /*
  +        store Repository instances and dummy animals
  +        */
  +        broker.beginTransaction();
  +        // store some dummy objects
  +        broker.store(bird_2);
  +        broker.store(mammal_2);
  +        broker.store(fish_2);
  +        // now store the main objects
  +        broker.store(rep_1);
  +        broker.store(rep_2);
  +        broker.store(rep_3);
  +        broker.commitTransaction();
  +
  +        Identity oid_rep_1 = new Identity(rep_1, broker);
  +        Identity oid_rep_2 = new Identity(rep_2, broker);
  +        Identity oid_rep_3 = new Identity(rep_3, broker);
  +
  +        broker.clearCache();
  +        // check the main objects
  +        Repository lookup_rep_1 = (Repository) broker.getObjectByIdentity(oid_rep_1);
  +        Repository lookup_rep_2 = (Repository) broker.getObjectByIdentity(oid_rep_2);
  +        Repository lookup_rep_3 = (Repository) broker.getObjectByIdentity(oid_rep_3);
  +
  +        assertNotNull(lookup_rep_1.getAnimal());
  +        assertTrue("Expected instance of Mammal, found "+lookup_rep_1.getAnimal(),
  +                lookup_rep_1.getAnimal() instanceof Mammal);
  +        assertEquals(mammal_1, lookup_rep_1.getAnimal());
  +
  +        assertNotNull(lookup_rep_2.getAnimal());
  +        assertTrue("Expected instance of Bird, found "+lookup_rep_2.getAnimal(),
  +                lookup_rep_2.getAnimal() instanceof Bird);
  +        assertEquals(bird_1, lookup_rep_2.getAnimal());
  +
  +        assertNotNull(lookup_rep_3.getAnimal());
  +        assertTrue("Expected instance of Fish, found "+lookup_rep_3.getAnimal(),
  +                lookup_rep_3.getAnimal() instanceof Fish);
  +        assertEquals(fish_1, lookup_rep_3.getAnimal());
  +
  +        broker.clearCache();
  +
  +        broker.beginTransaction();
  +        broker.delete(rep_1);
  +        broker.delete(rep_2);
  +        broker.delete(rep_3);
  +        broker.commitTransaction();
  +
  +        lookup_rep_1 = (Repository) broker.getObjectByIdentity(oid_rep_1);
  +        lookup_rep_2 = (Repository) broker.getObjectByIdentity(oid_rep_2);
  +        lookup_rep_3 = (Repository) broker.getObjectByIdentity(oid_rep_3);
  +        assertNull(lookup_rep_1);
  +        assertNull(lookup_rep_2);
  +        assertNull(lookup_rep_3);
  +
  +    }
  +
       private Repository[] prepareRepository(String referenceNamePrefix)
       {
           Reference[] ref = new Reference[]{
  @@ -308,10 +585,63 @@
           private ReferenceAIF refA;
           private ReferenceBIF refB;
   
  +        private Animal animal;
  +        private Integer animalId;
  +
           public Repository()
           {
           }
   
  +        public Integer getRefId()
  +        {
  +            return refId;
  +        }
  +
  +        public void setRefId(Integer refId)
  +        {
  +            this.refId = refId;
  +        }
  +
  +        public Integer getRefAId()
  +        {
  +            return refAId;
  +        }
  +
  +        public void setRefAId(Integer refAId)
  +        {
  +            this.refAId = refAId;
  +        }
  +
  +        public Integer getRefBId()
  +        {
  +            return refBId;
  +        }
  +
  +        public void setRefBId(Integer refBId)
  +        {
  +            this.refBId = refBId;
  +        }
  +
  +        public Animal getAnimal()
  +        {
  +            return animal;
  +        }
  +
  +        public void setAnimal(Animal animal)
  +        {
  +            this.animal = animal;
  +        }
  +
  +        public Integer getAnimalId()
  +        {
  +            return animalId;
  +        }
  +
  +        public void setAnimalId(Integer animalId)
  +        {
  +            this.animalId = animalId;
  +        }
  +
           public ReferenceIF getRef()
           {
               return ref;
  @@ -470,6 +800,150 @@
           public void setRefNameB(String refName)
           {
               this.refNameB = refName;
  +        }
  +    }
  +
  +    public static abstract class Animal
  +    {
  +        private Integer id;
  +        private String name;
  +
  +        public Integer getId()
  +        {
  +            return id;
  +        }
  +
  +        public void setId(Integer id)
  +        {
  +            this.id = id;
  +        }
  +
  +        public String getName()
  +        {
  +            return name;
  +        }
  +
  +        public void setName(String name)
  +        {
  +            this.name = name;
  +        }
  +
  +        public boolean equals(Object obj)
  +        {
  +            if(! (obj instanceof Animal)) return false;
  +            Animal animal = (Animal) obj;
  +            boolean result;
  +            result = name == null ? (animal.getName() == null) : name.equals(animal.getName());
  +            result = result && (id == null ? animal.getId() == null : id.equals(animal.getId()));
  +            return result;
  +        }
  +    }
  +
  +    public static class Mammal extends Animal
  +    {
  +        private Double height;
  +        private String ojbConcreteClass;
  +
  +        public Mammal()
  +        {
  +            ojbConcreteClass = Mammal.class.getName();
  +        }
  +
  +        public Double getHeight()
  +        {
  +            return height;
  +        }
  +
  +        public void setHeight(Double height)
  +        {
  +            this.height = height;
  +        }
  +
  +        public String getOjbConcreteClass()
  +        {
  +            return ojbConcreteClass;
  +        }
  +
  +        public void setOjbConcreteClass(String ojbConcreteClass)
  +        {
  +            this.ojbConcreteClass = ojbConcreteClass;
  +        }
  +
  +        public boolean equals(Object obj)
  +        {
  +            if(!(obj instanceof Mammal)) return false;
  +            Mammal m = (Mammal) obj;
  +            boolean result = super.equals(obj);
  +            result = result && (height == null ? m.getHeight() == null : height.equals(m.getHeight()));
  +            return result;
  +        }
  +    }
  +
  +    public static class Bird extends Animal
  +    {
  +        private Double wingspan;
  +        private String ojbConcreteClass;
  +
  +        public Bird()
  +        {
  +            ojbConcreteClass = Bird.class.getName();
  +        }
  +
  +        public boolean equals(Object obj)
  +        {
  +            if(!(obj instanceof Bird)) return false;
  +            Bird m = (Bird) obj;
  +            boolean result = super.equals(obj);
  +            result = result && (wingspan == null ? m.getWingspan() == null : wingspan.equals(m.getWingspan()));
  +            return result;
  +        }
  +
  +        public Double getWingspan()
  +        {
  +            return wingspan;
  +        }
  +
  +        public void setWingspan(Double wingspan)
  +        {
  +            this.wingspan = wingspan;
  +        }
  +
  +        public String getOjbConcreteClass()
  +        {
  +            return ojbConcreteClass;
  +        }
  +
  +        public void setOjbConcreteClass(String ojbConcreteClass)
  +        {
  +            this.ojbConcreteClass = ojbConcreteClass;
  +        }
  +    }
  +
  +    public static class Fish extends Animal
  +    {
  +        private Double length;
  +
  +        public Fish()
  +        {
  +        }
  +
  +        public boolean equals(Object obj)
  +        {
  +            if(!(obj instanceof Fish)) return false;
  +            Fish m = (Fish) obj;
  +            boolean result = super.equals(obj);
  +            result = result && (length == null ? m.getLength() == null : length.equals(m.getLength()));
  +            return result;
  +        }
  +
  +        public Double getLength()
  +        {
  +            return length;
  +        }
  +
  +        public void setLength(Double length)
  +        {
  +            this.length = length;
           }
       }
   }
  
  
  
  1.87      +114 -0    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.86
  retrieving revision 1.87
  diff -u -r1.86 -r1.87
  --- repository_junit.xml	14 Aug 2003 18:42:40 -0000	1.86
  +++ repository_junit.xml	22 Aug 2003 21:37:48 -0000	1.87
  @@ -3545,6 +3545,12 @@
            jdbc-type="INTEGER"
           />
   
  +        <field-descriptor
  +         name="animalId"
  +         column="ANIMAL_FK"
  +         jdbc-type="INTEGER"
  +        />
  +
           <reference-descriptor
               name="ref"
               class-ref="org.apache.ojb.broker.ReferenceTest$Reference"
  @@ -3574,6 +3580,16 @@
               auto-delete="true">
                   <foreignkey field-ref="refBId"/>
           </reference-descriptor>
  +
  +        <reference-descriptor
  +            name="animal"
  +            class-ref="org.apache.ojb.broker.ReferenceTest$Animal"
  +            proxy="false"
  +            auto-retrieve="true"
  +            auto-update="true"
  +            auto-delete="true">
  +                <foreignkey field-ref="animalId"/>
  +        </reference-descriptor>
       </class-descriptor>
   
       <class-descriptor
  @@ -3639,6 +3655,104 @@
                   <foreignkey field-ref="refBId"/>
           </reference-descriptor>
       </class-descriptor>
  +
  +
  +    <class-descriptor class="org.apache.ojb.broker.ReferenceTest$Animal">
  +        <extent-class class-ref="org.apache.ojb.broker.ReferenceTest$Mammal" />
  +        <extent-class class-ref="org.apache.ojb.broker.ReferenceTest$Bird" />
  +        <extent-class class-ref="org.apache.ojb.broker.ReferenceTest$Fish" />
  +    </class-descriptor>
  +
  +
  +    <class-descriptor
  +        class="org.apache.ojb.broker.ReferenceTest$Mammal"
  +        table="REF_ANIMAL"
  +    >
  +        <field-descriptor
  +         name="id"
  +         column="A_ID"
  +         jdbc-type="INTEGER"
  +         primarykey="true"
  +         autoincrement="true"
  +        />
  +
  +        <field-descriptor
  +         name="ojbConcreteClass"
  +         column="CLASS_NAME"
  +         jdbc-type="VARCHAR"
  +      />
  +
  +        <field-descriptor
  +         name="height"
  +         column="LENGTH"
  +         jdbc-type="DOUBLE"
  +        />
  +
  +        <field-descriptor
  +         name="name"
  +         column="NAME"
  +         jdbc-type="VARCHAR"
  +        />
  +    </class-descriptor>
  +
  +
  +    <class-descriptor
  +        class="org.apache.ojb.broker.ReferenceTest$Bird"
  +        table="REF_ANIMAL"
  +    >
  +        <field-descriptor
  +         name="id"
  +         column="A_ID"
  +         jdbc-type="INTEGER"
  +         primarykey="true"
  +         autoincrement="true"
  +        />
  +
  +        <field-descriptor
  +         name="ojbConcreteClass"
  +         column="CLASS_NAME"
  +         jdbc-type="VARCHAR"
  +      />
  +
  +        <field-descriptor
  +         name="wingspan"
  +         column="LENGTH"
  +         jdbc-type="DOUBLE"
  +        />
  +
  +        <field-descriptor
  +         name="name"
  +         column="NAME"
  +         jdbc-type="VARCHAR"
  +        />
  +    </class-descriptor>
  +
  +
  +    <class-descriptor
  +        class="org.apache.ojb.broker.ReferenceTest$Fish"
  +        table="REF_FISH"
  +    >
  +        <field-descriptor
  +         name="id"
  +         column="A_ID"
  +         jdbc-type="INTEGER"
  +         primarykey="true"
  +         autoincrement="true"
  +        />
  +
  +        <field-descriptor
  +         name="length"
  +         column="LENGTH"
  +         jdbc-type="DOUBLE"
  +        />
  +
  +        <field-descriptor
  +         name="name"
  +         column="NAME"
  +         jdbc-type="VARCHAR"
  +        />
  +    </class-descriptor>
  +
   
       <class-descriptor
           class="org.apache.ojb.broker.ReferenceTest$Reference"
  
  
  
  1.47      +15 -0     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.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- ojbtest-schema.xml	13 Aug 2003 18:05:35 -0000	1.46
  +++ ojbtest-schema.xml	22 Aug 2003 21:37:48 -0000	1.47
  @@ -645,6 +645,7 @@
           <column name="REF_FK" type="INTEGER"/>
           <column name="REFA_FK" type="INTEGER"/>
           <column name="REFB_FK" type="INTEGER"/>
  +        <column name="ANIMAL_FK" type="INTEGER"/>
           <column name="NAME" type="VARCHAR" size="100"/>
       </table>
   
  @@ -661,6 +662,20 @@
           <column name="NAME" type="VARCHAR" size="100"/>
           <column name="REF_NAME_A" type="VARCHAR" size="100"/>
           <column name="REF_NAME_B" type="VARCHAR" size="100"/>
  +    </table>
  +
  +    <table name="REF_ANIMAL">
  +        <column name="A_ID" required="true" primaryKey="true" type="INTEGER"/>
  +        <column name="NAME" type="VARCHAR" size="100"/>
  +        <column name="CLASS_NAME" type="VARCHAR" size="100"/>
  +        <column name="LENGTH" type="DOUBLE"/>
  +    </table>
  +
  +    <table name="REF_FISH">
  +        <column name="A_ID" required="true" primaryKey="true" type="INTEGER"/>
  +        <column name="NAME" type="VARCHAR" size="100"/>
  +        <column name="CLASS_NAME" type="VARCHAR" size="100"/>
  +        <column name="LENGTH" type="DOUBLE"/>
       </table>
   
       <!-- =================================================== -->
  
  
  

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