LANG-1225: Add RandomStringUtils#randomGraph and #randomPrint which match corresponding regular expression class
These are useful over randomAscii because they do not contain the DEL character but otherwise contain the full range of ASCII printing characters, and optionally include whitespace. This is useful for testing user defined inputs where characters like spaces, angle brakets, semicolons, dashes, etc. can cause issues.
This commit is contained in:
parent
9625891a7b
commit
1a002c67f2
|
@ -110,6 +110,18 @@ public class RandomStringUtils {
|
|||
return random(count, true, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a random string whose length is the number of characters specified.</p>
|
||||
*
|
||||
* <p>Characters will be chosen from the set of characters which match the POSIX [:graph:] regular expression.</p>
|
||||
*
|
||||
* @param count the length of random string to create
|
||||
* @return the random string
|
||||
*/
|
||||
public static String randomGraph(final int count) {
|
||||
return random(count, 33, 126, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a random string whose length is the number of characters
|
||||
* specified.</p>
|
||||
|
@ -124,6 +136,18 @@ public class RandomStringUtils {
|
|||
return random(count, false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a random string whose length is the number of characters specified.</p>
|
||||
*
|
||||
* <p>Characters will be chosen from the set of characters which match the POSIX [:print:] regular expression.</p>
|
||||
*
|
||||
* @param count the length of random string to create
|
||||
* @return the random string
|
||||
*/
|
||||
public static String randomPrint(final int count) {
|
||||
return random(count, 32, 126, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a random string whose length is the number of characters
|
||||
* specified.</p>
|
||||
|
|
|
@ -81,6 +81,14 @@ public class RandomStringUtilsTest {
|
|||
r2 = RandomStringUtils.randomAlphabetic(50);
|
||||
assertTrue("!r1.equals(r2)", !r1.equals(r2));
|
||||
|
||||
r1 = RandomStringUtils.randomGraph(50);
|
||||
assertEquals("randomGraph(50) length", 50, r1.length());
|
||||
for(int i = 0; i < r1.length(); i++) {
|
||||
assertTrue("char between 33 and 126", r1.charAt(i) >= 33 && r1.charAt(i) <= 126);
|
||||
}
|
||||
r2 = RandomStringUtils.randomGraph(50);
|
||||
assertTrue("!r1.equals(r2)", !r1.equals(r2));
|
||||
|
||||
r1 = RandomStringUtils.randomNumeric(50);
|
||||
assertEquals("randomNumeric(50)", 50, r1.length());
|
||||
for(int i = 0; i < r1.length(); i++) {
|
||||
|
@ -89,6 +97,14 @@ public class RandomStringUtilsTest {
|
|||
r2 = RandomStringUtils.randomNumeric(50);
|
||||
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));
|
||||
|
||||
String set = "abcdefg";
|
||||
r1 = RandomStringUtils.random(50, set);
|
||||
assertEquals("random(50, \"abcdefg\")", 50, r1.length());
|
||||
|
|
Loading…
Reference in New Issue