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/broker PersistenceBrokerTest.java
Date Sat, 12 Jul 2003 16:17:24 GMT
arminw      2003/07/12 09:17:23

  Modified:    src/java/org/apache/ojb/broker/core
                        PersistenceBrokerImpl.java
               src/java/org/apache/ojb/broker/util ObjectModification.java
               src/test/org/apache/ojb/broker PersistenceBrokerTest.java
  Log:
  - rollback made changes. Do not use Identity
  objects in markedForDelete list, because this
  is a performance impact. Put the object itself
  in the list again.
  - Remove blank lines
  - add test for double store/delete of objects
  
  Revision  Changes    Path
  1.27      +17 -12    db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java
  
  Index: PersistenceBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- PersistenceBrokerImpl.java	10 Jul 2003 22:05:17 -0000	1.26
  +++ PersistenceBrokerImpl.java	12 Jul 2003 16:17:23 -0000	1.27
  @@ -409,10 +409,13 @@
               /**
                * MBAIRD
                * 1. if we are marked for delete already, avoid recursing on this object
  +             *
  +             * arminw:
  +             * use object instead Identity object in markedForDelete List,
  +             * because using objects we get a better performance. I can't find
  +             * side-effects in doing so.
                */
  -            ClassDescriptor cld = descriptorRepository.getDescriptorFor(obj.getClass());
  -            Identity oid = new Identity(obj, this, cld);
  -            if (markedForDelete.contains(oid))
  +            if (markedForDelete.contains(obj))
               {
                   return;
               }
  @@ -420,7 +423,9 @@
                * MBAIRD
                * 2. register object in markedForDelete map.
                */
  -            markedForDelete.add(oid);
  +            markedForDelete.add(obj);
  +            ClassDescriptor cld = descriptorRepository.getDescriptorFor(obj.getClass());
  +            Identity oid = new Identity(obj, this, cld);
               // Invoke events on PersistenceBrokerAware instances and listeners
               BEFORE_DELETE_EVENT.setTarget(obj);
               fireBrokerEvent(BEFORE_DELETE_EVENT);
  @@ -2516,16 +2521,16 @@
       {
           //look for a collection descriptor on left  such as left.element-class-ref='right'
           DescriptorRepository dr = getDescriptorRepository();
  -        
  +
           Object leftObject = m2nImpl.getLeftObject();
           Class leftClass = leftObject.getClass();
           Object rightObject = m2nImpl.getRightObject();
           Class rightClass = rightObject.getClass();
  -        
  +
           logger.debug("Deleting MtoN implementor ["+leftClass+"] ["+rightClass+"]");
  -        
  +
           //
  -        //are written per class, maybe referencing abstract classes or interfaces 
  +        //are written per class, maybe referencing abstract classes or interfaces
           //so let's look for collection descriptors on the left class and try to handle
extents on teh right class
           ClassDescriptor leftCld = dr.getDescriptorFor(leftClass);
           ClassDescriptor rightCld = dr.getDescriptorFor(rightClass);
  @@ -2547,7 +2552,7 @@
                   +"] right["+rightClass+"]");
           }
   
  -        
  +
           if(leftObject == null || rightObject == null)
           {
               //TODO: to be implemented, must change MtoNImplementor
  @@ -2559,14 +2564,14 @@
               Object[] leftPkValues = brokerHelper.getKeyValues(leftCld, leftObject);
               Object[] rightPkValues = brokerHelper.getKeyValues(rightCld,rightObject);
               String[] pkLeftColumns = wanted.getFksToThisClass();
  -            String[] pkRightColumns = wanted.getFksToItemClass();            
  +            String[] pkRightColumns = wanted.getFksToItemClass();
               String table = wanted.getIndirectionTable();
               if(table == null) throw new PersistenceBrokerException("Can't remove MtoN implementor
withou an indirection table");
               String deleteStmt = sqlGenerator.getDeleteMNStatement(table, pkLeftColumns,
pkRightColumns);
               dbAccess.executeUpdateSQL(deleteStmt, leftCld, leftPkValues, rightPkValues);
           }
       }
  -    
  +
       /**
        * @see org.apache.ojb.broker.PersistenceBroker#addMtoNImplementor()
        */
  
  
  
  1.3       +22 -25    db-ojb/src/java/org/apache/ojb/broker/util/ObjectModification.java
  
  Index: ObjectModification.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/ObjectModification.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ObjectModification.java	17 Jun 2002 19:34:24 -0000	1.2
  +++ ObjectModification.java	12 Jul 2003 16:17:23 -0000	1.3
  @@ -53,30 +53,27 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    */
  -
  -
  -/**
  - *
  - * The Interface ObjectModification represents information about
  - * modifications of persistence capable objects.
  - * Allows clients of the PersistenceBroker (e.g. a TransactionServer)
  - * to interact with the Broker in order to generate optimized SQL Statements.
  - *
  - * @author Thomas Mahler
  - * @version $Id$
  - */
  -public interface ObjectModification extends java.io.Serializable
  -{
  -
  -    /**
  -     * returns true if the underlying Object needs an INSERT statement.
  -     * else Returns false.
  -     */
  +
  +/**
  + *
  + * The Interface ObjectModification represents information about
  + * modifications of persistence capable objects.
  + * Allows clients of the PersistenceBroker (e.g. a TransactionServer)
  + * to interact with the Broker in order to generate optimized SQL Statements.
  + *
  + * @author Thomas Mahler
  + * @version $Id$
  + */
  +public interface ObjectModification extends java.io.Serializable
  +{
  +    /**
  +     * returns true if the underlying Object needs an INSERT statement.
  +     * else Returns false.
  +     */
       public boolean needsInsert();
  -
  -    /**
  -     * returns true if the underlying Object needs an UPDATE statement.
  -     * else Returns false.
  -     */
  -    public boolean needsUpdate();
  +    /**
  +     * returns true if the underlying Object needs an UPDATE statement.
  +     * else Returns false.
  +     */
  +    public boolean needsUpdate();
   }
  
  
  
  1.27      +123 -16   db-ojb/src/test/org/apache/ojb/broker/PersistenceBrokerTest.java
  
  Index: PersistenceBrokerTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/PersistenceBrokerTest.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- PersistenceBrokerTest.java	23 Jun 2003 19:35:47 -0000	1.26
  +++ PersistenceBrokerTest.java	12 Jul 2003 16:17:23 -0000	1.27
  @@ -1,6 +1,7 @@
   package org.apache.ojb.broker;
   
   import junit.framework.TestCase;
  +import org.apache.commons.lang.SerializationUtils;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
  @@ -192,7 +193,7 @@
           }
           finally
           {
  -            if(broker != null) broker.close();
  +            if (broker != null) broker.close();
           }
       }
   
  @@ -224,7 +225,7 @@
           ProductGroup pg;
           pg = new ProductGroup();
           pg.setId(groupId);
  -                
  +
           Article a = createArticle(300 * testId);
           a.setProductGroup(pg);
   
  @@ -1157,7 +1158,7 @@
           b = readArticleByIdentity(testId);
           assertEquals("should be equal after update", newname, b.getArticleName());
       }
  -    
  +
       /**
        * test if reference to Proxy is updated
        * @throws Exception
  @@ -1165,29 +1166,135 @@
       public void testUpdateReferencedProxy() throws Exception
       {
           int id = 11;
  -        
  +
           deleteArticle(testId);
  -        Article a = readArticleByIdentity(id); 
  -        InterfaceProductGroup pga = a.getProductGroup();   
  +        Article a = readArticleByIdentity(id);
  +        InterfaceProductGroup pga = a.getProductGroup();
           Article b = createArticle(testId);
           b.setProductGroup(pga);
           storeArticle(b);
  -        
  -        broker.clearCache();       
  -        b = readArticleByIdentity(testId); 
  -        InterfaceProductGroup pgb = b.getProductGroup();         
  +
  +        broker.clearCache();
  +        b = readArticleByIdentity(testId);
  +        InterfaceProductGroup pgb = b.getProductGroup();
           assertEquals("should be equal after update", pga.getId(), pgb.getId());
  -          
  +
           // clear ProductGroup
  -        // this one fails because 
  +        // this one fails because
           // fld.getPersistentField().set(obj, null) has a problem with primitives
  -        /*         
  +        /*
           b.setProductGroup(null);
           storeArticle(b);
  -                 
  -        broker.clearCache();       
  -        b = readArticleByIdentity(testId); 
  +
  +        broker.clearCache();
  +        b = readArticleByIdentity(testId);
           assertEquals("should be null after update", null, b.getProductGroup());
           */
  +    }
  +
  +    public void testChangeFieldsWhileStoringObject()
  +    {
  +        long timestamp = System.currentTimeMillis();
  +
  +        broker.beginTransaction();
  +        Person p = new Person();
  +        p.setFirstname("no_1_" + timestamp);
  +        p.setLastname("no_1_" + timestamp);
  +        broker.store(p);
  +        // change fields
  +        p.setFirstname("no_2_" + timestamp);
  +        p.setLastname("no_2_" + timestamp);
  +        // store changed object again
  +        broker.store(p);
  +        broker.commitTransaction();
  +
  +        Identity id = new Identity(p, broker);
  +        Person result = (Person) broker.getObjectByIdentity(id);
  +        assertNotNull(result);
  +        assertEquals("no_2_" + timestamp, result.getFirstname());
  +        assertEquals("no_2_" + timestamp, result.getLastname());
  +
  +        /*
  +        same with cleared cache
  +        */
  +        timestamp = System.currentTimeMillis() + 1;
  +        broker.beginTransaction();
  +        p = new Person();
  +        p.setFirstname("no_3_" + timestamp);
  +        p.setLastname("no_3_" + timestamp);
  +        broker.store(p);
  +        broker.clearCache();
  +        p.setFirstname("no_4_" + timestamp);
  +        p.setLastname("no_4_" + timestamp);
  +        broker.store(p);
  +        broker.commitTransaction();
  +
  +        broker.clearCache();
  +        id = new Identity(p, broker);
  +        broker.clearCache();
  +        result = (Person) broker.getObjectByIdentity(id);
  +        assertNotNull(result);
  +        assertEquals("no_4_" + timestamp, result.getFirstname());
  +        assertEquals("no_4_" + timestamp, result.getLastname());
  +    }
  +
  +    public void testDoubleStore()
  +    {
  +        long timestamp = System.currentTimeMillis();
  +
  +        Person person = new Person();
  +        person.setFirstname("testDoubleStore_" + timestamp);
  +        person.setLastname("time_" + timestamp);
  +
  +        broker.beginTransaction();
  +        Identity oid = new Identity(person, broker);
  +        Person serializedPerson = (Person) SerializationUtils.clone(person);
  +
  +        broker.store(person);
  +        broker.store(person);
  +        broker.store(serializedPerson);
  +
  +        broker.commitTransaction();
  +
  +        Criteria crit = new Criteria();
  +        crit.addLike("firstName", "testDoubleStore_" + timestamp);
  +        Query query = QueryFactory.newQuery(Person.class, crit);
  +        Collection result = broker.getCollectionByQuery(query);
  +
  +        assertEquals("Expect to find exact 1 object", 1, result.size());
  +    }
  +
  +    public void testDoubleDelete()
  +    {
  +        long timestamp = System.currentTimeMillis();
  +
  +        Person person = new Person();
  +        person.setFirstname("testDoubleDelete_" + timestamp);
  +        person.setLastname("time_" + timestamp);
  +
  +        broker.beginTransaction();
  +        Identity oid = new Identity(person, broker);
  +        Person serializedPerson = (Person) SerializationUtils.clone(person);
  +        broker.store(person);
  +        broker.commitTransaction();
  +
  +        Criteria crit = new Criteria();
  +        crit.addLike("firstName", "testDoubleDelete_" + timestamp);
  +        Query query = QueryFactory.newQuery(Person.class, crit);
  +        Collection result = broker.getCollectionByQuery(query);
  +        assertEquals("Expect to find exact 1 object", 1, result.size());
  +
  +        broker.beginTransaction();
  +        broker.delete(person);
  +        broker.delete(serializedPerson);
  +        broker.delete(person);
  +        broker.commitTransaction();
  +
  +        broker.beginTransaction();
  +        broker.delete(serializedPerson);
  +        broker.commitTransaction();
  +
  +        result = broker.getCollectionByQuery(query);
  +        assertEquals("Expect to find none objects", 0, result.size());
       }
   }
  
  
  

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