Skip to content

Commit

Permalink
Add testEqualsAnonynous()
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Sep 26, 2024
1 parent 4a8aa9e commit 87e0fd7
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/test/java/org/apache/commons/lang3/tuple/PairTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -127,6 +128,89 @@ public void testEmptyArrayLength() {
assertEquals(0, empty.length);
}

@Test
public void testEqualsAnonynous() {
final Pair<String, String> pair = Pair.of("a", "b");
final String key = "a";
final String value = "b";
final Map.Entry<String, String> entry = new Map.Entry<String, String>() {

@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<String, String> entry2 = new Map.Entry<String, String>() {

@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<String, String> pair = Pair.of("Key", "Value");
Expand Down

0 comments on commit 87e0fd7

Please sign in to comment.