BAEL-3141: Created new module for 'Converting Java String to Double' and other newly created string articles. (#7548)

This commit is contained in:
Justin Albano
2019-08-10 12:40:34 -04:00
committed by maibin
parent efd0d81ac5
commit b891c53dcc
7 changed files with 245 additions and 56 deletions

View File

@@ -0,0 +1,3 @@
This file exists to ensure this empty directory is committed in Git.
Please remove this file when this directory is populated.

View File

@@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

View File

@@ -0,0 +1,56 @@
package com.baeldung.string.todouble;
import static org.junit.Assert.assertEquals;
import java.text.DecimalFormat;
import java.text.ParseException;
import org.junit.Test;
public class StringToDoubleConversionUnitTest {
@Test
public void givenValidString_WhenParseDouble_ThenResultIsPrimitiveDouble() {
assertEquals(1.23, Double.parseDouble("1.23"), 0.000001);
}
@Test(expected = NullPointerException.class)
public void givenNullString_WhenParseDouble_ThenNullPointerExceptionIsThrown() {
Double.parseDouble(null);
}
@Test(expected = NumberFormatException.class)
public void givenInalidString_WhenParseDouble_ThenNumberFormatExceptionIsThrown() {
Double.parseDouble("&");
}
@Test
public void givenValidString_WhenValueOf_ThenResultIsPrimitiveDouble() {
assertEquals(1.23, Double.valueOf("1.23"), 0.000001);
}
@Test(expected = NullPointerException.class)
public void givenNullString_WhenValueOf_ThenNullPointerExceptionIsThrown() {
Double.valueOf(null);
}
@Test(expected = NumberFormatException.class)
public void givenInalidString_WhenValueOf_ThenNumberFormatExceptionIsThrown() {
Double.valueOf("&");
}
@Test
public void givenValidString_WhenDecimalFormat_ThenResultIsValidDouble() throws ParseException {
assertEquals(1.23, new DecimalFormat("#").parse("1.23").doubleValue(), 0.000001);
}
@Test(expected = NullPointerException.class)
public void givenNullString_WhenDecimalFormat_ThenNullPointerExceptionIsThrown() throws ParseException {
new DecimalFormat("#").parse(null);
}
@Test(expected = ParseException.class)
public void givenInvalidString_WhenDecimalFormat_ThenParseExceptionIsThrown() throws ParseException {
new DecimalFormat("#").parse("&");
}
}

View File

@@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear