1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3;
18
19 import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
20 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNotEquals;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTimeout;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.junit.jupiter.api.Assertions.fail;
28
29 import java.nio.charset.Charset;
30 import java.nio.charset.StandardCharsets;
31 import java.time.Duration;
32 import java.util.Arrays;
33 import java.util.Random;
34 import java.util.function.IntFunction;
35 import java.util.stream.Stream;
36
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.Timeout;
39 import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
40 import org.junit.jupiter.params.ParameterizedTest;
41 import org.junit.jupiter.params.provider.MethodSource;
42 import org.junit.jupiter.params.provider.ValueSource;
43
44
45
46
47 class RandomStringUtilsTest extends AbstractLangTest {
48
49 private static final int LOOP_COUNT = 1_000;
50
51
52 private static final int BOUNDARY_SAMPLE_LENGTH = 100;
53
54
55 private static final int MAX_SAFE_COUNT = 63_913_201;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 private static void assertInclusiveRange(final IntFunction<String> generator, final char minInclusive, final char maxInclusive) {
72 boolean minFound = false;
73 boolean maxFound = false;
74 for (int i = 0; i < LOOP_COUNT; i++) {
75 final String randString = generator.apply(BOUNDARY_SAMPLE_LENGTH);
76 assertEquals(BOUNDARY_SAMPLE_LENGTH, randString.length(), "generated length");
77 for (int j = 0; j < randString.length(); j++) {
78 final char ch = randString.charAt(j);
79 assertTrue(ch >= minInclusive && ch <= maxInclusive, () -> "character out of range: " + (int) ch);
80 minFound |= ch == minInclusive;
81 maxFound |= ch == maxInclusive;
82 }
83 }
84 assertTrue(minFound, () -> "character not generated in " + LOOP_COUNT + " attempts: " + (int) minInclusive);
85 assertTrue(maxFound, () -> "character not generated in " + LOOP_COUNT + " attempts: " + (int) maxInclusive);
86 }
87
88 static Stream<RandomStringUtils> randomProvider() {
89 return Stream.of(RandomStringUtils.secure(), RandomStringUtils.secureStrong(), RandomStringUtils.insecure());
90 }
91
92
93
94
95
96
97
98 private double chiSquare(final int[] expected, final int[] observed) {
99 double sumSq = 0.0d;
100 for (int i = 0; i < observed.length; i++) {
101 final double dev = observed[i] - expected[i];
102 sumSq += dev * dev / expected[i];
103 }
104 return sumSq;
105 }
106
107
108
109
110 @Test
111 void testCharOverflow() {
112 final int start = Character.MAX_VALUE;
113 final int end = Integer.MAX_VALUE;
114
115 final Random fixedRandom = new Random() {
116 @Override
117 public int nextInt(final int n) {
118
119 return super.nextInt(n - 1) + 1;
120 }
121 };
122
123 final String result = RandomStringUtils.random(2, start, end, false, false, null, fixedRandom);
124 final int c = result.codePointAt(0);
125 assertTrue(c >= start && c < end, String.format("Character '%d' not in range [%d,%d).", c, start, end));
126 }
127
128 @Test
129 void testConstructor() {
130 assertNotNull(new RandomStringUtils());
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145 @Test
146 void testCustomLetterCharsArrayDoesNotThrowIAE() {
147 final char[] letters = { 'a', 'b', 'c' };
148 assertDoesNotThrow(() -> {
149 final String result = RandomStringUtils.random(5, 0, 0, true, false, letters, new Random(42));
150 assertEquals(5, result.length());
151 for (final char c : result.toCharArray()) {
152 assertTrue(c == 'a' || c == 'b' || c == 'c', () -> "Expected char from {a,b,c} but got: " + c);
153 }
154 }, "RandomStringUtils.random() threw IAE for valid letter chars array - pre-patch behavior");
155 }
156
157 @Test
158 void testExceptionsRandom() {
159 assertIllegalArgumentException(() -> RandomStringUtils.random(-1));
160 assertIllegalArgumentException(() -> RandomStringUtils.random(-1, true, true));
161 assertIllegalArgumentException(() -> RandomStringUtils.random(-1, new char[] { 'a' }));
162 assertIllegalArgumentException(() -> RandomStringUtils.random(1, new char[0]));
163 assertIllegalArgumentException(() -> RandomStringUtils.random(-1, ""));
164 assertIllegalArgumentException(() -> RandomStringUtils.random(-1, (String) null));
165 assertIllegalArgumentException(() -> RandomStringUtils.random(-1, 'a', 'z', false, false));
166 assertIllegalArgumentException(() -> RandomStringUtils.random(-1, 'a', 'z', false, false, new char[] { 'a' }));
167 assertIllegalArgumentException(() -> RandomStringUtils.random(-1, 'a', 'z', false, false, new char[] { 'a' }, new Random()));
168 assertIllegalArgumentException(() -> RandomStringUtils.random(8, 32, 48, false, true));
169 assertIllegalArgumentException(() -> RandomStringUtils.random(8, 32, 65, true, false));
170 assertIllegalArgumentException(() -> RandomStringUtils.random(1, Integer.MIN_VALUE, -10, false, false, null));
171 assertIllegalArgumentException(() -> RandomStringUtils.random(2, 4, 5, false, false, new char[] { 'a', 'b', 'c', 'd' }, new Random()));
172 assertIllegalArgumentException(() -> RandomStringUtils.random(2, 1, 5, false, false, new char[] { 'a', 'b', 'c', 'd' }, new Random()));
173
174 assertIllegalArgumentException(() -> RandomStringUtils.random(5, 0x80, 0xA0, true, false, null, new Random()));
175 }
176
177 @ParameterizedTest
178 @MethodSource("randomProvider")
179 void testExceptionsRandom(final RandomStringUtils rsu) {
180 assertIllegalArgumentException(() -> rsu.next(-1));
181 assertIllegalArgumentException(() -> rsu.next(-1, true, true));
182 assertIllegalArgumentException(() -> rsu.next(-1, new char[] { 'a' }));
183 assertIllegalArgumentException(() -> rsu.next(1, new char[0]));
184 assertIllegalArgumentException(() -> rsu.next(-1, ""));
185 assertIllegalArgumentException(() -> rsu.next(-1, (String) null));
186 assertIllegalArgumentException(() -> rsu.next(-1, 'a', 'z', false, false));
187 assertIllegalArgumentException(() -> rsu.next(-1, 'a', 'z', false, false, new char[] { 'a' }));
188 assertIllegalArgumentException(() -> rsu.next(8, 32, 48, false, true));
189 assertIllegalArgumentException(() -> rsu.next(8, 32, 65, true, false));
190 assertIllegalArgumentException(() -> rsu.next(1, Integer.MIN_VALUE, -10, false, false, null));
191 }
192
193 @Test
194 void testExceptionsRandomAlphabetic() {
195 assertIllegalArgumentException(() -> RandomStringUtils.randomAlphabetic(-1));
196 }
197
198 @ParameterizedTest
199 @MethodSource("randomProvider")
200 void testExceptionsRandomAlphabetic(final RandomStringUtils rsu) {
201 assertIllegalArgumentException(() -> rsu.nextAlphabetic(-1));
202 }
203
204 @Test
205 void testExceptionsRandomAscii() {
206 assertIllegalArgumentException(() -> RandomStringUtils.randomAscii(-1));
207 }
208
209 @ParameterizedTest
210 @MethodSource("randomProvider")
211 void testExceptionsRandomAscii(final RandomStringUtils rsu) {
212 assertIllegalArgumentException(() -> rsu.nextAscii(-1));
213 }
214
215 @Test
216 void testExceptionsRandomGraph() {
217 assertIllegalArgumentException(() -> RandomStringUtils.randomGraph(-1));
218 }
219
220 @ParameterizedTest
221 @MethodSource("randomProvider")
222 void testExceptionsRandomGraph(final RandomStringUtils rsu) {
223 assertIllegalArgumentException(() -> rsu.nextGraph(-1));
224 }
225
226 @Test
227 void testExceptionsRandomNumeric() {
228 assertIllegalArgumentException(() -> RandomStringUtils.randomNumeric(-1));
229 }
230
231 @ParameterizedTest
232 @MethodSource("randomProvider")
233 void testExceptionsRandomNumeric(final RandomStringUtils rsu) {
234 assertIllegalArgumentException(() -> rsu.nextNumeric(-1));
235 }
236
237 @Test
238 void testExceptionsRandomPrint() {
239 assertIllegalArgumentException(() -> RandomStringUtils.randomPrint(-1));
240 }
241
242 @ParameterizedTest
243 @MethodSource("randomProvider")
244 void testExceptionsRandomPrint(final RandomStringUtils rsu) {
245 assertIllegalArgumentException(() -> rsu.nextPrint(-1));
246 }
247
248 @Test
249 @Timeout(value = 2, threadMode = Timeout.ThreadMode.SAME_THREAD)
250 void testFilterLetters() {
251 assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(5, 0x80, 0xA0, true, false, null, new Random()));
252 }
253
254 @Test
255 @Timeout(value = 2, threadMode = Timeout.ThreadMode.SAME_THREAD)
256 void testFilterNumbers() {
257 assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(5, 0x80, 0xA0, false, true, null, new Random()));
258 }
259
260
261
262
263
264
265
266 @ParameterizedTest
267 @MethodSource("randomProvider")
268 void testHomogeneity(final RandomStringUtils rsu) {
269 final String set = "abc";
270 final char[] chars = set.toCharArray();
271 final int[] counts = { 0, 0, 0 };
272 final int[] expected = { 200, 200, 200 };
273 for (int i = 0; i < 100; i++) {
274 final String gen = rsu.next(6, chars);
275 for (int j = 0; j < 6; j++) {
276 switch (gen.charAt(j)) {
277 case 'a': {
278 counts[0]++;
279 break;
280 }
281 case 'b': {
282 counts[1]++;
283 break;
284 }
285 case 'c': {
286 counts[2]++;
287 break;
288 }
289 default: {
290 fail("generated character not in set");
291 }
292 }
293 }
294 }
295
296
297
298 assertTrue(chiSquare(expected, counts) < 23.025850929940457d, "test homogeneity -- will fail about 1 in 100,000 times");
299 }
300
301 @ParameterizedTest
302 @ValueSource(ints = {MAX_SAFE_COUNT, MAX_SAFE_COUNT + 1})
303 @EnabledIfSystemProperty(named = "test.large.heap", matches = "true")
304 void testHugeStrings(final int expectedLength) {
305 final String hugeString = RandomStringUtils.random(expectedLength);
306 assertEquals(expectedLength, hugeString.length(), "hugeString.length() == expectedLength");
307 }
308
309
310
311
312
313
314 @Test
315 void testLang100() {
316 final int size = 5000;
317 final Charset charset = StandardCharsets.UTF_8;
318 final String orig = RandomStringUtils.random(size);
319 final byte[] bytes = orig.getBytes(charset);
320 final String copy = new String(bytes, charset);
321
322
323 for (int i = 0; i < orig.length() && i < copy.length(); i++) {
324 final char o = orig.charAt(i);
325 final char c = copy.charAt(i);
326 assertEquals(o, c, "differs at " + i + "(" + Integer.toHexString(Character.valueOf(o).hashCode()) + ","
327 + Integer.toHexString(Character.valueOf(c).hashCode()) + ")");
328 }
329
330 assertEquals(orig.length(), copy.length());
331
332 assertEquals(orig, copy);
333 }
334
335
336
337
338
339
340
341 @ParameterizedTest
342 @MethodSource("randomProvider")
343 void testLang100(final RandomStringUtils rsu) {
344 final int size = 5000;
345 final Charset charset = StandardCharsets.UTF_8;
346 final String orig = rsu.next(size);
347 final byte[] bytes = orig.getBytes(charset);
348 final String copy = new String(bytes, charset);
349
350
351 for (int i = 0; i < orig.length() && i < copy.length(); i++) {
352 final char o = orig.charAt(i);
353 final char c = copy.charAt(i);
354 assertEquals(o, c, "differs at " + i + "(" + Integer.toHexString(Character.valueOf(o).hashCode()) + ","
355 + Integer.toHexString(Character.valueOf(c).hashCode()) + ")");
356 }
357
358 assertEquals(orig.length(), copy.length());
359
360 assertEquals(orig, copy);
361 }
362
363 @Test
364 void testLANG805() {
365 final long seedMillis = System.currentTimeMillis();
366 assertEquals("aaa", RandomStringUtils.random(3, 0, 0, false, false, new char[] { 'a' }, new Random(seedMillis)));
367 }
368
369 @ParameterizedTest
370 @MethodSource("randomProvider")
371 void testLANG807(final RandomStringUtils rsu) {
372 final IllegalArgumentException ex = assertIllegalArgumentException(() -> rsu.next(3, 5, 5, false, false));
373 final String msg = ex.getMessage();
374 assertTrue(msg.contains("start"), "Message (" + msg + ") must contain 'start'");
375 assertTrue(msg.contains("end"), "Message (" + msg + ") must contain 'end'");
376 }
377
378
379
380
381
382
383 @Test
384 void testLettersAndDigitsOverDigitOnlyRange() {
385 final String both = RandomStringUtils.random(100, '0', 'A', true, true, null, new Random(42));
386 assertEquals(100, both.length());
387 for (final char c : both.toCharArray()) {
388 assertTrue(c >= '0' && c <= '9', () -> "Expected a digit but got: " + c);
389 }
390
391 assertDoesNotThrow(() -> RandomStringUtils.random(100, '0', 'A', false, true, null, new Random(42)));
392 }
393
394
395
396
397
398
399
400 @Test
401 void testLettersAndDigitsOverEmptyAsciiRange() {
402 final IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
403 () -> RandomStringUtils.random(10, 'z' + 1, 0x7f, true, true, null, new Random(42)));
404 assertTrue(e.getMessage() != null && !e.getMessage().contains("number of bits"),
405 () -> "Expected a range-validation message but got: " + e.getMessage());
406 }
407
408
409
410
411
412
413 @ParameterizedTest
414 @MethodSource("randomProvider")
415 void testNonASCIILetters(final RandomStringUtils rsu) {
416
417 String r1 = rsu.next(10, 0x4e00, 0x4e01, true, false);
418 assertEquals(10, r1.length(), "wrong length");
419 for (int i = 0; i < r1.length(); i++) {
420 assertEquals(0x4e00, r1.charAt(i), "characters not all equal to 0x4e00");
421 }
422
423
424 r1 = rsu.next(10, 0x4e00, 0x4e01, true, true);
425 assertEquals(10, r1.length(), "wrong length");
426 for (int i = 0; i < r1.length(); i++) {
427 assertEquals(0x4e00, r1.charAt(i), "characters not all equal to 0x4e00");
428 }
429
430
431 boolean found = false;
432 r1 = rsu.next(40, 'F', 0x3000, true, false);
433 assertEquals(40, r1.length(), "wrong length");
434 for (int i = 0; i < r1.length(); i++) {
435 assertTrue(Character.isLetter(r1.charAt(i)), "characters not all letters");
436 if (r1.charAt(i) > 0x7f) {
437 found = true;
438 }
439 }
440 assertTrue(found, "no non-ASCII letter generated");
441 }
442
443
444
445
446
447
448 @ParameterizedTest
449 @MethodSource("randomProvider")
450 void testNonASCIINumbers(final RandomStringUtils rsu) {
451
452 String r1 = rsu.next(10, 0x0660, 0x0661, false, true);
453 assertEquals(10, r1.length(), "wrong length");
454 for (int i = 0; i < r1.length(); i++) {
455 assertEquals(0x0660, r1.charAt(i), "characters not all equal to 0x0660");
456 }
457
458
459 r1 = rsu.next(10, 0x0660, 0x0661, true, true);
460 assertEquals(10, r1.length(), "wrong length");
461 for (int i = 0; i < r1.length(); i++) {
462 assertEquals(0x0660, r1.charAt(i), "characters not all equal to 0x0660");
463 }
464
465
466 boolean found = false;
467 r1 = rsu.next(40, 'F', 0x3000, false, true);
468 assertEquals(40, r1.length(), "wrong length");
469 for (int i = 0; i < r1.length(); i++) {
470 assertTrue(Character.isDigit(r1.charAt(i)), "characters not all numbers");
471 if (r1.charAt(i) > 0x7f) {
472 found = true;
473 }
474 }
475 assertTrue(found, "no non-ASCII number generated");
476 }
477
478
479
480
481
482
483
484
485
486
487
488
489
490 @Test
491 void testOnlyRejectedCodePoints() {
492 assertTimeout(Duration.ofSeconds(2),
493 () -> assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(1, 0xE000, 0xF900, false, false, null, new Random(42))));
494 }
495
496
497
498
499 @Test
500 void testRandomAlphabetic() {
501 final char[] testChars = { 'a', 'z', 'A', 'Z' };
502 final boolean[] found = { false, false, false, false };
503 for (int i = 0; i < LOOP_COUNT; i++) {
504 final String randString = RandomStringUtils.randomAlphabetic(10);
505 for (int j = 0; j < testChars.length; j++) {
506 if (randString.indexOf(testChars[j]) > 0) {
507 found[j] = true;
508 }
509 }
510 }
511 for (int i = 0; i < testChars.length; i++) {
512 assertTrue(found[i], "alphanumeric character not generated in 1000 attempts: " + testChars[i] + " -- repeated failures indicate a problem ");
513 }
514 }
515
516
517
518
519
520
521 @ParameterizedTest
522 @MethodSource("randomProvider")
523 void testRandomAlphabetic(final RandomStringUtils rsu) {
524 final char[] testChars = { 'a', 'z', 'A', 'Z' };
525 final boolean[] found = { false, false, false, false };
526 for (int i = 0; i < LOOP_COUNT; i++) {
527 final String randString = rsu.nextAlphabetic(10);
528 for (int j = 0; j < testChars.length; j++) {
529 if (randString.indexOf(testChars[j]) > 0) {
530 found[j] = true;
531 }
532 }
533 }
534 for (int i = 0; i < testChars.length; i++) {
535 assertTrue(found[i], "alphanumeric character not generated in 1000 attempts: " + testChars[i] + " -- repeated failures indicate a problem ");
536 }
537 }
538
539 @Test
540 void testRandomAlphabeticRange() {
541 final int expectedMinLengthInclusive = 1;
542 final int expectedMaxLengthExclusive = 11;
543 final String pattern = "^\\p{Alpha}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
544
545 int maxCreatedLength = expectedMinLengthInclusive;
546 int minCreatedLength = expectedMaxLengthExclusive - 1;
547 for (int i = 0; i < LOOP_COUNT; i++) {
548 final String s = RandomStringUtils.randomAlphabetic(expectedMinLengthInclusive, expectedMaxLengthExclusive);
549 assertTrue(s.length() >= expectedMinLengthInclusive, "within range");
550 assertTrue(s.length() <= expectedMaxLengthExclusive - 1, "within range");
551 assertTrue(s.matches(pattern), s);
552
553 if (s.length() < minCreatedLength) {
554 minCreatedLength = s.length();
555 }
556
557 if (s.length() > maxCreatedLength) {
558 maxCreatedLength = s.length();
559 }
560 }
561 assertEquals(expectedMinLengthInclusive, minCreatedLength, "min generated, may fail randomly rarely");
562 assertEquals(expectedMaxLengthExclusive - 1, maxCreatedLength, "max generated, may fail randomly rarely");
563 }
564
565 @ParameterizedTest
566 @MethodSource("randomProvider")
567 void testRandomAlphabeticRange(final RandomStringUtils rsu) {
568 final int expectedMinLengthInclusive = 1;
569 final int expectedMaxLengthExclusive = 11;
570 final String pattern = "^\\p{Alpha}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
571
572 int maxCreatedLength = expectedMinLengthInclusive;
573 int minCreatedLength = expectedMaxLengthExclusive - 1;
574 for (int i = 0; i < LOOP_COUNT; i++) {
575 final String s = rsu.nextAlphabetic(expectedMinLengthInclusive, expectedMaxLengthExclusive);
576 assertTrue(s.length() >= expectedMinLengthInclusive, "within range");
577 assertTrue(s.length() <= expectedMaxLengthExclusive - 1, "within range");
578
579 assertTrue(s.matches(pattern), s);
580
581 if (s.length() < minCreatedLength) {
582 minCreatedLength = s.length();
583 }
584
585 if (s.length() > maxCreatedLength) {
586 maxCreatedLength = s.length();
587 }
588 }
589 assertEquals(expectedMinLengthInclusive, minCreatedLength, "min generated, may fail randomly rarely");
590 assertEquals(expectedMaxLengthExclusive - 1, maxCreatedLength, "max generated, may fail randomly rarely");
591 }
592
593
594
595
596 @Test
597 void testRandomAlphaNumeric() {
598 final char[] testChars = { 'a', 'z', 'A', 'Z', '0', '9' };
599 final boolean[] found = { false, false, false, false, false, false };
600 for (int i = 0; i < LOOP_COUNT; i++) {
601 final String randString = RandomStringUtils.randomAlphanumeric(10);
602 for (int j = 0; j < testChars.length; j++) {
603 if (randString.indexOf(testChars[j]) > 0) {
604 found[j] = true;
605 }
606 }
607 }
608 for (int i = 0; i < testChars.length; i++) {
609 assertTrue(found[i], "alphanumeric character not generated in 1000 attempts: " + testChars[i] + " -- repeated failures indicate a problem ");
610 }
611 }
612
613
614
615
616
617
618 @ParameterizedTest
619 @MethodSource("randomProvider")
620 void testRandomAlphaNumeric(final RandomStringUtils rsu) {
621 final char[] testChars = { 'a', 'z', 'A', 'Z', '0', '9' };
622 final boolean[] found = { false, false, false, false, false, false };
623 for (int i = 0; i < LOOP_COUNT; i++) {
624 final String randString = rsu.nextAlphanumeric(10);
625 for (int j = 0; j < testChars.length; j++) {
626 if (randString.indexOf(testChars[j]) > 0) {
627 found[j] = true;
628 }
629 }
630 }
631 for (int i = 0; i < testChars.length; i++) {
632 assertTrue(found[i], "alphanumeric character not generated in 1000 attempts: " + testChars[i] + " -- repeated failures indicate a problem ");
633 }
634 }
635
636 @Test
637 void testRandomAlphanumericRange() {
638 final int expectedMinLengthInclusive = 1;
639 final int expectedMaxLengthExclusive = 11;
640 final String pattern = "^\\p{Alnum}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
641
642 int maxCreatedLength = expectedMinLengthInclusive;
643 int minCreatedLength = expectedMaxLengthExclusive - 1;
644 for (int i = 0; i < LOOP_COUNT; i++) {
645 final String s = RandomStringUtils.randomAlphanumeric(expectedMinLengthInclusive, expectedMaxLengthExclusive);
646 assertTrue(s.length() >= expectedMinLengthInclusive, "within range");
647 assertTrue(s.length() <= expectedMaxLengthExclusive - 1, "within range");
648 assertTrue(s.matches(pattern), s);
649
650 if (s.length() < minCreatedLength) {
651 minCreatedLength = s.length();
652 }
653
654 if (s.length() > maxCreatedLength) {
655 maxCreatedLength = s.length();
656 }
657 }
658 assertEquals(expectedMinLengthInclusive, minCreatedLength, "min generated, may fail randomly rarely");
659 assertEquals(expectedMaxLengthExclusive - 1, maxCreatedLength, "max generated, may fail randomly rarely");
660 }
661
662
663
664
665
666
667 @ParameterizedTest
668 @MethodSource("randomProvider")
669 void testRandomApis(final RandomStringUtils rsu) {
670 String r1 = rsu.next(50);
671 assertEquals(50, r1.length(), "random(50) length");
672 String r2 = rsu.next(50);
673 assertEquals(50, r2.length(), "random(50) length");
674 assertNotEquals(r1, r2, "!r1.equals(r2)");
675
676 r1 = rsu.nextAscii(50);
677 assertEquals(50, r1.length(), "randomAscii(50) length");
678 for (int i = 0; i < r1.length(); i++) {
679 final int ch = r1.charAt(i);
680 assertTrue(ch >= 32, "char >= 32");
681 assertTrue(ch <= 127, "char <= 127");
682 }
683 r2 = rsu.nextAscii(50);
684 assertNotEquals(r1, r2, "!r1.equals(r2)");
685
686 r1 = rsu.nextAlphabetic(50);
687 assertEquals(50, r1.length(), "randomAlphabetic(50)");
688 for (int i = 0; i < r1.length(); i++) {
689 assertTrue(Character.isLetter(r1.charAt(i)) && !Character.isDigit(r1.charAt(i)), "r1 contains alphabetic");
690 }
691 r2 = rsu.nextAlphabetic(50);
692 assertNotEquals(r1, r2, "!r1.equals(r2)");
693
694 r1 = rsu.nextAlphanumeric(50);
695 assertEquals(50, r1.length(), "randomAlphanumeric(50)");
696 for (int i = 0; i < r1.length(); i++) {
697 assertTrue(Character.isLetterOrDigit(r1.charAt(i)), "r1 contains alphanumeric");
698 }
699 r2 = rsu.nextAlphabetic(50);
700 assertNotEquals(r1, r2, "!r1.equals(r2)");
701
702 r1 = rsu.nextGraph(50);
703 assertEquals(50, r1.length(), "randomGraph(50) length");
704 for (int i = 0; i < r1.length(); i++) {
705 assertTrue(r1.charAt(i) >= 33 && r1.charAt(i) <= 126, "char between 33 and 126");
706 }
707 r2 = rsu.nextGraph(50);
708 assertNotEquals(r1, r2, "!r1.equals(r2)");
709
710 r1 = rsu.nextNumeric(50);
711 assertEquals(50, r1.length(), "randomNumeric(50)");
712 for (int i = 0; i < r1.length(); i++) {
713 assertTrue(Character.isDigit(r1.charAt(i)) && !Character.isLetter(r1.charAt(i)), "r1 contains numeric");
714 }
715 r2 = rsu.nextNumeric(50);
716 assertNotEquals(r1, r2, "!r1.equals(r2)");
717
718 r1 = rsu.nextPrint(50);
719 assertEquals(50, r1.length(), "randomPrint(50) length");
720 for (int i = 0; i < r1.length(); i++) {
721 assertTrue(r1.charAt(i) >= 32 && r1.charAt(i) <= 126, "char between 32 and 126");
722 }
723 r2 = rsu.nextPrint(50);
724 assertNotEquals(r1, r2, "!r1.equals(r2)");
725
726 String set = "abcdefg";
727 r1 = rsu.next(50, set);
728 assertEquals(50, r1.length(), "random(50, \"abcdefg\")");
729 for (int i = 0; i < r1.length(); i++) {
730 assertTrue(set.indexOf(r1.charAt(i)) > -1, "random char in set");
731 }
732 r2 = rsu.next(50, set);
733 assertNotEquals(r1, r2, "!r1.equals(r2)");
734
735 r1 = rsu.next(50, (String) null);
736 assertEquals(50, r1.length(), "random(50) length");
737 r2 = rsu.next(50, (String) null);
738 assertEquals(50, r2.length(), "random(50) length");
739 assertNotEquals(r1, r2, "!r1.equals(r2)");
740
741 set = "stuvwxyz";
742 r1 = rsu.next(50, set.toCharArray());
743 assertEquals(50, r1.length(), "random(50, \"stuvwxyz\")");
744 for (int i = 0; i < r1.length(); i++) {
745 assertTrue(set.indexOf(r1.charAt(i)) > -1, "random char in set");
746 }
747 r2 = rsu.next(50, set);
748 assertNotEquals(r1, r2, "!r1.equals(r2)");
749
750 r1 = rsu.next(50, (char[]) null);
751 assertEquals(50, r1.length(), "random(50) length");
752 r2 = rsu.next(50, (char[]) null);
753 assertEquals(50, r2.length(), "random(50) length");
754 assertNotEquals(r1, r2, "!r1.equals(r2)");
755
756 r1 = rsu.next(0);
757 assertEquals("", r1, "random(0).equals(\"\")");
758 }
759
760 @ParameterizedTest
761 @MethodSource("randomProvider")
762 void testRandomAscii(final RandomStringUtils rsu) {
763 assertInclusiveRange(rsu::nextAscii, ' ', '~');
764 }
765
766 @ParameterizedTest
767 @MethodSource("randomProvider")
768 void testRandomAsciiRange(final RandomStringUtils rsu) {
769 final int expectedMinLengthInclusive = 1;
770 final int expectedMaxLengthExclusive = 11;
771 final String pattern = "^\\p{ASCII}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
772
773 int maxCreatedLength = expectedMinLengthInclusive;
774 int minCreatedLength = expectedMaxLengthExclusive - 1;
775 for (int i = 0; i < LOOP_COUNT; i++) {
776 final String s = rsu.nextAscii(expectedMinLengthInclusive, expectedMaxLengthExclusive);
777 assertTrue(s.length() >= expectedMinLengthInclusive, "within range");
778 assertTrue(s.length() <= expectedMaxLengthExclusive - 1, "within range");
779 assertTrue(s.matches(pattern), s);
780
781 if (s.length() < minCreatedLength) {
782 minCreatedLength = s.length();
783 }
784
785 if (s.length() > maxCreatedLength) {
786 maxCreatedLength = s.length();
787 }
788 }
789 assertEquals(expectedMinLengthInclusive, minCreatedLength, "min generated, may fail randomly rarely");
790 assertEquals(expectedMaxLengthExclusive - 1, maxCreatedLength, "max generated, may fail randomly rarely");
791 }
792
793 @ParameterizedTest
794 @MethodSource("randomProvider")
795 void testRandomAsciiRangeBoundaries(final RandomStringUtils rsu) {
796 assertInclusiveRange(count -> rsu.nextAscii(count, count + 1), ' ', '~');
797 }
798
799
800
801
802
803
804
805
806 @ParameterizedTest
807 @MethodSource("randomProvider")
808 void testRandomGraphIncludesTilde(final RandomStringUtils rsu) {
809 assertInclusiveRange(rsu::nextGraph, '!', '~');
810 }
811
812 @ParameterizedTest
813 @MethodSource("randomProvider")
814 void testRandomGraphRange(final RandomStringUtils rsu) {
815 final int expectedMinLengthInclusive = 1;
816 final int expectedMaxLengthExclusive = 11;
817 final String pattern = "^\\p{Graph}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
818
819 int maxCreatedLength = expectedMinLengthInclusive;
820 int minCreatedLength = expectedMaxLengthExclusive - 1;
821 for (int i = 0; i < LOOP_COUNT; i++) {
822 final String s = rsu.nextGraph(expectedMinLengthInclusive, expectedMaxLengthExclusive);
823 assertTrue(s.length() >= expectedMinLengthInclusive, "within range");
824 assertTrue(s.length() <= expectedMaxLengthExclusive - 1, "within range");
825 assertTrue(s.matches(pattern), s);
826
827 if (s.length() < minCreatedLength) {
828 minCreatedLength = s.length();
829 }
830
831 if (s.length() > maxCreatedLength) {
832 maxCreatedLength = s.length();
833 }
834 }
835 assertEquals(expectedMinLengthInclusive, minCreatedLength, "min generated, may fail randomly rarely");
836 assertEquals(expectedMaxLengthExclusive - 1, maxCreatedLength, "max generated, may fail randomly rarely");
837 }
838
839 @ParameterizedTest
840 @MethodSource("randomProvider")
841 void testRandomGraphRangeBoundaries(final RandomStringUtils rsu) {
842 assertInclusiveRange(count -> rsu.nextGraph(count, count + 1), '!', '~');
843 }
844
845 @Test
846 void testRandomGraphStaticBoundaries() {
847 assertInclusiveRange(RandomStringUtils::randomGraph, '!', '~');
848 }
849
850 @Test
851 void testRandomGraphStaticRangeBoundaries() {
852 assertInclusiveRange(count -> RandomStringUtils.randomGraph(count, count + 1), '!', '~');
853 }
854
855
856
857
858
859
860 @ParameterizedTest
861 @MethodSource("randomProvider")
862 void testRandomNumeric(final RandomStringUtils rsu) {
863 final char[] testChars = { '0', '9' };
864 final boolean[] found = { false, false };
865 for (int i = 0; i < LOOP_COUNT; i++) {
866 final String randString = rsu.nextNumeric(10);
867 for (int j = 0; j < testChars.length; j++) {
868 if (randString.indexOf(testChars[j]) > 0) {
869 found[j] = true;
870 }
871 }
872 }
873 for (int i = 0; i < testChars.length; i++) {
874 assertTrue(found[i], "digit not generated in 1000 attempts: " + testChars[i] + " -- repeated failures indicate a problem ");
875 }
876 }
877
878 @ParameterizedTest
879 @MethodSource("randomProvider")
880 void testRandomNumericRange(final RandomStringUtils rsu) {
881 final int expectedMinLengthInclusive = 1;
882 final int expectedMaxLengthExclusive = 11;
883 final String pattern = "^\\p{Digit}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
884
885 int maxCreatedLength = expectedMinLengthInclusive;
886 int minCreatedLength = expectedMaxLengthExclusive - 1;
887 for (int i = 0; i < LOOP_COUNT; i++) {
888 final String s = rsu.nextNumeric(expectedMinLengthInclusive, expectedMaxLengthExclusive);
889 assertTrue(s.length() >= expectedMinLengthInclusive, "within range");
890 assertTrue(s.length() <= expectedMaxLengthExclusive - 1, "within range");
891 assertTrue(s.matches(pattern), s);
892
893 if (s.length() < minCreatedLength) {
894 minCreatedLength = s.length();
895 }
896
897 if (s.length() > maxCreatedLength) {
898 maxCreatedLength = s.length();
899 }
900 }
901 assertEquals(expectedMinLengthInclusive, minCreatedLength, "min generated, may fail randomly rarely");
902 assertEquals(expectedMaxLengthExclusive - 1, maxCreatedLength, "max generated, may fail randomly rarely");
903 }
904
905 @Test
906 void testRandomParameter() {
907 final long seedMillis = System.currentTimeMillis();
908 final String r1 = RandomStringUtils.random(50, 0, 0, true, true, null, new Random(seedMillis));
909 final String r2 = RandomStringUtils.random(50, 0, 0, true, true, null, new Random(seedMillis));
910 assertEquals(r1, r2, "r1.equals(r2)");
911 }
912
913
914
915
916
917
918
919
920 @ParameterizedTest
921 @MethodSource("randomProvider")
922 void testRandomPrintIncludesTilde(final RandomStringUtils rsu) {
923 assertInclusiveRange(rsu::nextPrint, ' ', '~');
924 }
925
926 @ParameterizedTest
927 @MethodSource("randomProvider")
928 void testRandomPrintRange(final RandomStringUtils rsu) {
929 final int expectedMinLengthInclusive = 1;
930 final int expectedMaxLengthExclusive = 11;
931 final String pattern = "^\\p{Print}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
932
933 int maxCreatedLength = expectedMinLengthInclusive;
934 int minCreatedLength = expectedMaxLengthExclusive - 1;
935 for (int i = 0; i < LOOP_COUNT; i++) {
936 final String s = rsu.nextPrint(expectedMinLengthInclusive, expectedMaxLengthExclusive);
937 assertTrue(s.length() >= expectedMinLengthInclusive, "within range");
938 assertTrue(s.length() <= expectedMaxLengthExclusive - 1, "within range");
939 assertTrue(s.matches(pattern), s);
940
941 if (s.length() < minCreatedLength) {
942 minCreatedLength = s.length();
943 }
944
945 if (s.length() > maxCreatedLength) {
946 maxCreatedLength = s.length();
947 }
948 }
949 assertEquals(expectedMinLengthInclusive, minCreatedLength, "min generated, may fail randomly rarely");
950 assertEquals(expectedMaxLengthExclusive - 1, maxCreatedLength, "max generated, may fail randomly rarely");
951 }
952
953 @ParameterizedTest
954 @MethodSource("randomProvider")
955 void testRandomPrintRangeBoundaries(final RandomStringUtils rsu) {
956 assertInclusiveRange(count -> rsu.nextPrint(count, count + 1), ' ', '~');
957 }
958
959 @Test
960 void testRandomPrintStaticBoundaries() {
961 assertInclusiveRange(RandomStringUtils::randomPrint, ' ', '~');
962 }
963
964 @Test
965 void testRandomPrintStaticRangeBoundaries() {
966 assertInclusiveRange(count -> RandomStringUtils.randomPrint(count, count + 1), ' ', '~');
967 }
968
969
970
971
972
973
974 @ParameterizedTest
975 @MethodSource("randomProvider")
976 void testRandomWithChars(final RandomStringUtils rsu) {
977 final char[] digitChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
978 final String r1 = rsu.next(50, 0, 0, true, true, digitChars);
979 assertEquals(50, r1.length(), "randomNumeric(50)");
980 for (int i = 0; i < r1.length(); i++) {
981 assertTrue(
982 Character.isDigit(r1.charAt(i)) && !Character.isLetter(r1.charAt(i)),
983 "r1 contains numeric");
984 }
985 final String r2 = rsu.nextNumeric(50);
986 assertNotEquals(r1, r2);
987
988 final String r3 = rsu.next(50, 0, 0, true, true, digitChars);
989 assertNotEquals(r1, r3);
990 assertNotEquals(r2, r3);
991 }
992
993
994
995
996
997
998 @ParameterizedTest
999 @ValueSource(ints = { 0x0378, 0xD800, 0xE000, 0x10000 })
1000 void testRejectedCodePointWithControlledRandom(final int codePoint) {
1001 final Random random = new Random(0) {
1002 private int bytesGenerated;
1003
1004 @Override
1005 public void nextBytes(final byte[] bytes) {
1006 bytesGenerated += bytes.length;
1007 assertTrue(bytesGenerated < 1_024, "The rejection cap did not terminate generation");
1008 Arrays.fill(bytes, (byte) 0);
1009 }
1010 };
1011
1012 assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(1, codePoint, codePoint + 1, false, false, null, random));
1013 }
1014
1015
1016
1017
1018 @Test
1019 void testRejectionCapResetsAfterAcceptance() {
1020 final char[] chars = new char[255];
1021 Arrays.fill(chars, '-');
1022 chars[1] = 'x';
1023 final Random random = new Random(0) {
1024 private int bytesGenerated;
1025
1026 @Override
1027 public void nextBytes(final byte[] bytes) {
1028
1029
1030 for (int i = 0; i < bytes.length; i++) {
1031 bytes[i] = (byte) (++bytesGenerated % 10_001 == 0 ? 1 : 0);
1032 }
1033 }
1034 };
1035 assertEquals("xx", RandomStringUtils.random(2, 0, chars.length, true, false, chars, random));
1036 }
1037
1038
1039
1040
1041
1042
1043 @ParameterizedTest
1044 @ValueSource(ints = { 0, 255 })
1045 void testRejectionCapWithControlledRandom(final int rejectedValue) {
1046 final char[] chars = new char[255];
1047 Arrays.fill(chars, '-');
1048 chars[1] = 'x';
1049 final Random random = new Random(0) {
1050 private int bytesGenerated;
1051
1052 @Override
1053 public void nextBytes(final byte[] bytes) {
1054 bytesGenerated += bytes.length;
1055
1056 assertTrue(bytesGenerated < 20_000, "The rejection cap did not terminate generation");
1057 Arrays.fill(bytes, (byte) rejectedValue);
1058 }
1059 };
1060 assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(1, 0, chars.length, true, false, chars, random));
1061 }
1062
1063
1064
1065
1066 @Test
1067 void testSparseAcceptableCodePointsDoNotTripRejectionCap() {
1068 assertEquals(StringUtils.repeat('\uF900', 100), RandomStringUtils.random(100, 0xF8C0, 0xF901, true, false, null, new Random(0)));
1069 }
1070
1071
1072
1073
1074
1075
1076
1077
1078 @Test
1079 void testSparseAcceptableValuesDoNotTripRejectionCap() {
1080 final char[] chars = new char[64];
1081 Arrays.fill(chars, '-');
1082 chars[63] = 'x';
1083 for (int seed = 0; seed < 20; seed++) {
1084 assertEquals(StringUtils.repeat('x', 100), RandomStringUtils.random(100, 0, 64, true, false, chars, new Random(seed)));
1085 }
1086 }
1087 }