Patch for RandomString bug in which counts of 0 or negative counts would cause array exceptions.

Submitted by:	ville.skytta@iki.fi (Ville Skytt�)


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137289 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2003-04-09 14:13:03 +00:00
parent 082cde8df8
commit 3ac32dd142
2 changed files with 19 additions and 2 deletions

View File

@ -63,7 +63,7 @@ import java.util.Random;
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @author Stephen Colebourne
* @since 1.0
* @version $Id: RandomStringUtils.java,v 1.9 2003/03/23 18:00:59 scolebourne Exp $
* @version $Id: RandomStringUtils.java,v 1.10 2003/04/09 14:13:03 bayard Exp $
*/
public class RandomStringUtils {
@ -246,8 +246,14 @@ public class RandomStringUtils {
* @return the random string
* @throws ArrayIndexOutOfBoundsException if there are not
* <code>(end - start) + 1</code> characters in the set array.
* @throws IllegalArgumentException if <code>count</code> &lt; 0.
*/
public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] set, Random random) {
if( count == 0 ) {
return "";
} else if( count < 0 ) {
throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
}
if( (start == 0) && (end == 0) ) {
end = (int)'z';
start = (int)' ';

View File

@ -62,7 +62,7 @@ import junit.textui.TestRunner;
*
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @version $Id: RandomStringUtilsTest.java,v 1.4 2003/03/23 21:50:58 scolebourne Exp $
* @version $Id: RandomStringUtilsTest.java,v 1.5 2003/04/09 14:13:03 bayard Exp $
*/
public class RandomStringUtilsTest extends junit.framework.TestCase {
/**
@ -154,6 +154,17 @@ public class RandomStringUtilsTest extends junit.framework.TestCase {
r1 = RandomStringUtils.random(50,0,0,true,true,null,new Random(seed));
r2 = RandomStringUtils.random(50,0,0,true,true,null,new Random(seed));
assertEquals("r1.equals(r2)", r1, r2);
r1 = RandomStringUtils.random(0);
assertEquals("random(0).equals(\"\")", "", r1);
Exception e = null;
try {
r1 = RandomStringUtils.random(-1);
} catch (Exception e2) {
e = e2;
}
assertNotNull("random(<0) throws exception", e);
}
public static void main(String args[]) {