com.gargoylesoftware.base.testing

Class TestUtil

public class TestUtil extends Object

Utility methods dealing with JUnit testing.

Version: $Revision: 1.6 $

Author: Mike Bowler

Constructor Summary
TestUtil()
Method Summary
static booleanappearsEqual(Object a, Object b)
Return true if the two objects appear to be equal.
static voidassertAppearsEqual(String message, Object a, Object b)
Assert that a and b appear equal.
static voidassertAppearsNotEqual(String message, Object a, Object b)
Assert that a and b do not appear equal.
static voidassertNotNull(String fieldName, Object fieldValue)
Verify that the specified value is not null.
static voidcheckEquality(Object original, Object copy)
Assert that the two objects are equal
static ObjectcopyBySerialization(Object object)
Copy an object by serializing it into a buffer and then deserializing it again.
static booleanisEqual(Object a, Object b)
static voidtestClone(Object object, boolean checkEquality)
The the clone() method on an object.
static ObjecttestSerialization(Object object, boolean checkEquality)
Serialize the specified object to a stream and then deserialize it again.
static ObjecttestSerialization(Object object)
Same as testSerialization(object, true).

Constructor Detail

TestUtil

private TestUtil()

Method Detail

appearsEqual

public static boolean appearsEqual(Object a, Object b)
Return true if the two objects appear to be equal. Some objects cannot be compared for equality because they don't implement either the equals method or the Comparable interface. An example would be the Event objects used by AWT and Swing.

This method will attempt to determine if the two objects are equal by calling all the public accessor methods on the objects and performing equals checks on the results.

If an exception is thrown during the invocation of any of the getXX() methods then that method will be ignored for the purpose of considering equality.

Parameters: a The first object to be compared b The second object to be compared

Returns: True if the two objects appear to be the same.

assertAppearsEqual

public static void assertAppearsEqual(String message, Object a, Object b)
Assert that a and b appear equal. See appearsEqual for an explanation of "appears"

Parameters: message The message to display if the assert fails. a The first object to compare b The second object to compare

assertAppearsNotEqual

public static void assertAppearsNotEqual(String message, Object a, Object b)
Assert that a and b do not appear equal. See appearsEqual for an explanation of "appears"

Parameters: message The message to display if the assert fails. a The first object to compare b The second object to compare

assertNotNull

public static final void assertNotNull(String fieldName, Object fieldValue)
Verify that the specified value is not null. If it is then throw an exception

Parameters: fieldName The name of the field to check fieldValue The value of the field to check

Throws: DetailedNullPointerException If fieldValue is null

checkEquality

private static void checkEquality(Object original, Object copy)
Assert that the two objects are equal

Parameters: original The original object copy The object to compare against

copyBySerialization

public static Object copyBySerialization(Object object)
Copy an object by serializing it into a buffer and then deserializing it again.

Parameters: object The original.

Returns: The copy.

Throws: IOException If an error occcurs during serialization.

isEqual

private static boolean isEqual(Object a, Object b)

testClone

public static void testClone(Object object, boolean checkEquality)
The the clone() method on an object.

Parameters: object The original object to clone. checkEquality True if the original and copy are to be compared for equality after the clone().

Throws: IllegalAccessException If we do not have authority to call the clone() method. InvocationTargetException If an exception is thrown during the processing of the clone() method

testSerialization

public static Object testSerialization(Object object, boolean checkEquality)
Serialize the specified object to a stream and then deserialize it again. This tests the following things:
  1. Ensures that the original and reconstructed objects are equal.
  2. The object can be serialized

Parameters: object The object to test checkEquality True if the original and copy should be equal according to the semantics of the equals() method.

Returns: The copy.

Throws: IOException If an error occcurs during serialization.

testSerialization

public static Object testSerialization(Object object)
Same as testSerialization(object, true). Provided as a convenience as the equality check is usually wanted.

Parameters: object The object to test

Returns: The copy.

Throws: IOException If an error occcurs during serialization.