From 0df2b1bb4b8daf2e99630fa6635182434d684aaf Mon Sep 17 00:00:00 2001 From: Jonathan Cook Date: Tue, 25 Feb 2020 05:10:42 +0100 Subject: [PATCH] BAEL-3491 - Check for null before calling parse in the Double.parseDouble (#8752) * BAEL-3491 - Check for null before calling parse in the Double.parseDouble * BAEL-3491 - Check for null before calling parse in the Double.parseDouble - Return to indentation with spaces. --- java-numbers-3/pom.xml | 26 ++- .../StringToDoubleParserUnitTest.java | 149 ++++++++++++++++++ 2 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 java-numbers-3/src/test/java/com/baeldung/parsedouble/StringToDoubleParserUnitTest.java diff --git a/java-numbers-3/pom.xml b/java-numbers-3/pom.xml index bf5fe9b0e7..495618885a 100644 --- a/java-numbers-3/pom.xml +++ b/java-numbers-3/pom.xml @@ -12,15 +12,32 @@ 0.0.1-SNAPSHOT ../parent-java - + - + it.unimi.dsi dsiutils ${dsiutils.version} + + io.vavr + vavr + ${vavr.version} + + + org.apache.commons + commons-lang3 + ${commons.version} + test + + + org.assertj + assertj-core + ${assertj.version} + test + - + java-numbers-3 @@ -33,6 +50,9 @@ 2.6.0 + 0.10.2 + 3.9 + 3.6.1 diff --git a/java-numbers-3/src/test/java/com/baeldung/parsedouble/StringToDoubleParserUnitTest.java b/java-numbers-3/src/test/java/com/baeldung/parsedouble/StringToDoubleParserUnitTest.java new file mode 100644 index 0000000000..b3dec3d14f --- /dev/null +++ b/java-numbers-3/src/test/java/com/baeldung/parsedouble/StringToDoubleParserUnitTest.java @@ -0,0 +1,149 @@ +package com.baeldung.parsedouble; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Optional; + +import org.apache.commons.lang3.math.NumberUtils; +import org.junit.Test; + +import com.google.common.base.MoreObjects; +import com.google.common.primitives.Doubles; + +import io.vavr.control.Try; + +public class StringToDoubleParserUnitTest { + + @Test + public void givenNullValue_whenParseStringToDouble_thenDefaultNaNValueIsReturned() { + assertThat(parseStringToDouble(null)).isNaN(); + } + + @Test + public void givenEmptyStringValue_whenParseStringToDouble_thenDefaultNaNValueIsReturned() { + assertThat(parseStringToDouble("")).isNaN(); + } + + @Test + public void givenStringValue_whenParseStringToDouble_thenDoubleValueIsReturned() { + assertThat(parseStringToDouble("1")).isEqualTo(1.0d); + } + + @Test + public void givenStringValue_whenParseStringToDoubleWithDefault_thenDoubleValueIsReturned() { + assertThat(parseStringToDouble("1", 2.0d)).isEqualTo(1.0d); + } + + @Test + public void givenEmptyStringValue_whenParseStringToDoubleWithDefault_thenDefaultValueIsReturned() { + assertThat(parseStringToDouble("", 1.0d)).isEqualTo(1.0d); + } + + @Test + public void givenNullValue_whenParseStringToDoubleWithDefault_thenDefaultValueIsReturned() { + assertThat(parseStringToDouble(null, 1.0d)).isEqualTo(1.0d); + } + + @Test + public void givenStringValue_whenParseStringToOptionalDouble_thenOptionalValueIsReturned() { + assertThat(parseStringToOptionalDouble("1")).isEqualTo(Optional.of(1.0d)); + } + + @Test + public void givenNullValue_whenParseStringToOptionalDouble_thenOptionalValueIsEmpty() { + assertThat(parseStringToOptionalDouble(null)).isEqualTo(Optional.empty()); + } + + @Test + public void givenEmptyStringValue_whenParseStringToOptionalDouble_thenOptionalValueIsEmpty() { + assertThat(parseStringToOptionalDouble("")).isEqualTo(Optional.empty()); + } + + @Test + public void givenEmptyStringValue_whenParseStringToOptionalDouble_thenDefaulOptionalValueIsReturned() { + assertThat(parseStringToOptionalDouble("").orElse(1.0d)).isEqualTo(1.0d); + } + + @Test + public void givenNullValue_whenParseStringToOptionalDouble_thenDefaulOptionalValueIsReturned() { + assertThat(parseStringToOptionalDouble(null).orElse(1.0d)).isEqualTo(1.0d); + } + + @Test + public void givenStringValue_whenTryStringToDouble_thenDoubleValueIsReturned() { + assertThat(tryStringToDouble("1", 2.0d)).isEqualTo(1.0d); + } + + @Test + public void givenNullValue_whenTryStringToDoubleWithDefault_thenDoubleValueIsReturned() { + assertThat(tryStringToDouble(null, 2.0d)).isEqualTo(2.0d); + } + + @Test + public void givenEmptyStringValue_whenTryStringToDoubleWithDefault_thenDoubleValueIsReturned() { + assertThat(tryStringToDouble("", 2.0d)).isEqualTo(2.0d); + } + + @Test + public void givenTwoStringValues_whenTryParseFirstNonNull_thenDoubleValueIsReturned() { + assertThat(Doubles.tryParse(MoreObjects.firstNonNull("1.0", "2.0"))).isEqualTo(1.0d); + } + + @Test + public void givenNullStringValue_whenTryParseFirstNonNull_thenSecondDoubleValueIsReturned() { + assertThat(Doubles.tryParse(MoreObjects.firstNonNull(null, "2.0"))).isEqualTo(2.0d); + } + + @Test + public void givenEmptyStringValue_whenTryParseFirstNonNull_thenNullIsReturned() { + assertThat(Doubles.tryParse(MoreObjects.firstNonNull("", "2.0"))).isEqualTo(null); + } + + @Test + public void givenStringValue_whenToDouble_thenDoubleValueIsReturned() { + assertThat(NumberUtils.toDouble("1.0")).isEqualTo(1.0d); + } + + @Test + public void givenNullValue_whenToDouble_thenLibraryDefaultDoubleValueIsReturned() { + String nullString = null; + assertThat(NumberUtils.toDouble(nullString)).isEqualTo(0.0d); + } + + @Test + public void givenEmptyStringValue_whenToDouble_thenLibraryDefaultDoubleValueIsReturned() { + assertThat(NumberUtils.toDouble("")).isEqualTo(0.0d); + } + + @Test + public void givenEmptyStringValue_whenToDoubleWithDefault_thenDoubleValueIsReturned() { + assertThat(NumberUtils.toDouble("", 2.0d)).isEqualTo(2.0d); + } + + @Test + public void givenNullValue_whenToDoubleWithDefault_thenDoubleValueIsReturned() { + String nullString = null; + assertThat(NumberUtils.toDouble(nullString, 2.0d)).isEqualTo(2.0d); + } + + @Test + public void givenStringValue_whenToDoubleWithDefault_thenDoubleValueIsReturned() { + assertThat(NumberUtils.toDouble("1.0", 2.0d)).isEqualTo(1.0d); + } + + private static Optional parseStringToOptionalDouble(String value) { + return value == null || value.isEmpty() ? Optional.empty() : Optional.of(Double.valueOf(value)); + } + + private static double parseStringToDouble(String value) { + return value == null || value.isEmpty() ? Double.NaN : Double.parseDouble(value); + } + + private static double parseStringToDouble(String value, double defaultValue) { + return value == null || value.isEmpty() ? defaultValue : Double.parseDouble(value); + } + + private static double tryStringToDouble(String value, double defaultValue) { + return Try.of(() -> Double.parseDouble(value)).getOrElse(defaultValue); + } +}