From c58c9e7792097a7aa075d941c97ee698801e6931 Mon Sep 17 00:00:00 2001 From: Aaron Clark Date: Fri, 26 Nov 2021 20:22:25 +0000 Subject: [PATCH] #540 BigDecimal equality using compareTo --- .../extended_contract/BigDecimalTest.java | 69 ++++++++++++++----- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/BigDecimalTest.java b/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/BigDecimalTest.java index c506cf67e..b759afd16 100644 --- a/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/BigDecimalTest.java +++ b/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/BigDecimalTest.java @@ -12,9 +12,13 @@ public class BigDecimalTest { @Test - public void fail_whenBigDecimalsComparedUsingEquals() { + public void fail_whenBigDecimalsComparedUsingEqualsWithComparablyConsistentHashCode() { ExpectedException - .when(() -> EqualsVerifier.forClass(BigDecimalEquals.class).verify()) + .when(() -> + EqualsVerifier + .forClass(BigDecimalEqualsWithComparablyConsistentHashCode.class) + .verify() + ) .assertFailure() .assertMessageContains( "BigDecimal", @@ -25,19 +29,6 @@ public void fail_whenBigDecimalsComparedUsingEquals() { ); } - @Test - public void succeed_whenBigDecimalsComparedUsingEquals_givenBigDecimalEqualsWarningIsSuppressed() { - EqualsVerifier - .forClass(BigDecimalEquals.class) - .suppress(Warning.BIGDECIMAL_EQUALITY) - .verify(); - } - - @Test - public void succeed_whenBigDecimalsComparedUsingCompareTo() { - EqualsVerifier.forClass(BigDecimalCompareTo.class).verify(); - } - @Test public void fail_whenBigDecimalsComparedUsingCompareToWithInconsistentHashCode() { ExpectedException @@ -52,6 +43,19 @@ public void fail_whenBigDecimalsComparedUsingCompareToWithInconsistentHashCode() ); } + @Test + public void succeed_whenBigDecimalsComparedUsingEquals_givenBigDecimalEqualsWarningIsSuppressed() { + EqualsVerifier + .forClass(BigDecimalEquals.class) + .suppress(Warning.BIGDECIMAL_EQUALITY) + .verify(); + } + + @Test + public void succeed_whenBigDecimalsComparedUsingCompareTo() { + EqualsVerifier.forClass(BigDecimalCompareTo.class).verify(); + } + /** * Uses standard equals and hashCode for objects. * 0 and 0.0 are not equal. @@ -107,8 +111,39 @@ public int hashCode() { } /** - * Uses compareTo for BigDecimal equality but has hashCode that may be inconsistent. - * 0 and 0.0 are equal but may produce a different hashCode. + * Uses standard equals but with a consistent hashCode for comparably equal instances. + * 0 and 0.0 are not equal but produce the same hashCode. + */ + private static final class BigDecimalEqualsWithComparablyConsistentHashCode { + + private final BigDecimal bd; + + public BigDecimalEqualsWithComparablyConsistentHashCode(BigDecimal bd) { + this.bd = bd; + } + + public BigDecimal getBd() { + return bd; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof BigDecimalEqualsWithComparablyConsistentHashCode)) { + return false; + } + BigDecimalEqualsWithComparablyConsistentHashCode other = (BigDecimalEqualsWithComparablyConsistentHashCode) obj; + return Objects.equals(bd, other.bd); + } + + @Override + public int hashCode() { + return Objects.hashCode(comparablyHashed(bd)); + } + } + + /** + * Uses compareTo for BigDecimal equality but has hashCode that is inconsistent. + * 0 and 0.0 are equal but produce different hashCodes. */ private static final class BigDecimalInconsistentHashCode {