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 Thu, 26 Jun 2003 19:45:56 GMT
brj         2003/06/26 12:45:56

  Modified:    src/test/org/apache/ojb/broker MtoNTest.java
  Log:
  testcase for m:n auto-delete with RemovalAwareCollection
  
  Revision  Changes    Path
  1.4       +281 -117  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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MtoNTest.java	26 Jun 2003 19:16:39 -0000	1.3
  +++ MtoNTest.java	26 Jun 2003 19:45:56 -0000	1.4
  @@ -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.RemovalAwareCollection;
   
   /**
    * @author <a href="mailto:om@ppi.de">Oliver Matz</a>
  @@ -24,7 +25,7 @@
   
       public static void main(String[] args)
       {
  -        String[] arr = {CLASS.getName()};
  +        String[] arr = { CLASS.getName()};
           junit.textui.TestRunner.main(arr);
       }
   
  @@ -80,130 +81,293 @@
       }
   
       // delete from intermediary table when auto-delete is OFF
  -    public void testDeletionFromIntermediaryTable()
  +    public void testAutoDeleteOFF()
       {
  -        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);     
  -        Identity paperId = new Identity(paper, broker);
  -        broker.commitTransaction();
  +        ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
  +        CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
  +        boolean cascadeDelete = cod.getCascadeDelete();
   
  -        // 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();
  +        // disable auto-delete
  +        cod.setCascadeDelete(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);
  +
  +            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(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);
  +
  +            broker.commitTransaction();
  +        }
  +        finally
  +        {
  +            // reset auto-delete
  +            cod.setCascadeDelete(cascadeDelete);
  +        }
   
  -        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);   
  -             
  -        broker.commitTransaction();
  -               
       }
   
       // delete from intermediary AND target-table when auto-delete is ON
  -    public void testDeletionFromBothTables()
  +    public void testAutoDeleteON()
       {
           ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
           CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
           boolean cascadeDelete = cod.getCascadeDelete();
  -        
  +
           // enable auto-delete
           cod.setCascadeDelete(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);        
  +            qualifiers.add(qual2);
  +            paper.setQualifiers(qualifiers);
  +
  +            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(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 still be deleted
  +            Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1,
broker));
  +            Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2,
broker));
  +
  +            assertNull(retQual1);
  +            assertNotNull(retQual2);
  +
  +            broker.commitTransaction();
  +        }
  +        finally
  +        {
  +            // reset auto-delete
  +            cod.setCascadeDelete(cascadeDelete);
  +        }
  +    }
  +
  +    // delete from intermediary AND target-table when auto-delete is ON
  +    public void testAutoDeleteON_RemovalAware()
  +    {
  +        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);
  +
  +        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);
  +            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 still be deleted
  +            Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1,
broker));
  +            Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2,
broker));
  +
  +            assertNull(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()
  +    {
  +        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.beginTransaction();
               broker.store(qual1);
  -            broker.store(qual2);   
  -            broker.store(paper);     
  +            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);        
  +            Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
               qualifiers = retPaper.getQualifiers();
  -            
  -            assertEquals(2, qualifiers.size());        
  +
  +            assertEquals(2, qualifiers.size());
               broker.commitTransaction();
  -            
  +
               // remove first object
               paper.getQualifiers().remove(0);
               broker.beginTransaction();
  -            broker.store(paper); 
  +            broker.store(paper);
               broker.commitTransaction();
  -            
  +
               broker.clearCache();
               broker.beginTransaction();
  -            retPaper = (Paper) broker.getObjectByIdentity(paperId);        
  -            qualifiers = retPaper.getQualifiers();           
  -            assertEquals(1, qualifiers.size());        
  -            
  +            retPaper = (Paper) broker.getObjectByIdentity(paperId);
  +            qualifiers = retPaper.getQualifiers();
  +            assertEquals(1, qualifiers.size());
  +
               // target object qual1 should still be deleted
  -            Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1,broker));

  -            Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2,broker));

  +            Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1,
broker));
  +            Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2,
broker));
  +
  +            assertNotNull(retQual1);
  +            assertNotNull(retQual2);
   
  -            assertNull(retQual1);   
  -            assertNotNull(retQual2);   
  -            
  -            broker.commitTransaction();         
  +            broker.commitTransaction();
           }
           finally
           {
               // reset auto-delete
               cod.setCascadeDelete(cascadeDelete);
  +            cod.setCollectionClass(collectionClass);
           }
       }
   
  @@ -213,47 +377,47 @@
           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);        
  +        qualifiers.add(qual2);
           paper.setQualifiers(qualifiers);
  -        
  -        broker.beginTransaction();        
  +
  +        broker.beginTransaction();
           broker.store(qual1);
  -        broker.store(qual2);   
  -        broker.store(paper);     
  +        broker.store(qual2);
  +        broker.store(paper);
           Identity paperId = new Identity(paper, broker);
           broker.commitTransaction();
   
  -		// sanity check
  +        // sanity check
           broker.clearCache();
           broker.beginTransaction();
  -        Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);        
  +        Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
           qualifiers = retPaper.getQualifiers();
  -        
  -        assertEquals(2, qualifiers.size());        
  +
  +        assertEquals(2, qualifiers.size());
           broker.commitTransaction();
  -        
  +
           // now set collection to null and check if changes get persisted
           paper.setQualifiers(null);
  -     	broker.beginTransaction();
  -     	broker.store(paper); 
  -		broker.commitTransaction();
  +        broker.beginTransaction();
  +        broker.store(paper);
  +        broker.commitTransaction();
   
           broker.clearCache();
           broker.beginTransaction();
  -        retPaper = (Paper) broker.getObjectByIdentity(paperId);        
  +        retPaper = (Paper) broker.getObjectByIdentity(paperId);
           qualifiers = retPaper.getQualifiers();
  -        
  -        assertEquals(0, qualifiers.size());        
  +
  +        assertEquals(0, qualifiers.size());
           broker.commitTransaction();
  -		       
  +
       }
   
       public void testDeletionFromIntermediaryTableWithEmptyList()
  @@ -262,47 +426,47 @@
           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);        
  +        qualifiers.add(qual2);
           paper.setQualifiers(qualifiers);
  -        
  -        broker.beginTransaction();        
  +
  +        broker.beginTransaction();
           broker.store(qual1);
  -        broker.store(qual2);   
  -        broker.store(paper);     
  +        broker.store(qual2);
  +        broker.store(paper);
           Identity paperId = new Identity(paper, broker);
           broker.commitTransaction();
   
  -		// sanity check
  +        // sanity check
           broker.clearCache();
           broker.beginTransaction();
  -        Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);        
  +        Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
           qualifiers = retPaper.getQualifiers();
  -        
  -        assertEquals(2, qualifiers.size());        
  +
  +        assertEquals(2, qualifiers.size());
           broker.commitTransaction();
  -        
  +
           // now empty collection and check if changes get persisted
           paper.setQualifiers(new Vector());
  -     	broker.beginTransaction();
  -     	broker.store(paper); 
  -		broker.commitTransaction();
  +        broker.beginTransaction();
  +        broker.store(paper);
  +        broker.commitTransaction();
   
           broker.clearCache();
           broker.beginTransaction();
  -        retPaper = (Paper) broker.getObjectByIdentity(paperId);        
  +        retPaper = (Paper) broker.getObjectByIdentity(paperId);
           qualifiers = retPaper.getQualifiers();
  -        
  -        assertEquals(0, qualifiers.size());        
  +
  +        assertEquals(0, qualifiers.size());
           broker.commitTransaction();
  -		       
  +
       }
   
   }
  
  
  

Mime
View raw message