From 3ac32dd142df1d1e0a5a36f224dcc73b1fa066e5 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Wed, 9 Apr 2003 14:13:03 +0000 Subject: [PATCH] Patch for RandomString bug in which counts of 0 or negative counts would cause array exceptions. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../org/apache/commons/lang/RandomStringUtils.java | 8 +++++++- .../apache/commons/lang/RandomStringUtilsTest.java | 13 ++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/commons/lang/RandomStringUtils.java b/src/java/org/apache/commons/lang/RandomStringUtils.java index 41342bc4d..c1fae8148 100644 --- a/src/java/org/apache/commons/lang/RandomStringUtils.java +++ b/src/java/org/apache/commons/lang/RandomStringUtils.java @@ -63,7 +63,7 @@ import java.util.Random; * @author Steven Caswell * @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 * (end - start) + 1 characters in the set array. + * @throws IllegalArgumentException if count < 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)' '; diff --git a/src/test/org/apache/commons/lang/RandomStringUtilsTest.java b/src/test/org/apache/commons/lang/RandomStringUtilsTest.java index e5d154645..90ab3721a 100644 --- a/src/test/org/apache/commons/lang/RandomStringUtilsTest.java +++ b/src/test/org/apache/commons/lang/RandomStringUtilsTest.java @@ -62,7 +62,7 @@ import junit.textui.TestRunner; * * @author Steven Caswell * @author Ringo De Smet - * @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[]) {