1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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"));
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
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));
147 assertEquals("Alphabet", WordUtils.capitalizeFully("alphabet", new char[] { '!' }));
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, (String) null));
181 assertFalse(WordUtils.containsAllWords(null, ""));
182 assertFalse(WordUtils.containsAllWords(null, "ab"));
183
184 assertFalse(WordUtils.containsAllWords("", (String) null));
185 assertFalse(WordUtils.containsAllWords("", ""));
186 assertFalse(WordUtils.containsAllWords("", "ab"));
187
188 assertFalse(WordUtils.containsAllWords("foo", (String) null));
189 assertFalse(WordUtils.containsAllWords("bar", ""));
190 assertFalse(WordUtils.containsAllWords("zzabyycdxx", "by"));
191 assertTrue(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", "lorem", "dolor"));
192 assertFalse(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", null, "lorem", "dolor"));
193 assertFalse(WordUtils.containsAllWords("lorem ipsum null dolor sit amet", "ipsum", null, "lorem", "dolor"));
194 assertFalse(WordUtils.containsAllWords("ab", "b"));
195 assertFalse(WordUtils.containsAllWords("ab", "z"));
196 assertFalse(WordUtils.containsAllWords("ab", "["));
197 assertFalse(WordUtils.containsAllWords("ab", "]"));
198 assertFalse(WordUtils.containsAllWords("ab", "*"));
199 assertTrue(WordUtils.containsAllWords("ab x", "ab", "x"));
200 }
201
202 @Test
203 void testContainsAllWordsWithNull() {
204 assertFalse(WordUtils.containsAllWords("M", (CharSequence) null));
205 }
206
207 @Test
208 void testInitials_String() {
209 assertNull(WordUtils.initials(null));
210 assertEquals("", WordUtils.initials(""));
211 assertEquals("", WordUtils.initials(" "));
212
213 assertEquals("I", WordUtils.initials("I"));
214 assertEquals("i", WordUtils.initials("i"));
215 assertEquals("BJL", WordUtils.initials("Ben John Lee"));
216 assertEquals("BJL", WordUtils.initials(" Ben \n John\tLee\t"));
217 assertEquals("BJ", WordUtils.initials("Ben J.Lee"));
218 assertEquals("BJ.L", WordUtils.initials(" Ben John . Lee"));
219 assertEquals("iah1", WordUtils.initials("i am here 123"));
220 }
221
222 @Test
223 void testInitials_String_charArray() {
224 char[] array = null;
225 assertNull(WordUtils.initials(null, array));
226 assertEquals("", WordUtils.initials("", array));
227 assertEquals("", WordUtils.initials(" ", array));
228 assertEquals("I", WordUtils.initials("I", array));
229 assertEquals("i", WordUtils.initials("i", array));
230 assertEquals("S", WordUtils.initials("SJC", array));
231 assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
232 assertEquals("BJL", WordUtils.initials(" Ben \n John\tLee\t", array));
233 assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
234 assertEquals("BJ.L", WordUtils.initials(" Ben John . Lee", array));
235 assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
236 assertEquals("iah1", WordUtils.initials("i am here 123", array));
237
238 array = ArrayUtils.EMPTY_CHAR_ARRAY;
239 assertNull(WordUtils.initials(null, array));
240 assertEquals("", WordUtils.initials("", array));
241 assertEquals("", WordUtils.initials(" ", array));
242 assertEquals("", WordUtils.initials("I", array));
243 assertEquals("", WordUtils.initials("i", array));
244 assertEquals("", WordUtils.initials("SJC", array));
245 assertEquals("", WordUtils.initials("Ben John Lee", array));
246 assertEquals("", WordUtils.initials(" Ben \n John\tLee\t", array));
247 assertEquals("", WordUtils.initials("Ben J.Lee", array));
248 assertEquals("", WordUtils.initials(" Ben John . Lee", array));
249 assertEquals("", WordUtils.initials("Kay O'Murphy", array));
250 assertEquals("", WordUtils.initials("i am here 123", array));
251
252 array = " ".toCharArray();
253 assertNull(WordUtils.initials(null, array));
254 assertEquals("", WordUtils.initials("", array));
255 assertEquals("", WordUtils.initials(" ", array));
256 assertEquals("I", WordUtils.initials("I", array));
257 assertEquals("i", WordUtils.initials("i", array));
258 assertEquals("S", WordUtils.initials("SJC", array));
259 assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
260 assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
261 assertEquals("B\nJ", WordUtils.initials(" Ben \n John\tLee\t", array));
262 assertEquals("BJ.L", WordUtils.initials(" Ben John . Lee", array));
263 assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
264 assertEquals("iah1", WordUtils.initials("i am here 123", array));
265
266 array = " .".toCharArray();
267 assertNull(WordUtils.initials(null, array));
268 assertEquals("", WordUtils.initials("", array));
269 assertEquals("", WordUtils.initials(" ", array));
270 assertEquals("I", WordUtils.initials("I", array));
271 assertEquals("i", WordUtils.initials("i", array));
272 assertEquals("S", WordUtils.initials("SJC", array));
273 assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
274 assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
275 assertEquals("BJL", WordUtils.initials(" Ben John . Lee", array));
276 assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
277 assertEquals("iah1", WordUtils.initials("i am here 123", array));
278
279 array = " .'".toCharArray();
280 assertNull(WordUtils.initials(null, array));
281 assertEquals("", WordUtils.initials("", array));
282 assertEquals("", WordUtils.initials(" ", array));
283 assertEquals("I", WordUtils.initials("I", array));
284 assertEquals("i", WordUtils.initials("i", array));
285 assertEquals("S", WordUtils.initials("SJC", array));
286 assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
287 assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
288 assertEquals("BJL", WordUtils.initials(" Ben John . Lee", array));
289 assertEquals("KOM", WordUtils.initials("Kay O'Murphy", array));
290 assertEquals("iah1", WordUtils.initials("i am here 123", array));
291
292 array = "SIJo1".toCharArray();
293 assertNull(WordUtils.initials(null, array));
294 assertEquals("", WordUtils.initials("", array));
295 assertEquals(" ", WordUtils.initials(" ", array));
296 assertEquals("", WordUtils.initials("I", array));
297 assertEquals("i", WordUtils.initials("i", array));
298 assertEquals("C", WordUtils.initials("SJC", array));
299 assertEquals("Bh", WordUtils.initials("Ben John Lee", array));
300 assertEquals("B.", WordUtils.initials("Ben J.Lee", array));
301 assertEquals(" h", WordUtils.initials(" Ben John . Lee", array));
302 assertEquals("K", WordUtils.initials("Kay O'Murphy", array));
303 assertEquals("i2", WordUtils.initials("i am here 123", array));
304 }
305
306 @Test
307 void testInitialsSurrogatePairs() {
308
309 assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00\uD800\uDF01 \uD800\uDF02\uD800\uDF03"));
310 assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00\uD800\uDF01 \uD800\uDF02\uD800\uDF03", null));
311 assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00 \uD800\uDF02 ", null));
312
313
314 assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00\uD800\uDF01.\uD800\uDF02\uD800\uDF03", new char[] { '.' }));
315 assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00\uD800\uDF01A\uD800\uDF02\uD800\uDF03", new char[] { 'A' }));
316
317
318 assertEquals("\uD800\uDF00\uD800\uDF02",
319 WordUtils.initials("\uD800\uDF00\uD800\uDF01\uD800\uDF14\uD800\uDF02\uD800\uDF03", new char[] { '\uD800', '\uDF14' }));
320 assertEquals("\uD800\uDF00\uD800\uDF02", WordUtils.initials("\uD800\uDF00\uD800\uDF01\uD800\uDF14\uD800\uDF18\uD800\uDF02\uD800\uDF03",
321 new char[] { '\uD800', '\uDF14', '\uD800', '\uDF18' }));
322 }
323
324 @Test
325 void testLANG1292() {
326
327 WordUtils.wrap("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
328 70);
329 }
330
331 @Test
332 void testLANG673() {
333 assertEquals("01", WordUtils.abbreviate("01 23 45 67 89", 0, 40, ""));
334 assertEquals("01 23 45 67", WordUtils.abbreviate("01 23 45 67 89", 10, 40, ""));
335 assertEquals("01 23 45 67 89", WordUtils.abbreviate("01 23 45 67 89", 40, 40, ""));
336 }
337
338 @Test
339 void testSwapCase_String() {
340 assertNull(WordUtils.swapCase(null));
341 assertEquals("", WordUtils.swapCase(""));
342 assertEquals(" ", WordUtils.swapCase(" "));
343
344 assertEquals("i", WordUtils.swapCase("I"));
345 assertEquals("I", WordUtils.swapCase("i"));
346 assertEquals("I AM HERE 123", WordUtils.swapCase("i am here 123"));
347 assertEquals("i aM hERE 123", WordUtils.swapCase("I Am Here 123"));
348 assertEquals("I AM here 123", WordUtils.swapCase("i am HERE 123"));
349 assertEquals("i am here 123", WordUtils.swapCase("I AM HERE 123"));
350
351 final String test = "This String contains a TitleCase character: \u01C8";
352 final String expect = "tHIS sTRING CONTAINS A tITLEcASE CHARACTER: \u01C9";
353 assertEquals(expect, WordUtils.swapCase(test));
354 }
355
356 @Test
357 void testText123() throws Exception {
358
359 WordUtils.wrap("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
360 Integer.MAX_VALUE);
361 }
362
363 @Test
364 void testUncapitalize_String() {
365 assertNull(WordUtils.uncapitalize(null));
366 assertEquals("", WordUtils.uncapitalize(""));
367 assertEquals(" ", WordUtils.uncapitalize(" "));
368 assertEquals("i", WordUtils.uncapitalize("I"));
369 assertEquals("i", WordUtils.uncapitalize("i"));
370 assertEquals("i am here 123", WordUtils.uncapitalize("i am here 123"));
371 assertEquals("i am here 123", WordUtils.uncapitalize("I Am Here 123"));
372 assertEquals("i am hERE 123", WordUtils.uncapitalize("i am HERE 123"));
373 assertEquals("i aM hERE 123", WordUtils.uncapitalize("I AM HERE 123"));
374 assertEquals("a\tb\nc d", WordUtils.uncapitalize("A\tB\nC D"));
375 assertEquals("and \tbut \ncLEAT dome", WordUtils.uncapitalize("And \tBut \nCLEAT Dome"));
376
377 assertEquals(WHITESPACE, WordUtils.capitalizeFully(WHITESPACE));
378 assertEquals("A" + WHITESPACE + "B", WordUtils.capitalizeFully("a" + WHITESPACE + "b"));
379 }
380
381 @Test
382 void testUnCapitalize_Text88() {
383 assertEquals("i am fine now", WordUtils.uncapitalize("I am fine now", new char[] {}));
384 }
385
386 @Test
387 void testUncapitalizeWithDelimiters_String() {
388 assertNull(WordUtils.uncapitalize(null, null));
389 assertEquals("", WordUtils.uncapitalize("", ArrayUtils.EMPTY_CHAR_ARRAY));
390 assertEquals(" ", WordUtils.uncapitalize(" ", ArrayUtils.EMPTY_CHAR_ARRAY));
391
392 char[] chars = { '-', '+', ' ', '@' };
393 assertEquals("i", WordUtils.uncapitalize("I", chars));
394 assertEquals("i", WordUtils.uncapitalize("i", chars));
395 assertEquals("i am-here+123", WordUtils.uncapitalize("i am-here+123", chars));
396 assertEquals("i+am here-123", WordUtils.uncapitalize("I+Am Here-123", chars));
397 assertEquals("i-am+hERE 123", WordUtils.uncapitalize("i-am+HERE 123", chars));
398 assertEquals("i aM-hERE+123", WordUtils.uncapitalize("I AM-HERE+123", chars));
399 chars = new char[] { '.' };
400 assertEquals("i AM.fINE", WordUtils.uncapitalize("I AM.FINE", chars));
401 assertEquals("i aM.FINE", WordUtils.uncapitalize("I AM.FINE", null));
402 }
403
404 @Test
405 void testWrap_StringInt() {
406 assertNull(WordUtils.wrap(null, 20));
407 assertNull(WordUtils.wrap(null, -1));
408
409 assertEquals("", WordUtils.wrap("", 20));
410 assertEquals("", WordUtils.wrap("", -1));
411
412
413 final String systemNewLine = System.lineSeparator();
414 String input = "Here is one line of text that is going to be wrapped after 20 columns.";
415 String expected = "Here is one line of" + systemNewLine + "text that is going" + systemNewLine + "to be wrapped after" + systemNewLine + "20 columns.";
416 assertEquals(expected, WordUtils.wrap(input, 20));
417
418
419 input = "Click here to jump to the commons website - https://commons.apache.org";
420 expected = "Click here to jump" + systemNewLine + "to the commons" + systemNewLine + "website -" + systemNewLine + "https://commons.apache.org";
421 assertEquals(expected, WordUtils.wrap(input, 20));
422
423
424 input = "Click here, https://commons.apache.org, to jump to the commons website";
425 expected = "Click here," + systemNewLine + "https://commons.apache.org," + systemNewLine + "to jump to the" + systemNewLine + "commons website";
426 assertEquals(expected, WordUtils.wrap(input, 20));
427
428
429
430 input = "word1 word2 word3";
431 expected = "word1 " + systemNewLine + "word2 " + systemNewLine + "word3";
432 assertEquals(expected, WordUtils.wrap(input, 7));
433 }
434
435 @Test
436 void testWrap_StringIntStringBoolean() {
437 assertNull(WordUtils.wrap(null, 20, "\n", false));
438 assertNull(WordUtils.wrap(null, 20, "\n", true));
439 assertNull(WordUtils.wrap(null, 20, null, true));
440 assertNull(WordUtils.wrap(null, 20, null, false));
441 assertNull(WordUtils.wrap(null, -1, null, true));
442 assertNull(WordUtils.wrap(null, -1, null, false));
443
444 assertEquals("", WordUtils.wrap("", 20, "\n", false));
445 assertEquals("", WordUtils.wrap("", 20, "\n", true));
446 assertEquals("", WordUtils.wrap("", 20, null, false));
447 assertEquals("", WordUtils.wrap("", 20, null, true));
448 assertEquals("", WordUtils.wrap("", -1, null, false));
449 assertEquals("", WordUtils.wrap("", -1, null, true));
450
451
452 String input = "Here is one line of text that is going to be wrapped after 20 columns.";
453 String expected = "Here is one line of\ntext that is going\nto be wrapped after\n20 columns.";
454 assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
455 assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
456
457
458 input = "Here is one line of text that is going to be wrapped after 20 columns.";
459 expected = "Here is one line of<br />text that is going<br />to be wrapped after<br />20 columns.";
460 assertEquals(expected, WordUtils.wrap(input, 20, "<br />", false));
461 assertEquals(expected, WordUtils.wrap(input, 20, "<br />", true));
462
463
464 input = "Here is one line";
465 expected = "Here\nis one\nline";
466 assertEquals(expected, WordUtils.wrap(input, 6, "\n", false));
467 expected = "Here\nis\none\nline";
468 assertEquals(expected, WordUtils.wrap(input, 2, "\n", false));
469 assertEquals(expected, WordUtils.wrap(input, -1, "\n", false));
470
471
472 final String systemNewLine = System.lineSeparator();
473 input = "Here is one line of text that is going to be wrapped after 20 columns.";
474 expected = "Here is one line of" + systemNewLine + "text that is going" + systemNewLine + "to be wrapped after" + systemNewLine + "20 columns.";
475 assertEquals(expected, WordUtils.wrap(input, 20, null, false));
476 assertEquals(expected, WordUtils.wrap(input, 20, null, true));
477
478
479 input = " Here: is one line of text that is going to be wrapped after 20 columns.";
480 expected = "Here: is one line\nof text that is \ngoing to be \nwrapped after 20 \ncolumns.";
481 assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
482 assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
483
484
485 input = "Here is\tone line of text that is going to be wrapped after 20 columns.";
486 expected = "Here is\tone line of\ntext that is going\nto be wrapped after\n20 columns.";
487 assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
488 assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
489
490
491 input = "Here is one line of\ttext that is going to be wrapped after 20 columns.";
492 expected = "Here is one line\nof\ttext that is\ngoing to be wrapped\nafter 20 columns.";
493 assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
494 assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
495
496
497 input = "Click here to jump to the commons website - https://commons.apache.org";
498 expected = "Click here to jump\nto the commons\nwebsite -\nhttps://commons.apache.org";
499 assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
500 expected = "Click here to jump\nto the commons\nwebsite -\nhttps://commons.apac\nhe.org";
501 assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
502
503
504 input = "Click here, https://commons.apache.org, to jump to the commons website";
505 expected = "Click here,\nhttps://commons.apache.org,\nto jump to the\ncommons website";
506 assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
507 expected = "Click here,\nhttps://commons.apac\nhe.org, to jump to\nthe commons website";
508 assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
509 }
510
511 @Test
512 void testWrap_StringIntStringBooleanString() {
513
514
515 String input = "flammable/inflammable";
516 String expected = "flammable/inflammable";
517 assertEquals(expected, WordUtils.wrap(input, 30, "\n", false, "/"));
518
519
520 expected = "flammable\ninflammable";
521 assertEquals(expected, WordUtils.wrap(input, 2, "\n", false, "/"));
522
523
524 expected = "flammable\ninflammab\nle";
525 assertEquals(expected, WordUtils.wrap(input, 9, "\n", true, "/"));
526
527
528 expected = "flammable\ninflammable";
529 assertEquals(expected, WordUtils.wrap(input, 15, "\n", true, "/"));
530
531
532 input = "flammableinflammable";
533 expected = "flammableinflam\nmable";
534 assertEquals(expected, WordUtils.wrap(input, 15, "\n", true, "/"));
535 }
536
537 @Test
538 void testWrapAtMiddleTwice() {
539 assertEquals("abcdef\n\nabcdef", WordUtils.wrap("abcdefggabcdef", 2, "\n", false, "(?=g)"));
540 }
541
542 @Test
543 void testWrapAtStartAndEnd() {
544 assertEquals("\nabcdefabcdef\n", WordUtils.wrap("nabcdefabcdefn", 2, "\n", false, "(?=n)"));
545 }
546
547 @Test
548 void testWrapWithMultipleRegexMatchOfLength0() {
549 assertEquals("abc\ndefabc\ndef", WordUtils.wrap("abcdefabcdef", 2, "\n", false, "(?=d)"));
550 }
551
552 @Test
553 void testWrapWithRegexMatchOfLength0() {
554 assertEquals("abc\ndef", WordUtils.wrap("abcdef", 2, "\n", false, "(?=d)"));
555 }
556
557 }