Author: sebb
Date: Sat Jun  9 14:58:34 2012
New Revision: 1348422

URL: http://svn.apache.org/viewvc?rev=1348422&view=rev
Log:
LANG-805 RandomStringUtils.random(count, 0, 0, false, false, universe, random) 
always throws java.lang.ArrayIndexOutOfBoundsException

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
    
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1348422&r1=1348421&r2=1348422&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml Sat Jun  9 14:58:34 2012
@@ -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>  

Modified: 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/RandomStringUtils.java?rev=1348422&r1=1348421&r2=1348422&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
 (original)
+++ 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
 Sat Jun  9 14:58:34 2012
@@ -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} &lt; 0.
+     * @throws IllegalArgumentException if {@code count} &lt; 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} &lt; 0.
+     * @throws IllegalArgumentException if {@code count} &lt; 0 or the string 
is empty.
      */
     public static String random(int count, String chars) {
         if (chars == null) {

Modified: 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java?rev=1348422&r1=1348421&r2=1348422&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
 (original)
+++ 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
 Sat Jun  9 14:58:34 2012
@@ -123,9 +123,15 @@ public class RandomStringUtilsTest exten
 
         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,7 +141,11 @@ public class RandomStringUtilsTest exten
             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 {
@@ -143,15 +153,19 @@ public class RandomStringUtilsTest exten
             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) {}
     }


Reply via email to