db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/odmg ObjectEnvelope.java
Date Sat, 15 Feb 2003 16:05:26 GMT
thma        2003/02/15 08:05:26

  Modified:    src/test/org/apache/ojb/odmg PersonImpl.java AllTests.java
               src/test/org/apache/ojb repository_junit.xml
               src/java/org/apache/ojb/odmg ObjectEnvelope.java
  Added:       src/test/org/apache/ojb/odmg PersonWithArrayTest.java
  Log:
  OJB131 fixed (applying Olivers patch)
  
  Revision  Changes    Path
  1.2       +7 -1      db-ojb/src/test/org/apache/ojb/odmg/PersonImpl.java
  
  Index: PersonImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/PersonImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PersonImpl.java	20 Jun 2002 17:07:53 -0000	1.1
  +++ PersonImpl.java	15 Feb 2003 16:05:26 -0000	1.2
  @@ -90,7 +90,8 @@
   
       public void addChild(Person pChild)
       {
  -        Person[] newKids = new Person[children.length + 1];
  +        int numOfChildren = ((children == null) ? 0 : children.length);
  +        Person[] newKids = new Person[numOfChildren + 1];
           ArrayList list = new ArrayList(Arrays.asList(children));
           list.add(pChild);
           list.toArray(newKids);
  @@ -149,4 +150,9 @@
           this.motherId = motherId;
       }
   
  +    public String toString()
  +    {
  +      int numOfChildren = ((children == null) ? 0 : children.length);
  +      return "PersonImpl@" + System.identityHashCode(this) + "(" + id + "): " + getFirstname()
+ " " + getLastname() + ", " + numOfChildren + " children";
  +    }
   }
  
  
  
  1.10      +1 -1      db-ojb/src/test/org/apache/ojb/odmg/AllTests.java
  
  Index: AllTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/AllTests.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- AllTests.java	16 Jan 2003 17:40:02 -0000	1.9
  +++ AllTests.java	15 Feb 2003 16:05:26 -0000	1.10
  @@ -54,7 +54,7 @@
           suite.addTest(new TestSuite(OQLOrOnForeignKeyTest.class));
           suite.addTest(new TestSuite(FieldConversion_ForeigenKeyTest.class));
   		//suite.addTest(new TestSuite(OdmgCollectionsTest.class));
  -
  +		suite.addTest(new TestSuite(PersonWithArrayTest.class));
           return suite;
       }
   }
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/odmg/PersonWithArrayTest.java
  
  Index: PersonWithArrayTest.java
  ===================================================================
  /**
   * (C) 2003 ppi Media
   * User: om
   */
  
  package org.apache.ojb.odmg;
  
  import org.apache.ojb.broker.TestHelper;
  import org.odmg.Implementation;
  import org.odmg.Database;
  import org.odmg.Transaction;
  import org.odmg.OQLQuery;
  import org.odmg.ODMGException;
  import org.odmg.QueryInvalidException;
  import org.odmg.QueryException;
  
  import java.util.List;
  import java.util.Iterator;
  import java.util.Collection;
  import java.util.Arrays;
  
  import junit.framework.TestCase;
  
  /**
   * class PersonWithArrayTest
   *
   * @author <a href="mailto:om@ppi.de">Oliver Matz</a>
   * @version $Id$
   */
  public class PersonWithArrayTest extends TestCase
  {
      private static Class CLASS = PersonWithArrayTest.class;
  
      private String databaseName;
  
      public void setUp() throws ODMGException
      {
          databaseName = TestHelper.DEF_DATABASE_NAME;
          deleteAll(PersonImpl.class);
      }
  
      final void deleteAll(Class clazz) throws ODMGException
      {
          Implementation odmg = OJB.getInstance();
  
          Database db = odmg.newDatabase();
          db.open(databaseName, Database.OPEN_READ_WRITE);
          Transaction tx = odmg.newTransaction();
          tx.begin();
          OQLQuery query = odmg.newOQLQuery();
          query.create("select person from " + clazz.getName());
          List persons = (List) query.execute();
          Iterator it = persons.iterator();
          while (it.hasNext())
          {
              Object obj = it.next();
              db.deletePersistent(obj);
          }
          tx.commit();
  
      }
      /**
       * Insert the method's description here.
       * Creation date: (06.12.2000 21:59:14)
       */
      public void tearDown() throws Exception
      {
          databaseName = null;
      }
  
      public void testStoreThreePersons()
      {
          Implementation odmg = OJB.getInstance();
          Database db = odmg.newDatabase();
          int id = (int) System.currentTimeMillis();
          //open database
          try
          {
              db.open(databaseName, Database.OPEN_READ_WRITE);
          }
          catch (ODMGException ex)
          {
              fail("ODMGException: " + ex.getMessage());
          }
  
          Person karlHeinz = new PersonImpl(id++, "Karl-Heinz", "Schultz", 0, 0);
  
          Person knuth = new PersonImpl(id++, "Knuth", null, 0, 0);
  
          Person kurt = new PersonImpl(id++, "Kurt", null, 0, 0);
  
          Person[] karlHeinz_children = new Person[] { knuth, kurt };
          karlHeinz.setChildren(karlHeinz_children);
  
          knuth.setFather(karlHeinz);
          kurt.setFather(karlHeinz);
          Transaction tx = odmg.newTransaction();
          tx.begin();
          tx.lock(karlHeinz, Transaction.WRITE);
          tx.lock(kurt, Transaction.WRITE);
          tx.lock(knuth, Transaction.WRITE);
          tx.commit();
  
          karlHeinz.setChildren(null);
          karlHeinz.setFirstname(null);
          tx.begin();
          // make sure all objects are retrieved freshly in subsequent transactions
           ((TransactionImpl) tx).getBroker().clearCache();
          OQLQuery qry = odmg.newOQLQuery();
          try
          {
              qry.create(
                  "select a from " + PersonImpl.class.getName() + " where firstname=\"Karl-Heinz\"");
          }
          catch (QueryInvalidException exc)
          {
              fail("Exception: " + exc.toString());
          }
          Collection result = null;
          try
          {
              result = (Collection) qry.execute();
          }
          catch (QueryException exc)
          {
              fail("Exception: " + exc.toString());
          }
          assertEquals("Exactly one element in result set", result.size(), 1);
          Person retKarlHeinz = (Person) (result.iterator().next());
          assertTrue("not same", retKarlHeinz != karlHeinz);
          Person[] retKarlHeinz_children = retKarlHeinz.getChildren();
          assertNotNull(retKarlHeinz_children);
          // unfortunately, PersonImpl does not have a suitable equals method.
          assertEquals(
              "children's names are equal",
              Arrays.asList(getFirstNames(retKarlHeinz_children)),
              Arrays.asList(getFirstNames(karlHeinz_children)));
          System.out.println(Arrays.asList(getFirstNames(retKarlHeinz_children)));
          tx.abort();
      }
  
      private static String[] getFirstNames(Person[] persons)
      {
          int length = persons == null ? 0 : persons.length;
          String[] ret = new String[length];
          for (int i = 0; i < ret.length; i++)
          {
              ret[i] = persons[i].getFirstname();
          }
          return ret;
  
      }
  
      public static void main(String[] args)
      {
          String[] arr = { CLASS.getName()};
          junit.textui.TestRunner.main(arr);
      }
  }
  
  
  
  1.41      +7 -1      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.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- repository_junit.xml	7 Feb 2003 20:22:36 -0000	1.40
  +++ repository_junit.xml	15 Feb 2003 16:05:26 -0000	1.41
  @@ -1383,7 +1383,7 @@
   
   
   
  -<!-- Definitions for org.apache.ojb.broker.Person -->
  +<!-- Definitions for org.apache.odmg.broker.PersonImpl -->
      <class-descriptor
      	  class="org.apache.ojb.odmg.PersonImpl"
      	  table="FAMILY_MEMBER"
  @@ -1429,6 +1429,12 @@
         >
            <foreignkey field-ref="fatherId"/>
         </reference-descriptor>
  +      <collection-descriptor
  +          name="children"
  +          element-class-ref="org.apache.ojb.odmg.PersonImpl"
  +      >
  +          <inverse-foreignkey field-ref="fatherId"/>
  +     </collection-descriptor>
   
      </class-descriptor>
   
  
  
  
  1.17      +29 -21    db-ojb/src/java/org/apache/ojb/odmg/ObjectEnvelope.java
  
  Index: ObjectEnvelope.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/ObjectEnvelope.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- ObjectEnvelope.java	6 Feb 2003 21:03:17 -0000	1.16
  +++ ObjectEnvelope.java	15 Feb 2003 16:05:26 -0000	1.17
  @@ -112,7 +112,6 @@
        * end of the transaction.
        */
       private transient Map beforeImage;
  -    private transient Map afterImage;
   
       private transient TransactionImpl tx;
   
  @@ -120,7 +119,6 @@
   	{
   		myObj = null;
   		beforeImage = null;
  -		afterImage = null;
   		tx = null;
   	}
       /**
  @@ -148,7 +146,7 @@
           catch (Exception ex)
           {
               beforeImage = null;
  -            throw new org.odmg.ClassNotPersistenceCapableException(ex.getMessage());
  +            throw new org.odmg.ClassNotPersistenceCapableException(ex.toString());
           }
       }
   
  @@ -309,29 +307,39 @@
            * How do we handle proxied collections and collections of proxies
            * @see org.apache.ojb.odmg.OneToManyTest
            */
  -		Iterator collections = mif.getCollectionDescriptors().iterator();
  -		CollectionDescriptor collectionDescriptor = null;
  -        Collection items = null;
  -		while (collections.hasNext())
  -		{
  -			collectionDescriptor = (CollectionDescriptor) collections.next();
  -            items = (Collection)collectionDescriptor.getPersistentField().get(myObj);
  +        Iterator collections = mif.getCollectionDescriptors().iterator();
  +        CollectionDescriptor collectionDescriptor = null;
  +        while (collections.hasNext())
  +        {
  +            collectionDescriptor = (CollectionDescriptor) collections.next();
  +            Object items = collectionDescriptor.getPersistentField().get(myObj);
               if (items != null)
               {
  -                /**
  -                 * MBAIRD
  -                 * size isn't the safest thing to use as the dirty bit. This will
  -                 * need to be fixed.
  -                 *
  -                 * if items are a collectionproxy, and it's not already loaded, don't register
it.
  -                 */
  -                if (( items instanceof CollectionProxy) && !( (CollectionProxy)
items).isLoaded())
  +                // items might be a Collection or an array.
  +                if (items instanceof Collection)
                   {
  -                	continue;
  +                    /*
  +                     * if items are a collectionproxy, and it's not already loaded, don't
register it.
  +                     */
  +                    if (!( items instanceof CollectionProxy) || ( (CollectionProxy) items).isLoaded())
  +                    {
  +                            continue;
  +                    }
  +                    /**
  +                     * MBAIRD
  +                     * size isn't the safest thing to use as the dirty bit. This will
  +                     * need to be fixed.
  +                     */
  +                    Object dirtyMark = new Integer(((Collection)items).size());
  +                    fieldValues.put(collectionDescriptor, dirtyMark);
  +                }
  +                else  // items instanceof Object[]
  +                {
  +                    Object dirtyMark = new Integer(items.hashCode());
  +                    fieldValues.put(collectionDescriptor, dirtyMark);
                   }
  -                fieldValues.put(collectionDescriptor, new Integer(items.size()));
               }
  -		}
  +        }
           return fieldValues;
       }
   
  
  
  

Mime
View raw message