diff --git a/core-java-modules/core-java-string-conversions-3/README.md b/core-java-modules/core-java-string-conversions-3/README.md new file mode 100644 index 0000000000..4e03f060fb --- /dev/null +++ b/core-java-modules/core-java-string-conversions-3/README.md @@ -0,0 +1,6 @@ +## Java String Conversions + +This module contains articles about string conversions from/to another type. + +### Relevant Articles: +- More articles: [[next -->]](/core-java-modules/core-java-string-conversions-2) diff --git a/core-java-modules/core-java-string-conversions-3/pom.xml b/core-java-modules/core-java-string-conversions-3/pom.xml new file mode 100644 index 0000000000..ef51c0dfdf --- /dev/null +++ b/core-java-modules/core-java-string-conversions-3/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + core-java-string-conversions-3 + 0.1.0-SNAPSHOT + core-java-string-conversions-3 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + core-java-string-conversions-3 + + + src/main/resources + true + + + + \ No newline at end of file diff --git a/core-java-modules/core-java-string-conversions/src/main/java/com/baeldung/stringtoint/StringToIntConverter.java b/core-java-modules/core-java-string-conversions-3/src/main/java/com/baeldung/stringtoint/StringToIntConverter.java similarity index 99% rename from core-java-modules/core-java-string-conversions/src/main/java/com/baeldung/stringtoint/StringToIntConverter.java rename to core-java-modules/core-java-string-conversions-3/src/main/java/com/baeldung/stringtoint/StringToIntConverter.java index 03d77c2aea..fd19ca61d6 100644 --- a/core-java-modules/core-java-string-conversions/src/main/java/com/baeldung/stringtoint/StringToIntConverter.java +++ b/core-java-modules/core-java-string-conversions-3/src/main/java/com/baeldung/stringtoint/StringToIntConverter.java @@ -4,7 +4,7 @@ import java.util.Optional; import org.apache.commons.lang3.math.NumberUtils; public class StringToIntConverter { - + private StringToIntConverter() { } @@ -50,3 +50,4 @@ public class StringToIntConverter { return NumberUtils.toInt(input, Integer.MIN_VALUE); } } + diff --git a/core-java-modules/core-java-string-conversions-3/src/test/java/com/baeldung/stringtoint/StringToIntConverterUnitTest.java b/core-java-modules/core-java-string-conversions-3/src/test/java/com/baeldung/stringtoint/StringToIntConverterUnitTest.java new file mode 100644 index 0000000000..a1d365b361 --- /dev/null +++ b/core-java-modules/core-java-string-conversions-3/src/test/java/com/baeldung/stringtoint/StringToIntConverterUnitTest.java @@ -0,0 +1,53 @@ +package com.baeldung.stringtoint; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class StringToIntConverterUnitTest { + + @Test + void whenConvertingIntToString_thenInvalidCasesReturnIntegerMinValue() { + List testData = Arrays.asList( + new TestData("", Integer.MIN_VALUE), + new TestData(null, Integer.MIN_VALUE), + new TestData("23,56", Integer.MIN_VALUE), + new TestData("2147483648", Integer.MIN_VALUE), + new TestData("-2147483649", Integer.MIN_VALUE), + new TestData("hello", Integer.MIN_VALUE) + ); + testData.forEach(data -> { + Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerParseInt(data.input)); + Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerValueOf(data.input)); + Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingOptional(data.input)); + Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerDecode(data.input)); + Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingNumberUtils(data.input)); + }); + } + + @Test + void whenConvertingIntToString_thenValidCasesReturnUnboxedInt() { + List testData = Arrays.asList( + new TestData("23", 23), + new TestData("-23", -23) + ); + testData.forEach(data -> { + Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerParseInt(data.input)); + Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerValueOf(data.input)); + Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingOptional(data.input)); + Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingNumberUtils(data.input)); + Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerDecode(data.input)); + }); + } + + public static class TestData{ + String input; + Integer expectedOutput; + + TestData(String input, Integer expectedOutput){ + this.input = input; + this.expectedOutput = expectedOutput; + } + } +} diff --git a/core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntConverterUnitTest.java b/core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntConverterUnitTest.java deleted file mode 100644 index 2bfb4db161..0000000000 --- a/core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntConverterUnitTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.baeldung.stringtoint; - -import java.util.Arrays; -import java.util.List; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class StringToIntConverterUnitTest { - - @Test - void whenConvertingIntToString_thenInvalidCasesReturnIntegerMinValue() { - List testData = Arrays.asList( - new TestData("", Integer.MIN_VALUE), - new TestData(null, Integer.MIN_VALUE), - new TestData("23,56", Integer.MIN_VALUE), - new TestData("2147483648", Integer.MIN_VALUE), - new TestData("-2147483649", Integer.MIN_VALUE), - new TestData("hello", Integer.MIN_VALUE) - ); - testData.forEach(data -> { - Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerParseInt(data.input)); - Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerValueOf(data.input)); - Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingOptional(data.input)); - Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerDecode(data.input)); - Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingNumberUtils(data.input)); - }); - } - - @Test - void whenConvertingIntToString_thenValidCasesReturnUnboxedInt() { - List testData = Arrays.asList( - new TestData("23", 23), - new TestData("-23", -23) - ); - testData.forEach(data -> { - Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerParseInt(data.input)); - Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerValueOf(data.input)); - Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingOptional(data.input)); - Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingNumberUtils(data.input)); - Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerDecode(data.input)); - }); - } - - public static class TestData{ - String input; - Integer expectedOutput; - - TestData(String input, Integer expectedOutput){ - this.input = input; - this.expectedOutput = expectedOutput; - } - } -} \ No newline at end of file