Skip to content

Commit

Permalink
Use System.identityHashCode() to disambiguate objects with same hash …
Browse files Browse the repository at this point in the history
…code (fixes #45)

This commit also modifies the way the class name is printed: for
anonymous classes, the whole name is printed (since
Class.getSimpleName() returns an empty string).
  • Loading branch information
adutra authored and joel-costigliola committed Aug 15, 2018
1 parent 324ada5 commit bbc89eb
Show file tree
Hide file tree
Showing 4 changed files with 496 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public interface Representation {

/**
* Returns the {@code String} representation of the given object. It may or not the object's own implementation of
* Returns the {@code String} representation of the given object. It may or may not be the object's own implementation of
* {@code toString}.
*
* @param object the object to represent.
Expand All @@ -53,11 +53,11 @@ public interface Representation {
String toStringOf(Object object);

/**
* Returns the {@code String} representation of the given object with its type and hexadecimal hash code so that
* it can be differentied from other objects with the same {@link #toStringOf(Object)} representation.
* Returns the {@code String} representation of the given object with its type and hexadecimal identity hash code so that
* it can be differentiated from other objects with the same {@link #toStringOf(Object)} representation.
*
* @param object the object to represent.
* @return the {@code toString} representation of the given object.
* @return the unambiguous {@code toString} representation of the given object.
*/
String unambiguousToStringOf(Object object);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ protected boolean hasCustomFormatterFor(Object object) {
@Override
public String unambiguousToStringOf(Object obj) {
return obj == null ? null
: String.format("%s (%s@%s)", toStringOf(obj), obj.getClass().getSimpleName(), toHexString(obj.hashCode()));
: String.format("%s (%s@%s)",
toStringOf(obj),
obj.getClass().isAnonymousClass() ? obj.getClass().getName() : obj.getClass().getSimpleName(),
toHexString(System.identityHashCode(obj)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public void should_use_unambiguous_fields_description_when_standard_description_
List<Difference> differences = determineDifferences(withHashSet, withSortedSet, null, null);
// WHEN
// @format:off
String message = shouldBeEqualByComparingFieldByFieldRecursive(withSortedSet,
withHashSet,
String message = shouldBeEqualByComparingFieldByFieldRecursive(withHashSet,
withSortedSet,
differences,
REPRESENTATION)
.create(new TextDescription("Test"), REPRESENTATION);
Expand All @@ -83,8 +83,8 @@ public void should_use_unambiguous_fields_description_when_standard_description_
"Path to difference: <collection>%n" +
"- actual : <[\"bar\", \"foo\"] (LinkedHashSet@%s)>%n" +
"- expected: <[\"bar\", \"foo\"] (TreeSet@%s)>",
toHexString(withSortedSet.collection.hashCode()),
toHexString(withHashSet.collection.hashCode())));
toHexString(System.identityHashCode(withHashSet.collection)),
toHexString(System.identityHashCode(withSortedSet.collection))));
}

@Test
Expand All @@ -98,8 +98,8 @@ public void should_use_unambiguous_fields_description_when_standard_description_
List<Difference> differences = determineDifferences(withLinkedHashMap, withTreeMap, null, null);
// WHEN
// @format:off
String message = shouldBeEqualByComparingFieldByFieldRecursive(withTreeMap,
withLinkedHashMap,
String message = shouldBeEqualByComparingFieldByFieldRecursive(withLinkedHashMap,
withTreeMap,
differences,
REPRESENTATION)
.create(new TextDescription("Test"), REPRESENTATION);
Expand All @@ -115,8 +115,8 @@ public void should_use_unambiguous_fields_description_when_standard_description_
"Path to difference: <map>%n" +
"- actual : <{1L=true, 2L=false} (LinkedHashMap@%s)>%n" +
"- expected: <{1L=true, 2L=false} (TreeMap@%s)>",
toHexString(withTreeMap.map.hashCode()),
toHexString(withLinkedHashMap.map.hashCode())));
toHexString(System.identityHashCode(withLinkedHashMap.map)),
toHexString(System.identityHashCode(withTreeMap.map))));
}

@Test
Expand Down Expand Up @@ -152,6 +152,7 @@ public void should_precise_missing_fields_when_actual_does_not_declare_all_expec
personHash, personDAOHash));
}

@Test
public void should_not_fall_with_npe_if_field_of_one_of_compared_objects_is_null() {
final Name actualName = new Name("Andy");
final Name nullName = new Name(null);
Expand Down
Loading

0 comments on commit bbc89eb

Please sign in to comment.