diff --git a/src/changes/changes.xml b/src/changes/changes.xml index da5cd18fe..1aae92a6e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@
Characters will be chosen from the set of characters - * specified.
+ * specified by the string, must not be empty. + * If null, the set of all characters is used. * * @param count the length of random string to create * @param chars the String containing the set of characters to use, - * may be null + * may be null, but must not be empty * @return the random string - * @throws IllegalArgumentException if {@code count} < 0. + * @throws IllegalArgumentException if {@code count} < 0 or the string is empty. */ public static String random(int count, String chars) { if (chars == null) { diff --git a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java index 55ce99add..6c7b0c16d 100644 --- a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java @@ -123,9 +123,15 @@ public class RandomStringUtilsTest extends junit.framework.TestCase { r1 = RandomStringUtils.random(0); assertEquals("random(0).equals(\"\")", "", r1); - } + + public void testLANG805() { + long seed = System.currentTimeMillis(); + assertEquals("aaa", RandomStringUtils.random(3,0,0,false,false,new char[]{'a'},new Random(seed))); + } + public void testExceptions() { + final char[] DUMMY = new char[]{'a'}; // valid char array try { RandomStringUtils.random(-1); fail(); @@ -135,23 +141,31 @@ public class RandomStringUtilsTest extends junit.framework.TestCase { fail(); } catch (IllegalArgumentException ex) {} try { - RandomStringUtils.random(-1, new char[0]); + RandomStringUtils.random(-1, DUMMY); + fail(); + } catch (IllegalArgumentException ex) {} + try { + RandomStringUtils.random(1, new char[0]); // must not provide empty array => IAE fail(); } catch (IllegalArgumentException ex) {} try { RandomStringUtils.random(-1, ""); fail(); } catch (IllegalArgumentException ex) {} + try { + RandomStringUtils.random(-1, (String)null); + fail(); + } catch (IllegalArgumentException ex) {} try { RandomStringUtils.random(-1, 'a', 'z', false, false); fail(); } catch (IllegalArgumentException ex) {} try { - RandomStringUtils.random(-1, 'a', 'z', false, false, new char[0]); + RandomStringUtils.random(-1, 'a', 'z', false, false, DUMMY); fail(); } catch (IllegalArgumentException ex) {} try { - RandomStringUtils.random(-1, 'a', 'z', false, false, new char[0], new Random()); + RandomStringUtils.random(-1, 'a', 'z', false, false, DUMMY, new Random()); fail(); } catch (IllegalArgumentException ex) {} }