org.apache.commons.collections

Class AbstractTestObject


public abstract class AbstractTestObject
extends BulkTest

Abstract test class for java.lang.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(String testName)
JUnit constructor.

Method Summary

protected String
getCanonicalEmptyCollectionName(Object object)
protected String
getCanonicalFullCollectionName(Object object)
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 Object
makeObject()
Implement this method to return the object to test.
protected Object
readExternalFormFromBytes(byte[] b)
Read a Serialized or Externalized Object from bytes.
protected Object
readExternalFormFromDisk(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(Serializable o)
Converts a Serializable or Externalizable object to bytes.
protected void
writeExternalFormToDisk(Serializable o, String path)
Write a Serializable or Externalizable object as a file at the given path.

Field Details

COLLECTIONS_MAJOR_VERSION

public static final int COLLECTIONS_MAJOR_VERSION
Current major release for Collections
Field Value:
3

Constructor Details

AbstractTestObject

public AbstractTestObject(String testName)
JUnit constructor.
Parameters:
testName - the test class name

Method Details

getCanonicalEmptyCollectionName

protected String getCanonicalEmptyCollectionName(Object object)

getCanonicalFullCollectionName

protected String getCanonicalFullCollectionName(Object object)

getCompatibilityVersion

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

isEqualsCheckable

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

isTestSerialization

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

makeObject

public abstract Object makeObject()
Implement this method to return the object to test.
Returns:
the object to test

readExternalFormFromBytes

protected Object readExternalFormFromBytes(byte[] b)
            throws IOException,
                   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

readExternalFormFromDisk

protected Object readExternalFormFromDisk(String path)
            throws IOException,
                   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

skipSerializedCanonicalTests

protected boolean skipSerializedCanonicalTests()

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

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.

testEqualsNull

public void testEqualsNull()

testObjectEqualsSelf

public void testObjectEqualsSelf()

testObjectHashCodeEqualsContract

public void testObjectHashCodeEqualsContract()

testObjectHashCodeEqualsSelfHashCode

public void testObjectHashCodeEqualsSelfHashCode()

testSerializeDeserializeThenCompare

public void testSerializeDeserializeThenCompare()
            throws Exception

testSimpleSerialization

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

writeExternalFormToBytes

protected byte[] writeExternalFormToBytes(Serializable o)
            throws 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

writeExternalFormToDisk

protected void writeExternalFormToDisk(Serializable o,
                                       String path)
            throws 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

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