Skip to content

Commit c53095a

Browse files
iaforekpmaric
authored andcommitted
BAEL-838 Refromatted code using formatter.xml. Added Assert.assertEquals import. Renamed test to follow convention. Reordered tests.
(cherry picked from commit d9d35f8)
1 parent b29cb58 commit c53095a

File tree

2 files changed

+49
-69
lines changed

2 files changed

+49
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
/**
2-
*
3-
*/
41
package com.baeldung.string;
52

6-
/**
7-
* @author iaforek
8-
*
9-
*/
103
public class RemoveLastChar {
11-
public static String substring (String s) {
12-
if (s == null || s.length() == 0) {
13-
return s;
14-
} else {
15-
return (s.substring(0, s.length() - 1));
16-
}
17-
}
4+
public static String substring(String s) {
5+
if (s == null || s.length() == 0) {
6+
return s;
7+
} else {
8+
return (s.substring(0, s.length() - 1));
9+
}
10+
}
1811
}
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,49 @@
1-
/**
2-
*
3-
*/
41
package com.baeldung.string;
52

3+
import static org.junit.Assert.assertEquals;
4+
65
import org.apache.commons.lang3.StringUtils;
7-
import org.junit.Assert;
86
import org.junit.Test;
97

10-
/**
11-
* @author iaforek
12-
*
13-
*/
148
public class RemoveLastCharTest {
15-
16-
public static final String TEST_STRING = "abcdef";
17-
public static final String NULL_STRING = null;
18-
public static final String EMPTY_STRING = "";
19-
public static final String ONE_CHAR_STRING = "a";
20-
public static final String WHITE_SPACE_AT_THE_END_STRING = "abc ";
21-
22-
/**
23-
* Test method for {@link com.baeldung.string.RemoveLastChar#substring(java.lang.String)}.
24-
*/
25-
@Test
26-
public void givenTestString_substring_getStingWithoutLastChar() {
27-
Assert.assertEquals("abcde", RemoveLastChar.substring(TEST_STRING));
28-
Assert.assertEquals("abcde", StringUtils.chop(TEST_STRING));
29-
Assert.assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
30-
}
31-
32-
@Test
33-
public void givenNullString_substring_getNullString() {
34-
Assert.assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING));
35-
Assert.assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
36-
37-
}
38-
39-
@Test
40-
public void givenEmptyString_substring_getEmptyString() {
41-
Assert.assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING));
42-
Assert.assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
43-
Assert.assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
44-
45-
}
46-
47-
@Test
48-
public void givenOneCharString_substring_getEmptyString() {
49-
Assert.assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING));
50-
Assert.assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING));
51-
Assert.assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1));
52-
53-
}
54-
55-
@Test
56-
public void givenStringWithWhiteSpaceAtTheEnd_substring_getStringWithoutWhiteSpaceAtTheEnd() {
57-
Assert.assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING));
58-
Assert.assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING));
59-
Assert.assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1));
60-
}
61-
9+
10+
public static final String TEST_STRING = "abcdef";
11+
public static final String NULL_STRING = null;
12+
public static final String EMPTY_STRING = "";
13+
public static final String ONE_CHAR_STRING = "a";
14+
public static final String WHITE_SPACE_AT_THE_END_STRING = "abc ";
15+
16+
@Test
17+
public void givenTestString_whenSubstring_thenGetStingWithoutLastChar() {
18+
assertEquals("abcde", RemoveLastChar.substring(TEST_STRING));
19+
assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
20+
assertEquals("abcde", StringUtils.chop(TEST_STRING));
21+
}
22+
23+
@Test
24+
public void givenNullString_whenSubstring_thenGetNullString() {
25+
assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING));
26+
assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
27+
}
28+
29+
@Test
30+
public void givenEmptyString_whenSubstring_thenGetEmptyString() {
31+
assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING));
32+
assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
33+
assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
34+
}
35+
36+
@Test
37+
public void givenOneCharString_whenSubstring_thenGetEmptyString() {
38+
assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING));
39+
assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1));
40+
assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING));
41+
}
42+
43+
@Test
44+
public void givenStringWithWhiteSpaceAtTheEnd_whenSubstring_thenGetStringWithoutWhiteSpaceAtTheEnd() {
45+
assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING));
46+
assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1));
47+
assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING));
48+
}
6249
}

0 commit comments

Comments
 (0)