From 36edee6be794d1a07dd95db2a3550d196c0a8484 Mon Sep 17 00:00:00 2001 From: Eugene Kovko <37694937+eukovko@users.noreply.github.com> Date: Mon, 8 Jan 2024 03:50:22 +0100 Subject: [PATCH] BAEL-7447: Integer to BigDecimal (#15563) --- ...BigDecimalConversionArgumentsProvider.java | 33 ++++++++++++ .../IntegerToBigDecimalUnitTest.java | 53 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/integertobigdecimal/BigDecimalConversionArgumentsProvider.java create mode 100644 core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/integertobigdecimal/IntegerToBigDecimalUnitTest.java diff --git a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/integertobigdecimal/BigDecimalConversionArgumentsProvider.java b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/integertobigdecimal/BigDecimalConversionArgumentsProvider.java new file mode 100644 index 0000000000..8b9900ee9e --- /dev/null +++ b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/integertobigdecimal/BigDecimalConversionArgumentsProvider.java @@ -0,0 +1,33 @@ +package com.baeldung.integertobigdecimal; + +import java.math.BigDecimal; +import java.util.stream.Stream; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; + +public class BigDecimalConversionArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext context) { + return Stream.of( + Arguments.of(Integer.valueOf(0), BigDecimal.valueOf(0)), + Arguments.of(Integer.valueOf(1), BigDecimal.valueOf(1)), + Arguments.of(Integer.valueOf(2), BigDecimal.valueOf(2)), + Arguments.of(Integer.valueOf(3), BigDecimal.valueOf(3)), + Arguments.of(Integer.valueOf(4), BigDecimal.valueOf(4)), + Arguments.of(Integer.valueOf(5), BigDecimal.valueOf(5)), + Arguments.of(Integer.valueOf(6), BigDecimal.valueOf(6)), + Arguments.of(Integer.valueOf(7), BigDecimal.valueOf(7)), + Arguments.of(Integer.valueOf(8), BigDecimal.valueOf(8)), + Arguments.of(Integer.valueOf(9), BigDecimal.valueOf(9)), + Arguments.of(Integer.valueOf(10), BigDecimal.valueOf(10)), + Arguments.of(Integer.valueOf(11), BigDecimal.valueOf(11)), + Arguments.of(Integer.valueOf(12), BigDecimal.valueOf(12)), + Arguments.of(Integer.valueOf(13), BigDecimal.valueOf(13)), + Arguments.of(Integer.valueOf(14), BigDecimal.valueOf(14)), + Arguments.of(Integer.valueOf(15), BigDecimal.valueOf(15)) + ); + } + +} diff --git a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/integertobigdecimal/IntegerToBigDecimalUnitTest.java b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/integertobigdecimal/IntegerToBigDecimalUnitTest.java new file mode 100644 index 0000000000..111670efc3 --- /dev/null +++ b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/integertobigdecimal/IntegerToBigDecimalUnitTest.java @@ -0,0 +1,53 @@ +package com.baeldung.integertobigdecimal; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.math.BigDecimal; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ArgumentsSource; +import org.junit.jupiter.params.provider.ValueSource; + +class IntegerToBigDecimalUnitTest { + + @ParameterizedTest + @ArgumentsSource(BigDecimalConversionArgumentsProvider.class) + void giveIntegerWhenConvertWithConstructorToBigDecimalThenConversionCorrect(Integer given, BigDecimal expected) { + BigDecimal actual = new BigDecimal(given); + assertThat(actual).isEqualTo(expected); + } + + @ParameterizedTest + @ValueSource(ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) + void giveIntegerWhenConvertWithConstructorToBigDecimalThenConversionWithoutCaching(Integer given) { + BigDecimal firstBigDecimal = new BigDecimal(given); + BigDecimal secondBigDecimal = new BigDecimal(given); + assertThat(firstBigDecimal) + .isEqualTo(secondBigDecimal) + .isNotSameAs(secondBigDecimal); + } + + @ParameterizedTest + @ArgumentsSource(BigDecimalConversionArgumentsProvider.class) + void giveIntegerWhenConvertWithValueOfToBigDecimalThenConversionCorrect(Integer given, BigDecimal expected) { + BigDecimal actual = BigDecimal.valueOf(given); + assertThat(actual).isEqualTo(expected); + } + + @ParameterizedTest + @ValueSource(ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) + void giveIntegerWhenConvertWithValueOfToBigDecimalThenConversionCachesTheResults(Integer given) { + BigDecimal firstBigDecimal = BigDecimal.valueOf(given); + BigDecimal secondBigDecimal = BigDecimal.valueOf(given); + assertThat(firstBigDecimal).isSameAs(secondBigDecimal); + } + + @ParameterizedTest + @ValueSource(ints = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}) + void giveIntegerWhenConvertWithValueOfToBigDecimalThenConversionWontCacheTheResults(Integer given) { + BigDecimal firstBigDecimal = BigDecimal.valueOf(given); + BigDecimal secondBigDecimal = BigDecimal.valueOf(given); + assertThat(firstBigDecimal) + .isEqualTo(secondBigDecimal) + .isNotSameAs(secondBigDecimal); + } +}