com.gargoylesoftware.base.testing

Class EqualsTester

public class EqualsTester extends Assert

EqualsTester is used to test the equals contract on objects. The contract as specified by java.lang.Object states that if A.equals(B) is true then B.equals(A) is also true. It also specifies that if A.equals(B) is true then A.hashCode() will equals B.hashCode().

It is also common practice to implement equals using an instanceof check which will result in false positives in some cases. Specifically, it will result in false positives when comparing against a subclass with the same values. For an in-depth discussion of the common problems when implementing the equals contract, refer to the book "Practical Java" by Peter Haggar

 // WRONG way of implementing equals
 public boolean equals( final Object object ) {
     if( object instanceof this ) {
        // do check
     }
     return false;
 }
 
The correct way to implement equals is as follows
 public boolean equals( final Object object ) {
     if( object != null && object.getClass() == this.getClass() ) {
        // do check
     }
     return false;
 }
 
EqualsTester ensures that the equals() and hashCode() methods have been implemented correctly.

 final Object a = new Foo(4); // original object
 final Object b = new Foo(4); // another object that has the same values as the original
 final Object c = new Foo(5); // another object with different values
 final Object d = new Foo(4) {}; // a subclass of Foo with the same values as the original
 new EqualsTester(a, b, c, d);
 

Version: $Revision: 1.4 $

Author: Mike Bowler

Constructor Summary
EqualsTester(Object a, Object b, Object c, Object d)
Perform the test.
Method Summary
voidassertAEqualsA(Object a)
voidassertAEqualsB(Object a, Object b)
voidassertAEqualsNull(Object a)
voidassertANotEqualC(Object a, Object c)
voidassertCAllowedToBeNull(Class clazz)
C may not be null if it has a public non-default constructor or any setXX() methods
voidassertClassAndSubclass(Object a, Object d)
voidassertDDifferentClassThanA(Object a, Object d)
voidassertNotNull(Object object, String description)
voidassertNull(Object object, String description)
voidassertSameClassAsA(Object a, Object object, String name)
booleanisClassFinal(Class clazz)

Constructor Detail

EqualsTester

public EqualsTester(Object a, Object b, Object c, Object d)
Perform the test. The act of instantiating one of these will run the test.

Parameters: a The object to be tested b An object that is equal to A c An object of the same class that is not equal to A. If it is not possible to create a different one then pass null. d A subclass of A with the same values. If A is an instance of a final class then this must be null

Method Detail

assertAEqualsA

private void assertAEqualsA(Object a)

assertAEqualsB

private void assertAEqualsB(Object a, Object b)

assertAEqualsNull

private void assertAEqualsNull(Object a)

assertANotEqualC

private void assertANotEqualC(Object a, Object c)

assertCAllowedToBeNull

private void assertCAllowedToBeNull(Class clazz)
C may not be null if it has a public non-default constructor or any setXX() methods

Parameters: clazz

assertClassAndSubclass

private void assertClassAndSubclass(Object a, Object d)

assertDDifferentClassThanA

private void assertDDifferentClassThanA(Object a, Object d)

assertNotNull

private void assertNotNull(Object object, String description)

assertNull

private void assertNull(Object object, String description)

assertSameClassAsA

private void assertSameClassAsA(Object a, Object object, String name)

isClassFinal

private boolean isClassFinal(Class clazz)