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,39 @@ 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+ int length = length (delimiter );
115+ if (length == 1 ) {
116+ assertArrayEquals (split (str , delimiter .charAt (0 )), values );
117+ } else if (length > 1 ) {
118+ String [] valuesFromString = delimitedListToStringArray (str , delimiter );
119+ assertArrayEquals (valuesFromString , values );
120+ }
121+ return values ;
104122 }
105123
106124 @ Test
@@ -161,7 +179,10 @@ void testEndsWith() {
161179 @ Test
162180 void testReplace () {
163181 assertNull (replace (null , null , null ));
182+
164183 assertEquals (TEST_EMPTY_STRING , replace (TEST_EMPTY_STRING , null , null ));
184+ assertEquals (TEST_EMPTY_STRING , replace (TEST_EMPTY_STRING , "null" , null ));
185+ assertEquals (TEST_CSV_STRING , replace (TEST_CSV_STRING , "null" , "null" ));
165186 assertEquals (TEST_EMPTY_STRING , replace (TEST_EMPTY_STRING , TEST_EMPTY_STRING , null ));
166187 assertEquals (TEST_EMPTY_STRING , replace (TEST_EMPTY_STRING , TEST_EMPTY_STRING , TEST_EMPTY_STRING , 0 ));
167188
@@ -173,6 +194,9 @@ void testReplace() {
173194 assertEquals ("a|b|c" , replace (TEST_CSV_STRING , COMMA , VERTICAL_BAR ));
174195 assertEquals ("a|b|c" , replace (TEST_CSV_STRING , COMMA , VERTICAL_BAR , 100 ));
175196 assertEquals ("a|b,c" , replace (TEST_CSV_STRING , COMMA , VERTICAL_BAR , 1 ));
197+
198+ assertEquals ("abc" , replace (TEST_CSV_STRING , COMMA , EMPTY_STRING ));
199+
176200 }
177201
178202 @ Test
@@ -227,6 +251,7 @@ void testSubstringBeforeLast() {
227251 assertSame (TEST_EMPTY_STRING , substringBeforeLast (TEST_EMPTY_STRING , null ));
228252 assertSame (TEST_CSV_STRING , substringBeforeLast (TEST_CSV_STRING , null ));
229253 assertSame (TEST_CSV_STRING , substringBeforeLast (TEST_CSV_STRING , TEST_EMPTY_STRING ));
254+ assertSame (TEST_CSV_STRING , substringBeforeLast (TEST_CSV_STRING , SHARP ));
230255
231256 assertEquals ("a,b" , substringBeforeLast (TEST_CSV_STRING , COMMA ));
232257 assertEquals ("a," , substringBeforeLast (TEST_CSV_STRING , "b" ));
@@ -246,6 +271,7 @@ void testSubstringAfterLast() {
246271 assertEquals (",c" , substringAfterLast (TEST_CSV_STRING , "b" ));
247272 assertEquals ("c" , substringAfterLast (TEST_CSV_STRING , COMMA ));
248273 assertEquals (TEST_EMPTY_STRING , substringAfterLast (TEST_CSV_STRING , "c" ));
274+ assertEquals (TEST_EMPTY_STRING , substringAfterLast (TEST_CSV_STRING , SHARP ));
249275 }
250276
251277 @ Test
@@ -326,4 +352,12 @@ void testUncapitalize() {
326352 assertSame ("hello world" , uncapitalize ("hello world" ));
327353 }
328354
355+ @ Test
356+ void testToStringArray () {
357+ assertSame (EMPTY_STRING_ARRAY , toStringArray (null ));
358+ assertSame (EMPTY_STRING_ARRAY , toStringArray (emptyList ()));
359+ assertSame (EMPTY_STRING_ARRAY , toStringArray (newLinkedList ()));
360+ assertArrayEquals (ofArray ("a" , "b" , "c" ), toStringArray (ofList ("a" , "b" , "c" )));
361+ }
362+
329363}
0 commit comments