diff --git a/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java b/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java index c816ff36c8..523e1d8b3f 100644 --- a/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java +++ b/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java @@ -329,9 +329,9 @@ public void testBigDecimalEquals() { assertThat(small.equals(large)).isFalse(); BigDecimal doubleMax = BigDecimal.valueOf(Double.MAX_VALUE); - JsonPrimitive smallBD = new JsonPrimitive(doubleMax.add(new BigDecimal("100.0"))); - JsonPrimitive largeBD = new JsonPrimitive(doubleMax.add(new BigDecimal("200.0"))); - assertThat(smallBD.equals(largeBD)).isFalse(); + JsonPrimitive smallDecimal = new JsonPrimitive(doubleMax.add(new BigDecimal("100.0"))); + JsonPrimitive largeDecimal = new JsonPrimitive(doubleMax.add(new BigDecimal("200.0"))); + assertThat(smallDecimal.equals(largeDecimal)).isFalse(); } @Test @@ -347,6 +347,22 @@ public void testBigDecimalEqualsZero() { .isTrue(); } + /** + * Verifies that {@link JsonPrimitive#equals(Object)} is transitive for {@link BigDecimal}, + * as required by the {@link Object#equals(Object)} documentation. + */ + @Test + public void testBigDecimalEqualsTransitive() { + JsonPrimitive x = new JsonPrimitive(new BigDecimal("0")); + JsonPrimitive y = new JsonPrimitive(0.0d); + JsonPrimitive z = new JsonPrimitive(new BigDecimal("0.00")); + + assertThat(x.equals(y)).isTrue(); + assertThat(y.equals(z)).isTrue(); + // ... implies + assertThat(x.equals(z)).isTrue(); + } + @Test public void testEqualsDoubleNaNAndBigDecimal() { assertThat(new JsonPrimitive(Double.NaN).equals(new JsonPrimitive(new BigDecimal("1.0"))))