commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwaldh...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jux/src/java/org/apache/commons/jux ObjectTestCase.java
Date Sat, 08 Feb 2003 23:52:02 GMT
rwaldhoff    2003/02/08 15:52:02

  Modified:    jux/src/java/org/apache/commons/jux ObjectTestCase.java
  Log:
  * additional javadocs
  * extract serialize/deserialize methods
  
  Revision  Changes    Path
  1.3       +101 -23   jakarta-commons-sandbox/jux/src/java/org/apache/commons/jux/ObjectTestCase.java
  
  Index: ObjectTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jux/src/java/org/apache/commons/jux/ObjectTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ObjectTestCase.java	8 Feb 2003 14:49:32 -0000	1.2
  +++ ObjectTestCase.java	8 Feb 2003 23:52:02 -0000	1.3
  @@ -58,10 +58,13 @@
   
   import java.io.ByteArrayInputStream;
   import java.io.ByteArrayOutputStream;
  +import java.io.IOException;
   import java.io.InputStream;
   import java.io.ObjectInputStream;
   import java.io.ObjectOutputStream;
  +import java.io.OptionalDataException;
   import java.io.Serializable;
  +import java.io.StreamCorruptedException;
   
   import junit.framework.TestCase;
   
  @@ -86,16 +89,20 @@
        * to be tested.
        * Repeated invocations of this method should generally 
        * return distinct instances, which may or may not be equal,
  -     * but this doesn't necessarily need to be the case. 
  +     * but the precise relationship betwen the values returned
  +     * by repeated invocations of this method is not specified.
        */
       protected abstract Object makeObject() throws Exception;
       
       /** 
  -     * Return some non-<code>null</code> instance of the object
  -     * to be tested.
  -     * The values returned by multiple invocations of htis
  -     * method MUST be equal.  This is the instance of the
  -     * object compared to the canonical serialized form,
  +     * Return some not-<code>null</code> instance of the object
  +     * to be tested such that value returned by repeated 
  +     * invocations of this method will be <code>equal</code>,
  +     * or <code>null</code> if no such instance can be 
  +     * provided.  
  +     * When {@link Serializable} and {@link #isSerializable}
  +     * returns <code>true</code>, the value by
  +     * this method will be compared to the canonical serialized form,
        * if any.
        * The default implementation invokes {@link #makeObject}. 
        */
  @@ -125,22 +132,81 @@
           return true;
       }        
          
  +    /** 
  +     * Returns <code>true</code> iff 
  +     * it is safe to assume (in 
  +     * {@link #assertObjectsAreEqual} and related
  +     * methods) that for two object instances 
  +     * <i>a</i> and <i>b</i>,
  +     * when <code>a.equals(b)</code> then
  +     * <code>a.toString().equals(b.toString())</code>.
  +     * 
  +     * The default implementation returns <code>true</code>.
  +     */
       protected boolean equalsImpliesToStringEquals() {
           return true;
  -    }        
  -    
  +    }
  +           
  +    /** 
  +     * Returns <code>true</code> iff 
  +     * it is safe to assume (in 
  +     * {@link #assertObjectsAreNotEqual} and related
  +     * methods) that for two object instances 
  +     * <i>a</i> and <i>b</i>,
  +     * when <code>!(a.equals(b))</code> then
  +     * <code>!(a.toString().equals(b.toString()))</code>.
  +     * 
  +     * The default implementation returns <code>true</code>.
  +     */
       protected boolean notEqualImpliesNotToStringEqual() {
           return true;
  -    }        
  -
  +    }
  +           
  +    /** 
  +     * Returns <code>true</code> iff 
  +     * it is safe to assume (in 
  +     * {@link #assertObjectsAreNotEqual} and related
  +     * methods) that for two object instances 
  +     * <i>a</i> and <i>b</i>,
  +     * when <code>!(a.equals(b))</code> then
  +     * <code>a.hashCode() !=  b.hashCode()</code>.
  +     * 
  +     * The default implementation returns <code>true</code>.
  +     */
       protected boolean notEqualImpliesNotHashCodeEqual() {
           return true;
       }        
  -
  +           
  +    /** 
  +     * Returns <code>true</code> iff 
  +     * it is safe to assume (in 
  +     * {@link #testObjectSerializeDeserializeThenCompare} 
  +     * and related methods) that a {@link Serializable}
  +     * instance returned by {@link #makeObject} or 
  +     * {@link #makeCannonicalObject} is indeed 
  +     * {@link Serializable}.
  +     * (Returning <code>false</code> from this method
  +     * may be useful when extending a class that 
  +     * implements {@link Serializable} but in a way 
  +     * that is not actually {@link Serializable}.)
  +     * 
  +     * The default implementation returns <code>true</code>.
  +     */
       protected boolean isSerializable() {
           return true;
       }        
   
  +    /** 
  +     * Returns <code>true</code> iff 
  +     * it is safe to assume (in 
  +     * {@link #testObjectSerializeDeserializeThenCompare} 
  +     * and related methods) that a value 
  +     * returned by {@link #makeObject} will be equal 
  +     * to a serialized then deserialized "clone" of 
  +     * itself.
  +     * 
  +     * The default implementation returns <code>true</code>.
  +     */
       protected boolean serializedCloneIsEqual() {
           return true;
       }        
  @@ -150,9 +216,11 @@
   
       public final void testObjectEquals() throws Exception {
           Object obj = makeObject();
  -        assertObjectsAreEqual("equals must be reflexive",obj,obj);
  -        assertEquals("hashCode must be reflexive",obj.hashCode(),obj.hashCode());
  +        assertNotNull("makeObject() should not return null",obj);
  +        assertEquals("equals must be reflexive",obj,obj);
  +        assertEquals("hashCode must be consistent",obj.hashCode(),obj.hashCode());
           assertTrue("non-null object should not equal null",! obj.equals(null) ); 
  +        assertObjectsAreEqual(obj,obj);
           
           Object obj2 = makeObject();
           if(obj.equals(obj2)) {
  @@ -167,14 +235,8 @@
       public final void testObjectSerializeDeserializeThenCompare() throws Exception {
           Object obj = makeObject();
           if(isSerializable() && obj instanceof Serializable) {
  -            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  -            ObjectOutputStream out = new ObjectOutputStream(buffer);
  -            out.writeObject(obj);
  -            out.close();
  -            
  -            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  -            Object dest = in.readObject();
  -            in.close();
  +            byte[] serializedBytes = serialize(obj);            
  +            Object dest = deserialize(serializedBytes);
               if(serializedCloneIsEqual()) {
                   assertObjectsAreEqual(obj,dest);
               }
  @@ -264,6 +326,22 @@
           }
       }
       
  +    protected byte[] serialize(Object obj) throws IOException {
  +        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  +        ObjectOutputStream out = new ObjectOutputStream(buffer);
  +        out.writeObject(obj);
  +        out.close();            
  +        byte[] serializedBytes = buffer.toByteArray();
  +        return serializedBytes;
  +    }
  +
  +    public Object deserialize(byte[] serializedBytes) throws Exception {
  +        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedBytes));
  +        Object dest = in.readObject();
  +        in.close();
  +        return dest;
  +    }
  +
       // private utils
       // ------------------------------------------------------------------------
       /** Returns the equivalent to Object.toString for the given Object. */
  
  
  

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


Mime
View raw message