View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.util;
16  
17  import static java.nio.charset.StandardCharsets.UTF_8;
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import org.htmlunit.html.impl.Color;
25  import org.junit.jupiter.api.Assertions;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests for {@link StringUtils}.
30   *
31   * @author Ronald Brill
32   */
33  public class StringUtilsTest {
34  
35      /**
36       * @throws Exception if the test fails
37       */
38      @Test
39      public void isEmptyString() throws Exception {
40          assertFalse(StringUtils.isEmptyString(null));
41          assertTrue(StringUtils.isEmptyString(""));
42          assertFalse(StringUtils.isEmptyString(" "));
43          assertFalse(StringUtils.isEmptyString("\t"));
44          assertFalse(StringUtils.isEmptyString("\r"));
45          assertFalse(StringUtils.isEmptyString("\n"));
46          assertFalse(StringUtils.isEmptyString("string"));
47      }
48  
49      /**
50       * @throws Exception if the test fails
51       */
52      @Test
53      public void isEmptyOrNull() throws Exception {
54          assertTrue(StringUtils.isEmptyOrNull(null));
55          assertTrue(StringUtils.isEmptyOrNull(""));
56          assertFalse(StringUtils.isEmptyOrNull(" "));
57          assertFalse(StringUtils.isEmptyOrNull("\t"));
58          assertFalse(StringUtils.isEmptyOrNull("\r"));
59          assertFalse(StringUtils.isEmptyOrNull("\n"));
60          assertFalse(StringUtils.isEmptyOrNull("string"));
61      }
62  
63  
64      /**
65       * @throws Exception if the test fails
66       */
67      @Test
68      public void defaultIfEmptyOrNull() throws Exception {
69          assertEquals("X", StringUtils.defaultIfEmptyOrNull(null, "X"));
70          assertEquals("X", StringUtils.defaultIfEmptyOrNull("", "X"));
71  
72          assertEquals(" ", StringUtils.defaultIfEmptyOrNull(" ", "X"));
73          assertEquals("a", StringUtils.defaultIfEmptyOrNull("a", "X"));
74      }
75  
76      /**
77       * @throws Exception if the test fails
78       */
79      @Test
80      public void isBlank() throws Exception {
81          assertTrue(StringUtils.isBlank(null));
82          assertTrue(StringUtils.isBlank(""));
83          assertTrue(StringUtils.isBlank(" "));
84          assertTrue(StringUtils.isBlank("\t"));
85          assertTrue(StringUtils.isBlank("\r"));
86          assertTrue(StringUtils.isBlank("\n"));
87  
88          assertFalse(StringUtils.isBlank("x"));
89          assertFalse(StringUtils.isBlank("string"));
90          assertFalse(StringUtils.isBlank("    x"));
91      }
92  
93      /**
94       * @throws Exception if the test fails
95       */
96      @Test
97      public void isNotBlank() throws Exception {
98          assertFalse(StringUtils.isNotBlank(null));
99          assertFalse(StringUtils.isNotBlank(""));
100         assertFalse(StringUtils.isNotBlank(" "));
101         assertFalse(StringUtils.isNotBlank("\t"));
102         assertFalse(StringUtils.isNotBlank("\r"));
103         assertFalse(StringUtils.isNotBlank("\n"));
104 
105         assertTrue(StringUtils.isNotBlank("x"));
106         assertTrue(StringUtils.isNotBlank("string"));
107         assertTrue(StringUtils.isNotBlank("    x"));
108     }
109 
110     /**
111      * @throws Exception if the test fails
112      */
113     @Test
114     public void equalsChar() throws Exception {
115         assertFalse(StringUtils.equalsChar('#', null));
116         assertFalse(StringUtils.equalsChar('#', ""));
117         assertFalse(StringUtils.equalsChar('#', " "));
118         assertTrue(StringUtils.equalsChar('#', "#"));
119         assertFalse(StringUtils.equalsChar('#', "##"));
120         assertFalse(StringUtils.equalsChar('#', " #"));
121         assertFalse(StringUtils.equalsChar('#', "# "));
122     }
123 
124     /**
125      * @throws Exception if the test fails
126      */
127     @Test
128     public void startsWithIgnoreCase() throws Exception {
129         assertTrue(StringUtils.startsWithIgnoreCase("abcd", "a"));
130         assertTrue(StringUtils.startsWithIgnoreCase("abcd", "ab"));
131         assertTrue(StringUtils.startsWithIgnoreCase("abcd", "abcd"));
132         assertTrue(StringUtils.startsWithIgnoreCase("abcd", "A"));
133         assertTrue(StringUtils.startsWithIgnoreCase("abcd", "AB"));
134         assertTrue(StringUtils.startsWithIgnoreCase("Abcd", "abCd"));
135 
136         assertFalse(StringUtils.startsWithIgnoreCase("AB", "x"));
137         assertFalse(StringUtils.startsWithIgnoreCase("AB", "xxzOO"));
138         assertFalse(StringUtils.startsWithIgnoreCase("", "x"));
139         assertFalse(StringUtils.startsWithIgnoreCase("abcd", "bc"));
140 
141         assertFalse(StringUtils.startsWithIgnoreCase(null, "x"));
142 
143         Assertions.assertThrows(IllegalArgumentException.class, () -> StringUtils.startsWithIgnoreCase("AB", null));
144         Assertions.assertThrows(IllegalArgumentException.class, () -> StringUtils.startsWithIgnoreCase("AB", ""));
145         Assertions.assertThrows(IllegalArgumentException.class, () -> StringUtils.startsWithIgnoreCase("", ""));
146     }
147 
148     /**
149      * @throws Exception if the test fails
150      */
151     @Test
152     public void endsWithIgnoreCase() throws Exception {
153         assertTrue(StringUtils.endsWithIgnoreCase("abcd", "d"));
154         assertTrue(StringUtils.endsWithIgnoreCase("abcd", "cd"));
155         assertTrue(StringUtils.endsWithIgnoreCase("abcd", "abcd"));
156         assertTrue(StringUtils.endsWithIgnoreCase("abcd", "D"));
157         assertTrue(StringUtils.endsWithIgnoreCase("abcd", "CD"));
158         assertTrue(StringUtils.endsWithIgnoreCase("Abcd", "abCd"));
159 
160         assertFalse(StringUtils.endsWithIgnoreCase("AB", "x"));
161         assertFalse(StringUtils.endsWithIgnoreCase("AB", "xxzOO"));
162         assertFalse(StringUtils.endsWithIgnoreCase("", "x"));
163         assertFalse(StringUtils.endsWithIgnoreCase("abcd", "bc"));
164 
165         assertFalse(StringUtils.endsWithIgnoreCase(null, "x"));
166 
167         Assertions.assertThrows(IllegalArgumentException.class, () -> StringUtils.endsWithIgnoreCase("AB", null));
168         Assertions.assertThrows(IllegalArgumentException.class, () -> StringUtils.endsWithIgnoreCase("AB", ""));
169         Assertions.assertThrows(IllegalArgumentException.class, () -> StringUtils.endsWithIgnoreCase("", ""));
170     }
171 
172     /**
173      * @throws Exception if the test fails
174      */
175     @Test
176     public void containsIgnoreCase() throws Exception {
177         assertTrue(StringUtils.containsIgnoreCase("abcd", "a"));
178         assertTrue(StringUtils.containsIgnoreCase("abcd", "b"));
179         assertTrue(StringUtils.containsIgnoreCase("abcd", "c"));
180         assertTrue(StringUtils.containsIgnoreCase("abcd", "d"));
181         assertTrue(StringUtils.containsIgnoreCase("abcd", "A"));
182         assertTrue(StringUtils.containsIgnoreCase("abcd", "B"));
183         assertTrue(StringUtils.containsIgnoreCase("abcd", "C"));
184         assertTrue(StringUtils.containsIgnoreCase("abcd", "D"));
185         assertTrue(StringUtils.containsIgnoreCase("abcd", "cd"));
186         assertTrue(StringUtils.containsIgnoreCase("abcd", "Cd"));
187         assertTrue(StringUtils.containsIgnoreCase("abcd", "abcd"));
188         assertTrue(StringUtils.containsIgnoreCase("abcd", "D"));
189         assertTrue(StringUtils.containsIgnoreCase("abcd", "CD"));
190         assertTrue(StringUtils.containsIgnoreCase("Abcd", "abCd"));
191 
192         assertFalse(StringUtils.containsIgnoreCase("AB", "x"));
193         assertFalse(StringUtils.containsIgnoreCase("AB", "xxzOO"));
194         assertFalse(StringUtils.containsIgnoreCase("", "x"));
195         assertFalse(StringUtils.containsIgnoreCase("abcd", "bd"));
196 
197         assertFalse(StringUtils.containsIgnoreCase(null, "x"));
198 
199         Assertions.assertThrows(IllegalArgumentException.class, () -> StringUtils.containsIgnoreCase("AB", null));
200         Assertions.assertThrows(IllegalArgumentException.class, () -> StringUtils.containsIgnoreCase("AB", ""));
201         Assertions.assertThrows(IllegalArgumentException.class, () -> StringUtils.containsIgnoreCase("", ""));
202     }
203 
204 
205     /**
206      * @throws Exception if the test fails
207      */
208     @Test
209     public void containsOnly() {
210         assertFalse(StringUtils.containsOnly(null, "x".toCharArray()));
211         assertFalse(StringUtils.containsOnly("", "x".toCharArray()));
212         assertFalse(StringUtils.containsOnly("a", "x".toCharArray()));
213         assertFalse(StringUtils.containsOnly("ax", "x".toCharArray()));
214         assertFalse(StringUtils.containsOnly("xa", "x".toCharArray()));
215 
216         assertTrue(StringUtils.containsOnly("x", "ax".toCharArray()));
217         assertTrue(StringUtils.containsOnly("aa", "ax".toCharArray()));
218         assertTrue(StringUtils.containsOnly("ax", "ax".toCharArray()));
219         assertTrue(StringUtils.containsOnly("axaaa", "ax".toCharArray()));
220         assertTrue(StringUtils.containsOnly("xaaxxxaa", "xa".toCharArray()));
221 
222         Assertions.assertThrows(IllegalArgumentException.class, () -> StringUtils.containsOnly("AB", null));
223         Assertions.assertThrows(IllegalArgumentException.class,
224                 () -> StringUtils.containsOnly("AB", ArrayUtils.EMPTY_CHAR_ARRAY));
225     }
226 
227 
228     /**
229      * @throws Exception if the test fails
230      */
231     @Test
232     public void toRootLowerCase() throws Exception {
233         assertEquals("abcd", StringUtils.toRootLowerCase("abcd"));
234         assertEquals("abcd", StringUtils.toRootLowerCase("ABcD"));
235 
236         assertEquals("", StringUtils.toRootLowerCase(""));
237         assertNull(StringUtils.toRootLowerCase(null));
238     }
239 
240     /**
241      * @throws Exception if the test fails
242      */
243     @Test
244     public void asColorHexadecimal() throws Exception {
245         assertNull(StringUtils.asColorHexadecimal(null));
246         assertNull(StringUtils.asColorHexadecimal(""));
247         assertNull(StringUtils.asColorHexadecimal("    "));
248 
249         assertNull(StringUtils.asColorHexadecimal("#a1"));
250         assertEquals(new Color(0, 17, 170), StringUtils.asColorHexadecimal("#0011aa"));
251         assertEquals(new Color(0, 17, 170), StringUtils.asColorHexadecimal("#01A"));
252     }
253 
254     /**
255      * @throws Exception if the test fails
256      */
257     @Test
258     public void findColorRGB() throws Exception {
259         assertNull(StringUtils.findColorRGB(null));
260         assertNull(StringUtils.findColorRGB(""));
261         assertNull(StringUtils.findColorRGB("    "));
262 
263         assertNull(StringUtils.findColorRGB("#a1"));
264         assertNull(StringUtils.findColorRGB("rgb(1,12,256)"));
265         assertNull(StringUtils.findColorRGB("rgb(1,256,7)"));
266         assertNull(StringUtils.findColorRGB("rgb(256,13,7)"));
267 
268         assertEquals(new Color(1, 12, 13), StringUtils.findColorRGB("rgb(1,12,13)"));
269         assertEquals(new Color(1, 12, 13), StringUtils.findColorRGB("rgb(  1, \t12, 13  )"));
270         assertEquals(new Color(1, 12, 13), StringUtils.findColorRGB("beforergb(1,12,13)after"));
271     }
272 
273     /**
274      * @throws Exception if the test fails
275      */
276     @Test
277     public void findColorRGBA() throws Exception {
278         assertNull(StringUtils.findColorRGBA(null));
279         assertNull(StringUtils.findColorRGBA(""));
280         assertNull(StringUtils.findColorRGBA("    "));
281 
282         assertNull(StringUtils.findColorRGBA("#a1"));
283         assertNull(StringUtils.findColorRGBA("rgba(1,12,256, .1)"));
284 
285         assertEquals(new Color(1, 12, 13), StringUtils.findColorRGBA("rgba(1,12,13, 1)"));
286         assertEquals(new Color(1, 12, 17, 25), StringUtils.findColorRGBA("rgba(1, 12, 17, 0.1)"));
287         assertEquals(new Color(1, 12, 17, 25), StringUtils.findColorRGBA("rgba(1, 12, 17, .1)"));
288         assertEquals(new Color(1, 12, 13, 127), StringUtils.findColorRGBA("beforergba(1,12,13,0.5)after"));
289     }
290 
291     /**
292      * @throws Exception if the test fails
293      */
294     @Test
295     public void findColorHSL() throws Exception {
296         assertNull(StringUtils.findColorHSL(null));
297         assertNull(StringUtils.findColorHSL(""));
298         assertNull(StringUtils.findColorHSL("    "));
299 
300         assertNull(StringUtils.findColorHSL("#a1"));
301         assertNull(StringUtils.findColorHSL("hsl(1,12,256, .1)"));
302 
303         assertEquals(new Color(255, 0, 0), StringUtils.findColorHSL("hsl(0, 100%, 50%)"));
304         assertEquals(new Color(255, 85, 0), StringUtils.findColorHSL("hsl(20, 100%, 50%)"));
305         assertEquals(new Color(204, 68, 0), StringUtils.findColorHSL("hsl(20, 100%, 40%)"));
306         assertEquals(new Color(51, 153, 153), StringUtils.findColorHSL("hsl( 180 ,50%, 40% )"));
307         assertEquals(new Color(51, 153, 153), StringUtils.findColorHSL("beforehsl(180,50%,40%)after"));
308 
309         assertEquals(new Color(37, 58, 59), StringUtils.findColorHSL("hsl(181 , 22%, 19% )"));
310         assertEquals(new Color(38, 60, 60), StringUtils.findColorHSL("hsl(180.75 , 22.3%, 19.3333% )"));
311     }
312 
313     /**
314      * @throws Exception if the test fails
315      */
316     @Test
317     public void formatColor() throws Exception {
318         assertEquals("rgb(1, 12, 13)", StringUtils.formatColor(new Color(1, 12, 13)));
319     }
320 
321     /**
322      * Test for method {@link StringUtils#sanitizeForAppendReplacement(String)}.
323      */
324     @Test
325     public void sanitizeForAppendReplacement() {
326         assertNull(StringUtils.sanitizeForAppendReplacement(null));
327         assertEquals("", StringUtils.sanitizeForAppendReplacement(""));
328         assertEquals("aBc", StringUtils.sanitizeForAppendReplacement("aBc"));
329 
330         assertEquals("\\$1", StringUtils.sanitizeForAppendReplacement("$1"));
331         assertEquals("\\$1\\$2 \\$3", StringUtils.sanitizeForAppendReplacement("$1$2 $3"));
332         assertEquals("\\\\1", StringUtils.sanitizeForAppendReplacement("\\1"));
333         assertEquals("\\\\1\\$2 \\\\3", StringUtils.sanitizeForAppendReplacement("\\1$2 \\3"));
334     }
335 
336     /**
337      * Test for method {@link StringUtils#sanitizeForFileName(String)}.
338      */
339     @Test
340     public void sanitizeForFileName() {
341         assertEquals("HtmlUnit", StringUtils.sanitizeForFileName("HtmlUnit"));
342         assertEquals("Html_Uni_", StringUtils.sanitizeForFileName("Html:Uni\t"));
343         assertEquals("Html_Unit", StringUtils.sanitizeForFileName("Html\\Unit"));
344     }
345 
346     /**
347      * @throws Exception if the test fails
348      */
349     @Test
350     public void stringToByteArray() throws Exception {
351         byte[] result = StringUtils.toByteArray(null, UTF_8);
352         assertEquals(0, result.length);
353 
354         result = StringUtils.toByteArray("", UTF_8);
355         assertEquals(0, result.length);
356 
357         result = StringUtils.toByteArray("htmlunit", UTF_8);
358         assertEquals(8, result.length);
359         assertEquals(104, result[0]);
360     }
361 
362     /**
363      * Test for method {@link StringUtils#replaceChars(String, String, String)}.
364      */
365     @Test
366     public void replaceChars() {
367         assertEquals(null, StringUtils.replaceChars(null, "", ""));
368         assertEquals("", StringUtils.replaceChars("", "", null));
369         assertEquals("abc", StringUtils.replaceChars("abc", null, null));
370         assertEquals("abc", StringUtils.replaceChars("abc", "", ""));
371         assertEquals("ac", StringUtils.replaceChars("abc", "b", null));
372         assertEquals("ac", StringUtils.replaceChars("abc", "b", ""));
373         assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yz"));
374         assertEquals("ayya", StringUtils.replaceChars("abcba", "bc", "y"));
375         assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yzx"));
376 
377         assertEquals("abc", StringUtils.replaceChars("abc", "d", "e"));
378 
379         assertEquals("ebc", StringUtils.replaceChars("abc", "a", "e"));
380         assertEquals("bc", StringUtils.replaceChars("abc", "a", ""));
381 
382         assertEquals("abe", StringUtils.replaceChars("abc", "c", "e"));
383         assertEquals("ab", StringUtils.replaceChars("abc", "c", null));
384     }
385 
386 
387     /**
388      * @throws Exception if the test fails
389      */
390     @Test
391     public void escapeXmlAttributeValue() throws Exception {
392         final StringBuilder sb = new StringBuilder();
393         for (int i = 0; i < 1024; i++) {
394             sb.append((char) i);
395         }
396 
397         final StringBuilder expected = new StringBuilder("\t\n\r");
398         for (int i = 32; i < 1024; i++) {
399             if (i == '&') {
400                 expected.append("&amp;");
401             }
402             else if (i == '<') {
403                 expected.append("&lt;");
404             }
405             else if (i == '"') {
406                 expected.append("&quot;");
407             }
408             else {
409                 expected.append((char) i);
410             }
411         }
412 
413         assertEquals(expected.toString(), StringUtils.escapeXmlAttributeValue(sb.toString()));
414     }
415 
416     /**
417      * @throws Exception if the test fails
418      */
419     @Test
420     public void escapeXmlAttributeValueBorderCases() throws Exception {
421         assertEquals(null, StringUtils.escapeXmlAttributeValue(null));
422         assertEquals("", StringUtils.escapeXmlAttributeValue(""));
423 
424         // [#x20-#xD7FF]
425         assertEquals("", StringUtils.escapeXmlAttributeValue("\u001f"));
426         assertEquals("\u0020", StringUtils.escapeXmlAttributeValue("\u0020"));
427         assertEquals("\u0021", StringUtils.escapeXmlAttributeValue("\u0021"));
428 
429         assertEquals("\uD7FE", StringUtils.escapeXmlAttributeValue("\uD7FE"));
430         assertEquals("\uD7FF", StringUtils.escapeXmlAttributeValue("\uD7FF"));
431         assertEquals("", StringUtils.escapeXmlAttributeValue("\uD800"));
432 
433         // [#xE000-#xFFFD]
434         assertEquals("", StringUtils.escapeXmlAttributeValue("\uDFFF"));
435         assertEquals("\uE000", StringUtils.escapeXmlAttributeValue("\uE000"));
436         assertEquals("\uE001", StringUtils.escapeXmlAttributeValue("\uE001"));
437 
438         assertEquals("\uFFFC", StringUtils.escapeXmlAttributeValue("\uFFFC"));
439         assertEquals("\uFFFD", StringUtils.escapeXmlAttributeValue("\uFFFD"));
440         assertEquals("", StringUtils.escapeXmlAttributeValue("\uFFFE"));
441 
442         // [#x10000-#x10FFFF]
443     }
444 
445     /**
446      * @throws Exception if the test fails
447      */
448     @Test
449     public void escapeXml() throws Exception {
450         final StringBuilder sb = new StringBuilder();
451         for (int i = 0; i < 1024; i++) {
452             sb.append((char) i);
453         }
454 
455         final StringBuilder expected = new StringBuilder("\t\n\r");
456         for (int i = 32; i < 1024; i++) {
457             if (i == '&') {
458                 expected.append("&amp;");
459             }
460             else if (i == '<') {
461                 expected.append("&lt;");
462             }
463             else if (i == '>') {
464                 expected.append("&gt;");
465             }
466             else if (i == '\'') {
467                 expected.append("&apos;");
468             }
469             else if (i == '"') {
470                 expected.append("&quot;");
471             }
472             else {
473                 expected.append((char) i);
474             }
475         }
476 
477         assertEquals(expected.toString(), StringUtils.escapeXml(sb.toString()));
478     }
479 
480     /**
481      * @throws Exception if the test fails
482      */
483     @Test
484     public void escapeXmlBorderCases() throws Exception {
485         assertEquals(null, StringUtils.escapeXml(null));
486         assertEquals("", StringUtils.escapeXml(""));
487 
488         // [#x20-#xD7FF]
489         assertEquals("", StringUtils.escapeXml("\u001f"));
490         assertEquals("\u0020", StringUtils.escapeXml("\u0020"));
491         assertEquals("\u0021", StringUtils.escapeXml("\u0021"));
492 
493         assertEquals("\uD7FE", StringUtils.escapeXml("\uD7FE"));
494         assertEquals("\uD7FF", StringUtils.escapeXml("\uD7FF"));
495         assertEquals("", StringUtils.escapeXml("\uD800"));
496 
497         // [#xE000-#xFFFD]
498         assertEquals("", StringUtils.escapeXml("\uDFFF"));
499         assertEquals("\uE000", StringUtils.escapeXml("\uE000"));
500         assertEquals("\uE001", StringUtils.escapeXml("\uE001"));
501 
502         assertEquals("\uFFFC", StringUtils.escapeXml("\uFFFC"));
503         assertEquals("\uFFFD", StringUtils.escapeXml("\uFFFD"));
504         assertEquals("", StringUtils.escapeXml("\uFFFE"));
505 
506         // [#x10000-#x10FFFF]
507     }
508 
509     /**
510      * @throws Exception if the test fails
511      */
512     @Test
513     public void substringBefore() throws Exception {
514         assertThrows(IllegalArgumentException.class, () -> StringUtils.substringBefore(null, null));
515         assertThrows(IllegalArgumentException.class, () -> StringUtils.substringBefore(null, ""));
516 
517         assertEquals("", StringUtils.substringBefore("", "a"));
518         assertEquals("xyz", StringUtils.substringBefore("xyz", "a"));
519         assertEquals("", StringUtils.substringBefore("a", "a"));
520         assertEquals("x", StringUtils.substringBefore("xaba", "a"));
521         assertEquals(" ", StringUtils.substringBefore(" a", "a"));
522     }
523 
524     /**
525      * @throws Exception if the test fails
526      */
527     @Test
528     public void toInt() throws Exception {
529         assertEquals(17, StringUtils.toInt(null, 17));
530         assertEquals(17, StringUtils.toInt("", 17));
531         assertEquals(17, StringUtils.toInt(" ", 17));
532         assertEquals(4, StringUtils.toInt("\t", 4));
533         assertEquals(4, StringUtils.toInt("two", 4));
534 
535         assertEquals(21, StringUtils.toInt("21", 4));
536         assertEquals(-21, StringUtils.toInt("-21", 4));
537         assertEquals(0, StringUtils.toInt(" 21 ", 0));
538         assertEquals(0, StringUtils.toInt(" -  21  \t", 0));
539     }
540 
541     /**
542      * @throws Exception if the test fails
543      */
544     @Test
545     public void toFloat() throws Exception {
546         assertEquals(17.2f, StringUtils.toFloat(null, 17.2f));
547         assertEquals(17.2f, StringUtils.toFloat("", 17.2f));
548         assertEquals(17.2f, StringUtils.toFloat(" ", 17.2f));
549         assertEquals(4f, StringUtils.toFloat("\t", 4f));
550         assertEquals(4f, StringUtils.toFloat("two", 4));
551 
552         assertEquals(21f, StringUtils.toFloat("21", 4.1f));
553         assertEquals(-21f, StringUtils.toFloat("-21", 4.1f));
554         assertEquals(21f, StringUtils.toFloat(" 21 ", 0));
555         assertEquals(0, StringUtils.toFloat(" -  21  \t", 0f));
556     }
557 
558     /**
559      * @throws Exception if the test fails
560      */
561     @Test
562     public void substringAfter() throws Exception {
563         assertNull(StringUtils.substringAfter(null, null));
564         assertNull(StringUtils.substringAfter(null, ""));
565         assertNull(StringUtils.substringAfter(null, "abc"));
566 
567         assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter("", null));
568         assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter("", ""));
569         assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter("", "abc"));
570 
571         assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter(StringUtils.EMPTY_STRING, null));
572         assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter(StringUtils.EMPTY_STRING, ""));
573         assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter(StringUtils.EMPTY_STRING, "abc"));
574 
575         assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter("abc", null));
576         assertEquals("abc", StringUtils.substringAfter("abc", ""));
577 
578         assertEquals("bc", StringUtils.substringAfter("abc", "a"));
579         assertEquals("cba", StringUtils.substringAfter("abcba", "b"));
580         assertEquals("", StringUtils.substringAfter("abc", "c"));
581         assertEquals("", StringUtils.substringAfter("abc", "d"));
582     }
583 
584     /**
585      * @throws Exception if the test fails
586      */
587     @Test
588     public void trimRight() throws Exception {
589         assertNull(StringUtils.trimRight(null));
590         assertEquals(StringUtils.EMPTY_STRING, StringUtils.trimRight(""));
591         assertEquals("", StringUtils.trimRight(StringUtils.EMPTY_STRING));
592 
593         assertEquals("abc", StringUtils.trimRight("abc"));
594         assertEquals("  abc", StringUtils.trimRight("  abc"));
595         assertEquals("abc", StringUtils.trimRight("abc  "));
596         assertEquals(" a b c", StringUtils.trimRight(" a b c "));
597     }
598 }