1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3;
18
19 import static org.hamcrest.MatcherAssert.assertThat;
20 import static org.hamcrest.Matchers.lessThanOrEqualTo;
21 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertNotEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertSame;
28 import static org.junit.jupiter.api.Assertions.assertThrows;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30
31 import java.io.UnsupportedEncodingException;
32 import java.lang.reflect.Constructor;
33 import java.lang.reflect.Method;
34 import java.lang.reflect.Modifier;
35 import java.nio.CharBuffer;
36 import java.nio.charset.Charset;
37 import java.nio.charset.StandardCharsets;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.Locale;
43 import java.util.Objects;
44 import java.util.function.Supplier;
45 import java.util.regex.PatternSyntaxException;
46
47 import org.apache.commons.lang3.function.Suppliers;
48 import org.apache.commons.lang3.mutable.MutableInt;
49 import org.apache.commons.lang3.text.WordUtils;
50 import org.junit.jupiter.api.Disabled;
51 import org.junit.jupiter.api.Test;
52 import org.junit.jupiter.params.ParameterizedTest;
53 import org.junit.jupiter.params.provider.ValueSource;
54
55
56
57
58
59 @SuppressWarnings("deprecation")
60 public class StringUtilsTest extends AbstractLangTest {
61
62 static final String WHITESPACE;
63 static final String NON_WHITESPACE;
64 static final String HARD_SPACE;
65 static final String TRIMMABLE;
66 static final String NON_TRIMMABLE;
67
68 static {
69 final StringBuilder ws = new StringBuilder();
70 final StringBuilder nws = new StringBuilder();
71 final String hs = String.valueOf((char) 160);
72 final StringBuilder tr = new StringBuilder();
73 final StringBuilder ntr = new StringBuilder();
74 for (int i = 0; i < Character.MAX_VALUE; i++) {
75 if (Character.isWhitespace((char) i)) {
76 ws.append(String.valueOf((char) i));
77 if (i > 32) {
78 ntr.append(String.valueOf((char) i));
79 }
80 } else if (i < 40) {
81 nws.append(String.valueOf((char) i));
82 }
83 }
84 for (int i = 0; i <= 32; i++) {
85 tr.append(String.valueOf((char) i));
86 }
87 WHITESPACE = ws.toString();
88 NON_WHITESPACE = nws.toString();
89 HARD_SPACE = hs;
90 TRIMMABLE = tr.toString();
91 NON_TRIMMABLE = ntr.toString();
92 }
93
94 private static final String[] ARRAY_LIST = {"foo", "bar", "baz"};
95 private static final String[] EMPTY_ARRAY_LIST = {};
96 private static final String[] NULL_ARRAY_LIST = {null};
97 private static final Object[] NULL_TO_STRING_LIST = {
98 new Object() {
99 @Override
100 public String toString() {
101 return null;
102 }
103 }
104 };
105 private static final String[] MIXED_ARRAY_LIST = {null, "", "foo"};
106 private static final Object[] MIXED_TYPE_LIST = {"foo", Long.valueOf(2L)};
107 private static final long[] LONG_PRIM_LIST = {1, 2};
108 private static final int[] INT_PRIM_LIST = {1, 2};
109 private static final byte[] BYTE_PRIM_LIST = {1, 2};
110 private static final short[] SHORT_PRIM_LIST = {1, 2};
111 private static final char[] CHAR_PRIM_LIST = {'1', '2'};
112 private static final float[] FLOAT_PRIM_LIST = {1, 2};
113 private static final double[] DOUBLE_PRIM_LIST = {1, 2};
114 private static final List<String> MIXED_STRING_LIST = Arrays.asList(null, "", "foo");
115 private static final List<Object> MIXED_TYPE_OBJECT_LIST = Arrays.<Object>asList("foo", Long.valueOf(2L));
116 private static final List<String> STRING_LIST = Arrays.asList("foo", "bar", "baz");
117 private static final List<String> EMPTY_STRING_LIST = Collections.emptyList();
118 private static final List<String> NULL_STRING_LIST = Collections.singletonList(null);
119
120 private static final String SEPARATOR = ",";
121 private static final char SEPARATOR_CHAR = ';';
122 private static final char COMMA_SEPARATOR_CHAR = ',';
123
124 private static final String TEXT_LIST = "foo,bar,baz";
125 private static final String TEXT_LIST_CHAR = "foo;bar;baz";
126 private static final String TEXT_LIST_NOSEP = "foobarbaz";
127
128 private static final String FOO_UNCAP = "foo";
129 private static final String FOO_CAP = "Foo";
130
131 private static final String SENTENCE_UNCAP = "foo bar baz";
132 private static final String SENTENCE_CAP = "Foo Bar Baz";
133
134 private static final boolean[] EMPTY = {};
135 private static final boolean[] ARRAY_FALSE_FALSE = {false, false};
136 private static final boolean[] ARRAY_FALSE_TRUE = {false, true};
137 private static final boolean[] ARRAY_FALSE_TRUE_FALSE = {false, true, false};
138
139 private void assertAbbreviateWithAbbrevMarkerAndOffset(final String expected, final String abbrevMarker, final int offset, final int maxWidth) {
140 final String abcdefghijklmno = "abcdefghijklmno";
141 final String message = "abbreviate(String,String,int,int) failed";
142 final String actual = StringUtils.abbreviate(abcdefghijklmno, abbrevMarker, offset, maxWidth);
143 if (offset >= 0 && offset < abcdefghijklmno.length()) {
144 assertTrue(actual.indexOf((char) ('a' + offset)) != -1,
145 message + " -- should contain offset character");
146 }
147 assertThat(message + " -- should not be greater than maxWidth", actual.length(), lessThanOrEqualTo(maxWidth));
148 assertEquals(expected, actual, message);
149 }
150
151 private void assertAbbreviateWithOffset(final String expected, final int offset, final int maxWidth) {
152 final String abcdefghijklmno = "abcdefghijklmno";
153 final String message = "abbreviate(String,int,int) failed";
154 final String actual = StringUtils.abbreviate(abcdefghijklmno, offset, maxWidth);
155 if (offset >= 0 && offset < abcdefghijklmno.length()) {
156 assertTrue(actual.indexOf((char) ('a' + offset)) != -1,
157 message + " -- should contain offset character");
158 }
159 assertThat(message + " -- should not be greater than maxWidth", actual.length(), lessThanOrEqualTo(maxWidth));
160 assertEquals(expected, actual, message);
161 }
162
163 private void innerTestSplit(final char separator, final String sepStr, final char noMatch) {
164 final String msg = "Failed on separator hex(" + Integer.toHexString(separator) +
165 "), noMatch hex(" + Integer.toHexString(noMatch) + "), sepStr(" + sepStr + ")";
166
167 final String str = "a" + separator + "b" + separator + separator + noMatch + "c";
168 String[] res;
169
170 res = StringUtils.split(str, sepStr);
171 assertEquals(3, res.length, msg);
172 assertEquals("a", res[0]);
173 assertEquals("b", res[1]);
174 assertEquals(noMatch + "c", res[2]);
175
176 final String str2 = separator + "a" + separator;
177 res = StringUtils.split(str2, sepStr);
178 assertEquals(1, res.length, msg);
179 assertEquals("a", res[0], msg);
180
181 res = StringUtils.split(str, sepStr, -1);
182 assertEquals(3, res.length, msg);
183 assertEquals("a", res[0], msg);
184 assertEquals("b", res[1], msg);
185 assertEquals(noMatch + "c", res[2], msg);
186
187 res = StringUtils.split(str, sepStr, 0);
188 assertEquals(3, res.length, msg);
189 assertEquals("a", res[0], msg);
190 assertEquals("b", res[1], msg);
191 assertEquals(noMatch + "c", res[2], msg);
192
193 res = StringUtils.split(str, sepStr, 1);
194 assertEquals(1, res.length, msg);
195 assertEquals(str, res[0], msg);
196
197 res = StringUtils.split(str, sepStr, 2);
198 assertEquals(2, res.length, msg);
199 assertEquals("a", res[0], msg);
200 assertEquals(str.substring(2), res[1], msg);
201 }
202
203 private void innerTestSplitPreserveAllTokens(final char separator, final String sepStr, final char noMatch) {
204 final String msg = "Failed on separator hex(" + Integer.toHexString(separator) +
205 "), noMatch hex(" + Integer.toHexString(noMatch) + "), sepStr(" + sepStr + ")";
206
207 final String str = "a" + separator + "b" + separator + separator + noMatch + "c";
208 String[] res;
209
210 res = StringUtils.splitPreserveAllTokens(str, sepStr);
211 assertEquals(4, res.length, msg);
212 assertEquals("a", res[0], msg);
213 assertEquals("b", res[1], msg);
214 assertEquals("", res[2], msg);
215 assertEquals(noMatch + "c", res[3], msg);
216
217 final String str2 = separator + "a" + separator;
218 res = StringUtils.splitPreserveAllTokens(str2, sepStr);
219 assertEquals(3, res.length, msg);
220 assertEquals("", res[0], msg);
221 assertEquals("a", res[1], msg);
222 assertEquals("", res[2], msg);
223
224 res = StringUtils.splitPreserveAllTokens(str, sepStr, -1);
225 assertEquals(4, res.length, msg);
226 assertEquals("a", res[0], msg);
227 assertEquals("b", res[1], msg);
228 assertEquals("", res[2], msg);
229 assertEquals(noMatch + "c", res[3], msg);
230
231 res = StringUtils.splitPreserveAllTokens(str, sepStr, 0);
232 assertEquals(4, res.length, msg);
233 assertEquals("a", res[0], msg);
234 assertEquals("b", res[1], msg);
235 assertEquals("", res[2], msg);
236 assertEquals(noMatch + "c", res[3], msg);
237
238 res = StringUtils.splitPreserveAllTokens(str, sepStr, 1);
239 assertEquals(1, res.length, msg);
240 assertEquals(str, res[0], msg);
241
242 res = StringUtils.splitPreserveAllTokens(str, sepStr, 2);
243 assertEquals(2, res.length, msg);
244 assertEquals("a", res[0], msg);
245 assertEquals(str.substring(2), res[1], msg);
246 }
247
248 @Test
249 public void testAbbreviate_StringInt() {
250 assertNull(StringUtils.abbreviate(null, 10));
251 assertEquals("", StringUtils.abbreviate("", 10));
252 assertEquals("short", StringUtils.abbreviate("short", 10));
253 assertEquals("Now is ...", StringUtils.abbreviate("Now is the time for all good men to come to the aid of their party.", 10));
254
255 final String raspberry = "raspberry peach";
256 assertEquals("raspberry p...", StringUtils.abbreviate(raspberry, 14));
257 assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 15));
258 assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 16));
259 assertEquals("abc...", StringUtils.abbreviate("abcdefg", 6));
260 assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 7));
261 assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 8));
262 assertEquals("a...", StringUtils.abbreviate("abcdefg", 4));
263 assertEquals("", StringUtils.abbreviate("", 4));
264
265 assertThrows(
266 IllegalArgumentException.class,
267 () -> StringUtils.abbreviate("abc", 3),
268 "StringUtils.abbreviate expecting IllegalArgumentException");
269 }
270
271 @Test
272 public void testAbbreviate_StringIntInt() {
273 assertNull(StringUtils.abbreviate(null, 10, 12));
274 assertEquals("", StringUtils.abbreviate("", 0, 10));
275 assertEquals("", StringUtils.abbreviate("", 2, 10));
276
277 assertThrows(
278 IllegalArgumentException.class,
279 () -> StringUtils.abbreviate("abcdefghij", 0, 3),
280 "StringUtils.abbreviate expecting IllegalArgumentException");
281 assertThrows(
282 IllegalArgumentException.class,
283 () -> StringUtils.abbreviate("abcdefghij", 5, 6),
284 "StringUtils.abbreviate expecting IllegalArgumentException");
285
286 final String raspberry = "raspberry peach";
287 assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, 11, 15));
288
289 assertNull(StringUtils.abbreviate(null, 7, 14));
290 assertAbbreviateWithOffset("abcdefg...", -1, 10);
291 assertAbbreviateWithOffset("abcdefg...", 0, 10);
292 assertAbbreviateWithOffset("abcdefg...", 1, 10);
293 assertAbbreviateWithOffset("abcdefg...", 2, 10);
294 assertAbbreviateWithOffset("abcdefg...", 3, 10);
295 assertAbbreviateWithOffset("abcdefg...", 4, 10);
296 assertAbbreviateWithOffset("...fghi...", 5, 10);
297 assertAbbreviateWithOffset("...ghij...", 6, 10);
298 assertAbbreviateWithOffset("...hijk...", 7, 10);
299 assertAbbreviateWithOffset("...ijklmno", 8, 10);
300 assertAbbreviateWithOffset("...ijklmno", 9, 10);
301 assertAbbreviateWithOffset("...ijklmno", 10, 10);
302 assertAbbreviateWithOffset("...ijklmno", 11, 10);
303 assertAbbreviateWithOffset("...ijklmno", 12, 10);
304 assertAbbreviateWithOffset("...ijklmno", 13, 10);
305 assertAbbreviateWithOffset("...ijklmno", 14, 10);
306 assertAbbreviateWithOffset("...ijklmno", 15, 10);
307 assertAbbreviateWithOffset("...ijklmno", 16, 10);
308 assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
309 }
310
311 @Test
312 public void testAbbreviate_StringStringInt() {
313 assertNull(StringUtils.abbreviate(null, null, 10));
314 assertNull(StringUtils.abbreviate(null, "...", 10));
315 assertEquals("paranaguacu", StringUtils.abbreviate("paranaguacu", null, 10));
316 assertEquals("", StringUtils.abbreviate("", "...", 2));
317 assertEquals("wai**", StringUtils.abbreviate("waiheke", "**", 5));
318 assertEquals("And af,,,,", StringUtils.abbreviate("And after a long time, he finally met his son.", ",,,,", 10));
319
320 final String raspberry = "raspberry peach";
321 assertEquals("raspberry pe..", StringUtils.abbreviate(raspberry, "..", 14));
322 assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", "---*---", 15));
323 assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", ".", 16));
324 assertEquals("abc()(", StringUtils.abbreviate("abcdefg", "()(", 6));
325 assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", ";", 7));
326 assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "_-", 8));
327 assertEquals("abc.", StringUtils.abbreviate("abcdefg", ".", 4));
328 assertEquals("", StringUtils.abbreviate("", 4));
329
330 assertThrows(
331 IllegalArgumentException.class,
332 () -> StringUtils.abbreviate("abcdefghij", "...", 3),
333 "StringUtils.abbreviate expecting IllegalArgumentException");
334 }
335
336 @Test
337 public void testAbbreviate_StringStringIntInt() {
338 assertNull(StringUtils.abbreviate(null, null, 10, 12));
339 assertNull(StringUtils.abbreviate(null, "...", 10, 12));
340 assertEquals("", StringUtils.abbreviate("", null, 0, 10));
341 assertEquals("", StringUtils.abbreviate("", "...", 2, 10));
342
343 assertThrows(
344 IllegalArgumentException.class,
345 () -> StringUtils.abbreviate("abcdefghij", "::", 0, 2),
346 "StringUtils.abbreviate expecting IllegalArgumentException");
347 assertThrows(
348 IllegalArgumentException.class,
349 () -> StringUtils.abbreviate("abcdefghij", "!!!", 5, 6),
350 "StringUtils.abbreviate expecting IllegalArgumentException");
351
352 final String raspberry = "raspberry peach";
353 assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, "--", 12, 15));
354
355 assertNull(StringUtils.abbreviate(null, ";", 7, 14));
356 assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh;;", ";;", -1, 10);
357 assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi.", ".", 0, 10);
358 assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh++", "++", 1, 10);
359 assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi*", "*", 2, 10);
360 assertAbbreviateWithAbbrevMarkerAndOffset("abcdef{{{{", "{{{{", 4, 10);
361 assertAbbreviateWithAbbrevMarkerAndOffset("abcdef____", "____", 5, 10);
362 assertAbbreviateWithAbbrevMarkerAndOffset("==fghijk==", "==", 5, 10);
363 assertAbbreviateWithAbbrevMarkerAndOffset("___ghij___", "___", 6, 10);
364 assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 7, 10);
365 assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 8, 10);
366 assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 9, 10);
367 assertAbbreviateWithAbbrevMarkerAndOffset("///ijklmno", "///", 10, 10);
368 assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 10, 10);
369 assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 11, 10);
370 assertAbbreviateWithAbbrevMarkerAndOffset("...ijklmno", "...", 12, 10);
371 assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 13, 10);
372 assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 14, 10);
373 assertAbbreviateWithAbbrevMarkerAndOffset("999ijklmno", "999", 15, 10);
374 assertAbbreviateWithAbbrevMarkerAndOffset("_ghijklmno", "_", 16, 10);
375 assertAbbreviateWithAbbrevMarkerAndOffset("+ghijklmno", "+", Integer.MAX_VALUE, 10);
376 }
377
378
379 @Test
380 public void testAbbreviateMarkerWithEmptyString() {
381 final String greaterThanMaxTest = "much too long text";
382 assertEquals("much too long", StringUtils.abbreviate(greaterThanMaxTest, "", 13));
383 }
384
385 @Test
386 public void testAbbreviateMiddle() {
387
388 assertNull(StringUtils.abbreviateMiddle(null, null, 0));
389 assertEquals("abc", StringUtils.abbreviateMiddle("abc", null, 0));
390 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 0));
391 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 3));
392 assertEquals("ab.f", StringUtils.abbreviateMiddle("abcdef", ".", 4));
393
394
395 assertEquals(
396 "A very long text with un...f the text is complete.",
397 StringUtils.abbreviateMiddle(
398 "A very long text with unimportant stuff in the middle but interesting start and " +
399 "end to see if the text is complete.", "...", 50));
400
401
402 final String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text";
403 assertEquals(
404 "Start text->Close text",
405 StringUtils.abbreviateMiddle(longText, "->", 22));
406
407
408 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", -1));
409
410
411
412 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 1));
413 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 2));
414
415
416 assertEquals("a", StringUtils.abbreviateMiddle("a", ".", 1));
417
418
419 assertEquals("a.d", StringUtils.abbreviateMiddle("abcd", ".", 3));
420
421
422 assertEquals("a..f", StringUtils.abbreviateMiddle("abcdef", "..", 4));
423 assertEquals("ab.ef", StringUtils.abbreviateMiddle("abcdef", ".", 5));
424 }
425
426
427
428
429 @Test
430 public void testAppendIfMissing() {
431 assertNull(StringUtils.appendIfMissing(null, null), "appendIfMissing(null,null)");
432 assertEquals("abc", StringUtils.appendIfMissing("abc", null), "appendIfMissing(abc,null)");
433 assertEquals("xyz", StringUtils.appendIfMissing("", "xyz"), "appendIfMissing(\"\",xyz)");
434 assertEquals("abcxyz", StringUtils.appendIfMissing("abc", "xyz"), "appendIfMissing(abc,xyz)");
435 assertEquals("abcxyz", StringUtils.appendIfMissing("abcxyz", "xyz"), "appendIfMissing(abcxyz,xyz)");
436 assertEquals("aXYZxyz", StringUtils.appendIfMissing("aXYZ", "xyz"), "appendIfMissing(aXYZ,xyz)");
437
438 assertNull(StringUtils.appendIfMissing(null, null, (CharSequence[]) null), "appendIfMissing(null,null,null)");
439 assertEquals("abc", StringUtils.appendIfMissing("abc", null, (CharSequence[]) null), "appendIfMissing(abc,null,null)");
440 assertEquals("xyz", StringUtils.appendIfMissing("", "xyz", (CharSequence[]) null), "appendIfMissing(\"\",xyz,null))");
441 assertEquals("abcxyz", StringUtils.appendIfMissing("abc", "xyz", null), "appendIfMissing(abc,xyz,{null})");
442 assertEquals("abc", StringUtils.appendIfMissing("abc", "xyz", ""), "appendIfMissing(abc,xyz,\"\")");
443 assertEquals("abcxyz", StringUtils.appendIfMissing("abc", "xyz", "mno"), "appendIfMissing(abc,xyz,mno)");
444 assertEquals("abcxyz", StringUtils.appendIfMissing("abcxyz", "xyz", "mno"), "appendIfMissing(abcxyz,xyz,mno)");
445 assertEquals("abcmno", StringUtils.appendIfMissing("abcmno", "xyz", "mno"), "appendIfMissing(abcmno,xyz,mno)");
446 assertEquals("abcXYZxyz", StringUtils.appendIfMissing("abcXYZ", "xyz", "mno"), "appendIfMissing(abcXYZ,xyz,mno)");
447 assertEquals("abcMNOxyz", StringUtils.appendIfMissing("abcMNO", "xyz", "mno"), "appendIfMissing(abcMNO,xyz,mno)");
448 }
449
450
451
452
453 @Test
454 public void testAppendIfMissingIgnoreCase() {
455 assertNull(StringUtils.appendIfMissingIgnoreCase(null, null), "appendIfMissingIgnoreCase(null,null)");
456 assertEquals("abc", StringUtils.appendIfMissingIgnoreCase("abc", null), "appendIfMissingIgnoreCase(abc,null)");
457 assertEquals("xyz", StringUtils.appendIfMissingIgnoreCase("", "xyz"), "appendIfMissingIgnoreCase(\"\",xyz)");
458 assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abc", "xyz"), "appendIfMissingIgnoreCase(abc,xyz)");
459 assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abcxyz", "xyz"), "appendIfMissingIgnoreCase(abcxyz,xyz)");
460 assertEquals("abcXYZ", StringUtils.appendIfMissingIgnoreCase("abcXYZ", "xyz"), "appendIfMissingIgnoreCase(abcXYZ,xyz)");
461
462 assertNull(StringUtils.appendIfMissingIgnoreCase(null, null, (CharSequence[]) null), "appendIfMissingIgnoreCase(null,null,null)");
463 assertEquals("abc", StringUtils.appendIfMissingIgnoreCase("abc", null, (CharSequence[]) null), "appendIfMissingIgnoreCase(abc,null,null)");
464 assertEquals("xyz", StringUtils.appendIfMissingIgnoreCase("", "xyz", (CharSequence[]) null), "appendIfMissingIgnoreCase(\"\",xyz,null)");
465 assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abc", "xyz", null), "appendIfMissingIgnoreCase(abc,xyz,{null})");
466 assertEquals("abc", StringUtils.appendIfMissingIgnoreCase("abc", "xyz", ""), "appendIfMissingIgnoreCase(abc,xyz,\"\")");
467 assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abc", "xyz", "mno"), "appendIfMissingIgnoreCase(abc,xyz,mno)");
468 assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abcxyz", "xyz", "mno"), "appendIfMissingIgnoreCase(abcxyz,xyz,mno)");
469 assertEquals("abcmno", StringUtils.appendIfMissingIgnoreCase("abcmno", "xyz", "mno"), "appendIfMissingIgnoreCase(abcmno,xyz,mno)");
470 assertEquals("abcXYZ", StringUtils.appendIfMissingIgnoreCase("abcXYZ", "xyz", "mno"), "appendIfMissingIgnoreCase(abcXYZ,xyz,mno)");
471 assertEquals("abcMNO", StringUtils.appendIfMissingIgnoreCase("abcMNO", "xyz", "mno"), "appendIfMissingIgnoreCase(abcMNO,xyz,mno)");
472 }
473
474 @Test
475 public void testCapitalize() {
476 assertNull(StringUtils.capitalize(null));
477
478 assertEquals("", StringUtils.capitalize(""), "capitalize(empty-string) failed");
479 assertEquals("X", StringUtils.capitalize("x"), "capitalize(single-char-string) failed");
480 assertEquals(FOO_CAP, StringUtils.capitalize(FOO_CAP), "capitalize(String) failed");
481 assertEquals(FOO_CAP, StringUtils.capitalize(FOO_UNCAP), "capitalize(string) failed");
482
483 assertEquals("\u01C8", StringUtils.capitalize("\u01C9"), "capitalize(String) is not using TitleCase");
484
485
486 assertNull(StringUtils.capitalize(null));
487 assertEquals("", StringUtils.capitalize(""));
488 assertEquals("Cat", StringUtils.capitalize("cat"));
489 assertEquals("CAt", StringUtils.capitalize("cAt"));
490 assertEquals("'cat'", StringUtils.capitalize("'cat'"));
491 }
492
493 @Test
494 public void testCenter_StringInt() {
495 assertNull(StringUtils.center(null, -1));
496 assertNull(StringUtils.center(null, 4));
497 assertEquals(" ", StringUtils.center("", 4));
498 assertEquals("ab", StringUtils.center("ab", 0));
499 assertEquals("ab", StringUtils.center("ab", -1));
500 assertEquals("ab", StringUtils.center("ab", 1));
501 assertEquals(" ", StringUtils.center("", 4));
502 assertEquals(" ab ", StringUtils.center("ab", 4));
503 assertEquals("abcd", StringUtils.center("abcd", 2));
504 assertEquals(" a ", StringUtils.center("a", 4));
505 assertEquals(" a ", StringUtils.center("a", 5));
506 }
507
508 @Test
509 public void testCenter_StringIntChar() {
510 assertNull(StringUtils.center(null, -1, ' '));
511 assertNull(StringUtils.center(null, 4, ' '));
512 assertEquals(" ", StringUtils.center("", 4, ' '));
513 assertEquals("ab", StringUtils.center("ab", 0, ' '));
514 assertEquals("ab", StringUtils.center("ab", -1, ' '));
515 assertEquals("ab", StringUtils.center("ab", 1, ' '));
516 assertEquals(" ", StringUtils.center("", 4, ' '));
517 assertEquals(" ab ", StringUtils.center("ab", 4, ' '));
518 assertEquals("abcd", StringUtils.center("abcd", 2, ' '));
519 assertEquals(" a ", StringUtils.center("a", 4, ' '));
520 assertEquals(" a ", StringUtils.center("a", 5, ' '));
521 assertEquals("xxaxx", StringUtils.center("a", 5, 'x'));
522 }
523
524 @Test
525 public void testCenter_StringIntString() {
526 assertNull(StringUtils.center(null, 4, null));
527 assertNull(StringUtils.center(null, -1, " "));
528 assertNull(StringUtils.center(null, 4, " "));
529 assertEquals(" ", StringUtils.center("", 4, " "));
530 assertEquals("ab", StringUtils.center("ab", 0, " "));
531 assertEquals("ab", StringUtils.center("ab", -1, " "));
532 assertEquals("ab", StringUtils.center("ab", 1, " "));
533 assertEquals(" ", StringUtils.center("", 4, " "));
534 assertEquals(" ab ", StringUtils.center("ab", 4, " "));
535 assertEquals("abcd", StringUtils.center("abcd", 2, " "));
536 assertEquals(" a ", StringUtils.center("a", 4, " "));
537 assertEquals("yayz", StringUtils.center("a", 4, "yz"));
538 assertEquals("yzyayzy", StringUtils.center("a", 7, "yz"));
539 assertEquals(" abc ", StringUtils.center("abc", 7, null));
540 assertEquals(" abc ", StringUtils.center("abc", 7, ""));
541 }
542
543 @Test
544 public void testChomp() {
545
546 final String[][] chompCases = {
547 {FOO_UNCAP + "\r\n", FOO_UNCAP},
548 {FOO_UNCAP + "\n", FOO_UNCAP},
549 {FOO_UNCAP + "\r", FOO_UNCAP},
550 {FOO_UNCAP + " \r", FOO_UNCAP + " "},
551 {FOO_UNCAP, FOO_UNCAP},
552 {FOO_UNCAP + "\n\n", FOO_UNCAP + "\n"},
553 {FOO_UNCAP + "\r\n\r\n", FOO_UNCAP + "\r\n"},
554 {"foo\nfoo", "foo\nfoo"},
555 {"foo\n\rfoo", "foo\n\rfoo"},
556 {"\n", ""},
557 {"\r", ""},
558 {"a", "a"},
559 {"\r\n", ""},
560 {"", ""},
561 {null, null},
562 {FOO_UNCAP + "\n\r", FOO_UNCAP + "\n"}
563 };
564 for (final String[] chompCase : chompCases) {
565 final String original = chompCase[0];
566 final String expectedResult = chompCase[1];
567 assertEquals(expectedResult, StringUtils.chomp(original), "chomp(String) failed");
568 }
569
570 assertEquals("foo", StringUtils.chomp("foobar", "bar"), "chomp(String, String) failed");
571 assertEquals("foobar", StringUtils.chomp("foobar", "baz"), "chomp(String, String) failed");
572 assertEquals("foo", StringUtils.chomp("foo", "foooo"), "chomp(String, String) failed");
573 assertEquals("foobar", StringUtils.chomp("foobar", ""), "chomp(String, String) failed");
574 assertEquals("foobar", StringUtils.chomp("foobar", null), "chomp(String, String) failed");
575 assertEquals("", StringUtils.chomp("", "foo"), "chomp(String, String) failed");
576 assertEquals("", StringUtils.chomp("", null), "chomp(String, String) failed");
577 assertEquals("", StringUtils.chomp("", ""), "chomp(String, String) failed");
578 assertNull(StringUtils.chomp(null, "foo"), "chomp(String, String) failed");
579 assertNull(StringUtils.chomp(null, null), "chomp(String, String) failed");
580 assertNull(StringUtils.chomp(null, ""), "chomp(String, String) failed");
581 assertEquals("", StringUtils.chomp("foo", "foo"), "chomp(String, String) failed");
582 assertEquals(" ", StringUtils.chomp(" foo", "foo"), "chomp(String, String) failed");
583 assertEquals("foo ", StringUtils.chomp("foo ", "foo"), "chomp(String, String) failed");
584 }
585
586 @Test
587 public void testChop() {
588
589 final String[][] chopCases = {
590 {FOO_UNCAP + "\r\n", FOO_UNCAP},
591 {FOO_UNCAP + "\n", FOO_UNCAP},
592 {FOO_UNCAP + "\r", FOO_UNCAP},
593 {FOO_UNCAP + " \r", FOO_UNCAP + " "},
594 {"foo", "fo"},
595 {"foo\nfoo", "foo\nfo"},
596 {"\n", ""},
597 {"\r", ""},
598 {"\r\n", ""},
599 {null, null},
600 {"", ""},
601 {"a", ""},
602 };
603 for (final String[] chopCase : chopCases) {
604 final String original = chopCase[0];
605 final String expectedResult = chopCase[1];
606 assertEquals(expectedResult, StringUtils.chop(original), "chop(String) failed");
607 }
608 }
609
610 @Test
611 public void testConstructor() {
612 assertNotNull(new StringUtils());
613 final Constructor<?>[] cons = StringUtils.class.getDeclaredConstructors();
614 assertEquals(1, cons.length);
615 assertTrue(Modifier.isPublic(cons[0].getModifiers()));
616 assertTrue(Modifier.isPublic(StringUtils.class.getModifiers()));
617 assertFalse(Modifier.isFinal(StringUtils.class.getModifiers()));
618 }
619
620 @Test
621 public void testDefault_String() {
622 assertEquals("", StringUtils.defaultString(null));
623 assertEquals("", StringUtils.defaultString(""));
624 assertEquals("abc", StringUtils.defaultString("abc"));
625 }
626
627 @Test
628 public void testDefault_StringString() {
629 assertEquals("NULL", StringUtils.defaultString(null, "NULL"));
630 assertEquals("", StringUtils.defaultString("", "NULL"));
631 assertEquals("abc", StringUtils.defaultString("abc", "NULL"));
632 }
633
634 @Test
635 public void testDefaultIfBlank_CharBuffers() {
636 assertEquals("NULL", StringUtils.defaultIfBlank(CharBuffer.wrap(""), CharBuffer.wrap("NULL")).toString());
637 assertEquals("NULL", StringUtils.defaultIfBlank(CharBuffer.wrap(" "), CharBuffer.wrap("NULL")).toString());
638 assertEquals("abc", StringUtils.defaultIfBlank(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL")).toString());
639 assertNull(StringUtils.defaultIfBlank(CharBuffer.wrap(""), (CharBuffer) null));
640
641 final CharBuffer s = StringUtils.defaultIfBlank(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL"));
642 assertEquals("abc", s.toString());
643 }
644
645 @Test
646 public void testDefaultIfBlank_StringBuffers() {
647 assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuffer(""), new StringBuffer("NULL")).toString());
648 assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuffer(" "), new StringBuffer("NULL")).toString());
649 assertEquals("abc", StringUtils.defaultIfBlank(new StringBuffer("abc"), new StringBuffer("NULL")).toString());
650 assertNull(StringUtils.defaultIfBlank(new StringBuffer(""), (StringBuffer) null));
651
652 final StringBuffer s = StringUtils.defaultIfBlank(new StringBuffer("abc"), new StringBuffer("NULL"));
653 assertEquals("abc", s.toString());
654 }
655
656 @Test
657 public void testDefaultIfBlank_StringBuilders() {
658 assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuilder(""), new StringBuilder("NULL")).toString());
659 assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuilder(" "), new StringBuilder("NULL")).toString());
660 assertEquals("abc", StringUtils.defaultIfBlank(new StringBuilder("abc"), new StringBuilder("NULL")).toString());
661 assertNull(StringUtils.defaultIfBlank(new StringBuilder(""), (StringBuilder) null));
662
663 final StringBuilder s = StringUtils.defaultIfBlank(new StringBuilder("abc"), new StringBuilder("NULL"));
664 assertEquals("abc", s.toString());
665 }
666
667 @Test
668 public void testDefaultIfBlank_StringString() {
669 assertEquals("NULL", StringUtils.defaultIfBlank(null, "NULL"));
670 assertEquals("NULL", StringUtils.defaultIfBlank("", "NULL"));
671 assertEquals("NULL", StringUtils.defaultIfBlank(" ", "NULL"));
672 assertEquals("abc", StringUtils.defaultIfBlank("abc", "NULL"));
673 assertNull(StringUtils.defaultIfBlank("", (String) null));
674
675 final String s = StringUtils.defaultIfBlank("abc", "NULL");
676 assertEquals("abc", s);
677 }
678
679
680 @Test
681 public void testDefaultIfEmpty_CharBuffers() {
682 assertEquals("NULL", StringUtils.defaultIfEmpty(CharBuffer.wrap(""), CharBuffer.wrap("NULL")).toString());
683 assertEquals("abc", StringUtils.defaultIfEmpty(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL")).toString());
684 assertNull(StringUtils.defaultIfEmpty(CharBuffer.wrap(""), (CharBuffer) null));
685
686 final CharBuffer s = StringUtils.defaultIfEmpty(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL"));
687 assertEquals("abc", s.toString());
688 }
689
690 @Test
691 public void testDefaultIfEmpty_StringBuffers() {
692 assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuffer(""), new StringBuffer("NULL")).toString());
693 assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuffer("abc"), new StringBuffer("NULL")).toString());
694 assertNull(StringUtils.defaultIfEmpty(new StringBuffer(""), (StringBuffer) null));
695
696 final StringBuffer s = StringUtils.defaultIfEmpty(new StringBuffer("abc"), new StringBuffer("NULL"));
697 assertEquals("abc", s.toString());
698 }
699
700
701 @Test
702 public void testDefaultIfEmpty_StringBuilders() {
703 assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuilder(""), new StringBuilder("NULL")).toString());
704 assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuilder("abc"), new StringBuilder("NULL")).toString());
705 assertNull(StringUtils.defaultIfEmpty(new StringBuilder(""), (StringBuilder) null));
706
707 final StringBuilder s = StringUtils.defaultIfEmpty(new StringBuilder("abc"), new StringBuilder("NULL"));
708 assertEquals("abc", s.toString());
709 }
710
711 @Test
712 public void testDefaultIfEmpty_StringString() {
713 assertEquals("NULL", StringUtils.defaultIfEmpty(null, "NULL"));
714 assertEquals("NULL", StringUtils.defaultIfEmpty("", "NULL"));
715 assertEquals("abc", StringUtils.defaultIfEmpty("abc", "NULL"));
716 assertNull(StringUtils.getIfEmpty("", null));
717
718 final String s = StringUtils.defaultIfEmpty("abc", "NULL");
719 assertEquals("abc", s);
720 }
721
722 @Test
723 public void testDeleteWhitespace_String() {
724 assertNull(StringUtils.deleteWhitespace(null));
725 assertEquals("", StringUtils.deleteWhitespace(""));
726 assertEquals("", StringUtils.deleteWhitespace(" \u000C \t\t\u001F\n\n \u000B "));
727 assertEquals("", StringUtils.deleteWhitespace(StringUtilsTest.WHITESPACE));
728 assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils.deleteWhitespace(StringUtilsTest.NON_WHITESPACE));
729
730
731 assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace(" \u00A0 \t\t\n\n \u202F "));
732 assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace("\u00A0\u202F"));
733 assertEquals("test", StringUtils.deleteWhitespace("\u000Bt \t\n\u0009e\rs\n\n \tt"));
734 }
735
736 @Test
737 public void testDifference_StringString() {
738 assertNull(StringUtils.difference(null, null));
739 assertEquals("", StringUtils.difference("", ""));
740 assertEquals("abc", StringUtils.difference("", "abc"));
741 assertEquals("", StringUtils.difference("abc", ""));
742 assertEquals("i am a robot", StringUtils.difference(null, "i am a robot"));
743 assertEquals("i am a machine", StringUtils.difference("i am a machine", null));
744 assertEquals("robot", StringUtils.difference("i am a machine", "i am a robot"));
745 assertEquals("", StringUtils.difference("abc", "abc"));
746 assertEquals("you are a robot", StringUtils.difference("i am a robot", "you are a robot"));
747 }
748
749
750 @Test
751 public void testDifferenceAt_StringArray() {
752 assertEquals(-1, StringUtils.indexOfDifference((String[]) null));
753 assertEquals(-1, StringUtils.indexOfDifference());
754 assertEquals(-1, StringUtils.indexOfDifference("abc"));
755 assertEquals(-1, StringUtils.indexOfDifference(null, null));
756 assertEquals(-1, StringUtils.indexOfDifference("", ""));
757 assertEquals(0, StringUtils.indexOfDifference("", null));
758 assertEquals(0, StringUtils.indexOfDifference("abc", null, null));
759 assertEquals(0, StringUtils.indexOfDifference(null, null, "abc"));
760 assertEquals(0, StringUtils.indexOfDifference("", "abc"));
761 assertEquals(0, StringUtils.indexOfDifference("abc", ""));
762 assertEquals(-1, StringUtils.indexOfDifference("abc", "abc"));
763 assertEquals(1, StringUtils.indexOfDifference("abc", "a"));
764 assertEquals(2, StringUtils.indexOfDifference("ab", "abxyz"));
765 assertEquals(2, StringUtils.indexOfDifference("abcde", "abxyz"));
766 assertEquals(0, StringUtils.indexOfDifference("abcde", "xyz"));
767 assertEquals(0, StringUtils.indexOfDifference("xyz", "abcde"));
768 assertEquals(7, StringUtils.indexOfDifference("i am a machine", "i am a robot"));
769 }
770
771 @Test
772 public void testDifferenceAt_StringString() {
773 assertEquals(-1, StringUtils.indexOfDifference(null, null));
774 assertEquals(0, StringUtils.indexOfDifference(null, "i am a robot"));
775 assertEquals(-1, StringUtils.indexOfDifference("", ""));
776 assertEquals(0, StringUtils.indexOfDifference("", "abc"));
777 assertEquals(0, StringUtils.indexOfDifference("abc", ""));
778 assertEquals(0, StringUtils.indexOfDifference("i am a machine", null));
779 assertEquals(7, StringUtils.indexOfDifference("i am a machine", "i am a robot"));
780 assertEquals(-1, StringUtils.indexOfDifference("foo", "foo"));
781 assertEquals(0, StringUtils.indexOfDifference("i am a robot", "you are a robot"));
782 }
783
784
785
786
787 @Test
788 public void testEMPTY() {
789 assertNotNull(StringUtils.EMPTY);
790 assertEquals("", StringUtils.EMPTY);
791 assertEquals(0, StringUtils.EMPTY.length());
792 }
793
794 @Test
795 public void testEscapeSurrogatePairs() {
796 assertEquals("\uD83D\uDE30", StringEscapeUtils.escapeCsv("\uD83D\uDE30"));
797
798 assertEquals("\uD800\uDC00", StringEscapeUtils.escapeCsv("\uD800\uDC00"));
799 assertEquals("\uD834\uDD1E", StringEscapeUtils.escapeCsv("\uD834\uDD1E"));
800 assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeCsv("\uDBFF\uDFFD"));
801 assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeHtml3("\uDBFF\uDFFD"));
802 assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeHtml4("\uDBFF\uDFFD"));
803 assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeXml("\uDBFF\uDFFD"));
804 }
805
806
807
808
809 @Test
810 public void testEscapeSurrogatePairsLang858() {
811 assertEquals("\\uDBFF\\uDFFD", StringEscapeUtils.escapeJava("\uDBFF\uDFFD"));
812 assertEquals("\\uDBFF\\uDFFD", StringEscapeUtils.escapeEcmaScript("\uDBFF\uDFFD"));
813 }
814
815 @Test
816 public void testGeorgianSample() {
817 final char[] arrayI = {
818
819 (char) 0x0131,
820
821 (char) 0x03F4
822 };
823 final char[] arrayJ = {
824
825 (char) 0x0130,
826
827 (char) 0x03D1
828 };
829 for (final char i : arrayI) {
830 for (final char j : arrayJ) {
831 final String si = String.valueOf(i);
832 final String sj = String.valueOf(j);
833 final boolean res1 = si.equalsIgnoreCase(sj);
834 final CharSequence ci = new StringBuilder(si);
835 final CharSequence cj = new StringBuilder(sj);
836 boolean res2 = StringUtils.startsWithIgnoreCase(ci, cj);
837 assertEquals(res1, res2, "si : " + si + " sj : " + sj);
838 res2 = StringUtils.endsWithIgnoreCase(ci, cj);
839 assertEquals(res1, res2, "si : " + si + " sj : " + sj);
840 res2 = StringUtils.compareIgnoreCase(ci.toString(), cj.toString()) == 0;
841 assertEquals(res1, res2, "si : " + si + " sj : " + sj);
842 res2 = StringUtils.indexOfIgnoreCase(ci.toString(), cj.toString()) == 0;
843 assertEquals(res1, res2, "si : " + si + " sj : " + sj);
844 res2 = StringUtils.lastIndexOfIgnoreCase(ci.toString(), cj.toString()) == 0;
845 assertEquals(res1, res2, "si : " + si + " sj : " + sj);
846 }
847 }
848 }
849
850 @Test
851 public void testGetBytes_Charset() {
852 assertEquals(ArrayUtils.EMPTY_BYTE_ARRAY, StringUtils.getBytes(null, (Charset) null));
853 assertArrayEquals(StringUtils.EMPTY.getBytes(), StringUtils.getBytes(StringUtils.EMPTY, (Charset) null));
854 assertArrayEquals(StringUtils.EMPTY.getBytes(StandardCharsets.US_ASCII),
855 StringUtils.getBytes(StringUtils.EMPTY, StandardCharsets.US_ASCII));
856 }
857
858 @Test
859 public void testGetBytes_String() throws UnsupportedEncodingException {
860 assertEquals(ArrayUtils.EMPTY_BYTE_ARRAY, StringUtils.getBytes(null, (String) null));
861 assertArrayEquals(StringUtils.EMPTY.getBytes(), StringUtils.getBytes(StringUtils.EMPTY, (String) null));
862 assertArrayEquals(StringUtils.EMPTY.getBytes(StandardCharsets.US_ASCII.name()),
863 StringUtils.getBytes(StringUtils.EMPTY, StandardCharsets.US_ASCII.name()));
864 }
865
866 @Test
867 public void testGetCommonPrefix_StringArray() {
868 assertEquals("", StringUtils.getCommonPrefix((String[]) null));
869 assertEquals("", StringUtils.getCommonPrefix());
870 assertEquals("abc", StringUtils.getCommonPrefix("abc"));
871 assertEquals("", StringUtils.getCommonPrefix(null, null));
872 assertEquals("", StringUtils.getCommonPrefix("", ""));
873 assertEquals("", StringUtils.getCommonPrefix("", null));
874 assertEquals("", StringUtils.getCommonPrefix("abc", null, null));
875 assertEquals("", StringUtils.getCommonPrefix(null, null, "abc"));
876 assertEquals("", StringUtils.getCommonPrefix("", "abc"));
877 assertEquals("", StringUtils.getCommonPrefix("abc", ""));
878 assertEquals("abc", StringUtils.getCommonPrefix("abc", "abc"));
879 assertEquals("a", StringUtils.getCommonPrefix("abc", "a"));
880 assertEquals("ab", StringUtils.getCommonPrefix("ab", "abxyz"));
881 assertEquals("ab", StringUtils.getCommonPrefix("abcde", "abxyz"));
882 assertEquals("", StringUtils.getCommonPrefix("abcde", "xyz"));
883 assertEquals("", StringUtils.getCommonPrefix("xyz", "abcde"));
884 assertEquals("i am a ", StringUtils.getCommonPrefix("i am a machine", "i am a robot"));
885 }
886
887 @Test
888 public void testGetDigits() {
889 assertNull(StringUtils.getDigits(null));
890 assertEquals("", StringUtils.getDigits(""));
891 assertEquals("", StringUtils.getDigits("abc"));
892 assertEquals("1000", StringUtils.getDigits("1000$"));
893 assertEquals("12345", StringUtils.getDigits("123password45"));
894 assertEquals("5417543010", StringUtils.getDigits("(541) 754-3010"));
895 assertEquals("\u0967\u0968\u0969", StringUtils.getDigits("\u0967\u0968\u0969"));
896 }
897
898 @Test
899 public void testGetFuzzyDistance() {
900 assertEquals(0, StringUtils.getFuzzyDistance("", "", Locale.ENGLISH));
901 assertEquals(0, StringUtils.getFuzzyDistance("Workshop", "b", Locale.ENGLISH));
902 assertEquals(1, StringUtils.getFuzzyDistance("Room", "o", Locale.ENGLISH));
903 assertEquals(1, StringUtils.getFuzzyDistance("Workshop", "w", Locale.ENGLISH));
904 assertEquals(2, StringUtils.getFuzzyDistance("Workshop", "ws", Locale.ENGLISH));
905 assertEquals(4, StringUtils.getFuzzyDistance("Workshop", "wo", Locale.ENGLISH));
906 assertEquals(3, StringUtils.getFuzzyDistance("Apache Software Foundation", "asf", Locale.ENGLISH));
907 }
908
909 @Test
910 public void testGetFuzzyDistance_NullNullNull() {
911 assertThrows(IllegalArgumentException.class, () -> StringUtils.getFuzzyDistance(null, null, null));
912 }
913
914 @Test
915 public void testGetFuzzyDistance_NullStringLocale() {
916 assertThrows(IllegalArgumentException.class, () -> StringUtils.getFuzzyDistance(null, "clear", Locale.ENGLISH));
917 }
918
919 @Test
920 public void testGetFuzzyDistance_StringNullLoclae() {
921 assertThrows(IllegalArgumentException.class, () -> StringUtils.getFuzzyDistance(" ", null, Locale.ENGLISH));
922 }
923
924 @Test
925 public void testGetFuzzyDistance_StringStringNull() {
926 assertThrows(IllegalArgumentException.class, () -> StringUtils.getFuzzyDistance(" ", "clear", null));
927 }
928
929 @Test
930 public void testGetIfBlank_StringStringSupplier() {
931 assertEquals("NULL", StringUtils.getIfBlank(null, () -> "NULL"));
932 assertEquals("NULL", StringUtils.getIfBlank("", () -> "NULL"));
933 assertEquals("NULL", StringUtils.getIfBlank(" ", () -> "NULL"));
934 assertEquals("abc", StringUtils.getIfBlank("abc", () -> "NULL"));
935 assertNull(StringUtils.getIfBlank("", Suppliers.nul()));
936 assertNull(StringUtils.defaultIfBlank("", (String) null));
937
938 final String s = StringUtils.getIfBlank("abc", () -> "NULL");
939 assertEquals("abc", s);
940
941 final MutableInt numberOfCalls = new MutableInt(0);
942 final Supplier<String> countingDefaultSupplier = () -> {
943 numberOfCalls.increment();
944 return "NULL";
945 };
946 StringUtils.getIfBlank("abc", countingDefaultSupplier);
947 assertEquals(0, numberOfCalls.getValue());
948 StringUtils.getIfBlank("", countingDefaultSupplier);
949 assertEquals(1, numberOfCalls.getValue());
950 StringUtils.getIfBlank(" ", countingDefaultSupplier);
951 assertEquals(2, numberOfCalls.getValue());
952 StringUtils.getIfBlank(null, countingDefaultSupplier);
953 assertEquals(3, numberOfCalls.getValue());
954 }
955
956 @Test
957 public void testGetIfEmpty_StringStringSupplier() {
958 assertEquals("NULL", StringUtils.getIfEmpty((String) null, () -> "NULL"));
959 assertEquals("NULL", StringUtils.getIfEmpty("", () -> "NULL"));
960 assertEquals("abc", StringUtils.getIfEmpty("abc", () -> "NULL"));
961 assertNull(StringUtils.getIfEmpty("", Suppliers.nul()));
962 assertNull(StringUtils.defaultIfEmpty("", (String) null));
963
964 final String s = StringUtils.getIfEmpty("abc", () -> "NULL");
965 assertEquals("abc", s);
966
967 final MutableInt numberOfCalls = new MutableInt(0);
968 final Supplier<String> countingDefaultSupplier = () -> {
969 numberOfCalls.increment();
970 return "NULL";
971 };
972 StringUtils.getIfEmpty("abc", countingDefaultSupplier);
973 assertEquals(0, numberOfCalls.getValue());
974 StringUtils.getIfEmpty("", countingDefaultSupplier);
975 assertEquals(1, numberOfCalls.getValue());
976 StringUtils.getIfEmpty(null, countingDefaultSupplier);
977 assertEquals(2, numberOfCalls.getValue());
978 }
979
980 @Test
981 public void testGetJaroWinklerDistance_NullNull() {
982 assertThrows(IllegalArgumentException.class, () -> StringUtils.getJaroWinklerDistance(null, null));
983 }
984
985 @Test
986 public void testGetJaroWinklerDistance_NullString() {
987 assertThrows(IllegalArgumentException.class, () -> StringUtils.getJaroWinklerDistance(null, "clear"));
988 }
989
990 @Test
991 public void testGetJaroWinklerDistance_StringNull() {
992 assertThrows(IllegalArgumentException.class, () -> StringUtils.getJaroWinklerDistance(" ", null));
993 }
994
995 @Test
996 public void testGetJaroWinklerDistance_StringString() {
997 assertEquals(0.93d, StringUtils.getJaroWinklerDistance("frog", "fog"));
998 assertEquals(0.0d, StringUtils.getJaroWinklerDistance("fly", "ant"));
999 assertEquals(0.44d, StringUtils.getJaroWinklerDistance("elephant", "hippo"));
1000 assertEquals(0.84d, StringUtils.getJaroWinklerDistance("dwayne", "duane"));
1001 assertEquals(0.93d, StringUtils.getJaroWinklerDistance("ABC Corporation", "ABC Corp"));
1002 assertEquals(0.95d, StringUtils.getJaroWinklerDistance("D N H Enterprises Inc", "D & H Enterprises, Inc."));
1003 assertEquals(0.92d, StringUtils.getJaroWinklerDistance("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
1004 assertEquals(0.88d, StringUtils.getJaroWinklerDistance("PENNSYLVANIA", "PENNCISYLVNIA"));
1005 assertEquals(0.63d, StringUtils.getJaroWinklerDistance("Haus Ingeborg", "Ingeborg Esser"));
1006 }
1007
1008 @Test
1009 public void testGetLevenshteinDistance_NullString() {
1010 assertThrows(IllegalArgumentException.class, () -> StringUtils.getLevenshteinDistance("a", null));
1011 }
1012
1013 @Test
1014 public void testGetLevenshteinDistance_NullStringInt() {
1015 assertThrows(IllegalArgumentException.class, () -> StringUtils.getLevenshteinDistance(null, "a", 0));
1016 }
1017
1018 @Test
1019 public void testGetLevenshteinDistance_StringNull() {
1020 assertThrows(IllegalArgumentException.class, () -> StringUtils.getLevenshteinDistance(null, "a"));
1021 }
1022
1023 @Test
1024 public void testGetLevenshteinDistance_StringNullInt() {
1025 assertThrows(IllegalArgumentException.class, () -> StringUtils.getLevenshteinDistance("a", null, 0));
1026 }
1027
1028 @Test
1029 public void testGetLevenshteinDistance_StringString() {
1030 assertEquals(0, StringUtils.getLevenshteinDistance("", ""));
1031 assertEquals(1, StringUtils.getLevenshteinDistance("", "a"));
1032 assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", ""));
1033 assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog"));
1034 assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant"));
1035 assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo"));
1036 assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant"));
1037 assertEquals(8, StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz"));
1038 assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz", "hippo"));
1039 assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo"));
1040 }
1041
1042 @Test
1043 public void testGetLevenshteinDistance_StringStringInt() {
1044
1045 assertEquals(0, StringUtils.getLevenshteinDistance("", "", 0));
1046 assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "", 8));
1047 assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "", 7));
1048 assertEquals(-1, StringUtils.getLevenshteinDistance("aaapppp", "", 6));
1049
1050
1051 assertEquals(-1, StringUtils.getLevenshteinDistance("b", "a", 0));
1052 assertEquals(-1, StringUtils.getLevenshteinDistance("a", "b", 0));
1053
1054
1055 assertEquals(0, StringUtils.getLevenshteinDistance("aa", "aa", 0));
1056 assertEquals(0, StringUtils.getLevenshteinDistance("aa", "aa", 2));
1057
1058
1059 assertEquals(-1, StringUtils.getLevenshteinDistance("aaa", "bbb", 2));
1060 assertEquals(3, StringUtils.getLevenshteinDistance("aaa", "bbb", 3));
1061
1062
1063 assertEquals(6, StringUtils.getLevenshteinDistance("aaaaaa", "b", 10));
1064
1065
1066 assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "b", 8));
1067 assertEquals(3, StringUtils.getLevenshteinDistance("a", "bbb", 4));
1068
1069
1070 assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "b", 7));
1071 assertEquals(3, StringUtils.getLevenshteinDistance("a", "bbb", 3));
1072
1073
1074 assertEquals(-1, StringUtils.getLevenshteinDistance("a", "bbb", 2));
1075 assertEquals(-1, StringUtils.getLevenshteinDistance("bbb", "a", 2));
1076 assertEquals(-1, StringUtils.getLevenshteinDistance("aaapppp", "b", 6));
1077
1078
1079 assertEquals(-1, StringUtils.getLevenshteinDistance("a", "bbb", 1));
1080 assertEquals(-1, StringUtils.getLevenshteinDistance("bbb", "a", 1));
1081
1082
1083 assertEquals(-1, StringUtils.getLevenshteinDistance("12345", "1234567", 1));
1084 assertEquals(-1, StringUtils.getLevenshteinDistance("1234567", "12345", 1));
1085
1086
1087 assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog", 1));
1088 assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant", 3));
1089 assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo", 7));
1090 assertEquals(-1, StringUtils.getLevenshteinDistance("elephant", "hippo", 6));
1091 assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant", 7));
1092 assertEquals(-1, StringUtils.getLevenshteinDistance("hippo", "elephant", 6));
1093 assertEquals(8, StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz", 8));
1094 assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz", "hippo", 8));
1095 assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo", 1));
1096
1097 assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog", Integer.MAX_VALUE));
1098 assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant", Integer.MAX_VALUE));
1099 assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo", Integer.MAX_VALUE));
1100 assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant", Integer.MAX_VALUE));
1101 assertEquals(8, StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz", Integer.MAX_VALUE));
1102 assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz", "hippo", Integer.MAX_VALUE));
1103 assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo", Integer.MAX_VALUE));
1104 }
1105
1106 @Test
1107 public void testGetLevenshteinDistance_StringStringNegativeInt() {
1108 assertThrows(IllegalArgumentException.class, () -> StringUtils.getLevenshteinDistance("a", "a", -1));
1109 }
1110
1111
1112
1113
1114 @Test
1115 public void testIsAllLowerCase() {
1116 assertFalse(StringUtils.isAllLowerCase(null));
1117 assertFalse(StringUtils.isAllLowerCase(StringUtils.EMPTY));
1118 assertFalse(StringUtils.isAllLowerCase(" "));
1119 assertTrue(StringUtils.isAllLowerCase("abc"));
1120 assertFalse(StringUtils.isAllLowerCase("abc "));
1121 assertFalse(StringUtils.isAllLowerCase("abc\n"));
1122 assertFalse(StringUtils.isAllLowerCase("abC"));
1123 assertFalse(StringUtils.isAllLowerCase("ab c"));
1124 assertFalse(StringUtils.isAllLowerCase("ab1c"));
1125 assertFalse(StringUtils.isAllLowerCase("ab/c"));
1126 }
1127
1128
1129
1130
1131 @Test
1132 public void testIsAllUpperCase() {
1133 assertFalse(StringUtils.isAllUpperCase(null));
1134 assertFalse(StringUtils.isAllUpperCase(StringUtils.EMPTY));
1135 assertFalse(StringUtils.isAllUpperCase(" "));
1136 assertTrue(StringUtils.isAllUpperCase("ABC"));
1137 assertFalse(StringUtils.isAllUpperCase("ABC "));
1138 assertFalse(StringUtils.isAllUpperCase("ABC\n"));
1139 assertFalse(StringUtils.isAllUpperCase("aBC"));
1140 assertFalse(StringUtils.isAllUpperCase("A C"));
1141 assertFalse(StringUtils.isAllUpperCase("A1C"));
1142 assertFalse(StringUtils.isAllUpperCase("A/C"));
1143 }
1144
1145
1146
1147
1148 @Test
1149 public void testIsMixedCase() {
1150 assertFalse(StringUtils.isMixedCase(null));
1151 assertFalse(StringUtils.isMixedCase(StringUtils.EMPTY));
1152 assertFalse(StringUtils.isMixedCase(" "));
1153 assertFalse(StringUtils.isMixedCase("A"));
1154 assertFalse(StringUtils.isMixedCase("a"));
1155 assertFalse(StringUtils.isMixedCase("/"));
1156 assertFalse(StringUtils.isMixedCase("A/"));
1157 assertFalse(StringUtils.isMixedCase("/b"));
1158 assertFalse(StringUtils.isMixedCase("abc"));
1159 assertFalse(StringUtils.isMixedCase("ABC"));
1160 assertTrue(StringUtils.isMixedCase("aBc"));
1161 assertTrue(StringUtils.isMixedCase("aBc "));
1162 assertTrue(StringUtils.isMixedCase("A c"));
1163 assertTrue(StringUtils.isMixedCase("aBc\n"));
1164 assertTrue(StringUtils.isMixedCase("A1c"));
1165 assertTrue(StringUtils.isMixedCase("a/C"));
1166 }
1167
1168 @Test
1169 public void testJoin_ArrayCharSeparator() {
1170 assertNull(StringUtils.join((Object[]) null, ','));
1171 assertEquals(TEXT_LIST_CHAR, StringUtils.join(ARRAY_LIST, SEPARATOR_CHAR));
1172 assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR_CHAR));
1173 assertEquals(";;foo", StringUtils.join(MIXED_ARRAY_LIST, SEPARATOR_CHAR));
1174 assertEquals("foo;2", StringUtils.join(MIXED_TYPE_LIST, SEPARATOR_CHAR));
1175
1176 assertNull(StringUtils.join((Object[]) null, ',', 0, 1));
1177 assertEquals("/", StringUtils.join(MIXED_ARRAY_LIST, '/', 0, MIXED_ARRAY_LIST.length - 1));
1178 assertEquals("foo", StringUtils.join(MIXED_TYPE_LIST, '/', 0, 1));
1179 assertEquals("null", StringUtils.join(NULL_TO_STRING_LIST, '/', 0, 1));
1180 assertEquals("foo/2", StringUtils.join(MIXED_TYPE_LIST, '/', 0, 2));
1181 assertEquals("2", StringUtils.join(MIXED_TYPE_LIST, '/', 1, 2));
1182 assertEquals("", StringUtils.join(MIXED_TYPE_LIST, '/', 2, 1));
1183 }
1184
1185
1186 @Test
1187 public void testJoin_ArrayOfBooleans() {
1188 assertNull(StringUtils.join((boolean[]) null, COMMA_SEPARATOR_CHAR));
1189 assertEquals("false;false", StringUtils.join(ARRAY_FALSE_FALSE, SEPARATOR_CHAR));
1190 assertEquals("", StringUtils.join(EMPTY, SEPARATOR_CHAR));
1191 assertEquals("false,true,false", StringUtils.join(ARRAY_FALSE_TRUE_FALSE, COMMA_SEPARATOR_CHAR));
1192 assertEquals("true", StringUtils.join(ARRAY_FALSE_TRUE, SEPARATOR_CHAR, 1, 2));
1193 assertNull(StringUtils.join((boolean[]) null, SEPARATOR_CHAR, 0, 1));
1194 assertEquals(StringUtils.EMPTY, StringUtils.join(ARRAY_FALSE_FALSE, SEPARATOR_CHAR, 0, 0));
1195 assertEquals(StringUtils.EMPTY, StringUtils.join(ARRAY_FALSE_TRUE_FALSE, SEPARATOR_CHAR, 1, 0));
1196 }
1197
1198 @Test
1199 public void testJoin_ArrayOfBytes() {
1200 assertNull(StringUtils.join((byte[]) null, ','));
1201 assertEquals("1;2", StringUtils.join(BYTE_PRIM_LIST, SEPARATOR_CHAR));
1202 assertEquals("2", StringUtils.join(BYTE_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
1203 assertNull(StringUtils.join((byte[]) null, SEPARATOR_CHAR, 0, 1));
1204 assertEquals(StringUtils.EMPTY, StringUtils.join(BYTE_PRIM_LIST, SEPARATOR_CHAR, 0, 0));
1205 assertEquals(StringUtils.EMPTY, StringUtils.join(BYTE_PRIM_LIST, SEPARATOR_CHAR, 1, 0));
1206 }
1207
1208 @Test
1209 public void testJoin_ArrayOfChars() {
1210 assertNull(StringUtils.join((char[]) null, ','));
1211 assertEquals("1;2", StringUtils.join(CHAR_PRIM_LIST, SEPARATOR_CHAR));
1212 assertEquals("2", StringUtils.join(CHAR_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
1213 assertNull(StringUtils.join((char[]) null, SEPARATOR_CHAR, 0, 1));
1214 assertEquals(StringUtils.EMPTY, StringUtils.join(CHAR_PRIM_LIST, SEPARATOR_CHAR, 0, 0));
1215 assertEquals(StringUtils.EMPTY, StringUtils.join(CHAR_PRIM_LIST, SEPARATOR_CHAR, 1, 0));
1216 }
1217
1218 @Test
1219 public void testJoin_ArrayOfDoubles() {
1220 assertNull(StringUtils.join((double[]) null, ','));
1221 assertEquals("1.0;2.0", StringUtils.join(DOUBLE_PRIM_LIST, SEPARATOR_CHAR));
1222 assertEquals("2.0", StringUtils.join(DOUBLE_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
1223 assertNull(StringUtils.join((double[]) null, SEPARATOR_CHAR, 0, 1));
1224 assertEquals(StringUtils.EMPTY, StringUtils.join(DOUBLE_PRIM_LIST, SEPARATOR_CHAR, 0, 0));
1225 assertEquals(StringUtils.EMPTY, StringUtils.join(DOUBLE_PRIM_LIST, SEPARATOR_CHAR, 1, 0));
1226 }
1227
1228 @Test
1229 public void testJoin_ArrayOfFloats() {
1230 assertNull(StringUtils.join((float[]) null, ','));
1231 assertEquals("1.0;2.0", StringUtils.join(FLOAT_PRIM_LIST, SEPARATOR_CHAR));
1232 assertEquals("2.0", StringUtils.join(FLOAT_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
1233 assertNull(StringUtils.join((float[]) null, SEPARATOR_CHAR, 0, 1));
1234 assertEquals(StringUtils.EMPTY, StringUtils.join(FLOAT_PRIM_LIST, SEPARATOR_CHAR, 0, 0));
1235 assertEquals(StringUtils.EMPTY, StringUtils.join(FLOAT_PRIM_LIST, SEPARATOR_CHAR, 1, 0));
1236 }
1237
1238 @Test
1239 public void testJoin_ArrayOfInts() {
1240 assertNull(StringUtils.join((int[]) null, ','));
1241 assertEquals("1;2", StringUtils.join(INT_PRIM_LIST, SEPARATOR_CHAR));
1242 assertEquals("2", StringUtils.join(INT_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
1243 assertNull(StringUtils.join((int[]) null, SEPARATOR_CHAR, 0, 1));
1244 assertEquals(StringUtils.EMPTY, StringUtils.join(INT_PRIM_LIST, SEPARATOR_CHAR, 0, 0));
1245 assertEquals(StringUtils.EMPTY, StringUtils.join(INT_PRIM_LIST, SEPARATOR_CHAR, 1, 0));
1246 }
1247
1248 @Test
1249 public void testJoin_ArrayOfLongs() {
1250 assertNull(StringUtils.join((long[]) null, ','));
1251 assertEquals("1;2", StringUtils.join(LONG_PRIM_LIST, SEPARATOR_CHAR));
1252 assertEquals("2", StringUtils.join(LONG_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
1253 assertNull(StringUtils.join((long[]) null, SEPARATOR_CHAR, 0, 1));
1254 assertEquals(StringUtils.EMPTY, StringUtils.join(LONG_PRIM_LIST, SEPARATOR_CHAR, 0, 0));
1255 assertEquals(StringUtils.EMPTY, StringUtils.join(LONG_PRIM_LIST, SEPARATOR_CHAR, 1, 0));
1256 }
1257
1258 @Test
1259 public void testJoin_ArrayOfShorts() {
1260 assertNull(StringUtils.join((short[]) null, ','));
1261 assertEquals("1;2", StringUtils.join(SHORT_PRIM_LIST, SEPARATOR_CHAR));
1262 assertEquals("2", StringUtils.join(SHORT_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
1263 assertNull(StringUtils.join((short[]) null, SEPARATOR_CHAR, 0, 1));
1264 assertEquals(StringUtils.EMPTY, StringUtils.join(SHORT_PRIM_LIST, SEPARATOR_CHAR, 0, 0));
1265 assertEquals(StringUtils.EMPTY, StringUtils.join(SHORT_PRIM_LIST, SEPARATOR_CHAR, 1, 0));
1266 }
1267
1268 @Test
1269 public void testJoin_ArrayString_EmptyDelimiter() {
1270 assertNull(StringUtils.join((Object[]) null, null));
1271 assertEquals(TEXT_LIST_NOSEP, StringUtils.join(ARRAY_LIST, null));
1272 assertEquals(TEXT_LIST_NOSEP, StringUtils.join(ARRAY_LIST, ""));
1273
1274 assertEquals("", StringUtils.join(NULL_ARRAY_LIST, null));
1275
1276 assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, null));
1277 assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, ""));
1278
1279 assertEquals("", StringUtils.join(MIXED_ARRAY_LIST, "", 0, MIXED_ARRAY_LIST.length - 1));
1280 }
1281
1282 @ParameterizedTest
1283 @ValueSource(strings = {",", ";", Supplementary.CharU20000, Supplementary.CharU20001})
1284 public void testJoin_ArrayString_NonEmptyDelimiter(final String delimiter) {
1285 assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, delimiter));
1286
1287 assertEquals(String.join(delimiter, ARRAY_LIST), StringUtils.join(ARRAY_LIST, delimiter));
1288 assertEquals(delimiter + delimiter + "foo", StringUtils.join(MIXED_ARRAY_LIST, delimiter));
1289 assertEquals(String.join(delimiter, "foo", "2"), StringUtils.join(MIXED_TYPE_LIST, delimiter));
1290
1291 assertEquals(delimiter, StringUtils.join(MIXED_ARRAY_LIST, delimiter, 0, MIXED_ARRAY_LIST.length - 1));
1292 assertEquals("foo", StringUtils.join(MIXED_TYPE_LIST, delimiter, 0, 1));
1293 assertEquals(String.join(delimiter, "foo", "2"), StringUtils.join(MIXED_TYPE_LIST, delimiter, 0, 2));
1294 assertEquals("2", StringUtils.join(MIXED_TYPE_LIST, delimiter, 1, 2));
1295 assertEquals("", StringUtils.join(MIXED_TYPE_LIST, delimiter, 2, 1));
1296 }
1297
1298 @Test
1299 public void testJoin_IterableChar() {
1300 assertNull(StringUtils.join((Iterable<?>) null, ','));
1301 assertEquals(TEXT_LIST_CHAR, StringUtils.join(Arrays.asList(ARRAY_LIST), SEPARATOR_CHAR));
1302 assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST), SEPARATOR_CHAR));
1303 assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST), SEPARATOR_CHAR));
1304 assertEquals("foo", StringUtils.join(Collections.singleton("foo"), 'x'));
1305 }
1306
1307 @Test
1308 public void testJoin_IterableString() {
1309 assertNull(StringUtils.join((Iterable<?>) null, null));
1310 assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays.asList(ARRAY_LIST), null));
1311 assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays.asList(ARRAY_LIST), ""));
1312 assertEquals("foo", StringUtils.join(Collections.singleton("foo"), "x"));
1313 assertEquals("foo", StringUtils.join(Collections.singleton("foo"), null));
1314
1315 assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST), null));
1316
1317 assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST), null));
1318 assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST), ""));
1319 assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST), SEPARATOR));
1320
1321 assertEquals(TEXT_LIST, StringUtils.join(Arrays.asList(ARRAY_LIST), SEPARATOR));
1322 }
1323
1324 @Test
1325 public void testJoin_IteratorChar() {
1326 assertNull(StringUtils.join((Iterator<?>) null, ','));
1327 assertEquals(TEXT_LIST_CHAR, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), SEPARATOR_CHAR));
1328 assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST).iterator(), SEPARATOR_CHAR));
1329 assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), SEPARATOR_CHAR));
1330 assertEquals("foo", StringUtils.join(Collections.singleton("foo").iterator(), 'x'));
1331 assertEquals("null", StringUtils.join(Arrays.asList(NULL_TO_STRING_LIST).iterator(), SEPARATOR_CHAR));
1332 }
1333
1334 @Test
1335 public void testJoin_IteratorString() {
1336 assertNull(StringUtils.join((Iterator<?>) null, null));
1337 assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), null));
1338 assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), ""));
1339 assertEquals("foo", StringUtils.join(Collections.singleton("foo").iterator(), "x"));
1340 assertEquals("foo", StringUtils.join(Collections.singleton("foo").iterator(), null));
1341
1342 assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST).iterator(), null));
1343
1344 assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), null));
1345 assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), ""));
1346 assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), SEPARATOR));
1347
1348 assertEquals(TEXT_LIST, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), SEPARATOR));
1349
1350 assertEquals("null", StringUtils.join(Arrays.asList(NULL_TO_STRING_LIST).iterator(), SEPARATOR));
1351 }
1352
1353 @Test
1354 public void testJoin_List_CharDelimiter() {
1355 assertEquals("/", StringUtils.join(MIXED_STRING_LIST, '/', 0, MIXED_STRING_LIST.size() - 1));
1356 assertEquals("foo", StringUtils.join(MIXED_TYPE_OBJECT_LIST, '/', 0, 1));
1357 assertEquals("foo/2", StringUtils.join(MIXED_TYPE_OBJECT_LIST, '/', 0, 2));
1358 assertEquals("2", StringUtils.join(MIXED_TYPE_OBJECT_LIST, '/', 1, 2));
1359 assertEquals("", StringUtils.join(MIXED_TYPE_OBJECT_LIST, '/', 2, 1));
1360 assertNull(null, StringUtils.join((List<?>) null, '/', 0, 1));
1361 }
1362
1363 @Test
1364 public void testJoin_List_EmptyDelimiter() {
1365 assertNull(StringUtils.join((List<String>) null, null));
1366 assertEquals(TEXT_LIST_NOSEP, StringUtils.join(STRING_LIST, null));
1367 assertEquals(TEXT_LIST_NOSEP, StringUtils.join(STRING_LIST, ""));
1368
1369 assertEquals("", StringUtils.join(NULL_STRING_LIST, null));
1370
1371 assertEquals("", StringUtils.join(EMPTY_STRING_LIST, null));
1372 assertEquals("", StringUtils.join(EMPTY_STRING_LIST, ""));
1373
1374 assertEquals("", StringUtils.join(MIXED_STRING_LIST, "", 0, MIXED_STRING_LIST.size()- 1));
1375 }
1376
1377 @ParameterizedTest
1378 @ValueSource(strings = {",", ";", Supplementary.CharU20000, Supplementary.CharU20001})
1379 public void testJoin_List_NonEmptyDelimiter(final String delimiter) {
1380 assertEquals("", StringUtils.join(EMPTY_STRING_LIST, delimiter));
1381
1382 assertEquals(String.join(delimiter, STRING_LIST), StringUtils.join(STRING_LIST, delimiter));
1383 assertEquals(delimiter + delimiter + "foo", StringUtils.join(MIXED_STRING_LIST, delimiter));
1384 assertEquals(String.join(delimiter, "foo", "2"), StringUtils.join(MIXED_TYPE_OBJECT_LIST, delimiter));
1385
1386 assertEquals(delimiter, StringUtils.join(MIXED_STRING_LIST, delimiter, 0, MIXED_STRING_LIST.size() - 1));
1387 assertEquals("foo", StringUtils.join(MIXED_TYPE_OBJECT_LIST, delimiter, 0, 1));
1388 assertEquals(String.join(delimiter, "foo", "2"), StringUtils.join(MIXED_TYPE_OBJECT_LIST, delimiter, 0, 2));
1389 assertEquals("2", StringUtils.join(MIXED_TYPE_OBJECT_LIST, delimiter, 1, 2));
1390 assertEquals("", StringUtils.join(MIXED_TYPE_OBJECT_LIST, delimiter, 2, 1));
1391 assertNull(null, StringUtils.join((List<?>) null, delimiter, 0, 1));
1392 }
1393
1394 @Test
1395 public void testJoin_Objectarray() {
1396
1397 assertNull(StringUtils.join((Object[]) null));
1398
1399 assertEquals("", StringUtils.join());
1400 assertEquals("", StringUtils.join((Object) null));
1401
1402 assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST));
1403 assertEquals("", StringUtils.join(NULL_ARRAY_LIST));
1404 assertEquals("null", StringUtils.join(NULL_TO_STRING_LIST));
1405 assertEquals("abc", StringUtils.join("a", "b", "c"));
1406 assertEquals("a", StringUtils.join(null, "a", ""));
1407 assertEquals("foo", StringUtils.join(MIXED_ARRAY_LIST));
1408 assertEquals("foo2", StringUtils.join(MIXED_TYPE_LIST));
1409 }
1410
1411 @Test
1412 public void testJoin_Objects() {
1413 assertEquals("abc", StringUtils.join("a", "b", "c"));
1414 assertEquals("a", StringUtils.join(null, "", "a"));
1415 assertNull(StringUtils.join((Object[]) null));
1416 }
1417
1418 @ParameterizedTest
1419 @ValueSource(strings = {",", ";", Supplementary.CharU20000, Supplementary.CharU20001})
1420 public void testJoinWith(final String delimiter) {
1421 assertEquals("", StringUtils.joinWith(delimiter));
1422 assertEquals("", StringUtils.joinWith(delimiter, (Object[]) NULL_ARRAY_LIST));
1423 assertEquals("null", StringUtils.joinWith(delimiter, NULL_TO_STRING_LIST));
1424
1425 assertEquals(String.join(delimiter, "a", "b", "c"), StringUtils.joinWith(delimiter, "a", "b", "c"));
1426 assertEquals(String.join(delimiter, "", "a", ""), StringUtils.joinWith(delimiter, null, "a", ""));
1427 assertEquals(String.join(delimiter, "", "a", ""), StringUtils.joinWith(delimiter, "", "a", ""));
1428
1429 assertEquals("ab", StringUtils.joinWith(null, "a", "b"));
1430 }
1431
1432 @Test
1433 public void testJoinWithThrowsException() {
1434 assertThrows(IllegalArgumentException.class, () -> StringUtils.joinWith(",", (Object[]) null));
1435 }
1436
1437 @Disabled
1438 @Test
1439 public void testLang1593() {
1440 final int[] arr = {1, 2, 3, 4, 5, 6, 7};
1441 final String expected = StringUtils.join(arr, '-');
1442 final String actual = StringUtils.join(arr, "-");
1443 assertEquals(expected, actual);
1444 }
1445
1446 @Test
1447 public void testLang623() {
1448 assertEquals("t", StringUtils.replaceChars("\u00DE", '\u00DE', 't'));
1449 assertEquals("t", StringUtils.replaceChars("\u00FE", '\u00FE', 't'));
1450 }
1451
1452 @Test
1453 public void testLANG666() {
1454 assertEquals("12", StringUtils.stripEnd("120.00", ".0"));
1455 assertEquals("121", StringUtils.stripEnd("121.00", ".0"));
1456 }
1457
1458 @Test
1459 public void testLeftPad_StringInt() {
1460 assertNull(StringUtils.leftPad(null, 5));
1461 assertEquals(" ", StringUtils.leftPad("", 5));
1462 assertEquals(" abc", StringUtils.leftPad("abc", 5));
1463 assertEquals("abc", StringUtils.leftPad("abc", 2));
1464 }
1465
1466 @Test
1467 public void testLeftPad_StringIntChar() {
1468 assertNull(StringUtils.leftPad(null, 5, ' '));
1469 assertEquals(" ", StringUtils.leftPad("", 5, ' '));
1470 assertEquals(" abc", StringUtils.leftPad("abc", 5, ' '));
1471 assertEquals("xxabc", StringUtils.leftPad("abc", 5, 'x'));
1472 assertEquals("\uffff\uffffabc", StringUtils.leftPad("abc", 5, '\uffff'));
1473 assertEquals("abc", StringUtils.leftPad("abc", 2, ' '));
1474 final String str = StringUtils.leftPad("aaa", 10000, 'a');
1475 assertEquals(10000, str.length());
1476 assertTrue(StringUtils.containsOnly(str, 'a'));
1477 }
1478
1479 @Test
1480 public void testLeftPad_StringIntString() {
1481 assertNull(StringUtils.leftPad(null, 5, "-+"));
1482 assertNull(StringUtils.leftPad(null, 5, null));
1483 assertEquals(" ", StringUtils.leftPad("", 5, " "));
1484 assertEquals("-+-+abc", StringUtils.leftPad("abc", 7, "-+"));
1485 assertEquals("-+~abc", StringUtils.leftPad("abc", 6, "-+~"));
1486 assertEquals("-+abc", StringUtils.leftPad("abc", 5, "-+~"));
1487 assertEquals("abc", StringUtils.leftPad("abc", 2, " "));
1488 assertEquals("abc", StringUtils.leftPad("abc", -1, " "));
1489 assertEquals(" abc", StringUtils.leftPad("abc", 5, null));
1490 assertEquals(" abc", StringUtils.leftPad("abc", 5, ""));
1491 }
1492
1493 @Test
1494 public void testLength_CharBuffer() {
1495 assertEquals(0, StringUtils.length(CharBuffer.wrap("")));
1496 assertEquals(1, StringUtils.length(CharBuffer.wrap("A")));
1497 assertEquals(1, StringUtils.length(CharBuffer.wrap(" ")));
1498 assertEquals(8, StringUtils.length(CharBuffer.wrap("ABCDEFGH")));
1499 }
1500
1501 @Test
1502 public void testLengthString() {
1503 assertEquals(0, StringUtils.length(null));
1504 assertEquals(0, StringUtils.length(""));
1505 assertEquals(0, StringUtils.length(StringUtils.EMPTY));
1506 assertEquals(1, StringUtils.length("A"));
1507 assertEquals(1, StringUtils.length(" "));
1508 assertEquals(8, StringUtils.length("ABCDEFGH"));
1509 }
1510
1511 @Test
1512 public void testLengthStringBuffer() {
1513 assertEquals(0, StringUtils.length(new StringBuffer("")));
1514 assertEquals(0, StringUtils.length(new StringBuffer(StringUtils.EMPTY)));
1515 assertEquals(1, StringUtils.length(new StringBuffer("A")));
1516 assertEquals(1, StringUtils.length(new StringBuffer(" ")));
1517 assertEquals(8, StringUtils.length(new StringBuffer("ABCDEFGH")));
1518 }
1519
1520 @Test
1521 public void testLengthStringBuilder() {
1522 assertEquals(0, StringUtils.length(new StringBuilder("")));
1523 assertEquals(0, StringUtils.length(new StringBuilder(StringUtils.EMPTY)));
1524 assertEquals(1, StringUtils.length(new StringBuilder("A")));
1525 assertEquals(1, StringUtils.length(new StringBuilder(" ")));
1526 assertEquals(8, StringUtils.length(new StringBuilder("ABCDEFGH")));
1527 }
1528
1529 @Test
1530 public void testLowerCase() {
1531 assertNull(StringUtils.lowerCase(null));
1532 assertNull(StringUtils.lowerCase(null, Locale.ENGLISH));
1533 assertEquals("foo test thing", StringUtils.lowerCase("fOo test THING"), "lowerCase(String) failed");
1534 assertEquals("", StringUtils.lowerCase(""), "lowerCase(empty-string) failed");
1535 assertEquals("foo test thing", StringUtils.lowerCase("fOo test THING", Locale.ENGLISH),
1536 "lowerCase(String, Locale) failed");
1537 assertEquals("", StringUtils.lowerCase("", Locale.ENGLISH), "lowerCase(empty-string, Locale) failed");
1538 }
1539
1540 @Test
1541 public void testNormalizeSpace() {
1542
1543 assertFalse(Character.isWhitespace('\u00A0'));
1544
1545 assertNull(StringUtils.normalizeSpace(null));
1546 assertEquals("", StringUtils.normalizeSpace(""));
1547 assertEquals("", StringUtils.normalizeSpace(" "));
1548 assertEquals("", StringUtils.normalizeSpace("\t"));
1549 assertEquals("", StringUtils.normalizeSpace("\n"));
1550 assertEquals("", StringUtils.normalizeSpace("\u0009"));
1551 assertEquals("", StringUtils.normalizeSpace("\u000B"));
1552 assertEquals("", StringUtils.normalizeSpace("\u000C"));
1553 assertEquals("", StringUtils.normalizeSpace("\u001C"));
1554 assertEquals("", StringUtils.normalizeSpace("\u001D"));
1555 assertEquals("", StringUtils.normalizeSpace("\u001E"));
1556 assertEquals("", StringUtils.normalizeSpace("\u001F"));
1557 assertEquals("", StringUtils.normalizeSpace("\f"));
1558 assertEquals("", StringUtils.normalizeSpace("\r"));
1559 assertEquals("a", StringUtils.normalizeSpace(" a "));
1560 assertEquals("a b c", StringUtils.normalizeSpace(" a b c "));
1561 assertEquals("a b c", StringUtils.normalizeSpace("a\t\f\r b\u000B c\n"));
1562 assertEquals("a b c", StringUtils.normalizeSpace("a\t\f\r " + HARD_SPACE + HARD_SPACE + "b\u000B c\n"));
1563 assertEquals("b", StringUtils.normalizeSpace("\u0000b"));
1564 assertEquals("b", StringUtils.normalizeSpace("b\u0000"));
1565 }
1566
1567 @Test
1568 public void testOverlay_StringStringIntInt() {
1569 assertNull(StringUtils.overlay(null, null, 2, 4));
1570 assertNull(StringUtils.overlay(null, null, -2, -4));
1571
1572 assertEquals("", StringUtils.overlay("", null, 0, 0));
1573 assertEquals("", StringUtils.overlay("", "", 0, 0));
1574 assertEquals("zzzz", StringUtils.overlay("", "zzzz", 0, 0));
1575 assertEquals("zzzz", StringUtils.overlay("", "zzzz", 2, 4));
1576 assertEquals("zzzz", StringUtils.overlay("", "zzzz", -2, -4));
1577
1578 assertEquals("abef", StringUtils.overlay("abcdef", null, 2, 4));
1579 assertEquals("abef", StringUtils.overlay("abcdef", null, 4, 2));
1580 assertEquals("abef", StringUtils.overlay("abcdef", "", 2, 4));
1581 assertEquals("abef", StringUtils.overlay("abcdef", "", 4, 2));
1582 assertEquals("abzzzzef", StringUtils.overlay("abcdef", "zzzz", 2, 4));
1583 assertEquals("abzzzzef", StringUtils.overlay("abcdef", "zzzz", 4, 2));
1584
1585 assertEquals("zzzzef", StringUtils.overlay("abcdef", "zzzz", -1, 4));
1586 assertEquals("zzzzef", StringUtils.overlay("abcdef", "zzzz", 4, -1));
1587 assertEquals("zzzzabcdef", StringUtils.overlay("abcdef", "zzzz", -2, -1));
1588 assertEquals("zzzzabcdef", StringUtils.overlay("abcdef", "zzzz", -1, -2));
1589 assertEquals("abcdzzzz", StringUtils.overlay("abcdef", "zzzz", 4, 10));
1590 assertEquals("abcdzzzz", StringUtils.overlay("abcdef", "zzzz", 10, 4));
1591 assertEquals("abcdefzzzz", StringUtils.overlay("abcdef", "zzzz", 8, 10));
1592 assertEquals("abcdefzzzz", StringUtils.overlay("abcdef", "zzzz", 10, 8));
1593 }
1594
1595
1596
1597
1598 @Test
1599 public void testPrependIfMissing() {
1600 assertNull(StringUtils.prependIfMissing(null, null), "prependIfMissing(null,null)");
1601 assertEquals("abc", StringUtils.prependIfMissing("abc", null), "prependIfMissing(abc,null)");
1602 assertEquals("xyz", StringUtils.prependIfMissing("", "xyz"), "prependIfMissing(\"\",xyz)");
1603 assertEquals("xyzabc", StringUtils.prependIfMissing("abc", "xyz"), "prependIfMissing(abc,xyz)");
1604 assertEquals("xyzabc", StringUtils.prependIfMissing("xyzabc", "xyz"), "prependIfMissing(xyzabc,xyz)");
1605 assertEquals("xyzXYZabc", StringUtils.prependIfMissing("XYZabc", "xyz"), "prependIfMissing(XYZabc,xyz)");
1606
1607 assertNull(StringUtils.prependIfMissing(null, null, (CharSequence[]) null), "prependIfMissing(null,null null)");
1608 assertEquals("abc", StringUtils.prependIfMissing("abc", null, (CharSequence[]) null), "prependIfMissing(abc,null,null)");
1609 assertEquals("xyz", StringUtils.prependIfMissing("", "xyz", (CharSequence[]) null), "prependIfMissing(\"\",xyz,null)");
1610 assertEquals("xyzabc", StringUtils.prependIfMissing("abc", "xyz", null), "prependIfMissing(abc,xyz,{null})");
1611 assertEquals("abc", StringUtils.prependIfMissing("abc", "xyz", ""), "prependIfMissing(abc,xyz,\"\")");
1612 assertEquals("xyzabc", StringUtils.prependIfMissing("abc", "xyz", "mno"), "prependIfMissing(abc,xyz,mno)");
1613 assertEquals("xyzabc", StringUtils.prependIfMissing("xyzabc", "xyz", "mno"), "prependIfMissing(xyzabc,xyz,mno)");
1614 assertEquals("mnoabc", StringUtils.prependIfMissing("mnoabc", "xyz", "mno"), "prependIfMissing(mnoabc,xyz,mno)");
1615 assertEquals("xyzXYZabc", StringUtils.prependIfMissing("XYZabc", "xyz", "mno"), "prependIfMissing(XYZabc,xyz,mno)");
1616 assertEquals("xyzMNOabc", StringUtils.prependIfMissing("MNOabc", "xyz", "mno"), "prependIfMissing(MNOabc,xyz,mno)");
1617 }
1618
1619
1620
1621
1622 @Test
1623 public void testPrependIfMissingIgnoreCase() {
1624 assertNull(StringUtils.prependIfMissingIgnoreCase(null, null), "prependIfMissingIgnoreCase(null,null)");
1625 assertEquals("abc", StringUtils.prependIfMissingIgnoreCase("abc", null), "prependIfMissingIgnoreCase(abc,null)");
1626 assertEquals("xyz", StringUtils.prependIfMissingIgnoreCase("", "xyz"), "prependIfMissingIgnoreCase(\"\",xyz)");
1627 assertEquals("xyzabc", StringUtils.prependIfMissingIgnoreCase("abc", "xyz"), "prependIfMissingIgnoreCase(abc,xyz)");
1628 assertEquals("xyzabc", StringUtils.prependIfMissingIgnoreCase("xyzabc", "xyz"), "prependIfMissingIgnoreCase(xyzabc,xyz)");
1629 assertEquals("XYZabc", StringUtils.prependIfMissingIgnoreCase("XYZabc", "xyz"), "prependIfMissingIgnoreCase(XYZabc,xyz)");
1630
1631 assertNull(StringUtils.prependIfMissingIgnoreCase(null, null, (CharSequence[]) null), "prependIfMissingIgnoreCase(null,null null)");
1632 assertEquals("abc", StringUtils.prependIfMissingIgnoreCase("abc", null, (CharSequence[]) null), "prependIfMissingIgnoreCase(abc,null,null)");
1633 assertEquals("xyz", StringUtils.prependIfMissingIgnoreCase("", "xyz", (CharSequence[]) null), "prependIfMissingIgnoreCase(\"\",xyz,null)");
1634 assertEquals("xyzabc", StringUtils.prependIfMissingIgnoreCase("abc", "xyz", null), "prependIfMissingIgnoreCase(abc,xyz,{null})");
1635 assertEquals("abc", StringUtils.prependIfMissingIgnoreCase("abc", "xyz", ""), "prependIfMissingIgnoreCase(abc,xyz,\"\")");
1636 assertEquals("xyzabc", StringUtils.prependIfMissingIgnoreCase("abc", "xyz", "mno"), "prependIfMissingIgnoreCase(abc,xyz,mno)");
1637 assertEquals("xyzabc", StringUtils.prependIfMissingIgnoreCase("xyzabc", "xyz", "mno"), "prependIfMissingIgnoreCase(xyzabc,xyz,mno)");
1638 assertEquals("mnoabc", StringUtils.prependIfMissingIgnoreCase("mnoabc", "xyz", "mno"), "prependIfMissingIgnoreCase(mnoabc,xyz,mno)");
1639 assertEquals("XYZabc", StringUtils.prependIfMissingIgnoreCase("XYZabc", "xyz", "mno"), "prependIfMissingIgnoreCase(XYZabc,xyz,mno)");
1640 assertEquals("MNOabc", StringUtils.prependIfMissingIgnoreCase("MNOabc", "xyz", "mno"), "prependIfMissingIgnoreCase(MNOabc,xyz,mno)");
1641 }
1642
1643 @Test
1644 public void testReCapitalize() {
1645
1646 assertEquals(SENTENCE_UNCAP, StringUtils.uncapitalize(StringUtils.capitalize(SENTENCE_UNCAP)),
1647 "uncapitalize(capitalize(String)) failed");
1648 assertEquals(SENTENCE_CAP, StringUtils.capitalize(StringUtils.uncapitalize(SENTENCE_CAP)),
1649 "capitalize(uncapitalize(String)) failed");
1650
1651
1652 assertEquals(FOO_UNCAP, StringUtils.uncapitalize(StringUtils.capitalize(FOO_UNCAP)),
1653 "uncapitalize(capitalize(String)) failed");
1654 assertEquals(FOO_CAP, StringUtils.capitalize(StringUtils.uncapitalize(FOO_CAP)),
1655 "capitalize(uncapitalize(String)) failed");
1656 }
1657
1658 @Test
1659 public void testRemove_char() {
1660
1661 assertNull(StringUtils.remove(null, null));
1662 assertNull(StringUtils.remove(null, 'a'));
1663
1664
1665 assertEquals("", StringUtils.remove("", null));
1666 assertEquals("", StringUtils.remove("", 'a'));
1667
1668
1669 assertEquals("qeed", StringUtils.remove("queued", 'u'));
1670
1671
1672 assertEquals("queued", StringUtils.remove("queued", 'z'));
1673 }
1674
1675 @Test
1676 public void testRemove_String() {
1677
1678 assertNull(StringUtils.remove(null, null));
1679 assertNull(StringUtils.remove(null, ""));
1680 assertNull(StringUtils.remove(null, "a"));
1681
1682
1683 assertEquals("", StringUtils.remove("", null));
1684 assertEquals("", StringUtils.remove("", ""));
1685 assertEquals("", StringUtils.remove("", "a"));
1686
1687
1688 assertNull(StringUtils.remove(null, null));
1689 assertEquals("", StringUtils.remove("", null));
1690 assertEquals("a", StringUtils.remove("a", null));
1691
1692
1693 assertNull(StringUtils.remove(null, ""));
1694 assertEquals("", StringUtils.remove("", ""));
1695 assertEquals("a", StringUtils.remove("a", ""));
1696
1697
1698 assertEquals("qd", StringUtils.remove("queued", "ue"));
1699
1700
1701 assertEquals("queued", StringUtils.remove("queued", "zz"));
1702 }
1703
1704 @Test
1705 public void testRemoveAll_StringString() {
1706 assertNull(StringUtils.removeAll(null, ""));
1707 assertEquals("any", StringUtils.removeAll("any", null));
1708
1709 assertEquals("any", StringUtils.removeAll("any", ""));
1710 assertEquals("", StringUtils.removeAll("any", ".*"));
1711 assertEquals("", StringUtils.removeAll("any", ".+"));
1712 assertEquals("", StringUtils.removeAll("any", ".?"));
1713
1714 assertEquals("A\nB", StringUtils.removeAll("A<__>\n<__>B", "<.*>"));
1715 assertEquals("AB", StringUtils.removeAll("A<__>\n<__>B", "(?s)<.*>"));
1716 assertEquals("ABC123", StringUtils.removeAll("ABCabc123abc", "[a-z]"));
1717
1718 assertThrows(
1719 PatternSyntaxException.class,
1720 () -> StringUtils.removeAll("any", "{badRegexSyntax}"),
1721 "StringUtils.removeAll expecting PatternSyntaxException");
1722 }
1723
1724 @Test
1725 public void testRemoveEnd() {
1726
1727 assertNull(StringUtils.removeEnd(null, null));
1728 assertNull(StringUtils.removeEnd(null, ""));
1729 assertNull(StringUtils.removeEnd(null, "a"));
1730
1731
1732 assertEquals(StringUtils.removeEnd("", null), "");
1733 assertEquals(StringUtils.removeEnd("", ""), "");
1734 assertEquals(StringUtils.removeEnd("", "a"), "");
1735
1736
1737 assertEquals(StringUtils.removeEnd("www.domain.com.", ".com"), "www.domain.com.");
1738 assertEquals(StringUtils.removeEnd("www.domain.com", ".com"), "www.domain");
1739 assertEquals(StringUtils.removeEnd("www.domain", ".com"), "www.domain");
1740 assertEquals(StringUtils.removeEnd("domain.com", ""), "domain.com");
1741 assertEquals(StringUtils.removeEnd("domain.com", null), "domain.com");
1742 }
1743
1744 @Test
1745 public void testRemoveEndIgnoreCase() {
1746
1747 assertNull(StringUtils.removeEndIgnoreCase(null, null), "removeEndIgnoreCase(null, null)");
1748 assertNull(StringUtils.removeEndIgnoreCase(null, ""), "removeEndIgnoreCase(null, \"\")");
1749 assertNull(StringUtils.removeEndIgnoreCase(null, "a"), "removeEndIgnoreCase(null, \"a\")");
1750
1751
1752 assertEquals(StringUtils.removeEndIgnoreCase("", null), "", "removeEndIgnoreCase(\"\", null)");
1753 assertEquals(StringUtils.removeEndIgnoreCase("", ""), "", "removeEndIgnoreCase(\"\", \"\")");
1754 assertEquals(StringUtils.removeEndIgnoreCase("", "a"), "", "removeEndIgnoreCase(\"\", \"a\")");
1755
1756
1757 assertEquals(StringUtils.removeEndIgnoreCase("www.domain.com.", ".com"), "www.domain.com.", "removeEndIgnoreCase(\"www.domain.com.\", \".com\")");
1758 assertEquals(StringUtils.removeEndIgnoreCase("www.domain.com", ".com"), "www.domain", "removeEndIgnoreCase(\"www.domain.com\", \".com\")");
1759 assertEquals(StringUtils.removeEndIgnoreCase("www.domain", ".com"), "www.domain", "removeEndIgnoreCase(\"www.domain\", \".com\")");
1760 assertEquals(StringUtils.removeEndIgnoreCase("domain.com", ""), "domain.com", "removeEndIgnoreCase(\"domain.com\", \"\")");
1761 assertEquals(StringUtils.removeEndIgnoreCase("domain.com", null), "domain.com", "removeEndIgnoreCase(\"domain.com\", null)");
1762
1763
1764 assertEquals(StringUtils.removeEndIgnoreCase("www.domain.com", ".COM"), "www.domain", "removeEndIgnoreCase(\"www.domain.com\", \".COM\")");
1765 assertEquals(StringUtils.removeEndIgnoreCase("www.domain.COM", ".com"), "www.domain", "removeEndIgnoreCase(\"www.domain.COM\", \".com\")");
1766 }
1767
1768 @Test
1769 public void testRemoveFirst_StringString() {
1770 assertNull(StringUtils.removeFirst(null, ""));
1771 assertEquals("any", StringUtils.removeFirst("any", null));
1772
1773 assertEquals("any", StringUtils.removeFirst("any", ""));
1774 assertEquals("", StringUtils.removeFirst("any", ".*"));
1775 assertEquals("", StringUtils.removeFirst("any", ".+"));
1776 assertEquals("bc", StringUtils.removeFirst("abc", ".?"));
1777
1778 assertEquals("A\n<__>B", StringUtils.removeFirst("A<__>\n<__>B", "<.*>"));
1779 assertEquals("AB", StringUtils.removeFirst("A<__>\n<__>B", "(?s)<.*>"));
1780 assertEquals("ABCbc123", StringUtils.removeFirst("ABCabc123", "[a-z]"));
1781 assertEquals("ABC123abc", StringUtils.removeFirst("ABCabc123abc", "[a-z]+"));
1782
1783 assertThrows(
1784 PatternSyntaxException.class,
1785 () -> StringUtils.removeFirst("any", "{badRegexSyntax}"),
1786 "StringUtils.removeFirst expecting PatternSyntaxException");
1787 }
1788
1789 @Test
1790 public void testRemoveIgnoreCase_String() {
1791
1792 assertNull(StringUtils.removeIgnoreCase(null, null));
1793 assertNull(StringUtils.removeIgnoreCase(null, ""));
1794 assertNull(StringUtils.removeIgnoreCase(null, "a"));
1795
1796
1797 assertEquals("", StringUtils.removeIgnoreCase("", null));
1798 assertEquals("", StringUtils.removeIgnoreCase("", ""));
1799 assertEquals("", StringUtils.removeIgnoreCase("", "a"));
1800
1801
1802 assertNull(StringUtils.removeIgnoreCase(null, null));
1803 assertEquals("", StringUtils.removeIgnoreCase("", null));
1804 assertEquals("a", StringUtils.removeIgnoreCase("a", null));
1805
1806
1807 assertNull(StringUtils.removeIgnoreCase(null, ""));
1808 assertEquals("", StringUtils.removeIgnoreCase("", ""));
1809 assertEquals("a", StringUtils.removeIgnoreCase("a", ""));
1810
1811
1812 assertEquals("qd", StringUtils.removeIgnoreCase("queued", "ue"));
1813
1814
1815 assertEquals("queued", StringUtils.removeIgnoreCase("queued", "zz"));
1816
1817
1818
1819 assertEquals("qd", StringUtils.removeIgnoreCase("quEUed", "UE"));
1820
1821
1822 assertEquals("queued", StringUtils.removeIgnoreCase("queued", "zZ"));
1823
1824
1825 assertEquals("\u0130", StringUtils.removeIgnoreCase("\u0130x", "x"));
1826
1827
1828 StringUtils.removeIgnoreCase("Ä°a", "a");
1829 }
1830
1831 @Test
1832 public void testRemovePattern_StringString() {
1833 assertNull(StringUtils.removePattern(null, ""));
1834 assertEquals("any", StringUtils.removePattern("any", null));
1835
1836 assertEquals("", StringUtils.removePattern("", ""));
1837 assertEquals("", StringUtils.removePattern("", ".*"));
1838 assertEquals("", StringUtils.removePattern("", ".+"));
1839
1840 assertEquals("AB", StringUtils.removePattern("A<__>\n<__>B", "<.*>"));
1841 assertEquals("AB", StringUtils.removePattern("A<__>\\n<__>B", "<.*>"));
1842 assertEquals("", StringUtils.removePattern("<A>x\\ny</A>", "<A>.*</A>"));
1843 assertEquals("", StringUtils.removePattern("<A>\nxy\n</A>", "<A>.*</A>"));
1844
1845 assertEquals("ABC123", StringUtils.removePattern("ABCabc123", "[a-z]"));
1846 }
1847
1848 @Test
1849 public void testRemoveStartChar() {
1850
1851 assertNull(StringUtils.removeStart(null, '\0'));
1852 assertNull(StringUtils.removeStart(null, 'a'));
1853
1854
1855 assertEquals(StringUtils.removeStart("", '\0'), "");
1856 assertEquals(StringUtils.removeStart("", 'a'), "");
1857
1858
1859 assertEquals(StringUtils.removeStart("/path", '/'), "path");
1860 assertEquals(StringUtils.removeStart("path", '/'), "path");
1861 assertEquals(StringUtils.removeStart("path", '\0'), "path");
1862 }
1863
1864 @Test
1865 public void testRemoveStartIgnoreCase() {
1866
1867 assertNull(StringUtils.removeStartIgnoreCase(null, null), "removeStartIgnoreCase(null, null)");
1868 assertNull(StringUtils.removeStartIgnoreCase(null, ""), "removeStartIgnoreCase(null, \"\")");
1869 assertNull(StringUtils.removeStartIgnoreCase(null, "a"), "removeStartIgnoreCase(null, \"a\")");
1870
1871
1872 assertEquals(StringUtils.removeStartIgnoreCase("", null), "", "removeStartIgnoreCase(\"\", null)");
1873 assertEquals(StringUtils.removeStartIgnoreCase("", ""), "", "removeStartIgnoreCase(\"\", \"\")");
1874 assertEquals(StringUtils.removeStartIgnoreCase("", "a"), "", "removeStartIgnoreCase(\"\", \"a\")");
1875
1876
1877 assertEquals(StringUtils.removeStartIgnoreCase("www.domain.com", "www."), "domain.com", "removeStartIgnoreCase(\"www.domain.com\", \"www.\")");
1878 assertEquals(StringUtils.removeStartIgnoreCase("domain.com", "www."), "domain.com", "removeStartIgnoreCase(\"domain.com\", \"www.\")");
1879 assertEquals(StringUtils.removeStartIgnoreCase("domain.com", ""), "domain.com", "removeStartIgnoreCase(\"domain.com\", \"\")");
1880 assertEquals(StringUtils.removeStartIgnoreCase("domain.com", null), "domain.com", "removeStartIgnoreCase(\"domain.com\", null)");
1881
1882
1883 assertEquals(StringUtils.removeStartIgnoreCase("www.domain.com", "WWW."), "domain.com", "removeStartIgnoreCase(\"www.domain.com\", \"WWW.\")");
1884 }
1885
1886 @Test
1887 public void testRemoveStartString() {
1888
1889 assertNull(StringUtils.removeStart(null, null));
1890 assertNull(StringUtils.removeStart(null, ""));
1891 assertNull(StringUtils.removeStart(null, "a"));
1892
1893
1894 assertEquals(StringUtils.removeStart("", null), "");
1895 assertEquals(StringUtils.removeStart("", ""), "");
1896 assertEquals(StringUtils.removeStart("", "a"), "");
1897
1898
1899 assertEquals(StringUtils.removeStart("www.domain.com", "www."), "domain.com");
1900 assertEquals(StringUtils.removeStart("domain.com", "www."), "domain.com");
1901 assertEquals(StringUtils.removeStart("domain.com", ""), "domain.com");
1902 assertEquals(StringUtils.removeStart("domain.com", null), "domain.com");
1903 }
1904
1905 @Test
1906 public void testRepeat_CharInt() {
1907 assertEquals("zzz", StringUtils.repeat('z', 3));
1908 assertEquals("", StringUtils.repeat('z', 0));
1909 assertEquals("", StringUtils.repeat('z', -2));
1910 }
1911
1912 @Test
1913 public void testRepeat_StringInt() {
1914 assertNull(StringUtils.repeat(null, 2));
1915 assertEquals("", StringUtils.repeat("ab", 0));
1916 assertEquals("", StringUtils.repeat("", 3));
1917 assertEquals("aaa", StringUtils.repeat("a", 3));
1918 assertEquals("", StringUtils.repeat("a", -2));
1919 assertEquals("ababab", StringUtils.repeat("ab", 3));
1920 assertEquals("abcabcabc", StringUtils.repeat("abc", 3));
1921 final String str = StringUtils.repeat("a", 10000);
1922 assertEquals(10000, str.length());
1923 assertTrue(StringUtils.containsOnly(str, 'a'));
1924 }
1925
1926 @Test
1927 public void testRepeat_StringStringInt() {
1928 assertNull(StringUtils.repeat(null, null, 2));
1929 assertNull(StringUtils.repeat(null, "x", 2));
1930 assertEquals("", StringUtils.repeat("", null, 2));
1931
1932 assertEquals("", StringUtils.repeat("ab", "", 0));
1933 assertEquals("", StringUtils.repeat("", "", 2));
1934
1935 assertEquals("xx", StringUtils.repeat("", "x", 3));
1936
1937 assertEquals("?, ?, ?", StringUtils.repeat("?", ", ", 3));
1938 }
1939
1940
1941
1942
1943 @Test
1944 public void testReplace_StringStringArrayStringArray() {
1945
1946 assertNull(StringUtils.replaceEach(null, new String[]{"a"}, new String[]{"b"}));
1947 assertEquals(StringUtils.replaceEach("", new String[]{"a"}, new String[]{"b"}), "");
1948 assertEquals(StringUtils.replaceEach("aba", null, null), "aba");
1949 assertEquals(StringUtils.replaceEach("aba", new String[0], null), "aba");
1950 assertEquals(StringUtils.replaceEach("aba", null, new String[0]), "aba");
1951 assertEquals(StringUtils.replaceEach("aba", new String[]{"a"}, null), "aba");
1952
1953 assertEquals(StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""}), "b");
1954 assertEquals(StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"}), "aba");
1955 assertEquals(StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}), "wcte");
1956 assertEquals(StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}), "dcte");
1957
1958
1959 assertEquals("bcc", StringUtils.replaceEach("abc", new String[]{"a", "b"}, new String[]{"b", "c"}));
1960 assertEquals("q651.506bera", StringUtils.replaceEach("d216.102oren",
1961 new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
1962 "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D",
1963 "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
1964 "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9"},
1965 new String[]{"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a",
1966 "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "N", "O", "P", "Q",
1967 "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G",
1968 "H", "I", "J", "K", "L", "M", "5", "6", "7", "8", "9", "1", "2", "3", "4"}));
1969
1970
1971 assertEquals(StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{null}), "aba");
1972 assertEquals(StringUtils.replaceEach("aba", new String[]{"a", "b"}, new String[]{"c", null}), "cbc");
1973
1974 assertThrows(
1975 IllegalArgumentException.class,
1976 () -> StringUtils.replaceEach("abba", new String[]{"a"}, new String[]{"b", "a"}),
1977 "StringUtils.replaceEach(String, String[], String[]) expecting IllegalArgumentException");
1978 }
1979
1980
1981
1982
1983 @Test
1984 public void testReplace_StringStringArrayStringArrayBoolean() {
1985
1986 assertNull(StringUtils.replaceEachRepeatedly(null, new String[]{"a"}, new String[]{"b"}));
1987 assertEquals("", StringUtils.replaceEachRepeatedly("", new String[]{"a"}, new String[]{"b"}));
1988 assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", null, null));
1989 assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", new String[0], null));
1990 assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", null, new String[0]));
1991 assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", new String[0], null));
1992
1993 assertEquals("b", StringUtils.replaceEachRepeatedly("aba", new String[]{"a"}, new String[]{""}));
1994 assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", new String[]{null}, new String[]{"a"}));
1995 assertEquals("wcte", StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}));
1996 assertEquals("tcte", StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}));
1997 assertEquals("blaan", StringUtils.replaceEachRepeatedly("blllaan", new String[]{"llaan"}, new String[]{"laan"}) );
1998
1999 assertThrows(
2000 IllegalStateException.class,
2001 () -> StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}),
2002 "Should be a circular reference");
2003
2004
2005 }
2006
2007 @Test
2008 public void testReplace_StringStringString() {
2009 assertNull(StringUtils.replace(null, null, null));
2010 assertNull(StringUtils.replace(null, null, "any"));
2011 assertNull(StringUtils.replace(null, "any", null));
2012 assertNull(StringUtils.replace(null, "any", "any"));
2013
2014 assertEquals("", StringUtils.replace("", null, null));
2015 assertEquals("", StringUtils.replace("", null, "any"));
2016 assertEquals("", StringUtils.replace("", "any", null));
2017 assertEquals("", StringUtils.replace("", "any", "any"));
2018
2019 assertEquals("FOO", StringUtils.replace("FOO", "", "any"));
2020 assertEquals("FOO", StringUtils.replace("FOO", null, "any"));
2021 assertEquals("FOO", StringUtils.replace("FOO", "F", null));
2022 assertEquals("FOO", StringUtils.replace("FOO", null, null));
2023
2024 assertEquals("", StringUtils.replace("foofoofoo", "foo", ""));
2025 assertEquals("barbarbar", StringUtils.replace("foofoofoo", "foo", "bar"));
2026 assertEquals("farfarfar", StringUtils.replace("foofoofoo", "oo", "ar"));
2027 }
2028
2029 @Test
2030 public void testReplace_StringStringStringInt() {
2031 assertNull(StringUtils.replace(null, null, null, 2));
2032 assertNull(StringUtils.replace(null, null, "any", 2));
2033 assertNull(StringUtils.replace(null, "any", null, 2));
2034 assertNull(StringUtils.replace(null, "any", "any", 2));
2035
2036 assertEquals("", StringUtils.replace("", null, null, 2));
2037 assertEquals("", StringUtils.replace("", null, "any", 2));
2038 assertEquals("", StringUtils.replace("", "any", null, 2));
2039 assertEquals("", StringUtils.replace("", "any", "any", 2));
2040
2041 final String str = new String(new char[]{'o', 'o', 'f', 'o', 'o'});
2042 assertSame(str, StringUtils.replace(str, "x", "", -1));
2043
2044 assertEquals("f", StringUtils.replace("oofoo", "o", "", -1));
2045 assertEquals("oofoo", StringUtils.replace("oofoo", "o", "", 0));
2046 assertEquals("ofoo", StringUtils.replace("oofoo", "o", "", 1));
2047 assertEquals("foo", StringUtils.replace("oofoo", "o", "", 2));
2048 assertEquals("fo", StringUtils.replace("oofoo", "o", "", 3));
2049 assertEquals("f", StringUtils.replace("oofoo", "o", "", 4));
2050
2051 assertEquals("f", StringUtils.replace("oofoo", "o", "", -5));
2052 assertEquals("f", StringUtils.replace("oofoo", "o", "", 1000));
2053 }
2054
2055 @Test
2056 public void testReplaceAll_StringStringString() {
2057 assertNull(StringUtils.replaceAll(null, "", ""));
2058
2059 assertEquals("any", StringUtils.replaceAll("any", null, ""));
2060 assertEquals("any", StringUtils.replaceAll("any", "", null));
2061
2062 assertEquals("zzz", StringUtils.replaceAll("", "", "zzz"));
2063 assertEquals("zzz", StringUtils.replaceAll("", ".*", "zzz"));
2064 assertEquals("", StringUtils.replaceAll("", ".+", "zzz"));
2065 assertEquals("ZZaZZbZZcZZ", StringUtils.replaceAll("abc", "", "ZZ"));
2066
2067 assertEquals("z\nz", StringUtils.replaceAll("<__>\n<__>", "<.*>", "z"));
2068 assertEquals("z", StringUtils.replaceAll("<__>\n<__>", "(?s)<.*>", "z"));
2069
2070 assertEquals("ABC___123", StringUtils.replaceAll("ABCabc123", "[a-z]", "_"));
2071 assertEquals("ABC_123", StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "_"));
2072 assertEquals("ABC123", StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", ""));
2073 assertEquals("Lorem_ipsum_dolor_sit",
2074 StringUtils.replaceAll("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2"));
2075
2076 assertThrows(
2077 PatternSyntaxException.class,
2078 () -> StringUtils.replaceAll("any", "{badRegexSyntax}", ""),
2079 "StringUtils.replaceAll expecting PatternSyntaxException");
2080 }
2081
2082 @Test
2083 public void testReplaceChars_StringCharChar() {
2084 assertNull(StringUtils.replaceChars(null, 'b', 'z'));
2085 assertEquals("", StringUtils.replaceChars("", 'b', 'z'));
2086 assertEquals("azcza", StringUtils.replaceChars("abcba", 'b', 'z'));
2087 assertEquals("abcba", StringUtils.replaceChars("abcba", 'x', 'z'));
2088 }
2089
2090 @Test
2091 public void testReplaceChars_StringStringString() {
2092 assertNull(StringUtils.replaceChars(null, null, null));
2093 assertNull(StringUtils.replaceChars(null, "", null));
2094 assertNull(StringUtils.replaceChars(null, "a", null));
2095 assertNull(StringUtils.replaceChars(null, null, ""));
2096 assertNull(StringUtils.replaceChars(null, null, "x"));
2097
2098 assertEquals("", StringUtils.replaceChars("", null, null));
2099 assertEquals("", StringUtils.replaceChars("", "", null));
2100 assertEquals("", StringUtils.replaceChars("", "a", null));
2101 assertEquals("", StringUtils.replaceChars("", null, ""));
2102 assertEquals("", StringUtils.replaceChars("", null, "x"));
2103
2104 assertEquals("abc", StringUtils.replaceChars("abc", null, null));
2105 assertEquals("abc", StringUtils.replaceChars("abc", null, ""));
2106 assertEquals("abc", StringUtils.replaceChars("abc", null, "x"));
2107
2108 assertEquals("abc", StringUtils.replaceChars("abc", "", null));
2109 assertEquals("abc", StringUtils.replaceChars("abc", "", ""));
2110 assertEquals("abc", StringUtils.replaceChars("abc", "", "x"));
2111
2112 assertEquals("ac", StringUtils.replaceChars("abc", "b", null));
2113 assertEquals("ac", StringUtils.replaceChars("abc", "b", ""));
2114 assertEquals("axc", StringUtils.replaceChars("abc", "b", "x"));
2115
2116 assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yz"));
2117 assertEquals("ayya", StringUtils.replaceChars("abcba", "bc", "y"));
2118 assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yzx"));
2119
2120 assertEquals("abcba", StringUtils.replaceChars("abcba", "z", "w"));
2121 assertSame("abcba", StringUtils.replaceChars("abcba", "z", "w"));
2122
2123
2124 assertEquals("jelly", StringUtils.replaceChars("hello", "ho", "jy"));
2125 assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yz"));
2126 assertEquals("ayya", StringUtils.replaceChars("abcba", "bc", "y"));
2127 assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yzx"));
2128
2129
2130 assertEquals("bcc", StringUtils.replaceChars("abc", "ab", "bc"));
2131 assertEquals("q651.506bera", StringUtils.replaceChars("d216.102oren",
2132 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789",
2133 "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM567891234"));
2134 }
2135
2136 @Test
2137 public void testReplaceFirst_StringStringString() {
2138 assertNull(StringUtils.replaceFirst(null, "", ""));
2139
2140 assertEquals("any", StringUtils.replaceFirst("any", null, ""));
2141 assertEquals("any", StringUtils.replaceFirst("any", "", null));
2142
2143 assertEquals("zzz", StringUtils.replaceFirst("", "", "zzz"));
2144 assertEquals("zzz", StringUtils.replaceFirst("", ".*", "zzz"));
2145 assertEquals("", StringUtils.replaceFirst("", ".+", "zzz"));
2146 assertEquals("ZZabc", StringUtils.replaceFirst("abc", "", "ZZ"));
2147
2148 assertEquals("z\n<__>", StringUtils.replaceFirst("<__>\n<__>", "<.*>", "z"));
2149 assertEquals("z", StringUtils.replaceFirst("<__>\n<__>", "(?s)<.*>", "z"));
2150
2151 assertEquals("ABC_bc123", StringUtils.replaceFirst("ABCabc123", "[a-z]", "_"));
2152 assertEquals("ABC_123abc", StringUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "_"));
2153 assertEquals("ABC123abc", StringUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", ""));
2154 assertEquals("Lorem_ipsum dolor sit",
2155 StringUtils.replaceFirst("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2"));
2156
2157 assertThrows(
2158 PatternSyntaxException.class,
2159 () -> StringUtils.replaceFirst("any", "{badRegexSyntax}", ""),
2160 "StringUtils.replaceFirst expecting PatternSyntaxException");
2161 }
2162
2163 @Test
2164 public void testReplaceIgnoreCase_StringStringString() {
2165 assertNull(StringUtils.replaceIgnoreCase(null, null, null));
2166 assertNull(StringUtils.replaceIgnoreCase(null, null, "any"));
2167 assertNull(StringUtils.replaceIgnoreCase(null, "any", null));
2168 assertNull(StringUtils.replaceIgnoreCase(null, "any", "any"));
2169
2170 assertEquals("", StringUtils.replaceIgnoreCase("", null, null));
2171 assertEquals("", StringUtils.replaceIgnoreCase("", null, "any"));
2172 assertEquals("", StringUtils.replaceIgnoreCase("", "any", null));
2173 assertEquals("", StringUtils.replaceIgnoreCase("", "any", "any"));
2174
2175 assertEquals("FOO", StringUtils.replaceIgnoreCase("FOO", "", "any"));
2176 assertEquals("FOO", StringUtils.replaceIgnoreCase("FOO", null, "any"));
2177 assertEquals("FOO", StringUtils.replaceIgnoreCase("FOO", "F", null));
2178 assertEquals("FOO", StringUtils.replaceIgnoreCase("FOO", null, null));
2179
2180 assertEquals("", StringUtils.replaceIgnoreCase("foofoofoo", "foo", ""));
2181 assertEquals("barbarbar", StringUtils.replaceIgnoreCase("foofoofoo", "foo", "bar"));
2182 assertEquals("farfarfar", StringUtils.replaceIgnoreCase("foofoofoo", "oo", "ar"));
2183
2184
2185 assertEquals("", StringUtils.replaceIgnoreCase("foofoofoo", "FOO", ""));
2186 assertEquals("barbarbar", StringUtils.replaceIgnoreCase("fooFOOfoo", "foo", "bar"));
2187 assertEquals("farfarfar", StringUtils.replaceIgnoreCase("foofOOfoo", "OO", "ar"));
2188 }
2189
2190 @Test
2191 public void testReplaceIgnoreCase_StringStringStringInt() {
2192 assertNull(StringUtils.replaceIgnoreCase(null, null, null, 2));
2193 assertNull(StringUtils.replaceIgnoreCase(null, null, "any", 2));
2194 assertNull(StringUtils.replaceIgnoreCase(null, "any", null, 2));
2195 assertNull(StringUtils.replaceIgnoreCase(null, "any", "any", 2));
2196
2197 assertEquals("", StringUtils.replaceIgnoreCase("", null, null, 2));
2198 assertEquals("", StringUtils.replaceIgnoreCase("", null, "any", 2));
2199 assertEquals("", StringUtils.replaceIgnoreCase("", "any", null, 2));
2200 assertEquals("", StringUtils.replaceIgnoreCase("", "any", "any", 2));
2201
2202 final String str = new String(new char[] { 'o', 'o', 'f', 'o', 'o' });
2203 assertSame(str, StringUtils.replaceIgnoreCase(str, "x", "", -1));
2204
2205 assertEquals("f", StringUtils.replaceIgnoreCase("oofoo", "o", "", -1));
2206 assertEquals("oofoo", StringUtils.replaceIgnoreCase("oofoo", "o", "", 0));
2207 assertEquals("ofoo", StringUtils.replaceIgnoreCase("oofoo", "o", "", 1));
2208 assertEquals("foo", StringUtils.replaceIgnoreCase("oofoo", "o", "", 2));
2209 assertEquals("fo", StringUtils.replaceIgnoreCase("oofoo", "o", "", 3));
2210 assertEquals("f", StringUtils.replaceIgnoreCase("oofoo", "o", "", 4));
2211
2212 assertEquals("f", StringUtils.replaceIgnoreCase("oofoo", "o", "", -5));
2213 assertEquals("f", StringUtils.replaceIgnoreCase("oofoo", "o", "", 1000));
2214
2215
2216 assertEquals("f", StringUtils.replaceIgnoreCase("oofoo", "O", "", -1));
2217 assertEquals("oofoo", StringUtils.replaceIgnoreCase("oofoo", "O", "", 0));
2218 assertEquals("ofoo", StringUtils.replaceIgnoreCase("oofoo", "O", "", 1));
2219 assertEquals("foo", StringUtils.replaceIgnoreCase("oofoo", "O", "", 2));
2220 assertEquals("fo", StringUtils.replaceIgnoreCase("oofoo", "O", "", 3));
2221 assertEquals("f", StringUtils.replaceIgnoreCase("oofoo", "O", "", 4));
2222
2223 assertEquals("f", StringUtils.replaceIgnoreCase("oofoo", "O", "", -5));
2224 assertEquals("f", StringUtils.replaceIgnoreCase("oofoo", "O", "", 1000));
2225 }
2226
2227 @Test
2228 public void testReplaceOnce_StringStringString() {
2229 assertNull(StringUtils.replaceOnce(null, null, null));
2230 assertNull(StringUtils.replaceOnce(null, null, "any"));
2231 assertNull(StringUtils.replaceOnce(null, "any", null));
2232 assertNull(StringUtils.replaceOnce(null, "any", "any"));
2233
2234 assertEquals("", StringUtils.replaceOnce("", null, null));
2235 assertEquals("", StringUtils.replaceOnce("", null, "any"));
2236 assertEquals("", StringUtils.replaceOnce("", "any", null));
2237 assertEquals("", StringUtils.replaceOnce("", "any", "any"));
2238
2239 assertEquals("FOO", StringUtils.replaceOnce("FOO", "", "any"));
2240 assertEquals("FOO", StringUtils.replaceOnce("FOO", null, "any"));
2241 assertEquals("FOO", StringUtils.replaceOnce("FOO", "F", null));
2242 assertEquals("FOO", StringUtils.replaceOnce("FOO", null, null));
2243
2244 assertEquals("foofoo", StringUtils.replaceOnce("foofoofoo", "foo", ""));
2245 }
2246
2247 @Test
2248 public void testReplaceOnceIgnoreCase_StringStringString() {
2249 assertNull(StringUtils.replaceOnceIgnoreCase(null, null, null));
2250 assertNull(StringUtils.replaceOnceIgnoreCase(null, null, "any"));
2251 assertNull(StringUtils.replaceOnceIgnoreCase(null, "any", null));
2252 assertNull(StringUtils.replaceOnceIgnoreCase(null, "any", "any"));
2253
2254 assertEquals("", StringUtils.replaceOnceIgnoreCase("", null, null));
2255 assertEquals("", StringUtils.replaceOnceIgnoreCase("", null, "any"));
2256 assertEquals("", StringUtils.replaceOnceIgnoreCase("", "any", null));
2257 assertEquals("", StringUtils.replaceOnceIgnoreCase("", "any", "any"));
2258
2259 assertEquals("FOO", StringUtils.replaceOnceIgnoreCase("FOO", "", "any"));
2260 assertEquals("FOO", StringUtils.replaceOnceIgnoreCase("FOO", null, "any"));
2261 assertEquals("FOO", StringUtils.replaceOnceIgnoreCase("FOO", "F", null));
2262 assertEquals("FOO", StringUtils.replaceOnceIgnoreCase("FOO", null, null));
2263
2264 assertEquals("foofoo", StringUtils.replaceOnceIgnoreCase("foofoofoo", "foo", ""));
2265
2266
2267 assertEquals("Foofoo", StringUtils.replaceOnceIgnoreCase("FoOFoofoo", "foo", ""));
2268 }
2269
2270 @Test
2271 public void testReplacePattern_StringStringString() {
2272 assertNull(StringUtils.replacePattern(null, "", ""));
2273 assertEquals("any", StringUtils.replacePattern("any", null, ""));
2274 assertEquals("any", StringUtils.replacePattern("any", "", null));
2275
2276 assertEquals("zzz", StringUtils.replacePattern("", "", "zzz"));
2277 assertEquals("zzz", StringUtils.replacePattern("", ".*", "zzz"));
2278 assertEquals("", StringUtils.replacePattern("", ".+", "zzz"));
2279
2280 assertEquals("z", StringUtils.replacePattern("<__>\n<__>", "<.*>", "z"));
2281 assertEquals("z", StringUtils.replacePattern("<__>\\n<__>", "<.*>", "z"));
2282 assertEquals("X", StringUtils.replacePattern("<A>\nxy\n</A>", "<A>.*</A>", "X"));
2283
2284 assertEquals("ABC___123", StringUtils.replacePattern("ABCabc123", "[a-z]", "_"));
2285 assertEquals("ABC_123", StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "_"));
2286 assertEquals("ABC123", StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", ""));
2287 assertEquals("Lorem_ipsum_dolor_sit",
2288 StringUtils.replacePattern("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2"));
2289 }
2290
2291 @Test
2292 public void testReverse_String() {
2293 assertNull(StringUtils.reverse(null));
2294 assertEquals("", StringUtils.reverse(""));
2295 assertEquals("sdrawkcab", StringUtils.reverse("backwards"));
2296 }
2297
2298 @Test
2299 public void testReverseDelimited_StringChar() {
2300 assertNull(StringUtils.reverseDelimited(null, '.'));
2301 assertEquals("", StringUtils.reverseDelimited("", '.'));
2302 assertEquals("c.b.a", StringUtils.reverseDelimited("a.b.c", '.'));
2303 assertEquals("a b c", StringUtils.reverseDelimited("a b c", '.'));
2304 assertEquals("", StringUtils.reverseDelimited("", '.'));
2305 }
2306
2307 @Test
2308 public void testRightPad_StringInt() {
2309 assertNull(StringUtils.rightPad(null, 5));
2310 assertEquals(" ", StringUtils.rightPad("", 5));
2311 assertEquals("abc ", StringUtils.rightPad("abc", 5));
2312 assertEquals("abc", StringUtils.rightPad("abc", 2));
2313 assertEquals("abc", StringUtils.rightPad("abc", -1));
2314 }
2315
2316 @Test
2317 public void testRightPad_StringIntChar() {
2318 assertNull(StringUtils.rightPad(null, 5, ' '));
2319 assertEquals(" ", StringUtils.rightPad("", 5, ' '));
2320 assertEquals("abc ", StringUtils.rightPad("abc", 5, ' '));
2321 assertEquals("abc", StringUtils.rightPad("abc", 2, ' '));
2322 assertEquals("abc", StringUtils.rightPad("abc", -1, ' '));
2323 assertEquals("abcxx", StringUtils.rightPad("abc", 5, 'x'));
2324 final String str = StringUtils.rightPad("aaa", 10000, 'a');
2325 assertEquals(10000, str.length());
2326 assertTrue(StringUtils.containsOnly(str, 'a'));
2327 }
2328
2329 @Test
2330 public void testRightPad_StringIntString() {
2331 assertNull(StringUtils.rightPad(null, 5, "-+"));
2332 assertEquals(" ", StringUtils.rightPad("", 5, " "));
2333 assertNull(StringUtils.rightPad(null, 8, null));
2334 assertEquals("abc-+-+", StringUtils.rightPad("abc", 7, "-+"));
2335 assertEquals("abc-+~", StringUtils.rightPad("abc", 6, "-+~"));
2336 assertEquals("abc-+", StringUtils.rightPad("abc", 5, "-+~"));
2337 assertEquals("abc", StringUtils.rightPad("abc", 2, " "));
2338 assertEquals("abc", StringUtils.rightPad("abc", -1, " "));
2339 assertEquals("abc ", StringUtils.rightPad("abc", 5, null));
2340 assertEquals("abc ", StringUtils.rightPad("abc", 5, ""));
2341 }
2342
2343 @Test
2344 public void testRotate_StringInt() {
2345 assertNull(StringUtils.rotate(null, 1));
2346 assertEquals("", StringUtils.rotate("", 1));
2347 assertEquals("abcdefg", StringUtils.rotate("abcdefg", 0));
2348 assertEquals("fgabcde", StringUtils.rotate("abcdefg", 2));
2349 assertEquals("cdefgab", StringUtils.rotate("abcdefg", -2));
2350 assertEquals("abcdefg", StringUtils.rotate("abcdefg", 7));
2351 assertEquals("abcdefg", StringUtils.rotate("abcdefg", -7));
2352 assertEquals("fgabcde", StringUtils.rotate("abcdefg", 9));
2353 assertEquals("cdefgab", StringUtils.rotate("abcdefg", -9));
2354 assertEquals("efgabcd", StringUtils.rotate("abcdefg", 17));
2355 assertEquals("defgabc", StringUtils.rotate("abcdefg", -17));
2356 }
2357
2358 @Test
2359 public void testSplit_String() {
2360 assertNull(StringUtils.split(null));
2361 assertEquals(0, StringUtils.split("").length);
2362
2363 String str = "a b .c";
2364 String[] res = StringUtils.split(str);
2365 assertEquals(3, res.length);
2366 assertEquals("a", res[0]);
2367 assertEquals("b", res[1]);
2368 assertEquals(".c", res[2]);
2369
2370 str = " a ";
2371 res = StringUtils.split(str);
2372 assertEquals(1, res.length);
2373 assertEquals("a", res[0]);
2374
2375 str = "a" + WHITESPACE + "b" + NON_WHITESPACE + "c";
2376 res = StringUtils.split(str);
2377 assertEquals(2, res.length);
2378 assertEquals("a", res[0]);
2379 assertEquals("b" + NON_WHITESPACE + "c", res[1]);
2380 }
2381
2382 @Test
2383 public void testSplit_StringChar() {
2384 assertNull(StringUtils.split(null, '.'));
2385 assertEquals(0, StringUtils.split("", '.').length);
2386
2387 String str = "a.b.. c";
2388 String[] res = StringUtils.split(str, '.');
2389 assertEquals(3, res.length);
2390 assertEquals("a", res[0]);
2391 assertEquals("b", res[1]);
2392 assertEquals(" c", res[2]);
2393
2394 str = ".a.";
2395 res = StringUtils.split(str, '.');
2396 assertEquals(1, res.length);
2397 assertEquals("a", res[0]);
2398
2399 str = "a b c";
2400 res = StringUtils.split(str, ' ');
2401 assertEquals(3, res.length);
2402 assertEquals("a", res[0]);
2403 assertEquals("b", res[1]);
2404 assertEquals("c", res[2]);
2405 }
2406
2407 @Test
2408 public void testSplit_StringString_StringStringInt() {
2409 assertNull(StringUtils.split(null, "."));
2410 assertNull(StringUtils.split(null, ".", 3));
2411
2412 assertEquals(0, StringUtils.split("", ".").length);
2413 assertEquals(0, StringUtils.split("", ".", 3).length);
2414
2415 innerTestSplit('.', ".", ' ');
2416 innerTestSplit('.', ".", ',');
2417 innerTestSplit('.', ".,", 'x');
2418 for (int i = 0; i < WHITESPACE.length(); i++) {
2419 for (int j = 0; j < NON_WHITESPACE.length(); j++) {
2420 innerTestSplit(WHITESPACE.charAt(i), null, NON_WHITESPACE.charAt(j));
2421 innerTestSplit(WHITESPACE.charAt(i), String.valueOf(WHITESPACE.charAt(i)), NON_WHITESPACE.charAt(j));
2422 }
2423 }
2424
2425 String[] results;
2426 final String[] expectedResults = {"ab", "de fg"};
2427 results = StringUtils.split("ab de fg", null, 2);
2428 assertEquals(expectedResults.length, results.length);
2429 for (int i = 0; i < expectedResults.length; i++) {
2430 assertEquals(expectedResults[i], results[i]);
2431 }
2432
2433 final String[] expectedResults2 = {"ab", "cd:ef"};
2434 results = StringUtils.split("ab:cd:ef", ":", 2);
2435 assertEquals(expectedResults2.length, results.length);
2436 for (int i = 0; i < expectedResults2.length; i++) {
2437 assertEquals(expectedResults2[i], results[i]);
2438 }
2439 }
2440
2441 @Test
2442 public void testSplitByCharacterType() {
2443 assertNull(StringUtils.splitByCharacterType(null));
2444 assertEquals(0, StringUtils.splitByCharacterType("").length);
2445
2446 assertTrue(Objects.deepEquals(new String[]{"ab", " ", "de", " ",
2447 "fg"}, StringUtils.splitByCharacterType("ab de fg")));
2448
2449 assertTrue(Objects.deepEquals(new String[]{"ab", " ", "de", " ",
2450 "fg"}, StringUtils.splitByCharacterType("ab de fg")));
2451
2452 assertTrue(Objects.deepEquals(new String[]{"ab", ":", "cd", ":",
2453 "ef"}, StringUtils.splitByCharacterType("ab:cd:ef")));
2454
2455 assertTrue(Objects.deepEquals(new String[]{"number", "5"},
2456 StringUtils.splitByCharacterType("number5")));
2457
2458 assertTrue(Objects.deepEquals(new String[]{"foo", "B", "ar"},
2459 StringUtils.splitByCharacterType("fooBar")));
2460
2461 assertTrue(Objects.deepEquals(new String[]{"foo", "200", "B", "ar"},
2462 StringUtils.splitByCharacterType("foo200Bar")));
2463
2464 assertTrue(Objects.deepEquals(new String[]{"ASFR", "ules"},
2465 StringUtils.splitByCharacterType("ASFRules")));
2466 }
2467
2468 @Test
2469 public void testSplitByCharacterTypeCamelCase() {
2470 assertNull(StringUtils.splitByCharacterTypeCamelCase(null));
2471 assertEquals(0, StringUtils.splitByCharacterTypeCamelCase("").length);
2472
2473 assertTrue(Objects.deepEquals(new String[]{"ab", " ", "de", " ",
2474 "fg"}, StringUtils.splitByCharacterTypeCamelCase("ab de fg")));
2475
2476 assertTrue(Objects.deepEquals(new String[]{"ab", " ", "de", " ",
2477 "fg"}, StringUtils.splitByCharacterTypeCamelCase("ab de fg")));
2478
2479 assertTrue(Objects.deepEquals(new String[]{"ab", ":", "cd", ":",
2480 "ef"}, StringUtils.splitByCharacterTypeCamelCase("ab:cd:ef")));
2481
2482 assertTrue(Objects.deepEquals(new String[]{"number", "5"},
2483 StringUtils.splitByCharacterTypeCamelCase("number5")));
2484
2485 assertTrue(Objects.deepEquals(new String[]{"foo", "Bar"},
2486 StringUtils.splitByCharacterTypeCamelCase("fooBar")));
2487
2488 assertTrue(Objects.deepEquals(new String[]{"foo", "200", "Bar"},
2489 StringUtils.splitByCharacterTypeCamelCase("foo200Bar")));
2490
2491 assertTrue(Objects.deepEquals(new String[]{"ASF", "Rules"},
2492 StringUtils.splitByCharacterTypeCamelCase("ASFRules")));
2493 }
2494
2495 @Test
2496 public void testSplitByWholeSeparatorPreserveAllTokens_StringString() {
2497 assertArrayEquals(null, StringUtils.splitByWholeSeparatorPreserveAllTokens(null, "."));
2498
2499 assertEquals(0, StringUtils.splitByWholeSeparatorPreserveAllTokens("", ".").length);
2500
2501
2502 String input = "ab de fg";
2503 String[] expected = {"ab", "", "", "de", "fg"};
2504
2505 String[] actual = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, null);
2506 assertEquals(expected.length, actual.length);
2507 for (int i = 0; i < actual.length; i += 1) {
2508 assertEquals(expected[i], actual[i]);
2509 }
2510
2511
2512 input = "1::2:::3::::4";
2513 expected = new String[]{"1", "", "2", "", "", "3", "", "", "", "4"};
2514
2515 actual = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, ":");
2516 assertEquals(expected.length, actual.length);
2517 for (int i = 0; i < actual.length; i += 1) {
2518 assertEquals(expected[i], actual[i]);
2519 }
2520
2521
2522 input = "1::2:::3::::4";
2523 expected = new String[]{"1", "2", ":3", "", "4"};
2524
2525 actual = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, "::");
2526 assertEquals(expected.length, actual.length);
2527 for (int i = 0; i < actual.length; i += 1) {
2528 assertEquals(expected[i], actual[i]);
2529 }
2530 }
2531
2532 @Test
2533 public void testSplitByWholeSeparatorPreserveAllTokens_StringStringInt() {
2534 assertArrayEquals(null, StringUtils.splitByWholeSeparatorPreserveAllTokens(null, ".", -1));
2535
2536 assertEquals(0, StringUtils.splitByWholeSeparatorPreserveAllTokens("", ".", -1).length);
2537
2538
2539 String input = "ab de fg";
2540 String[] expected = {"ab", "", "", "de", "fg"};
2541
2542 String[] actual = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, null, -1);
2543 assertEquals(expected.length, actual.length);
2544 for (int i = 0; i < actual.length; i += 1) {
2545 assertEquals(expected[i], actual[i]);
2546 }
2547
2548
2549 input = "1::2:::3::::4";
2550 expected = new String[]{"1", "", "2", "", "", "3", "", "", "", "4"};
2551
2552 actual = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, ":", -1);
2553 assertEquals(expected.length, actual.length);
2554 for (int i = 0; i < actual.length; i += 1) {
2555 assertEquals(expected[i], actual[i]);
2556 }
2557
2558
2559 input = "1::2:::3::::4";
2560 expected = new String[]{"1", "2", ":3", "", "4"};
2561
2562 actual = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, "::", -1);
2563 assertEquals(expected.length, actual.length);
2564 for (int i = 0; i < actual.length; i += 1) {
2565 assertEquals(expected[i], actual[i]);
2566 }
2567
2568
2569 input = "1::2::3:4";
2570 expected = new String[]{"1", "", "2", ":3:4"};
2571
2572 actual = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, ":", 4);
2573 assertEquals(expected.length, actual.length);
2574 for (int i = 0; i < actual.length; i += 1) {
2575 assertEquals(expected[i], actual[i]);
2576 }
2577 }
2578
2579 @Test
2580 public void testSplitByWholeString_StringStringBoolean() {
2581 assertArrayEquals(null, StringUtils.splitByWholeSeparator(null, "."));
2582
2583 assertEquals(0, StringUtils.splitByWholeSeparator("", ".").length);
2584
2585 final String stringToSplitOnNulls = "ab de fg";
2586 final String[] splitOnNullExpectedResults = {"ab", "de", "fg"};
2587
2588 final String[] splitOnNullResults = StringUtils.splitByWholeSeparator(stringToSplitOnNulls, null);
2589 assertEquals(splitOnNullExpectedResults.length, splitOnNullResults.length);
2590 for (int i = 0; i < splitOnNullExpectedResults.length; i += 1) {
2591 assertEquals(splitOnNullExpectedResults[i], splitOnNullResults[i]);
2592 }
2593
2594 final String stringToSplitOnCharactersAndString = "abstemiouslyaeiouyabstemiously";
2595
2596 final String[] splitOnStringExpectedResults = {"abstemiously", "abstemiously"};
2597 final String[] splitOnStringResults = StringUtils.splitByWholeSeparator(stringToSplitOnCharactersAndString, "aeiouy");
2598 assertEquals(splitOnStringExpectedResults.length, splitOnStringResults.length);
2599 for (int i = 0; i < splitOnStringExpectedResults.length; i += 1) {
2600 assertEquals(splitOnStringExpectedResults[i], splitOnStringResults[i]);
2601 }
2602
2603 final String[] splitWithMultipleSeparatorExpectedResults = {"ab", "cd", "ef"};
2604 final String[] splitWithMultipleSeparator = StringUtils.splitByWholeSeparator("ab:cd::ef", ":");
2605 assertEquals(splitWithMultipleSeparatorExpectedResults.length, splitWithMultipleSeparator.length);
2606 for (int i = 0; i < splitWithMultipleSeparatorExpectedResults.length; i++) {
2607 assertEquals(splitWithMultipleSeparatorExpectedResults[i], splitWithMultipleSeparator[i]);
2608 }
2609 }
2610
2611 @Test
2612 public void testSplitByWholeString_StringStringBooleanInt() {
2613 assertArrayEquals(null, StringUtils.splitByWholeSeparator(null, ".", 3));
2614
2615 assertEquals(0, StringUtils.splitByWholeSeparator("", ".", 3).length);
2616
2617 final String stringToSplitOnNulls = "ab de fg";
2618 final String[] splitOnNullExpectedResults = {"ab", "de fg"};
2619
2620
2621 final String[] splitOnNullResults = StringUtils.splitByWholeSeparator(stringToSplitOnNulls, null, 2);
2622 assertEquals(splitOnNullExpectedResults.length, splitOnNullResults.length);
2623 for (int i = 0; i < splitOnNullExpectedResults.length; i += 1) {
2624 assertEquals(splitOnNullExpectedResults[i], splitOnNullResults[i]);
2625 }
2626
2627 final String stringToSplitOnCharactersAndString = "abstemiouslyaeiouyabstemiouslyaeiouyabstemiously";
2628
2629 final String[] splitOnStringExpectedResults = {"abstemiously", "abstemiouslyaeiouyabstemiously"};
2630
2631 final String[] splitOnStringResults = StringUtils.splitByWholeSeparator(stringToSplitOnCharactersAndString, "aeiouy", 2);
2632 assertEquals(splitOnStringExpectedResults.length, splitOnStringResults.length);
2633 for (int i = 0; i < splitOnStringExpectedResults.length; i++) {
2634 assertEquals(splitOnStringExpectedResults[i], splitOnStringResults[i]);
2635 }
2636 }
2637
2638 @Test
2639 public void testSplitPreserveAllTokens_String() {
2640 assertNull(StringUtils.splitPreserveAllTokens(null));
2641 assertEquals(0, StringUtils.splitPreserveAllTokens("").length);
2642
2643 String str = "abc def";
2644 String[] res = StringUtils.splitPreserveAllTokens(str);
2645 assertEquals(2, res.length);
2646 assertEquals("abc", res[0]);
2647 assertEquals("def", res[1]);
2648
2649 str = "abc def";
2650 res = StringUtils.splitPreserveAllTokens(str);
2651 assertEquals(3, res.length);
2652 assertEquals("abc", res[0]);
2653 assertEquals("", res[1]);
2654 assertEquals("def", res[2]);
2655
2656 str = " abc ";
2657 res = StringUtils.splitPreserveAllTokens(str);
2658 assertEquals(3, res.length);
2659 assertEquals("", res[0]);
2660 assertEquals("abc", res[1]);
2661 assertEquals("", res[2]);
2662
2663 str = "a b .c";
2664 res = StringUtils.splitPreserveAllTokens(str);
2665 assertEquals(3, res.length);
2666 assertEquals("a", res[0]);
2667 assertEquals("b", res[1]);
2668 assertEquals(".c", res[2]);
2669
2670 str = " a b .c";
2671 res = StringUtils.splitPreserveAllTokens(str);
2672 assertEquals(4, res.length);
2673 assertEquals("", res[0]);
2674 assertEquals("a", res[1]);
2675 assertEquals("b", res[2]);
2676 assertEquals(".c", res[3]);
2677
2678 str = "a b .c";
2679 res = StringUtils.splitPreserveAllTokens(str);
2680 assertEquals(5, res.length);
2681 assertEquals("a", res[0]);
2682 assertEquals("", res[1]);
2683 assertEquals("b", res[2]);
2684 assertEquals("", res[3]);
2685 assertEquals(".c", res[4]);
2686
2687 str = " a ";
2688 res = StringUtils.splitPreserveAllTokens(str);
2689 assertEquals(4, res.length);
2690 assertEquals("", res[0]);
2691 assertEquals("a", res[1]);
2692 assertEquals("", res[2]);
2693 assertEquals("", res[3]);
2694
2695 str = " a b";
2696 res = StringUtils.splitPreserveAllTokens(str);
2697 assertEquals(4, res.length);
2698 assertEquals("", res[0]);
2699 assertEquals("a", res[1]);
2700 assertEquals("", res[2]);
2701 assertEquals("b", res[3]);
2702
2703 str = "a" + WHITESPACE + "b" + NON_WHITESPACE + "c";
2704 res = StringUtils.splitPreserveAllTokens(str);
2705 assertEquals(WHITESPACE.length() + 1, res.length);
2706 assertEquals("a", res[0]);
2707 for (int i = 1; i < WHITESPACE.length() - 1; i++) {
2708 assertEquals("", res[i]);
2709 }
2710 assertEquals("b" + NON_WHITESPACE + "c", res[WHITESPACE.length()]);
2711 }
2712
2713 @Test
2714 public void testSplitPreserveAllTokens_StringChar() {
2715 assertNull(StringUtils.splitPreserveAllTokens(null, '.'));
2716 assertEquals(0, StringUtils.splitPreserveAllTokens("", '.').length);
2717
2718 String str = "a.b. c";
2719 String[] res = StringUtils.splitPreserveAllTokens(str, '.');
2720 assertEquals(3, res.length);
2721 assertEquals("a", res[0]);
2722 assertEquals("b", res[1]);
2723 assertEquals(" c", res[2]);
2724
2725 str = "a.b.. c";
2726 res = StringUtils.splitPreserveAllTokens(str, '.');
2727 assertEquals(4, res.length);
2728 assertEquals("a", res[0]);
2729 assertEquals("b", res[1]);
2730 assertEquals("", res[2]);
2731 assertEquals(" c", res[3]);
2732
2733 str = ".a.";
2734 res = StringUtils.splitPreserveAllTokens(str, '.');
2735 assertEquals(3, res.length);
2736 assertEquals("", res[0]);
2737 assertEquals("a", res[1]);
2738 assertEquals("", res[2]);
2739
2740 str = ".a..";
2741 res = StringUtils.splitPreserveAllTokens(str, '.');
2742 assertEquals(4, res.length);
2743 assertEquals("", res[0]);
2744 assertEquals("a", res[1]);
2745 assertEquals("", res[2]);
2746 assertEquals("", res[3]);
2747
2748 str = "..a.";
2749 res = StringUtils.splitPreserveAllTokens(str, '.');
2750 assertEquals(4, res.length);
2751 assertEquals("", res[0]);
2752 assertEquals("", res[1]);
2753 assertEquals("a", res[2]);
2754 assertEquals("", res[3]);
2755
2756 str = "..a";
2757 res = StringUtils.splitPreserveAllTokens(str, '.');
2758 assertEquals(3, res.length);
2759 assertEquals("", res[0]);
2760 assertEquals("", res[1]);
2761 assertEquals("a", res[2]);
2762
2763 str = "a b c";
2764 res = StringUtils.splitPreserveAllTokens(str, ' ');
2765 assertEquals(3, res.length);
2766 assertEquals("a", res[0]);
2767 assertEquals("b", res[1]);
2768 assertEquals("c", res[2]);
2769
2770 str = "a b c";
2771 res = StringUtils.splitPreserveAllTokens(str, ' ');
2772 assertEquals(5, res.length);
2773 assertEquals("a", res[0]);
2774 assertEquals("", res[1]);
2775 assertEquals("b", res[2]);
2776 assertEquals("", res[3]);
2777 assertEquals("c", res[4]);
2778
2779 str = " a b c";
2780 res = StringUtils.splitPreserveAllTokens(str, ' ');
2781 assertEquals(4, res.length);
2782 assertEquals("", res[0]);
2783 assertEquals("a", res[1]);
2784 assertEquals("b", res[2]);
2785 assertEquals("c", res[3]);
2786
2787 str = " a b c";
2788 res = StringUtils.splitPreserveAllTokens(str, ' ');
2789 assertEquals(5, res.length);
2790 assertEquals("", res[0]);
2791 assertEquals("", res[1]);
2792 assertEquals("a", res[2]);
2793 assertEquals("b", res[3]);
2794 assertEquals("c", res[4]);
2795
2796 str = "a b c ";
2797 res = StringUtils.splitPreserveAllTokens(str, ' ');
2798 assertEquals(4, res.length);
2799 assertEquals("a", res[0]);
2800 assertEquals("b", res[1]);
2801 assertEquals("c", res[2]);
2802 assertEquals("", res[3]);
2803
2804 str = "a b c ";
2805 res = StringUtils.splitPreserveAllTokens(str, ' ');
2806 assertEquals(5, res.length);
2807 assertEquals("a", res[0]);
2808 assertEquals("b", res[1]);
2809 assertEquals("c", res[2]);
2810 assertEquals("", res[3]);
2811 assertEquals("", res[4]);
2812
2813
2814 {
2815 final String[] results;
2816 final String[] expectedResults = {"a", "", "b", "c"};
2817 results = StringUtils.splitPreserveAllTokens("a..b.c", '.');
2818 assertEquals(expectedResults.length, results.length);
2819 for (int i = 0; i < expectedResults.length; i++) {
2820 assertEquals(expectedResults[i], results[i]);
2821 }
2822 }
2823 }
2824
2825 @Test
2826 public void testSplitPreserveAllTokens_StringString_StringStringInt() {
2827 assertNull(StringUtils.splitPreserveAllTokens(null, "."));
2828 assertNull(StringUtils.splitPreserveAllTokens(null, ".", 3));
2829
2830 assertEquals(0, StringUtils.splitPreserveAllTokens("", ".").length);
2831 assertEquals(0, StringUtils.splitPreserveAllTokens("", ".", 3).length);
2832
2833 innerTestSplitPreserveAllTokens('.', ".", ' ');
2834 innerTestSplitPreserveAllTokens('.', ".", ',');
2835 innerTestSplitPreserveAllTokens('.', ".,", 'x');
2836 for (int i = 0; i < WHITESPACE.length(); i++) {
2837 for (int j = 0; j < NON_WHITESPACE.length(); j++) {
2838 innerTestSplitPreserveAllTokens(WHITESPACE.charAt(i), null, NON_WHITESPACE.charAt(j));
2839 innerTestSplitPreserveAllTokens(WHITESPACE.charAt(i), String.valueOf(WHITESPACE.charAt(i)), NON_WHITESPACE.charAt(j));
2840 }
2841 }
2842
2843 {
2844 final String[] results;
2845 final String[] expectedResults = {"ab", "de fg"};
2846 results = StringUtils.splitPreserveAllTokens("ab de fg", null, 2);
2847 assertEquals(expectedResults.length, results.length);
2848 for (int i = 0; i < expectedResults.length; i++) {
2849 assertEquals(expectedResults[i], results[i]);
2850 }
2851 }
2852
2853 {
2854 final String[] results;
2855 final String[] expectedResults = {"ab", " de fg"};
2856 results = StringUtils.splitPreserveAllTokens("ab de fg", null, 2);
2857 assertEquals(expectedResults.length, results.length);
2858 for (int i = 0; i < expectedResults.length; i++) {
2859 assertEquals(expectedResults[i], results[i]);
2860 }
2861 }
2862
2863 {
2864 final String[] results;
2865 final String[] expectedResults = {"ab", "::de:fg"};
2866 results = StringUtils.splitPreserveAllTokens("ab:::de:fg", ":", 2);
2867 assertEquals(expectedResults.length, results.length);
2868 for (int i = 0; i < expectedResults.length; i++) {
2869 assertEquals(expectedResults[i], results[i]);
2870 }
2871 }
2872
2873 {
2874 final String[] results;
2875 final String[] expectedResults = {"ab", "", " de fg"};
2876 results = StringUtils.splitPreserveAllTokens("ab de fg", null, 3);
2877 assertEquals(expectedResults.length, results.length);
2878 for (int i = 0; i < expectedResults.length; i++) {
2879 assertEquals(expectedResults[i], results[i]);
2880 }
2881 }
2882
2883 {
2884 final String[] results;
2885 final String[] expectedResults = {"ab", "", "", "de fg"};
2886 results = StringUtils.splitPreserveAllTokens("ab de fg", null, 4);
2887 assertEquals(expectedResults.length, results.length);
2888 for (int i = 0; i < expectedResults.length; i++) {
2889 assertEquals(expectedResults[i], results[i]);
2890 }
2891 }
2892
2893 {
2894 final String[] expectedResults = {"ab", "cd:ef"};
2895 final String[] results;
2896 results = StringUtils.splitPreserveAllTokens("ab:cd:ef", ":", 2);
2897 assertEquals(expectedResults.length, results.length);
2898 for (int i = 0; i < expectedResults.length; i++) {
2899 assertEquals(expectedResults[i], results[i]);
2900 }
2901 }
2902
2903 {
2904 final String[] results;
2905 final String[] expectedResults = {"ab", ":cd:ef"};
2906 results = StringUtils.splitPreserveAllTokens("ab::cd:ef", ":", 2);
2907 assertEquals(expectedResults.length, results.length);
2908 for (int i = 0; i < expectedResults.length; i++) {
2909 assertEquals(expectedResults[i], results[i]);
2910 }
2911 }
2912
2913 {
2914 final String[] results;
2915 final String[] expectedResults = {"ab", "", ":cd:ef"};
2916 results = StringUtils.splitPreserveAllTokens("ab:::cd:ef", ":", 3);
2917 assertEquals(expectedResults.length, results.length);
2918 for (int i = 0; i < expectedResults.length; i++) {
2919 assertEquals(expectedResults[i], results[i]);
2920 }
2921 }
2922
2923 {
2924 final String[] results;
2925 final String[] expectedResults = {"ab", "", "", "cd:ef"};
2926 results = StringUtils.splitPreserveAllTokens("ab:::cd:ef", ":", 4);
2927 assertEquals(expectedResults.length, results.length);
2928 for (int i = 0; i < expectedResults.length; i++) {
2929 assertEquals(expectedResults[i], results[i]);
2930 }
2931 }
2932
2933 {
2934 final String[] results;
2935 final String[] expectedResults = {"", "ab", "", "", "cd:ef"};
2936 results = StringUtils.splitPreserveAllTokens(":ab:::cd:ef", ":", 5);
2937 assertEquals(expectedResults.length, results.length);
2938 for (int i = 0; i < expectedResults.length; i++) {
2939 assertEquals(expectedResults[i], results[i]);
2940 }
2941 }
2942
2943 {
2944 final String[] results;
2945 final String[] expectedResults = {"", "", "ab", "", "", "cd:ef"};
2946 results = StringUtils.splitPreserveAllTokens("::ab:::cd:ef", ":", 6);
2947 assertEquals(expectedResults.length, results.length);
2948 for (int i = 0; i < expectedResults.length; i++) {
2949 assertEquals(expectedResults[i], results[i]);
2950 }
2951 }
2952
2953 }
2954
2955
2956
2957
2958
2959 @Test
2960 public void testStringUtilsCharSequenceContract() {
2961 final Class<StringUtils> c = StringUtils.class;
2962
2963 final String[] excludeMethods = {
2964 "public static int org.apache.commons.lang3.StringUtils.compare(java.lang.String,java.lang.String)",
2965 "public static int org.apache.commons.lang3.StringUtils.compare(java.lang.String,java.lang.String,boolean)",
2966 "public static int org.apache.commons.lang3.StringUtils.compareIgnoreCase(java.lang.String,java.lang.String)",
2967 "public static int org.apache.commons.lang3.StringUtils.compareIgnoreCase(java.lang.String,java.lang.String,boolean)",
2968 "public static byte[] org.apache.commons.lang3.StringUtils.getBytes(java.lang.String,java.nio.charset.Charset)",
2969 "public static byte[] org.apache.commons.lang3.StringUtils.getBytes(java.lang.String,java.lang.String) throws java.io.UnsupportedEncodingException"
2970 };
2971 final Method[] methods = c.getMethods();
2972
2973 for (final Method m : methods) {
2974 final String methodStr = m.toString();
2975 if (m.getReturnType() == String.class || m.getReturnType() == String[].class) {
2976
2977
2978
2979 final Class<?>[] params = m.getParameterTypes();
2980 if (params.length > 0 && (params[0] == CharSequence.class || params[0] == CharSequence[].class)) {
2981 assertFalse(ArrayUtils.contains(excludeMethods, methodStr), "The method \"" + methodStr + "\" appears to be mutable in spirit and therefore must not accept a CharSequence");
2982 }
2983 } else {
2984
2985
2986 final Class<?>[] params = m.getParameterTypes();
2987 if (params.length > 0 && (params[0] == String.class || params[0] == String[].class)) {
2988 assertTrue(ArrayUtils.contains(excludeMethods, methodStr),
2989 "The method \"" + methodStr + "\" appears to be immutable in spirit and therefore must not accept a String");
2990 }
2991 }
2992 }
2993 }
2994
2995 @Test
2996 public void testSwapCase_String() {
2997 assertNull(StringUtils.swapCase(null));
2998 assertEquals("", StringUtils.swapCase(""));
2999 assertEquals(" ", StringUtils.swapCase(" "));
3000
3001 assertEquals("i", WordUtils.swapCase("I"));
3002 assertEquals("I", WordUtils.swapCase("i"));
3003 assertEquals("I AM HERE 123", StringUtils.swapCase("i am here 123"));
3004 assertEquals("i aM hERE 123", StringUtils.swapCase("I Am Here 123"));
3005 assertEquals("I AM here 123", StringUtils.swapCase("i am HERE 123"));
3006 assertEquals("i am here 123", StringUtils.swapCase("I AM HERE 123"));
3007
3008 final String test = "This String contains a TitleCase character: \u01C8";
3009 final String expect = "tHIS sTRING CONTAINS A tITLEcASE CHARACTER: \u01C9";
3010 assertEquals(expect, WordUtils.swapCase(test));
3011 assertEquals(expect, StringUtils.swapCase(test));
3012 }
3013
3014 @Test
3015 public void testToCodePoints() {
3016 final int orphanedHighSurrogate = 0xD801;
3017 final int orphanedLowSurrogate = 0xDC00;
3018 final int supplementary = 0x2070E;
3019
3020 final int[] codePoints = {'a', orphanedHighSurrogate, 'b', 'c', supplementary,
3021 'd', orphanedLowSurrogate, 'e'};
3022 final String s = new String(codePoints, 0, codePoints.length);
3023 assertArrayEquals(codePoints, StringUtils.toCodePoints(s));
3024
3025 assertNull(StringUtils.toCodePoints(null));
3026 assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, StringUtils.toCodePoints(""));
3027 }
3028
3029
3030
3031
3032
3033
3034 @Test
3035 public void testToEncodedString() {
3036 final String expectedString = "The quick brown fox jumps over the lazy dog.";
3037 String encoding = SystemUtils.FILE_ENCODING;
3038 byte[] expectedBytes = expectedString.getBytes(Charset.defaultCharset());
3039
3040 assertArrayEquals(expectedBytes, expectedString.getBytes());
3041
3042 assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.defaultCharset()));
3043 assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.forName(encoding)));
3044 encoding = "UTF-16";
3045 expectedBytes = expectedString.getBytes(Charset.forName(encoding));
3046 assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.forName(encoding)));
3047 }
3048
3049 @Test
3050 public void testToRootLowerCase() {
3051 assertNull(StringUtils.toRootLowerCase(null));
3052 assertEquals("a", StringUtils.toRootLowerCase("A"));
3053 assertEquals("a", StringUtils.toRootLowerCase("a"));
3054 final Locale TURKISH = Locale.forLanguageTag("tr");
3055
3056 assertNotEquals("title", "TITLE".toLowerCase(TURKISH));
3057 assertEquals("title", "TITLE".toLowerCase(Locale.ROOT));
3058 assertEquals("title", StringUtils.toRootLowerCase("TITLE"));
3059
3060 final Locale defaultLocale = Locale.getDefault();
3061 try {
3062 Locale.setDefault(TURKISH);
3063 assertEquals("title", StringUtils.toRootLowerCase("TITLE"));
3064 } finally {
3065 Locale.setDefault(defaultLocale);
3066 }
3067 }
3068
3069 @Test
3070 public void testToRootUpperCase() {
3071 assertNull(StringUtils.toRootUpperCase(null));
3072 assertEquals("A", StringUtils.toRootUpperCase("a"));
3073 assertEquals("A", StringUtils.toRootUpperCase("A"));
3074 final Locale TURKISH = Locale.forLanguageTag("tr");
3075
3076 assertNotEquals("TITLE", "title".toUpperCase(TURKISH));
3077 assertEquals("TITLE", "title".toUpperCase(Locale.ROOT));
3078 assertEquals("TITLE", StringUtils.toRootUpperCase("title"));
3079
3080 final Locale defaultLocale = Locale.getDefault();
3081 try {
3082 Locale.setDefault(TURKISH);
3083 assertEquals("TITLE", StringUtils.toRootUpperCase("title"));
3084 } finally {
3085 Locale.setDefault(defaultLocale);
3086 }
3087 }
3088
3089
3090
3091
3092
3093
3094
3095 @Test
3096 public void testToString() throws UnsupportedEncodingException {
3097 final String expectedString = "The quick brown fox jumps over the lazy dog.";
3098 byte[] expectedBytes = expectedString.getBytes(Charset.defaultCharset());
3099
3100 assertArrayEquals(expectedBytes, expectedString.getBytes());
3101
3102 assertEquals(expectedString, StringUtils.toString(expectedBytes, null));
3103 assertEquals(expectedString, StringUtils.toString(expectedBytes, SystemUtils.FILE_ENCODING));
3104 final String encoding = "UTF-16";
3105 expectedBytes = expectedString.getBytes(Charset.forName(encoding));
3106 assertEquals(expectedString, StringUtils.toString(expectedBytes, encoding));
3107 }
3108
3109 @Test
3110 public void testTruncate_StringInt() {
3111 assertNull(StringUtils.truncate(null, 12));
3112 assertThrows(
3113 IllegalArgumentException.class, () -> StringUtils.truncate(null, -1), "maxWith cannot be negative");
3114 assertThrows(
3115 IllegalArgumentException.class, () -> StringUtils.truncate(null, -10), "maxWith cannot be negative");
3116 assertThrows(
3117 IllegalArgumentException.class,
3118 () -> StringUtils.truncate(null, Integer.MIN_VALUE),
3119 "maxWith cannot be negative");
3120 assertEquals("", StringUtils.truncate("", 10));
3121 assertEquals("abc", StringUtils.truncate("abcdefghij", 3));
3122 assertEquals("abcdef", StringUtils.truncate("abcdefghij", 6));
3123 assertEquals("", StringUtils.truncate("abcdefghij", 0));
3124 assertThrows(
3125 IllegalArgumentException.class,
3126 () -> StringUtils.truncate("abcdefghij", -1),
3127 "maxWith cannot be negative");
3128 assertThrows(
3129 IllegalArgumentException.class,
3130 () -> StringUtils.truncate("abcdefghij", -100),
3131 "maxWith cannot be negative");
3132 assertThrows(
3133 IllegalArgumentException.class,
3134 () -> StringUtils.truncate("abcdefghij", Integer.MIN_VALUE),
3135 "maxWith cannot be negative");
3136 assertEquals("abcdefghij", StringUtils.truncate("abcdefghijklmno", 10));
3137 assertEquals("abcdefghijklmno", StringUtils.truncate("abcdefghijklmno", Integer.MAX_VALUE));
3138 assertEquals("abcde", StringUtils.truncate("abcdefghijklmno", 5));
3139 assertEquals("abc", StringUtils.truncate("abcdefghijklmno", 3));
3140 }
3141
3142 @Test
3143 public void testTruncate_StringIntInt() {
3144 assertNull(StringUtils.truncate(null, 0, 12));
3145 assertThrows(
3146 IllegalArgumentException.class, () -> StringUtils.truncate(null, -1, 0), "offset cannot be negative");
3147 assertThrows(
3148 IllegalArgumentException.class,
3149 () -> StringUtils.truncate(null, -10, -4),
3150 "offset cannot be negative");
3151 assertThrows(
3152 IllegalArgumentException.class,
3153 () -> StringUtils.truncate(null, Integer.MIN_VALUE, Integer.MIN_VALUE),
3154 "offset cannot be negative");
3155 assertNull(StringUtils.truncate(null, 10, 12));
3156 assertEquals("", StringUtils.truncate("", 0, 10));
3157 assertEquals("", StringUtils.truncate("", 2, 10));
3158 assertEquals("abc", StringUtils.truncate("abcdefghij", 0, 3));
3159 assertEquals("fghij", StringUtils.truncate("abcdefghij", 5, 6));
3160 assertEquals("", StringUtils.truncate("abcdefghij", 0, 0));
3161 assertThrows(
3162 IllegalArgumentException.class,
3163 () -> StringUtils.truncate("abcdefghij", 0, -1),
3164 "maxWith cannot be negative");
3165 assertThrows(
3166 IllegalArgumentException.class,
3167 () -> StringUtils.truncate("abcdefghij", 0, -10),
3168 "maxWith cannot be negative");
3169 assertThrows(
3170 IllegalArgumentException.class,
3171 () -> StringUtils.truncate("abcdefghij", 0, -100),
3172 "maxWith cannot be negative");
3173 assertThrows(
3174 IllegalArgumentException.class,
3175 () -> StringUtils.truncate("abcdefghij", 1, -100),
3176 "maxWith cannot be negative");
3177 assertThrows(
3178 IllegalArgumentException.class,
3179 () -> StringUtils.truncate("abcdefghij", 0, Integer.MIN_VALUE),
3180 "maxWith cannot be negative");
3181 assertThrows(
3182 IllegalArgumentException.class,
3183 () -> StringUtils.truncate("abcdefghij", -1, 0),
3184 "offset cannot be negative");
3185 assertThrows(
3186 IllegalArgumentException.class,
3187 () -> StringUtils.truncate("abcdefghij", -10, 0),
3188 "offset cannot be negative");
3189 assertThrows(
3190 IllegalArgumentException.class,
3191 () -> StringUtils.truncate("abcdefghij", -100, 1),
3192 "offset cannot be negative");
3193 assertThrows(
3194 IllegalArgumentException.class,
3195 () -> StringUtils.truncate("abcdefghij", Integer.MIN_VALUE, 0),
3196 "offset cannot be negative");
3197 assertThrows(
3198 IllegalArgumentException.class,
3199 () -> StringUtils.truncate("abcdefghij", -1, -1),
3200 "offset cannot be negative");
3201 assertThrows(
3202 IllegalArgumentException.class,
3203 () -> StringUtils.truncate("abcdefghij", -10, -10),
3204 "offset cannot be negative");
3205 assertThrows(
3206 IllegalArgumentException.class,
3207 () -> StringUtils.truncate("abcdefghij", -100, -100),
3208 "offset cannot be negative");
3209 assertThrows(
3210 IllegalArgumentException.class,
3211 () -> StringUtils.truncate("abcdefghij", Integer.MIN_VALUE, Integer.MIN_VALUE),
3212 "offset cannot be negative");
3213 final String raspberry = "raspberry peach";
3214 assertEquals("peach", StringUtils.truncate(raspberry, 10, 15));
3215 assertEquals("abcdefghij", StringUtils.truncate("abcdefghijklmno", 0, 10));
3216 assertEquals("abcdefghijklmno", StringUtils.truncate("abcdefghijklmno", 0, Integer.MAX_VALUE));
3217 assertEquals("bcdefghijk", StringUtils.truncate("abcdefghijklmno", 1, 10));
3218 assertEquals("cdefghijkl", StringUtils.truncate("abcdefghijklmno", 2, 10));
3219 assertEquals("defghijklm", StringUtils.truncate("abcdefghijklmno", 3, 10));
3220 assertEquals("efghijklmn", StringUtils.truncate("abcdefghijklmno", 4, 10));
3221 assertEquals("fghijklmno", StringUtils.truncate("abcdefghijklmno", 5, 10));
3222 assertEquals("fghij", StringUtils.truncate("abcdefghijklmno", 5, 5));
3223 assertEquals("fgh", StringUtils.truncate("abcdefghijklmno", 5, 3));
3224 assertEquals("klm", StringUtils.truncate("abcdefghijklmno", 10, 3));
3225 assertEquals("klmno", StringUtils.truncate("abcdefghijklmno", 10, Integer.MAX_VALUE));
3226 assertEquals("n", StringUtils.truncate("abcdefghijklmno", 13, 1));
3227 assertEquals("no", StringUtils.truncate("abcdefghijklmno", 13, Integer.MAX_VALUE));
3228 assertEquals("o", StringUtils.truncate("abcdefghijklmno", 14, 1));
3229 assertEquals("o", StringUtils.truncate("abcdefghijklmno", 14, Integer.MAX_VALUE));
3230 assertEquals("", StringUtils.truncate("abcdefghijklmno", 15, 1));
3231 assertEquals("", StringUtils.truncate("abcdefghijklmno", 15, Integer.MAX_VALUE));
3232 assertEquals("", StringUtils.truncate("abcdefghijklmno", Integer.MAX_VALUE, Integer.MAX_VALUE));
3233 }
3234
3235 @Test
3236 public void testUnCapitalize() {
3237 assertNull(StringUtils.uncapitalize(null));
3238
3239 assertEquals(FOO_UNCAP, StringUtils.uncapitalize(FOO_CAP), "uncapitalize(String) failed");
3240 assertEquals(FOO_UNCAP, StringUtils.uncapitalize(FOO_UNCAP), "uncapitalize(string) failed");
3241 assertEquals("", StringUtils.uncapitalize(""), "uncapitalize(empty-string) failed");
3242 assertEquals("x", StringUtils.uncapitalize("X"), "uncapitalize(single-char-string) failed");
3243
3244
3245 assertEquals("cat", StringUtils.uncapitalize("cat"));
3246 assertEquals("cat", StringUtils.uncapitalize("Cat"));
3247 assertEquals("cAT", StringUtils.uncapitalize("CAT"));
3248 }
3249
3250 @Test
3251 public void testUnescapeSurrogatePairs() {
3252 assertEquals("\uD83D\uDE30", StringEscapeUtils.unescapeCsv("\uD83D\uDE30"));
3253
3254 assertEquals("\uD800\uDC00", StringEscapeUtils.unescapeCsv("\uD800\uDC00"));
3255 assertEquals("\uD834\uDD1E", StringEscapeUtils.unescapeCsv("\uD834\uDD1E"));
3256 assertEquals("\uDBFF\uDFFD", StringEscapeUtils.unescapeCsv("\uDBFF\uDFFD"));
3257 assertEquals("\uDBFF\uDFFD", StringEscapeUtils.unescapeHtml3("\uDBFF\uDFFD"));
3258 assertEquals("\uDBFF\uDFFD", StringEscapeUtils.unescapeHtml4("\uDBFF\uDFFD"));
3259 }
3260
3261 @Test
3262 public void testUnwrap_StringChar() {
3263 assertNull(StringUtils.unwrap(null, null));
3264 assertNull(StringUtils.unwrap(null, CharUtils.NUL));
3265 assertNull(StringUtils.unwrap(null, '1'));
3266
3267 assertEquals("abc", StringUtils.unwrap("abc", null));
3268 assertEquals("a", StringUtils.unwrap("a", "a"));
3269 assertEquals("", StringUtils.unwrap("aa", "a"));
3270 assertEquals("abc", StringUtils.unwrap("\'abc\'", '\''));
3271 assertEquals("abc", StringUtils.unwrap("AabcA", 'A'));
3272 assertEquals("AabcA", StringUtils.unwrap("AAabcAA", 'A'));
3273 assertEquals("abc", StringUtils.unwrap("abc", 'b'));
3274 assertEquals("#A", StringUtils.unwrap("#A", '#'));
3275 assertEquals("A#", StringUtils.unwrap("A#", '#'));
3276 assertEquals("ABA", StringUtils.unwrap("AABAA", 'A'));
3277 }
3278
3279 @Test
3280 public void testUnwrap_StringString() {
3281 assertNull(StringUtils.unwrap(null, null));
3282 assertNull(StringUtils.unwrap(null, ""));
3283 assertNull(StringUtils.unwrap(null, "1"));
3284
3285 assertEquals("abc", StringUtils.unwrap("abc", null));
3286 assertEquals("abc", StringUtils.unwrap("abc", ""));
3287 assertEquals("a", StringUtils.unwrap("a", "a"));
3288 assertEquals("ababa", StringUtils.unwrap("ababa", "aba"));
3289 assertEquals("", StringUtils.unwrap("aa", "a"));
3290 assertEquals("abc", StringUtils.unwrap("\'abc\'", "\'"));
3291 assertEquals("abc", StringUtils.unwrap("\"abc\"", "\""));
3292 assertEquals("abc\"xyz", StringUtils.unwrap("\"abc\"xyz\"", "\""));
3293 assertEquals("abc\"xyz\"", StringUtils.unwrap("\"abc\"xyz\"\"", "\""));
3294 assertEquals("abc\'xyz\'", StringUtils.unwrap("\"abc\'xyz\'\"", "\""));
3295 assertEquals("\"abc\'xyz\'\"", StringUtils.unwrap("AA\"abc\'xyz\'\"AA", "AA"));
3296 assertEquals("\"abc\'xyz\'\"", StringUtils.unwrap("123\"abc\'xyz\'\"123", "123"));
3297 assertEquals("AA\"abc\'xyz\'\"", StringUtils.unwrap("AA\"abc\'xyz\'\"", "AA"));
3298 assertEquals("AA\"abc\'xyz\'\"AA", StringUtils.unwrap("AAA\"abc\'xyz\'\"AAA", "A"));
3299 assertEquals("\"abc\'xyz\'\"AA", StringUtils.unwrap("\"abc\'xyz\'\"AA", "AA"));
3300 }
3301
3302 @Test
3303 public void testUpperCase() {
3304 assertNull(StringUtils.upperCase(null));
3305 assertNull(StringUtils.upperCase(null, Locale.ENGLISH));
3306 assertEquals("FOO TEST THING", StringUtils.upperCase("fOo test THING"), "upperCase(String) failed");
3307 assertEquals("", StringUtils.upperCase(""), "upperCase(empty-string) failed");
3308 assertEquals("FOO TEST THING", StringUtils.upperCase("fOo test THING", Locale.ENGLISH),
3309 "upperCase(String, Locale) failed");
3310 assertEquals("", StringUtils.upperCase("", Locale.ENGLISH),
3311 "upperCase(empty-string, Locale) failed");
3312 }
3313
3314 @Test
3315 public void testWrap_StringChar() {
3316 assertNull(StringUtils.wrap(null, CharUtils.NUL));
3317 assertNull(StringUtils.wrap(null, '1'));
3318
3319 assertEquals("", StringUtils.wrap("", CharUtils.NUL));
3320 assertEquals("xabx", StringUtils.wrap("ab", 'x'));
3321 assertEquals("\"ab\"", StringUtils.wrap("ab", '\"'));
3322 assertEquals("\"\"ab\"\"", StringUtils.wrap("\"ab\"", '\"'));
3323 assertEquals("'ab'", StringUtils.wrap("ab", '\''));
3324 assertEquals("''abcd''", StringUtils.wrap("'abcd'", '\''));
3325 assertEquals("'\"abcd\"'", StringUtils.wrap("\"abcd\"", '\''));
3326 assertEquals("\"'abcd'\"", StringUtils.wrap("'abcd'", '\"'));
3327 }
3328
3329 @Test
3330 public void testWrap_StringString() {
3331 assertNull(StringUtils.wrap(null, null));
3332 assertNull(StringUtils.wrap(null, ""));
3333 assertNull(StringUtils.wrap(null, "1"));
3334
3335 assertNull(StringUtils.wrap(null, null));
3336 assertEquals("", StringUtils.wrap("", ""));
3337 assertEquals("ab", StringUtils.wrap("ab", null));
3338 assertEquals("xabx", StringUtils.wrap("ab", "x"));
3339 assertEquals("\"ab\"", StringUtils.wrap("ab", "\""));
3340 assertEquals("\"\"ab\"\"", StringUtils.wrap("\"ab\"", "\""));
3341 assertEquals("'ab'", StringUtils.wrap("ab", "'"));
3342 assertEquals("''abcd''", StringUtils.wrap("'abcd'", "'"));
3343 assertEquals("'\"abcd\"'", StringUtils.wrap("\"abcd\"", "'"));
3344 assertEquals("\"'abcd'\"", StringUtils.wrap("'abcd'", "\""));
3345 }
3346
3347 @Test
3348 public void testWrapIfMissing_StringChar() {
3349 assertNull(StringUtils.wrapIfMissing(null, CharUtils.NUL));
3350 assertNull(StringUtils.wrapIfMissing(null, '1'));
3351
3352 assertEquals("", StringUtils.wrapIfMissing("", CharUtils.NUL));
3353 assertEquals("xabx", StringUtils.wrapIfMissing("ab", 'x'));
3354 assertEquals("\"ab\"", StringUtils.wrapIfMissing("ab", '\"'));
3355 assertEquals("\"ab\"", StringUtils.wrapIfMissing("\"ab\"", '\"'));
3356 assertEquals("'ab'", StringUtils.wrapIfMissing("ab", '\''));
3357 assertEquals("'abcd'", StringUtils.wrapIfMissing("'abcd'", '\''));
3358 assertEquals("'\"abcd\"'", StringUtils.wrapIfMissing("\"abcd\"", '\''));
3359 assertEquals("\"'abcd'\"", StringUtils.wrapIfMissing("'abcd'", '\"'));
3360 assertEquals("/x/", StringUtils.wrapIfMissing("x", '/'));
3361 assertEquals("/x/y/z/", StringUtils.wrapIfMissing("x/y/z", '/'));
3362 assertEquals("/x/y/z/", StringUtils.wrapIfMissing("/x/y/z", '/'));
3363 assertEquals("/x/y/z/", StringUtils.wrapIfMissing("x/y/z/", '/'));
3364
3365 assertSame("/", StringUtils.wrapIfMissing("/", '/'));
3366 assertSame("/x/", StringUtils.wrapIfMissing("/x/", '/'));
3367 }
3368
3369 @Test
3370 public void testWrapIfMissing_StringString() {
3371 assertNull(StringUtils.wrapIfMissing(null, "\0"));
3372 assertNull(StringUtils.wrapIfMissing(null, "1"));
3373
3374 assertEquals("", StringUtils.wrapIfMissing("", "\0"));
3375 assertEquals("xabx", StringUtils.wrapIfMissing("ab", "x"));
3376 assertEquals("\"ab\"", StringUtils.wrapIfMissing("ab", "\""));
3377 assertEquals("\"ab\"", StringUtils.wrapIfMissing("\"ab\"", "\""));
3378 assertEquals("'ab'", StringUtils.wrapIfMissing("ab", "\'"));
3379 assertEquals("'abcd'", StringUtils.wrapIfMissing("'abcd'", "\'"));
3380 assertEquals("'\"abcd\"'", StringUtils.wrapIfMissing("\"abcd\"", "\'"));
3381 assertEquals("\"'abcd'\"", StringUtils.wrapIfMissing("'abcd'", "\""));
3382 assertEquals("/x/", StringUtils.wrapIfMissing("x", "/"));
3383 assertEquals("/x/y/z/", StringUtils.wrapIfMissing("x/y/z", "/"));
3384 assertEquals("/x/y/z/", StringUtils.wrapIfMissing("/x/y/z", "/"));
3385 assertEquals("/x/y/z/", StringUtils.wrapIfMissing("x/y/z/", "/"));
3386 assertEquals("/", StringUtils.wrapIfMissing("/", "/"));
3387 assertEquals("ab/ab", StringUtils.wrapIfMissing("/", "ab"));
3388
3389 assertSame("ab/ab", StringUtils.wrapIfMissing("ab/ab", "ab"));
3390 assertSame("//x//", StringUtils.wrapIfMissing("//x//", "//"));
3391 }
3392 }