commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From morg...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestObject.java
Date Tue, 26 Feb 2002 00:48:14 GMT
morgand     02/02/25 16:48:14

  Modified:    collections/src/test/org/apache/commons/collections Tag:
                        collections_1_x_branch TestObject.java
  Log:
  merging serialization test methods into the 1.x branch
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +130 -5    jakarta-commons/collections/src/test/org/apache/commons/collections/TestObject.java
  
  Index: TestObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestObject.java,v
  retrieving revision 1.1
  retrieving revision 1.1.2.1
  diff -u -r1.1 -r1.1.2.1
  --- TestObject.java	20 Apr 2001 16:54:03 -0000	1.1
  +++ TestObject.java	26 Feb 2002 00:48:14 -0000	1.1.2.1
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestObject.java,v
1.1 2001/04/20 16:54:03 rwaldhoff Exp $
  - * $Revision: 1.1 $
  - * $Date: 2001/04/20 16:54:03 $
  + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestObject.java,v
1.1.2.1 2002/02/26 00:48:14 morgand Exp $
  + * $Revision: 1.1.2.1 $
  + * $Date: 2002/02/26 00:48:14 $
    *
    * ====================================================================
    *
  @@ -62,6 +62,18 @@
   package org.apache.commons.collections;
   
   import junit.framework.*;
  +
  +import java.io.ByteArrayInputStream;
  +import java.io.ByteArrayOutputStream;
  +import java.io.FileInputStream;
  +import java.io.FileOutputStream;
  +import java.io.InputStream;
  +import java.io.IOException;
  +import java.io.ObjectInputStream;
  +import java.io.ObjectOutputStream;
  +import java.io.OutputStream;
  +import java.io.Serializable;
  +
   import java.util.Collection;
   import java.util.ArrayList;
   import java.util.Iterator;
  @@ -71,20 +83,39 @@
    * Tests base {@link java.util.Object} methods and contracts.
    * <p>
    * To use, simply extend this class, and implement
  - * the {@link #makeObject} method.
  + * the {@link #makeObject()} method.
    * <p>
    * If your {@link Object} fails one of these tests by design,
    * you may still use this base set of cases.  Simply override the
    * test case (method) your {@link Object} fails.
    *
    * @author Rodney Waldhoff
  - * @version $Id: TestObject.java,v 1.1 2001/04/20 16:54:03 rwaldhoff Exp $
  + * @version $Id: TestObject.java,v 1.1.2.1 2002/02/26 00:48:14 morgand Exp $
    */
   public abstract class TestObject extends TestCase {
       public TestObject(String testName) {
           super(testName);
       }
   
  +    // current major release for Collections
  +    public static final int COLLECTIONS_MAJOR_VERSION = 2;
  +
  +    /**
  +     * This constant makes it possible for TestMap (and other subclasses,
  +     * if necessary) to automatically check CVS for a versionX copy of a
  +     * Serialized object, so we can make sure that compatibility is maintained.
  +     * See, for example, TestMap.getCanonicalFullMapName(Map map).
  +     * Subclasses can override this variable, indicating compatibility
  +     * with earlier Collections versions.
  +     * Defaults to 1, the earliest Collections version.  (Note: some
  +     * collections did not even exist in this version).
  +     * 
  +     * @return 1
  +     */
  +    public int getCompatibilityVersion() {
  +        return 1;
  +    }
  +
       /**
        * Return a new, empty {@link Object} to used for testing.
        */
  @@ -108,6 +139,100 @@
           Object obj2 = makeObject();
           if(obj1.equals(obj2)) {
               assertEquals("[2] When two objects are equal, their hashCodes should be also.",obj1.hashCode(),obj2.hashCode());
  +            assertTrue("When obj1.equals(obj2) is true, then obj2.equals(obj1) should also
be true", obj2.equals(obj1));
  +        }
  +    }
  +
  +    private void writeExternalFormToStream(Serializable o, OutputStream stream) 
  +    throws IOException {
  +        ObjectOutputStream oStream = new ObjectOutputStream(stream);
  +        oStream.writeObject(o);
  +    }
  +
  +    /**
  +     * Write a Serializable or Externalizable object as
  +     * a file at the given path.  NOT USEFUL as part
  +     * of a unit test; this is just a utility method
  +     * for creating disk-based objects in CVS that can become
  +     * the basis for compatibility tests using
  +     * readExternalFormFromDisk(String path)
  +     * 
  +     * @param o Object to serialize
  +     * @param path path to write the serialized Object
  +     * @exception IOException
  +     */
  +    protected void writeExternalFormToDisk(Serializable o, String path) 
  +    throws IOException {
  +        FileOutputStream fileStream = new FileOutputStream(path);
  +        writeExternalFormToStream(o,fileStream);
  +    }
  +
  +    /**
  +     * Converts a Serializable or Externalizable object to
  +     * bytes.  Useful for in-memory tests of serialization
  +     * 
  +     * @param o Object to convert to bytes
  +     * @return serialized form of the Object
  +     * @exception IOException
  +     */
  +    protected byte[] writeExternalFormToBytes(Serializable o) 
  +    throws IOException {
  +        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
  +        writeExternalFormToStream(o,byteStream);
  +        return byteStream.toByteArray();
  +    }
  +
  +    private Object readExternalFormFromStream(InputStream stream) 
  +    throws IOException, ClassNotFoundException {
  +        ObjectInputStream oStream = new ObjectInputStream(stream);
  +        return oStream.readObject();
  +    }
  +
  +    /**
  +     * Reads a Serialized or Externalized Object from disk.
  +     * Useful for creating compatibility tests betweeen
  +     * different CVS versions of the same class
  +     * 
  +     * @param path path to the serialized Object
  +     * @return the Object at the given path
  +     * @exception IOException
  +     * @exception ClassNotFoundException
  +     */
  +    protected Object readExternalFormFromDisk(String path) 
  +    throws IOException, ClassNotFoundException {
  +        FileInputStream stream = new FileInputStream(path);
  +        return readExternalFormFromStream(stream);
  +    }
  +
  +    /**
  +     * Read a Serialized or Externalized Object from bytes.
  +     * Useful for verifying serialization in memory.
  +     * 
  +     * @param b byte array containing a serialized Object
  +     * @return Object contained in the bytes
  +     * @exception IOException
  +     * @exception ClassNotFoundException
  +     */
  +    protected Object readExternalFormFromBytes(byte[] b) 
  +    throws IOException, ClassNotFoundException {
  +        ByteArrayInputStream stream = new ByteArrayInputStream(b);
  +        return readExternalFormFromStream(stream);
  +    }
  +
  +    /**
  +     * Sanity check method, makes sure that any Serializable
  +     * class can be serialized and de-serialized in memory, 
  +     * using the handy makeObject() method
  +     * 
  +     * @exception IOException
  +     * @exception ClassNotFoundException
  +     */
  +    public void testSimpleSerialization() 
  +    throws IOException, ClassNotFoundException {
  +        Object o = makeObject();
  +        if (o instanceof Serializable) {
  +            byte[] objekt = writeExternalFormToBytes((Serializable) o);
  +            Object p = readExternalFormFromBytes(objekt);
           }
       }
   }
  
  
  

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


Mime
View raw message