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