Package 

Object EqualsContract


  • 
    public class EqualsContract
    
                        

    Tests Reflexive, Symmetric, Transitive, Consistent, and non-nullity properties of the equals() contract. If you think this is confusing, realize that there is no way to implement a one-sided equals() correctly with inheritance - it's a broken concept, but it's still used so often that you have to do your best with it.

    I got the idea of contract-based testing from watching Bill Venners: https://www.youtube.com/watch?v=bCTZQi2dpl8

    • Method Summary

      Modifier and Type Method Description
      final static <T extends Any> Unit permutations(List<T> items, Function2<T, T, Object> f) Apply the given function against all unique pairings of items in the list.
      final static Unit equalsHashCode(Object equiv1, Object equiv2, Object equiv3, Object different, Boolean requireDistinctHashes) Tests Reflexive, Symmetric, Transitive, Consistent, and non-nullity properties of the equals() contract.
      final static Unit equalsSameHashCode(Object equiv1, Object equiv2, Object equiv3, Object different) Tests Reflexive, Symmetric, Transitive, Consistent, and non-nullity properties of the equals() contract.
      final static Unit equalsDistinctHashCode(Object equiv1, Object equiv2, Object equiv3, Object different) Tests Reflexive, Symmetric, Transitive, Consistent, and non-nullity properties of the equals() contract.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • permutations

         final static <T extends Any> Unit permutations(List<T> items, Function2<T, T, Object> f)

        Apply the given function against all unique pairings of items in the list. Does this belong on Function2 instead of List?

      • equalsHashCode

         final static Unit equalsHashCode(Object equiv1, Object equiv2, Object equiv3, Object different, Boolean requireDistinctHashes)

        Tests Reflexive, Symmetric, Transitive, Consistent, and non-nullity properties of the equals() contract. See note in class documentation.

        Parameters:
        equiv1 - First equivalent (but unique) object
        equiv2 - Second equivalent (but unique) object (could be a different class)
        equiv3 - Third equivalent (but unique) object (could be a different class)
        different - Non-equivalent object with a (maybe) different hashCode (should be an otherwise compatible class)
        requireDistinctHashes - if true, require that the fourth object have a different hashCode.
      • equalsSameHashCode

         final static Unit equalsSameHashCode(Object equiv1, Object equiv2, Object equiv3, Object different)

        Tests Reflexive, Symmetric, Transitive, Consistent, and non-nullity properties of the equals() contract. See note in class documentation.

        Parameters:
        equiv1 - First equivalent (but unique) object
        equiv2 - Second equivalent (but unique) object (could be a different class)
        equiv3 - Third equivalent (but unique) object (could be a different class)
        different - Non-equivalent object with the same hashCode as the previous three
      • equalsDistinctHashCode

         final static Unit equalsDistinctHashCode(Object equiv1, Object equiv2, Object equiv3, Object different)

        Tests Reflexive, Symmetric, Transitive, Consistent, and non-nullity properties of the equals() contract. See note in class documentation.

        Parameters:
        equiv1 - First equivalent (but unique) object
        equiv2 - Second equivalent (but unique) object (could be a different class)
        equiv3 - Third equivalent (but unique) object (could be a different class)
        different - Non-equivalent object with a different hashCode (should be an otherwise compatible class)