diff --git a/src/test/java/org/apache/commons/lang3/tuple/PairTest.java b/src/test/java/org/apache/commons/lang3/tuple/PairTest.java index ec7e22cbe91..229c8b4f62b 100644 --- a/src/test/java/org/apache/commons/lang3/tuple/PairTest.java +++ b/src/test/java/org/apache/commons/lang3/tuple/PairTest.java @@ -29,6 +29,7 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.TreeMap; import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; @@ -127,6 +128,89 @@ public void testEmptyArrayLength() { assertEquals(0, empty.length); } + @Test + public void testEqualsAnonynous() { + final Pair pair = Pair.of("a", "b"); + final String key = "a"; + final String value = "b"; + final Map.Entry entry = new Map.Entry() { + + @Override + public boolean equals(final Object o) { + if (!(o instanceof Map.Entry)) { + return false; + } + final Map.Entry e = (Map.Entry) o; + // FYI java.util.AbstractMap.SimpleEntry.equals(Object) and JDK-8015417 + return Objects.equals(getKey(), e.getKey()) && Objects.equals(getValue(), e.getValue()); + } + + @Override + public String getKey() { + return key; + } + + @Override + public String getValue() { + return value; + } + + @Override + public int hashCode() { + return (getKey() == null ? 0 : getKey().hashCode()) ^ (getValue() == null ? 0 : getValue().hashCode()); + } + + @Override + public String setValue(final String value) { + return null; + } + }; + final Map.Entry entry2 = new Map.Entry() { + + @Override + public boolean equals(final Object o) { + if (!(o instanceof Map.Entry)) { + return false; + } + final Map.Entry e = (Map.Entry) o; + // FYI java.util.AbstractMap.SimpleEntry.equals(Object) and JDK-8015417 + return Objects.equals(getKey(), e.getKey()) && Objects.equals(getValue(), e.getValue()); + } + + @Override + public String getKey() { + return key; + } + + @Override + public String getValue() { + return value; + } + @Override + public int hashCode() { + return (getKey() == null ? 0 : getKey().hashCode()) ^ (getValue() == null ? 0 : getValue().hashCode()); + } + + @Override + public String setValue(final String value) { + return null; + } + }; + assertTrue(pair.equals(entry)); + assertEquals(pair.hashCode(), entry.hashCode()); + assertTrue(pair.equals(entry2)); + assertEquals(pair.hashCode(), entry2.hashCode()); + assertTrue(entry.equals(entry)); + assertEquals(entry.hashCode(), entry.hashCode()); + assertTrue(entry2.equals(entry2)); + assertEquals(entry2.hashCode(), entry2.hashCode()); + assertTrue(entry.equals(entry2)); + assertEquals(entry.hashCode(), entry2.hashCode()); + assertTrue(entry.equals(pair)); + assertEquals(entry.hashCode(), pair.hashCode()); + + } + @Test public void testFormattable_padded() { final Pair pair = Pair.of("Key", "Value");