Sunday, 12. September 2010
PatternTesting 1.0.2 released

Two objects which are equals should return the same hash code (and not the other way around, as some developers think).

...Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
This is corresponding Javadoc about the equals method. For those who forget this PatternTesting offers now an ObjectTester which checks classes with overwitten equals methods:
    ObjectTester.assertEquals(my.little.Dummy.class);
The ObjectTester will create with this call two Dummy objects to check the implementation. If it is not ok an Assertion Error will be thrown.

You can also add a package to check if all classes in the package have a correct equals/hashCode implementation:
    ObjectTester.assertEquals(Dummy.class.getPackage());
    ObjectTester.assertEqualsOfPackage("my.little");
Both calls are identical. You can also exclude classes from this check. This is sometimes necessary for classes where the constructor produces different objects (as is the case for the Date class, for example).

In a similar way you can check classes with the SerializableTester and CloneableTester if they are really serializable or clonable.

... comment