Fix RandomStringUtils to not throw NPE all the time
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137543 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6d98f1d5f3
commit
43db5237ab
|
@ -64,7 +64,7 @@ import java.util.Random;
|
||||||
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
||||||
* @author Phil Steitz
|
* @author Phil Steitz
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
* @version $Id: RandomStringUtils.java,v 1.19 2003/07/26 10:32:17 scolebourne Exp $
|
* @version $Id: RandomStringUtils.java,v 1.20 2003/07/31 23:24:35 scolebourne Exp $
|
||||||
*/
|
*/
|
||||||
public class RandomStringUtils {
|
public class RandomStringUtils {
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ public class RandomStringUtils {
|
||||||
* @return the random string
|
* @return the random string
|
||||||
*/
|
*/
|
||||||
public static String random(int count, int start, int end, boolean letters, boolean numbers) {
|
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 @@ public class RandomStringUtils {
|
||||||
* @param end the position in set of chars to end before
|
* @param end the position in set of chars to end before
|
||||||
* @param letters only allow letters?
|
* @param letters only allow letters?
|
||||||
* @param numbers only allow numbers?
|
* @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.
|
* If <code>null</code>, then it will use the set of all chars.
|
||||||
* @return the random string
|
* @return the random string
|
||||||
* @throws ArrayIndexOutOfBoundsException if there are not
|
* @throws ArrayIndexOutOfBoundsException if there are not
|
||||||
* <code>(end - start) + 1</code> characters in the set array.
|
* <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) {
|
public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars) {
|
||||||
return random(count,start,end,letters,numbers,set,RANDOM);
|
return random(count, start, end, letters, numbers, chars, RANDOM);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -241,7 +241,7 @@ public class RandomStringUtils {
|
||||||
* @param end the position in set of chars to end before
|
* @param end the position in set of chars to end before
|
||||||
* @param letters only allow letters?
|
* @param letters only allow letters?
|
||||||
* @param numbers only allow numbers?
|
* @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.
|
* If <code>null</code>, then it will use the set of all chars.
|
||||||
* @param random a source of randomness.
|
* @param random a source of randomness.
|
||||||
* @return the random string
|
* @return the random string
|
||||||
|
@ -249,16 +249,16 @@ public class RandomStringUtils {
|
||||||
* <code>(end - start) + 1</code> characters in the set array.
|
* <code>(end - start) + 1</code> characters in the set array.
|
||||||
* @throws IllegalArgumentException if <code>count</code> < 0.
|
* @throws IllegalArgumentException if <code>count</code> < 0.
|
||||||
*/
|
*/
|
||||||
public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] set, Random random) {
|
public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars, Random random) {
|
||||||
if( count == 0 ) {
|
if (count == 0) {
|
||||||
return "";
|
return "";
|
||||||
} else if( count < 0 ) {
|
} else if (count < 0) {
|
||||||
throw new IllegalArgumentException("Requested random string length " + count + " is less than 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;
|
end = 'z' + 1;
|
||||||
start = ' ';
|
start = ' ';
|
||||||
if(!letters && !numbers) {
|
if (!letters && !numbers) {
|
||||||
start = 0;
|
start = 0;
|
||||||
end = Integer.MAX_VALUE;
|
end = Integer.MAX_VALUE;
|
||||||
}
|
}
|
||||||
|
@ -267,20 +267,18 @@ public class RandomStringUtils {
|
||||||
StringBuffer buffer = new StringBuffer();
|
StringBuffer buffer = new StringBuffer();
|
||||||
int gap = end - start;
|
int gap = end - start;
|
||||||
|
|
||||||
while(count-- != 0) {
|
while (count-- != 0) {
|
||||||
char ch;
|
char ch;
|
||||||
if(set == null) {
|
if (chars == null) {
|
||||||
ch = (char)(random.nextInt(gap) + start);
|
ch = (char) (random.nextInt(gap) + start);
|
||||||
} else {
|
} else {
|
||||||
ch = set[random.nextInt(gap) + start];
|
ch = chars[random.nextInt(gap) + start];
|
||||||
}
|
}
|
||||||
if( (letters && numbers && Character.isLetterOrDigit(ch)) ||
|
if ((letters && numbers && Character.isLetterOrDigit(ch))
|
||||||
(letters && Character.isLetter(ch)) ||
|
|| (letters && Character.isLetter(ch))
|
||||||
(numbers && Character.isDigit(ch)) ||
|
|| (numbers && Character.isDigit(ch))
|
||||||
(!letters && !numbers)
|
|| (!letters && !numbers)) {
|
||||||
)
|
buffer.append(ch);
|
||||||
{
|
|
||||||
buffer.append( ch );
|
|
||||||
} else {
|
} else {
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
@ -296,13 +294,16 @@ public class RandomStringUtils {
|
||||||
* specified.</p>
|
* specified.</p>
|
||||||
*
|
*
|
||||||
* @param count the length of random string to create
|
* @param count the length of random string to create
|
||||||
* @param set the String containing the set of characters to use,
|
* @param chars the String containing the set of characters to use,
|
||||||
* must not be <code>null</code>
|
* may be null
|
||||||
* @return the random string
|
* @return the random string
|
||||||
* @throws NullPointerException if the set is <code>null</code>
|
* @throws IllegalArgumentException if <code>count</code> < 0.
|
||||||
*/
|
*/
|
||||||
public static String random(int count, String set) {
|
public static String random(int count, String chars) {
|
||||||
return random(count, set.toCharArray());
|
if (chars == null) {
|
||||||
|
return random(count, 0, 0, false, false, null, RANDOM);
|
||||||
|
}
|
||||||
|
return random(count, chars.toCharArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -312,12 +313,16 @@ public class RandomStringUtils {
|
||||||
* <p>Characters will be chosen from the set of characters specified.</p>
|
* <p>Characters will be chosen from the set of characters specified.</p>
|
||||||
*
|
*
|
||||||
* @param count the length of random string to create
|
* @param count the length of random string to create
|
||||||
* @param set the character array containing the set of characters to use
|
* @param chars the character array containing the set of characters to use,
|
||||||
* must not be <code>null</code>
|
* may be null
|
||||||
* @return the random string
|
* @return the random string
|
||||||
* @throws NullPointerException if the set is <code>null</code>
|
* @throws IllegalArgumentException if <code>count</code> < 0.
|
||||||
*/
|
*/
|
||||||
public static String random(int count, char[] set) {
|
public static String random(int count, char[] chars) {
|
||||||
return random(count, 0, set.length, false, false, set);
|
if (chars == null) {
|
||||||
|
return random(count, 0, 0, false, false, null, RANDOM);
|
||||||
|
}
|
||||||
|
return random(count, 0, chars.length, false, false, chars, RANDOM);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ import junit.textui.TestRunner;
|
||||||
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
|
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
|
||||||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
||||||
* @author Phil Steitz
|
* @author Phil Steitz
|
||||||
* @version $Id: RandomStringUtilsTest.java,v 1.9 2003/07/30 22:21:39 scolebourne Exp $
|
* @version $Id: RandomStringUtilsTest.java,v 1.10 2003/07/31 23:24:35 scolebourne Exp $
|
||||||
*/
|
*/
|
||||||
public class RandomStringUtilsTest extends junit.framework.TestCase {
|
public class RandomStringUtilsTest extends junit.framework.TestCase {
|
||||||
/**
|
/**
|
||||||
|
@ -157,6 +157,12 @@ public class RandomStringUtilsTest extends junit.framework.TestCase {
|
||||||
r2 = RandomStringUtils.random(50, set);
|
r2 = RandomStringUtils.random(50, set);
|
||||||
assertTrue("!r1.equals(r2)", !r1.equals(r2));
|
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";
|
set = "stuvwxyz";
|
||||||
r1 = RandomStringUtils.random(50, set.toCharArray());
|
r1 = RandomStringUtils.random(50, set.toCharArray());
|
||||||
assertEquals("random(50, \"stuvwxyz\")", 50, r1.length());
|
assertEquals("random(50, \"stuvwxyz\")", 50, r1.length());
|
||||||
|
@ -165,6 +171,12 @@ public class RandomStringUtilsTest extends junit.framework.TestCase {
|
||||||
}
|
}
|
||||||
r2 = RandomStringUtils.random(50, set);
|
r2 = RandomStringUtils.random(50, set);
|
||||||
assertTrue("!r1.equals(r2)", !r1.equals(r2));
|
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();
|
long seed = System.currentTimeMillis();
|
||||||
r1 = RandomStringUtils.random(50,0,0,true,true,null,new Random(seed));
|
r1 = RandomStringUtils.random(50,0,0,true,true,null,new Random(seed));
|
||||||
|
@ -174,13 +186,36 @@ public class RandomStringUtilsTest extends junit.framework.TestCase {
|
||||||
r1 = RandomStringUtils.random(0);
|
r1 = RandomStringUtils.random(0);
|
||||||
assertEquals("random(0).equals(\"\")", "", r1);
|
assertEquals("random(0).equals(\"\")", "", r1);
|
||||||
|
|
||||||
Exception e = null;
|
}
|
||||||
|
public void testExceptions() {
|
||||||
try {
|
try {
|
||||||
r1 = RandomStringUtils.random(-1);
|
RandomStringUtils.random(-1);
|
||||||
} catch (Exception e2) {
|
fail();
|
||||||
e = e2;
|
} catch (IllegalArgumentException ex) {}
|
||||||
}
|
try {
|
||||||
assertNotNull("random(<0) throws exception", e);
|
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) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue