commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang RandomStringUtilsTest.java
Date Wed, 09 Apr 2003 14:13:03 GMT
bayard      2003/04/09 07:13:03

  Modified:    lang/src/java/org/apache/commons/lang RandomStringUtils.java
               lang/src/test/org/apache/commons/lang
                        RandomStringUtilsTest.java
  Log:
  Patch for RandomString bug in which counts of 0 or negative counts would cause array exceptions.
  
  Submitted by:	ville.skytta@iki.fi (Ville Skyttä)
  
  Revision  Changes    Path
  1.10      +7 -1      jakarta-commons/lang/src/java/org/apache/commons/lang/RandomStringUtils.java
  
  Index: RandomStringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/RandomStringUtils.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- RandomStringUtils.java	23 Mar 2003 18:00:59 -0000	1.9
  +++ RandomStringUtils.java	9 Apr 2003 14:13:03 -0000	1.10
  @@ -246,8 +246,14 @@
        * @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)' ';
  
  
  
  1.5       +12 -1     jakarta-commons/lang/src/test/org/apache/commons/lang/RandomStringUtilsTest.java
  
  Index: RandomStringUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/RandomStringUtilsTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- RandomStringUtilsTest.java	23 Mar 2003 21:50:58 -0000	1.4
  +++ RandomStringUtilsTest.java	9 Apr 2003 14:13:03 -0000	1.5
  @@ -154,6 +154,17 @@
           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[]) {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message