de.smartics.util.test.theories
Class ObjectTheories

Package class diagram package ObjectTheories
java.lang.Object
  extended by de.smartics.util.test.theories.ObjectTheories

public abstract class ObjectTheories
     
     
extends Object

Tests common object theories. This includes

  1. Object.equals(Object)
  2. Object.hashCode()
  3. Object.toString()

Author:
Robert Reiner

Field Summary
static int DEFAULT_COUNT_CONSISTENCY_CHECK
          The default value for the number of iterations for the consistency check.
static boolean DEFAULT_RUN_EQUALS_THEORY_ON_DIFFERENT_TYPES
          The default value to be returned by checkForDifferentTypesInEquals().
static boolean DEFAULT_RUN_HASH_CODE_THEORY_ON_UNEQUAL_INSTANCES
          The default value to be returned by checkForUnequalHashCodes().
 
Constructor Summary
ObjectTheories()
           
 
Method Summary
protected  boolean checkForDifferentTypesInEquals()
          Determines if a theory to check that equals fails on different types should be checked.
protected  boolean checkForUnequalHashCodes()
          Determines if a theory to check for unequal hash code values for unequal instances should be checked.
 void equalsIsConsistent(Object uutX, Object uutY)
          Checks the consistent property of the Object.equals(Object) method.
 void equalsIsReflexive(Object uut)
          Checks the reflexive property of the Object.equals(Object) method.
 void equalsIsSymmetric(Object uutX, Object uutY)
          Checks the symmetric property of the Object.equals(Object) method.
 void equalsIsTransitive(Object uutX, Object uutY, Object uutZ)
          Checks the transitive property of the Object.equals(Object) method.
 void equalsReturnFalseOnInstanceOfOtherType(Object uut)
          Checks the consistent property of the Object.equals(Object) method regarding comparison with elements of other type.
 void equalsReturnFalseOnNull(Object uut)
          Checks the not-null property of the Object.equals(Object) method.
protected  int getConsistencyIterationCount()
          Determines the default number of iterations to go through for the consistency checks.
protected  int getEqualsConsistencyIterationCount()
          Determines the number of iterations to go through for the equals consistency check.
protected  int getHashCodeConsistencyIterationCount()
          Determines the number of iterations to go through for the hash code consistency check.
 void hashCodeIsConsistent(Object uut)
          Checks the consistency property of the Object.hashCode() method.
 void hashCodeIsConsistentWithEquals(Object uutX, Object uutY)
          Checks the consistency-with-equals property of the Object.hashCode() method.
 void hashCodeProducesUnequalHashCodesForUnequalInstances(Object uutX, Object uutY)
          Checks the guideline for optimum performance of the Object.hashCode() method.
 void toStringRunsWithoutFailure(Object uut)
          This theory simply checks that calling the Object.toString() method of the unit under test (UUT) does not fail with raising an exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_COUNT_CONSISTENCY_CHECK

public static final int DEFAULT_COUNT_CONSISTENCY_CHECK
The default value for the number of iterations for the consistency check. May be specified by subclasses by overriding getConsistencyIterationCount().

The value of this constant is 7.

Constant Field Value:
7

DEFAULT_RUN_EQUALS_THEORY_ON_DIFFERENT_TYPES

public static final boolean DEFAULT_RUN_EQUALS_THEORY_ON_DIFFERENT_TYPES
The default value to be returned by checkForDifferentTypesInEquals().

The value of this constant is true.

Constant Field Value:
true

DEFAULT_RUN_HASH_CODE_THEORY_ON_UNEQUAL_INSTANCES

public static final boolean DEFAULT_RUN_HASH_CODE_THEORY_ON_UNEQUAL_INSTANCES
The default value to be returned by checkForUnequalHashCodes().

The value of this constant is false.

Constant Field Value:
false
Constructor Detail

ObjectTheories

public ObjectTheories()
Method Detail

getConsistencyIterationCount

protected int getConsistencyIterationCount()
Determines the default number of iterations to go through for the consistency checks.

Returns:
7 per default.
See Also:
getEqualsConsistencyIterationCount(), getHashCodeConsistencyIterationCount()

getEqualsConsistencyIterationCount

protected int getEqualsConsistencyIterationCount()
Determines the number of iterations to go through for the equals consistency check.

Returns:
result of getConsistencyIterationCount() per default.

getHashCodeConsistencyIterationCount

protected int getHashCodeConsistencyIterationCount()
Determines the number of iterations to go through for the hash code consistency check.

Returns:
result of getConsistencyIterationCount() per default.

checkForDifferentTypesInEquals

protected boolean checkForDifferentTypesInEquals()
Determines if a theory to check that equals fails on different types should be checked.

Returns:
true if the theory should be executed, false otherwise. Per default returns true

checkForUnequalHashCodes

protected boolean checkForUnequalHashCodes()
Determines if a theory to check for unequal hash code values for unequal instances should be checked.

Returns:
true if the theory should be executed, false otherwise. Per default returns false

equalsIsReflexive

public final void equalsIsReflexive(Object uut)
Checks the reflexive property of the Object.equals(Object) method.

It is reflexive: for any non-null reference value x, x.equals(x) should return true.

Parameters:
uut - the unit under test.
See Also:
Object.equals(Object)

equalsIsSymmetric

public final void equalsIsSymmetric(Object uutX,
                                    Object uutY)
Checks the symmetric property of the Object.equals(Object) method.

It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.

Parameters:
uutX - the unit under test to test for symmetry.
uutY - the unit under test to test for symmetry.
See Also:
Object.equals(Object)

equalsIsTransitive

public final void equalsIsTransitive(Object uutX,
                                     Object uutY,
                                     Object uutZ)
Checks the transitive property of the Object.equals(Object) method.

It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.

Parameters:
uutX - the unit under test to test for transitivity.
uutY - the unit under test to test for transitivity.
uutZ - the unit under test to test for transitivity.
See Also:
Object.equals(Object)

equalsIsConsistent

public final void equalsIsConsistent(Object uutX,
                                     Object uutY)
Checks the consistent property of the Object.equals(Object) method.

It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.

Parameters:
uutX - the unit under test to test for consistency.
uutY - the unit under test to test for consistency.
See Also:
Object.equals(Object)

equalsReturnFalseOnNull

public final void equalsReturnFalseOnNull(Object uut)
Checks the not-null property of the Object.equals(Object) method.

For any non-null reference value x, x.equals(null) should return false.

Parameters:
uut - the unit under test.
See Also:
Object.equals(Object)

equalsReturnFalseOnInstanceOfOtherType

public final void equalsReturnFalseOnInstanceOfOtherType(Object uut)
Checks the consistent property of the Object.equals(Object) method regarding comparison with elements of other type.

Parameters:
uut - the unit under test.
See Also:
Object.equals(Object)

hashCodeIsConsistent

public final void hashCodeIsConsistent(Object uut)
Checks the consistency property of the Object.hashCode() method.

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.

Parameters:
uut - the unit under test.
See Also:
Object.hashCode()

hashCodeIsConsistentWithEquals

public final void hashCodeIsConsistentWithEquals(Object uutX,
                                                 Object uutY)
Checks the consistency-with-equals property of the Object.hashCode() method.

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

Parameters:
uutX - the unit under test to test for consistency-with-equals.
uutY - the unit under test to test for consistency-with-equals.
See Also:
Object.hashCode()

hashCodeProducesUnequalHashCodesForUnequalInstances

public final void hashCodeProducesUnequalHashCodesForUnequalInstances(Object uutX,
                                                                      Object uutY)
Checks the guideline for optimum performance of the Object.hashCode() method.

It is not required that if two objects are unequal according to the Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

Parameters:
uutX - the unit under test.
uutY - the unit under test.
See Also:
Object.hashCode()

toStringRunsWithoutFailure

public final void toStringRunsWithoutFailure(Object uut)
This theory simply checks that calling the Object.toString() method of the unit under test (UUT) does not fail with raising an exception.

The theory is only executed on UUTs that are not null.

Parameters:
uut - the unit under test.


Copyright © 2008-2013 Kronseder & Reiner GmbH - smartics. All Rights Reserved.