You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2004/01/11 02:30:46 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/util BrokerHelper.java

arminw      2004/01/10 17:30:46

  Modified:    src/test/org/apache/ojb/broker PersistenceBrokerTest.java
                        ObjectRepository.java AnonymousFieldsTest.java
               src/java/org/apache/ojb/broker/util BrokerHelper.java
  Log:
  - fix bug in autoincrement fields '0' value handling
  - add tests for the fix
  - modify classes for support new tests
  
  Revision  Changes    Path
  1.37      +103 -2    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.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- PersistenceBrokerTest.java	4 Jan 2004 18:14:55 -0000	1.36
  +++ PersistenceBrokerTest.java	11 Jan 2004 01:30:46 -0000	1.37
  @@ -6,6 +6,7 @@
   import java.util.Iterator;
   import java.util.Set;
   import java.util.Vector;
  +import java.sql.Statement;
   
   import junit.framework.TestCase;
   import org.apache.commons.lang.SerializationUtils;
  @@ -60,8 +61,6 @@
       {
           try
           {
  -//            Article a = createArticle(testId);
  -//            deleteArticle(a);
               broker.clearCache();
           }
           catch (Throwable t)
  @@ -70,6 +69,108 @@
           finally
           {
               if (broker != null) broker.close();
  +        }
  +    }
  +
  +    /**
  +     * PK fields with primitive data types interpret '0' value as
  +     * 'null' by default. But if we don't use primitive data types and read
  +     * an object with 0 as PK value and store such an object without
  +     * changes, nothing should happen.
  +     */
  +    public void testNull_0_Complex() throws Exception
  +    {
  +        Class objClass = ObjectRepository.E.class;
  +        ClassDescriptor cld = broker.getClassDescriptor(objClass);
  +        Integer someOtherValue = new Integer(1111111111);
  +        String insert = "INSERT INTO TABLE_E VALUES(0,"+someOtherValue.intValue()+")";
  +        String delete = "DELETE FROM TABLE_E WHERE ID=0";
  +
  +        try
  +        {
  +            // prepare test
  +            Statement stmt = broker.serviceStatementManager().getGenericStatement(cld, false);
  +            // insert object with 0 as PK
  +            stmt.executeUpdate(insert);
  +            broker.serviceStatementManager().closeResources(stmt, null);
  +
  +            // find all objects with 'someSubValue' 111111111
  +            Criteria crit = new Criteria();
  +            crit.addEqualTo( "someSuperValue", someOtherValue);
  +            Query queryAllSubs = new QueryByCriteria(objClass, crit );
  +            Collection resultBefore = broker.getCollectionByQuery( queryAllSubs );
  +            int matchesBefore = resultBefore.size();
  +
  +            // materialize object with 0 PK
  +            Criteria c = new Criteria();
  +            c.addEqualTo( "id", new Integer(0));
  +            Query q = new QueryByCriteria(objClass, c );
  +            ObjectRepository.E obj = (ObjectRepository.E) broker.getObjectByQuery( q );
  +            // store the unchanged read object
  +            broker.beginTransaction();
  +            broker.store( obj );
  +            broker.commitTransaction();
  +
  +            Collection resultAfter = broker.getCollectionByQuery( queryAllSubs );
  +            int matchesAfter = resultAfter.size();
  +
  +            assertEquals("We don't store new objects, thus we expect same numbers", matchesBefore, matchesAfter);
  +        }
  +        finally
  +        {
  +            // cleanup
  +            Statement stmt = broker.serviceStatementManager().getGenericStatement(cld, false);
  +            stmt.executeUpdate(delete);
  +            broker.serviceStatementManager().closeResources(stmt, null);
  +        }
  +    }
  +
  +    /**
  +     * Object with autoincrement 'true' and a NON primitive
  +     * data type for the PK field. It should be allowed to set an
  +     * new object with PK 0, because PK field is not primitive
  +     */
  +    public void testNull_0_Complex_2() throws Exception
  +    {
  +        Class objClass = ObjectRepository.E.class;
  +        ClassDescriptor cld = broker.getClassDescriptor(objClass);
  +        Integer someOtherValue = new Integer(1111111111);
  +        String insert = "INSERT INTO TABLE_E VALUES(0,"+someOtherValue.intValue()+")";
  +        String delete = "DELETE FROM TABLE_E WHERE ID=0";
  +
  +        try
  +        {
  +            // find all objects with 'someSubValue' 111111111
  +            Criteria crit = new Criteria();
  +            crit.addEqualTo( "someSuperValue", someOtherValue);
  +            Query queryAllSubs = new QueryByCriteria(objClass, crit );
  +            Collection resultBefore = broker.getCollectionByQuery( queryAllSubs );
  +            int matchesBefore = resultBefore.size();
  +
  +            ObjectRepository.E obj = new ObjectRepository.E();
  +            obj.setId(new Integer(0));
  +            obj.setSomeSuperValue(someOtherValue.intValue());
  +            broker.beginTransaction();
  +            broker.store( obj );
  +            broker.commitTransaction();
  +
  +            broker.clearCache();
  +            Collection resultAfter = broker.getCollectionByQuery( queryAllSubs );
  +            int matchesAfter = resultAfter.size();
  +            assertEquals("We store new object, but was not written to DB", matchesBefore + 1, matchesAfter);
  +            // lookup object with 0 PK
  +            Criteria c = new Criteria();
  +            c.addEqualTo( "id", new Integer(0));
  +            Query q = new QueryByCriteria(objClass, c );
  +            obj = (ObjectRepository.E) broker.getObjectByQuery( q );
  +            assertEquals("We should found object with id 0 for PK field", new Integer(0), obj.getId());
  +        }
  +        finally
  +        {
  +            // cleanup
  +            Statement stmt = broker.serviceStatementManager().getGenericStatement(cld, false);
  +            stmt.executeUpdate(delete);
  +            broker.serviceStatementManager().closeResources(stmt, null);
           }
       }
   
  
  
  
  1.5       +4 -4      db-ojb/src/test/org/apache/ojb/broker/ObjectRepository.java
  
  Index: ObjectRepository.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/ObjectRepository.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ObjectRepository.java	9 Dec 2003 13:46:46 -0000	1.4
  +++ ObjectRepository.java	11 Jan 2004 01:30:46 -0000	1.5
  @@ -317,15 +317,15 @@
   
       public static class E implements Serializable
       {
  -        int id;
  +        Integer id;
           protected int someSuperValue;
   
  -        public int getId()
  +        public Integer getId()
           {
               return id;
           }
   
  -        public void setId(int id)
  +        public void setId(Integer id)
           {
               this.id = id;
           }
  
  
  
  1.12      +2 -2      db-ojb/src/test/org/apache/ojb/broker/AnonymousFieldsTest.java
  
  Index: AnonymousFieldsTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/AnonymousFieldsTest.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- AnonymousFieldsTest.java	5 Jan 2004 00:17:52 -0000	1.11
  +++ AnonymousFieldsTest.java	11 Jan 2004 01:30:46 -0000	1.12
  @@ -486,9 +486,9 @@
           broker.beginTransaction();
           broker.store(entry);
           broker.commitTransaction();
  -        int id = entry.getId();
  +        Integer id = entry.getId();
           broker.clearCache();
  -        entry = (F1) findById(ObjectRepository.F1.class, id);
  +        entry = (F1) findById(ObjectRepository.F1.class, id.intValue());
           assertEquals(id, entry.getId());
           assertEquals(1, entry.getSomeSuperValue());
           assertEquals(2, entry.getSomeValue());
  
  
  
  1.32      +25 -13    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.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- BrokerHelper.java	9 Dec 2003 17:19:58 -0000	1.31
  +++ BrokerHelper.java	11 Jan 2004 01:30:46 -0000	1.32
  @@ -161,7 +161,7 @@
   	 * Answer the real ClassDescriptor for anObj
   	 * ie. aCld may be an Interface of anObj, so the cld for anObj is returned
   	 */
  -	protected ClassDescriptor getRealClassDescriptor(ClassDescriptor aCld, Object anObj)
  +	private ClassDescriptor getRealClassDescriptor(ClassDescriptor aCld, Object anObj)
   	{
   		ClassDescriptor result;
   
  @@ -275,19 +275,31 @@
   	}
   
       /**
  -     * Return true if aValue is regarded as null<br>
  -     * null, Number(0) or empty String
  -     * @param aValue
  -     * @return
  +     * Decide if the given object value represents 'null'.<br/>
  +     *
  +     * - If given value is 'null' itself, true will be returned<br/>
  +     *
  +     * - If given value is instance of Number with value 0 and the field-descriptor
  +     * is a primary key and represents a primitive field, true will be returned<br/>
  +     *
  +     * - If given value is instance of String with length 0 and the field-descriptor
  +     * is a primary key, true will be returned<br/>
        */
  -    private boolean isNull(Object aValue)
  +    public static boolean representsNull(FieldDescriptor fld, Object aValue)
       {
  -        return (
  -            (aValue == null)
  -                || ((aValue instanceof Number) && (((Number) aValue).longValue() == 0))
  -                || ((aValue instanceof String) && (((String) aValue).length() == 0)));
  -    }
  +        if(aValue == null) return true;
   
  +        boolean result = false;
  +        if(((aValue instanceof Number) && (((Number) aValue).longValue() == 0)))
  +        {
  +            result = fld.getPersistentField().getType().isPrimitive() && fld.isPrimaryKey();
  +        }
  +        else if((aValue instanceof String) && (((String) aValue).length() == 0))
  +        {
  +            result = fld.isPrimaryKey();
  +        }
  +        return result;
  +    }
   
   	/**
   	 * Get an autoincremented value that has already
  @@ -302,7 +314,7 @@
   	 */
   	protected Object getAutoIncrementValue(FieldDescriptor fd, Object obj, Object cv)
       {
  -        if (isNull(cv))
  +        if (representsNull(fd, cv))
           {
               PersistentField f = fd.getPersistentField();
               try
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org