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    *      http://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.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.assertTrue;
24  
25  import java.lang.reflect.Constructor;
26  import java.lang.reflect.Modifier;
27  
28  import org.apache.commons.lang3.AbstractLangTest;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Unit tests for WordUtils class.
33   */
34  @Deprecated
35  public class WordUtilsTest extends AbstractLangTest {
36  
37      @Test
38      public void testCapitalize_String() {
39          assertNull(WordUtils.capitalize(null));
40          assertEquals("", WordUtils.capitalize(""));
41          assertEquals("  ", WordUtils.capitalize("  "));
42  
43          assertEquals("I", WordUtils.capitalize("I") );
44          assertEquals("I", WordUtils.capitalize("i") );
45          assertEquals("I Am Here 123", WordUtils.capitalize("i am here 123") );
46          assertEquals("I Am Here 123", WordUtils.capitalize("I Am Here 123") );
47          assertEquals("I Am HERE 123", WordUtils.capitalize("i am HERE 123") );
48          assertEquals("I AM HERE 123", WordUtils.capitalize("I AM HERE 123") );
49      }
50  
51      @Test
52      public void testCapitalizeFully_String() {
53          assertNull(WordUtils.capitalizeFully(null));
54          assertEquals("", WordUtils.capitalizeFully(""));
55          assertEquals("  ", WordUtils.capitalizeFully("  "));
56  
57          assertEquals("I", WordUtils.capitalizeFully("I") );
58          assertEquals("I", WordUtils.capitalizeFully("i") );
59          assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am here 123") );
60          assertEquals("I Am Here 123", WordUtils.capitalizeFully("I Am Here 123") );
61          assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am HERE 123") );
62          assertEquals("I Am Here 123", WordUtils.capitalizeFully("I AM HERE 123") );
63      }
64  
65      @Test
66      public void testCapitalizeFullyWithDelimiters_String() {
67          assertNull(WordUtils.capitalizeFully(null, null));
68          assertEquals("", WordUtils.capitalizeFully(""));
69          assertEquals("  ", WordUtils.capitalizeFully("  "));
70  
71          char[] chars = { '-', '+', ' ', '@' };
72          assertEquals("I", WordUtils.capitalizeFully("I", chars) );
73          assertEquals("I", WordUtils.capitalizeFully("i", chars) );
74          assertEquals("I-Am Here+123", WordUtils.capitalizeFully("i-am here+123", chars) );
75          assertEquals("I Am+Here-123", WordUtils.capitalizeFully("I Am+Here-123", chars) );
76          assertEquals("I+Am-Here 123", WordUtils.capitalizeFully("i+am-HERE 123", chars) );
77          assertEquals("I-Am Here+123", WordUtils.capitalizeFully("I-AM HERE+123", chars) );
78          chars = new char[] {'.'};
79          assertEquals("I am.Fine", WordUtils.capitalizeFully("i aM.fine", chars) );
80          assertEquals("I Am.fine", WordUtils.capitalizeFully("i am.fine", null) );
81      }
82  
83      @Test
84      public void testCapitalizeWithDelimiters_String() {
85          assertNull(WordUtils.capitalize(null, null));
86          assertEquals("", WordUtils.capitalize(""));
87          assertEquals("  ", WordUtils.capitalize("  "));
88  
89          char[] chars = { '-', '+', ' ', '@' };
90          assertEquals("I", WordUtils.capitalize("I", chars) );
91          assertEquals("I", WordUtils.capitalize("i", chars) );
92          assertEquals("I-Am Here+123", WordUtils.capitalize("i-am here+123", chars) );
93          assertEquals("I Am+Here-123", WordUtils.capitalize("I Am+Here-123", chars) );
94          assertEquals("I+Am-HERE 123", WordUtils.capitalize("i+am-HERE 123", chars) );
95          assertEquals("I-AM HERE+123", WordUtils.capitalize("I-AM HERE+123", chars) );
96          chars = new char[] {'.'};
97          assertEquals("I aM.Fine", WordUtils.capitalize("i aM.fine", chars) );
98          assertEquals("I Am.fine", WordUtils.capitalize("i am.fine", null) );
99      }
100 
101     @Test
102     public void testConstructor() {
103         assertNotNull(new WordUtils());
104         final Constructor<?>[] cons = WordUtils.class.getDeclaredConstructors();
105         assertEquals(1, cons.length);
106         assertTrue(Modifier.isPublic(cons[0].getModifiers()));
107         assertTrue(Modifier.isPublic(WordUtils.class.getModifiers()));
108         assertFalse(Modifier.isFinal(WordUtils.class.getModifiers()));
109     }
110 
111     @Test
112     public void testContainsAllWords_StringString() {
113         assertFalse(WordUtils.containsAllWords(null, (String) null));
114         assertFalse(WordUtils.containsAllWords(null, ""));
115         assertFalse(WordUtils.containsAllWords(null, "ab"));
116 
117         assertFalse(WordUtils.containsAllWords("", (String) null));
118         assertFalse(WordUtils.containsAllWords("", ""));
119         assertFalse(WordUtils.containsAllWords("", "ab"));
120 
121         assertFalse(WordUtils.containsAllWords("foo", (String) null));
122         assertFalse(WordUtils.containsAllWords("bar", ""));
123         assertFalse(WordUtils.containsAllWords("zzabyycdxx", "by"));
124         assertTrue(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", "lorem", "dolor"));
125         assertFalse(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", null, "lorem", "dolor"));
126         assertFalse(WordUtils.containsAllWords("lorem ipsum null dolor sit amet", "ipsum", null, "lorem", "dolor"));
127         assertFalse(WordUtils.containsAllWords("ab", "b"));
128         assertFalse(WordUtils.containsAllWords("ab", "z"));
129     }
130 
131     @Test
132     public void testInitials_String() {
133         assertNull(WordUtils.initials(null));
134         assertEquals("", WordUtils.initials(""));
135         assertEquals("", WordUtils.initials("  "));
136 
137         assertEquals("I", WordUtils.initials("I"));
138         assertEquals("i", WordUtils.initials("i"));
139         assertEquals("BJL", WordUtils.initials("Ben John Lee"));
140         assertEquals("BJL", WordUtils.initials("   Ben \n   John\tLee\t"));
141         assertEquals("BJ", WordUtils.initials("Ben J.Lee"));
142         assertEquals("BJ.L", WordUtils.initials(" Ben   John  . Lee"));
143         assertEquals("iah1", WordUtils.initials("i am here 123"));
144     }
145 
146     @Test
147     public void testInitials_String_charArray() {
148         char[] array = null;
149         assertNull(WordUtils.initials(null, array));
150         assertEquals("", WordUtils.initials("", array));
151         assertEquals("", WordUtils.initials("  ", array));
152         assertEquals("I", WordUtils.initials("I", array));
153         assertEquals("i", WordUtils.initials("i", array));
154         assertEquals("S", WordUtils.initials("SJC", array));
155         assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
156         assertEquals("BJL", WordUtils.initials("   Ben \n   John\tLee\t", array));
157         assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
158         assertEquals("BJ.L", WordUtils.initials(" Ben   John  . Lee", array));
159         assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
160         assertEquals("iah1", WordUtils.initials("i am here 123", array));
161 
162         array = new char[0];
163         assertNull(WordUtils.initials(null, array));
164         assertEquals("", WordUtils.initials("", array));
165         assertEquals("", WordUtils.initials("  ", array));
166         assertEquals("", WordUtils.initials("I", array));
167         assertEquals("", WordUtils.initials("i", array));
168         assertEquals("", WordUtils.initials("SJC", array));
169         assertEquals("", WordUtils.initials("Ben John Lee", array));
170         assertEquals("", WordUtils.initials("   Ben \n   John\tLee\t", array));
171         assertEquals("", WordUtils.initials("Ben J.Lee", array));
172         assertEquals("", WordUtils.initials(" Ben   John  . Lee", array));
173         assertEquals("", WordUtils.initials("Kay O'Murphy", array));
174         assertEquals("", WordUtils.initials("i am here 123", array));
175 
176         array = " ".toCharArray();
177         assertNull(WordUtils.initials(null, array));
178         assertEquals("", WordUtils.initials("", array));
179         assertEquals("", WordUtils.initials("  ", array));
180         assertEquals("I", WordUtils.initials("I", array));
181         assertEquals("i", WordUtils.initials("i", array));
182         assertEquals("S", WordUtils.initials("SJC", array));
183         assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
184         assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
185         assertEquals("B\nJ", WordUtils.initials("   Ben \n   John\tLee\t", array));
186         assertEquals("BJ.L", WordUtils.initials(" Ben   John  . Lee", array));
187         assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
188         assertEquals("iah1", WordUtils.initials("i am here 123", array));
189 
190         array = " .".toCharArray();
191         assertNull(WordUtils.initials(null, array));
192         assertEquals("", WordUtils.initials("", array));
193         assertEquals("", WordUtils.initials("  ", array));
194         assertEquals("I", WordUtils.initials("I", array));
195         assertEquals("i", WordUtils.initials("i", array));
196         assertEquals("S", WordUtils.initials("SJC", array));
197         assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
198         assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
199         assertEquals("BJL", WordUtils.initials(" Ben   John  . Lee", array));
200         assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
201         assertEquals("iah1", WordUtils.initials("i am here 123", array));
202 
203         array = " .'".toCharArray();
204         assertNull(WordUtils.initials(null, array));
205         assertEquals("", WordUtils.initials("", array));
206         assertEquals("", WordUtils.initials("  ", array));
207         assertEquals("I", WordUtils.initials("I", array));
208         assertEquals("i", WordUtils.initials("i", array));
209         assertEquals("S", WordUtils.initials("SJC", array));
210         assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
211         assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
212         assertEquals("BJL", WordUtils.initials(" Ben   John  . Lee", array));
213         assertEquals("KOM", WordUtils.initials("Kay O'Murphy", array));
214         assertEquals("iah1", WordUtils.initials("i am here 123", array));
215 
216         array = "SIJo1".toCharArray();
217         assertNull(WordUtils.initials(null, array));
218         assertEquals("", WordUtils.initials("", array));
219         assertEquals(" ", WordUtils.initials("  ", array));
220         assertEquals("", WordUtils.initials("I", array));
221         assertEquals("i", WordUtils.initials("i", array));
222         assertEquals("C", WordUtils.initials("SJC", array));
223         assertEquals("Bh", WordUtils.initials("Ben John Lee", array));
224         assertEquals("B.", WordUtils.initials("Ben J.Lee", array));
225         assertEquals(" h", WordUtils.initials(" Ben   John  . Lee", array));
226         assertEquals("K", WordUtils.initials("Kay O'Murphy", array));
227         assertEquals("i2", WordUtils.initials("i am here 123", array));
228     }
229 
230     @Test
231     public void testLANG1292() {
232         // Prior to fix, this was throwing StringIndexOutOfBoundsException
233         WordUtils.wrap("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
234                 + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
235                 + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 70);
236     }
237 
238     @Test
239     public void testLANG1397() {
240         // Prior to fix, this was throwing StringIndexOutOfBoundsException
241         WordUtils.wrap("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
242             + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
243             + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Integer.MAX_VALUE);
244     }
245 
246     @Test
247     public void testSwapCase_String() {
248         assertNull(WordUtils.swapCase(null));
249         assertEquals("", WordUtils.swapCase(""));
250         assertEquals("  ", WordUtils.swapCase("  "));
251 
252         assertEquals("i", WordUtils.swapCase("I") );
253         assertEquals("I", WordUtils.swapCase("i") );
254         assertEquals("I AM HERE 123", WordUtils.swapCase("i am here 123") );
255         assertEquals("i aM hERE 123", WordUtils.swapCase("I Am Here 123") );
256         assertEquals("I AM here 123", WordUtils.swapCase("i am HERE 123") );
257         assertEquals("i am here 123", WordUtils.swapCase("I AM HERE 123") );
258 
259         final String test = "This String contains a TitleCase character: \u01C8";
260         final String expect = "tHIS sTRING CONTAINS A tITLEcASE CHARACTER: \u01C9";
261         assertEquals(expect, WordUtils.swapCase(test));
262     }
263 
264     @Test
265     public void testUncapitalize_String() {
266         assertNull(WordUtils.uncapitalize(null));
267         assertEquals("", WordUtils.uncapitalize(""));
268         assertEquals("  ", WordUtils.uncapitalize("  "));
269 
270         assertEquals("i", WordUtils.uncapitalize("I") );
271         assertEquals("i", WordUtils.uncapitalize("i") );
272         assertEquals("i am here 123", WordUtils.uncapitalize("i am here 123") );
273         assertEquals("i am here 123", WordUtils.uncapitalize("I Am Here 123") );
274         assertEquals("i am hERE 123", WordUtils.uncapitalize("i am HERE 123") );
275         assertEquals("i aM hERE 123", WordUtils.uncapitalize("I AM HERE 123") );
276     }
277 
278     @Test
279     public void testUncapitalizeWithDelimiters_String() {
280         assertNull(WordUtils.uncapitalize(null, null));
281         assertEquals("", WordUtils.uncapitalize(""));
282         assertEquals("  ", WordUtils.uncapitalize("  "));
283 
284         char[] chars = { '-', '+', ' ', '@' };
285         assertEquals("i", WordUtils.uncapitalize("I", chars) );
286         assertEquals("i", WordUtils.uncapitalize("i", chars) );
287         assertEquals("i am-here+123", WordUtils.uncapitalize("i am-here+123", chars) );
288         assertEquals("i+am here-123", WordUtils.uncapitalize("I+Am Here-123", chars) );
289         assertEquals("i-am+hERE 123", WordUtils.uncapitalize("i-am+HERE 123", chars) );
290         assertEquals("i aM-hERE+123", WordUtils.uncapitalize("I AM-HERE+123", chars) );
291         chars = new char[] {'.'};
292         assertEquals("i AM.fINE", WordUtils.uncapitalize("I AM.FINE", chars) );
293         assertEquals("i aM.FINE", WordUtils.uncapitalize("I AM.FINE", null) );
294     }
295 
296     @Test
297     public void testWrap_StringInt() {
298         assertNull(WordUtils.wrap(null, 20));
299         assertNull(WordUtils.wrap(null, -1));
300 
301         assertEquals("", WordUtils.wrap("", 20));
302         assertEquals("", WordUtils.wrap("", -1));
303 
304         // normal
305         final String systemNewLine = System.lineSeparator();
306         String input = "Here is one line of text that is going to be wrapped after 20 columns.";
307         String expected = "Here is one line of" + systemNewLine + "text that is going"
308             + systemNewLine + "to be wrapped after" + systemNewLine + "20 columns.";
309         assertEquals(expected, WordUtils.wrap(input, 20));
310 
311         // long word at end
312         input = "Click here to jump to the commons website - https://commons.apache.org";
313         expected = "Click here to jump" + systemNewLine + "to the commons" + systemNewLine
314             + "website -" + systemNewLine + "https://commons.apache.org";
315         assertEquals(expected, WordUtils.wrap(input, 20));
316 
317         // long word in middle
318         input = "Click here, https://commons.apache.org, to jump to the commons website";
319         expected = "Click here," + systemNewLine + "https://commons.apache.org," + systemNewLine
320             + "to jump to the" + systemNewLine + "commons website";
321         assertEquals(expected, WordUtils.wrap(input, 20));
322 
323         // leading spaces on a new line are stripped
324         // trailing spaces are not stripped
325         input = "word1             word2                        word3";
326         expected = "word1  " + systemNewLine + "word2  " + systemNewLine + "word3";
327         assertEquals(expected, WordUtils.wrap(input, 7));
328     }
329 
330     @Test
331     public void testWrap_StringIntStringBoolean() {
332         assertNull(WordUtils.wrap(null, 20, "\n", false));
333         assertNull(WordUtils.wrap(null, 20, "\n", true));
334         assertNull(WordUtils.wrap(null, 20, null, true));
335         assertNull(WordUtils.wrap(null, 20, null, false));
336         assertNull(WordUtils.wrap(null, -1, null, true));
337         assertNull(WordUtils.wrap(null, -1, null, false));
338 
339         assertEquals("", WordUtils.wrap("", 20, "\n", false));
340         assertEquals("", WordUtils.wrap("", 20, "\n", true));
341         assertEquals("", WordUtils.wrap("", 20, null, false));
342         assertEquals("", WordUtils.wrap("", 20, null, true));
343         assertEquals("", WordUtils.wrap("", -1, null, false));
344         assertEquals("", WordUtils.wrap("", -1, null, true));
345 
346         // normal
347         String input = "Here is one line of text that is going to be wrapped after 20 columns.";
348         String expected = "Here is one line of\ntext that is going\nto be wrapped after\n20 columns.";
349         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
350         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
351 
352         // unusual newline char
353         input = "Here is one line of text that is going to be wrapped after 20 columns.";
354         expected = "Here is one line of<br />text that is going<br />to be wrapped after<br />20 columns.";
355         assertEquals(expected, WordUtils.wrap(input, 20, "<br />", false));
356         assertEquals(expected, WordUtils.wrap(input, 20, "<br />", true));
357 
358         // short line length
359         input = "Here is one line";
360         expected = "Here\nis one\nline";
361         assertEquals(expected, WordUtils.wrap(input, 6, "\n", false));
362         expected = "Here\nis\none\nline";
363         assertEquals(expected, WordUtils.wrap(input, 2, "\n", false));
364         assertEquals(expected, WordUtils.wrap(input, -1, "\n", false));
365 
366         // system newline char
367         final String systemNewLine = System.lineSeparator();
368         input = "Here is one line of text that is going to be wrapped after 20 columns.";
369         expected = "Here is one line of" + systemNewLine + "text that is going" + systemNewLine
370             + "to be wrapped after" + systemNewLine + "20 columns.";
371         assertEquals(expected, WordUtils.wrap(input, 20, null, false));
372         assertEquals(expected, WordUtils.wrap(input, 20, null, true));
373 
374         // with extra spaces
375         input = " Here:  is  one  line  of  text  that  is  going  to  be  wrapped  after  20  columns.";
376         expected = "Here:  is  one  line\nof  text  that  is \ngoing  to  be \nwrapped  after  20 \ncolumns.";
377         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
378         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
379 
380         // with tab
381         input = "Here is\tone line of text that is going to be wrapped after 20 columns.";
382         expected = "Here is\tone line of\ntext that is going\nto be wrapped after\n20 columns.";
383         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
384         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
385 
386         // with tab at wrapColumn
387         input = "Here is one line of\ttext that is going to be wrapped after 20 columns.";
388         expected = "Here is one line\nof\ttext that is\ngoing to be wrapped\nafter 20 columns.";
389         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
390         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
391 
392         // difference because of long word
393         input = "Click here to jump to the commons website - https://commons.apache.org";
394         expected = "Click here to jump\nto the commons\nwebsite -\nhttps://commons.apache.org";
395         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
396         expected = "Click here to jump\nto the commons\nwebsite -\nhttps://commons.apac\nhe.org";
397         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
398 
399         // difference because of long word in middle
400         input = "Click here, https://commons.apache.org, to jump to the commons website";
401         expected = "Click here,\nhttps://commons.apache.org,\nto jump to the\ncommons website";
402         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
403         expected = "Click here,\nhttps://commons.apac\nhe.org, to jump to\nthe commons website";
404         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
405     }
406 
407     @Test
408     public void testWrap_StringIntStringBooleanString() {
409 
410         //no changes test
411         String input = "flammable/inflammable";
412         String expected = "flammable/inflammable";
413         assertEquals(expected, WordUtils.wrap(input, 30, "\n", false, "/"));
414 
415         // wrap on / and small width
416         expected = "flammable\ninflammable";
417         assertEquals(expected, WordUtils.wrap(input, 2, "\n", false, "/"));
418 
419         // wrap long words on / 1
420         expected = "flammable\ninflammab\nle";
421         assertEquals(expected, WordUtils.wrap(input, 9, "\n", true, "/"));
422 
423         // wrap long words on / 2
424         expected = "flammable\ninflammable";
425         assertEquals(expected, WordUtils.wrap(input, 15, "\n", true, "/"));
426 
427         // wrap long words on / 3
428         input = "flammableinflammable";
429         expected = "flammableinflam\nmable";
430         assertEquals(expected, WordUtils.wrap(input, 15, "\n", true, "/"));
431     }
432 }