From c76c811b1cb72b71c8c127c9e3da41a731e4adff Mon Sep 17 00:00:00 2001 From: patkorek Date: Tue, 7 Apr 2020 10:23:15 +0200 Subject: [PATCH 1/5] BAEL-2660 few examples to convert String to Int --- .../strings/ConvertStringToInt.groovy | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy diff --git a/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy b/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy new file mode 100644 index 0000000000..40c53965a5 --- /dev/null +++ b/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy @@ -0,0 +1,92 @@ +package com.baeldung.strings + +import org.junit.Test + +import java.text.DecimalFormat + +import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertNull + +class ConvertStringToInt { + + @Test + void givenString_thenConvertToIntegerUsingAsInteger() { + def stringNum = "123" + def invalidString = "123a" + Integer expectedInteger = 123 + Integer integerNum = stringNum as Integer + def intNum = invalidString?.isInteger() ? invalidString as Integer : null + + assertNull(null, intNum) + assertEquals(integerNum, expectedInteger) + } + + @Test + void givenString_thenConvertToIntUsingAsInt() { + def stringNum = "123" + int expectedInt = 123 + int intNum = stringNum as int + + assertEquals(intNum, expectedInt) + } + + @Test + void givenString_thenConvertToIntegerUsingToInteger() { + def stringNum = "123" + int expectedInt = 123 + int intNum = stringNum.toInteger() + + assertEquals(intNum, expectedInt) + } + + @Test + void givenString_thenConvertToIntegerUsingParseInt() { + def stringNum = "123" + int expectedInt = 123 + int intNum = Integer.parseInt(stringNum) + + assertEquals(intNum, expectedInt) + } + + @Test + void givenString_thenConvertToIntegerUsingValueOf() { + def stringNum = "123" + int expectedInt = 123 + int intNum = Integer.valueOf(stringNum) + + assertEquals(intNum, expectedInt) + } + + @Test + void givenString_thenConvertToIntegerUsingIntValue() { + def stringNum = "123" + int expectedInt = 123 + int intNum = new Integer(stringNum).intValue() + int secondIntNum = new Integer(stringNum) + + assertEquals(intNum, expectedInt) + assertEquals(secondIntNum, expectedInt) + } + + @Test + void givenString_thenConvertToIntegerUsingDecimalFormat() { + def stringNum = "123" + int expectedInt = 123 + DecimalFormat decimalFormat = new DecimalFormat("#") + int intNum = decimalFormat.parse(stringNum).intValue() + + assertEquals(intNum, expectedInt) + } + + @Test(expected = NumberFormatException.class) + void givenInvalidString_whenUsingAs_thenThrowNumberFormatException() { + def invalidString = "123a" + invalidString as Integer + } + + @Test(expected = NullPointerException.class) + void givenNullString_whenUsingToInteger_thenThrowNullPointerException() { + def invalidString = null + invalidString.toInteger() + } +} From a59aa144001d35a75c7014eb03f7e0457e67161f Mon Sep 17 00:00:00 2001 From: patkorek Date: Tue, 7 Apr 2020 10:34:38 +0200 Subject: [PATCH 2/5] Renamed methods --- .../com/baeldung/strings/ConvertStringToInt.groovy | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy b/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy index 40c53965a5..3b179ee193 100644 --- a/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy +++ b/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy @@ -10,7 +10,7 @@ import static org.junit.Assert.assertNull class ConvertStringToInt { @Test - void givenString_thenConvertToIntegerUsingAsInteger() { + void givenString_whenUsingAsInteger_thenConvertToInteger() { def stringNum = "123" def invalidString = "123a" Integer expectedInteger = 123 @@ -22,7 +22,7 @@ class ConvertStringToInt { } @Test - void givenString_thenConvertToIntUsingAsInt() { + void givenString_whenUsingAsInt_thenConvertToInt() { def stringNum = "123" int expectedInt = 123 int intNum = stringNum as int @@ -31,7 +31,7 @@ class ConvertStringToInt { } @Test - void givenString_thenConvertToIntegerUsingToInteger() { + void givenString_whenUsingToInteger_thenConvertToInteger() { def stringNum = "123" int expectedInt = 123 int intNum = stringNum.toInteger() @@ -40,7 +40,7 @@ class ConvertStringToInt { } @Test - void givenString_thenConvertToIntegerUsingParseInt() { + void givenString_whenUsingParseInt_thenConvertToInteger() { def stringNum = "123" int expectedInt = 123 int intNum = Integer.parseInt(stringNum) @@ -49,7 +49,7 @@ class ConvertStringToInt { } @Test - void givenString_thenConvertToIntegerUsingValueOf() { + void givenString_whenUsingValueOf_thenConvertToInteger() { def stringNum = "123" int expectedInt = 123 int intNum = Integer.valueOf(stringNum) @@ -58,7 +58,7 @@ class ConvertStringToInt { } @Test - void givenString_thenConvertToIntegerUsingIntValue() { + void givenString_whenUsingIntValue_thenConvertToInteger() { def stringNum = "123" int expectedInt = 123 int intNum = new Integer(stringNum).intValue() @@ -69,7 +69,7 @@ class ConvertStringToInt { } @Test - void givenString_thenConvertToIntegerUsingDecimalFormat() { + void givenString_whenUsingDecimalFormat_thenConvertToInteger() { def stringNum = "123" int expectedInt = 123 DecimalFormat decimalFormat = new DecimalFormat("#") From 76cdbd811ebe8ad93758b22807b8ff4b331a50c5 Mon Sep 17 00:00:00 2001 From: patkorek Date: Fri, 10 Apr 2020 12:49:52 +0200 Subject: [PATCH 3/5] Modified one method --- .../com/baeldung/strings/ConvertStringToInt.groovy | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy b/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy index 3b179ee193..3b73c31596 100644 --- a/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy +++ b/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy @@ -62,10 +62,17 @@ class ConvertStringToInt { def stringNum = "123" int expectedInt = 123 int intNum = new Integer(stringNum).intValue() - int secondIntNum = new Integer(stringNum) assertEquals(intNum, expectedInt) - assertEquals(secondIntNum, expectedInt) + } + + @Test + void givenString_whenUsingNewInteger_thenConvertToInteger() { + def stringNum = "123" + int expectedInt = 123 + int intNum = new Integer(stringNum) + + assertEquals(intNum, expectedInt) } @Test From 949881921e6513972cc7143517c94b85b81f5644 Mon Sep 17 00:00:00 2001 From: patkorek Date: Fri, 10 Apr 2020 13:27:27 +0200 Subject: [PATCH 4/5] Added test case with isInteger() method. --- .../com/baeldung/strings/ConvertStringToInt.groovy | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy b/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy index 3b73c31596..7a4be4a957 100644 --- a/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy +++ b/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy @@ -96,4 +96,15 @@ class ConvertStringToInt { def invalidString = null invalidString.toInteger() } + + @Test + void givenString_whenUsingIsInteger_thenCheckIfCorrectValue() { + def invalidString = "123a" + def validString = "123" + def invalidNum = invalidString?.isInteger() ? invalidString as Integer : false + def correctNum = validString?.isInteger() ? validString as Integer : false + + assertEquals(false, invalidNum) + assertEquals(123, correctNum) + } } From 552c1d805b281c6c575b269892e58d9b13d84b5b Mon Sep 17 00:00:00 2001 From: patkorek Date: Sat, 11 Apr 2020 14:39:36 +0200 Subject: [PATCH 5/5] Moved to com.baeldung.stringtoint package. --- .../baeldung/{strings => stringtoint}/ConvertStringToInt.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename core-groovy/src/test/groovy/com/baeldung/{strings => stringtoint}/ConvertStringToInt.groovy (98%) diff --git a/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy b/core-groovy/src/test/groovy/com/baeldung/stringtoint/ConvertStringToInt.groovy similarity index 98% rename from core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy rename to core-groovy/src/test/groovy/com/baeldung/stringtoint/ConvertStringToInt.groovy index 7a4be4a957..48cf48fa8a 100644 --- a/core-groovy/src/test/groovy/com/baeldung/strings/ConvertStringToInt.groovy +++ b/core-groovy/src/test/groovy/com/baeldung/stringtoint/ConvertStringToInt.groovy @@ -1,4 +1,4 @@ -package com.baeldung.strings +package com.baeldung.stringtoint import org.junit.Test