BAEL-838 Re-adding last changes with regexp and new line strings. (#1825)
* 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.
This commit is contained in:
parent
e8e3234647
commit
9fd2a911b1
|
@ -8,4 +8,12 @@ public class RemoveLastChar {
|
||||||
return (s.substring(0, s.length() - 1));
|
return (s.substring(0, s.length() - 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String chop(String s) {
|
||||||
|
if (s == null) {
|
||||||
|
return s;
|
||||||
|
} else {
|
||||||
|
return s.replaceAll(".$", "");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package com.baeldung.string;
|
package com.baeldung.string;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -12,18 +12,23 @@ public class RemoveLastCharTest {
|
||||||
public static final String EMPTY_STRING = "";
|
public static final String EMPTY_STRING = "";
|
||||||
public static final String ONE_CHAR_STRING = "a";
|
public static final String ONE_CHAR_STRING = "a";
|
||||||
public static final String WHITE_SPACE_AT_THE_END_STRING = "abc ";
|
public static final String WHITE_SPACE_AT_THE_END_STRING = "abc ";
|
||||||
|
public static final String NEW_LINE_AT_THE_END_STRING = "abc\n";
|
||||||
|
public static final String MULTIPLE_LINES_STRING = "abc\ndef";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTestString_whenSubstring_thenGetStingWithoutLastChar() {
|
public void givenTestString_whenSubstring_thenGetStingWithoutLastChar() {
|
||||||
assertEquals("abcde", RemoveLastChar.substring(TEST_STRING));
|
assertEquals("abcde", RemoveLastChar.substring(TEST_STRING));
|
||||||
assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
|
assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
|
||||||
assertEquals("abcde", StringUtils.chop(TEST_STRING));
|
assertEquals("abcde", StringUtils.chop(TEST_STRING));
|
||||||
|
assertEquals("abcde", TEST_STRING.replaceAll(".$", ""));
|
||||||
|
assertEquals("abcde", RemoveLastChar.chop(TEST_STRING));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNullString_whenSubstring_thenGetNullString() {
|
public void givenNullString_whenSubstring_thenGetNullString() {
|
||||||
assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING));
|
assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING));
|
||||||
assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
|
assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
|
||||||
|
assertEquals(NULL_STRING, RemoveLastChar.chop(NULL_STRING));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -31,6 +36,8 @@ public class RemoveLastCharTest {
|
||||||
assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING));
|
assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING));
|
||||||
assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
|
assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
|
||||||
assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
|
assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
|
||||||
|
assertEquals(EMPTY_STRING, EMPTY_STRING.replaceAll(".$", ""));
|
||||||
|
assertEquals(EMPTY_STRING, RemoveLastChar.chop(EMPTY_STRING));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -38,6 +45,8 @@ public class RemoveLastCharTest {
|
||||||
assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING));
|
assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING));
|
||||||
assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1));
|
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, StringUtils.chop(ONE_CHAR_STRING));
|
||||||
|
assertEquals(EMPTY_STRING, ONE_CHAR_STRING.replaceAll(".$", ""));
|
||||||
|
assertEquals(EMPTY_STRING, RemoveLastChar.chop(ONE_CHAR_STRING));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -45,5 +54,25 @@ public class RemoveLastCharTest {
|
||||||
assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING));
|
assertEquals("abc", RemoveLastChar.substring(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.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", 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenStringWithNewLineAtTheEnd_whenSubstring_thenGetStringWithoutNewLine() {
|
||||||
|
assertEquals("abc", RemoveLastChar.substring(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));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMultiLineString_whenSubstring_thenGetStringWithoutNewLine() {
|
||||||
|
assertEquals("abc\nde", RemoveLastChar.substring(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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue