You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pa...@apache.org on 2017/06/18 07:03:04 UTC
[text] TEXT-91: RandomStringGenerator should be able to generate a
String with a random length (closes #51)
Repository: commons-text
Updated Branches:
refs/heads/master 6276d029b -> 2035ed14f
TEXT-91: RandomStringGenerator should be able to generate a String with a random length (closes #51)
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/2035ed14
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/2035ed14
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/2035ed14
Branch: refs/heads/master
Commit: 2035ed14fc77342909a35c1b61b28e8be66f49e0
Parents: 6276d02
Author: Pascal Schumacher <pa...@gmx.net>
Authored: Wed Jun 14 22:34:09 2017 +0200
Committer: Pascal Schumacher <pa...@gmx.net>
Committed: Sun Jun 18 09:02:55 2017 +0200
----------------------------------------------------------------------
src/changes/changes.xml | 1 +
.../commons/text/RandomStringGenerator.java | 21 +++++++++++++++++
.../commons/text/RandomStringGeneratorTest.java | 24 ++++++++++++++++++++
3 files changed, 46 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-text/blob/2035ed14/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 86abf92..a97b474 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -46,6 +46,7 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<release version="1.2" date="tbd" description="tbd">
+ <action issue="TEXT-91" type="add" dev="pschumacher">RandomStringGenerator should be able to generate a String with a random length</action>
<action issue="TEXT-92" type="update" dev="pschumacher">Update commons-lang dependency to version 3.6</action>
<action issue="TEXT-83" type="update" dev="chtompki" due-to="Amey Jadiye">Document that commons-csv should be used in preference to CsvTranslators</action>
<action issue="TEXT-67" type="update" dev="kinow">NumericEntityUnescaper.options - fix TODO</action>
http://git-wip-us.apache.org/repos/asf/commons-text/blob/2035ed14/src/main/java/org/apache/commons/text/RandomStringGenerator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/RandomStringGenerator.java b/src/main/java/org/apache/commons/text/RandomStringGenerator.java
index 6df21aa..37aec28 100644
--- a/src/main/java/org/apache/commons/text/RandomStringGenerator.java
+++ b/src/main/java/org/apache/commons/text/RandomStringGenerator.java
@@ -177,6 +177,27 @@ public final class RandomStringGenerator {
}
/**
+ * Generates a random string, containing between the minimum (inclusive) and the maximum (inclusive)
+ * number of code points.
+ *
+ * @param minLengthInclusive
+ * the minimum (inclusive) number of code points to generate
+ * @param maxLengthInclusive
+ * the maximum (inclusive) number of code points to generate
+ * @return the generated string
+ * @throws IllegalArgumentException
+ * if {@code minLengthInclusive < 0}, or {@code maxLengthInclusive < minLengthInclusive}
+ * @see RandomStringGenerator#generate(int)
+ * @since 1.2
+ */
+ public String generate(final int minLengthInclusive, final int maxLengthInclusive) {
+ Validate.isTrue(minLengthInclusive >= 0, "Minimum length %d is smaller than zero.", minLengthInclusive);
+ Validate.isTrue(minLengthInclusive <= maxLengthInclusive,
+ "Maximum length %d is smaller than minimum length %d.", maxLengthInclusive, minLengthInclusive);
+ return generate(generateRandomNumber(minLengthInclusive, maxLengthInclusive));
+ }
+
+ /**
* <p>A builder for generating {@code RandomStringGenerator} instances.</p>
* <p>The behaviour of a generator is controlled by properties set by this
* builder. Each property has a default value, which can be overridden by
http://git-wip-us.apache.org/repos/asf/commons-text/blob/2035ed14/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java b/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java
index 86537fa..ff50ac4 100644
--- a/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java
+++ b/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java
@@ -18,6 +18,9 @@ package org.apache.commons.text;
import org.junit.Test;
+import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.*;
/**
@@ -45,6 +48,18 @@ public class RandomStringGeneratorTest {
generator.generate(-1);
}
+ @Test(expected = IllegalArgumentException.class)
+ public void testGenerateMinMaxLengthInvalidLength() {
+ RandomStringGenerator generator = new RandomStringGenerator.Builder().build();
+ generator.generate(-1, 0);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGenerateMinMaxLengthMinGreaterThanMax() {
+ RandomStringGenerator generator = new RandomStringGenerator.Builder().build();
+ generator.generate(1, 0);
+ }
+
private static int codePointLength(String s) {
return s.codePointCount(0, s.length());
}
@@ -57,6 +72,15 @@ public class RandomStringGeneratorTest {
assertEquals(length, codePointLength(str));
}
+ @Test
+ public void testGenerateMinMaxLength() {
+ final int minLength = 0;
+ final int maxLength = 3;
+ RandomStringGenerator generator = new RandomStringGenerator.Builder().build();
+ String str = generator.generate(minLength, maxLength);
+ assertThat(codePointLength(str), allOf(greaterThanOrEqualTo(0), lessThanOrEqualTo(3)));
+ }
+
@Test(expected = IllegalArgumentException.class)
public void testBadMinimumCodePoint() {
new RandomStringGenerator.Builder().withinRange(-1, 1);