Junit-Quickcheck: create string matching pattern

I am using the pholser port. I need to generate lines that match the given pattern as \[a-zA-Z0-9\\.\\-\\\\;\\:\\_\\@\\[\\]\\^/\\|\\}\\{]* Length 40 .

I am extending the Generator class as follows:

 public class InputGenerator extends Generator<TestData> {...} 

It overloads the function:

 publicTestData generate(SourceOfRandomness random, GenerationStatus status) {...} 

Now random ones have functions like nextDouble (), nextInt () , but there is nothing for strings! How can I generate random strings matching the pattern above?

+5
source share
2 answers

Find below a snippet for a custom generator that implements the generate(..) method to return a random string matching your published template.

 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 use of the MyCharacterGenerator class.

 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(): 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\ ... 
+4
source

There is no random.nextString() , but there is a way to generate random strings in the junit-quickcheck-generators library. You can access it when creating new generators using gen().type(String.class) . However, it seems we do not have much control over this.

Here is a stupid StringBuilder generator example demonstrating how to use the String generator:

 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
source

All Articles