forked from apache/commons-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtilsTest.java
More file actions
3297 lines (2866 loc) · 165 KB
/
Copy pathStringUtilsTest.java
File metadata and controls
3297 lines (2866 loc) · 165 KB
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.commons.lang3;
import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.lang3.function.Suppliers;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.text.WordUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.junitpioneer.jupiter.DefaultLocale;
import org.junitpioneer.jupiter.ReadsDefaultLocale;
import org.junitpioneer.jupiter.WritesDefaultLocale;
/**
* Tests for methods of {@link StringUtils}
* which been moved to their own test classes.
*/
@SuppressWarnings("deprecation") // deliberate use of deprecated code
class StringUtilsTest extends AbstractLangTest {
static final String WHITESPACE;
static final String NON_WHITESPACE;
static final String HARD_SPACE;
static final String TRIMMABLE;
static final String NON_TRIMMABLE;
static {
final StringBuilder ws = new StringBuilder();
final StringBuilder nws = new StringBuilder();
final String hs = String.valueOf((char) 160);
final StringBuilder tr = new StringBuilder();
final StringBuilder ntr = new StringBuilder();
for (int i = 0; i < Character.MAX_VALUE; i++) {
if (Character.isWhitespace((char) i)) {
ws.append((char) i);
if (i > 32) {
ntr.append((char) i);
}
} else if (i < 40) {
nws.append((char) i);
}
}
for (int i = 0; i <= 32; i++) {
tr.append((char) i);
}
WHITESPACE = ws.toString();
NON_WHITESPACE = nws.toString();
HARD_SPACE = hs;
TRIMMABLE = tr.toString();
NON_TRIMMABLE = ntr.toString();
}
private static final String[] ARRAY_LIST = {"foo", "bar", "baz"};
private static final String[] EMPTY_ARRAY_LIST = {};
private static final String[] NULL_ARRAY_LIST = {null};
private static final Object[] NULL_TO_STRING_LIST = {
new Object() {
@Override
public String toString() {
return null;
}
}
};
private static final String[] MIXED_ARRAY_LIST = {null, "", "foo"};
private static final Object[] MIXED_TYPE_LIST = {"foo", Long.valueOf(2L)};
private static final long[] LONG_PRIM_LIST = {1, 2};
private static final int[] INT_PRIM_LIST = {1, 2};
private static final byte[] BYTE_PRIM_LIST = {1, 2};
private static final short[] SHORT_PRIM_LIST = {1, 2};
private static final char[] CHAR_PRIM_LIST = {'1', '2'};
private static final float[] FLOAT_PRIM_LIST = {1, 2};
private static final double[] DOUBLE_PRIM_LIST = {1, 2};
private static final List<String> MIXED_STRING_LIST = Arrays.asList(null, "", "foo");
private static final List<Object> MIXED_TYPE_OBJECT_LIST = Arrays.<Object>asList("foo", Long.valueOf(2L));
private static final List<String> STRING_LIST = Arrays.asList("foo", "bar", "baz");
private static final List<String> EMPTY_STRING_LIST = Collections.emptyList();
private static final List<String> NULL_STRING_LIST = Collections.singletonList(null);
private static final String SEPARATOR = ",";
private static final char SEPARATOR_CHAR = ';';
private static final char COMMA_SEPARATOR_CHAR = ',';
private static final String TEXT_LIST = "foo,bar,baz";
private static final String TEXT_LIST_CHAR = "foo;bar;baz";
private static final String TEXT_LIST_NOSEP = "foobarbaz";
private static final String FOO_UNCAP = "foo";
private static final String FOO_CAP = "Foo";
private static final String SENTENCE_UNCAP = "foo bar baz";
private static final String SENTENCE_CAP = "Foo Bar Baz";
private static final boolean[] EMPTY = {};
private static final boolean[] ARRAY_FALSE_FALSE = {false, false};
private static final boolean[] ARRAY_FALSE_TRUE = {false, true};
private static final boolean[] ARRAY_FALSE_TRUE_FALSE = {false, true, false};
private void innerTestSplit(final char separator, final String sepStr, final char noMatch) {
final String msg = "Failed on separator hex(" + Integer.toHexString(separator) +
"), noMatch hex(" + Integer.toHexString(noMatch) + "), sepStr(" + sepStr + ")";
final String str = "a" + separator + "b" + separator + separator + noMatch + "c";
String[] res;
// (str, sepStr)
res = StringUtils.split(str, sepStr);
assertEquals(3, res.length, msg);
assertEquals("a", res[0]);
assertEquals("b", res[1]);
assertEquals(noMatch + "c", res[2]);
final String str2 = separator + "a" + separator;
res = StringUtils.split(str2, sepStr);
assertEquals(1, res.length, msg);
assertEquals("a", res[0], msg);
res = StringUtils.split(str, sepStr, -1);
assertEquals(3, res.length, msg);
assertEquals("a", res[0], msg);
assertEquals("b", res[1], msg);
assertEquals(noMatch + "c", res[2], msg);
res = StringUtils.split(str, sepStr, 0);
assertEquals(3, res.length, msg);
assertEquals("a", res[0], msg);
assertEquals("b", res[1], msg);
assertEquals(noMatch + "c", res[2], msg);
res = StringUtils.split(str, sepStr, 1);
assertEquals(1, res.length, msg);
assertEquals(str, res[0], msg);
res = StringUtils.split(str, sepStr, 2);
assertEquals(2, res.length, msg);
assertEquals("a", res[0], msg);
assertEquals(str.substring(2), res[1], msg);
}
private void innerTestSplitPreserveAllTokens(final char separator, final String sepStr, final char noMatch) {
final String msg = "Failed on separator hex(" + Integer.toHexString(separator) +
"), noMatch hex(" + Integer.toHexString(noMatch) + "), sepStr(" + sepStr + ")";
final String str = "a" + separator + "b" + separator + separator + noMatch + "c";
String[] res;
// (str, sepStr)
res = StringUtils.splitPreserveAllTokens(str, sepStr);
assertEquals(4, res.length, msg);
assertEquals("a", res[0], msg);
assertEquals("b", res[1], msg);
assertEquals("", res[2], msg);
assertEquals(noMatch + "c", res[3], msg);
final String str2 = separator + "a" + separator;
res = StringUtils.splitPreserveAllTokens(str2, sepStr);
assertEquals(3, res.length, msg);
assertEquals("", res[0], msg);
assertEquals("a", res[1], msg);
assertEquals("", res[2], msg);
res = StringUtils.splitPreserveAllTokens(str, sepStr, -1);
assertEquals(4, res.length, msg);
assertEquals("a", res[0], msg);
assertEquals("b", res[1], msg);
assertEquals("", res[2], msg);
assertEquals(noMatch + "c", res[3], msg);
res = StringUtils.splitPreserveAllTokens(str, sepStr, 0);
assertEquals(4, res.length, msg);
assertEquals("a", res[0], msg);
assertEquals("b", res[1], msg);
assertEquals("", res[2], msg);
assertEquals(noMatch + "c", res[3], msg);
res = StringUtils.splitPreserveAllTokens(str, sepStr, 1);
assertEquals(1, res.length, msg);
assertEquals(str, res[0], msg);
res = StringUtils.splitPreserveAllTokens(str, sepStr, 2);
assertEquals(2, res.length, msg);
assertEquals("a", res[0], msg);
assertEquals(str.substring(2), res[1], msg);
}
/**
* Tests {@code appendIfMissing}.
*/
@Test
void testAppendIfMissing() {
assertNull(StringUtils.appendIfMissing(null, null), "appendIfMissing(null,null)");
assertEquals("abc", StringUtils.appendIfMissing("abc", null), "appendIfMissing(abc,null)");
assertEquals("xyz", StringUtils.appendIfMissing("", "xyz"), "appendIfMissing(\"\",xyz)");
assertEquals("abcxyz", StringUtils.appendIfMissing("abc", "xyz"), "appendIfMissing(abc,xyz)");
assertEquals("abcxyz", StringUtils.appendIfMissing("abcxyz", "xyz"), "appendIfMissing(abcxyz,xyz)");
assertEquals("aXYZxyz", StringUtils.appendIfMissing("aXYZ", "xyz"), "appendIfMissing(aXYZ,xyz)");
assertNull(StringUtils.appendIfMissing(null, null, (CharSequence[]) null), "appendIfMissing(null,null,null)");
assertEquals("abc", StringUtils.appendIfMissing("abc", null, (CharSequence[]) null), "appendIfMissing(abc,null,null)");
assertEquals("xyz", StringUtils.appendIfMissing("", "xyz", (CharSequence[]) null), "appendIfMissing(\"\",xyz,null))");
assertEquals("abcxyz", StringUtils.appendIfMissing("abc", "xyz", (CharSequence) null), "appendIfMissing(abc,xyz,{null})");
assertEquals("abc", StringUtils.appendIfMissing("abc", "xyz", ""), "appendIfMissing(abc,xyz,\"\")");
assertEquals("abcxyz", StringUtils.appendIfMissing("abc", "xyz", "mno"), "appendIfMissing(abc,xyz,mno)");
assertEquals("abcxyz", StringUtils.appendIfMissing("abcxyz", "xyz", "mno"), "appendIfMissing(abcxyz,xyz,mno)");
assertEquals("abcmno", StringUtils.appendIfMissing("abcmno", "xyz", "mno"), "appendIfMissing(abcmno,xyz,mno)");
assertEquals("abcXYZxyz", StringUtils.appendIfMissing("abcXYZ", "xyz", "mno"), "appendIfMissing(abcXYZ,xyz,mno)");
assertEquals("abcMNOxyz", StringUtils.appendIfMissing("abcMNO", "xyz", "mno"), "appendIfMissing(abcMNO,xyz,mno)");
}
/**
* Tests {@code appendIfMissingIgnoreCase}.
*/
@Test
void testAppendIfMissingIgnoreCase() {
assertNull(StringUtils.appendIfMissingIgnoreCase(null, null), "appendIfMissingIgnoreCase(null,null)");
assertEquals("abc", StringUtils.appendIfMissingIgnoreCase("abc", null), "appendIfMissingIgnoreCase(abc,null)");
assertEquals("xyz", StringUtils.appendIfMissingIgnoreCase("", "xyz"), "appendIfMissingIgnoreCase(\"\",xyz)");
assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abc", "xyz"), "appendIfMissingIgnoreCase(abc,xyz)");
assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abcxyz", "xyz"), "appendIfMissingIgnoreCase(abcxyz,xyz)");
assertEquals("abcXYZ", StringUtils.appendIfMissingIgnoreCase("abcXYZ", "xyz"), "appendIfMissingIgnoreCase(abcXYZ,xyz)");
assertNull(StringUtils.appendIfMissingIgnoreCase(null, null, (CharSequence[]) null), "appendIfMissingIgnoreCase(null,null,null)");
assertEquals("abc", StringUtils.appendIfMissingIgnoreCase("abc", null, (CharSequence[]) null), "appendIfMissingIgnoreCase(abc,null,null)");
assertEquals("xyz", StringUtils.appendIfMissingIgnoreCase("", "xyz", (CharSequence[]) null), "appendIfMissingIgnoreCase(\"\",xyz,null)");
assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abc", "xyz", (CharSequence) null), "appendIfMissingIgnoreCase(abc,xyz,{null})");
assertEquals("abc", StringUtils.appendIfMissingIgnoreCase("abc", "xyz", ""), "appendIfMissingIgnoreCase(abc,xyz,\"\")");
assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abc", "xyz", "mno"), "appendIfMissingIgnoreCase(abc,xyz,mno)");
assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abcxyz", "xyz", "mno"), "appendIfMissingIgnoreCase(abcxyz,xyz,mno)");
assertEquals("abcmno", StringUtils.appendIfMissingIgnoreCase("abcmno", "xyz", "mno"), "appendIfMissingIgnoreCase(abcmno,xyz,mno)");
assertEquals("abcXYZ", StringUtils.appendIfMissingIgnoreCase("abcXYZ", "xyz", "mno"), "appendIfMissingIgnoreCase(abcXYZ,xyz,mno)");
assertEquals("abcMNO", StringUtils.appendIfMissingIgnoreCase("abcMNO", "xyz", "mno"), "appendIfMissingIgnoreCase(abcMNO,xyz,mno)");
}
@Test
void testCapitalize() {
assertNull(StringUtils.capitalize(null));
assertEquals("", StringUtils.capitalize(""), "capitalize(empty-string) failed");
assertEquals("X", StringUtils.capitalize("x"), "capitalize(single-char-string) failed");
assertEquals(FOO_CAP, StringUtils.capitalize(FOO_CAP), "capitalize(String) failed");
assertEquals(FOO_CAP, StringUtils.capitalize(FOO_UNCAP), "capitalize(string) failed");
assertEquals("\u01C8", StringUtils.capitalize("\u01C9"), "capitalize(String) is not using TitleCase");
// Javadoc examples
assertNull(StringUtils.capitalize(null));
assertEquals("", StringUtils.capitalize(""));
assertEquals("Cat", StringUtils.capitalize("cat"));
assertEquals("CAt", StringUtils.capitalize("cAt"));
assertEquals("'cat'", StringUtils.capitalize("'cat'"));
}
@Test
void testCenter_StringInt() {
assertNull(StringUtils.center(null, -1));
assertNull(StringUtils.center(null, 4));
assertEquals(" ", StringUtils.center("", 4));
assertEquals("ab", StringUtils.center("ab", 0));
assertEquals("ab", StringUtils.center("ab", -1));
assertEquals("ab", StringUtils.center("ab", 1));
assertEquals(" ", StringUtils.center("", 4));
assertEquals(" ab ", StringUtils.center("ab", 4));
assertEquals("abcd", StringUtils.center("abcd", 2));
assertEquals(" a ", StringUtils.center("a", 4));
assertEquals(" a ", StringUtils.center("a", 5));
}
@Test
void testCenter_StringIntChar() {
assertNull(StringUtils.center(null, -1, ' '));
assertNull(StringUtils.center(null, 4, ' '));
assertEquals(" ", StringUtils.center("", 4, ' '));
assertEquals("ab", StringUtils.center("ab", 0, ' '));
assertEquals("ab", StringUtils.center("ab", -1, ' '));
assertEquals("ab", StringUtils.center("ab", 1, ' '));
assertEquals(" ", StringUtils.center("", 4, ' '));
assertEquals(" ab ", StringUtils.center("ab", 4, ' '));
assertEquals("abcd", StringUtils.center("abcd", 2, ' '));
assertEquals(" a ", StringUtils.center("a", 4, ' '));
assertEquals(" a ", StringUtils.center("a", 5, ' '));
assertEquals("xxaxx", StringUtils.center("a", 5, 'x'));
}
@Test
void testCenter_StringIntString() {
assertNull(StringUtils.center(null, 4, null));
assertNull(StringUtils.center(null, -1, " "));
assertNull(StringUtils.center(null, 4, " "));
assertEquals(" ", StringUtils.center("", 4, " "));
assertEquals("ab", StringUtils.center("ab", 0, " "));
assertEquals("ab", StringUtils.center("ab", -1, " "));
assertEquals("ab", StringUtils.center("ab", 1, " "));
assertEquals(" ", StringUtils.center("", 4, " "));
assertEquals(" ab ", StringUtils.center("ab", 4, " "));
assertEquals("abcd", StringUtils.center("abcd", 2, " "));
assertEquals(" a ", StringUtils.center("a", 4, " "));
assertEquals("yayz", StringUtils.center("a", 4, "yz"));
assertEquals("yzyayzy", StringUtils.center("a", 7, "yz"));
assertEquals(" abc ", StringUtils.center("abc", 7, null));
assertEquals(" abc ", StringUtils.center("abc", 7, ""));
}
@Test
void testChomp() {
final String[][] chompCases = {
{FOO_UNCAP + "\r\n", FOO_UNCAP},
{FOO_UNCAP + "\n", FOO_UNCAP},
{FOO_UNCAP + "\r", FOO_UNCAP},
{FOO_UNCAP + " \r", FOO_UNCAP + " "},
{FOO_UNCAP, FOO_UNCAP},
{FOO_UNCAP + "\n\n", FOO_UNCAP + "\n"},
{FOO_UNCAP + "\r\n\r\n", FOO_UNCAP + "\r\n"},
{"foo\nfoo", "foo\nfoo"},
{"foo\n\rfoo", "foo\n\rfoo"},
{"\n", ""},
{"\r", ""},
{"a", "a"},
{"\r\n", ""},
{"", ""},
{null, null},
{FOO_UNCAP + "\n\r", FOO_UNCAP + "\n"}
};
for (final String[] chompCase : chompCases) {
final String original = chompCase[0];
final String expectedResult = chompCase[1];
assertEquals(expectedResult, StringUtils.chomp(original), "chomp(String) failed");
}
assertEquals("foo", StringUtils.chomp("foobar", "bar"), "chomp(String, String) failed");
assertEquals("foobar", StringUtils.chomp("foobar", "baz"), "chomp(String, String) failed");
assertEquals("foo", StringUtils.chomp("foo", "foooo"), "chomp(String, String) failed");
assertEquals("foobar", StringUtils.chomp("foobar", ""), "chomp(String, String) failed");
assertEquals("foobar", StringUtils.chomp("foobar", null), "chomp(String, String) failed");
assertEquals("", StringUtils.chomp("", "foo"), "chomp(String, String) failed");
assertEquals("", StringUtils.chomp("", null), "chomp(String, String) failed");
assertEquals("", StringUtils.chomp("", ""), "chomp(String, String) failed");
assertNull(StringUtils.chomp(null, "foo"), "chomp(String, String) failed");
assertNull(StringUtils.chomp(null, null), "chomp(String, String) failed");
assertNull(StringUtils.chomp(null, ""), "chomp(String, String) failed");
assertEquals("", StringUtils.chomp("foo", "foo"), "chomp(String, String) failed");
assertEquals(" ", StringUtils.chomp(" foo", "foo"), "chomp(String, String) failed");
assertEquals("foo ", StringUtils.chomp("foo ", "foo"), "chomp(String, String) failed");
}
@Test
void testChop() {
final String[][] chopCases = {
{FOO_UNCAP + "\r\n", FOO_UNCAP},
{FOO_UNCAP + "\n", FOO_UNCAP},
{FOO_UNCAP + "\r", FOO_UNCAP},
{FOO_UNCAP + " \r", FOO_UNCAP + " "},
{"foo", "fo"},
{"foo\nfoo", "foo\nfo"},
{"\n", ""},
{"\r", ""},
{"\r\n", ""},
{null, null},
{"", ""},
{"a", ""},
};
for (final String[] chopCase : chopCases) {
final String original = chopCase[0];
final String expectedResult = chopCase[1];
assertEquals(expectedResult, StringUtils.chop(original), "chop(String) failed");
}
}
@Test
void testConstructor() {
assertNotNull(new StringUtils());
final Constructor<?>[] cons = StringUtils.class.getDeclaredConstructors();
assertEquals(1, cons.length);
assertTrue(Modifier.isPublic(cons[0].getModifiers()));
assertTrue(Modifier.isPublic(StringUtils.class.getModifiers()));
assertFalse(Modifier.isFinal(StringUtils.class.getModifiers()));
}
@Test
void testDefault_String() {
assertEquals("", StringUtils.defaultString(null));
assertEquals("", StringUtils.defaultString(""));
assertEquals("abc", StringUtils.defaultString("abc"));
}
@Test
void testDefault_StringString() {
assertEquals("NULL", StringUtils.defaultString(null, "NULL"));
assertEquals("", StringUtils.defaultString("", "NULL"));
assertEquals("abc", StringUtils.defaultString("abc", "NULL"));
}
@Test
void testDefaultIfBlank_CharBuffers() {
assertEquals("NULL", StringUtils.defaultIfBlank(CharBuffer.wrap(""), CharBuffer.wrap("NULL")).toString());
assertEquals("NULL", StringUtils.defaultIfBlank(CharBuffer.wrap(" "), CharBuffer.wrap("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfBlank(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL")).toString());
assertNull(StringUtils.defaultIfBlank(CharBuffer.wrap(""), (CharBuffer) null));
// Tests compatibility for the API return type
final CharBuffer s = StringUtils.defaultIfBlank(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL"));
assertEquals("abc", s.toString());
}
@Test
void testDefaultIfBlank_StringBuffers() {
assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuffer(""), new StringBuffer("NULL")).toString());
assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuffer(" "), new StringBuffer("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfBlank(new StringBuffer("abc"), new StringBuffer("NULL")).toString());
assertNull(StringUtils.defaultIfBlank(new StringBuffer(""), (StringBuffer) null));
// Tests compatibility for the API return type
final StringBuffer s = StringUtils.defaultIfBlank(new StringBuffer("abc"), new StringBuffer("NULL"));
assertEquals("abc", s.toString());
}
@Test
void testDefaultIfBlank_StringBuilders() {
assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuilder(""), new StringBuilder("NULL")).toString());
assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuilder(" "), new StringBuilder("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfBlank(new StringBuilder("abc"), new StringBuilder("NULL")).toString());
assertNull(StringUtils.defaultIfBlank(new StringBuilder(""), (StringBuilder) null));
// Tests compatibility for the API return type
final StringBuilder s = StringUtils.defaultIfBlank(new StringBuilder("abc"), new StringBuilder("NULL"));
assertEquals("abc", s.toString());
}
@Test
void testDefaultIfBlank_StringString() {
assertEquals("NULL", StringUtils.defaultIfBlank(null, "NULL"));
assertEquals("NULL", StringUtils.defaultIfBlank("", "NULL"));
assertEquals("NULL", StringUtils.defaultIfBlank(" ", "NULL"));
assertEquals("abc", StringUtils.defaultIfBlank("abc", "NULL"));
assertNull(StringUtils.defaultIfBlank("", (String) null));
// Tests compatibility for the API return type
final String s = StringUtils.defaultIfBlank("abc", "NULL");
assertEquals("abc", s);
}
@Test
void testDefaultIfEmpty_CharBuffers() {
assertEquals("NULL", StringUtils.defaultIfEmpty(CharBuffer.wrap(""), CharBuffer.wrap("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfEmpty(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL")).toString());
assertNull(StringUtils.defaultIfEmpty(CharBuffer.wrap(""), (CharBuffer) null));
// Tests compatibility for the API return type
final CharBuffer s = StringUtils.defaultIfEmpty(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL"));
assertEquals("abc", s.toString());
}
@Test
void testDefaultIfEmpty_StringBuffers() {
assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuffer(""), new StringBuffer("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuffer("abc"), new StringBuffer("NULL")).toString());
assertNull(StringUtils.defaultIfEmpty(new StringBuffer(""), (StringBuffer) null));
// Tests compatibility for the API return type
final StringBuffer s = StringUtils.defaultIfEmpty(new StringBuffer("abc"), new StringBuffer("NULL"));
assertEquals("abc", s.toString());
}
@Test
void testDefaultIfEmpty_StringBuilders() {
assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuilder(""), new StringBuilder("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuilder("abc"), new StringBuilder("NULL")).toString());
assertNull(StringUtils.defaultIfEmpty(new StringBuilder(""), (StringBuilder) null));
// Tests compatibility for the API return type
final StringBuilder s = StringUtils.defaultIfEmpty(new StringBuilder("abc"), new StringBuilder("NULL"));
assertEquals("abc", s.toString());
}
@Test
void testDefaultIfEmpty_StringString() {
assertEquals("NULL", StringUtils.defaultIfEmpty(null, "NULL"));
assertEquals("NULL", StringUtils.defaultIfEmpty("", "NULL"));
assertEquals("abc", StringUtils.defaultIfEmpty("abc", "NULL"));
assertNull(StringUtils.getIfEmpty("", null));
// Tests compatibility for the API return type
final String s = StringUtils.defaultIfEmpty("abc", "NULL");
assertEquals("abc", s);
}
@Test
void testDeleteWhitespace_String() {
assertNull(StringUtils.deleteWhitespace(null));
assertEquals("", StringUtils.deleteWhitespace(""));
assertEquals("", StringUtils.deleteWhitespace(" \u000C \t\t\u001F\n\n \u000B "));
assertEquals("", StringUtils.deleteWhitespace(StringUtilsTest.WHITESPACE));
assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils.deleteWhitespace(StringUtilsTest.NON_WHITESPACE));
// Note: u-2007 and u-000A both cause problems in the source code
// it should ignore 2007 but delete 000A
assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace(" \u00A0 \t\t\n\n \u202F "));
assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace("\u00A0\u202F"));
assertEquals("test", StringUtils.deleteWhitespace("\u000Bt \t\n\u0009e\rs\n\n \tt"));
}
@Test
void testDifference_StringString() {
assertNull(StringUtils.difference(null, null));
assertEquals("", StringUtils.difference("", ""));
assertEquals("abc", StringUtils.difference("", "abc"));
assertEquals("", StringUtils.difference("abc", ""));
assertEquals("i am a robot", StringUtils.difference(null, "i am a robot"));
assertEquals("i am a machine", StringUtils.difference("i am a machine", null));
assertEquals("robot", StringUtils.difference("i am a machine", "i am a robot"));
assertEquals("", StringUtils.difference("abc", "abc"));
assertEquals("you are a robot", StringUtils.difference("i am a robot", "you are a robot"));
// 0x10400 and 0x10401 share the same high surrogate; the difference must not begin with a lone low surrogate
final String cp10400 = new String(Character.toChars(0x10400));
final String cp10401 = new String(Character.toChars(0x10401));
assertEquals(cp10401, StringUtils.difference(cp10400, cp10401));
assertEquals("Y", StringUtils.difference(cp10400 + "X", cp10400 + "Y"));
}
@Test
void testDifferenceAt_StringArray() {
assertEquals(-1, StringUtils.indexOfDifference((String[]) null));
assertEquals(-1, StringUtils.indexOfDifference());
assertEquals(-1, StringUtils.indexOfDifference("abc"));
assertEquals(-1, StringUtils.indexOfDifference(null, null));
assertEquals(-1, StringUtils.indexOfDifference("", ""));
assertEquals(0, StringUtils.indexOfDifference("", null));
assertEquals(0, StringUtils.indexOfDifference("abc", null, null));
assertEquals(0, StringUtils.indexOfDifference(null, null, "abc"));
assertEquals(0, StringUtils.indexOfDifference("", "abc"));
assertEquals(0, StringUtils.indexOfDifference("abc", ""));
assertEquals(-1, StringUtils.indexOfDifference("abc", "abc"));
assertEquals(1, StringUtils.indexOfDifference("abc", "a"));
assertEquals(2, StringUtils.indexOfDifference("ab", "abxyz"));
assertEquals(2, StringUtils.indexOfDifference("abcde", "abxyz"));
assertEquals(0, StringUtils.indexOfDifference("abcde", "xyz"));
assertEquals(0, StringUtils.indexOfDifference("xyz", "abcde"));
assertEquals(7, StringUtils.indexOfDifference("i am a machine", "i am a robot"));
// a difference that falls inside a shared surrogate pair is reported at the start of the pair, not mid-pair
final String cp10400 = new String(Character.toChars(0x10400));
final String cp10401 = new String(Character.toChars(0x10401));
assertEquals(0, StringUtils.indexOfDifference(new String[] {cp10400, cp10401}));
assertEquals(2, StringUtils.indexOfDifference(new String[] {cp10400 + "X", cp10400 + "Y"}));
}
@Test
void testDifferenceAt_StringString() {
assertEquals(-1, StringUtils.indexOfDifference(null, null));
assertEquals(0, StringUtils.indexOfDifference(null, "i am a robot"));
assertEquals(-1, StringUtils.indexOfDifference("", ""));
assertEquals(0, StringUtils.indexOfDifference("", "abc"));
assertEquals(0, StringUtils.indexOfDifference("abc", ""));
assertEquals(0, StringUtils.indexOfDifference("i am a machine", null));
assertEquals(7, StringUtils.indexOfDifference("i am a machine", "i am a robot"));
assertEquals(-1, StringUtils.indexOfDifference("foo", "foo"));
assertEquals(0, StringUtils.indexOfDifference("i am a robot", "you are a robot"));
// a difference that falls inside a shared surrogate pair is reported at the start of the pair, not mid-pair
final String cp10400 = new String(Character.toChars(0x10400));
final String cp10401 = new String(Character.toChars(0x10401));
assertEquals(0, StringUtils.indexOfDifference(cp10400, cp10401));
assertEquals(2, StringUtils.indexOfDifference(cp10400 + "X", cp10400 + "Y"));
}
/**
* A sanity check for {@link StringUtils#EMPTY}.
*/
@Test
void testEMPTY() {
assertNotNull(StringUtils.EMPTY);
assertEquals("", StringUtils.EMPTY);
assertEquals(0, StringUtils.EMPTY.length());
}
@Test
void testEscapeSurrogatePairs() {
assertEquals("\uD83D\uDE30", StringEscapeUtils.escapeCsv("\uD83D\uDE30"));
// Examples from https://en.wikipedia.org/wiki/UTF-16
assertEquals("\uD800\uDC00", StringEscapeUtils.escapeCsv("\uD800\uDC00"));
assertEquals("\uD834\uDD1E", StringEscapeUtils.escapeCsv("\uD834\uDD1E"));
assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeCsv("\uDBFF\uDFFD"));
assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeHtml3("\uDBFF\uDFFD"));
assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeHtml4("\uDBFF\uDFFD"));
assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeXml("\uDBFF\uDFFD"));
}
/**
* Tests LANG-858.
*/
@Test
void testEscapeSurrogatePairsLang858() {
assertEquals("\\uDBFF\\uDFFD", StringEscapeUtils.escapeJava("\uDBFF\uDFFD")); //fail LANG-858
assertEquals("\\uDBFF\\uDFFD", StringEscapeUtils.escapeEcmaScript("\uDBFF\uDFFD")); //fail LANG-858
}
@Test
void testGeorgianSample() {
final char[] arrayI = {
//Latin Small Letter dotless I
(char) 0x0131,
//Greek Capital Letter Theta
(char) 0x03F4
};
final char[] arrayJ = {
//Latin Capital Letter I with dot above
(char) 0x0130,
//Greek Theta Symbol
(char) 0x03D1
};
for (final char i : arrayI) {
for (final char j : arrayJ) {
final String si = String.valueOf(i);
final String sj = String.valueOf(j);
final boolean res1 = si.equalsIgnoreCase(sj);
final CharSequence ci = new StringBuilder(si);
final CharSequence cj = new StringBuilder(sj);
boolean res2 = StringUtils.startsWithIgnoreCase(ci, cj);
assertEquals(res1, res2, "si : " + si + " sj : " + sj);
res2 = StringUtils.endsWithIgnoreCase(ci, cj);
assertEquals(res1, res2, "si : " + si + " sj : " + sj);
res2 = StringUtils.compareIgnoreCase(ci.toString(), cj.toString()) == 0;
assertEquals(res1, res2, "si : " + si + " sj : " + sj);
res2 = StringUtils.indexOfIgnoreCase(ci.toString(), cj.toString()) == 0;
assertEquals(res1, res2, "si : " + si + " sj : " + sj);
res2 = StringUtils.lastIndexOfIgnoreCase(ci.toString(), cj.toString()) == 0;
assertEquals(res1, res2, "si : " + si + " sj : " + sj);
}
}
}
@Test
void testGetBytes_Charset() {
assertEquals(ArrayUtils.EMPTY_BYTE_ARRAY, StringUtils.getBytes(null, (Charset) null));
assertArrayEquals(StringUtils.EMPTY.getBytes(), StringUtils.getBytes(StringUtils.EMPTY, (Charset) null));
assertArrayEquals(StringUtils.EMPTY.getBytes(StandardCharsets.US_ASCII),
StringUtils.getBytes(StringUtils.EMPTY, StandardCharsets.US_ASCII));
}
@Test
void testGetBytes_String() throws UnsupportedEncodingException {
assertEquals(ArrayUtils.EMPTY_BYTE_ARRAY, StringUtils.getBytes(null, (String) null));
assertArrayEquals(StringUtils.EMPTY.getBytes(), StringUtils.getBytes(StringUtils.EMPTY, (String) null));
assertArrayEquals(StringUtils.EMPTY.getBytes(StandardCharsets.US_ASCII.name()),
StringUtils.getBytes(StringUtils.EMPTY, StandardCharsets.US_ASCII.name()));
}
@Test
void testGetCommonPrefix_StringArray() {
assertEquals("", StringUtils.getCommonPrefix((String[]) null));
assertEquals("", StringUtils.getCommonPrefix());
assertEquals("abc", StringUtils.getCommonPrefix("abc"));
assertEquals("", StringUtils.getCommonPrefix(null, null));
assertEquals("", StringUtils.getCommonPrefix("", ""));
assertEquals("", StringUtils.getCommonPrefix("", null));
assertEquals("", StringUtils.getCommonPrefix("abc", null, null));
assertEquals("", StringUtils.getCommonPrefix(null, null, "abc"));
assertEquals("", StringUtils.getCommonPrefix("", "abc"));
assertEquals("", StringUtils.getCommonPrefix("abc", ""));
assertEquals("abc", StringUtils.getCommonPrefix("abc", "abc"));
assertEquals("a", StringUtils.getCommonPrefix("abc", "a"));
assertEquals("ab", StringUtils.getCommonPrefix("ab", "abxyz"));
assertEquals("ab", StringUtils.getCommonPrefix("abcde", "abxyz"));
assertEquals("", StringUtils.getCommonPrefix("abcde", "xyz"));
assertEquals("", StringUtils.getCommonPrefix("xyz", "abcde"));
assertEquals("i am a ", StringUtils.getCommonPrefix("i am a machine", "i am a robot"));
// 0x10400 and 0x10401 share the high surrogate but differ; the common prefix must not be a lone high surrogate
final String cp10400 = new String(Character.toChars(0x10400));
final String cp10401 = new String(Character.toChars(0x10401));
assertEquals("", StringUtils.getCommonPrefix(cp10400, cp10401));
assertEquals(cp10400, StringUtils.getCommonPrefix(cp10400 + "X", cp10400 + "Y"));
}
@Test
void testGetDigits() {
assertNull(StringUtils.getDigits(null));
assertEquals("", StringUtils.getDigits(""));
assertEquals("", StringUtils.getDigits("abc"));
assertEquals("1000", StringUtils.getDigits("1000$"));
assertEquals("12345", StringUtils.getDigits("123password45"));
assertEquals("5417543010", StringUtils.getDigits("(541) 754-3010"));
assertEquals("\u0967\u0968\u0969", StringUtils.getDigits("\u0967\u0968\u0969"));
}
@Test
void testGetDigitsKeycaps() {
assertEquals("0123456789", StringUtils.getDigits("0️⃣1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣#️⃣"));
}
@Test
void testGetDigitsSupplementary() {
// U+1D7CF MATHEMATICAL BOLD DIGIT ONE: Character.isDigit is true but it is a surrogate pair
final String mathOne = new String(Character.toChars(0x1D7CF));
assertEquals(mathOne, StringUtils.getDigits(mathOne));
assertEquals(mathOne + "9", StringUtils.getDigits("a" + mathOne + "9"));
}
@Test
void testGetFuzzyDistance() {
assertEquals(0, StringUtils.getFuzzyDistance("", "", Locale.ENGLISH));
assertEquals(0, StringUtils.getFuzzyDistance("Workshop", "b", Locale.ENGLISH));
assertEquals(1, StringUtils.getFuzzyDistance("Room", "o", Locale.ENGLISH));
assertEquals(1, StringUtils.getFuzzyDistance("Workshop", "w", Locale.ENGLISH));
assertEquals(2, StringUtils.getFuzzyDistance("Workshop", "ws", Locale.ENGLISH));
assertEquals(4, StringUtils.getFuzzyDistance("Workshop", "wo", Locale.ENGLISH));
assertEquals(3, StringUtils.getFuzzyDistance("Apache Software Foundation", "asf", Locale.ENGLISH));
}
@Test
void testGetFuzzyDistance_NullNullNull() {
assertIllegalArgumentException(() -> StringUtils.getFuzzyDistance(null, null, null));
}
@Test
void testGetFuzzyDistance_NullStringLocale() {
assertIllegalArgumentException(() -> StringUtils.getFuzzyDistance(null, "clear", Locale.ENGLISH));
}
@Test
void testGetFuzzyDistance_StringNullLoclae() {
assertIllegalArgumentException(() -> StringUtils.getFuzzyDistance(" ", null, Locale.ENGLISH));
}
@Test
void testGetFuzzyDistance_StringStringNull() {
assertIllegalArgumentException(() -> StringUtils.getFuzzyDistance(" ", "clear", null));
}
@Test
void testGetIfBlank_StringStringSupplier() {
assertEquals("NULL", StringUtils.getIfBlank(null, () -> "NULL"));
assertEquals("NULL", StringUtils.getIfBlank("", () -> "NULL"));
assertEquals("NULL", StringUtils.getIfBlank(" ", () -> "NULL"));
assertEquals("abc", StringUtils.getIfBlank("abc", () -> "NULL"));
assertNull(StringUtils.getIfBlank("", Suppliers.nul()));
assertNull(StringUtils.defaultIfBlank("", (String) null));
// Tests compatibility for the API return type
final String s = StringUtils.getIfBlank("abc", () -> "NULL");
assertEquals("abc", s);
//Checking that default value supplied only on demand
final MutableInt numberOfCalls = new MutableInt(0);
final Supplier<String> countingDefaultSupplier = () -> {
numberOfCalls.increment();
return "NULL";
};
StringUtils.getIfBlank("abc", countingDefaultSupplier);
assertEquals(0, numberOfCalls.get());
StringUtils.getIfBlank("", countingDefaultSupplier);
assertEquals(1, numberOfCalls.get());
StringUtils.getIfBlank(" ", countingDefaultSupplier);
assertEquals(2, numberOfCalls.get());
StringUtils.getIfBlank(null, countingDefaultSupplier);
assertEquals(3, numberOfCalls.get());
}
@Test
void testGetIfEmpty_StringStringSupplier() {
assertEquals("NULL", StringUtils.getIfEmpty((String) null, () -> "NULL"));
assertEquals("NULL", StringUtils.getIfEmpty("", () -> "NULL"));
assertEquals("abc", StringUtils.getIfEmpty("abc", () -> "NULL"));
assertNull(StringUtils.getIfEmpty("", Suppliers.nul()));
assertNull(StringUtils.defaultIfEmpty("", (String) null));
// Tests compatibility for the API return type
final String s = StringUtils.getIfEmpty("abc", () -> "NULL");
assertEquals("abc", s);
//Checking that default value supplied only on demand
final MutableInt numberOfCalls = new MutableInt(0);
final Supplier<String> countingDefaultSupplier = () -> {
numberOfCalls.increment();
return "NULL";
};
StringUtils.getIfEmpty("abc", countingDefaultSupplier);
assertEquals(0, numberOfCalls.get());
StringUtils.getIfEmpty("", countingDefaultSupplier);
assertEquals(1, numberOfCalls.get());
StringUtils.getIfEmpty(null, countingDefaultSupplier);
assertEquals(2, numberOfCalls.get());
}
@Test
void testGetJaroWinklerDistance_NullNull() {
assertIllegalArgumentException(() -> StringUtils.getJaroWinklerDistance(null, null));
}
@Test
void testGetJaroWinklerDistance_NullString() {
assertIllegalArgumentException(() -> StringUtils.getJaroWinklerDistance(null, "clear"));
}
@Test
void testGetJaroWinklerDistance_StringNull() {
assertIllegalArgumentException(() -> StringUtils.getJaroWinklerDistance(" ", null));
}
@Test
void testGetJaroWinklerDistance_StringString() {
assertEquals(0.93d, StringUtils.getJaroWinklerDistance("frog", "fog"));
assertEquals(0.0d, StringUtils.getJaroWinklerDistance("fly", "ant"));
assertEquals(0.44d, StringUtils.getJaroWinklerDistance("elephant", "hippo"));
assertEquals(0.84d, StringUtils.getJaroWinklerDistance("dwayne", "duane"));
assertEquals(0.93d, StringUtils.getJaroWinklerDistance("ABC Corporation", "ABC Corp"));
assertEquals(0.95d, StringUtils.getJaroWinklerDistance("D N H Enterprises Inc", "D & H Enterprises, Inc."));
assertEquals(0.92d, StringUtils.getJaroWinklerDistance("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
assertEquals(0.88d, StringUtils.getJaroWinklerDistance("PENNSYLVANIA", "PENNCISYLVNIA"));
assertEquals(0.63d, StringUtils.getJaroWinklerDistance("Haus Ingeborg", "Ingeborg Esser"));
}
@Test
void testGetLevenshteinDistance_NullString() {
assertIllegalArgumentException(() -> StringUtils.getLevenshteinDistance("a", null));
}
@Test
void testGetLevenshteinDistance_NullStringInt() {
assertIllegalArgumentException(() -> StringUtils.getLevenshteinDistance(null, "a", 0));
}
@Test
void testGetLevenshteinDistance_StringNull() {
assertIllegalArgumentException(() -> StringUtils.getLevenshteinDistance(null, "a"));
}
@Test
void testGetLevenshteinDistance_StringNullInt() {
assertIllegalArgumentException(() -> StringUtils.getLevenshteinDistance("a", null, 0));
}
@Test
void testGetLevenshteinDistance_StringString() {
assertEquals(0, StringUtils.getLevenshteinDistance("", ""));
assertEquals(1, StringUtils.getLevenshteinDistance("", "a"));
assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", ""));
assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog"));
assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant"));
assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo"));
assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant"));
assertEquals(8, StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz"));
assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz", "hippo"));
assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo"));
}
@Test
void testGetLevenshteinDistance_StringStringInt() {
// empty strings
assertEquals(0, StringUtils.getLevenshteinDistance("", "", 0));
assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "", 8));
assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "", 7));
assertEquals(-1, StringUtils.getLevenshteinDistance("aaapppp", "", 6));
// unequal strings, zero threshold
assertEquals(-1, StringUtils.getLevenshteinDistance("b", "a", 0));
assertEquals(-1, StringUtils.getLevenshteinDistance("a", "b", 0));
// equal strings
assertEquals(0, StringUtils.getLevenshteinDistance("aa", "aa", 0));
assertEquals(0, StringUtils.getLevenshteinDistance("aa", "aa", 2));
// same length
assertEquals(-1, StringUtils.getLevenshteinDistance("aaa", "bbb", 2));
assertEquals(3, StringUtils.getLevenshteinDistance("aaa", "bbb", 3));
// big stripe
assertEquals(6, StringUtils.getLevenshteinDistance("aaaaaa", "b", 10));
// distance less than threshold
assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "b", 8));
assertEquals(3, StringUtils.getLevenshteinDistance("a", "bbb", 4));
// distance equal to threshold
assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "b", 7));
assertEquals(3, StringUtils.getLevenshteinDistance("a", "bbb", 3));
// distance greater than threshold
assertEquals(-1, StringUtils.getLevenshteinDistance("a", "bbb", 2));
assertEquals(-1, StringUtils.getLevenshteinDistance("bbb", "a", 2));
assertEquals(-1, StringUtils.getLevenshteinDistance("aaapppp", "b", 6));
// stripe runs off array, strings not similar
assertEquals(-1, StringUtils.getLevenshteinDistance("a", "bbb", 1));
assertEquals(-1, StringUtils.getLevenshteinDistance("bbb", "a", 1));
// stripe runs off array, strings are similar
assertEquals(-1, StringUtils.getLevenshteinDistance("12345", "1234567", 1));
assertEquals(-1, StringUtils.getLevenshteinDistance("1234567", "12345", 1));
// old getLevenshteinDistance test cases
assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog", 1));
assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant", 3));
assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo", 7));
assertEquals(-1, StringUtils.getLevenshteinDistance("elephant", "hippo", 6));
assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant", 7));
assertEquals(-1, StringUtils.getLevenshteinDistance("hippo", "elephant", 6));
assertEquals(8, StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz", 8));
assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz", "hippo", 8));
assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo", 1));
assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog", Integer.MAX_VALUE));
assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant", Integer.MAX_VALUE));
assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo", Integer.MAX_VALUE));
assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant", Integer.MAX_VALUE));
assertEquals(8, StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz", Integer.MAX_VALUE));
assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz", "hippo", Integer.MAX_VALUE));
assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo", Integer.MAX_VALUE));
}
@Test
void testGetLevenshteinDistance_StringStringNegativeInt() {
assertIllegalArgumentException(() -> StringUtils.getLevenshteinDistance("a", "a", -1));
}
/**
* Test for {@link StringUtils#isAllLowerCase(CharSequence)}.
*/
@Test
void testIsAllLowerCase() {
assertFalse(StringUtils.isAllLowerCase(null));
assertFalse(StringUtils.isAllLowerCase(StringUtils.EMPTY));
assertFalse(StringUtils.isAllLowerCase(" "));
assertTrue(StringUtils.isAllLowerCase("abc"));
assertFalse(StringUtils.isAllLowerCase("abc "));
assertFalse(StringUtils.isAllLowerCase("abc\n"));
assertFalse(StringUtils.isAllLowerCase("abC"));
assertFalse(StringUtils.isAllLowerCase("ab c"));
assertFalse(StringUtils.isAllLowerCase("ab1c"));
assertFalse(StringUtils.isAllLowerCase("ab/c"));
}
/**
* Test for {@link StringUtils#isAllLowerCase(CharSequence)} with supplementary code points.
*/
@Test
void testIsAllLowerCaseSupplementary() {
// U+10428 DESERET SMALL LETTER LONG I is a lowercase supplementary letter
assertTrue(StringUtils.isAllLowerCase(new String(Character.toChars(0x10428))));
// U+10400 DESERET CAPITAL LETTER LONG I is an uppercase supplementary letter
assertFalse(StringUtils.isAllLowerCase(new String(Character.toChars(0x10400))));
}
/**
* Test for {@link StringUtils#isAllUpperCase(CharSequence)}.
*/
@Test
void testIsAllUpperCase() {
assertFalse(StringUtils.isAllUpperCase(null));
assertFalse(StringUtils.isAllUpperCase(StringUtils.EMPTY));
assertFalse(StringUtils.isAllUpperCase(" "));
assertTrue(StringUtils.isAllUpperCase("ABC"));
assertFalse(StringUtils.isAllUpperCase("ABC "));
assertFalse(StringUtils.isAllUpperCase("ABC\n"));
assertFalse(StringUtils.isAllUpperCase("aBC"));
assertFalse(StringUtils.isAllUpperCase("A C"));
assertFalse(StringUtils.isAllUpperCase("A1C"));
assertFalse(StringUtils.isAllUpperCase("A/C"));
}
/**
* Test for {@link StringUtils#isAllUpperCase(CharSequence)} with supplementary code points.
*/
@Test
void testIsAllUpperCaseSupplementary() {
// U+10400 DESERET CAPITAL LETTER LONG I is an uppercase supplementary letter
assertTrue(StringUtils.isAllUpperCase(new String(Character.toChars(0x10400))));
// U+10428 DESERET SMALL LETTER LONG I is a lowercase supplementary letter
assertFalse(StringUtils.isAllUpperCase(new String(Character.toChars(0x10428))));
}
/**
* Test for {@link StringUtils#isMixedCase(CharSequence)}.
*/