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.
This commit is contained in:
Jonathan Cook 2020-02-25 05:10:42 +01:00 committed by GitHub
parent bbd41d5672
commit 0df2b1bb4b
2 changed files with 172 additions and 3 deletions

View File

@ -12,15 +12,32 @@
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>dsiutils</artifactId>
<version>${dsiutils.version}</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>java-numbers-3</finalName>
<resources>
@ -33,6 +50,9 @@
<properties>
<dsiutils.version>2.6.0</dsiutils.version>
<vavr.version>0.10.2</vavr.version>
<commons.version>3.9</commons.version>
<assertj.version>3.6.1</assertj.version>
</properties>
</project>

View File

@ -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<Double> 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);
}
}