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 CollectionTest.java
Date Sun, 17 Apr 2005 13:08:08 GMT
brj         2005/04/17 06:08:08

  Modified:    src/java/org/apache/ojb/broker/core
                        PersistenceBrokerImpl.java
               src/java/org/apache/ojb/broker/util BrokerHelper.java
               src/test/org/apache/ojb/broker CollectionTest.java
  Log:
  - do not delete non persistent object in afterStore()
  - throw PBException when deleting non persistent objct
  see http://issues.apache.org/jira/browse/OJB-23
  
  Revision  Changes    Path
  1.115     +21 -7     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.114
  retrieving revision 1.115
  diff -u -r1.114 -r1.115
  --- PersistenceBrokerImpl.java	14 Apr 2005 19:56:54 -0000	1.114
  +++ PersistenceBrokerImpl.java	17 Apr 2005 13:08:08 -0000	1.115
  @@ -617,13 +617,21 @@
               {
                   return;
               }
  +            ClassDescriptor cld = getClassDescriptor(obj.getClass());
  +            
  +            //BRJ: check for valid pk
  +            if (!serviceBrokerHelper().assertValidPkForDelete(cld, obj))
  +            {
  +                String msg = "Cannot delete object without valid PKs. " + obj;
  +                logger.error(msg);
  +                throw new PersistenceBrokerException(msg);
  +            }
  +
               /**
                * MBAIRD
                * 2. register object in markedForDelete map.
                */
               markedForDelete.add(obj);
  -            ClassDescriptor cld = getClassDescriptor(obj.getClass());
  -            Identity oid = new Identity(obj, this, cld);
   
               // Invoke events on PersistenceBrokerAware instances and listeners
               BEFORE_DELETE_EVENT.setTarget(obj);
  @@ -639,6 +647,7 @@
               getBatchManager().executeDelete(cld, obj); // use obj not oid to delete, BRJ
   
               // 3. Add OID to the set of deleted objects
  +            Identity oid = new Identity(obj, this, cld);
               deletedDuringTransaction.add(oid);
   
               // 4. delete dependend upon objects last to avoid FK violations
  @@ -991,7 +1000,12 @@
               Iterator iter = tc.getDeletedObjects().iterator();
               while (iter.hasNext())
               {
  -                delete(iter.next());
  +                Object obj = iter.next();
  +                ClassDescriptor cld = getClassDescriptor(obj.getClass());
  +                if (serviceBrokerHelper().assertValidPkForDelete(cld, obj))
  +                {    
  +                    delete(obj);
  +                }    
               }
               tc.clearDeletedObjects();
               tc.clearNewObjects();
  @@ -1859,7 +1873,7 @@
        * can recurse this function
        * @param obj
        * @param cld
  -     * @param oid   BRJ: what is it good for ???
  +     * @param oid   
        * @param insert
        */
       private void storeToDb(Object obj, ClassDescriptor cld, Identity oid, boolean insert)
  @@ -1868,12 +1882,12 @@
           storeReferences(obj, cld, insert);
   
           Object[] pkValues = oid.getPrimaryKeyValues();
  -        if (!serviceBrokerHelper().assertValidPkFields(cld.getPkFields(), pkValues))
  +        if (!serviceBrokerHelper().assertValidPksForStore(cld, oid.getPrimaryKeyValues()))
           {
               // BRJ: fk values may be part of pk, but the are not known during
               // creation of Identity. so we have to get them here
               pkValues = serviceBrokerHelper().getKeyValues(cld, obj);
  -            if (!serviceBrokerHelper().assertValidPkFields(cld.getPkFields(), pkValues))
  +            if (!serviceBrokerHelper().assertValidPksForStore(cld, pkValues))
               {
                   String append = insert ? " on insert" : " on update" ;
                   throw new PersistenceBrokerException("assertValidPkFields failed for Object
of type: " + cld.getClassNameOfObject() + append);
  
  
  
  1.82      +31 -6     db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java
  
  Index: BrokerHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java,v
  retrieving revision 1.81
  retrieving revision 1.82
  diff -u -r1.81 -r1.82
  --- BrokerHelper.java	13 Apr 2005 19:19:38 -0000	1.81
  +++ BrokerHelper.java	17 Apr 2005 13:08:08 -0000	1.82
  @@ -382,7 +382,8 @@
       } 
       
       /**
  -     * Get the values of the fields for an obj
  +     * Get the values of the fields for an obj.
  +     * Autoincrement values are automatically set.
        * @param fields
        * @param objOrProxy
        * @throws PersistenceBrokerException
  @@ -460,16 +461,17 @@
       }
   
       /**
  -     * returns true if the primary key fields are valid, else false.
  +     * returns true if the primary key fields are valid for store, else false.
        * PK fields are valid if each of them is either an OJB managed
        * attribute (autoincrement or locking) or if it contains
        * a valid non-null value
  -     * @param fieldDescriptors the array of PK fielddescriptors
  -     * @param pkValues the array of PK values
  +     * @param cld the ClassDescriptor
  +     * @param pkValues the pk-values
        * @return boolean
        */
  -    public boolean assertValidPkFields(FieldDescriptor[] fieldDescriptors, Object[] pkValues)
  +    public boolean assertValidPksForStore(ClassDescriptor cld, Object pkValues[])
       {
  +        FieldDescriptor fieldDescriptors[] = cld.getPkFields();
           int fieldDescriptorSize = fieldDescriptors.length;
           for(int i = 0; i < fieldDescriptorSize; i++)
           {
  @@ -489,6 +491,29 @@
       }
   
       /**
  +     * returns true if the primary key fields are valid for delete, else false.
  +     * PK fields are valid if each of them contains a valid non-null value
  +     * @param cld the ClassDescriptor
  +     * @param obj the object 
  +     * @return boolean
  +     */
  +    public boolean assertValidPkForDelete(ClassDescriptor cld, Object obj)
  +    {
  +        FieldDescriptor fieldDescriptors[] = cld.getPkFields();
  +        int fieldDescriptorSize = fieldDescriptors.length;
  +        for(int i = 0; i < fieldDescriptorSize; i++)
  +        {
  +            FieldDescriptor fd = fieldDescriptors[i];
  +            Object pkValue = fd.getPersistentField().get(obj);
  +            if (!assertValidPkValue(pkValue))
  +            {
  +                return false;
  +            }
  +        }
  +        return true;
  +    }
  +
  +    /**
        * returns true if a value is non-null, STring instances are also checked,
        * if they are non-empty.
        * @param pkValue the value to check
  
  
  
  1.20      +39 -1     db-ojb/src/test/org/apache/ojb/broker/CollectionTest.java
  
  Index: CollectionTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/CollectionTest.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- CollectionTest.java	25 Mar 2005 19:06:28 -0000	1.19
  +++ CollectionTest.java	17 Apr 2005 13:08:08 -0000	1.20
  @@ -856,6 +856,44 @@
       }
   
       /**
  +     * Test RemovalAwareCollection remove() of non persistent obj
  +     */
  +    public void testRemovalAwareCollection2()
  +    {
  +        String prefix = "testRemovalAwareCollection2_" + System.currentTimeMillis();
  +
  +        Identity gathererId;
  +        Gatherer loadedCopy;
  +        Gatherer gatherer = new Gatherer(null, "Gatherer_" + prefix);
  +        List coll = new ArrayList();
  +        coll.add(new CollectibleBase("Base_1_" + prefix));
  +        coll.add(new CollectibleBase("Base_2_" + prefix));
  +        gatherer.setCollectiblesBase(coll);
  +
  +        broker.beginTransaction();
  +        broker.store(gatherer);
  +        broker.commitTransaction();
  +        assertTrue(gatherer.getGatId() != null);
  +        gathererId = broker.serviceIdentity().buildIdentity(gatherer);
  +
  +        broker.clearCache();
  +        loadedCopy = (Gatherer) broker.getObjectByIdentity(gathererId);
  +        assertNotNull(loadedCopy);
  +        assertNotNull(loadedCopy.getCollectiblesBase());
  +        assertTrue(loadedCopy.getCollectiblesBase() instanceof RemovalAwareCollection);
  +        assertEquals(2, loadedCopy.getCollectiblesBase().size());
  +        
  +        // add and remove non persistent obj
  +        loadedCopy.getCollectiblesBase().add(new CollectibleBase("Base_3_" + prefix));
  +        assertEquals(3, loadedCopy.getCollectiblesBase().size());
  +        loadedCopy.getCollectiblesBase().remove(2);
  +
  +        broker.beginTransaction();
  +        broker.store(loadedCopy);
  +        broker.commitTransaction();
  +    }
  +
  +    /**
        * Test RemovalAwareCollection remove() and clear()
        */
       public void testRemovalAwareCollectionProxy()
  
  
  

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