Repository: commons-lang Updated Branches: refs/heads/master 9625891a7 -> f26f04dc6
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. Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/1a002c67 Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/1a002c67 Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/1a002c67 Branch: refs/heads/master Commit: 1a002c67f2d3c198411a62165e53b31d6b414aa3 Parents: 9625891 Author: Caleb Cushing <xenoterrac...@gmail.com> Authored: Sat Jun 27 13:52:41 2015 -0500 Committer: pascalschumacher <pascalschumac...@gmx.net> Committed: Sun May 29 09:39:09 2016 +0200 ---------------------------------------------------------------------- .../apache/commons/lang3/RandomStringUtils.java | 24 ++++++++++++++++++++ .../commons/lang3/RandomStringUtilsTest.java | 16 +++++++++++++ 2 files changed, 40 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/1a002c67/src/main/java/org/apache/commons/lang3/RandomStringUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java index cc8d70b..662249f 100644 --- a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java +++ b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java @@ -111,6 +111,18 @@ public class RandomStringUtils { } /** + * <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> * @@ -125,6 +137,18 @@ public class RandomStringUtils { } /** + * <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> * http://git-wip-us.apache.org/repos/asf/commons-lang/blob/1a002c67/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java index d251db4..7cd71e8 100644 --- a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java @@ -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());