org.apache.commons.collections
abstract public class: AbstractTestObject [javadoc |
source]
java.lang.Object
junit.framework.TestCase
org.apache.commons.collections.BulkTest
org.apache.commons.collections.AbstractTestObject
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
TestIntIterator, TestSynchronizedSet, TestSynchronizedList, TestMapValues, AbstractTestList, TestPredicatedSortedSet, TestTransformedSortedMap, TestLazyMap, AbstractTestSortedBag, TestObjectArrayListIterator2, TestIteratorCharIterator, TestSortedSetSubSet, TestBoundedFifoBuffer2, TestByteCollectionCollection, TestUnmodifiableBidiMap, TestTransformedBag, TestListIteratorFloatListIterator, AbstractTestIterator, TestUnmodifiableIterator, TestCollectionShortCollection, TestStaticBucketMap, TestDoubleIteratorIterator, TestIteratorFloatIterator, TestSingletonIterator2, TestListView, TestCaseInsensitiveMap, TestListOrderedMap2, TestHashBag, TestUnmodifiableSet, TestUnmodifiableBuffer, TestShortCollectionCollection, TestCompositeSet, TestSynchronizedBuffer, TestListOrderedSet, TestHeadMap, TestCharCollectionCollection, AbstractTestBag, TestIteratorByteIterator, TestBlockingBuffer, TestSubMap, TestMapBackedSet, TestLinkedList, TestBooleanComparator, TestShortListIteratorListIterator, TestUnmodifiableSortedSet, AbstractTestOrderedMapIterator, TestShortIteratorIterator, TestPredicatedSortedBag, TestBoundedFifoBuffer, TestTransformedSet, TestTypedSortedSet, TestSynchronizedSortedSet, TestHashedMap, TestUnmodifiableMap, TestFloatListIterator, TestUnmodifiableMapIterator, InnerTestMapIterator, TestCollectionByteCollection, TestListIteratorShortListIterator, AbstractTestOrderedMap, TestTransformedList, TestUnmodifiableOrderedMapIterator, TestByteIteratorIterator, BulkTestSubList, TestIntListIteratorListIterator, AbstractTestCollection, TestTransformedSortedBag, TestListIteratorDoubleListIterator, TestCharListIterator, TestTransformedMap, TestPredicatedSortedMap, TestCharListIteratorListIterator, AbstractTestMap, TestFloatListIteratorListIterator, AbstractTestMapIterator, TestSingletonMap, TestIdentityMap, TestListView, TestCompositeMap, TestAbstractLinkedList, TestReferenceMap, AbstractTestIterableMap, TestSynchronizedCollection, TestLongIterator, TestTypedSet, TestFixedSizeList, TestPredicatedList, TestTreeBidiMap, TestPriorityBuffer, TestIntListIterator, TestLRUMap, TestMapEntrySet, TestBidiOrderedMapIterator, TestIntIteratorIterator, TestCollectionIntCollection, TestTypedBag, TestFlatMapIterator, TestShortListIterator, TestPredicatedBuffer, TestUnmodifiableSortedBidiMap, TestDoubleListIterator, TestUnmodifiableCollection, TestListOrderedMap, TestCursorableLinkedList, TestCircularFifoBuffer, TestFixedSizeMap, TestInverseBidiMap, AbstractTestSortedSet, AbstractTestSortedBidiMap, TestListOrderedSet2, TestCollectionCharCollection, TestDoubleCollectionCollection, AbstractTestListIterator, TestIteratorIntIterator, TestDualTreeBidiMap, TestFlat3Map, TestObjectArrayIterator, TestCharIteratorIterator, TestUnboundedFifoBuffer, TestShortIterator, TestViewMap, TestListIteratorCharListIterator, TestTreeBag, TestPredicatedMap, TestDualHashBidiMap, TestObjectArrayListIterator, TestDoubleIterator, TestFloatCollectionCollection, TestSetUniqueList, TestAbstractOrderedBidiMapDecorator, TestIntCollectionCollection, TestBidiMapIterator, AbstractTestSortedMap, TestNodeCachingLinkedList, AbstractTestSet, TestUnmodifiableOrderedMap, TestLongIteratorIterator, TestCollectionLongCollection, TestCharIterator, TestMultiKeyMap, TestPredicatedBag, TestUnmodifiableList, TestReaderCharIterator, TestLongCollectionCollection, TestLinkedMap, TestCollectionFloatCollection, TestFixedSizeSortedMap, TestUnmodifiableSortedMap, TestByteIterator, TestInputStreamByteIterator, TestTypedSortedBag, TestListIteratorIntListIterator, TestListIteratorByteListIterator, TestFloatIteratorIterator, TestPredicatedSet, TestLongListIterator, TestFloatIterator, TestListIterator, TestTypedList, TestUnmodifiableListIterator, TestListIteratorLongListIterator, TestDualTreeBidiMap2, TestIteratorShortIterator, TestTransformedSortedSet, TestIteratorLongIterator, TestMapBackedSet2, TestListView, TestReferenceIdentityMap, TestMapKeySet, InnerTestOrderedMapIterator, TestIteratorDoubleIterator, TestLongListIteratorListIterator, TestTransformedCollection, TestTailMap, TestLazySortedMap, TestCollectionDoubleCollection, TestDoubleListIteratorListIterator, AbstractTestOrderedBidiMap, AbstractTestBidiMap, AbstractTestComparator, TestByteListIteratorListIterator, TestObjectGraphIterator, TestPredicatedCollection, TestByteListIterator, TestCompositeCollection, TestUnmodifiableOrderedBidiMap, TestTreeList
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.
- version:
$ - Revision: 1.7 $ $Date: 2004/06/01 22:55:14 $
- author:
Rodney - Waldhoff
- author:
Stephen - Colebourne
- author:
Anonymous -
| Field Summary |
|---|
| public static final int | COLLECTIONS_MAJOR_VERSION | Current major release for Collections |
| Method from org.apache.commons.collections.AbstractTestObject Summary: |
|---|
|
getCanonicalEmptyCollectionName, getCanonicalFullCollectionName, getCompatibilityVersion, isEqualsCheckable, isTestSerialization, makeObject, readExternalFormFromBytes, readExternalFormFromDisk, skipSerializedCanonicalTests, supportsEmptyCollections, supportsFullCollections, testCanonicalEmptyCollectionExists, testCanonicalFullCollectionExists, testEqualsNull, testObjectEqualsSelf, testObjectHashCodeEqualsContract, testObjectHashCodeEqualsSelfHashCode, testSerializeDeserializeThenCompare, testSimpleSerialization, writeExternalFormToBytes, writeExternalFormToDisk |
| Method from org.apache.commons.collections.AbstractTestObject Detail: |
protected String getCanonicalEmptyCollectionName(Object object) {
StringBuffer retval = new StringBuffer();
retval.append("data/test/");
String colName = object.getClass().getName();
colName = colName.substring(colName.lastIndexOf(".") + 1, colName.length());
retval.append(colName);
retval.append(".emptyCollection.version");
retval.append(getCompatibilityVersion());
retval.append(".obj");
return retval.toString();
}
|
protected String getCanonicalFullCollectionName(Object object) {
StringBuffer retval = new StringBuffer();
retval.append("data/test/");
String colName = object.getClass().getName();
colName = colName.substring(colName.lastIndexOf(".") + 1, colName.length());
retval.append(colName);
retval.append(".fullCollection.version");
retval.append(getCompatibilityVersion());
retval.append(".obj");
return retval.toString();
}
|
public String getCompatibilityVersion() {
return "1";
}
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. |
public boolean isEqualsCheckable() {
return true;
}
Returns true to indicate that the collection supports equals() comparisons.
This implementation returns true; |
public boolean isTestSerialization() {
return true;
}
Is serialization testing supported.
Default is true. |
abstract public Object makeObject()
Implement this method to return the object to test. |
protected Object readExternalFormFromBytes(byte[] b) throws ClassNotFoundException, IOException {
ByteArrayInputStream stream = new ByteArrayInputStream(b);
return readExternalFormFromStream(stream);
}
Read a Serialized or Externalized Object from bytes.
Useful for verifying serialization in memory. |
protected Object readExternalFormFromDisk(String path) throws ClassNotFoundException, IOException {
FileInputStream stream = new FileInputStream(path);
return readExternalFormFromStream(stream);
}
Reads a Serialized or Externalized Object from disk.
Useful for creating compatibility tests between
different CVS versions of the same class |
protected boolean skipSerializedCanonicalTests() {
return Boolean.getBoolean("org.apache.commons.collections:with-clover");
}
|
public boolean supportsEmptyCollections() {
return true;
}
Override this method if a subclass is testing an object
that cannot serialize an "empty" Collection.
(e.g. Comparators have no contents) |
public boolean supportsFullCollections() {
return true;
}
Override this method if a subclass is testing an object
that cannot serialize a "full" Collection.
(e.g. Comparators have no contents) |
public void testCanonicalEmptyCollectionExists() {
if (supportsEmptyCollections() && isTestSerialization() && !skipSerializedCanonicalTests()) {
Object object = makeObject();
if (object instanceof Serializable) {
String name = getCanonicalEmptyCollectionName(object);
assertTrue(
"Canonical empty collection (" + name + ") is not in CVS",
new File(name).exists());
}
}
}
Tests serialization by comparing against a previously stored version in CVS.
If the test object is serializable, confirm that a canonical form exists. |
public void testCanonicalFullCollectionExists() {
if (supportsFullCollections() && isTestSerialization() && !skipSerializedCanonicalTests()) {
Object object = makeObject();
if (object instanceof Serializable) {
String name = getCanonicalFullCollectionName(object);
assertTrue(
"Canonical full collection (" + name + ") is not in CVS",
new File(name).exists());
}
}
}
Tests serialization by comparing against a previously stored version in CVS.
If the test object is serializable, confirm that a canonical form exists. |
public void testEqualsNull() {
Object obj = makeObject();
assertEquals(false, obj.equals(null)); // make sure this doesn't throw NPE either
}
|
public void testObjectEqualsSelf() {
Object obj = makeObject();
assertEquals("A Object should equal itself", obj, obj);
}
|
public void testObjectHashCodeEqualsContract() {
Object obj1 = makeObject();
if (obj1.equals(obj1)) {
assertEquals(
"[1] When two objects are equal, their hashCodes should be also.",
obj1.hashCode(), obj1.hashCode());
}
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));
}
}
|
public void testObjectHashCodeEqualsSelfHashCode() {
Object obj = makeObject();
assertEquals("hashCode should be repeatable", obj.hashCode(), obj.hashCode());
}
|
public void testSerializeDeserializeThenCompare() throws Exception {
Object obj = makeObject();
if (obj instanceof Serializable && isTestSerialization()) {
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();
if (isEqualsCheckable()) {
assertEquals("obj != deserialize(serialize(obj))", obj, dest);
}
}
}
|
public void testSimpleSerialization() throws Exception {
Object o = makeObject();
if (o instanceof Serializable && isTestSerialization()) {
byte[] objekt = writeExternalFormToBytes((Serializable) o);
Object p = readExternalFormFromBytes(objekt);
}
}
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) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
writeExternalFormToStream(o, byteStream);
return byteStream.toByteArray();
}
Converts a Serializable or Externalizable object to
bytes. Useful for in-memory tests of serialization |
protected void writeExternalFormToDisk(Serializable o,
String path) throws IOException {
FileOutputStream fileStream = new FileOutputStream(path);
writeExternalFormToStream(o, fileStream);
}
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) |