diff --git a/android/guava-testlib/src/com/google/common/testing/EqualsTester.java b/android/guava-testlib/src/com/google/common/testing/EqualsTester.java index dc2d1972c95e..2c8e08b87c57 100644 --- a/android/guava-testlib/src/com/google/common/testing/EqualsTester.java +++ b/android/guava-testlib/src/com/google/common/testing/EqualsTester.java @@ -127,6 +127,11 @@ private void testItems() { "the Object#hashCode of " + item + " must be consistent", item.hashCode(), item.hashCode()); + if (!(item instanceof String)) { + assertTrue( + item + " must not be Object#equals to its Object#toString representation", + !item.equals(item.toString())); + } } } diff --git a/android/guava-testlib/test/com/google/common/testing/EqualsTesterTest.java b/android/guava-testlib/test/com/google/common/testing/EqualsTesterTest.java index b992f1b4bf1f..d615af6633a2 100644 --- a/android/guava-testlib/test/com/google/common/testing/EqualsTesterTest.java +++ b/android/guava-testlib/test/com/google/common/testing/EqualsTesterTest.java @@ -272,6 +272,15 @@ public void testEqualityGroups() { .testEquals(); } + public void testEqualityBasedOnToString() { + try { + new EqualsTester().addEqualityGroup(new EqualsBasedOnToString("foo")).testEquals(); + fail(); + } catch (AssertionFailedError e) { + assertTrue(e.getMessage().contains("toString representation")); + } + } + private static void assertErrorMessage(Throwable e, String message) { // TODO(kevinb): use a Truth assertion here if (!e.getMessage().contains(message)) { @@ -422,4 +431,27 @@ public String toString() { return name; } } + + private static final class EqualsBasedOnToString { + private final String s; + + private EqualsBasedOnToString(String s) { + this.s = s; + } + + @Override + public boolean equals(Object obj) { + return obj != null && obj.toString().equals(toString()); + } + + @Override + public int hashCode() { + return s.hashCode(); + } + + @Override + public String toString() { + return s; + } + } } diff --git a/guava-gwt/test/com/google/common/testing/EqualsTesterTest_gwt.java b/guava-gwt/test/com/google/common/testing/EqualsTesterTest_gwt.java index 1c1fade62f88..acf3aa534111 100644 --- a/guava-gwt/test/com/google/common/testing/EqualsTesterTest_gwt.java +++ b/guava-gwt/test/com/google/common/testing/EqualsTesterTest_gwt.java @@ -42,6 +42,12 @@ public void testAddTwoEqualObjectsAtOnceWithNull() throws Exception { testCase.testAddTwoEqualObjectsAtOnceWithNull(); } +public void testEqualityBasedOnToString() throws Exception { + com.google.common.testing.EqualsTesterTest testCase = new com.google.common.testing.EqualsTesterTest(); + testCase.setUp(); + testCase.testEqualityBasedOnToString(); +} + public void testEqualityGroups() throws Exception { com.google.common.testing.EqualsTesterTest testCase = new com.google.common.testing.EqualsTesterTest(); testCase.setUp(); diff --git a/guava-testlib/src/com/google/common/testing/EqualsTester.java b/guava-testlib/src/com/google/common/testing/EqualsTester.java index dc2d1972c95e..2c8e08b87c57 100644 --- a/guava-testlib/src/com/google/common/testing/EqualsTester.java +++ b/guava-testlib/src/com/google/common/testing/EqualsTester.java @@ -127,6 +127,11 @@ private void testItems() { "the Object#hashCode of " + item + " must be consistent", item.hashCode(), item.hashCode()); + if (!(item instanceof String)) { + assertTrue( + item + " must not be Object#equals to its Object#toString representation", + !item.equals(item.toString())); + } } } diff --git a/guava-testlib/test/com/google/common/testing/EqualsTesterTest.java b/guava-testlib/test/com/google/common/testing/EqualsTesterTest.java index b992f1b4bf1f..d615af6633a2 100644 --- a/guava-testlib/test/com/google/common/testing/EqualsTesterTest.java +++ b/guava-testlib/test/com/google/common/testing/EqualsTesterTest.java @@ -272,6 +272,15 @@ public void testEqualityGroups() { .testEquals(); } + public void testEqualityBasedOnToString() { + try { + new EqualsTester().addEqualityGroup(new EqualsBasedOnToString("foo")).testEquals(); + fail(); + } catch (AssertionFailedError e) { + assertTrue(e.getMessage().contains("toString representation")); + } + } + private static void assertErrorMessage(Throwable e, String message) { // TODO(kevinb): use a Truth assertion here if (!e.getMessage().contains(message)) { @@ -422,4 +431,27 @@ public String toString() { return name; } } + + private static final class EqualsBasedOnToString { + private final String s; + + private EqualsBasedOnToString(String s) { + this.s = s; + } + + @Override + public boolean equals(Object obj) { + return obj != null && obj.toString().equals(toString()); + } + + @Override + public int hashCode() { + return s.hashCode(); + } + + @Override + public String toString() { + return s; + } + } }