You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/08/01 01:24:35 UTC

cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang RandomStringUtilsTest.java

scolebourne    2003/07/31 16:24:35

  Modified:    lang/src/java/org/apache/commons/lang RandomStringUtils.java
               lang/src/test/org/apache/commons/lang
                        RandomStringUtilsTest.java
  Log:
  Fix RandomStringUtils to not throw NPE all the time
  
  Revision  Changes    Path
  1.20      +37 -32    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.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- RandomStringUtils.java	26 Jul 2003 10:32:17 -0000	1.19
  +++ RandomStringUtils.java	31 Jul 2003 23:24:35 -0000	1.20
  @@ -190,7 +190,7 @@
        * @return the random string
        */
       public static String random(int count, int start, int end, boolean letters, boolean numbers) {
  -        return random(count, start, end, letters, numbers, null);
  +        return random(count, start, end, letters, numbers, null, RANDOM);
       }
   
       /**
  @@ -207,14 +207,14 @@
        * @param end  the position in set of chars to end before
        * @param letters  only allow letters?
        * @param numbers  only allow numbers?
  -     * @param set  the set of chars to choose randoms from.
  +     * @param chars  the set of chars to choose randoms from.
        *  If <code>null</code>, then it will use the set of all chars.
        * @return the random string
        * @throws ArrayIndexOutOfBoundsException if there are not
        *  <code>(end - start) + 1</code> characters in the set array.
        */
  -    public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] set) {
  -        return random(count,start,end,letters,numbers,set,RANDOM);
  +    public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars) {
  +        return random(count, start, end, letters, numbers, chars, RANDOM);
       }
   
       /**
  @@ -241,7 +241,7 @@
        * @param end  the position in set of chars to end before
        * @param letters  only allow letters?
        * @param numbers  only allow numbers?
  -     * @param set  the set of chars to choose randoms from.
  +     * @param chars  the set of chars to choose randoms from.
        *  If <code>null</code>, then it will use the set of all chars.
        * @param random  a source of randomness.
        * @return the random string
  @@ -249,16 +249,16 @@
        *  <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 ) {
  +    public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars, Random random) {
  +        if (count == 0) {
               return "";
  -        } else if( count < 0 ) {
  +        } else if (count < 0) {
               throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
           }
  -        if( (start == 0) && (end == 0) ) {
  +        if ((start == 0) && (end == 0)) {
               end = 'z' + 1;
               start = ' ';
  -            if(!letters && !numbers) {
  +            if (!letters && !numbers) {
                   start = 0;
                   end = Integer.MAX_VALUE;
               }
  @@ -267,20 +267,18 @@
           StringBuffer buffer = new StringBuffer();
           int gap = end - start;
   
  -        while(count-- != 0) {
  +        while (count-- != 0) {
               char ch;
  -            if(set == null) {
  -                ch = (char)(random.nextInt(gap) + start);
  +            if (chars == null) {
  +                ch = (char) (random.nextInt(gap) + start);
               } else {
  -                ch = set[random.nextInt(gap) + start];
  +                ch = chars[random.nextInt(gap) + start];
               }
  -            if( (letters && numbers && Character.isLetterOrDigit(ch)) ||
  -                (letters && Character.isLetter(ch)) ||
  -                (numbers && Character.isDigit(ch)) ||
  -                (!letters && !numbers)
  -              ) 
  -            {
  -                buffer.append( ch );
  +            if ((letters && numbers && Character.isLetterOrDigit(ch))
  +                || (letters && Character.isLetter(ch))
  +                || (numbers && Character.isDigit(ch))
  +                || (!letters && !numbers)) {
  +                buffer.append(ch);
               } else {
                   count++;
               }
  @@ -296,13 +294,16 @@
        * specified.</p>
        *
        * @param count  the length of random string to create
  -     * @param set  the String containing the set of characters to use,
  -     *  must not be <code>null</code>
  +     * @param chars  the String containing the set of characters to use,
  +     *  may be null
        * @return the random string
  -     * @throws NullPointerException if the set is <code>null</code>
  +     * @throws IllegalArgumentException if <code>count</code> &lt; 0.
        */
  -    public static String random(int count, String set) {
  -        return random(count, set.toCharArray());
  +    public static String random(int count, String chars) {
  +        if (chars == null) {
  +            return random(count, 0, 0, false, false, null, RANDOM);
  +        }
  +        return random(count, chars.toCharArray());
       }
   
       /**
  @@ -312,12 +313,16 @@
        * <p>Characters will be chosen from the set of characters specified.</p>
        *
        * @param count  the length of random string to create
  -     * @param set  the character array containing the set of characters to use
  -     *  must not be <code>null</code>
  +     * @param chars  the character array containing the set of characters to use,
  +     *  may be null
        * @return the random string
  -     * @throws NullPointerException if the set is <code>null</code>
  +     * @throws IllegalArgumentException if <code>count</code> &lt; 0.
        */
  -    public static String random(int count, char[] set) {
  -        return random(count, 0, set.length, false, false, set);
  +    public static String random(int count, char[] chars) {
  +        if (chars == null) {
  +            return random(count, 0, 0, false, false, null, RANDOM);
  +        }
  +        return random(count, 0, chars.length, false, false, chars, RANDOM);
       }
  +    
   }
  
  
  
  1.10      +42 -7     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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- RandomStringUtilsTest.java	30 Jul 2003 22:21:39 -0000	1.9
  +++ RandomStringUtilsTest.java	31 Jul 2003 23:24:35 -0000	1.10
  @@ -157,6 +157,12 @@
           r2 = RandomStringUtils.random(50, set);
           assertTrue("!r1.equals(r2)", !r1.equals(r2));
           
  +        r1 = RandomStringUtils.random(50, (String) null);
  +        assertEquals("random(50) length", 50, r1.length());
  +        r2 = RandomStringUtils.random(50, (String) null);
  +        assertEquals("random(50) length", 50, r2.length());
  +        assertTrue("!r1.equals(r2)", !r1.equals(r2));
  +        
           set = "stuvwxyz";
           r1 = RandomStringUtils.random(50, set.toCharArray());
           assertEquals("random(50, \"stuvwxyz\")", 50, r1.length());
  @@ -165,6 +171,12 @@
           }
           r2 = RandomStringUtils.random(50, set);
           assertTrue("!r1.equals(r2)", !r1.equals(r2));
  +        
  +        r1 = RandomStringUtils.random(50, (char[]) null);
  +        assertEquals("random(50) length", 50, r1.length());
  +        r2 = RandomStringUtils.random(50, (char[]) null);
  +        assertEquals("random(50) length", 50, r2.length());
  +        assertTrue("!r1.equals(r2)", !r1.equals(r2));
   
           long seed = System.currentTimeMillis();
           r1 = RandomStringUtils.random(50,0,0,true,true,null,new Random(seed));
  @@ -174,13 +186,36 @@
           r1 = RandomStringUtils.random(0);
           assertEquals("random(0).equals(\"\")", "", r1);
   
  -        Exception e = null;
  +    }
  +    public void testExceptions() {
           try {
  -            r1 = RandomStringUtils.random(-1);
  -        } catch (Exception e2) {
  -            e = e2;
  -        }
  -        assertNotNull("random(<0) throws exception", e);
  +            RandomStringUtils.random(-1);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            RandomStringUtils.random(-1, true, true);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            RandomStringUtils.random(-1, new char[0]);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            RandomStringUtils.random(-1, "");
  +            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]);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            RandomStringUtils.random(-1, 'a', 'z', false, false, new char[0], new Random());
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
       }
       
       /**
  
  
  

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