From b29cb580a62b70661a23ba8109bd885908ee697a Mon Sep 17 00:00:00 2001 From: iaforek Date: Mon, 8 May 2017 00:11:03 +0200 Subject: [PATCH] BAEL-838 Corrected Helper class and associated empty string test case. Added StringUtils.substing tests. (cherry picked from commit 124966a) --- .../main/java/com/baeldung/string/RemoveLastChar.java | 2 +- .../java/com/baeldung/string/RemoveLastCharTest.java | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java b/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java index 3816a61f45..faf30f7b25 100644 --- a/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java +++ b/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java @@ -10,7 +10,7 @@ package com.baeldung.string; public class RemoveLastChar { public static String substring (String s) { if (s == null || s.length() == 0) { - return null; + return s; } else { return (s.substring(0, s.length() - 1)); } diff --git a/core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java b/core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java index 80d76e96ac..a39212ced4 100644 --- a/core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java +++ b/core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java @@ -26,6 +26,7 @@ public class RemoveLastCharTest { public void givenTestString_substring_getStingWithoutLastChar() { Assert.assertEquals("abcde", RemoveLastChar.substring(TEST_STRING)); Assert.assertEquals("abcde", StringUtils.chop(TEST_STRING)); + Assert.assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1)); } @Test @@ -37,20 +38,25 @@ public class RemoveLastCharTest { @Test public void givenEmptyString_substring_getEmptyString() { - Assert.assertEquals(NULL_STRING, RemoveLastChar.substring(EMPTY_STRING)); - Assert.assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING)); + Assert.assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING)); + Assert.assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING)); + Assert.assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1)); + } @Test public void givenOneCharString_substring_getEmptyString() { Assert.assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING)); Assert.assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING)); + Assert.assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1)); + } @Test public void givenStringWithWhiteSpaceAtTheEnd_substring_getStringWithoutWhiteSpaceAtTheEnd() { Assert.assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING)); Assert.assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING)); + Assert.assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1)); } }