jdave.contract
Class EqualsHashCodeContract<T>

java.lang.Object
  extended by jdave.contract.EqualsHashCodeContract<T>
All Implemented Interfaces:
IContract

public abstract class EqualsHashCodeContract<T>
extends java.lang.Object
implements IContract

A contract to ensure that equals method is correctly implemented and is consistent with hashCode method. Example:

 public void isConsistentWithEqualsAndHashCode() {
     object = new SampleDomainObject(1);
     specify(object, satisfies(new EqualsHashCodeContract<SampleDomainObject>() {
         protected SampleDomainObject equal() {
            return new SampleDomainObject(1);
         }
         protected SampleDomainObject nonEqual() {
            return new SampleDomainObject(2);
         }
         protected SampleDomainObject subType() {
            return new SampleDomainObject(1) {};
         }
      }));
  }
 

Author:
Joni Freeman
See Also:
Object.equals(Object), Object.hashCode()

Constructor Summary
EqualsHashCodeContract()
           
 
Method Summary
protected abstract  T equal()
           
 void isSatisfied(java.lang.Object obj)
           
protected abstract  T nonEqual()
           
protected abstract  T subType()
          Generally sub-types should not equal with super types.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EqualsHashCodeContract

public EqualsHashCodeContract()
Method Detail

isSatisfied

public void isSatisfied(java.lang.Object obj)
                 throws ExpectationFailedException
Specified by:
isSatisfied in interface IContract
Throws:
ExpectationFailedException

equal

protected abstract T equal()
Returns:
an Object which is equal to the Object whose contract is enforced

nonEqual

protected abstract T nonEqual()
Returns:
an Object which is not equal to the Object whose contract is enforced

subType

protected abstract T subType()
Generally sub-types should not equal with super types. This check can be skipped by returning null.

Returns:
an Object which is a sub-type of the Object whose contract is enforced or null


Copyright © 2008. All Rights Reserved.