BAEL-838 Refactord code with Java8 examples. (#1833)

* Code for Dependency Injection Article.

* Added Java based configuration. Downloaded formatter.xml and reformatted
all changed files. Manually changed tab into 4 spaces in XML
configuration files.

* BAEL-434 - Spring Roo project files generated by Spring Roo. No
formatting applied. Added POM, java and resources folders.

* Moved project from roo to spring-roo folder.

* BAEL-838 Initial code showing how to remove last char - helper class and tests.

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

* BAEL-838 Refromatted code using formatter.xml. Added Assert.assertEquals import. Renamed test to follow convention. Reordered tests.

* BAEL-838 - Added regex method and updated tests.

* BAEL-838 Added new line examples.

* BAEL-838 Renamed RemoveLastChar class to StringHelper and added Java8 examples. Refactord code.

* BAEL-838 Changed method names
This commit is contained in:
iaforek 2017-05-13 23:24:31 +01:00 committed by pedja4
parent d172d2d63d
commit 075eeb1539
3 changed files with 55 additions and 34 deletions

View File

@ -1,19 +0,0 @@
package com.baeldung.string;
public class RemoveLastChar {
public static String substring(String s) {
if (s == null || s.length() == 0) {
return s;
} else {
return (s.substring(0, s.length() - 1));
}
}
public static String chop(String s) {
if (s == null) {
return s;
} else {
return s.replaceAll(".$", "");
}
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.string;
import java.util.Optional;
public class StringHelper {
public static String removeLastChar(String s) {
return (s == null || s.length() == 0) ? s : (s.substring(0, s.length() - 1));
}
public static String removeLastCharRegex(String s) {
return (s == null) ? s : s.replaceAll(".$", "");
}
public static String removeLastCharOptional(String s) {
return Optional.ofNullable(s)
.filter(str -> str.length() != 0)
.map(str -> str.substring(0, str.length() - 1))
.orElse(s);
}
public static String removeLastCharRegexOptional(String s) {
return Optional.ofNullable(s)
.map(str -> str.replaceAll(".$", ""))
.orElse(null);
}
}

View File

@ -5,7 +5,7 @@ import static org.junit.Assert.assertNotEquals;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
public class RemoveLastCharTest {
public class StringHelperTest {
public static final String TEST_STRING = "abcdef";
public static final String NULL_STRING = null;
@ -17,62 +17,76 @@ public class RemoveLastCharTest {
@Test
public void givenTestString_whenSubstring_thenGetStingWithoutLastChar() {
assertEquals("abcde", RemoveLastChar.substring(TEST_STRING));
assertEquals("abcde", StringHelper.removeLastChar(TEST_STRING));
assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
assertEquals("abcde", StringUtils.chop(TEST_STRING));
assertEquals("abcde", TEST_STRING.replaceAll(".$", ""));
assertEquals("abcde", RemoveLastChar.chop(TEST_STRING));
assertEquals("abcde", StringHelper.removeLastCharRegex(TEST_STRING));
assertEquals("abcde", StringHelper.removeLastCharOptional(TEST_STRING));
assertEquals("abcde", StringHelper.removeLastCharRegexOptional(TEST_STRING));
}
@Test
public void givenNullString_whenSubstring_thenGetNullString() {
assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING));
assertEquals(NULL_STRING, StringHelper.removeLastChar(NULL_STRING));
assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
assertEquals(NULL_STRING, RemoveLastChar.chop(NULL_STRING));
assertEquals(NULL_STRING, StringHelper.removeLastCharRegex(NULL_STRING));
assertEquals(NULL_STRING, StringHelper.removeLastCharOptional(NULL_STRING));
assertEquals(NULL_STRING, StringHelper.removeLastCharRegexOptional(NULL_STRING));
}
@Test
public void givenEmptyString_whenSubstring_thenGetEmptyString() {
assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
assertEquals(EMPTY_STRING, EMPTY_STRING.replaceAll(".$", ""));
assertEquals(EMPTY_STRING, RemoveLastChar.chop(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegex(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharOptional(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegexOptional(EMPTY_STRING));
}
@Test
public void givenOneCharString_whenSubstring_thenGetEmptyString() {
assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1));
assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, ONE_CHAR_STRING.replaceAll(".$", ""));
assertEquals(EMPTY_STRING, RemoveLastChar.chop(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegex(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharOptional(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegexOptional(ONE_CHAR_STRING));
}
@Test
public void givenStringWithWhiteSpaceAtTheEnd_whenSubstring_thenGetStringWithoutWhiteSpaceAtTheEnd() {
assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastChar(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1));
assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", WHITE_SPACE_AT_THE_END_STRING.replaceAll(".$", ""));
assertEquals("abc", RemoveLastChar.chop(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastCharRegex(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastCharOptional(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastCharRegexOptional(WHITE_SPACE_AT_THE_END_STRING));
}
@Test
public void givenStringWithNewLineAtTheEnd_whenSubstring_thenGetStringWithoutNewLine() {
assertEquals("abc", RemoveLastChar.substring(NEW_LINE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastChar(NEW_LINE_AT_THE_END_STRING));
assertEquals("abc", StringUtils.substring(NEW_LINE_AT_THE_END_STRING, 0, NEW_LINE_AT_THE_END_STRING.length() - 1));
assertEquals("abc", StringUtils.chop(NEW_LINE_AT_THE_END_STRING));
assertNotEquals("abc", NEW_LINE_AT_THE_END_STRING.replaceAll(".$", ""));
assertNotEquals("abc", RemoveLastChar.chop(NEW_LINE_AT_THE_END_STRING));
assertNotEquals("abc", StringHelper.removeLastCharRegex(NEW_LINE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastCharOptional(NEW_LINE_AT_THE_END_STRING));
assertNotEquals("abc", StringHelper.removeLastCharRegexOptional(NEW_LINE_AT_THE_END_STRING));
}
@Test
public void givenMultiLineString_whenSubstring_thenGetStringWithoutNewLine() {
assertEquals("abc\nde", RemoveLastChar.substring(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringHelper.removeLastChar(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringUtils.substring(MULTIPLE_LINES_STRING, 0, MULTIPLE_LINES_STRING.length() - 1));
assertEquals("abc\nde", StringUtils.chop(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", MULTIPLE_LINES_STRING.replaceAll(".$", ""));
assertEquals("abc\nde", RemoveLastChar.chop(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringHelper.removeLastCharRegex(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringHelper.removeLastCharOptional(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringHelper.removeLastCharRegexOptional(MULTIPLE_LINES_STRING));
}
}