Skip to content

Commit

Permalink
make random-beans-validation more random (fixes j-easy#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSchumacher committed Feb 15, 2017
1 parent d4a47dd commit 66e396a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Random;

class DecimaMinMaxAnnotationHandler implements BeanValidationAnnotationHandler {

private long seed;
private final Random random;

public DecimaMinMaxAnnotationHandler(long seed) {
this.seed = seed;
random = new Random(seed);
}

public Randomizer<?> getRandomizer(Field field) {
Expand All @@ -62,49 +63,49 @@ public Randomizer<?> getRandomizer(Field field) {
return new ByteRangeRandomizer(
minValue == null ? null : minValue.byteValue(),
maxValue == null ? null : maxValue.byteValue(),
seed
random.nextLong()
);
}
if (fieldType.equals(Short.TYPE) || fieldType.equals(Short.class)) {
return new ShortRangeRandomizer(
minValue == null ? null : minValue.shortValue(),
maxValue == null ? null : maxValue.shortValue(),
seed
random.nextLong()
);
}
if (fieldType.equals(Integer.TYPE) || fieldType.equals(Integer.class)) {
return new IntegerRangeRandomizer(
minValue == null ? null : minValue.intValue(),
maxValue == null ? null : maxValue.intValue(),
seed
random.nextLong()
);
}
if (fieldType.equals(Long.TYPE) || fieldType.equals(Long.class)) {
return new LongRangeRandomizer(
minValue == null ? null : minValue.longValue(),
maxValue == null ? null : maxValue.longValue(),
seed
random.nextLong()
);
}
if (fieldType.equals(BigInteger.class)) {
return new BigIntegerRangeRandomizer(
minValue == null ? null : minValue.intValue(),
maxValue == null ? null : maxValue.intValue(),
seed
random.nextLong()
);
}
if (fieldType.equals(BigDecimal.class)) {
return new BigDecimalRangeRandomizer(
minValue == null ? null : minValue.longValue(),
maxValue == null ? null : maxValue.longValue(),
seed
random.nextLong()
);
}
if (fieldType.equals(String.class)) {
BigDecimalRangeRandomizer delegate = new BigDecimalRangeRandomizer(
minValue == null ? null : minValue.longValue(),
maxValue == null ? null : maxValue.longValue(),
seed
random.nextLong()
);
return new StringDelegatingRandomizer(delegate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

class FutureAnnotationHandler implements BeanValidationAnnotationHandler {

private long seed;
private final Random random;

public FutureAnnotationHandler(long seed) {
this.seed = seed;
random = new Random(seed);
}

public Randomizer<?> getRandomizer(Field field) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, Constants.DEFAULT_DATE_RANGE);
return new DateRangeRandomizer(new Date(), calendar.getTime(), seed);
return new DateRangeRandomizer(new Date(), calendar.getTime(), random.nextLong());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Random;

class MinMaxAnnotationHandler implements BeanValidationAnnotationHandler {

private long seed;
private final Random random;

public MinMaxAnnotationHandler(long seed) {
this.seed = seed;
random = new Random(seed);
}

public Randomizer<?> getRandomizer(Field field) {
Expand All @@ -61,42 +62,42 @@ public Randomizer<?> getRandomizer(Field field) {
return new ByteRangeRandomizer(
minValue == null ? null : minValue.byteValue(),
maxValue == null ? null : maxValue.byteValue(),
seed
random.nextLong()
);
}
if (fieldType.equals(Short.TYPE) || fieldType.equals(Short.class)) {
return new ShortRangeRandomizer(
minValue == null ? null : minValue.shortValue(),
maxValue == null ? null : maxValue.shortValue(),
seed
random.nextLong()
);
}
if (fieldType.equals(Integer.TYPE) || fieldType.equals(Integer.class)) {
return new IntegerRangeRandomizer(
minValue == null ? null : minValue.intValue(),
maxValue == null ? null : maxValue.intValue(),
seed
random.nextLong()
);
}
if (fieldType.equals(Long.TYPE) || fieldType.equals(Long.class)) {
return new LongRangeRandomizer(
minValue == null ? null : minValue,
maxValue == null ? null : maxValue,
seed
random.nextLong()
);
}
if (fieldType.equals(BigInteger.class)) {
return new BigIntegerRangeRandomizer(
minValue == null ? null : minValue.intValue(),
maxValue == null ? null : maxValue.intValue(),
seed
random.nextLong()
);
}
if (fieldType.equals(BigDecimal.class)) {
return new BigDecimalRangeRandomizer(
minValue == null ? null : minValue,
maxValue == null ? null : maxValue,
seed
random.nextLong()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

class PastAnnotationHandler implements BeanValidationAnnotationHandler {

private long seed;
private final Random random;

public PastAnnotationHandler(long seed) {
this.seed = seed;
random = new Random(seed);
}

public Randomizer<?> getRandomizer(Field field) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -Constants.DEFAULT_DATE_RANGE);
return new DateRangeRandomizer(calendar.getTime(), new Date(), seed);
return new DateRangeRandomizer(calendar.getTime(), new Date(), random.nextLong());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@

import javax.validation.constraints.Pattern;
import java.lang.reflect.Field;
import java.util.Random;

class PatternAnnotationHandler implements BeanValidationAnnotationHandler {

private long seed;
private final Random random;

public PatternAnnotationHandler(long seed) {
this.seed = seed;
random = new Random(seed);
}

public Randomizer<?> getRandomizer(Field field) {
Expand All @@ -43,7 +44,7 @@ public Randomizer<?> getRandomizer(Field field) {

final String regex = patternAnnotation.regexp();
if (fieldType.equals(String.class)) {
return new RegularExpressionRandomizer(regex, seed);
return new RegularExpressionRandomizer(regex, random.nextLong());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,21 @@
package io.github.benas.randombeans.validation;

import io.github.benas.randombeans.api.Randomizer;
import io.github.benas.randombeans.randomizers.text.CharacterRandomizer;
import io.github.benas.randombeans.randomizers.text.StringRandomizer;

import javax.validation.constraints.Size;
import java.lang.reflect.Field;
import java.nio.charset.Charset;

import static io.github.benas.randombeans.randomizers.range.IntegerRangeRandomizer.aNewIntegerRangeRandomizer;
import static io.github.benas.randombeans.randomizers.text.CharacterRandomizer.aNewCharacterRandomizer;
import java.util.Random;

class SizeAnnotationHandler implements BeanValidationAnnotationHandler {

private long seed;
private final Random random;

private Charset charset;

public SizeAnnotationHandler(long seed, Charset charset) {
this.seed = seed;
random = new Random(seed);
this.charset = charset;
}

Expand All @@ -51,19 +49,7 @@ public Randomizer<?> getRandomizer(Field field) {
final int min = sizeAnnotation.min();
final int max = sizeAnnotation.max();
if (fieldType.equals(String.class)) {
final int randomLength = aNewIntegerRangeRandomizer(min, max).getRandomValue();
return new Randomizer<String>() {
private final CharacterRandomizer characterRandomizer = aNewCharacterRandomizer(charset, seed);

@Override
public String getRandomValue() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < randomLength; i++) {
stringBuilder.append(characterRandomizer.getRandomValue());
}
return stringBuilder.toString();
}
};
return new StringRandomizer(charset, min, max, random.nextLong());
}
return null;
}
Expand Down

0 comments on commit 66e396a

Please sign in to comment.