22
33import org .junit .jupiter .api .Test ;
44
5+ import static io .microsphere .collection .ListUtils .newLinkedList ;
6+ import static io .microsphere .collection .Lists .ofList ;
57import static io .microsphere .constants .SymbolConstants .COMMA ;
6- import static io .microsphere .constants .SymbolConstants .COMMA_CHAR ;
78import static io .microsphere .constants .SymbolConstants .DOT ;
9+ import static io .microsphere .constants .SymbolConstants .SHARP ;
810import static io .microsphere .constants .SymbolConstants .SPACE ;
9- import static io .microsphere .constants .SymbolConstants .SPACE_CHAR ;
1011import static io .microsphere .constants .SymbolConstants .VERTICAL_BAR ;
1112import static io .microsphere .util .ArrayUtils .ofArray ;
13+ import static io .microsphere .util .CharSequenceUtils .length ;
1214import static io .microsphere .util .CharSequenceUtilsTest .TEST_BLANK_STRING ;
1315import static io .microsphere .util .CharSequenceUtilsTest .TEST_CSV_STRING ;
1416import static io .microsphere .util .CharSequenceUtilsTest .TEST_EMPTY_STRING ;
3133import static io .microsphere .util .StringUtils .substringBefore ;
3234import static io .microsphere .util .StringUtils .substringBeforeLast ;
3335import static io .microsphere .util .StringUtils .substringBetween ;
36+ import static io .microsphere .util .StringUtils .toStringArray ;
3437import static io .microsphere .util .StringUtils .trimAllWhitespace ;
3538import static io .microsphere .util .StringUtils .trimLeadingWhitespace ;
3639import static io .microsphere .util .StringUtils .trimTrailingWhitespace ;
3740import static io .microsphere .util .StringUtils .trimWhitespace ;
3841import static io .microsphere .util .StringUtils .uncapitalize ;
42+ import static java .util .Collections .emptyList ;
3943import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
4044import static org .junit .jupiter .api .Assertions .assertEquals ;
4145import static org .junit .jupiter .api .Assertions .assertFalse ;
4246import static org .junit .jupiter .api .Assertions .assertNull ;
4347import static org .junit .jupiter .api .Assertions .assertSame ;
4448import static org .junit .jupiter .api .Assertions .assertTrue ;
49+ import static org .springframework .util .StringUtils .delimitedListToStringArray ;
4550
4651/**
4752 * {@link StringUtils} Test
@@ -81,26 +86,37 @@ void testIsNotBlank() {
8186
8287 @ Test
8388 void testSplit () {
84- String [] values = split (null , SPACE_CHAR );
85- assertSame (EMPTY_STRING_ARRAY , values );
89+ assertSame (EMPTY_STRING_ARRAY , assertSplit (null , SPACE ));
8690
87- values = split (TEST_EMPTY_STRING , SPACE );
88- assertSame (EMPTY_STRING_ARRAY , values );
91+ assertSame (EMPTY_STRING_ARRAY , assertSplit (TEST_EMPTY_STRING , SPACE ));
8992
90- values = split (TEST_BLANK_STRING , null );
91- assertSame (EMPTY_STRING_ARRAY , values );
93+ assertSame (EMPTY_STRING_ARRAY , assertSplit (TEST_EMPTY_STRING , EMPTY_STRING ));
9294
93- values = split (TEST_BLANK_STRING , SPACE );
94- assertArrayEquals (EMPTY_STRING_ARRAY , values );
95+ assertArrayEquals (ofArray (TEST_BLANK_STRING ), assertSplit (TEST_BLANK_STRING , null ));
9596
96- values = split (SPACE + SPACE , SPACE );
97- assertArrayEquals (EMPTY_STRING_ARRAY , values );
97+ assertArrayEquals (ofArray (TEST_BLANK_STRING ), assertSplit (TEST_BLANK_STRING , EMPTY_STRING ));
9898
99- values = split (SPACE + SPACE + SPACE , SPACE );
100- assertArrayEquals (EMPTY_STRING_ARRAY , values );
99+ assertArrayEquals (ofArray (EMPTY_STRING , EMPTY_STRING ), assertSplit (TEST_BLANK_STRING , SPACE ));
101100
102- values = split (TEST_CSV_STRING , COMMA_CHAR );
103- assertArrayEquals (ofArray ("a" , "b" , "c" ), values );
101+ assertArrayEquals (ofArray (EMPTY_STRING , EMPTY_STRING , EMPTY_STRING ), assertSplit (SPACE + SPACE , SPACE ));
102+
103+ assertArrayEquals (ofArray (EMPTY_STRING , EMPTY_STRING , EMPTY_STRING , EMPTY_STRING ), assertSplit (SPACE + SPACE + SPACE , SPACE ));
104+
105+ assertArrayEquals (ofArray ("a" , "b" , "c" ), assertSplit (TEST_CSV_STRING , COMMA ));
106+
107+ assertArrayEquals (ofArray (TEST_CSV_STRING ), assertSplit (TEST_CSV_STRING , SPACE ));
108+
109+ assertArrayEquals (ofArray ("a" , "" , "b" , "c" ), assertSplit ("a, , b, c" , ", " ));
110+ }
111+
112+ String [] assertSplit (String str , String delimiter ) {
113+ String [] values = split (str , delimiter );
114+ if (length (delimiter ) == 1 ) {
115+ assertArrayEquals (split (str , delimiter .charAt (0 )), values );
116+ }
117+ String [] valuesFromString = delimitedListToStringArray (str , delimiter );
118+ assertArrayEquals (valuesFromString , values );
119+ return values ;
104120 }
105121
106122 @ Test
@@ -161,6 +177,10 @@ void testEndsWith() {
161177 @ Test
162178 void testReplace () {
163179 assertNull (replace (null , null , null ));
180+ assertEquals (TEST_EMPTY_STRING , replace (TEST_EMPTY_STRING , null , null ));
181+ assertEquals (TEST_EMPTY_STRING , replace (TEST_EMPTY_STRING , "null" , null ));
182+ assertEquals (TEST_CSV_STRING , replace (TEST_CSV_STRING , "null" , "null" ));
183+
164184 assertEquals (TEST_EMPTY_STRING , replace (TEST_EMPTY_STRING , null , null ));
165185 assertEquals (TEST_EMPTY_STRING , replace (TEST_EMPTY_STRING , TEST_EMPTY_STRING , null ));
166186 assertEquals (TEST_EMPTY_STRING , replace (TEST_EMPTY_STRING , TEST_EMPTY_STRING , TEST_EMPTY_STRING , 0 ));
@@ -173,6 +193,9 @@ void testReplace() {
173193 assertEquals ("a|b|c" , replace (TEST_CSV_STRING , COMMA , VERTICAL_BAR ));
174194 assertEquals ("a|b|c" , replace (TEST_CSV_STRING , COMMA , VERTICAL_BAR , 100 ));
175195 assertEquals ("a|b,c" , replace (TEST_CSV_STRING , COMMA , VERTICAL_BAR , 1 ));
196+
197+ assertEquals ("abc" , replace (TEST_CSV_STRING , COMMA , EMPTY_STRING ));
198+
176199 }
177200
178201 @ Test
@@ -227,6 +250,7 @@ void testSubstringBeforeLast() {
227250 assertSame (TEST_EMPTY_STRING , substringBeforeLast (TEST_EMPTY_STRING , null ));
228251 assertSame (TEST_CSV_STRING , substringBeforeLast (TEST_CSV_STRING , null ));
229252 assertSame (TEST_CSV_STRING , substringBeforeLast (TEST_CSV_STRING , TEST_EMPTY_STRING ));
253+ assertSame (TEST_CSV_STRING , substringBeforeLast (TEST_CSV_STRING , SHARP ));
230254
231255 assertEquals ("a,b" , substringBeforeLast (TEST_CSV_STRING , COMMA ));
232256 assertEquals ("a," , substringBeforeLast (TEST_CSV_STRING , "b" ));
@@ -246,6 +270,7 @@ void testSubstringAfterLast() {
246270 assertEquals (",c" , substringAfterLast (TEST_CSV_STRING , "b" ));
247271 assertEquals ("c" , substringAfterLast (TEST_CSV_STRING , COMMA ));
248272 assertEquals (TEST_EMPTY_STRING , substringAfterLast (TEST_CSV_STRING , "c" ));
273+ assertEquals (TEST_EMPTY_STRING , substringAfterLast (TEST_CSV_STRING , SHARP ));
249274 }
250275
251276 @ Test
@@ -326,4 +351,12 @@ void testUncapitalize() {
326351 assertSame ("hello world" , uncapitalize ("hello world" ));
327352 }
328353
354+ @ Test
355+ void testToStringArray () {
356+ assertSame (EMPTY_STRING_ARRAY , toStringArray (null ));
357+ assertSame (EMPTY_STRING_ARRAY , toStringArray (emptyList ()));
358+ assertSame (EMPTY_STRING_ARRAY , toStringArray (newLinkedList ()));
359+ assertArrayEquals (ofArray ("a" , "b" , "c" ), toStringArray (ofList ("a" , "b" , "c" )));
360+ }
361+
329362}
0 commit comments