LANG-805 RandomStringUtils.random(count, 0, 0, false, false, universe, random) always throws java.lang.ArrayIndexOutOfBoundsException
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1348422 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1971d3ed8b
commit
27bcbcc728
|
@ -22,6 +22,7 @@
|
|||
<body>
|
||||
|
||||
<release version="3.2" date="TBA" description="Next release">
|
||||
<action issue="LANG-805" type="fix">RandomStringUtils.random(count, 0, 0, false, false, universe, random) always throws java.lang.ArrayIndexOutOfBoundsException</action>
|
||||
<action issue="LANG-802" type="fix">LocaleUtils - unnecessary recursive call in SyncAvoid class.</action>
|
||||
<action issue="LANG-800" type="fix">Javadoc bug in DateUtils#ceiling for Calendar and Object versions.</action>
|
||||
<action issue="LANG-798" type="update">Use generics in SerializationUtils</action>
|
||||
|
|
|
@ -211,13 +211,13 @@ public class RandomStringUtils {
|
|||
* @param end the position in set of chars to end before
|
||||
* @param letters only allow letters?
|
||||
* @param numbers only allow numbers?
|
||||
* @param chars the set of chars to choose randoms from.
|
||||
* @param chars the set of chars to choose randoms from, must not be empty.
|
||||
* If {@code null}, then it will use the set of all chars.
|
||||
* @param random a source of randomness.
|
||||
* @return the random string
|
||||
* @throws ArrayIndexOutOfBoundsException if there are not
|
||||
* {@code (end - start) + 1} characters in the set array.
|
||||
* @throws IllegalArgumentException if {@code count} < 0.
|
||||
* @throws IllegalArgumentException if {@code count} < 0 or the provided chars array is empty.
|
||||
* @since 2.0
|
||||
*/
|
||||
public static String random(int count, int start, int end, boolean letters, boolean numbers,
|
||||
|
@ -227,12 +227,20 @@ public class RandomStringUtils {
|
|||
} else if (count < 0) {
|
||||
throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
|
||||
}
|
||||
if (chars != null && chars.length == 0) {
|
||||
throw new IllegalArgumentException("The chars array must not be empty");
|
||||
}
|
||||
|
||||
if (start == 0 && end == 0) {
|
||||
end = 'z' + 1;
|
||||
start = ' ';
|
||||
if (!letters && !numbers) {
|
||||
start = 0;
|
||||
end = Integer.MAX_VALUE;
|
||||
if (chars != null) {
|
||||
end = chars.length;
|
||||
} else {
|
||||
if (!letters && !numbers) {
|
||||
end = Integer.MAX_VALUE;
|
||||
} else {
|
||||
end = 'z' + 1;
|
||||
start = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -285,13 +293,14 @@ public class RandomStringUtils {
|
|||
* specified.</p>
|
||||
*
|
||||
* <p>Characters will be chosen from the set of characters
|
||||
* specified.</p>
|
||||
* specified by the string, must not be empty.
|
||||
* If null, the set of all characters is used.</p>
|
||||
*
|
||||
* @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) {
|
||||
|
|
|
@ -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) {}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue