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 MtoNTest.java
Date Sat, 28 Jun 2003 15:46:29 GMT
brj         2003/06/28 08:46:29

  Modified:    src/test/org/apache/ojb/broker MtoNTest.java
  Log:
  reworked testcases for removal aware and autoUpdate
  
  Revision  Changes    Path
  1.6       +121 -251  db-ojb/src/test/org/apache/ojb/broker/MtoNTest.java
  
  Index: MtoNTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/MtoNTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- MtoNTest.java	26 Jun 2003 20:41:57 -0000	1.5
  +++ MtoNTest.java	28 Jun 2003 15:46:28 -0000	1.6
  @@ -12,6 +12,7 @@
   import junit.framework.TestCase;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
  +import org.apache.ojb.broker.util.collections.ManageableArrayList;
   import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
   
   /**
  @@ -54,355 +55,210 @@
   
       }
   
  -    public void testStoringWithoutAutoUpdateEqTrue()
  +    private Paper createPaper()
       {
           String now = new Date().toString();
           Paper paper = new Paper();
           paper.setAuthor("Jonny Myers");
           paper.setDate(now);
  -        Qualifier qual = new Topic();
  -        qual.setName("qual " + now);
  -        paper.setQualifiers(Arrays.asList(new Qualifier[] { qual }));
  +
  +        Qualifier qual1 = new Topic();
  +        qual1.setName("qual1 " + now);
  +        Qualifier qual2 = new Topic();
  +        qual2.setName("qual2 " + now);
  +
  +        List qualifiers = new Vector();
  +        qualifiers.add(qual1);
  +        qualifiers.add(qual2);
  +        paper.setQualifiers(qualifiers);
  +
           broker.beginTransaction();
  +        broker.store(qual1);
  +        broker.store(qual2);
           broker.store(paper);
  -        broker.store(qual);
           Identity paperId = new Identity(paper, broker);
           broker.commitTransaction();
   
  +        // sanity check
           broker.clearCache();
           broker.beginTransaction();
           Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -        System.out.println("retrieved: " + retPaper);
  -        List qualifiers = retPaper.getQualifiers();
  -        System.out.println("ret qualifiers: " + qualifiers);
  -        assertEquals(1, qualifiers.size());
  -        System.out.println("ret qual: " + ((Qualifier) qualifiers.get(0)).getName());
  +        qualifiers = retPaper.getQualifiers();
  +
  +        assertEquals(2, qualifiers.size());
           broker.commitTransaction();
  +
  +        return retPaper;
       }
   
  -    // delete from intermediary table when auto-delete is OFF
  -    public void testAutoDeleteOFF()
  +    public void testCreate()
  +    {
  +        Paper paper = createPaper();    
  +    }
  +
  +    public void testStoringWithAutoUpdateFalse()
       {
           ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
           CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
  -        boolean cascadeDelete = cod.getCascadeDelete();
  -
  -        // disable auto-delete
  -        cod.setCascadeDelete(false);
  +        boolean autoUpdate = cod.getCascadeStore();
   
  +        cod.setCascadeStore(false);
  + 
           try
           {
               String now = new Date().toString();
               Paper paper = new Paper();
               paper.setAuthor("Jonny Myers");
               paper.setDate(now);
  -
  -            Qualifier qual1 = new Topic();
  -            qual1.setName("qual1 " + now);
  -            Qualifier qual2 = new Topic();
  -            qual2.setName("qual2 " + now);
  -
  -            List qualifiers = new Vector();
  -            qualifiers.add(qual1);
  -            qualifiers.add(qual2);
  -            paper.setQualifiers(qualifiers);
  -
  +            Qualifier qual = new Topic();
  +            qual.setName("qual " + now);
  +            paper.setQualifiers(Arrays.asList(new Qualifier[] { qual }));
               broker.beginTransaction();
  -            broker.store(qual1);
  -            broker.store(qual2);
  -            broker.store(paper);
  +            broker.store(paper);        // store Paper only
               Identity paperId = new Identity(paper, broker);
               broker.commitTransaction();
  -
  -            // sanity check
  +                       
               broker.clearCache();
               broker.beginTransaction();
               Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -            qualifiers = retPaper.getQualifiers();
  -
  -            assertEquals(2, qualifiers.size());
  -            broker.commitTransaction();
  -
  -            // remove first object
  -            paper.getQualifiers().remove(0);
  -            broker.beginTransaction();
  -            broker.store(paper);
  -            broker.commitTransaction();
  -
  -            broker.clearCache();
  -            broker.beginTransaction();
  -            retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -            qualifiers = retPaper.getQualifiers();
  -            assertEquals(1, qualifiers.size());
  -
  -            // target object should still be there
  -            Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1,
broker));
  -            Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2,
broker));
  -
  -            assertNotNull(retQual1);
  -            assertNotNull(retQual2);
  -
  +            assertEquals(0, retPaper.getQualifiers().size());
               broker.commitTransaction();
           }
           finally
           {
  -            // reset auto-delete
  -            cod.setCascadeDelete(cascadeDelete);
  +            cod.setCascadeStore(autoUpdate);
           }
  -
       }
   
  -    // delete from intermediary AND target-table when auto-delete is ON
  -    public void _testAutoDeleteON()
  +    public void testStoringWithAutoUpdateTrue()
       {
           ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
           CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
  -        boolean cascadeDelete = cod.getCascadeDelete();
  -
  -        // enable auto-delete
  -        cod.setCascadeDelete(true);
  +        boolean autoUpdate = cod.getCascadeStore();
   
  +        cod.setCascadeStore(true);
  + 
           try
           {
               String now = new Date().toString();
               Paper paper = new Paper();
               paper.setAuthor("Jonny Myers");
               paper.setDate(now);
  -
  -            Qualifier qual1 = new Topic();
  -            qual1.setName("qual1 " + now);
  -            Qualifier qual2 = new Topic();
  -            qual2.setName("qual2 " + now);
  -
  -            List qualifiers = new Vector();
  -            qualifiers.add(qual1);
  -            qualifiers.add(qual2);
  -            paper.setQualifiers(qualifiers);
  -
  +            Qualifier qual = new Topic();
  +            qual.setName("qual " + now);
  +            paper.setQualifiers(Arrays.asList(new Qualifier[] { qual }));
               broker.beginTransaction();
  -            broker.store(qual1);
  -            broker.store(qual2);
  -            broker.store(paper);
  +            broker.store(paper);        // store Paper and Quelifier
               Identity paperId = new Identity(paper, broker);
               broker.commitTransaction();
  -
  -            // sanity check
  +                       
               broker.clearCache();
               broker.beginTransaction();
               Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -            qualifiers = retPaper.getQualifiers();
  -
  -            assertEquals(2, qualifiers.size());
  -            broker.commitTransaction();
  -
  -            // remove first object
  -            paper.getQualifiers().remove(0);
  -            broker.beginTransaction();
  -            broker.store(paper);
  -            broker.commitTransaction();
  -
  -            broker.clearCache();
  -            broker.beginTransaction();
  -            retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -            qualifiers = retPaper.getQualifiers();
  -            assertEquals(1, qualifiers.size());
  -
  -            // target object qual1 should be deleted
  -            Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1,
broker));
  -            Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2,
broker));
  -
  -            assertNull(retQual1);
  -            assertNotNull(retQual2);
  -
  +            assertEquals(1, retPaper.getQualifiers().size());
               broker.commitTransaction();
           }
           finally
           {
  -            // reset auto-delete
  -            cod.setCascadeDelete(cascadeDelete);
  +            cod.setCascadeStore(autoUpdate);
           }
       }
   
  -    // delete from intermediary AND target-table when auto-delete is ON
  -    public void testAutoDeleteON_RemovalAware()
  +
  +    // delete from intermediary table only when collection NOT removal aware
  +    public void testDelete_NonRemovalAware()
       {
           ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
           CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
  -        boolean cascadeDelete = cod.getCascadeDelete();
           Class collectionClass = cod.getCollectionClass();
   
  -        // enable auto-delete
  -        cod.setCascadeDelete(true);
  -        cod.setCollectionClass(RemovalAwareCollection.class);
  +        cod.setCollectionClass(ManageableArrayList.class);
   
           try
           {
  -            String now = new Date().toString();
  -            Paper paper = new Paper();
  -            paper.setAuthor("Jonny Myers");
  -            paper.setDate(now);
  -
  -            Qualifier qual1 = new Topic();
  -            qual1.setName("qual1 " + now);
  -            Qualifier qual2 = new Topic();
  -            qual2.setName("qual2 " + now);
  -
  -            List qualifiers = new RemovalAwareCollection();
  -            qualifiers.add(qual1);
  -            qualifiers.add(qual2);
  -            paper.setQualifiers(qualifiers);
  -
  -            broker.beginTransaction();
  -            broker.store(qual1);
  -            broker.store(qual2);
  -            broker.store(paper);
  +            Paper paper = createPaper();
               Identity paperId = new Identity(paper, broker);
  -            broker.commitTransaction();
  -
  -            // sanity check
  -            broker.clearCache();
  -            broker.beginTransaction();
  -            Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -            qualifiers = retPaper.getQualifiers();
  -
  -            assertEquals(2, qualifiers.size());
  -            broker.commitTransaction();
  +            List qualifiers = paper.getQualifiers();
  +            Qualifier qual1 = (Qualifier) qualifiers.get(0);
  +            Qualifier qual2 = (Qualifier) qualifiers.get(1);
   
               // remove first object
  -            paper.getQualifiers().remove(0);
  +            qualifiers.remove(0);
               broker.beginTransaction();
               broker.store(paper);
               broker.commitTransaction();
   
               broker.clearCache();
               broker.beginTransaction();
  -            retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -            qualifiers = retPaper.getQualifiers();
  -            assertEquals(1, qualifiers.size());
  +            Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
  +            assertEquals(1, retPaper.getQualifiers().size());
   
  -            // target object qual1 should be deleted
  +            // target object qual1 should NOT be deleted
               Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1,
broker));
               Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2,
broker));
   
  -            assertNull(retQual1);
  +            assertNotNull(retQual1);
               assertNotNull(retQual2);
   
               broker.commitTransaction();
           }
           finally
           {
  -            // reset auto-delete
  -            cod.setCascadeDelete(cascadeDelete);
               cod.setCollectionClass(collectionClass);
           }
  +
       }
   
  -    // delete from intermediary table when auto-delete is OFF
  -    public void _testAutoDeleteOFF_RemovalAware()
  +    // delete from intermediary AND target-table when collection removal aware
  +    public void testDelete_RemovalAware()
       {
           ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
           CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
  -        boolean cascadeDelete = cod.getCascadeDelete();
           Class collectionClass = cod.getCollectionClass();
   
  -        // disable auto-delete
  -        cod.setCascadeDelete(false);
           cod.setCollectionClass(RemovalAwareCollection.class);
   
           try
           {
  -            String now = new Date().toString();
  -            Paper paper = new Paper();
  -            paper.setAuthor("Jonny Myers");
  -            paper.setDate(now);
  -
  -            Qualifier qual1 = new Topic();
  -            qual1.setName("qual1 " + now);
  -            Qualifier qual2 = new Topic();
  -            qual2.setName("qual2 " + now);
  -
  -            List qualifiers = new RemovalAwareCollection();
  -            qualifiers.add(qual1);
  -            qualifiers.add(qual2);
  -            paper.setQualifiers(qualifiers);
  -
  -            broker.beginTransaction();
  -            broker.store(qual1);
  -            broker.store(qual2);
  -            broker.store(paper);
  +            Paper paper = createPaper();
  +            List qualifiers = paper.getQualifiers();
  +            Qualifier qual1 = (Qualifier) qualifiers.get(0);
  +            Qualifier qual2 = (Qualifier) qualifiers.get(1);
               Identity paperId = new Identity(paper, broker);
  -            broker.commitTransaction();
  -
  -            // sanity check
  -            broker.clearCache();
  -            broker.beginTransaction();
  -            Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -            qualifiers = retPaper.getQualifiers();
  -
  -            assertEquals(2, qualifiers.size());
  -            broker.commitTransaction();
   
               // remove first object
  -            paper.getQualifiers().remove(0);
  +            qualifiers.remove(0);
               broker.beginTransaction();
               broker.store(paper);
               broker.commitTransaction();
   
               broker.clearCache();
               broker.beginTransaction();
  -            retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -            qualifiers = retPaper.getQualifiers();
  -            assertEquals(1, qualifiers.size());
  +            Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
  +            assertEquals(1, retPaper.getQualifiers().size());
   
  -            // target object qual1 should still be deleted
  +            // target object qual1 should be deleted
               Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1,
broker));
               Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2,
broker));
   
  -            assertNotNull(retQual1);
  +            assertNull(retQual1);
               assertNotNull(retQual2);
   
               broker.commitTransaction();
           }
           finally
           {
  -            // reset auto-delete
  -            cod.setCascadeDelete(cascadeDelete);
               cod.setCollectionClass(collectionClass);
           }
       }
   
       public void testDeletionFromIntermediaryTableWithNullList()
       {
  -        String now = new Date().toString();
  -        Paper paper = new Paper();
  -        paper.setAuthor("Jonny Myers");
  -        paper.setDate(now);
  -
  -        Qualifier qual1 = new Topic();
  -        qual1.setName("qual1 " + now);
  -        Qualifier qual2 = new Topic();
  -        qual2.setName("qual2 " + now);
  -
  -        List qualifiers = new Vector();
  -        qualifiers.add(qual1);
  -        qualifiers.add(qual2);
  -        paper.setQualifiers(qualifiers);
  -
  -        broker.beginTransaction();
  -        broker.store(qual1);
  -        broker.store(qual2);
  -        broker.store(paper);
  +        Paper paper = createPaper();
           Identity paperId = new Identity(paper, broker);
  -        broker.commitTransaction();
  -
  -        // sanity check
  -        broker.clearCache();
  -        broker.beginTransaction();
  -        Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -        qualifiers = retPaper.getQualifiers();
  -
  -        assertEquals(2, qualifiers.size());
  -        broker.commitTransaction();
  +        List qualifiers = paper.getQualifiers();
  +        Qualifier qual1 = (Qualifier) qualifiers.get(0);
  +        Qualifier qual2 = (Qualifier) qualifiers.get(1);
   
           // now set collection to null and check if changes get persisted
           paper.setQualifiers(null);
  @@ -412,61 +268,75 @@
   
           broker.clearCache();
           broker.beginTransaction();
  -        retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -        qualifiers = retPaper.getQualifiers();
  +        Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
  +        assertEquals(0, retPaper.getQualifiers().size());
   
  -        assertEquals(0, qualifiers.size());
  -        broker.commitTransaction();
  +        // target objects should NOT be deleted
  +        Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1,
broker));
  +        Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2,
broker));
   
  +        assertNotNull(retQual1);
  +        assertNotNull(retQual2);
  +
  +        broker.commitTransaction();
       }
   
  -    public void testDeletionFromIntermediaryTableWithEmptyList()
  +    public void testDeletionWithClearedList()
       {
  -        String now = new Date().toString();
  -        Paper paper = new Paper();
  -        paper.setAuthor("Jonny Myers");
  -        paper.setDate(now);
  -
  -        Qualifier qual1 = new Topic();
  -        qual1.setName("qual1 " + now);
  -        Qualifier qual2 = new Topic();
  -        qual2.setName("qual2 " + now);
  -
  -        List qualifiers = new Vector();
  -        qualifiers.add(qual1);
  -        qualifiers.add(qual2);
  -        paper.setQualifiers(qualifiers);
  +        Paper paper = createPaper();
  +        Identity paperId = new Identity(paper, broker);
  +        List qualifiers = paper.getQualifiers();
  +        Qualifier qual1 = (Qualifier) qualifiers.get(0);
  +        Qualifier qual2 = (Qualifier) qualifiers.get(1);
   
  +        // now clear collection 
  +        paper.getQualifiers().clear();
           broker.beginTransaction();
  -        broker.store(qual1);
  -        broker.store(qual2);
           broker.store(paper);
  -        Identity paperId = new Identity(paper, broker);
           broker.commitTransaction();
   
  -        // sanity check
           broker.clearCache();
           broker.beginTransaction();
           Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -        qualifiers = retPaper.getQualifiers();
  +        assertEquals(0, retPaper.getQualifiers().size());
  +
  +        // target objects should be deleted
  +        Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1,
broker));
  +        Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2,
broker));
  +
  +        assertNull(retQual1);
  +        assertNull(retQual2);
   
  -        assertEquals(2, qualifiers.size());
           broker.commitTransaction();
  +    }
  +
  +    public void testDeletionFromIntermediaryTableWithEmptyList()
  +    {
  +        Paper paper = createPaper();
  +        Identity paperId = new Identity(paper, broker);
  +        List qualifiers = paper.getQualifiers();
  +        Qualifier qual1 = (Qualifier) qualifiers.get(0);
  +        Qualifier qual2 = (Qualifier) qualifiers.get(1);
   
           // now empty collection and check if changes get persisted
  -        paper.setQualifiers(new Vector());
  +        paper.setQualifiers(new RemovalAwareCollection());
           broker.beginTransaction();
           broker.store(paper);
           broker.commitTransaction();
   
           broker.clearCache();
           broker.beginTransaction();
  -        retPaper = (Paper) broker.getObjectByIdentity(paperId);
  -        qualifiers = retPaper.getQualifiers();
  +        Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
  +        assertEquals(0, retPaper.getQualifiers().size());
   
  -        assertEquals(0, qualifiers.size());
  +        // target objects should NOT be deleted
  +        Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1,
broker));
  +        Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2,
broker));
  +
  +        assertNotNull(retQual1);
  +        assertNotNull(retQual2);
  +        
           broker.commitTransaction();
  -
       }
   
   }
  
  
  

Mime
View raw message