Skip to content

Commit 711ceea

Browse files
bjhargravescordio
andauthored
Update MapEntry::hashCode to honor Map.Entry::hashCode contract (assertj#2503)
Co-authored-by: Stefano Cordio <stefano_cordio@epam.com>
1 parent fac028d commit 711ceea

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/main/java/org/assertj/core/data/MapEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public boolean equals(Object object) {
5858

5959
@Override
6060
public int hashCode() {
61-
return Objects.hash(key, value);
61+
return Objects.hashCode(key) ^ Objects.hashCode(value);
6262
}
6363

6464
@Override

src/test/java/org/assertj/core/data/MapEntry_Test.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
import static org.assertj.core.api.BDDAssertions.then;
1717
import static org.assertj.core.data.MapEntry.entry;
1818

19+
import java.util.Objects;
20+
1921
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.params.ParameterizedTest;
23+
import org.junit.jupiter.params.provider.CsvSource;
2024

2125
import nl.jqno.equalsverifier.EqualsVerifier;
2226

2327
/**
24-
* Tests for {@link MapEntry}.
25-
*
2628
* @author Alex Ruiz
2729
*/
2830
class MapEntry_Test {
@@ -34,6 +36,23 @@ void should_honor_equals_contract() {
3436
.verify();
3537
}
3638

39+
@ParameterizedTest
40+
@CsvSource({
41+
"name, Yoda",
42+
" , Yoda",
43+
"name, ",
44+
" , ",
45+
})
46+
void should_honor_Entry_hashCode_contract(String key, String value) {
47+
// GIVEN
48+
MapEntry<String, String> underTest = entry(key, value);
49+
int expected = Objects.hashCode(key) ^ Objects.hashCode(value);
50+
// WHEN
51+
int result = underTest.hashCode();
52+
// THEN
53+
then(result).isEqualTo(expected);
54+
}
55+
3756
@Test
3857
void setValue_should_fail() {
3958
// GIVEN

0 commit comments

Comments
 (0)