/** * <p>Creates a random string whose length is between the inclusive minimum and * the exclusive maximum.</p> * * <p>Characters will be chosen from the set of \p{Print} characters.</p> * * @param minLengthInclusive the inclusive minimum length of the string to generate * @param maxLengthExclusive the exclusive maximum length of the string to generate * @return the random string * @since 3.5 */ public static String randomPrint(final int minLengthInclusive, final int maxLengthExclusive) { return randomPrint(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); }
@Test public void testRandomPrintRange() { final int expectedMinLengthInclusive = 1; final int expectedMaxLengthExclusive = 11; final String pattern = "^\\p{Print}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$"; int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; for (int i = 0; i < 1000; i++) { final String s = RandomStringUtils.randomPrint(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s, s.matches(pattern)); if (s.length() < minCreatedLength) { minCreatedLength = s.length(); } if (s.length() > maxCreatedLength) { maxCreatedLength = s.length(); } } assertThat("min generated, may fail randomly rarely", minCreatedLength, is(expectedMinLengthInclusive)); assertThat("max generated, may fail randomly rarely", maxCreatedLength, is(expectedMaxLengthExclusive - 1)); }
assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.randomPrint(50); assertEquals("randomPrint(50) length", 50, r1.length()); for(int i = 0; i < r1.length(); i++) { assertTrue("char between 32 and 126", r1.charAt(i) >= 32 && r1.charAt(i) <= 126); r2 = RandomStringUtils.randomPrint(50); assertTrue("!r1.equals(r2)", !r1.equals(r2));
/** * <p>Creates a random string whose length is between the inclusive minimum and * the exclusive maximum.</p> * * <p>Characters will be chosen from the set of \p{Print} characters.</p> * * @param minLengthInclusive the inclusive minimum length of the string to generate * @param maxLengthExclusive the exclusive maximum length of the string to generate * @return the random string * @since 3.5 */ public static String randomPrint(final int minLengthInclusive, final int maxLengthExclusive) { return randomPrint(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); }
/** * <p>Creates a random string whose length is between the inclusive minimum and * the exclusive maximum.</p> * * <p>Characters will be chosen from the set of \p{Print} characters.</p> * * @param minLengthInclusive the inclusive minimum length of the string to generate * @param maxLengthExclusive the exclusive maximum length of the string to generate * @return the random string * @since 3.5 */ public static String randomPrint(final int minLengthInclusive, final int maxLengthExclusive) { return randomPrint(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); }
/** * <p>Creates a random string whose length is between the inclusive minimum and * the exclusive maximum.</p> * * <p>Characters will be chosen from the set of \p{Print} characters.</p> * * @param minLengthInclusive the inclusive minimum length of the string to generate * @param maxLengthExclusive the exclusive maximum length of the string to generate * @return the random string * @since 3.5 */ public static String randomPrint(final int minLengthInclusive, final int maxLengthExclusive) { return randomPrint(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); }