You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2003/02/02 04:46:13 UTC
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang RandomStringUtilsTest.java
bayard 2003/02/01 19:46:13
Modified: lang/src/java/org/apache/commons/lang RandomStringUtils.java
lang/src/test/org/apache/commons/lang
RandomStringUtilsTest.java
Log:
Applied patch from Tomasz Skutnik which allows a Random object to be passed
in. This means that method calls to RandomStringUtils can be predictable.
Submitted by: Tomasz Skutnik <To...@e-point.pl>
Revision Changes Path
1.8 +38 -6 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.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- RandomStringUtils.java 23 Dec 2002 00:32:24 -0000 1.7
+++ RandomStringUtils.java 2 Feb 2003 03:46:13 -0000 1.8
@@ -191,9 +191,35 @@
public static String random(int count, int start, int end, boolean letters, boolean numbers) {
return random(count, start, end, letters, numbers, null);
}
-
+
/**
- * <p>Creates a random string based on a variety of options.</p>
+ * <p>Creates a random string based on a variety of options, using
+ * default source of randomness.</p>
+ *
+ * This method has exactly the same semantics as {@link
+ * #random(int,int,int,boolean,boolean,char[],Random)}, but
+ * instead of depending on internal source of randomness ({@link
+ * #RANDOM}) it uses externally supplied instance of {@link
+ * Random} class.
+ *
+ * @param count length of random string to create
+ * @param start position in set of chars to start at
+ * @param end position in set of chars to end before
+ * @param letters only allow letters?
+ * @param numbers only allow numbers?
+ * @param set 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);
+ }
+
+ /**
+ * <p>Creates a random string based on a variety of options, using
+ * supplied source of randomness.</p>
*
* <p>If start and end are both <code>0</code>, start and end are set
* to <code>' '</code> and <code>'z'</code>, the ASCII printable
@@ -204,6 +230,11 @@
* <p>If set is not <code>null</code>, characters between start and
* end are chosen.</p>
*
+ * <p>As a source of randomness is used supplied {@link Random}
+ * instance. This makes method behave predictively, and allows
+ * usage of <code>RandomStringUtils</code> in situations that need
+ * repetitive behaviour.</p>
+ *
* @param count length of random string to create
* @param start position in set of chars to start at
* @param end position in set of chars to end before
@@ -211,11 +242,12 @@
* @param numbers only allow numbers?
* @param set set of chars to choose randoms from. If <code>null</code>,
* then it will use the set of all chars.
+ * @param random source of randomness.
* @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) {
+ public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] set, Random random) {
if( (start == 0) && (end == 0) ) {
end = (int)'z';
start = (int)' ';
@@ -231,9 +263,9 @@
while(count-- != 0) {
char ch;
if(set == null) {
- ch = (char)(RANDOM.nextInt(gap) + start);
+ ch = (char)(random.nextInt(gap) + start);
} else {
- ch = set[RANDOM.nextInt(gap) + start];
+ ch = set[random.nextInt(gap) + start];
}
if( (letters && numbers && Character.isLetterOrDigit(ch)) ||
(letters && Character.isLetter(ch)) ||
1.3 +8 -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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- RandomStringUtilsTest.java 8 Oct 2002 19:01:39 -0000 1.2
+++ RandomStringUtilsTest.java 2 Feb 2003 03:46:13 -0000 1.3
@@ -54,6 +54,8 @@
* <http://www.apache.org/>.
*/
+import java.util.Random;
+
import junit.framework.*;
import junit.textui.TestRunner;
/**
@@ -148,6 +150,11 @@
}
r2 = RandomStringUtils.random(50, set);
assertTrue("!r1.equals(r2)", !r1.equals(r2));
+
+ long seed = System.currentTimeMillis();
+ 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);
}
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