forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringUtilsTests.java
780 lines (685 loc) · 35.4 KB
/
StringUtilsTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util;
import java.util.Arrays;
import java.util.Locale;
import java.util.Properties;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* @author Rod Johnson
* @author Juergen Hoeller
* @author Rick Evans
* @author Sam Brannen
*/
class StringUtilsTests {
@Test
void hasTextBlank() {
String blank = " ";
assertThat(StringUtils.hasText(blank)).isEqualTo(false);
}
@Test
void hasTextNullEmpty() {
assertThat(StringUtils.hasText(null)).isEqualTo(false);
assertThat(StringUtils.hasText("")).isEqualTo(false);
}
@Test
void hasTextValid() {
assertThat(StringUtils.hasText("t")).isEqualTo(true);
}
@Test
void containsWhitespace() {
assertThat(StringUtils.containsWhitespace(null)).isFalse();
assertThat(StringUtils.containsWhitespace("")).isFalse();
assertThat(StringUtils.containsWhitespace("a")).isFalse();
assertThat(StringUtils.containsWhitespace("abc")).isFalse();
assertThat(StringUtils.containsWhitespace(" ")).isTrue();
assertThat(StringUtils.containsWhitespace("\t")).isTrue();
assertThat(StringUtils.containsWhitespace("\n")).isTrue();
assertThat(StringUtils.containsWhitespace(" a")).isTrue();
assertThat(StringUtils.containsWhitespace("abc ")).isTrue();
assertThat(StringUtils.containsWhitespace("a b")).isTrue();
assertThat(StringUtils.containsWhitespace("a b")).isTrue();
}
@Test
void trimWhitespace() {
assertThat(StringUtils.trimWhitespace(null)).isEqualTo(null);
assertThat(StringUtils.trimWhitespace("")).isEqualTo("");
assertThat(StringUtils.trimWhitespace(" ")).isEqualTo("");
assertThat(StringUtils.trimWhitespace("\t")).isEqualTo("");
assertThat(StringUtils.trimWhitespace("\n")).isEqualTo("");
assertThat(StringUtils.trimWhitespace(" \t\n")).isEqualTo("");
assertThat(StringUtils.trimWhitespace(" a")).isEqualTo("a");
assertThat(StringUtils.trimWhitespace("a ")).isEqualTo("a");
assertThat(StringUtils.trimWhitespace(" a ")).isEqualTo("a");
assertThat(StringUtils.trimWhitespace(" a b ")).isEqualTo("a b");
assertThat(StringUtils.trimWhitespace(" a b c ")).isEqualTo("a b c");
}
@Test
void trimAllWhitespace() {
assertThat(StringUtils.trimAllWhitespace(null)).isEqualTo(null);
assertThat(StringUtils.trimAllWhitespace("")).isEqualTo("");
assertThat(StringUtils.trimAllWhitespace(" ")).isEqualTo("");
assertThat(StringUtils.trimAllWhitespace("\t")).isEqualTo("");
assertThat(StringUtils.trimAllWhitespace("\n")).isEqualTo("");
assertThat(StringUtils.trimAllWhitespace(" \t\n")).isEqualTo("");
assertThat(StringUtils.trimAllWhitespace(" a")).isEqualTo("a");
assertThat(StringUtils.trimAllWhitespace("a ")).isEqualTo("a");
assertThat(StringUtils.trimAllWhitespace(" a ")).isEqualTo("a");
assertThat(StringUtils.trimAllWhitespace(" a b ")).isEqualTo("ab");
assertThat(StringUtils.trimAllWhitespace(" a b c ")).isEqualTo("abc");
}
@Test
void trimLeadingWhitespace() {
assertThat(StringUtils.trimLeadingWhitespace(null)).isEqualTo(null);
assertThat(StringUtils.trimLeadingWhitespace("")).isEqualTo("");
assertThat(StringUtils.trimLeadingWhitespace(" ")).isEqualTo("");
assertThat(StringUtils.trimLeadingWhitespace("\t")).isEqualTo("");
assertThat(StringUtils.trimLeadingWhitespace("\n")).isEqualTo("");
assertThat(StringUtils.trimLeadingWhitespace(" \t\n")).isEqualTo("");
assertThat(StringUtils.trimLeadingWhitespace(" a")).isEqualTo("a");
assertThat(StringUtils.trimLeadingWhitespace("a ")).isEqualTo("a ");
assertThat(StringUtils.trimLeadingWhitespace(" a ")).isEqualTo("a ");
assertThat(StringUtils.trimLeadingWhitespace(" a b ")).isEqualTo("a b ");
assertThat(StringUtils.trimLeadingWhitespace(" a b c ")).isEqualTo("a b c ");
}
@Test
void trimTrailingWhitespace() {
assertThat(StringUtils.trimTrailingWhitespace(null)).isEqualTo(null);
assertThat(StringUtils.trimTrailingWhitespace("")).isEqualTo("");
assertThat(StringUtils.trimTrailingWhitespace(" ")).isEqualTo("");
assertThat(StringUtils.trimTrailingWhitespace("\t")).isEqualTo("");
assertThat(StringUtils.trimTrailingWhitespace("\n")).isEqualTo("");
assertThat(StringUtils.trimTrailingWhitespace(" \t\n")).isEqualTo("");
assertThat(StringUtils.trimTrailingWhitespace("a ")).isEqualTo("a");
assertThat(StringUtils.trimTrailingWhitespace(" a")).isEqualTo(" a");
assertThat(StringUtils.trimTrailingWhitespace(" a ")).isEqualTo(" a");
assertThat(StringUtils.trimTrailingWhitespace(" a b ")).isEqualTo(" a b");
assertThat(StringUtils.trimTrailingWhitespace(" a b c ")).isEqualTo(" a b c");
}
@Test
void trimLeadingCharacter() {
assertThat(StringUtils.trimLeadingCharacter(null, ' ')).isEqualTo(null);
assertThat(StringUtils.trimLeadingCharacter("", ' ')).isEqualTo("");
assertThat(StringUtils.trimLeadingCharacter(" ", ' ')).isEqualTo("");
assertThat(StringUtils.trimLeadingCharacter("\t", ' ')).isEqualTo("\t");
assertThat(StringUtils.trimLeadingCharacter(" a", ' ')).isEqualTo("a");
assertThat(StringUtils.trimLeadingCharacter("a ", ' ')).isEqualTo("a ");
assertThat(StringUtils.trimLeadingCharacter(" a ", ' ')).isEqualTo("a ");
assertThat(StringUtils.trimLeadingCharacter(" a b ", ' ')).isEqualTo("a b ");
assertThat(StringUtils.trimLeadingCharacter(" a b c ", ' ')).isEqualTo("a b c ");
}
@Test
void trimTrailingCharacter() {
assertThat(StringUtils.trimTrailingCharacter(null, ' ')).isEqualTo(null);
assertThat(StringUtils.trimTrailingCharacter("", ' ')).isEqualTo("");
assertThat(StringUtils.trimTrailingCharacter(" ", ' ')).isEqualTo("");
assertThat(StringUtils.trimTrailingCharacter("\t", ' ')).isEqualTo("\t");
assertThat(StringUtils.trimTrailingCharacter("a ", ' ')).isEqualTo("a");
assertThat(StringUtils.trimTrailingCharacter(" a", ' ')).isEqualTo(" a");
assertThat(StringUtils.trimTrailingCharacter(" a ", ' ')).isEqualTo(" a");
assertThat(StringUtils.trimTrailingCharacter(" a b ", ' ')).isEqualTo(" a b");
assertThat(StringUtils.trimTrailingCharacter(" a b c ", ' ')).isEqualTo(" a b c");
}
@Test
void startsWithIgnoreCase() {
String prefix = "fOo";
assertThat(StringUtils.startsWithIgnoreCase("foo", prefix)).isTrue();
assertThat(StringUtils.startsWithIgnoreCase("Foo", prefix)).isTrue();
assertThat(StringUtils.startsWithIgnoreCase("foobar", prefix)).isTrue();
assertThat(StringUtils.startsWithIgnoreCase("foobarbar", prefix)).isTrue();
assertThat(StringUtils.startsWithIgnoreCase("Foobar", prefix)).isTrue();
assertThat(StringUtils.startsWithIgnoreCase("FoobarBar", prefix)).isTrue();
assertThat(StringUtils.startsWithIgnoreCase("foObar", prefix)).isTrue();
assertThat(StringUtils.startsWithIgnoreCase("FOObar", prefix)).isTrue();
assertThat(StringUtils.startsWithIgnoreCase("fOobar", prefix)).isTrue();
assertThat(StringUtils.startsWithIgnoreCase(null, prefix)).isFalse();
assertThat(StringUtils.startsWithIgnoreCase("fOobar", null)).isFalse();
assertThat(StringUtils.startsWithIgnoreCase("b", prefix)).isFalse();
assertThat(StringUtils.startsWithIgnoreCase("barfoo", prefix)).isFalse();
assertThat(StringUtils.startsWithIgnoreCase("barfoobar", prefix)).isFalse();
}
@Test
void endsWithIgnoreCase() {
String suffix = "fOo";
assertThat(StringUtils.endsWithIgnoreCase("foo", suffix)).isTrue();
assertThat(StringUtils.endsWithIgnoreCase("Foo", suffix)).isTrue();
assertThat(StringUtils.endsWithIgnoreCase("barfoo", suffix)).isTrue();
assertThat(StringUtils.endsWithIgnoreCase("barbarfoo", suffix)).isTrue();
assertThat(StringUtils.endsWithIgnoreCase("barFoo", suffix)).isTrue();
assertThat(StringUtils.endsWithIgnoreCase("barBarFoo", suffix)).isTrue();
assertThat(StringUtils.endsWithIgnoreCase("barfoO", suffix)).isTrue();
assertThat(StringUtils.endsWithIgnoreCase("barFOO", suffix)).isTrue();
assertThat(StringUtils.endsWithIgnoreCase("barfOo", suffix)).isTrue();
assertThat(StringUtils.endsWithIgnoreCase(null, suffix)).isFalse();
assertThat(StringUtils.endsWithIgnoreCase("barfOo", null)).isFalse();
assertThat(StringUtils.endsWithIgnoreCase("b", suffix)).isFalse();
assertThat(StringUtils.endsWithIgnoreCase("foobar", suffix)).isFalse();
assertThat(StringUtils.endsWithIgnoreCase("barfoobar", suffix)).isFalse();
}
@Test
void substringMatch() {
assertThat(StringUtils.substringMatch("foo", 0, "foo")).isTrue();
assertThat(StringUtils.substringMatch("foo", 1, "oo")).isTrue();
assertThat(StringUtils.substringMatch("foo", 2, "o")).isTrue();
assertThat(StringUtils.substringMatch("foo", 0, "fOo")).isFalse();
assertThat(StringUtils.substringMatch("foo", 1, "fOo")).isFalse();
assertThat(StringUtils.substringMatch("foo", 2, "fOo")).isFalse();
assertThat(StringUtils.substringMatch("foo", 3, "fOo")).isFalse();
assertThat(StringUtils.substringMatch("foo", 1, "Oo")).isFalse();
assertThat(StringUtils.substringMatch("foo", 2, "Oo")).isFalse();
assertThat(StringUtils.substringMatch("foo", 3, "Oo")).isFalse();
assertThat(StringUtils.substringMatch("foo", 2, "O")).isFalse();
assertThat(StringUtils.substringMatch("foo", 3, "O")).isFalse();
}
@Test
void countOccurrencesOf() {
assertThat(StringUtils.countOccurrencesOf(null, null) == 0).as("nullx2 = 0").isTrue();
assertThat(StringUtils.countOccurrencesOf("s", null) == 0).as("null string = 0").isTrue();
assertThat(StringUtils.countOccurrencesOf(null, "s") == 0).as("null substring = 0").isTrue();
String s = "erowoiueoiur";
assertThat(StringUtils.countOccurrencesOf(s, "WERWER") == 0).as("not found = 0").isTrue();
assertThat(StringUtils.countOccurrencesOf(s, "x") == 0).as("not found char = 0").isTrue();
assertThat(StringUtils.countOccurrencesOf(s, " ") == 0).as("not found ws = 0").isTrue();
assertThat(StringUtils.countOccurrencesOf(s, "") == 0).as("not found empty string = 0").isTrue();
assertThat(StringUtils.countOccurrencesOf(s, "e") == 2).as("found char=2").isTrue();
assertThat(StringUtils.countOccurrencesOf(s, "oi") == 2).as("found substring=2").isTrue();
assertThat(StringUtils.countOccurrencesOf(s, "oiu") == 2).as("found substring=2").isTrue();
assertThat(StringUtils.countOccurrencesOf(s, "oiur") == 1).as("found substring=3").isTrue();
assertThat(StringUtils.countOccurrencesOf(s, "r") == 2).as("test last").isTrue();
}
@Test
void replace() {
String inString = "a6AazAaa77abaa";
String oldPattern = "aa";
String newPattern = "foo";
// Simple replace
String s = StringUtils.replace(inString, oldPattern, newPattern);
assertThat(s.equals("a6AazAfoo77abfoo")).as("Replace 1 worked").isTrue();
// Non match: no change
s = StringUtils.replace(inString, "qwoeiruqopwieurpoqwieur", newPattern);
assertThat(s).as("Replace non-matched is returned as-is").isSameAs(inString);
// Null new pattern: should ignore
s = StringUtils.replace(inString, oldPattern, null);
assertThat(s).as("Replace non-matched is returned as-is").isSameAs(inString);
// Null old pattern: should ignore
s = StringUtils.replace(inString, null, newPattern);
assertThat(s).as("Replace non-matched is returned as-is").isSameAs(inString);
}
@Test
void delete() {
String inString = "The quick brown fox jumped over the lazy dog";
String noThe = StringUtils.delete(inString, "the");
assertThat(noThe.equals("The quick brown fox jumped over lazy dog")).as("Result has no the [" + noThe + "]").isTrue();
String nohe = StringUtils.delete(inString, "he");
assertThat(nohe.equals("T quick brown fox jumped over t lazy dog")).as("Result has no he [" + nohe + "]").isTrue();
String nosp = StringUtils.delete(inString, " ");
assertThat(nosp.equals("Thequickbrownfoxjumpedoverthelazydog")).as("Result has no spaces").isTrue();
String killEnd = StringUtils.delete(inString, "dog");
assertThat(killEnd.equals("The quick brown fox jumped over the lazy ")).as("Result has no dog").isTrue();
String mismatch = StringUtils.delete(inString, "dxxcxcxog");
assertThat(mismatch.equals(inString)).as("Result is unchanged").isTrue();
String nochange = StringUtils.delete(inString, "");
assertThat(nochange.equals(inString)).as("Result is unchanged").isTrue();
}
@Test
void deleteAny() {
String inString = "Able was I ere I saw Elba";
String res = StringUtils.deleteAny(inString, "I");
assertThat(res).as("Result has no 'I'").isEqualTo("Able was ere saw Elba");
res = StringUtils.deleteAny(inString, "AeEba!");
assertThat(res).as("Result has no 'AeEba!'").isEqualTo("l ws I r I sw l");
res = StringUtils.deleteAny(inString, "#@$#$^");
assertThat(res).as("Result is unchanged").isEqualTo(inString);
}
@Test
void deleteAnyWhitespace() {
String whitespace = "This is\n\n\n \t a messagy string with whitespace\n";
assertThat(whitespace).as("Has CR").contains("\n");
assertThat(whitespace).as("Has tab").contains("\t");
assertThat(whitespace).as("Has space").contains(" ");
String cleaned = StringUtils.deleteAny(whitespace, "\n\t ");
assertThat(cleaned).as("Has no CR").doesNotContain("\n");
assertThat(cleaned).as("Has no tab").doesNotContain("\t");
assertThat(cleaned).as("Has no space").doesNotContain(" ");
assertThat(cleaned.length()).as("Still has chars").isGreaterThan(10);
}
@Test
void quote() {
assertThat(StringUtils.quote("myString")).isEqualTo("'myString'");
assertThat(StringUtils.quote("")).isEqualTo("''");
assertThat(StringUtils.quote(null)).isNull();
}
@Test
void quoteIfString() {
assertThat(StringUtils.quoteIfString("myString")).isEqualTo("'myString'");
assertThat(StringUtils.quoteIfString("")).isEqualTo("''");
assertThat(StringUtils.quoteIfString(5)).isEqualTo(5);
assertThat(StringUtils.quoteIfString(null)).isNull();
}
@Test
void unqualify() {
String qualified = "i.am.not.unqualified";
assertThat(StringUtils.unqualify(qualified)).isEqualTo("unqualified");
}
@Test
void capitalize() {
String capitalized = "i am not capitalized";
assertThat(StringUtils.capitalize(capitalized)).isEqualTo("I am not capitalized");
}
@Test
void uncapitalize() {
String capitalized = "I am capitalized";
assertThat(StringUtils.uncapitalize(capitalized)).isEqualTo("i am capitalized");
}
@Test
void getFilename() {
assertThat(StringUtils.getFilename(null)).isEqualTo(null);
assertThat(StringUtils.getFilename("")).isEqualTo("");
assertThat(StringUtils.getFilename("myfile")).isEqualTo("myfile");
assertThat(StringUtils.getFilename("mypath/myfile")).isEqualTo("myfile");
assertThat(StringUtils.getFilename("myfile.")).isEqualTo("myfile.");
assertThat(StringUtils.getFilename("mypath/myfile.")).isEqualTo("myfile.");
assertThat(StringUtils.getFilename("myfile.txt")).isEqualTo("myfile.txt");
assertThat(StringUtils.getFilename("mypath/myfile.txt")).isEqualTo("myfile.txt");
}
@Test
void getFilenameExtension() {
assertThat(StringUtils.getFilenameExtension(null)).isEqualTo(null);
assertThat(StringUtils.getFilenameExtension("")).isEqualTo(null);
assertThat(StringUtils.getFilenameExtension("myfile")).isEqualTo(null);
assertThat(StringUtils.getFilenameExtension("myPath/myfile")).isEqualTo(null);
assertThat(StringUtils.getFilenameExtension("/home/user/.m2/settings/myfile")).isEqualTo(null);
assertThat(StringUtils.getFilenameExtension("myfile.")).isEqualTo("");
assertThat(StringUtils.getFilenameExtension("myPath/myfile.")).isEqualTo("");
assertThat(StringUtils.getFilenameExtension("myfile.txt")).isEqualTo("txt");
assertThat(StringUtils.getFilenameExtension("mypath/myfile.txt")).isEqualTo("txt");
assertThat(StringUtils.getFilenameExtension("/home/user/.m2/settings/myfile.txt")).isEqualTo("txt");
}
@Test
void stripFilenameExtension() {
assertThat(StringUtils.stripFilenameExtension("")).isEqualTo("");
assertThat(StringUtils.stripFilenameExtension("myfile")).isEqualTo("myfile");
assertThat(StringUtils.stripFilenameExtension("myfile.")).isEqualTo("myfile");
assertThat(StringUtils.stripFilenameExtension("myfile.txt")).isEqualTo("myfile");
assertThat(StringUtils.stripFilenameExtension("mypath/myfile")).isEqualTo("mypath/myfile");
assertThat(StringUtils.stripFilenameExtension("mypath/myfile.")).isEqualTo("mypath/myfile");
assertThat(StringUtils.stripFilenameExtension("mypath/myfile.txt")).isEqualTo("mypath/myfile");
assertThat(StringUtils.stripFilenameExtension("/home/user/.m2/settings/myfile")).isEqualTo("/home/user/.m2/settings/myfile");
assertThat(StringUtils.stripFilenameExtension("/home/user/.m2/settings/myfile.")).isEqualTo("/home/user/.m2/settings/myfile");
assertThat(StringUtils.stripFilenameExtension("/home/user/.m2/settings/myfile.txt")).isEqualTo("/home/user/.m2/settings/myfile");
}
@Test
void cleanPath() {
assertThat(StringUtils.cleanPath("mypath/myfile")).isEqualTo("mypath/myfile");
assertThat(StringUtils.cleanPath("mypath\\myfile")).isEqualTo("mypath/myfile");
assertThat(StringUtils.cleanPath("mypath/../mypath/myfile")).isEqualTo("mypath/myfile");
assertThat(StringUtils.cleanPath("mypath/myfile/../../mypath/myfile")).isEqualTo("mypath/myfile");
assertThat(StringUtils.cleanPath("../mypath/myfile")).isEqualTo("../mypath/myfile");
assertThat(StringUtils.cleanPath("../mypath/../mypath/myfile")).isEqualTo("../mypath/myfile");
assertThat(StringUtils.cleanPath("mypath/../../mypath/myfile")).isEqualTo("../mypath/myfile");
assertThat(StringUtils.cleanPath("/../mypath/myfile")).isEqualTo("/../mypath/myfile");
assertThat(StringUtils.cleanPath("/a/:b/../../mypath/myfile")).isEqualTo("/mypath/myfile");
assertThat(StringUtils.cleanPath("/")).isEqualTo("/");
assertThat(StringUtils.cleanPath("/mypath/../")).isEqualTo("/");
assertThat(StringUtils.cleanPath("mypath/..")).isEqualTo("");
assertThat(StringUtils.cleanPath("mypath/../.")).isEqualTo("");
assertThat(StringUtils.cleanPath("mypath/../")).isEqualTo("./");
assertThat(StringUtils.cleanPath("././")).isEqualTo("./");
assertThat(StringUtils.cleanPath("./")).isEqualTo("./");
assertThat(StringUtils.cleanPath("../")).isEqualTo("../");
assertThat(StringUtils.cleanPath("./../")).isEqualTo("../");
assertThat(StringUtils.cleanPath(".././")).isEqualTo("../");
assertThat(StringUtils.cleanPath("file:/")).isEqualTo("file:/");
assertThat(StringUtils.cleanPath("file:/mypath/../")).isEqualTo("file:/");
assertThat(StringUtils.cleanPath("file:mypath/..")).isEqualTo("file:");
assertThat(StringUtils.cleanPath("file:mypath/../.")).isEqualTo("file:");
assertThat(StringUtils.cleanPath("file:mypath/../")).isEqualTo("file:./");
assertThat(StringUtils.cleanPath("file:././")).isEqualTo("file:./");
assertThat(StringUtils.cleanPath("file:./")).isEqualTo("file:./");
assertThat(StringUtils.cleanPath("file:../")).isEqualTo("file:../");
assertThat(StringUtils.cleanPath("file:./../")).isEqualTo("file:../");
assertThat(StringUtils.cleanPath("file:.././")).isEqualTo("file:../");
assertThat(StringUtils.cleanPath("file:/mypath/spring.factories")).isEqualTo("file:/mypath/spring.factories");
assertThat(StringUtils.cleanPath("file:///c:/some/../path/the%20file.txt")).isEqualTo("file:///c:/path/the%20file.txt");
assertThat(StringUtils.cleanPath("jar:file:///c:\\some\\..\\path\\.\\the%20file.txt")).isEqualTo("jar:file:///c:/path/the%20file.txt");
assertThat(StringUtils.cleanPath("jar:file:///c:/some/../path/./the%20file.txt")).isEqualTo("jar:file:///c:/path/the%20file.txt");
}
@Test
void pathEquals() {
assertThat(StringUtils.pathEquals("/dummy1/dummy2/dummy3", "/dummy1/dummy2/dummy3")).as("Must be true for the same strings").isTrue();
assertThat(StringUtils.pathEquals("C:\\dummy1\\dummy2\\dummy3", "C:\\dummy1\\dummy2\\dummy3")).as("Must be true for the same win strings").isTrue();
assertThat(StringUtils.pathEquals("/dummy1/bin/../dummy2/dummy3", "/dummy1/dummy2/dummy3")).as("Must be true for one top path on 1").isTrue();
assertThat(StringUtils.pathEquals("C:\\dummy1\\dummy2\\dummy3", "C:\\dummy1\\bin\\..\\dummy2\\dummy3")).as("Must be true for one win top path on 2").isTrue();
assertThat(StringUtils.pathEquals("/dummy1/bin/../dummy2/bin/../dummy3", "/dummy1/dummy2/dummy3")).as("Must be true for two top paths on 1").isTrue();
assertThat(StringUtils.pathEquals("C:\\dummy1\\dummy2\\dummy3", "C:\\dummy1\\bin\\..\\dummy2\\bin\\..\\dummy3")).as("Must be true for two win top paths on 2").isTrue();
assertThat(StringUtils.pathEquals("/dummy1/bin/tmp/../../dummy2/dummy3", "/dummy1/dummy2/dummy3")).as("Must be true for double top paths on 1").isTrue();
assertThat(StringUtils.pathEquals("/dummy1/dummy2/dummy3", "/dummy1/dum/dum/../../dummy2/dummy3")).as("Must be true for double top paths on 2 with similarity").isTrue();
assertThat(StringUtils.pathEquals("./dummy1/dummy2/dummy3", "dummy1/dum/./dum/../../dummy2/dummy3")).as("Must be true for current paths").isTrue();
assertThat(StringUtils.pathEquals("./dummy1/dummy2/dummy3", "/dummy1/dum/./dum/../../dummy2/dummy3")).as("Must be false for relative/absolute paths").isFalse();
assertThat(StringUtils.pathEquals("/dummy1/dummy2/dummy3", "/dummy1/dummy4/dummy3")).as("Must be false for different strings").isFalse();
assertThat(StringUtils.pathEquals("/dummy1/bin/tmp/../dummy2/dummy3", "/dummy1/dummy2/dummy3")).as("Must be false for one false path on 1").isFalse();
assertThat(StringUtils.pathEquals("C:\\dummy1\\dummy2\\dummy3", "C:\\dummy1\\bin\\tmp\\..\\dummy2\\dummy3")).as("Must be false for one false win top path on 2").isFalse();
assertThat(StringUtils.pathEquals("/dummy1/bin/../dummy2/dummy3", "/dummy1/dummy2/dummy4")).as("Must be false for top path on 1 + difference").isFalse();
}
@Test
void concatenateStringArrays() {
String[] input1 = new String[] {"myString2"};
String[] input2 = new String[] {"myString1", "myString2"};
String[] result = StringUtils.concatenateStringArrays(input1, input2);
assertThat(result.length).isEqualTo(3);
assertThat(result[0]).isEqualTo("myString2");
assertThat(result[1]).isEqualTo("myString1");
assertThat(result[2]).isEqualTo("myString2");
assertThat(StringUtils.concatenateStringArrays(input1, null)).isEqualTo(input1);
assertThat(StringUtils.concatenateStringArrays(null, input2)).isEqualTo(input2);
assertThat(StringUtils.concatenateStringArrays(null, null)).isNull();
}
@Test
@Deprecated
void mergeStringArrays() {
String[] input1 = new String[] {"myString2"};
String[] input2 = new String[] {"myString1", "myString2"};
String[] result = StringUtils.mergeStringArrays(input1, input2);
assertThat(result.length).isEqualTo(2);
assertThat(result[0]).isEqualTo("myString2");
assertThat(result[1]).isEqualTo("myString1");
assertThat(StringUtils.mergeStringArrays(input1, null)).isEqualTo(input1);
assertThat(StringUtils.mergeStringArrays(null, input2)).isEqualTo(input2);
assertThat(StringUtils.mergeStringArrays(null, null)).isNull();
}
@Test
void sortStringArray() {
String[] input = new String[] {"myString2"};
input = StringUtils.addStringToArray(input, "myString1");
assertThat(input[0]).isEqualTo("myString2");
assertThat(input[1]).isEqualTo("myString1");
StringUtils.sortStringArray(input);
assertThat(input[0]).isEqualTo("myString1");
assertThat(input[1]).isEqualTo("myString2");
}
@Test
void removeDuplicateStrings() {
String[] input = new String[] {"myString2", "myString1", "myString2"};
input = StringUtils.removeDuplicateStrings(input);
assertThat(input[0]).isEqualTo("myString2");
assertThat(input[1]).isEqualTo("myString1");
}
@Test
void splitArrayElementsIntoProperties() {
String[] input = new String[] {"key1=value1 ", "key2 =\"value2\""};
Properties result = StringUtils.splitArrayElementsIntoProperties(input, "=");
assertThat(result.getProperty("key1")).isEqualTo("value1");
assertThat(result.getProperty("key2")).isEqualTo("\"value2\"");
}
@Test
void splitArrayElementsIntoPropertiesAndDeletedChars() {
String[] input = new String[] {"key1=value1 ", "key2 =\"value2\""};
Properties result = StringUtils.splitArrayElementsIntoProperties(input, "=", "\"");
assertThat(result.getProperty("key1")).isEqualTo("value1");
assertThat(result.getProperty("key2")).isEqualTo("value2");
}
@Test
void tokenizeToStringArray() {
String[] sa = StringUtils.tokenizeToStringArray("a,b , ,c", ",");
assertThat(sa.length).isEqualTo(3);
assertThat(sa[0].equals("a") && sa[1].equals("b") && sa[2].equals("c")).as("components are correct").isTrue();
}
@Test
void tokenizeToStringArrayWithNotIgnoreEmptyTokens() {
String[] sa = StringUtils.tokenizeToStringArray("a,b , ,c", ",", true, false);
assertThat(sa.length).isEqualTo(4);
assertThat(sa[0].equals("a") && sa[1].equals("b") && sa[2].isEmpty() && sa[3].equals("c")).as("components are correct").isTrue();
}
@Test
void tokenizeToStringArrayWithNotTrimTokens() {
String[] sa = StringUtils.tokenizeToStringArray("a,b ,c", ",", false, true);
assertThat(sa.length).isEqualTo(3);
assertThat(sa[0].equals("a") && sa[1].equals("b ") && sa[2].equals("c")).as("components are correct").isTrue();
}
@Test
void commaDelimitedListToStringArrayWithNullProducesEmptyArray() {
String[] sa = StringUtils.commaDelimitedListToStringArray(null);
assertThat(sa != null).as("String array isn't null with null input").isTrue();
assertThat(sa.length == 0).as("String array length == 0 with null input").isTrue();
}
@Test
void commaDelimitedListToStringArrayWithEmptyStringProducesEmptyArray() {
String[] sa = StringUtils.commaDelimitedListToStringArray("");
assertThat(sa != null).as("String array isn't null with null input").isTrue();
assertThat(sa.length == 0).as("String array length == 0 with null input").isTrue();
}
@Test
void delimitedListToStringArrayWithComma() {
String[] sa = StringUtils.delimitedListToStringArray("a,b", ",");
assertThat(sa.length).isEqualTo(2);
assertThat(sa[0]).isEqualTo("a");
assertThat(sa[1]).isEqualTo("b");
}
@Test
void delimitedListToStringArrayWithSemicolon() {
String[] sa = StringUtils.delimitedListToStringArray("a;b", ";");
assertThat(sa.length).isEqualTo(2);
assertThat(sa[0]).isEqualTo("a");
assertThat(sa[1]).isEqualTo("b");
}
@Test
void delimitedListToStringArrayWithEmptyDelimiter() {
String[] sa = StringUtils.delimitedListToStringArray("a,b", "");
assertThat(sa.length).isEqualTo(3);
assertThat(sa[0]).isEqualTo("a");
assertThat(sa[1]).isEqualTo(",");
assertThat(sa[2]).isEqualTo("b");
}
@Test
void delimitedListToStringArrayWithNullDelimiter() {
String[] sa = StringUtils.delimitedListToStringArray("a,b", null);
assertThat(sa.length).isEqualTo(1);
assertThat(sa[0]).isEqualTo("a,b");
}
@Test
void commaDelimitedListToStringArrayMatchWords() {
// Could read these from files
String[] sa = new String[] {"foo", "bar", "big"};
doTestCommaDelimitedListToStringArrayLegalMatch(sa);
doTestStringArrayReverseTransformationMatches(sa);
sa = new String[] {"a", "b", "c"};
doTestCommaDelimitedListToStringArrayLegalMatch(sa);
doTestStringArrayReverseTransformationMatches(sa);
// Test same words
sa = new String[] {"AA", "AA", "AA", "AA", "AA"};
doTestCommaDelimitedListToStringArrayLegalMatch(sa);
doTestStringArrayReverseTransformationMatches(sa);
}
private void doTestStringArrayReverseTransformationMatches(String[] sa) {
String[] reverse =
StringUtils.commaDelimitedListToStringArray(StringUtils.arrayToCommaDelimitedString(sa));
assertThat(Arrays.asList(reverse)).as("Reverse transformation is equal").isEqualTo(Arrays.asList(sa));
}
@Test
void commaDelimitedListToStringArraySingleString() {
// Could read these from files
String s = "woeirqupoiewuropqiewuorpqiwueopriquwopeiurqopwieur";
String[] sa = StringUtils.commaDelimitedListToStringArray(s);
assertThat(sa.length == 1).as("Found one String with no delimiters").isTrue();
assertThat(sa[0].equals(s)).as("Single array entry matches input String with no delimiters").isTrue();
}
@Test
void commaDelimitedListToStringArrayWithOtherPunctuation() {
// Could read these from files
String[] sa = new String[] {"xcvwert4456346&*.", "///", ".!", ".", ";"};
doTestCommaDelimitedListToStringArrayLegalMatch(sa);
}
/**
* We expect to see the empty Strings in the output.
*/
@Test
void commaDelimitedListToStringArrayEmptyStrings() {
// Could read these from files
String[] sa = StringUtils.commaDelimitedListToStringArray("a,,b");
assertThat(sa.length).as("a,,b produces array length 3").isEqualTo(3);
assertThat(sa[0].equals("a") && sa[1].isEmpty() && sa[2].equals("b")).as("components are correct").isTrue();
sa = new String[] {"", "", "a", ""};
doTestCommaDelimitedListToStringArrayLegalMatch(sa);
}
private void doTestCommaDelimitedListToStringArrayLegalMatch(String[] components) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < components.length; i++) {
if (i != 0) {
sb.append(',');
}
sb.append(components[i]);
}
String[] sa = StringUtils.commaDelimitedListToStringArray(sb.toString());
assertThat(sa != null).as("String array isn't null with legal match").isTrue();
assertThat(sa.length).as("String array length is correct with legal match").isEqualTo(components.length);
assertThat(Arrays.equals(sa, components)).as("Output equals input").isTrue();
}
@Test
void parseLocaleStringSunnyDay() {
Locale expectedLocale = Locale.UK;
Locale locale = StringUtils.parseLocaleString(expectedLocale.toString());
assertThat(locale).as("When given a bona-fide Locale string, must not return null.").isNotNull();
assertThat(locale).isEqualTo(expectedLocale);
}
@Test
void parseLocaleStringWithMalformedLocaleString() {
Locale locale = StringUtils.parseLocaleString("_banjo_on_my_knee");
assertThat(locale).as("When given a malformed Locale string, must not return null.").isNotNull();
}
@Test
void parseLocaleStringWithEmptyLocaleStringYieldsNullLocale() {
Locale locale = StringUtils.parseLocaleString("");
assertThat(locale).as("When given an empty Locale string, must return null.").isNull();
}
@Test // SPR-8637
void parseLocaleWithMultiSpecialCharactersInVariant() {
String variant = "proper-northern";
String localeString = "en_GB_" + variant;
Locale locale = StringUtils.parseLocaleString(localeString);
assertThat(locale.getVariant()).as("Multi-valued variant portion of the Locale not extracted correctly.").isEqualTo(variant);
}
@Test // SPR-3671
void parseLocaleWithMultiValuedVariant() {
String variant = "proper_northern";
String localeString = "en_GB_" + variant;
Locale locale = StringUtils.parseLocaleString(localeString);
assertThat(locale.getVariant()).as("Multi-valued variant portion of the Locale not extracted correctly.").isEqualTo(variant);
}
@Test // SPR-3671
void parseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() {
String variant = "proper northern";
String localeString = "en GB " + variant;
Locale locale = StringUtils.parseLocaleString(localeString);
assertThat(locale.getVariant()).as("Multi-valued variant portion of the Locale not extracted correctly.").isEqualTo(variant);
}
@Test // SPR-3671
void parseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators() {
String variant = "proper northern";
String localeString = "en_GB_" + variant;
Locale locale = StringUtils.parseLocaleString(localeString);
assertThat(locale.getVariant()).as("Multi-valued variant portion of the Locale not extracted correctly.").isEqualTo(variant);
}
@Test // SPR-3671
void parseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace() {
String variant = "proper northern";
String localeString = "en GB " + variant; // lots of whitespace
Locale locale = StringUtils.parseLocaleString(localeString);
assertThat(locale.getVariant()).as("Multi-valued variant portion of the Locale not extracted correctly.").isEqualTo(variant);
}
@Test // SPR-3671
void parseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() {
String variant = "proper_northern";
String localeString = "en_GB_____" + variant; // lots of underscores
Locale locale = StringUtils.parseLocaleString(localeString);
assertThat(locale.getVariant()).as("Multi-valued variant portion of the Locale not extracted correctly.").isEqualTo(variant);
}
@Test // SPR-7779
void parseLocaleWithInvalidCharacters() {
assertThatIllegalArgumentException().isThrownBy(() ->
StringUtils.parseLocaleString("%0D%0AContent-length:30%0D%0A%0D%0A%3Cscript%3Ealert%28123%29%3C/script%3E"));
}
@Test // SPR-9420
void parseLocaleWithSameLowercaseTokenForLanguageAndCountry() {
assertThat(StringUtils.parseLocaleString("tr_tr").toString()).isEqualTo("tr_TR");
assertThat(StringUtils.parseLocaleString("bg_bg_vnt").toString()).isEqualTo("bg_BG_vnt");
}
@Test // SPR-11806
void parseLocaleWithVariantContainingCountryCode() {
String variant = "GBtest";
String localeString = "en_GB_" + variant;
Locale locale = StringUtils.parseLocaleString(localeString);
assertThat(locale.getVariant()).as("Variant containing country code not extracted correctly").isEqualTo(variant);
}
@Test // SPR-14718, SPR-7598
void parseJava7Variant() {
assertThat(StringUtils.parseLocaleString("sr__#LATN").toString()).isEqualTo("sr__#LATN");
}
@Test // SPR-16651
void availableLocalesWithLocaleString() {
for (Locale locale : Locale.getAvailableLocales()) {
Locale parsedLocale = StringUtils.parseLocaleString(locale.toString());
if (parsedLocale == null) {
assertThat(locale.getLanguage()).isEqualTo("");
}
else {
assertThat(locale.toString()).isEqualTo(parsedLocale.toString());
}
}
}
@Test // SPR-16651
void availableLocalesWithLanguageTag() {
for (Locale locale : Locale.getAvailableLocales()) {
Locale parsedLocale = StringUtils.parseLocale(locale.toLanguageTag());
if (parsedLocale == null) {
assertThat(locale.getLanguage()).isEqualTo("");
}
else {
assertThat(locale.toLanguageTag()).isEqualTo(parsedLocale.toLanguageTag());
}
}
}
@Test
void invalidLocaleWithLocaleString() {
assertThat(StringUtils.parseLocaleString("invalid")).isEqualTo(new Locale("invalid"));
assertThat(StringUtils.parseLocaleString("invalidvalue")).isEqualTo(new Locale("invalidvalue"));
assertThat(StringUtils.parseLocaleString("invalidvalue_foo")).isEqualTo(new Locale("invalidvalue", "foo"));
assertThat(StringUtils.parseLocaleString("")).isNull();
}
@Test
void invalidLocaleWithLanguageTag() {
assertThat(StringUtils.parseLocale("invalid")).isEqualTo(new Locale("invalid"));
assertThat(StringUtils.parseLocale("invalidvalue")).isEqualTo(new Locale("invalidvalue"));
assertThat(StringUtils.parseLocale("invalidvalue_foo")).isEqualTo(new Locale("invalidvalue", "foo"));
assertThat(StringUtils.parseLocale("")).isNull();
}
@Test
void split() {
assertThat(StringUtils.split("Hello, world", ",")).containsExactly("Hello", " world");
assertThat(StringUtils.split(",Hello world", ",")).containsExactly("", "Hello world");
assertThat(StringUtils.split("Hello world,", ",")).containsExactly("Hello world", "");
assertThat(StringUtils.split("Hello, world,", ",")).containsExactly("Hello", " world,");
}
@Test
void splitWithEmptyStringOrNull() {
assertThat(StringUtils.split("Hello, world", "")).isNull();
assertThat(StringUtils.split("", ",")).isNull();
assertThat(StringUtils.split(null, ",")).isNull();
assertThat(StringUtils.split("Hello, world", null)).isNull();
assertThat(StringUtils.split(null, null)).isNull();
}
}