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.text;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.lang.reflect.Constructor;
27  import java.lang.reflect.Modifier;
28  import java.util.stream.IntStream;
29  
30  import org.apache.commons.lang3.ArrayUtils;
31  import org.apache.commons.lang3.StringUtils;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests {@link WordUtils}.
36   */
37  class WordUtilsTest {
38  
39      private static final String WHITESPACE = IntStream.rangeClosed(Character.MIN_CODE_POINT, Character.MAX_CODE_POINT).filter(Character::isWhitespace)
40              .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString();
41  
42      @Test
43      void testAbbreviateForLowerThanMinusOneValues() {
44          assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate("01 23 45 67 89", 9, -10, null));
45      }
46  
47      @Test
48      void testAbbreviateForLowerValue() {
49          assertEquals("012", WordUtils.abbreviate("012 3456789", 0, 5, null));
50          assertEquals("01234", WordUtils.abbreviate("01234 56789", 5, 10, null));
51          assertEquals("01 23 45 67", WordUtils.abbreviate("01 23 45 67 89", 9, -1, null));
52          assertEquals("01 23 45 6", WordUtils.abbreviate("01 23 45 67 89", 9, 10, null));
53          assertEquals("0123456789", WordUtils.abbreviate("0123456789", 15, 20, null));
54      }
55  
56      @Test
57      void testAbbreviateForLowerValueAndAppendedString() {
58          assertEquals("012", WordUtils.abbreviate("012 3456789", 0, 5, null));
59          assertEquals("01234-", WordUtils.abbreviate("01234 56789", 5, 10, "-"));
60          assertEquals("01 23 45 67abc", WordUtils.abbreviate("01 23 45 67 89", 9, -1, "abc"));
61          assertEquals("01 23 45 6", WordUtils.abbreviate("01 23 45 67 89", 9, 10, ""));
62      }
63  
64      @Test
65      void testAbbreviateForNullAndEmptyString() {
66          assertNull(WordUtils.abbreviate(null, 1, -1, ""));
67          assertEquals(StringUtils.EMPTY, WordUtils.abbreviate("", 1, -1, ""));
68          assertEquals("", WordUtils.abbreviate("0123456790", 0, 0, ""));
69          assertEquals("", WordUtils.abbreviate(" 0123456790", 0, -1, ""));
70      }
71  
72      @Test
73      void testAbbreviateForUpperLimit() {
74          assertEquals("01234", WordUtils.abbreviate("0123456789", 0, 5, ""));
75          assertEquals("012", WordUtils.abbreviate("012 3456789", 2, 5, ""));
76          assertEquals("0123456789", WordUtils.abbreviate("0123456789", 0, -1, ""));
77      }
78  
79      @Test
80      void testAbbreviateForUpperLimitAndAppendedString() {
81          assertEquals("01234-", WordUtils.abbreviate("0123456789", 0, 5, "-"));
82          assertEquals("012", WordUtils.abbreviate("012 3456789", 2, 5, null));
83          assertEquals("0123456789", WordUtils.abbreviate("0123456789", 0, -1, ""));
84      }
85  
86      @Test
87      void testAbbreviateUpperLessThanLowerValues() {
88          assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate("0123456789", 5, 2, ""));
89      }
90  
91      @Test
92      void testCapitalize_String() {
93          assertNull(WordUtils.capitalize(null));
94          assertEquals("", WordUtils.capitalize(""));
95          assertEquals("  ", WordUtils.capitalize("  "));
96  
97          assertEquals("I", WordUtils.capitalize("I"));
98          assertEquals("I", WordUtils.capitalize("i"));
99          assertEquals("I Am Here 123", WordUtils.capitalize("i am here 123"));
100         assertEquals("I Am Here 123", WordUtils.capitalize("I Am Here 123"));
101         assertEquals("I Am HERE 123", WordUtils.capitalize("i am HERE 123"));
102         assertEquals("I AM HERE 123", WordUtils.capitalize("I AM HERE 123"));
103     }
104 
105     @Test
106     void testCapitalizeFully_String() {
107         assertNull(WordUtils.capitalizeFully(null));
108         assertEquals("", WordUtils.capitalizeFully(""));
109         assertEquals("  ", WordUtils.capitalizeFully("  "));
110         assertEquals("I", WordUtils.capitalizeFully("I"));
111         assertEquals("I", WordUtils.capitalizeFully("i"));
112         assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am here 123"));
113         assertEquals("I Am Here 123", WordUtils.capitalizeFully("I Am Here 123"));
114         assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am HERE 123"));
115         assertEquals("I Am Here 123", WordUtils.capitalizeFully("I AM HERE 123"));
116         assertEquals("Alphabet", WordUtils.capitalizeFully("alphabet")); // single word
117         assertEquals("A\tB\nC D", WordUtils.capitalizeFully("a\tb\nc d"));
118         assertEquals("And \tBut \nCleat  Dome", WordUtils.capitalizeFully("and \tbut \ncleat  dome"));
119         // All whitespace
120         assertEquals(WHITESPACE, WordUtils.capitalizeFully(WHITESPACE));
121         assertEquals("A" + WHITESPACE + "B", WordUtils.capitalizeFully("a" + WHITESPACE + "b"));
122 
123     }
124 
125     @Test
126     void testCapitalizeFully_Text88() {
127         assertEquals("I am fine now", WordUtils.capitalizeFully("i am fine now", new char[] {}));
128     }
129 
130     @Test
131     void testCapitalizeFullyWithDelimiters_String() {
132         assertNull(WordUtils.capitalizeFully(null, null));
133         assertEquals("", WordUtils.capitalizeFully("", ArrayUtils.EMPTY_CHAR_ARRAY));
134         assertEquals("  ", WordUtils.capitalizeFully("  ", ArrayUtils.EMPTY_CHAR_ARRAY));
135 
136         char[] chars = { '-', '+', ' ', '@' };
137         assertEquals("I", WordUtils.capitalizeFully("I", chars));
138         assertEquals("I", WordUtils.capitalizeFully("i", chars));
139         assertEquals("I-Am Here+123", WordUtils.capitalizeFully("i-am here+123", chars));
140         assertEquals("I Am+Here-123", WordUtils.capitalizeFully("I Am+Here-123", chars));
141         assertEquals("I+Am-Here 123", WordUtils.capitalizeFully("i+am-HERE 123", chars));
142         assertEquals("I-Am Here+123", WordUtils.capitalizeFully("I-AM HERE+123", chars));
143         chars = new char[] { '.' };
144         assertEquals("I am.Fine", WordUtils.capitalizeFully("i aM.fine", chars));
145         assertEquals("I Am.fine", WordUtils.capitalizeFully("i am.fine", null));
146         assertEquals("Alphabet", WordUtils.capitalizeFully("alphabet", null)); // single word
147         assertEquals("Alphabet", WordUtils.capitalizeFully("alphabet", new char[] { '!' })); // no matching delim
148     }
149 
150     @Test
151     void testCapitalizeWithDelimiters_String() {
152         assertNull(WordUtils.capitalize(null, null));
153         assertEquals("", WordUtils.capitalize("", ArrayUtils.EMPTY_CHAR_ARRAY));
154         assertEquals("  ", WordUtils.capitalize("  ", ArrayUtils.EMPTY_CHAR_ARRAY));
155 
156         char[] chars = { '-', '+', ' ', '@' };
157         assertEquals("I", WordUtils.capitalize("I", chars));
158         assertEquals("I", WordUtils.capitalize("i", chars));
159         assertEquals("I-Am Here+123", WordUtils.capitalize("i-am here+123", chars));
160         assertEquals("I Am+Here-123", WordUtils.capitalize("I Am+Here-123", chars));
161         assertEquals("I+Am-HERE 123", WordUtils.capitalize("i+am-HERE 123", chars));
162         assertEquals("I-AM HERE+123", WordUtils.capitalize("I-AM HERE+123", chars));
163         chars = new char[] { '.' };
164         assertEquals("I aM.Fine", WordUtils.capitalize("i aM.fine", chars));
165         assertEquals("I Am.fine", WordUtils.capitalize("i am.fine", null));
166     }
167 
168     @Test
169     void testConstructor() {
170         assertNotNull(new WordUtils());
171         final Constructor<?>[] cons = WordUtils.class.getDeclaredConstructors();
172         assertEquals(1, cons.length);
173         assertTrue(Modifier.isPublic(cons[0].getModifiers()));
174         assertTrue(Modifier.isPublic(WordUtils.class.getModifiers()));
175         assertFalse(Modifier.isFinal(WordUtils.class.getModifiers()));
176     }
177 
178     @Test
179     void testContainsAllWords_StringString() {
180         assertFalse(WordUtils.containsAllWords(null));
181         assertFalse(WordUtils.containsAllWords(null, ""));
182         assertFalse(WordUtils.containsAllWords(null, "ab"));
183 
184         assertFalse(WordUtils.containsAllWords(""));
185         assertFalse(WordUtils.containsAllWords("", (String) null));
186         assertFalse(WordUtils.containsAllWords("", ""));
187         assertFalse(WordUtils.containsAllWords("", "ab"));
188 
189         assertFalse(WordUtils.containsAllWords("foo"));
190         assertFalse(WordUtils.containsAllWords("foo", (String) null));
191         assertFalse(WordUtils.containsAllWords("bar", ""));
192         assertFalse(WordUtils.containsAllWords("zzabyycdxx", "by"));
193         assertTrue(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", "lorem", "dolor"));
194         assertFalse(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", null, "lorem", "dolor"));
195         assertFalse(WordUtils.containsAllWords("lorem ipsum null dolor sit amet", "ipsum", null, "lorem", "dolor"));
196         assertFalse(WordUtils.containsAllWords("ab", "b"));
197         assertFalse(WordUtils.containsAllWords("ab", "z"));
198         assertFalse(WordUtils.containsAllWords("ab", "["));
199         assertFalse(WordUtils.containsAllWords("ab", "]"));
200         assertFalse(WordUtils.containsAllWords("ab", "*"));
201         assertTrue(WordUtils.containsAllWords("ab x", "ab", "x"));
202     }
203 
204     @Test
205     void testContainsAllWordsWithNull() {
206         assertFalse(WordUtils.containsAllWords("M", (CharSequence) null));
207     }
208 
209     @Test
210     void testInitials_String() {
211         assertNull(WordUtils.initials(null));
212         assertEquals("", WordUtils.initials(""));
213         assertEquals("", WordUtils.initials("  "));
214 
215         assertEquals("I", WordUtils.initials("I"));
216         assertEquals("i", WordUtils.initials("i"));
217         assertEquals("BJL", WordUtils.initials("Ben John Lee"));
218         assertEquals("BJL", WordUtils.initials("   Ben \n   John\tLee\t"));
219         assertEquals("BJ", WordUtils.initials("Ben J.Lee"));
220         assertEquals("BJ.L", WordUtils.initials(" Ben   John  . Lee"));
221         assertEquals("iah1", WordUtils.initials("i am here 123"));
222     }
223 
224     @Test
225     void testInitials_String_charArray() {
226         char[] array = null;
227         assertNull(WordUtils.initials(null, array));
228         assertEquals("", WordUtils.initials("", array));
229         assertEquals("", WordUtils.initials("  ", array));
230         assertEquals("I", WordUtils.initials("I", array));
231         assertEquals("i", WordUtils.initials("i", array));
232         assertEquals("S", WordUtils.initials("SJC", array));
233         assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
234         assertEquals("BJL", WordUtils.initials("   Ben \n   John\tLee\t", array));
235         assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
236         assertEquals("BJ.L", WordUtils.initials(" Ben   John  . Lee", array));
237         assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
238         assertEquals("iah1", WordUtils.initials("i am here 123", array));
239 
240         array = ArrayUtils.EMPTY_CHAR_ARRAY;
241         assertNull(WordUtils.initials(null, array));
242         assertEquals("", WordUtils.initials("", array));
243         assertEquals("", WordUtils.initials("  ", array));
244         assertEquals("", WordUtils.initials("I", array));
245         assertEquals("", WordUtils.initials("i", array));
246         assertEquals("", WordUtils.initials("SJC", array));
247         assertEquals("", WordUtils.initials("Ben John Lee", array));
248         assertEquals("", WordUtils.initials("   Ben \n   John\tLee\t", array));
249         assertEquals("", WordUtils.initials("Ben J.Lee", array));
250         assertEquals("", WordUtils.initials(" Ben   John  . Lee", array));
251         assertEquals("", WordUtils.initials("Kay O'Murphy", array));
252         assertEquals("", WordUtils.initials("i am here 123", array));
253 
254         array = " ".toCharArray();
255         assertNull(WordUtils.initials(null, array));
256         assertEquals("", WordUtils.initials("", array));
257         assertEquals("", WordUtils.initials("  ", array));
258         assertEquals("I", WordUtils.initials("I", array));
259         assertEquals("i", WordUtils.initials("i", array));
260         assertEquals("S", WordUtils.initials("SJC", array));
261         assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
262         assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
263         assertEquals("B\nJ", WordUtils.initials("   Ben \n   John\tLee\t", array));
264         assertEquals("BJ.L", WordUtils.initials(" Ben   John  . Lee", array));
265         assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
266         assertEquals("iah1", WordUtils.initials("i am here 123", array));
267 
268         array = " .".toCharArray();
269         assertNull(WordUtils.initials(null, array));
270         assertEquals("", WordUtils.initials("", array));
271         assertEquals("", WordUtils.initials("  ", array));
272         assertEquals("I", WordUtils.initials("I", array));
273         assertEquals("i", WordUtils.initials("i", array));
274         assertEquals("S", WordUtils.initials("SJC", array));
275         assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
276         assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
277         assertEquals("BJL", WordUtils.initials(" Ben   John  . Lee", array));
278         assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
279         assertEquals("iah1", WordUtils.initials("i am here 123", array));
280 
281         array = " .'".toCharArray();
282         assertNull(WordUtils.initials(null, array));
283         assertEquals("", WordUtils.initials("", array));
284         assertEquals("", WordUtils.initials("  ", array));
285         assertEquals("I", WordUtils.initials("I", array));
286         assertEquals("i", WordUtils.initials("i", array));
287         assertEquals("S", WordUtils.initials("SJC", array));
288         assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
289         assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
290         assertEquals("BJL", WordUtils.initials(" Ben   John  . Lee", array));
291         assertEquals("KOM", WordUtils.initials("Kay O'Murphy", array));
292         assertEquals("iah1", WordUtils.initials("i am here 123", array));
293 
294         array = "SIJo1".toCharArray();
295         assertNull(WordUtils.initials(null, array));
296         assertEquals("", WordUtils.initials("", array));
297         assertEquals(" ", WordUtils.initials("  ", array));
298         assertEquals("", WordUtils.initials("I", array));
299         assertEquals("i", WordUtils.initials("i", array));
300         assertEquals("C", WordUtils.initials("SJC", array));
301         assertEquals("Bh", WordUtils.initials("Ben John Lee", array));
302         assertEquals("B.", WordUtils.initials("Ben J.Lee", array));
303         assertEquals(" h", WordUtils.initials(" Ben   John  . Lee", array));
304         assertEquals("K", WordUtils.initials("Kay O'Murphy", array));
305         assertEquals("i2", WordUtils.initials("i am here 123", array));
306     }
307 
308     @Test
309     void testInitialsSurrogatePairs() {
310         // Tests with space as default delimiter
311         assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00\uD800\uDF01 \uD800\uDF02\uD800\uDF03"));
312         assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00\uD800\uDF01 \uD800\uDF02\uD800\uDF03", null));
313         assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00 \uD800\uDF02 ", null));
314 
315         // Tests with UTF-16 as delimiters
316         assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00\uD800\uDF01.\uD800\uDF02\uD800\uDF03", new char[] { '.' }));
317         assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00\uD800\uDF01A\uD800\uDF02\uD800\uDF03", new char[] { 'A' }));
318 
319         // Tests with UTF-32 as delimiters
320         assertEquals("\uD800\uDF00\uD800\uDF02",
321                 WordUtils.initials("\uD800\uDF00\uD800\uDF01\uD800\uDF14\uD800\uDF02\uD800\uDF03", new char[] { '\uD800', '\uDF14' }));
322         assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00\uD800\uDF01\uD800\uDF14\uD800\uDF18\uD800\uDF02\uD800\uDF03",
323                 new char[] { '\uD800', '\uDF14', '\uD800', '\uDF18' }));
324     }
325 
326     @Test
327     void testIsDelimiter() {
328         assertFalse(WordUtils.isDelimiter('.', null));
329         assertTrue(WordUtils.isDelimiter(' ', null));
330 
331         assertFalse(WordUtils.isDelimiter(' ', new char[] { '.' }));
332         assertTrue(WordUtils.isDelimiter('.', new char[] { '.' }));
333 
334         assertFalse(WordUtils.isDelimiter(' ', new char[] { '.', '_', 'a' }));
335         assertTrue(WordUtils.isDelimiter('.', new char[] { '.', '_', 'a', '.' }));
336     }
337 
338     @Test
339     void testIsDelimiterCodePoint() {
340         assertFalse(WordUtils.isDelimiter((int) '.', null));
341         assertTrue(WordUtils.isDelimiter((int) ' ', null));
342 
343         assertFalse(WordUtils.isDelimiter((int) ' ', new char[] { '.' }));
344         assertTrue(WordUtils.isDelimiter((int) '.', new char[] { '.' }));
345 
346         assertFalse(WordUtils.isDelimiter((int) ' ', new char[] { '.', '_', 'a' }));
347         assertTrue(WordUtils.isDelimiter((int) '.', new char[] { '.', '_', 'a', '.' }));
348     }
349 
350     @Test
351     void testLANG1292() {
352         // Prior to fix, this was throwing StringIndexOutOfBoundsException
353         WordUtils.wrap("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
354                 70);
355     }
356 
357     @Test
358     void testLANG673() {
359         assertEquals("01", WordUtils.abbreviate("01 23 45 67 89", 0, 40, ""));
360         assertEquals("01 23 45 67", WordUtils.abbreviate("01 23 45 67 89", 10, 40, ""));
361         assertEquals("01 23 45 67 89", WordUtils.abbreviate("01 23 45 67 89", 40, 40, ""));
362     }
363 
364     @Test
365     void testSwapCase_String() {
366         assertNull(WordUtils.swapCase(null));
367         assertEquals("", WordUtils.swapCase(""));
368         assertEquals("  ", WordUtils.swapCase("  "));
369 
370         assertEquals("i", WordUtils.swapCase("I"));
371         assertEquals("I", WordUtils.swapCase("i"));
372         assertEquals("I AM HERE 123", WordUtils.swapCase("i am here 123"));
373         assertEquals("i aM hERE 123", WordUtils.swapCase("I Am Here 123"));
374         assertEquals("I AM here 123", WordUtils.swapCase("i am HERE 123"));
375         assertEquals("i am here 123", WordUtils.swapCase("I AM HERE 123"));
376 
377         final String test = "This String contains a TitleCase character: \u01C8";
378         final String expect = "tHIS sTRING CONTAINS A tITLEcASE CHARACTER: \u01C9";
379         assertEquals(expect, WordUtils.swapCase(test));
380     }
381 
382     @Test
383     void testText123() throws Exception {
384         // Prior to fix, this was throwing StringIndexOutOfBoundsException
385         WordUtils.wrap("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
386                 Integer.MAX_VALUE);
387     }
388 
389     @Test
390     void testUncapitalize_String() {
391         assertNull(WordUtils.uncapitalize(null));
392         assertEquals("", WordUtils.uncapitalize(""));
393         assertEquals("  ", WordUtils.uncapitalize("  "));
394         assertEquals("i", WordUtils.uncapitalize("I"));
395         assertEquals("i", WordUtils.uncapitalize("i"));
396         assertEquals("i am here 123", WordUtils.uncapitalize("i am here 123"));
397         assertEquals("i am here 123", WordUtils.uncapitalize("I Am Here 123"));
398         assertEquals("i am hERE 123", WordUtils.uncapitalize("i am HERE 123"));
399         assertEquals("i aM hERE 123", WordUtils.uncapitalize("I AM HERE 123"));
400         assertEquals("a\tb\nc d", WordUtils.uncapitalize("A\tB\nC D"));
401         assertEquals("and \tbut \ncLEAT  dome", WordUtils.uncapitalize("And \tBut \nCLEAT  Dome"));
402         // All whitespace
403         assertEquals(WHITESPACE, WordUtils.capitalizeFully(WHITESPACE));
404         assertEquals("A" + WHITESPACE + "B", WordUtils.capitalizeFully("a" + WHITESPACE + "b"));
405     }
406 
407     @Test
408     void testUnCapitalize_Text88() {
409         assertEquals("i am fine now", WordUtils.uncapitalize("I am fine now", new char[] {}));
410     }
411 
412     @Test
413     void testUncapitalizeWithDelimiters_String() {
414         assertNull(WordUtils.uncapitalize(null, null));
415         assertEquals("", WordUtils.uncapitalize("", ArrayUtils.EMPTY_CHAR_ARRAY));
416         assertEquals("  ", WordUtils.uncapitalize("  ", ArrayUtils.EMPTY_CHAR_ARRAY));
417 
418         char[] chars = { '-', '+', ' ', '@' };
419         assertEquals("i", WordUtils.uncapitalize("I", chars));
420         assertEquals("i", WordUtils.uncapitalize("i", chars));
421         assertEquals("i am-here+123", WordUtils.uncapitalize("i am-here+123", chars));
422         assertEquals("i+am here-123", WordUtils.uncapitalize("I+Am Here-123", chars));
423         assertEquals("i-am+hERE 123", WordUtils.uncapitalize("i-am+HERE 123", chars));
424         assertEquals("i aM-hERE+123", WordUtils.uncapitalize("I AM-HERE+123", chars));
425         chars = new char[] { '.' };
426         assertEquals("i AM.fINE", WordUtils.uncapitalize("I AM.FINE", chars));
427         assertEquals("i aM.FINE", WordUtils.uncapitalize("I AM.FINE", null));
428     }
429 
430     @Test
431     void testWrap_StringInt() {
432         assertNull(WordUtils.wrap(null, 20));
433         assertNull(WordUtils.wrap(null, -1));
434 
435         assertEquals("", WordUtils.wrap("", 20));
436         assertEquals("", WordUtils.wrap("", -1));
437 
438         // normal
439         final String systemNewLine = System.lineSeparator();
440         String input = "Here is one line of text that is going to be wrapped after 20 columns.";
441         String expected = "Here is one line of" + systemNewLine + "text that is going" + systemNewLine + "to be wrapped after" + systemNewLine + "20 columns.";
442         assertEquals(expected, WordUtils.wrap(input, 20));
443 
444         // long word at end
445         input = "Click here to jump to the commons website - https://commons.apache.org";
446         expected = "Click here to jump" + systemNewLine + "to the commons" + systemNewLine + "website -" + systemNewLine + "https://commons.apache.org";
447         assertEquals(expected, WordUtils.wrap(input, 20));
448 
449         // long word in middle
450         input = "Click here, https://commons.apache.org, to jump to the commons website";
451         expected = "Click here," + systemNewLine + "https://commons.apache.org," + systemNewLine + "to jump to the" + systemNewLine + "commons website";
452         assertEquals(expected, WordUtils.wrap(input, 20));
453 
454         // leading spaces on a new line are stripped
455         // trailing spaces are not stripped
456         input = "word1             word2                        word3";
457         expected = "word1  " + systemNewLine + "word2  " + systemNewLine + "word3";
458         assertEquals(expected, WordUtils.wrap(input, 7));
459     }
460 
461     @Test
462     void testWrap_StringIntStringBoolean() {
463         assertNull(WordUtils.wrap(null, 20, "\n", false));
464         assertNull(WordUtils.wrap(null, 20, "\n", true));
465         assertNull(WordUtils.wrap(null, 20, null, true));
466         assertNull(WordUtils.wrap(null, 20, null, false));
467         assertNull(WordUtils.wrap(null, -1, null, true));
468         assertNull(WordUtils.wrap(null, -1, null, false));
469 
470         assertEquals("", WordUtils.wrap("", 20, "\n", false));
471         assertEquals("", WordUtils.wrap("", 20, "\n", true));
472         assertEquals("", WordUtils.wrap("", 20, null, false));
473         assertEquals("", WordUtils.wrap("", 20, null, true));
474         assertEquals("", WordUtils.wrap("", -1, null, false));
475         assertEquals("", WordUtils.wrap("", -1, null, true));
476 
477         // normal
478         String input = "Here is one line of text that is going to be wrapped after 20 columns.";
479         String expected = "Here is one line of\ntext that is going\nto be wrapped after\n20 columns.";
480         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
481         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
482 
483         // unusual newline char
484         input = "Here is one line of text that is going to be wrapped after 20 columns.";
485         expected = "Here is one line of<br />text that is going<br />to be wrapped after<br />20 columns.";
486         assertEquals(expected, WordUtils.wrap(input, 20, "<br />", false));
487         assertEquals(expected, WordUtils.wrap(input, 20, "<br />", true));
488 
489         // short line length
490         input = "Here is one line";
491         expected = "Here\nis one\nline";
492         assertEquals(expected, WordUtils.wrap(input, 6, "\n", false));
493         expected = "Here\nis\none\nline";
494         assertEquals(expected, WordUtils.wrap(input, 2, "\n", false));
495         assertEquals(expected, WordUtils.wrap(input, -1, "\n", false));
496 
497         // system newline char
498         final String systemNewLine = System.lineSeparator();
499         input = "Here is one line of text that is going to be wrapped after 20 columns.";
500         expected = "Here is one line of" + systemNewLine + "text that is going" + systemNewLine + "to be wrapped after" + systemNewLine + "20 columns.";
501         assertEquals(expected, WordUtils.wrap(input, 20, null, false));
502         assertEquals(expected, WordUtils.wrap(input, 20, null, true));
503 
504         // with extra spaces
505         input = " Here:  is  one  line  of  text  that  is  going  to  be  wrapped  after  20  columns.";
506         expected = "Here:  is  one  line\nof  text  that  is \ngoing  to  be \nwrapped  after  20 \ncolumns.";
507         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
508         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
509 
510         // with tab
511         input = "Here is\tone line of text that is going to be wrapped after 20 columns.";
512         expected = "Here is\tone line of\ntext that is going\nto be wrapped after\n20 columns.";
513         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
514         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
515 
516         // with tab at wrapColumn
517         input = "Here is one line of\ttext that is going to be wrapped after 20 columns.";
518         expected = "Here is one line\nof\ttext that is\ngoing to be wrapped\nafter 20 columns.";
519         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
520         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
521 
522         // difference because of long word
523         input = "Click here to jump to the commons website - https://commons.apache.org";
524         expected = "Click here to jump\nto the commons\nwebsite -\nhttps://commons.apache.org";
525         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
526         expected = "Click here to jump\nto the commons\nwebsite -\nhttps://commons.apac\nhe.org";
527         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
528 
529         // difference because of long word in middle
530         input = "Click here, https://commons.apache.org, to jump to the commons website";
531         expected = "Click here,\nhttps://commons.apache.org,\nto jump to the\ncommons website";
532         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
533         expected = "Click here,\nhttps://commons.apac\nhe.org, to jump to\nthe commons website";
534         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
535     }
536 
537     @Test
538     void testWrap_StringIntStringBooleanString() {
539 
540         // no changes test
541         String input = "flammable/inflammable";
542         String expected = "flammable/inflammable";
543         assertEquals(expected, WordUtils.wrap(input, 30, "\n", false, "/"));
544 
545         // wrap on / and small width
546         expected = "flammable\ninflammable";
547         assertEquals(expected, WordUtils.wrap(input, 2, "\n", false, "/"));
548 
549         // wrap long words on / 1
550         expected = "flammable\ninflammab\nle";
551         assertEquals(expected, WordUtils.wrap(input, 9, "\n", true, "/"));
552 
553         // wrap long words on / 2
554         expected = "flammable\ninflammable";
555         assertEquals(expected, WordUtils.wrap(input, 15, "\n", true, "/"));
556 
557         // wrap long words on / 3
558         input = "flammableinflammable";
559         expected = "flammableinflam\nmable";
560         assertEquals(expected, WordUtils.wrap(input, 15, "\n", true, "/"));
561     }
562 
563     @Test
564     void testWrapAtMiddleTwice() {
565         assertEquals("abcdef\n\nabcdef", WordUtils.wrap("abcdefggabcdef", 2, "\n", false, "(?=g)"));
566     }
567 
568     @Test
569     void testWrapAtStartAndEnd() {
570         assertEquals("\nabcdefabcdef\n", WordUtils.wrap("nabcdefabcdefn", 2, "\n", false, "(?=n)"));
571     }
572 
573     @Test
574     void testWrapWithMultipleRegexMatchOfLength0() {
575         assertEquals("abc\ndefabc\ndef", WordUtils.wrap("abcdefabcdef", 2, "\n", false, "(?=d)"));
576     }
577 
578     @Test
579     void testWrapWithRegexMatchOfLength0() {
580         assertEquals("abc\ndef", WordUtils.wrap("abcdef", 2, "\n", false, "(?=d)"));
581     }
582 
583 }