LANG-1224: Extend RandomStringUtils with methods that generate strings between a min and max length (closes #101, closes #157)
This commit is contained in:
parent
1169aac7a1
commit
ec2fa6bd56
|
@ -46,6 +46,7 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
<body>
|
||||
|
||||
<release version="3.5" date="tba" description="tba">
|
||||
<action issue="LANG-1224" type="add" dev="pschumacher" due-to="Caleb Cushing">Extend RandomStringUtils with methods that generate strings between a min and max length</action>
|
||||
<action issue="LANG-1214" type="fix" dev="pschumacher" due-to="Henry Tung">Handle "void" in ClassUtils.getClass()</action>
|
||||
<action issue="LANG-1250" type="fix" dev="pschumacher" due-to="Glease Wang">SerializationUtils#deserialize has unnecessary code and a comment for that</action>
|
||||
<action issue="LANG-1259" type="update" dev="britter" due-to="Dominik Stadler">JavaDoc for ArrayUtils.isNotEmpty() is slightly misleading</action>
|
||||
|
|
|
@ -81,7 +81,22 @@ public class RandomStringUtils {
|
|||
public static String randomAscii(final int count) {
|
||||
return random(count, 32, 127, false, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <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 characters whose
|
||||
* ASCII value is between {@code 32} and {@code 126} (inclusive).</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
|
||||
*/
|
||||
public static String randomAscii(final int minLengthInclusive, final int maxLengthExclusive) {
|
||||
return randomAscii(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a random string whose length is the number of characters
|
||||
* specified.</p>
|
||||
|
@ -95,7 +110,21 @@ public class RandomStringUtils {
|
|||
public static String randomAlphabetic(final int count) {
|
||||
return random(count, true, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <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 alphabetic 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
|
||||
*/
|
||||
public static String randomAlphabetic(final int minLengthInclusive, final int maxLengthExclusive) {
|
||||
return randomAlphabetic(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a random string whose length is the number of characters
|
||||
* specified.</p>
|
||||
|
@ -109,7 +138,21 @@ public class RandomStringUtils {
|
|||
public static String randomAlphanumeric(final int count) {
|
||||
return random(count, true, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <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 alpha-numeric 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
|
||||
*/
|
||||
public static String randomAlphanumeric(final int minLengthInclusive, final int maxLengthExclusive) {
|
||||
return randomAlphanumeric(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a random string whose length is the number of characters specified.</p>
|
||||
*
|
||||
|
@ -124,7 +167,21 @@ public class RandomStringUtils {
|
|||
public static String randomGraph(final int count) {
|
||||
return random(count, 33, 126, false, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <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{Graph} 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
|
||||
*/
|
||||
public static String randomGraph(final int minLengthInclusive, final int maxLengthExclusive) {
|
||||
return randomGraph(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a random string whose length is the number of characters
|
||||
* specified.</p>
|
||||
|
@ -139,6 +196,20 @@ public class RandomStringUtils {
|
|||
return random(count, false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* <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{Digit} 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
|
||||
*/
|
||||
public static String randomNumeric(final int minLengthInclusive, final int maxLengthExclusive) {
|
||||
return randomNumeric(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a random string whose length is the number of characters specified.</p>
|
||||
*
|
||||
|
@ -154,6 +225,20 @@ public class RandomStringUtils {
|
|||
return random(count, 32, 126, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* <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
|
||||
*/
|
||||
public static String randomPrint(final int minLengthInclusive, final int maxLengthExclusive) {
|
||||
return randomPrint(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a random string whose length is the number of characters
|
||||
* specified.</p>
|
||||
|
|
|
@ -16,9 +16,14 @@
|
|||
*/
|
||||
package org.apache.commons.lang3;
|
||||
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.lessThanOrEqualTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
|
@ -299,6 +304,156 @@ public class RandomStringUtilsTest {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomAsciiRange() {
|
||||
final int expectedMinLengthInclusive = 1;
|
||||
final int expectedMaxLengthExclusive = 11;
|
||||
final String pattern = "^\\p{ASCII}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
|
||||
|
||||
int maxCreatedLength = expectedMinLengthInclusive;
|
||||
int minCreatedLength = expectedMaxLengthExclusive - 1;
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
final String s = RandomStringUtils.randomAscii(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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomAlphabeticRange() {
|
||||
final int expectedMinLengthInclusive = 1;
|
||||
final int expectedMaxLengthExclusive = 11;
|
||||
final String pattern = "^\\p{Alpha}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
|
||||
|
||||
int maxCreatedLength = expectedMinLengthInclusive;
|
||||
int minCreatedLength = expectedMaxLengthExclusive - 1;
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
final String s = RandomStringUtils.randomAlphabetic(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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomAlphanumericRange() {
|
||||
final int expectedMinLengthInclusive = 1;
|
||||
final int expectedMaxLengthExclusive = 11;
|
||||
final String pattern = "^\\p{Alnum}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
|
||||
|
||||
int maxCreatedLength = expectedMinLengthInclusive;
|
||||
int minCreatedLength = expectedMaxLengthExclusive - 1;
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
final String s = RandomStringUtils.randomAlphanumeric(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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomGraphRange() {
|
||||
final int expectedMinLengthInclusive = 1;
|
||||
final int expectedMaxLengthExclusive = 11;
|
||||
final String pattern = "^\\p{Graph}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
|
||||
|
||||
int maxCreatedLength = expectedMinLengthInclusive;
|
||||
int minCreatedLength = expectedMaxLengthExclusive - 1;
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
final String s = RandomStringUtils.randomGraph(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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomNumericRange() {
|
||||
final int expectedMinLengthInclusive = 1;
|
||||
final int expectedMaxLengthExclusive = 11;
|
||||
final String pattern = "^\\p{Digit}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$";
|
||||
|
||||
int maxCreatedLength = expectedMinLengthInclusive;
|
||||
int minCreatedLength = expectedMaxLengthExclusive - 1;
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
final String s = RandomStringUtils.randomNumeric(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));
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test homogeneity of random strings generated --
|
||||
|
|
Loading…
Reference in New Issue