org.apache.commons.collections
Class AbstractTestObject

java.lang.Object
  extended byjunit.framework.Assert
      extended byjunit.framework.TestCase
          extended byorg.apache.commons.collections.BulkTest
              extended byorg.apache.commons.collections.AbstractTestObject
All Implemented Interfaces:
java.lang.Cloneable, junit.framework.Test
Direct Known Subclasses:
AbstractTestBag, AbstractTestCollection, AbstractTestComparator, AbstractTestIterator, AbstractTestMap

public abstract class AbstractTestObject
extends BulkTest

Abstract test class for Object methods and contracts.

To use, simply extend this class, and implement the makeObject() method.

If your Object fails one of these tests by design, you may still use this base set of cases. Simply override the test case (method) your Object fails.


Field Summary
static int COLLECTIONS_MAJOR_VERSION
          Current major release for Collections
 
Constructor Summary
AbstractTestObject(java.lang.String testName)
          JUnit constructor.
 
Method Summary
protected  java.lang.String getCanonicalEmptyCollectionName(java.lang.Object object)
           
protected  java.lang.String getCanonicalFullCollectionName(java.lang.Object object)
           
 java.lang.String getCompatibilityVersion()
          Get the version of Collections that this object tries to maintain serialization compatibility with.
 boolean isEqualsCheckable()
          Returns true to indicate that the collection supports equals() comparisons.
 boolean isTestSerialization()
          Is serialization testing supported.
abstract  java.lang.Object makeObject()
          Implement this method to return the object to test.
protected  java.lang.Object readExternalFormFromBytes(byte[] b)
          Read a Serialized or Externalized Object from bytes.
protected  java.lang.Object readExternalFormFromDisk(java.lang.String path)
          Reads a Serialized or Externalized Object from disk.
protected  boolean skipSerializedCanonicalTests()
           
 boolean supportsEmptyCollections()
          Override this method if a subclass is testing an object that cannot serialize an "empty" Collection.
 boolean supportsFullCollections()
          Override this method if a subclass is testing an object that cannot serialize a "full" Collection.
 void testCanonicalEmptyCollectionExists()
          Tests serialization by comparing against a previously stored version in CVS.
 void testCanonicalFullCollectionExists()
          Tests serialization by comparing against a previously stored version in CVS.
 void testEqualsNull()
           
 void testObjectEqualsSelf()
           
 void testObjectHashCodeEqualsContract()
           
 void testObjectHashCodeEqualsSelfHashCode()
           
 void testSerializeDeserializeThenCompare()
           
 void testSimpleSerialization()
          Sanity check method, makes sure that any Serializable class can be serialized and de-serialized in memory, using the handy makeObject() method
protected  byte[] writeExternalFormToBytes(java.io.Serializable o)
          Converts a Serializable or Externalizable object to bytes.
protected  void writeExternalFormToDisk(java.io.Serializable o, java.lang.String path)
          Write a Serializable or Externalizable object as a file at the given path.
 
Methods inherited from class org.apache.commons.collections.BulkTest
clone, ignoredTests, makeSuite, toString
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, setUp, tearDown
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

COLLECTIONS_MAJOR_VERSION

public static final int COLLECTIONS_MAJOR_VERSION
Current major release for Collections

See Also:
Constant Field Values
Constructor Detail

AbstractTestObject

public AbstractTestObject(java.lang.String testName)
JUnit constructor.

Parameters:
testName - the test class name
Method Detail

makeObject

public abstract java.lang.Object makeObject()
Implement this method to return the object to test.

Returns:
the object to test

supportsEmptyCollections

public boolean supportsEmptyCollections()
Override this method if a subclass is testing an object that cannot serialize an "empty" Collection. (e.g. Comparators have no contents)

Returns:
true

supportsFullCollections

public boolean supportsFullCollections()
Override this method if a subclass is testing an object that cannot serialize a "full" Collection. (e.g. Comparators have no contents)

Returns:
true

isTestSerialization

public boolean isTestSerialization()
Is serialization testing supported. Default is true.


isEqualsCheckable

public boolean isEqualsCheckable()
Returns true to indicate that the collection supports equals() comparisons. This implementation returns true;


testObjectEqualsSelf

public void testObjectEqualsSelf()

testEqualsNull

public void testEqualsNull()

testObjectHashCodeEqualsSelfHashCode

public void testObjectHashCodeEqualsSelfHashCode()

testObjectHashCodeEqualsContract

public void testObjectHashCodeEqualsContract()

testSerializeDeserializeThenCompare

public void testSerializeDeserializeThenCompare()
                                         throws java.lang.Exception
Throws:
java.lang.Exception

testSimpleSerialization

public void testSimpleSerialization()
                             throws java.lang.Exception
Sanity check method, makes sure that any Serializable class can be serialized and de-serialized in memory, using the handy makeObject() method

Throws:
java.io.IOException
java.lang.ClassNotFoundException
java.lang.Exception

testCanonicalEmptyCollectionExists

public void testCanonicalEmptyCollectionExists()
Tests serialization by comparing against a previously stored version in CVS. If the test object is serializable, confirm that a canonical form exists.


testCanonicalFullCollectionExists

public void testCanonicalFullCollectionExists()
Tests serialization by comparing against a previously stored version in CVS. If the test object is serializable, confirm that a canonical form exists.


getCompatibilityVersion

public java.lang.String getCompatibilityVersion()
Get the version of Collections that this object tries to maintain serialization compatibility with. Defaults to 1, the earliest Collections version. (Note: some collections did not even exist in this version). 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.

Returns:
The version, or null if this object shouldn't be tested for compatibility with previous versions.

getCanonicalEmptyCollectionName

protected java.lang.String getCanonicalEmptyCollectionName(java.lang.Object object)

getCanonicalFullCollectionName

protected java.lang.String getCanonicalFullCollectionName(java.lang.Object object)

writeExternalFormToDisk

protected void writeExternalFormToDisk(java.io.Serializable o,
                                       java.lang.String path)
                                throws java.io.IOException
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)

Parameters:
o - Object to serialize
path - path to write the serialized Object
Throws:
java.io.IOException

writeExternalFormToBytes

protected byte[] writeExternalFormToBytes(java.io.Serializable o)
                                   throws java.io.IOException
Converts a Serializable or Externalizable object to bytes. Useful for in-memory tests of serialization

Parameters:
o - Object to convert to bytes
Returns:
serialized form of the Object
Throws:
java.io.IOException

readExternalFormFromDisk

protected java.lang.Object readExternalFormFromDisk(java.lang.String path)
                                             throws java.io.IOException,
                                                    java.lang.ClassNotFoundException
Reads a Serialized or Externalized Object from disk. Useful for creating compatibility tests between different CVS versions of the same class

Parameters:
path - path to the serialized Object
Returns:
the Object at the given path
Throws:
java.io.IOException
java.lang.ClassNotFoundException

readExternalFormFromBytes

protected java.lang.Object readExternalFormFromBytes(byte[] b)
                                              throws java.io.IOException,
                                                     java.lang.ClassNotFoundException
Read a Serialized or Externalized Object from bytes. Useful for verifying serialization in memory.

Parameters:
b - byte array containing a serialized Object
Returns:
Object contained in the bytes
Throws:
java.io.IOException
java.lang.ClassNotFoundException

skipSerializedCanonicalTests

protected boolean skipSerializedCanonicalTests()


Copyright © 2001-2007 Apache Software Foundation. All Rights Reserved.