BAEL-838 Corrected Helper class and associated empty string test case. Added StringUtils.substing tests.

(cherry picked from commit 124966a)
This commit is contained in:
iaforek 2017-05-08 00:11:03 +02:00 committed by Predrag Maric
parent 6e589636ed
commit b29cb580a6
2 changed files with 9 additions and 3 deletions

View File

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

View File

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