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 repository_junit_odmg.xml
Date Sat, 26 Nov 2005 02:19:55 GMT
arminw      2005/11/25 18:19:55

  Modified:    src/test/org/apache/ojb/odmg Tag: OJB_1_0_RELEASE
                        ObjectImageTest.java
               src/test/org/apache/ojb Tag: OJB_1_0_RELEASE
                        repository_junit_odmg.xml
  Log:
  add new tests
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.2.6   +294 -27   db-ojb/src/test/org/apache/ojb/odmg/ObjectImageTest.java
  
  Index: ObjectImageTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/ObjectImageTest.java,v
  retrieving revision 1.1.2.5
  retrieving revision 1.1.2.6
  diff -u -r1.1.2.5 -r1.1.2.6
  --- ObjectImageTest.java	7 May 2005 16:02:57 -0000	1.1.2.5
  +++ ObjectImageTest.java	26 Nov 2005 02:19:54 -0000	1.1.2.6
  @@ -20,9 +20,15 @@
   import java.util.Collection;
   import java.util.Date;
   import java.util.List;
  +import java.util.Iterator;
   
   import org.apache.commons.lang.SerializationUtils;
  +import org.apache.commons.lang.SystemUtils;
  +import org.apache.commons.lang.builder.EqualsBuilder;
   import org.apache.ojb.broker.Identity;
  +import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryFactory;
   import org.apache.ojb.broker.core.proxy.CollectionProxy;
   import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl;
   import org.apache.ojb.broker.core.proxy.CollectionProxyListener;
  @@ -43,15 +49,264 @@
    *
    * Test cases for refactored odmg-api implementation. These tests asserts previously outstanding
    * ODMG-issues and proxy object handling in the ODMG API.
  - * 
  + *
    * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
    * @version $Id$
    */
   public class ObjectImageTest extends ODMGTestCase
   {
  +    static final int NONE = ObjectReferenceDescriptor.CASCADE_NONE;
  +    static final int LINK = ObjectReferenceDescriptor.CASCADE_LINK;
  +    static final int OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT;
  +    static final String EOL = SystemUtils.LINE_SEPARATOR;
  +
  +    public static void main(String[] args)
  +    {
  +        junit.textui.TestRunner.main(new String[]{ObjectImageTest.class.getName()});
  +    }
  +
  +    public void testReplaceOneToOneReference() throws Exception
  +    {
  +        String prefix = "testReplaceOneToOneReference_" + System.currentTimeMillis();
  +        ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, false);
  +
  +        TransactionExt tx = (TransactionExt) odmg.newTransaction();
  +        tx.begin();
  +        Book book = new Book(prefix, null, null);
  +        Publisher p_1 = new PublisherImpl(prefix);
  +        Publisher p_2 = new PublisherImpl(prefix + "_replaced");
  +        book.setPublisher(p_1);
  +        database.makePersistent(book);
  +        database.makePersistent(p_1);
  +        database.makePersistent(p_2);
  +        tx.commit();
  +
  +        Integer book_version = book.getVersion();
  +        Integer p1_version = p_1.getVersion();
  +        Integer p2_version = p_2.getVersion();
  +
  +        tx.begin();
  +        tx.lock(book, Transaction.WRITE);
  +        tx.lock(book.getPublisher(), Transaction.READ);
  +        tx.lock(p_2, Transaction.READ);
  +        book.setPublisher(p_2);
  +        tx.commit();
  +
  +        assertEquals(book_version.intValue() + 1, book.getVersion().intValue());
  +        assertEquals(p1_version, p_1.getVersion());
  +        assertEquals(p2_version, p_2.getVersion());
  +    }
  +
  +    public void testAddCollectionObjectToExistingObject() throws Exception
  +    {
  +        String prefix = "testAddCollectionObjectToExistingObject_" + System.currentTimeMillis();
  +        ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, false);
  +
  +        TransactionExt tx = (TransactionExt) odmg.newTransaction();
  +        tx.begin();
  +        Book book = new Book(prefix, null, null);
  +        database.makePersistent(book);
  +        tx.commit();
  +        Integer book_version = book.getVersion();
  +
  +        Review r1 = new Review(prefix + "_1");
  +        Review r2 = new Review(prefix + "_2");
  +        tx.begin();
  +        tx.lock(book, Transaction.READ);
  +        book.addReview(r1);
  +        book.addReview(r2);
  +        database.makePersistent(r1);
  +        database.makePersistent(r2);
  +        tx.commit();
  +
  +        assertEquals(book_version, book.getVersion());
  +        assertTrue(book.getId() != null);
  +
  +        Integer r_1_version = r1.getVersion();
  +        Integer r_2_version = r2.getVersion();
  +
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +        Book loadedCopy = (Book) tx.getBroker().getObjectByIdentity(
  +                tx.getBroker().serviceIdentity().buildIdentity(Book.class, book.getId()));
  +        assertNotNull(loadedCopy);
  +        assertNotNull(loadedCopy.getReviews());
  +        assertEquals(2, loadedCopy.getReviews().size());
  +        assertEquals(book_version, loadedCopy.getVersion());
  +
  +        tx.getBroker().clearCache();
  +        Criteria criteria = new Criteria();
  +        criteria.addLike("title", prefix);
  +        Query q = QueryFactory.newQuery(Book.class, criteria);
  +        Collection books = tx.getBroker().getCollectionByQuery(q);
  +        assertNotNull(books);
  +        assertEquals(1, books.size());
  +        Book new_book = (Book) books.iterator().next();
  +        tx.commit();
  +        assertEquals(book_version, new_book.getVersion());
  +
  +        tx.begin();
  +        tx.lock(loadedCopy, Transaction.WRITE);
  +        Review removed = (Review) loadedCopy.getReviews().remove(0);
  +        Review stayed =  (Review) loadedCopy.getReviews().get(0);
  +        tx.commit();
  +        // expect same version, nothing should be changed
  +        assertEquals(r_1_version, stayed.getVersion());
  +        //
  +        //assertEquals(r_1_version, removed.getVersion());
  +
  +        tx.begin();
  +        OQLQuery query = odmg.newOQLQuery();
  +        query.create("select books from " + Book.class.getName() + " where title like $1");
  +        query.bind(prefix);
  +        Collection result = (Collection) query.execute();
  +        assertEquals(1, result.size());
  +        Book b = (Book) result.iterator().next();
  +        tx.commit();
  +
  +        assertEquals(1, b.getReviews().size());
  +        Review r = (Review) b.getReviews().get(0);
  +        if(!r.equals(r1) && !r.equals(r2))
  +        {
  +            fail("Wrong object or wrong object version returned. Returned obj was "
  +                    + EOL + r + " expected object was " + EOL + r1 + " or " + EOL + r2);
  +        }
  +    }
  +
  +    /**
  +     * test persistence by reachability of collection reference objects
  +     */
  +    public void testPersistenceByReachability_1() throws Exception
  +    {
  +        String name = "testPersistenceByReachability_1_" + System.currentTimeMillis();
  +        ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, true);
  +
  +        Date date = new Date();
  +        byte[] cover = new byte[]{2,3,4,5,6,7,8,9};
  +        Book book = new Book(name, date, cover);
  +        Review r1 = new Review(name);
  +        Review r2 = new Review(name);
  +        Review r3 = new Review(name + "_not_persistent");
  +        ArrayList reviews = new ArrayList();
  +        reviews.add(r1);
  +        reviews.add(r2);
  +        reviews.add(r3);
  +        book.setReviews(reviews);
  +
  +        TransactionExt tx = (TransactionExt) odmg.newTransaction();
  +        tx.begin();
  +        database.makePersistent(book);
  +        Review r4 = new Review(name + "_new_added");
  +        // add a new review after make persistent main object
  +        book.addReview(r4);
  +        tx.setCascadingDelete(Book.class, true);
  +        // remove object after make persistent main object
  +        book.removeReview(r3);
  +        tx.commit();
  +        // System.err.println("## Insert main object with 3 referecnes");
  +
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +
  +        OQLQuery query = odmg.newOQLQuery();
  +        query.create("select books from " + Book.class.getName() + " where title like $1");
  +        query.bind(name);
  +        Collection result = (Collection) query.execute();
  +        assertEquals(1, result.size());
  +        Book b = (Book) result.iterator().next();
  +        assertNotNull(b.getReviews());
  +        assertEquals(3, b.getReviews().size());
  +
  +        query = odmg.newOQLQuery();
  +        query.create("select reviews from " + Review.class.getName() + " where summary
like $1");
  +        query.bind(name + "_new_added");
  +        result = (Collection) query.execute();
  +        // we expect the delayed added Review object
  +        assertEquals(1, result.size());
  +
  +        query = odmg.newOQLQuery();
  +        query.create("select reviews from " + Review.class.getName() + " where summary
like $1");
  +        query.bind(name + "_not_persistent");
  +        result = (Collection) query.execute();
  +        // we expect the removed Review object wasn't persistet
  +        assertEquals(0, result.size());
  +        tx.commit();
  +    }
  +
  +    /**
  +     * test persistence by reachability of collection reference objects
  +     */
  +    public void testPersistenceByReachability_2() throws Exception
  +    {
  +        String name = "testPersistenceByReachability_2_" + System.currentTimeMillis();
  +        ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, true);
  +
  +        Date date = new Date();
  +        byte[] cover = new byte[]{2,3,4,5,6,7,8,9};
  +        Book book = new Book(name, date, cover);
  +        Review r1 = new Review(name);
  +        Review r2 = new Review(name);
  +        Review r3 = new Review(name + "_not_persistent");
  +        ArrayList reviews = new ArrayList();
  +        reviews.add(r1);
  +        reviews.add(r2);
  +        reviews.add(r3);
  +        book.setReviews(reviews);
  +
  +        TransactionExt tx = (TransactionExt) odmg.newTransaction();
  +        tx.begin();
  +        database.makePersistent(book);
  +        Review r4 = new Review(name + "_new_added");
  +        // add a new review after make persistent main object
  +        book.addReview(r4);
  +        tx.setCascadingDelete(Book.class, true);
  +        // remove object after make persistent main object
  +        book.removeReview(r3);
  +        tx.checkpoint();
  +
  +        //tx.begin();
  +        tx.getBroker().clearCache();
  +
  +        OQLQuery query = odmg.newOQLQuery();
  +        query.create("select books from " + Book.class.getName() + " where title like $1");
  +        query.bind(name);
  +        Collection result = (Collection) query.execute();
  +        assertEquals(1, result.size());
  +        Book b = (Book) result.iterator().next();
  +        assertNotNull(b.getReviews());
  +        assertEquals(3, b.getReviews().size());
  +
  +        query = odmg.newOQLQuery();
  +        query.create("select reviews from " + Review.class.getName() + " where summary
like $1");
  +        query.bind(name + "_new_added%");
  +        result = (Collection) query.execute();
  +        // we expect the delayed added Review object
  +        assertEquals(1, result.size());
  +
  +        query = odmg.newOQLQuery();
  +        query.create("select reviews from " + Review.class.getName() + " where summary
like $1");
  +        query.bind(name + "_not_persistent");
  +        result = (Collection) query.execute();
  +        // we expect the removed Review object wasn't persistet
  +        assertEquals(0, result.size());
  +
  +        b.setTitle(name + "_updated");
  +        tx.commit();
  +
  +        query = odmg.newOQLQuery();
  +        query.create("select books from " + Book.class.getName() + " where title like $1");
  +        query.bind(name + "_updated");
  +        result = (Collection) query.execute();
  +        assertEquals(1, result.size());
  +        Book b_updated = (Book) result.iterator().next();
  +        assertNotNull(b_updated.getReviews());
  +        assertEquals(3, b_updated.getReviews().size());
  +        assertEquals(name+"_updated", b_updated.getTitle());
  +    }
  +
       public void testAddPersistentObjectTo1toN() throws Exception
       {
  -        String name = "testAddPersistentObjectOnNSide_" + System.currentTimeMillis();
  +        String name = "testAddPersistentObjectTo1toN_" + System.currentTimeMillis();
           Review review = new Review(name);
           TransactionExt tx = (TransactionExt) odmg.newTransaction();
           tx.begin();
  @@ -88,7 +343,7 @@
   
       public void testAddPersistentObjectToMtoN() throws Exception
       {
  -        String name = "testAddPersistentObjectOnNSide_" + System.currentTimeMillis();
  +        String name = "testAddPersistentObjectToMtoN_" + System.currentTimeMillis();
           Author author = new Author(name, null);
           TransactionExt tx = (TransactionExt) odmg.newTransaction();
           tx.begin();
  @@ -266,9 +521,8 @@
       public void testChangeOneToOneReference_1b() throws Exception
       {
           String name = "testChangeOneToOneReference_1b_" + System.currentTimeMillis();
  -        ojbChangeReferenceSetting(Book.class, "publisher", true,
  -                ObjectReferenceDescriptor.CASCADE_LINK,
  -                ObjectReferenceDescriptor.CASCADE_LINK, true);
  +        ojbChangeReferenceSetting(Book.class, "publisher", true, NONE, NONE, true);
  +
           Date date = new Date();
           byte[] cover = new byte[]{2,3,4,5,6,7,8,9};
           Book book = new Book(name, date, cover);
  @@ -446,9 +700,7 @@
        */
       public void testChangeOneToOneReference_4() throws Exception
       {
  -        ojbChangeReferenceSetting(Book.class, "publisher", true,
  -                ObjectReferenceDescriptor.CASCADE_LINK,
  -                ObjectReferenceDescriptor.CASCADE_LINK, true);
  +        ojbChangeReferenceSetting(Book.class, "publisher", true, NONE, NONE, true);
           String name = "testChangeOneToOneReference_4_" + System.currentTimeMillis();
           Date date = new Date();
           byte[] cover = new byte[]{2,3,4,5,6,7,8,9};
  @@ -498,9 +750,7 @@
        */
       public void testChangeOneToOneReference_5() throws Exception
       {
  -        ojbChangeReferenceSetting(Book.class, "publisher", true,
  -                ObjectReferenceDescriptor.CASCADE_LINK,
  -                ObjectReferenceDescriptor.CASCADE_LINK, true);
  +        ojbChangeReferenceSetting(Book.class, "publisher", true, NONE, NONE, true);
           String name = "testChangeOneToOneReference_5_" + System.currentTimeMillis();
           Date date = new Date();
           byte[] cover = new byte[]{2,3,4,5,6,7,8,9};
  @@ -784,10 +1034,7 @@
       public void testCollectionReference_1() throws Exception
       {
           String name = "testCollectionReference_1_" + System.currentTimeMillis();
  -
  -        ojbChangeReferenceSetting(Book.class, "reviews", true,
  -                ObjectReferenceDescriptor.CASCADE_LINK,
  -                ObjectReferenceDescriptor.CASCADE_NONE, true);
  +        ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, true);
   
           Date date = new Date();
           byte[] cover = new byte[]{2,3,4,5,6,7,8,9};
  @@ -921,9 +1168,7 @@
       {
           String name = "testCollectionReference_2b_" + System.currentTimeMillis();
   
  -        ojbChangeReferenceSetting(Book.class, "reviews", true,
  -                ObjectReferenceDescriptor.CASCADE_LINK,
  -                ObjectReferenceDescriptor.CASCADE_NONE, true);
  +        ojbChangeReferenceSetting(Book.class, "reviews", true, NONE, NONE, true);
   
           Date date = new Date();
           byte[] cover = new byte[]{2,3,4,5,6,7,8,9};
  @@ -1358,8 +1603,18 @@
           assertNotNull(b.getAuthors());
           assertEquals(2, b.getAuthors().size());
           Author newA = (Author) b.getAuthors().get(0);
  -        // as we have circular references we expect the same object instance
  -        assertTrue(newA==((Review) b.getReviews().get(0)).getAuthor());
  +        boolean failed = true;
  +        for(Iterator iterator = b.getReviews().iterator(); iterator.hasNext();)
  +        {
  +            Review review =  (Review) iterator.next();
  +            if(newA.equals(review.getAuthor()))
  +            {
  +                // as we have circular references we expect the same object instance
  +                assertSame(newA, review.getAuthor());
  +                failed = false;
  +            }
  +        }
  +        if(failed) fail("Expect the same object instance, but not found for " + newA);
   
           // Book instance should be already locked
           // now mark Book for delete and disable cascading delete
  @@ -1397,11 +1652,6 @@
           assertEquals(0, result.size());
       }
   
  -    public static void main(String[] args)
  -    {
  -        junit.textui.TestRunner.main(new String[]{ObjectImageTest.class.getName()});
  -    }
  -
       //=======================================================
       // inner test classes
       //=======================================================
  @@ -1446,6 +1696,12 @@
               reviews.add(review);
           }
   
  +        public boolean removeReview(Review review)
  +        {
  +            if(reviews != null) return reviews.remove(review);
  +            else return false;
  +        }
  +
           public Integer getId()
           {
               return id;
  @@ -1716,6 +1972,17 @@
           {
               this.author = author;
           }
  +
  +        public boolean equals(Object obj)
  +        {
  +            boolean result = false;
  +            if(obj instanceof Review)
  +            {
  +                Review other = (Review) obj;
  +                result = new EqualsBuilder().append(id, other.id).append(version, other.version).isEquals();
  +            }
  +            return result;
  +        }
       }
   
   }
  
  
  
  No                   revision
  No                   revision
  1.13.2.18 +2 -1      db-ojb/src/test/org/apache/ojb/repository_junit_odmg.xml
  
  Index: repository_junit_odmg.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit_odmg.xml,v
  retrieving revision 1.13.2.17
  retrieving revision 1.13.2.18
  diff -u -r1.13.2.17 -r1.13.2.18
  --- repository_junit_odmg.xml	13 Nov 2005 15:45:55 -0000	1.13.2.17
  +++ repository_junit_odmg.xml	26 Nov 2005 02:19:54 -0000	1.13.2.18
  @@ -1779,6 +1779,7 @@
           auto-update="none"
           auto-delete="none"
           >
  +        <orderby name="fkBook" sort="ASC"/>
           <inverse-foreignkey field-ref="fkBook"/>
       </collection-descriptor>
   
  
  
  

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