1 package org.apache.maven.shared.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 import org.junit.Assert;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.TemporaryFolder;
31
32 import static org.hamcrest.CoreMatchers.is;
33 import static org.hamcrest.CoreMatchers.nullValue;
34
35
36
37
38
39
40
41 public class StringUtilsTest
42 extends Assert
43 {
44
45 @Rule
46 public TemporaryFolder tempFolder = new TemporaryFolder();
47
48
49 @Test( expected = NullPointerException.class )
50 public void testAbbreviate_NPE()
51 {
52 assertThat( StringUtils.abbreviate( null, 10 )
53 , nullValue() );
54 }
55
56 @Test( expected = IllegalArgumentException.class )
57 public void testAbbreviate_MinLength()
58 {
59 assertThat( StringUtils.abbreviate( "This is a longtext", 3 )
60 , is( "T" ) );
61 }
62
63 @Test
64 public void testAbbreviate()
65 {
66 assertThat( StringUtils.abbreviate( "This is a longtext", 10 )
67 , is( "This is..." ) );
68
69 assertThat( StringUtils.abbreviate( "This is a longtext", 50 )
70 , is( "This is a longtext" ) );
71 }
72
73 @Test( expected = NullPointerException.class )
74 public void testAbbreviate_Offset_NPE()
75 {
76 assertThat( StringUtils.abbreviate( null, 10, 20 )
77 , nullValue() );
78 }
79
80 @Test( expected = IllegalArgumentException.class )
81 public void testAbbreviate_Offset_MinLength()
82 {
83 assertThat( StringUtils.abbreviate( "This is a longtext", 10, 3 )
84 , is( "T" ) );
85 }
86
87 @Test
88 public void testAbbreviate_Offset()
89 {
90 assertThat( StringUtils.abbreviate( "This is a longtext", 5, 10 )
91 , is( "...is a..." ) );
92
93 assertThat( StringUtils.abbreviate( "This is a longtext", 10, 20 )
94 , is( "This is a longtext" ) );
95
96 assertThat( StringUtils.abbreviate( "This is a longtext", 50, 20 )
97 , is( "This is a longtext" ) );
98 }
99
100 @Test( expected = NullPointerException.class )
101 public void testAddAndDeHump_NPE()
102 {
103 StringUtils.addAndDeHump( null );
104 }
105
106 @Test
107 public void testAddAndDeHump()
108 {
109 assertThat( StringUtils.addAndDeHump( "lalala" )
110 , is( "lalala" ) );
111
112 assertThat( StringUtils.addAndDeHump( "LaLaLa" )
113 , is( "la-la-la" ) );
114
115 assertThat( StringUtils.addAndDeHump( "ALLUPPER" )
116 , is( "a-l-l-u-p-p-e-r" ) );
117
118 }
119
120 @Test
121 public void testCapitalise()
122 {
123 assertThat( StringUtils.capitalise( null )
124 , nullValue() );
125
126 assertThat( StringUtils.capitalise( "startBig" )
127 , is( "StartBig" ) );
128 }
129
130 @Test
131 public void testCapitaliseAllWords()
132 {
133 assertThat( StringUtils.capitaliseAllWords( null )
134 , nullValue() );
135
136 assertThat( StringUtils.capitaliseAllWords( "start all big" )
137 , is( "Start All Big" ) );
138 }
139
140 @Test( expected = NullPointerException.class )
141 public void testCapitalizeFirstLetter_NPE()
142 {
143 assertThat( StringUtils.capitalizeFirstLetter( null )
144 , nullValue() );
145 }
146
147 @Test
148 public void testCapitalizeFirstLetter()
149 {
150 assertThat( StringUtils.capitalizeFirstLetter( "Dings" )
151 , is( "Dings" ) );
152
153 assertThat( StringUtils.capitalizeFirstLetter( " dings" )
154 , is( " dings" ) );
155
156 assertThat( StringUtils.capitalizeFirstLetter( "start all big" )
157 , is( "Start all big" ) );
158 }
159
160 @Test( expected = NullPointerException.class )
161 public void testCenter_NPE()
162 {
163 StringUtils.center( null, 20 );
164 }
165
166 @Test
167 public void testCenter()
168 {
169 assertThat( StringUtils.center( "centerMe", 20 )
170 , is( " centerMe " ) );
171
172 assertThat( StringUtils.center( "centerMe", 4 )
173 , is( "centerMe" ) );
174
175 assertThat( StringUtils.center( " centerMe", 20 )
176 , is( " centerMe " ) );
177 }
178
179 @Test( expected = NullPointerException.class )
180 public void testCenter_Delim_NPE()
181 {
182 StringUtils.center( null, 20, "*" );
183 }
184
185 @Test
186 public void testCenter_Delim()
187 {
188 assertThat( StringUtils.center( "centerMe", 20, "*" )
189 , is( "******centerMe******" ) );
190
191 assertThat( StringUtils.center( "centerMe", 4, "*" )
192 , is( "centerMe" ) );
193
194 assertThat( StringUtils.center( " centerMe", 20, "*" )
195 , is( "** centerMe**" ) );
196 }
197
198 @Test( expected = NullPointerException.class )
199 public void testChomp_NPE()
200 {
201 StringUtils.chomp( null );
202 }
203
204 @Test
205 public void testChomp()
206 {
207 assertThat( StringUtils.chomp( "dings" )
208 , is( "dings" ) );
209
210 assertThat( StringUtils.chomp( "dings\n" )
211 , is( "dings" ) );
212
213 assertThat( StringUtils.chomp( "dings\nbums" )
214 , is( "dings" ) );
215
216 assertThat( StringUtils.chomp( "dings\nbums\ndongs" )
217 , is( "dings\nbums" ) );
218 }
219
220 @Test( expected = NullPointerException.class )
221 public void testChomp_Delim_NPE()
222 {
223 StringUtils.chomp( null, "+" );
224 }
225
226 @Test
227 public void testChomp_Delim()
228 {
229 assertThat( StringUtils.chomp( "dings", "+" )
230 , is( "dings" ) );
231
232 assertThat( StringUtils.chomp( "dings+", "+" )
233 , is( "dings" ) );
234
235 assertThat( StringUtils.chomp( "dings+bums", "+" )
236 , is( "dings" ) );
237
238 assertThat( StringUtils.chomp( "dings+bums+dongs", "+" )
239 , is( "dings+bums" ) );
240 }
241
242
243 @Test( expected = NullPointerException.class )
244 public void testChompLast_NPE()
245 {
246 StringUtils.chompLast( null );
247 }
248
249 @Test
250 public void testChompLast()
251 {
252 assertThat( StringUtils.chompLast( "dings" )
253 , is( "dings" ) );
254
255 assertThat( StringUtils.chompLast( "\n" )
256 , is( "" ) );
257
258 assertThat( StringUtils.chompLast( "dings\n" )
259 , is( "dings" ) );
260
261 assertThat( StringUtils.chompLast( "dings\nbums" )
262 , is( "dings\nbums" ) );
263
264 assertThat( StringUtils.chompLast( "dings\nbums\ndongs\n" )
265 , is( "dings\nbums\ndongs" ) );
266 }
267
268 @Test( expected = NullPointerException.class )
269 public void testChompLast_Delim_NPE()
270 {
271 StringUtils.chompLast( null, "+" );
272 }
273
274 @Test
275 public void testChompLast_Delim()
276 {
277 assertThat( StringUtils.chompLast( "dings", "+" )
278 , is( "dings" ) );
279
280 assertThat( StringUtils.chompLast( "+", "+" )
281 , is( "" ) );
282
283 assertThat( StringUtils.chompLast( "dings+", "+" )
284 , is( "dings" ) );
285
286 assertThat( StringUtils.chompLast( "dings+bums", "+" )
287 , is( "dings+bums" ) );
288
289 assertThat( StringUtils.chompLast( "dings+bums+dongs+", "+" )
290 , is( "dings+bums+dongs" ) );
291 }
292
293 @Test( expected = NullPointerException.class )
294 public void testChop_NPE()
295 {
296 StringUtils.chop( null );
297 }
298
299 @Test
300 public void testChop()
301 {
302 assertThat( StringUtils.chop( "dings" )
303 , is( "ding" ) );
304
305 assertThat( StringUtils.chop( "x" )
306 , is( "" ) );
307
308 assertThat( StringUtils.chop( "dings\n" )
309 , is( "dings" ) );
310
311 assertThat( StringUtils.chop( "dings\r\n" )
312 , is( "dings" ) );
313
314 assertThat( StringUtils.chop( "dings\n\r" )
315 , is( "dings\n" ) );
316 }
317
318 @Test( expected = NullPointerException.class )
319 public void testChopNewline_NPE()
320 {
321 StringUtils.chopNewline( null );
322 }
323
324 @Test
325 public void testChopNewline()
326 {
327 assertThat( StringUtils.chopNewline( "dings" )
328 , is( "dings" ) );
329
330 assertThat( StringUtils.chopNewline( "x" )
331 , is( "x" ) );
332
333
334 assertThat( StringUtils.chopNewline( "dings\n" )
335 , is( "dings" ) );
336
337 assertThat( StringUtils.chopNewline( "dings\r\n" )
338 , is( "dings" ) );
339
340 assertThat( StringUtils.chopNewline( "dings\n\r" )
341 , is( "dings\n\r" ) );
342 }
343
344
345 @Test
346 public void testClean()
347 {
348 assertThat( StringUtils.clean( null )
349 , is( "" ) );
350
351 assertThat( StringUtils.clean( " " )
352 , is( "" ) );
353
354 assertThat( StringUtils.clean( " c " )
355 , is( "c" ) );
356
357 assertThat( StringUtils.clean( " dings \n " )
358 , is( "dings" ) );
359 }
360
361
362 @Test( expected = NullPointerException.class )
363 public void testConcatenate_NPE()
364 {
365 StringUtils.concatenate( null );
366 }
367
368 @Test
369 public void testConcatenate()
370 {
371 assertThat( StringUtils.concatenate( new String[0] )
372 , is( "" ) );
373
374 assertThat( StringUtils.concatenate( new String[]{ "x" } )
375 , is( "x" ) );
376
377 assertThat( StringUtils.concatenate( new String[]{ "x", "y", "z" } )
378 , is( "xyz" ) );
379 }
380
381 @Test
382 public void testContains_String()
383 {
384 assertThat( StringUtils.contains( null, null )
385 , is( false ) );
386
387 assertThat( StringUtils.contains( null, "string" )
388 , is( false ) );
389
390 assertThat( StringUtils.contains( "string", null )
391 , is( false ) );
392
393 assertThat( StringUtils.contains( "string", "" )
394 , is( true ) );
395
396 assertThat( StringUtils.contains( "string", "in" )
397 , is( true ) );
398
399 assertThat( StringUtils.contains( "string", "IN" )
400 , is( false ) );
401 }
402
403 @Test
404 public void testContains_Char()
405 {
406 assertThat( StringUtils.contains( null, 'c' )
407 , is( false ) );
408
409 assertThat( StringUtils.contains( "string", "c" )
410 , is( false ) );
411
412 assertThat( StringUtils.contains( "string", "" )
413 , is( true ) );
414
415 assertThat( StringUtils.contains( "string", "r" )
416 , is( true ) );
417
418 assertThat( StringUtils.contains( "string", "R" )
419 , is( false ) );
420
421 }
422
423 @Test( expected = NullPointerException.class )
424 public void testCountMatches_NPE()
425 {
426 StringUtils.countMatches( null, null );
427 }
428
429 @Test( expected = NullPointerException.class )
430 public void testCountMatches_NPE2()
431 {
432 StringUtils.countMatches( "this is it", null );
433 }
434
435 @Test
436 public void testCountMatches()
437 {
438 assertThat( StringUtils.countMatches( null, "is" )
439 , is( 0 ) );
440
441 assertThat( StringUtils.countMatches( "this is it", "is" )
442 , is( 2 ) );
443
444 assertThat( StringUtils.countMatches( "this is it", "notincluded" )
445 , is( 0 ) );
446 }
447
448 @Test
449 public void testDefaultString()
450 {
451 assertThat( StringUtils.defaultString( null )
452 , is( "" ) );
453
454 assertThat( StringUtils.defaultString( "dings" )
455 , is( "dings" ) );
456 }
457
458 @Test
459 public void testDefaultString_defaultValue()
460 {
461 assertThat( StringUtils.defaultString( null, "defaultValue" )
462 , is( "defaultValue" ) );
463
464 assertThat( StringUtils.defaultString( "dings", "defaultValue" )
465 , is( "dings" ) );
466 }
467
468 @Test( expected = NullPointerException.class )
469 public void testDeleteWhitespace_NPE()
470 {
471 StringUtils.deleteWhitespace( null );
472 }
473
474 @Test
475 public void testDeleteWhitespace()
476 {
477 assertThat( StringUtils.deleteWhitespace( " \t \n" )
478 , is( "" ) );
479
480 assertThat( StringUtils.deleteWhitespace( " \t \b \n" )
481 , is( "\b" ) );
482
483 assertThat( StringUtils.deleteWhitespace( "dings" )
484 , is( "dings" ) );
485
486 assertThat( StringUtils.deleteWhitespace( "\n dings \t " )
487 , is( "dings" ) );
488 }
489
490 @Test( expected = NullPointerException.class )
491 public void testDifference_NPE()
492 {
493 StringUtils.difference( null, null );
494 }
495
496 @Test( expected = NullPointerException.class )
497 public void testDifference_NPE2()
498 {
499 StringUtils.difference( null, "another" );
500 }
501
502 @Test( expected = NullPointerException.class )
503 public void testDifference_NPE3()
504 {
505 StringUtils.difference( "this", null );
506 }
507
508 @Test
509 public void testDifference()
510 {
511 assertThat( StringUtils.difference( "this", "another" )
512 , is( "another" ) );
513
514 assertThat( StringUtils.difference( "I am human", "I am a robot" )
515 , is( "a robot" ) );
516
517 assertThat( StringUtils.difference( "I am human", "I AM a robot" )
518 , is( "AM a robot" ) );
519 }
520
521 @Test( expected = NullPointerException.class )
522 public void testDifferenceAt_NPE()
523 {
524 StringUtils.differenceAt( null, null );
525 }
526
527 @Test( expected = NullPointerException.class )
528 public void testDifferenceAt_NPE2()
529 {
530 StringUtils.differenceAt( "test", null );
531 }
532
533 @Test( expected = NullPointerException.class )
534 public void testDifferenceAt_NPE3()
535 {
536 StringUtils.differenceAt( null, "test" );
537 }
538
539 @Test
540 public void testDifferenceAt()
541 {
542 assertThat( StringUtils.differenceAt( "this", "another" )
543 , is( 0 ) );
544
545 assertThat( StringUtils.differenceAt( "I am human", "I am a robot" )
546 , is( 5 ) );
547
548 assertThat( StringUtils.differenceAt( "I am human", "I AM a robot" )
549 , is( 2 ) );
550 }
551
552 @Test
553 public void testEndsWithIgnoreCase()
554 {
555 assertThat( StringUtils.endsWithIgnoreCase( null, null )
556 , is( false ) );
557
558 assertThat( StringUtils.endsWithIgnoreCase( null, "string" )
559 , is( false ) );
560
561 assertThat( StringUtils.endsWithIgnoreCase( "string", null )
562 , is( false ) );
563
564 assertThat( StringUtils.endsWithIgnoreCase( "string", "ing" )
565 , is( true ) );
566
567 assertThat( StringUtils.endsWithIgnoreCase( "string", "a string" )
568 , is( false ) );
569
570 assertThat( StringUtils.endsWithIgnoreCase( "string", "str" )
571 , is( false ) );
572 }
573
574 @Test
575 public void testEquals()
576 {
577 assertThat( StringUtils.equals( null, null )
578 , is( true ) );
579
580 assertThat( StringUtils.equals( "x", null )
581 , is( false ) );
582
583 assertThat( StringUtils.equals( null, "x" )
584 , is( false ) );
585
586 assertThat( StringUtils.equals( "X", "x" )
587 , is( false ) );
588
589 assertThat( StringUtils.equals( "dings", "dings" )
590 , is( true ) );
591 }
592
593 @Test
594 public void testEqualsIgnoreCase()
595 {
596 assertThat( StringUtils.equalsIgnoreCase( null, null )
597 , is( true ) );
598
599 assertThat( StringUtils.equalsIgnoreCase( "x", null )
600 , is( false ) );
601
602 assertThat( StringUtils.equalsIgnoreCase( null, "x" )
603 , is( false ) );
604
605 assertThat( StringUtils.equalsIgnoreCase( "X", "x" )
606 , is( true ) );
607
608 assertThat( StringUtils.equalsIgnoreCase( "dings", "dings" )
609 , is( true ) );
610
611 assertThat( StringUtils.equalsIgnoreCase( "dings", "diNGs" )
612 , is( true ) );
613 }
614
615 @Test( expected = NullPointerException.class )
616 public void testEscape_NPE()
617 {
618 StringUtils.escape( null );
619 }
620
621 @Test
622 public void testEscape()
623 {
624 assertThat( StringUtils.escape( "dings" )
625 , is( "dings" ) );
626
627 assertThat( StringUtils.escape( "dings\tbums" )
628 , is( "dings\\tbums" ) );
629
630 assertThat( StringUtils.escape( "dings\nbums" )
631 , is( "dings\\nbums" ) );
632 }
633
634
635 @Test
636 public void testEscape2()
637 {
638 assertThat( StringUtils.escape( null, null, '#' )
639 , nullValue() );
640
641 assertThat( StringUtils.escape( "dings", new char[]{ '\t', '\b' }, '+' )
642 , is( "dings" ) );
643
644 assertThat( StringUtils.escape( "dings\tbums", new char[]{ '\t', '\b' }, '+' )
645 , is( "dings+\tbums" ) );
646
647 assertThat( StringUtils.escape( "dings\nbums", new char[]{ '\t', '\b' }, '+' )
648 , is( "dings\nbums" ) );
649 assertThat( StringUtils.escape( "dings\bbums", new char[]{ '\t', '\b' }, '+' )
650 , is( "dings+\bbums" ) );
651 }
652
653 @Test( expected = NullPointerException.class )
654 public void testGetChomp_NPE1()
655 {
656 StringUtils.getChomp( null, null );
657 }
658
659 @Test( expected = NullPointerException.class )
660 public void testGetChomp_NPE2()
661 {
662 StringUtils.getChomp( "dings", null );
663 }
664
665 @Test( expected = NullPointerException.class )
666 public void testGetChomp_NPE3()
667 {
668 StringUtils.getChomp( null, "dings" );
669 }
670
671 @Test
672 public void testGetChomp()
673 {
674 assertThat( StringUtils.getChomp( "dings-bums", "-" )
675 , is( "-bums" ) );
676
677 assertThat( StringUtils.getChomp( "dings-", "-" )
678 , is( "-" ) );
679
680 assertThat( StringUtils.getChomp( "dingsbums", "-" )
681 , is( "" ) );
682 }
683
684 @Test( expected = NullPointerException.class )
685 public void testGetNestedString_NPE()
686 {
687 assertThat( StringUtils.getNestedString( " +dings+ ", null )
688 , nullValue() );
689 }
690
691 @Test
692 public void testGetNestedString()
693 {
694 assertThat( StringUtils.getNestedString( null, null )
695 , nullValue() );
696
697 assertThat( StringUtils.getNestedString( " +dings+ ", "+" )
698 , is( "dings" ) );
699
700 assertThat( StringUtils.getNestedString( " +dings+ ", "not" )
701 , nullValue() );
702 }
703
704 @Test( expected = NullPointerException.class )
705 public void testGetNestedString2_NPE1()
706 {
707 assertThat( StringUtils.getNestedString( " +dings+ ", null, null )
708 , nullValue() );
709 }
710
711 @Test( expected = NullPointerException.class )
712 public void testGetNestedString2_NPE2()
713 {
714 assertThat( StringUtils.getNestedString( " +dings+ ", null, "neither" )
715 , nullValue() );
716 }
717
718 @Test
719 public void testGetNestedString2()
720 {
721 assertThat( StringUtils.getNestedString( null, null, null )
722 , nullValue() );
723
724 assertThat( StringUtils.getNestedString( " +dings+ ", "not", null )
725 , nullValue() );
726
727 assertThat( StringUtils.getNestedString( " +dings- ", "+", "-" )
728 , is( "dings" ) );
729
730 assertThat( StringUtils.getNestedString( " +dings+ ", "not", "neither" )
731 , nullValue() );
732 }
733
734 @Test( expected = NullPointerException.class )
735 public void testGetPrechomp_NPE1()
736 {
737 StringUtils.getPrechomp( null, null );
738 }
739
740 @Test( expected = NullPointerException.class )
741 public void testGetPrechomp_NPE2()
742 {
743 StringUtils.getPrechomp( null, "bums" );
744 }
745
746 @Test
747 public void testGetPrechomp()
748 {
749 assertThat( StringUtils.getPrechomp( "dings bums dongs", "bums" )
750 , is( "dings bums" ) );
751
752 assertThat( StringUtils.getPrechomp( "dings bums dongs", "non" )
753 , is( "" ) );
754 }
755
756 @Test
757 public void testIndexOfAny()
758 {
759 assertThat( StringUtils.indexOfAny( null, null )
760 , is( -1 ) );
761
762 assertThat( StringUtils.indexOfAny( "dings", null )
763 , is( -1 ) );
764
765 assertThat( StringUtils.indexOfAny( null, new String[]{} )
766 , is( -1 ) );
767
768 assertThat( StringUtils.indexOfAny( "dings bums dongs", new String[]{ "knuff", "bums" } )
769 , is( 6 ) );
770 }
771
772 @Test( expected = NullPointerException.class )
773 public void testInterpolate_NPE()
774 {
775 StringUtils.interpolate( null, null );
776 }
777
778 @Test( expected = NullPointerException.class )
779 public void testInterpolate_NPE2()
780 {
781 StringUtils.interpolate( "This ${text} will get replaced", null );
782 }
783
784 @Test
785 public void testInterpolate()
786 {
787 Map<String, String> variables = new HashMap<String, String>();
788 assertThat( StringUtils.interpolate( "This ${text} will get replaced", variables )
789 , is( "This ${text} will get replaced" ) );
790
791 variables.put( "text", "with a special content" );
792
793 assertThat( StringUtils.interpolate( "This ${text} will get replaced", variables )
794 , is( "This with a special content will get replaced" ) );
795 }
796
797 @Test
798 public void testIsAlpha()
799 {
800 assertThat( StringUtils.isAlpha( null )
801 , is( false ) );
802
803 assertThat( StringUtils.isAlpha( "2" )
804 , is( false ) );
805
806 assertThat( StringUtils.isAlpha( "asvsdfSDF" )
807 , is( true ) );
808
809 assertThat( StringUtils.isAlpha( "asvsdfSDF \t " )
810 , is( false ) );
811
812 assertThat( StringUtils.isAlpha( "435afsafd3!" )
813 , is( false ) );
814 }
815
816 @Test
817 public void testIsAlphaSpace()
818 {
819 assertThat( StringUtils.isAlphaSpace( null )
820 , is( false ) );
821
822 assertThat( StringUtils.isAlphaSpace( "2" )
823 , is( false ) );
824
825 assertThat( StringUtils.isAlphaSpace( "asvsdfSDF" )
826 , is( true ) );
827
828 assertThat( StringUtils.isAlphaSpace( "asvsdfSDF " )
829 , is( true ) );
830
831 assertThat( StringUtils.isAlphaSpace( "asvsdfSDF \t " )
832 , is( false ) );
833
834 assertThat( StringUtils.isAlphaSpace( "435afsafd3!" )
835 , is( false ) );
836 }
837
838 @Test
839 public void testIsAlphanumeric()
840 {
841 assertThat( StringUtils.isAlphanumeric( null )
842 , is( false ) );
843
844 assertThat( StringUtils.isAlphanumeric( "2" )
845 , is( true ) );
846
847 assertThat( StringUtils.isAlphanumeric( "asvsdfSDF" )
848 , is( true ) );
849
850 assertThat( StringUtils.isAlphanumeric( "asvsdfSDF " )
851 , is( false ) );
852
853 assertThat( StringUtils.isAlphanumeric( "asvsdfSDF \t " )
854 , is( false ) );
855
856 assertThat( StringUtils.isAlphanumeric( "435afsafd3!" )
857 , is( false ) );
858
859 assertThat( StringUtils.isAlphanumeric( "435afsafd3" )
860 , is( true ) );
861
862 assertThat( StringUtils.isAlphanumeric( "435 " )
863 , is( false ) );
864
865 assertThat( StringUtils.isAlphanumeric( "435" )
866 , is( true ) );
867 }
868
869 @Test
870 public void testIsAlphanumericSpace()
871 {
872 assertThat( StringUtils.isAlphanumericSpace( null )
873 , is( false ) );
874
875 assertThat( StringUtils.isAlphanumericSpace( "2" )
876 , is( true ) );
877
878 assertThat( StringUtils.isAlphanumericSpace( "asvsdfSDF" )
879 , is( true ) );
880
881 assertThat( StringUtils.isAlphanumericSpace( "asvsdfSDF " )
882 , is( true ) );
883
884 assertThat( StringUtils.isAlphanumericSpace( "asvsdfSDF \t " )
885 , is( false ) );
886
887 assertThat( StringUtils.isAlphanumericSpace( "435afsafd3!" )
888 , is( false ) );
889
890 assertThat( StringUtils.isAlphanumericSpace( "435afsafd3" )
891 , is( true ) );
892
893 assertThat( StringUtils.isAlphanumericSpace( "435 " )
894 , is( true ) );
895
896 assertThat( StringUtils.isAlphanumericSpace( "435" )
897 , is( true ) );
898 }
899
900 @Test
901 public void testIsBlank()
902 {
903 assertThat( StringUtils.isBlank( null )
904 , is( true ) );
905
906 assertThat( StringUtils.isBlank( "xx" )
907 , is( false ) );
908
909 assertThat( StringUtils.isBlank( "xx " )
910 , is( false ) );
911
912 assertThat( StringUtils.isBlank( " " )
913 , is( true ) );
914
915 assertThat( StringUtils.isBlank( " \t " )
916 , is( true ) );
917
918 assertThat( StringUtils.isBlank( " \n " )
919 , is( true ) );
920 }
921
922
923 @Test
924 public void testEmpty()
925 {
926 assertThat( StringUtils.isEmpty( null )
927 , is( true ) );
928
929 assertThat( StringUtils.isEmpty( "xx" )
930 , is( false ) );
931
932 assertThat( StringUtils.isEmpty( "xx " )
933 , is( false ) );
934
935 assertThat( StringUtils.isEmpty( " " )
936 , is( true ) );
937
938 assertThat( StringUtils.isEmpty( " \t " )
939 , is( true ) );
940
941 assertThat( StringUtils.isEmpty( " \n " )
942 , is( true ) );
943 }
944
945 @Test
946 public void testNotBlank()
947 {
948 assertThat( StringUtils.isNotBlank( null )
949 , is( false ) );
950
951 assertThat( StringUtils.isNotBlank( "xx" )
952 , is( true ) );
953
954 assertThat( StringUtils.isNotBlank( "xx " )
955 , is( true ) );
956
957 assertThat( StringUtils.isNotBlank( " " )
958 , is( false ) );
959
960 assertThat( StringUtils.isNotBlank( " \t " )
961 , is( false ) );
962
963 assertThat( StringUtils.isNotBlank( " \n " )
964 , is( false ) );
965 }
966
967 @Test
968 public void testNotEmpty()
969 {
970 assertThat( StringUtils.isNotEmpty( null )
971 , is( false ) );
972
973 assertThat( StringUtils.isNotEmpty( "xx" )
974 , is( true ) );
975
976 assertThat( StringUtils.isNotEmpty( "xx " )
977 , is( true ) );
978
979 assertThat( StringUtils.isNotEmpty( " " )
980 , is( true ) );
981
982 assertThat( StringUtils.isNotEmpty( "" )
983 , is( false ) );
984
985 assertThat( StringUtils.isNotEmpty( " \t " )
986 , is( true ) );
987
988 assertThat( StringUtils.isNotEmpty( " \n " )
989 , is( true ) );
990 }
991
992 @Test
993 public void testIsNumeric()
994 {
995 assertThat( StringUtils.isNumeric( null )
996 , is( false ) );
997
998 assertThat( StringUtils.isNumeric( "2" )
999 , is( true ) );
1000
1001 assertThat( StringUtils.isNumeric( "asvsdfSDF" )
1002 , is( false ) );
1003
1004 assertThat( StringUtils.isNumeric( "asvsdfSDF " )
1005 , is( false ) );
1006
1007 assertThat( StringUtils.isNumeric( "asvsdfSDF \t " )
1008 , is( false ) );
1009
1010 assertThat( StringUtils.isNumeric( "435afsafd3!" )
1011 , is( false ) );
1012
1013 assertThat( StringUtils.isNumeric( "435afsafd3" )
1014 , is( false ) );
1015
1016 assertThat( StringUtils.isNumeric( "435 " )
1017 , is( false ) );
1018
1019 assertThat( StringUtils.isNumeric( "435" )
1020 , is( true ) );
1021 }
1022
1023 @Test
1024 public void testIsWhitespace()
1025 {
1026 assertThat( StringUtils.isWhitespace( null )
1027 , is( false ) );
1028
1029 assertThat( StringUtils.isWhitespace( "xx" )
1030 , is( false ) );
1031
1032 assertThat( StringUtils.isWhitespace( "xx " )
1033 , is( false ) );
1034
1035 assertThat( StringUtils.isWhitespace( " " )
1036 , is( true ) );
1037
1038 assertThat( StringUtils.isWhitespace( "" )
1039 , is( true ) );
1040
1041 assertThat( StringUtils.isWhitespace( " \t " )
1042 , is( true ) );
1043
1044 assertThat( StringUtils.isWhitespace( " \n " )
1045 , is( true ) );
1046 }
1047
1048 @Test( expected = NullPointerException.class )
1049 public void testJoin_Array_NPE()
1050 {
1051 StringUtils.join( ( Object[] ) null, null );
1052 }
1053
1054 @Test
1055 public void testJoin_Array()
1056 {
1057 assertThat( StringUtils.join( new Object[0], null )
1058 , is( "" ) );
1059
1060 assertThat( StringUtils.join( new Object[]{ "a", "b", "c" }, null )
1061 , is( "abc" ) );
1062
1063 assertThat( StringUtils.join( new Object[]{ "a", "b", "c" }, "__" )
1064 , is( "a__b__c" ) );
1065 }
1066
1067 @Test( expected = NullPointerException.class )
1068 public void testJoin_Iterator_NPE()
1069 {
1070 StringUtils.join( (Iterator<?>) null, null );
1071 }
1072
1073 @Test
1074 public void testJoin_Iterator()
1075 {
1076 ArrayList<String> list = new ArrayList<String>();
1077
1078 assertThat( StringUtils.join( list.iterator(), null )
1079 , is( "" ) );
1080
1081 list.add( "a" );
1082 list.add( "b" );
1083 list.add( "c" );
1084
1085 assertThat( StringUtils.join( list.iterator(), null )
1086 , is( "abc" ) );
1087
1088 assertThat( StringUtils.join( list.iterator(), "__" )
1089 , is( "a__b__c" ) );
1090 }
1091
1092 @Test
1093 public void testLastIndexOfAny()
1094 {
1095 assertThat( StringUtils.lastIndexOfAny( null, null )
1096 , is( -1 ) );
1097
1098 assertThat( StringUtils.lastIndexOfAny( "dings", null )
1099 , is( -1 ) );
1100
1101 assertThat( StringUtils.lastIndexOfAny( "dings bums boms", new String[] { "ms", " b" } )
1102 , is( 13 ) );
1103
1104 assertThat( StringUtils.lastIndexOfAny( "dings bums boms", new String[] { "nix", "da" } )
1105 , is( -1 ) );
1106 }
1107
1108 @Test( expected = IllegalArgumentException.class )
1109 public void testLeft_IAE()
1110 {
1111 StringUtils.left( null, -1 );
1112 }
1113
1114 @Test
1115 public void testLeft()
1116 {
1117 assertThat( StringUtils.left( null, 4 )
1118 , nullValue() );
1119
1120 assertThat( StringUtils.left( "dingsbums", 4 )
1121 , is( "ding" ) );
1122
1123 assertThat( StringUtils.left( "dingsbums", 40 )
1124 , is( "dingsbums" ) );
1125
1126 assertThat( StringUtils.left( "dingsbums", 0 )
1127 , is( "" ) );
1128 }
1129
1130 @Test( expected = NullPointerException.class )
1131 public void testLeftPad1_NPE()
1132 {
1133 StringUtils.leftPad( null, 0 );
1134 }
1135
1136 @Test
1137 public void testLeftPad1()
1138 {
1139 assertThat( StringUtils.leftPad( "dings", 0 )
1140 , is( "dings") );
1141
1142 assertThat( StringUtils.leftPad( "dings", 2 )
1143 , is( "dings") );
1144
1145 assertThat( StringUtils.leftPad( "dings", 10 )
1146 , is( " dings") );
1147 }
1148
1149 @Test( expected = NullPointerException.class )
1150 public void testLeftPad2_NPE1()
1151 {
1152 StringUtils.leftPad( null, 0, null );
1153 }
1154
1155 @Test( expected = NullPointerException.class )
1156 public void testLeftPad2_NPE2()
1157 {
1158 StringUtils.leftPad( "dings", 0, null );
1159 }
1160
1161 @Test( expected = NullPointerException.class )
1162 public void testLeftPad2_NPE3()
1163 {
1164 StringUtils.leftPad( null, 0, "*" );
1165 }
1166
1167 @Test
1168 public void testLeftPad2()
1169 {
1170 assertThat( StringUtils.leftPad( "dings", 0, "*" )
1171 , is( "dings") );
1172
1173 assertThat( StringUtils.leftPad( "dings", 2, "*" )
1174 , is( "dings") );
1175
1176 assertThat( StringUtils.leftPad( "dings", 10, "*" )
1177 , is( "*****dings") );
1178 }
1179
1180 @Test
1181 public void testLowerCase()
1182 {
1183 assertThat( StringUtils.lowerCase( null )
1184 , nullValue() );
1185
1186 assertThat( StringUtils.lowerCase( "dinGSbuMS" )
1187 , is( "dingsbums" ) );
1188
1189 assertThat( StringUtils.lowerCase( "" )
1190 , is( "" ) );
1191
1192 }
1193
1194 @Test( expected = NullPointerException.class )
1195 public void testLowerCaseFirstLetter_NPE()
1196 {
1197 StringUtils.lowercaseFirstLetter( null );
1198 }
1199
1200 @Test
1201 public void testLowerCaseFirstLetter()
1202 {
1203 assertThat( StringUtils.lowercaseFirstLetter( "Dings Bums" )
1204 , is( "dings Bums" ) );
1205 }
1206
1207 @Test( expected = IllegalArgumentException.class )
1208 public void testMid_NegativeLen()
1209 {
1210 StringUtils.mid( null, 0, -2 );
1211 }
1212
1213 @Test( expected = IndexOutOfBoundsException.class )
1214 public void testMid_WrongPos()
1215 {
1216 StringUtils.mid( null, -2, 3 );
1217 }
1218
1219 @Test
1220 public void testMid()
1221 {
1222 assertThat( StringUtils.mid( null, 0, 0 )
1223 , nullValue() );
1224
1225 assertThat( StringUtils.mid( "dings bums", 0, 0 )
1226 , is( "" ) );
1227
1228 assertThat( StringUtils.mid( "dings bums", 3, 4 )
1229 , is( "gs b" ) );
1230 }
1231
1232 @Test( expected = NullPointerException.class )
1233 public void testOverlayString_NPE1()
1234 {
1235 StringUtils.overlayString( null, null, 0, 0 );
1236 }
1237
1238 @Test( expected = NullPointerException.class )
1239 public void testOverlayString_NPE2()
1240 {
1241 StringUtils.overlayString( "dings", null, 0, 0 );
1242 }
1243
1244 @Test( expected = NullPointerException.class )
1245 public void testOverlayString_NPE3()
1246 {
1247 StringUtils.overlayString( null, "bums", 0, 0 );
1248 }
1249
1250 @Test
1251 public void testOverlayString()
1252 {
1253 assertThat( StringUtils.overlayString( "dings", "bums", 0, 0 )
1254 , is( "bumsdings" ) );
1255
1256 assertThat( StringUtils.overlayString( "dings", "bums", 2, 4 )
1257 , is( "dibumss" ) );
1258 }
1259
1260 @Test( expected = NullPointerException.class )
1261 public void testPrechomp_NPE1()
1262 {
1263 StringUtils.prechomp( null, null );
1264 }
1265
1266 @Test( expected = NullPointerException.class )
1267 public void testPrechomp_NPE2()
1268 {
1269 StringUtils.prechomp( "dings", null );
1270 }
1271
1272 @Test( expected = NullPointerException.class )
1273 public void testPrechomp_NPE3()
1274 {
1275 StringUtils.prechomp( null, "bums" );
1276 }
1277
1278 @Test
1279 public void testPrechomp()
1280 {
1281 assertThat( StringUtils.prechomp( "dings bums", " " )
1282 , is( "bums" ) );
1283
1284 assertThat( StringUtils.prechomp( "dings bums", "nix" )
1285 , is( "dings bums" ) );
1286 }
1287
1288 @Test
1289 public void testQuoteAndEscape1()
1290 {
1291 assertThat( StringUtils.quoteAndEscape( null, '+' )
1292 , nullValue() );
1293
1294 assertThat( StringUtils.quoteAndEscape( "", '+' )
1295 , is( "" ) );
1296
1297 assertThat( StringUtils.quoteAndEscape( "abc", '"' )
1298 , is( "abc" ) );
1299
1300 assertThat( StringUtils.quoteAndEscape( "a\"bc", '"' )
1301 , is( "\"a\\\"bc\"" ) );
1302
1303 assertThat( StringUtils.quoteAndEscape( "a\'bc", '\'' )
1304 , is( "\'a\\'bc\'" ) );
1305
1306 assertThat( StringUtils.quoteAndEscape( "a\"bc", '\'' )
1307 , is( "a\"bc" ) );
1308 }
1309
1310 @Test
1311 public void testQuoteAndEscape2()
1312 {
1313 assertThat( StringUtils.quoteAndEscape( null, '+', new char[]{ '"' } )
1314 , nullValue() );
1315
1316 assertThat( StringUtils.quoteAndEscape( "", '+', new char[]{ '"' } )
1317 , is( "" ) );
1318
1319 assertThat( StringUtils.quoteAndEscape( "abc", '"', new char[]{ '"' } )
1320 , is( "abc" ) );
1321
1322 assertThat( StringUtils.quoteAndEscape( "a\"bc", '"', new char[]{ '"' } )
1323 , is( "\"a\\\"bc\"" ) );
1324
1325 assertThat( StringUtils.quoteAndEscape( "a\'bc", '\'', new char[]{ '"' } )
1326 , is( "\'a\\'bc\'" ) );
1327
1328 assertThat( StringUtils.quoteAndEscape( "a\"bc", '\'', new char[]{ '\'' } )
1329 , is( "a\"bc" ) );
1330
1331 assertThat( StringUtils.quoteAndEscape( "a\"bc", '\'', new char[]{ '\'', '"' } )
1332 , is( "\'a\"bc\'" ) );
1333 }
1334
1335 @Test
1336 public void testQuoteAndEscape3()
1337 {
1338 assertThat( StringUtils.quoteAndEscape( null, '+', new char[]{ '"' }, '\\', false )
1339 , nullValue() );
1340
1341 assertThat( StringUtils.quoteAndEscape( "", '+', new char[]{ '"' }, '\\', false )
1342 , is( "" ) );
1343
1344 assertThat( StringUtils.quoteAndEscape( "abc", '"', new char[]{ '"' }, '\\', false )
1345 , is( "abc" ) );
1346
1347 assertThat( StringUtils.quoteAndEscape( "a\"bc", '"', new char[]{ '"' }, '\\', false )
1348 , is( "\"a\\\"bc\"" ) );
1349
1350 assertThat( StringUtils.quoteAndEscape( "a\'bc", '\'', new char[]{ '"' }, '\\', false )
1351 , is( "a\'bc" ) );
1352
1353 assertThat( StringUtils.quoteAndEscape( "a\"bc", '\'', new char[]{ '\'' }, '\\', false )
1354 , is( "a\"bc" ) );
1355
1356 assertThat( StringUtils.quoteAndEscape( "a\"bc", '\'', new char[]{ '\'', '"' }, '\\', false )
1357 , is( "\'a\\\"bc\'" ) );
1358
1359
1360 assertThat( StringUtils.quoteAndEscape( null, '+', new char[]{ '"' }, '\\', true )
1361 , nullValue() );
1362
1363 assertThat( StringUtils.quoteAndEscape( "", '+', new char[]{ '"' }, '\\', true )
1364 , is( "++" ) );
1365
1366 assertThat( StringUtils.quoteAndEscape( "abc", '"', new char[]{ '"' }, '\\', true )
1367 , is( "\"abc\"" ) );
1368
1369 assertThat( StringUtils.quoteAndEscape( "a\"bc", '"', new char[]{ '"' }, '\\', true )
1370 , is( "\"a\\\"bc\"" ) );
1371
1372 assertThat( StringUtils.quoteAndEscape( "a\'bc", '\'', new char[]{ '"' }, '\\', true )
1373 , is( "\'a\'bc\'" ) );
1374
1375 assertThat( StringUtils.quoteAndEscape( "a\"bc", '\'', new char[]{ '\'' }, '\\', true )
1376 , is( "\'a\"bc\'" ) );
1377
1378 assertThat( StringUtils.quoteAndEscape( "a\"bc", '\'', new char[]{ '\'', '"' }, '\\', true )
1379 , is( "\'a\\\"bc\'" ) );
1380 }
1381
1382 @Test
1383 public void testQuoteAndEscape4()
1384 {
1385 assertThat( StringUtils.quoteAndEscape( null, '+', new char[]{ '"' }, new char[]{ '"' }, '\\', false )
1386 , nullValue() );
1387
1388 assertThat( StringUtils.quoteAndEscape( "", '+', new char[]{ '"' }, new char[]{ '"' }, '\\', false )
1389 , is( "" ) );
1390
1391 assertThat( StringUtils.quoteAndEscape( "abc", '"', new char[]{ '"' }, new char[]{ '"' }, '\\', false )
1392 , is( "abc" ) );
1393
1394 assertThat( StringUtils.quoteAndEscape( "a\"bc", '"', new char[]{ '"' }, new char[]{ '"' }, '\\', false )
1395 , is( "\"a\\\"bc\"" ) );
1396
1397 assertThat( StringUtils.quoteAndEscape( "a\'bc", '\'', new char[]{ '"' }, new char[]{ '"' }, '\\', false )
1398 , is( "a\'bc" ) );
1399
1400 assertThat( StringUtils.quoteAndEscape( "a\"bc", '\'', new char[]{ '\'' }, new char[]{ '"' }, '\\', false )
1401 , is( "\'a\"bc\'" ) );
1402
1403 assertThat( StringUtils.quoteAndEscape( "\'a\"bc\'", '\'', new char[]{ '\'', '"' }, new char[]{ '"' }, '\\', false )
1404 , is( "\'a\"bc\'" ) );
1405
1406
1407 assertThat( StringUtils.quoteAndEscape( null, '+', new char[]{ '"' }, new char[]{ '"' }, '\\', true )
1408 , nullValue() );
1409
1410 assertThat( StringUtils.quoteAndEscape( "", '+', new char[]{ '"' }, new char[]{ '"' }, '\\', true )
1411 , is( "++" ) );
1412
1413 assertThat( StringUtils.quoteAndEscape( "abc", '"', new char[]{ '"' }, new char[]{ '"' }, '\\', true )
1414 , is( "\"abc\"" ) );
1415
1416 assertThat( StringUtils.quoteAndEscape( "a\"bc", '"', new char[]{ '"' }, new char[]{ '"' }, '\\', true )
1417 , is( "\"a\\\"bc\"" ) );
1418
1419 assertThat( StringUtils.quoteAndEscape( "a\'bc", '\'', new char[]{ '"' }, new char[]{ '"' }, '\\', true )
1420 , is( "\'a\'bc\'" ) );
1421
1422 assertThat( StringUtils.quoteAndEscape( "a\"bc", '\'', new char[]{ '\'' }, new char[]{ '"' }, '\\', true )
1423 , is( "\'a\"bc\'" ) );
1424
1425 assertThat( StringUtils.quoteAndEscape( "a\"bc", '\'', new char[]{ '\'', '"' }, new char[]{ '"' }, '\\', true )
1426 , is( "\'a\\\"bc\'" ) );
1427 }
1428
1429 @Test( expected = NullPointerException.class )
1430 public void testRemoveAndHump_NPE1()
1431 {
1432 StringUtils.removeAndHump( null, null );
1433 }
1434
1435 @Test( expected = NullPointerException.class )
1436 public void testRemoveAndHump_NPE2()
1437 {
1438 StringUtils.removeAndHump( "dings", null );
1439 }
1440
1441 @Test( expected = NullPointerException.class )
1442 public void testRemoveAndHump_NPE3()
1443 {
1444 StringUtils.removeAndHump( null, "bums" );
1445 }
1446
1447 @Test
1448 public void testRemoveAndHump()
1449 {
1450 assertThat( StringUtils.removeAndHump( "dings", "bums" )
1451 , is( "Ding" ) );
1452
1453 assertThat( StringUtils.removeAndHump( "this-is-it", "-" )
1454 , is( "ThisIsIt" ) );
1455
1456 assertThat( StringUtils.removeAndHump( "THIS-IS-IT", "-" )
1457 , is( "THISISIT" ) );
1458
1459 }
1460
1461 @Test( expected = NullPointerException.class )
1462 public void testRemoveDuplicateWhitespace_NPE()
1463 {
1464 StringUtils.removeDuplicateWhitespace( null );
1465 }
1466
1467 @Test
1468 public void testRemoveDuplicateWhitespace()
1469 {
1470 assertThat( StringUtils.removeDuplicateWhitespace( "dings" )
1471 , is( "dings" ) );
1472
1473 assertThat( StringUtils.removeDuplicateWhitespace( "dings bums" )
1474 , is( "dings bums" ) );
1475
1476 assertThat( StringUtils.removeDuplicateWhitespace( "dings bums" )
1477 , is( "dings bums" ) );
1478
1479 assertThat( StringUtils.removeDuplicateWhitespace( "dings \t bums" )
1480 , is( "dings bums" ) );
1481
1482 }
1483
1484 @Test( expected = NullPointerException.class )
1485 public void testRepeat_NPE()
1486 {
1487 StringUtils.repeat( null, 0 );
1488 }
1489
1490 @Test( expected = NegativeArraySizeException.class )
1491 public void testRepeat_NegativeAmount()
1492 {
1493 StringUtils.repeat( "dings", -1 );
1494 }
1495
1496
1497 @Test
1498 public void testRepeat()
1499 {
1500 assertThat( StringUtils.repeat( "dings", 0 )
1501 , is( "" ) );
1502
1503 assertThat( StringUtils.repeat( "dings", 1 )
1504 , is( "dings" ) );
1505
1506 assertThat( StringUtils.repeat( "dings", 3 )
1507 , is( "dingsdingsdings" ) );
1508 }
1509
1510
1511 @Test
1512 public void testReplace_char()
1513 {
1514 assertThat( StringUtils.replace( null, 'i', 'o' )
1515 , nullValue() );
1516
1517 assertThat( StringUtils.replace( "dings", 'i', 'o' )
1518 , is( "dongs" ) );
1519
1520 assertThat( StringUtils.replace( "dingsbims", 'i', 'o' )
1521 , is( "dongsboms" ) );
1522
1523 assertThat( StringUtils.replace( "dings", 'x', 'o' )
1524 , is( "dings" ) );
1525 }
1526
1527 @Test
1528 public void testReplace2_char_max()
1529 {
1530 assertThat( StringUtils.replace( null, 'i', 'o', 0 )
1531 , nullValue() );
1532
1533 assertThat( StringUtils.replace( "dingsibumsi", 'i', 'o', 3 )
1534 , is( "dongsobumso" ) );
1535
1536 assertThat( StringUtils.replace( "dingsibumsi", 'i', 'o', 2 )
1537 , is( "dongsobumsi" ) );
1538
1539 assertThat( StringUtils.replace( "dingsibumsi", 'i', 'o', 0 )
1540 , is( "dongsobumso" ) );
1541
1542 assertThat( StringUtils.replace( "dingsibumsi", 'i', 'o', -2 )
1543 , is( "dongsobumso" ) );
1544
1545 assertThat( StringUtils.replace( "dings", 'x', 'o', 2 )
1546 , is( "dings" ) );
1547 }
1548
1549 @Test
1550 public void testReplace_string()
1551 {
1552 assertThat( StringUtils.replace( null, "in", "ox" )
1553 , nullValue() );
1554
1555 assertThat( StringUtils.replace( "dings", "in", "ox" )
1556 , is( "doxgs" ) );
1557
1558 assertThat( StringUtils.replace( "dingsbins", "in", "ox" )
1559 , is( "doxgsboxs" ) );
1560
1561 assertThat( StringUtils.replace( "dings", "nin", "ox" )
1562 , is( "dings" ) );
1563 }
1564
1565
1566 @Test
1567 public void testReplace2_string_max()
1568 {
1569 assertThat( StringUtils.replace( null, "in", "ox", 0 )
1570 , nullValue() );
1571
1572 assertThat( StringUtils.replace( "dingsibumsi", "si", "xo", 3 )
1573 , is( "dingxobumxo" ) );
1574
1575 assertThat( StringUtils.replace( "dingsibumsi", "si", "xo", 2 )
1576 , is( "dingxobumxo" ) );
1577
1578 assertThat( StringUtils.replace( "dingsibumsi", "si", "xo", 1 )
1579 , is( "dingxobumsi" ) );
1580
1581 assertThat( StringUtils.replace( "dingsibumsi", "si", "xo", 0 )
1582 , is( "dingxobumxo" ) );
1583
1584 assertThat( StringUtils.replace( "dingsibumsi", "si", "xo", -2 )
1585 , is( "dingxobumxo" ) );
1586
1587 assertThat( StringUtils.replace( "dings", "si", "xo", 2 )
1588 , is( "dings" ) );
1589 }
1590
1591 @Test
1592 public void testReplaceOnce_char()
1593 {
1594 assertThat( StringUtils.replaceOnce( null, 'i', 'o' )
1595 , nullValue() );
1596
1597 assertThat( StringUtils.replaceOnce( "dingsibumsi", 'i', 'o' )
1598 , is( "dongsibumsi" ) );
1599
1600 assertThat( StringUtils.replaceOnce( "dings", 'x', 'o' )
1601 , is( "dings" ) );
1602 }
1603
1604 @Test
1605 public void testReplaceOnce_string()
1606 {
1607 assertThat( StringUtils.replaceOnce( null, "in", "ox" )
1608 , nullValue() );
1609
1610 assertThat( StringUtils.replaceOnce( "dingsibumsi", "si", "xo" )
1611 , is( "dingxobumsi" ) );
1612
1613 assertThat( StringUtils.replaceOnce( "dings", "si", "xo" )
1614 , is( "dings" ) );
1615 }
1616
1617
1618 @Test
1619 public void testReverse()
1620 {
1621 assertThat( StringUtils.reverse( null )
1622 , nullValue() );
1623
1624 assertThat( StringUtils.reverse( "" )
1625 , is( "" ) );
1626
1627 assertThat( StringUtils.reverse( "dings" )
1628 , is( "sgnid" ) );
1629
1630 assertThat( StringUtils.reverse( " dings " )
1631 , is( " sgnid " ) );
1632 }
1633
1634 @Test( expected = NullPointerException.class )
1635 public void testReverseDelimitedString_NPE1()
1636 {
1637 StringUtils.reverseDelimitedString( null, null );
1638 }
1639
1640 @Test( expected = NullPointerException.class )
1641 public void testReverseDelimitedString_NPE2()
1642 {
1643 StringUtils.reverseDelimitedString( null, " " );
1644 }
1645
1646 @Test
1647 public void testReverseDelimitedString()
1648 {
1649 assertThat( StringUtils.reverseDelimitedString( "dings", null )
1650 , is( "dings" ) );
1651
1652 assertThat( StringUtils.reverseDelimitedString( "", " " )
1653 , is( "" ) );
1654
1655 assertThat( StringUtils.reverseDelimitedString( "dings", " " )
1656 , is( "dings" ) );
1657
1658 assertThat( StringUtils.reverseDelimitedString( " dings ", " " )
1659 , is( "dings" ) );
1660
1661 assertThat( StringUtils.reverseDelimitedString( "dings bums", " " )
1662 , is( "bums dings" ) );
1663 }
1664
1665 @Test( expected = IllegalArgumentException.class )
1666 public void testRight_IAE1()
1667 {
1668 StringUtils.right( null, -1 );
1669 }
1670
1671 @Test( expected = IllegalArgumentException.class )
1672 public void testRight_IAE2()
1673 {
1674 StringUtils.right( "dings", -1 );
1675 }
1676
1677 @Test
1678 public void testRight()
1679 {
1680 assertThat( StringUtils.right( null, 0 )
1681 , nullValue() );
1682
1683 assertThat( StringUtils.right( "dings", 0 )
1684 , is( "" ) );
1685
1686 assertThat( StringUtils.right( "dings", 3 )
1687 , is( "ngs" ) );
1688
1689 assertThat( StringUtils.right( "dings ", 3 )
1690 , is( "gs " ) );
1691 }
1692
1693 @Test( expected = NullPointerException.class )
1694 public void testRightPad1_NPE()
1695 {
1696 StringUtils.rightPad( null, 0 );
1697 }
1698
1699 @Test
1700 public void testRightPad1()
1701 {
1702 assertThat( StringUtils.rightPad( "dings", 0 )
1703 , is( "dings" ) );
1704
1705 assertThat( StringUtils.rightPad( "dings", 3 )
1706 , is( "dings" ) );
1707
1708 assertThat( StringUtils.rightPad( "dings", 10 )
1709 , is( "dings " ) );
1710 }
1711
1712 @Test( expected = NullPointerException.class )
1713 public void testRightPad2_NPE1()
1714 {
1715 StringUtils.rightPad( null, 0, null );
1716 }
1717
1718 @Test( expected = NullPointerException.class )
1719 public void testRightPad2_NPE2()
1720 {
1721 StringUtils.rightPad( "dings", 0, null );
1722 }
1723
1724 @Test( expected = NullPointerException.class )
1725 public void testRightPad2_NPE23()
1726 {
1727 StringUtils.rightPad( null, 0, "+" );
1728 }
1729
1730 @Test
1731 public void testRightPad2()
1732 {
1733 assertThat( StringUtils.rightPad( "dings", 0, "+" )
1734 , is( "dings" ) );
1735
1736 assertThat( StringUtils.rightPad( "dings", 3, "+" )
1737 , is( "dings" ) );
1738
1739 assertThat( StringUtils.rightPad( "dings", 10, "+" )
1740 , is( "dings+++++" ) );
1741 }
1742
1743
1744 @Test( expected = NullPointerException.class )
1745 public void testSplit1_NPE()
1746 {
1747 StringUtils.split( null );
1748 }
1749
1750 @Test
1751 public void testSplit1()
1752 {
1753 assertThat( StringUtils.split( "dings" )
1754 , is( new String[]{ "dings" } ) );
1755
1756 assertThat( StringUtils.split( "dings bums" )
1757 , is( new String[]{ "dings", "bums" } ) );
1758 }
1759
1760 @Test( expected = NullPointerException.class )
1761 public void testSplit2_NPE1()
1762 {
1763 StringUtils.split( null, null );
1764 }
1765
1766 @Test( expected = NullPointerException.class )
1767 public void testSplit2_NPE2()
1768 {
1769 StringUtils.split( null, " " );
1770 }
1771
1772 @Test
1773 public void testSplit2()
1774 {
1775 assertThat( StringUtils.split( "dings", null )
1776 , is( new String[]{ "dings" } ) );
1777
1778 assertThat( StringUtils.split( "dings bums", null )
1779 , is( new String[]{ "dings", "bums" } ) );
1780
1781 assertThat( StringUtils.split( "dings", "+" )
1782 , is( new String[]{ "dings" } ) );
1783
1784 assertThat( StringUtils.split( "dings+bums", "+" )
1785 , is( new String[]{ "dings", "bums" } ) );
1786 }
1787
1788 @Test( expected = NullPointerException.class )
1789 public void testSplit3_NPE1()
1790 {
1791 StringUtils.split( null, null, 1 );
1792 }
1793
1794 @Test( expected = NullPointerException.class )
1795 public void testSplit3_NPE2()
1796 {
1797 StringUtils.split( null, " ", 1 );
1798 }
1799
1800 @Test
1801 public void testSplit3()
1802 {
1803 assertThat( StringUtils.split( "dings", null, 3 )
1804 , is( new String[]{ "dings" } ) );
1805
1806 assertThat( StringUtils.split( "dings bums", null, 3 )
1807 , is( new String[]{ "dings", "bums" } ) );
1808
1809 assertThat( StringUtils.split( "dings", "+", 3 )
1810 , is( new String[]{ "dings" } ) );
1811
1812 assertThat( StringUtils.split( "dings+bums", "+", 3 )
1813 , is( new String[]{ "dings", "bums" } ) );
1814
1815 assertThat( StringUtils.split( "dings+bums", "+", 1 )
1816 , is( new String[]{ "dings+bums" } ) );
1817
1818 assertThat( StringUtils.split( "dings+bums", "+", 0 )
1819 , is( new String[]{ "dings", "bums" } ) );
1820
1821 assertThat( StringUtils.split( "dings+bums", "+", -5 )
1822 , is( new String[]{ "dings", "bums" } ) );
1823
1824 }
1825
1826
1827 @Test
1828 public void testStrip1()
1829 {
1830 assertThat( StringUtils.strip( null )
1831 , nullValue() );
1832
1833 assertThat( StringUtils.strip( "dings" )
1834 , is( "dings" ) );
1835
1836 assertThat( StringUtils.strip( " dings \t " )
1837 , is( "dings" ) );
1838 }
1839
1840 @Test
1841 public void testStrip2()
1842 {
1843 assertThat( StringUtils.strip( null, " " )
1844 , nullValue() );
1845
1846 assertThat( StringUtils.strip( null, null )
1847 , nullValue() );
1848
1849 assertThat( StringUtils.strip( "dings", " " )
1850 , is( "dings" ) );
1851
1852 assertThat( StringUtils.strip( " dings \t ", " " )
1853 , is( "dings \t" ) );
1854 }
1855
1856 @Test
1857 public void testStripAll1()
1858 {
1859 assertThat( StringUtils.stripAll( null )
1860 , nullValue() );
1861
1862 assertThat( StringUtils.stripAll( new String[]{} )
1863 , is( new String[]{} ) );
1864
1865 assertThat( StringUtils.stripAll( new String[]{ "dings" } )
1866 , is( new String[]{ "dings" } ) );
1867
1868 assertThat( StringUtils.stripAll( new String[]{ " dings ", " bums \t " } )
1869 , is( new String[]{ "dings", "bums" } ) );
1870 }
1871
1872 @Test
1873 public void testStripAll2()
1874 {
1875 assertThat( StringUtils.stripAll( null, " " )
1876 , nullValue() );
1877
1878 assertThat( StringUtils.stripAll( new String[]{}, " " )
1879 , is( new String[]{} ) );
1880
1881 assertThat( StringUtils.stripAll( new String[]{ "dings" }, " " )
1882 , is( new String[]{ "dings" } ) );
1883
1884 assertThat( StringUtils.stripAll( new String[]{ " dings ", " bums \t " }, " " )
1885 , is( new String[]{ "dings", "bums \t" } ) );
1886 }
1887
1888 @Test
1889 public void testStripEnd()
1890 {
1891 assertThat( StringUtils.stripEnd( null, null )
1892 , nullValue() );
1893
1894 assertThat( StringUtils.stripEnd( "dings", null )
1895 , is( "dings" ) );
1896
1897 assertThat( StringUtils.stripEnd( " dings \t ", null )
1898 , is( " dings" ) );
1899
1900 assertThat( StringUtils.stripEnd( null, " " )
1901 , nullValue() );
1902
1903 assertThat( StringUtils.stripEnd( "dings", " " )
1904 , is( "dings" ) );
1905
1906 assertThat( StringUtils.stripEnd( " dings \t ", " " )
1907 , is( " dings \t" ) );
1908 }
1909
1910 @Test
1911 public void testStripStart()
1912 {
1913 assertThat( StringUtils.stripStart( null, null )
1914 , nullValue() );
1915
1916 assertThat( StringUtils.stripStart( "dings", null )
1917 , is( "dings" ) );
1918
1919 assertThat( StringUtils.stripStart( " dings \t ", null )
1920 , is( "dings \t " ) );
1921
1922 assertThat( StringUtils.stripStart( null, " " )
1923 , nullValue() );
1924
1925 assertThat( StringUtils.stripStart( "dings", " " )
1926 , is( "dings" ) );
1927
1928 assertThat( StringUtils.stripStart( " \t dings \t ", " " )
1929 , is( "\t dings \t " ) );
1930 }
1931
1932 @Test
1933 public void testSubstring1()
1934 {
1935 assertThat( StringUtils.substring( null, 0 )
1936 , nullValue() );
1937 assertThat( StringUtils.substring( null, -3 )
1938 , nullValue() );
1939
1940 assertThat( StringUtils.substring( "dings", 2 )
1941 , is( "ngs" ) );
1942
1943 assertThat( StringUtils.substring( "dings", -2 )
1944 , is( "gs" ) );
1945
1946 assertThat( StringUtils.substring( "dings", 20 )
1947 , is( "" ) );
1948 }
1949
1950 @Test
1951 public void testSubstring2()
1952 {
1953 assertThat( StringUtils.substring( null, 0, 2 )
1954 , nullValue() );
1955
1956 assertThat( StringUtils.substring( null, -3, 0 )
1957 , nullValue() );
1958
1959 assertThat( StringUtils.substring( "dings", 2, 4 )
1960 , is( "ng" ) );
1961
1962 assertThat( StringUtils.substring( "dings", -2, 4 )
1963 , is( "g" ) );
1964
1965 assertThat( StringUtils.substring( "dings", 20, 23 )
1966 , is( "" ) );
1967
1968 assertThat( StringUtils.substring( "dings", 4, 2 )
1969 , is( "" ) );
1970 }
1971
1972 @Test
1973 public void testSwapCase()
1974 {
1975 assertThat( StringUtils.swapCase( null )
1976 , nullValue() );
1977
1978 assertThat( StringUtils.swapCase( "dings" )
1979 , is( "DINGS" ) );
1980
1981 assertThat( StringUtils.swapCase( "DinGs" )
1982 , is( "dINgS" ) );
1983
1984 }
1985
1986 @Test
1987 public void testTrim()
1988 {
1989 assertThat( StringUtils.trim( null )
1990 , nullValue() );
1991
1992 assertThat( StringUtils.trim( " " )
1993 , is( "" ) );
1994
1995 assertThat( StringUtils.trim( " c " )
1996 , is( "c" ) );
1997
1998 assertThat( StringUtils.trim( " dings \n " )
1999 , is( "dings" ) );
2000 }
2001
2002 @Test
2003 public void testUncapitalise()
2004 {
2005 assertThat( StringUtils.uncapitalise( null )
2006 , nullValue() );
2007
2008 assertThat( StringUtils.uncapitalise( " " )
2009 , is( " " ) );
2010
2011 assertThat( StringUtils.uncapitalise( "dings" )
2012 , is( "dings" ) );
2013
2014 assertThat( StringUtils.uncapitalise( "Dings" )
2015 , is( "dings" ) );
2016
2017 assertThat( StringUtils.uncapitalise( "DINGS" )
2018 , is( "dINGS" ) );
2019 }
2020
2021 @Test
2022 public void testUncapitaliseAllWords()
2023 {
2024 assertThat( StringUtils.uncapitaliseAllWords( null )
2025 , nullValue() );
2026
2027 assertThat( StringUtils.uncapitaliseAllWords( " " )
2028 , is( " " ) );
2029
2030 assertThat( StringUtils.uncapitaliseAllWords( "dings bums" )
2031 , is( "dings bums" ) );
2032
2033 assertThat( StringUtils.uncapitaliseAllWords( "Dings Bums" )
2034 , is( "dings bums" ) );
2035
2036 assertThat( StringUtils.uncapitaliseAllWords( "DINGS Bums" )
2037 , is( "dINGS bums" ) );
2038 }
2039
2040 @Test
2041 public void testUnifyLineSeparators1()
2042 {
2043 String sls = System.getProperty( "line.separator" );
2044
2045 assertThat( StringUtils.unifyLineSeparators( null )
2046 , nullValue() );
2047
2048 assertThat( StringUtils.unifyLineSeparators( " " )
2049 , is( " " ) );
2050
2051 assertThat( StringUtils.unifyLineSeparators( "dings\nbums\r\ndongs" )
2052 , is( "dings" + sls + "bums" + sls + "dongs") );
2053 }
2054
2055 @Test
2056 public void testUnifyLineSeparators2()
2057 {
2058 assertThat( StringUtils.unifyLineSeparators( null, "\n" )
2059 , nullValue() );
2060
2061 assertThat( StringUtils.unifyLineSeparators( " ", "\n" )
2062 , is( " " ) );
2063
2064 assertThat( StringUtils.unifyLineSeparators( " ", null )
2065 , is( " " ) );
2066
2067 assertThat( StringUtils.unifyLineSeparators( "dings\nbums\r\ndongs", "\n" )
2068 , is( "dings\nbums\ndongs" ) );
2069 }
2070
2071 @Test
2072 public void testUppercase()
2073 {
2074 assertThat( StringUtils.upperCase( null )
2075 , nullValue() );
2076
2077 assertThat( StringUtils.upperCase( " " )
2078 , is( " " ) );
2079
2080 assertThat( StringUtils.upperCase( "" )
2081 , is( "" ) );
2082
2083 assertThat( StringUtils.upperCase( "dings" )
2084 , is( "DINGS" ) );
2085
2086 }
2087
2088 }