I am using pholser's port. I have to generate strings matching a given pattern like \[a-zA-Z0-9\\.\\-\\\\;\\:\\_\\@\\[\\]\\^/\\|\\}\\{]* Length 40
.
我正在使用pholser的端口。我必须生成与\ [a-zA-Z0-9 \\。\\ - \\\\; \\:\\ _ \ | \\} \\ {] *长度40。
I extend the Generator class as:
我将Generator类扩展为:
public class InputGenerator extends Generator<TestData> {...}
It overloads a function:
它重载了一个函数:
publicTestData generate(SourceOfRandomness random, GenerationStatus status) {...}
Now, random has functions like nextDouble(), nextInt() but there is nothing for strings! How can I generate random strings matching the above pattern?
现在,random有像nextDouble(),nextInt()这样的函数,但没有字符串!如何生成与上述模式匹配的随机字符串?
2 个解决方案
#1
Find below snippet for a custom generator which implement the generate(..)
method to return a random string matching your posted pattern.
查找自定义生成器的以下片段,该生成器实现generate(..)方法以返回与您发布的模式匹配的随机字符串。
public class MyCharacterGenerator extends Generator<String> {
private static final String LOWERCASE_CHARS = "abcdefghijklmnopqrstuvwxyz";
private static final String UPPERCASE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String NUMBERS = "0123456789";
private static final String SPECIAL_CHARS = ".-\\;:_@[]^/|}{";
private static final String ALL_MY_CHARS = LOWERCASE_CHARS
+ UPPERCASE_CHARS + NUMBERS + SPECIAL_CHARS;
public static final int CAPACITY = 40;
public MyCharacterGenerator () {
super(String.class);
}
@Override
public String generate(SourceOfRandomness random, GenerationStatus status) {
StringBuilder sb = new StringBuilder(CAPACITY);
for (int i = 0; i < CAPACITY; i++) {
int randomIndex = random.nextInt(ALL_MY_CHARS.length());
sb.append(ALL_MY_CHARS.charAt(randomIndex));
}
return sb.toString();
}
}
edit A simple unit test to demonstrate the usage of the MyCharacterGenerator
class.
编辑一个简单的单元测试,用于演示MyCharacterGenerator类的用法。
import com.pholser.junit.quickcheck.ForAll;
import com.pholser.junit.quickcheck.From;
import static org.junit.Assert.assertTrue;
import org.junit.contrib.theories.Theories;
import org.junit.contrib.theories.Theory;
import org.junit.runner.RunWith;
@RunWith(Theories.class)
public class MyCharacterGeneratorTest {
@Theory
public void shouldHold(@ForAll @From(MyCharacterGenerator.class) String s) {
// here you should add your unit test which uses the generated output
//
// assertTrue(doMyUnitTest(s) == expectedResult);
// the below lines only for demonstration and currently
// check that the generated random has the expected
// length and matches the expected pattern
System.out.println("shouldHold(): " + s);
assertTrue(s.length() == MyCharacterGenerator.CAPACITY);
assertTrue(s.matches("[a-zA-Z0-9.\\-\\\\;:_@\\[\\]^/|}{]*"));
}
}
sample output generated by shouldHold
shouldHold生成的样本输出
shouldHold(): MD}o/LAkW/hbJVWPGdI;:RHpwo_T.lGs^DOFwu2.
shouldHold(): IT_O{8Umhkz{@PY:pmK6}Cb[Wc19GqGZjWVa@4li
shouldHold(): KQwpEz.CW28vy_/WJR3Lx2.tRC6uLIjOTQtYP/VR
shouldHold(): pc2_T4hLdZpK78UfcVmU\RTe9WaJBSGJ}5v@z[Z\
...
#2
There is no random.nextString()
, but there is a way to generate random strings within junit-quickcheck-generators library. You can access it when creating new generators using gen().type(String.class)
. However, it seems we don't have much control over it.
没有random.nextString(),但有一种方法可以在junit-quickcheck-generators库中生成随机字符串。使用gen()。type(String.class)创建新生成器时可以访问它。但是,似乎我们对它没有多少控制权。
Here is a silly example of a StringBuilder
generator to demonstrate how to use the String
generator:
下面是StringBuilder生成器的一个愚蠢示例,用于演示如何使用String生成器:
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
public class StringBuilderGenerator extends Generator<StringBuilder> {
public StringBuilderGenerator() {
super(StringBuilder.class);
}
@Override
public StringBuilder generate(SourceOfRandomness random, GenerationStatus status) {
String s = gen().type(String.class).generate(random, status);
return new StringBuilder(s);
}
}
#1
Find below snippet for a custom generator which implement the generate(..)
method to return a random string matching your posted pattern.
查找自定义生成器的以下片段,该生成器实现generate(..)方法以返回与您发布的模式匹配的随机字符串。
public class MyCharacterGenerator extends Generator<String> {
private static final String LOWERCASE_CHARS = "abcdefghijklmnopqrstuvwxyz";
private static final String UPPERCASE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String NUMBERS = "0123456789";
private static final String SPECIAL_CHARS = ".-\\;:_@[]^/|}{";
private static final String ALL_MY_CHARS = LOWERCASE_CHARS
+ UPPERCASE_CHARS + NUMBERS + SPECIAL_CHARS;
public static final int CAPACITY = 40;
public MyCharacterGenerator () {
super(String.class);
}
@Override
public String generate(SourceOfRandomness random, GenerationStatus status) {
StringBuilder sb = new StringBuilder(CAPACITY);
for (int i = 0; i < CAPACITY; i++) {
int randomIndex = random.nextInt(ALL_MY_CHARS.length());
sb.append(ALL_MY_CHARS.charAt(randomIndex));
}
return sb.toString();
}
}
edit A simple unit test to demonstrate the usage of the MyCharacterGenerator
class.
编辑一个简单的单元测试,用于演示MyCharacterGenerator类的用法。
import com.pholser.junit.quickcheck.ForAll;
import com.pholser.junit.quickcheck.From;
import static org.junit.Assert.assertTrue;
import org.junit.contrib.theories.Theories;
import org.junit.contrib.theories.Theory;
import org.junit.runner.RunWith;
@RunWith(Theories.class)
public class MyCharacterGeneratorTest {
@Theory
public void shouldHold(@ForAll @From(MyCharacterGenerator.class) String s) {
// here you should add your unit test which uses the generated output
//
// assertTrue(doMyUnitTest(s) == expectedResult);
// the below lines only for demonstration and currently
// check that the generated random has the expected
// length and matches the expected pattern
System.out.println("shouldHold(): " + s);
assertTrue(s.length() == MyCharacterGenerator.CAPACITY);
assertTrue(s.matches("[a-zA-Z0-9.\\-\\\\;:_@\\[\\]^/|}{]*"));
}
}
sample output generated by shouldHold
shouldHold生成的样本输出
shouldHold(): MD}o/LAkW/hbJVWPGdI;:RHpwo_T.lGs^DOFwu2.
shouldHold(): IT_O{8Umhkz{@PY:pmK6}Cb[Wc19GqGZjWVa@4li
shouldHold(): KQwpEz.CW28vy_/WJR3Lx2.tRC6uLIjOTQtYP/VR
shouldHold(): pc2_T4hLdZpK78UfcVmU\RTe9WaJBSGJ}5v@z[Z\
...
#2
There is no random.nextString()
, but there is a way to generate random strings within junit-quickcheck-generators library. You can access it when creating new generators using gen().type(String.class)
. However, it seems we don't have much control over it.
没有random.nextString(),但有一种方法可以在junit-quickcheck-generators库中生成随机字符串。使用gen()。type(String.class)创建新生成器时可以访问它。但是,似乎我们对它没有多少控制权。
Here is a silly example of a StringBuilder
generator to demonstrate how to use the String
generator:
下面是StringBuilder生成器的一个愚蠢示例,用于演示如何使用String生成器:
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
public class StringBuilderGenerator extends Generator<StringBuilder> {
public StringBuilderGenerator() {
super(StringBuilder.class);
}
@Override
public StringBuilder generate(SourceOfRandomness random, GenerationStatus status) {
String s = gen().type(String.class).generate(random, status);
return new StringBuilder(s);
}
}