diff --git a/easy-random-bean-validation/src/main/java/org/jeasy/random/validation/SizeAnnotationHandler.java b/easy-random-bean-validation/src/main/java/org/jeasy/random/validation/SizeAnnotationHandler.java index 538ad36d..27e908c6 100644 --- a/easy-random-bean-validation/src/main/java/org/jeasy/random/validation/SizeAnnotationHandler.java +++ b/easy-random-bean-validation/src/main/java/org/jeasy/random/validation/SizeAnnotationHandler.java @@ -62,8 +62,6 @@ public Randomizer getRandomizer(Field field) { final int min = sizeAnnotation.min(); final int max = sizeAnnotation.max() == Integer.MAX_VALUE ? 255 : sizeAnnotation.max(); if (easyRandom == null) { - parameters.setCollectionSizeRange(new EasyRandomParameters.Range<>(min, max)); - parameters.setStringLengthRange(new EasyRandomParameters.Range<>(min, max)); easyRandom = new EasyRandom(parameters); } diff --git a/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationAnnotatedBean.java b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationAnnotatedBean.java index 8796a806..4ceec9dd 100644 --- a/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationAnnotatedBean.java +++ b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationAnnotatedBean.java @@ -119,6 +119,9 @@ class BeanValidationAnnotatedBean { @Size(min = 2, max = 10) private List sizedList; + @Size(min = 2, max = 10) + private List sizedListEmbeddedBean; + @Size(min = 2, max = 10) private Set sizedSet; @@ -269,6 +272,10 @@ public String getSizedString() { return this.sizedString; } + public List getSizedListEmbeddedBean() { + return sizedListEmbeddedBean; + } + public String getRegexString() { return this.regexString; } @@ -405,6 +412,10 @@ public void setSizedString(String sizedString) { this.sizedString = sizedString; } + public void setSizedListEmbeddedBean(List sizedListEmbeddedBean) { + this.sizedListEmbeddedBean = sizedListEmbeddedBean; + } + public void setRegexString(String regexString) { this.regexString = regexString; } diff --git a/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationTest.java b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationTest.java index ad44cf8f..338af3e1 100644 --- a/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationTest.java +++ b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationTest.java @@ -41,7 +41,6 @@ import javax.validation.constraints.Digits; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; - import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.Date; @@ -59,6 +58,22 @@ void setUp() { easyRandom = new EasyRandom(); } + @Test + void sizeConstraintShouldNotBePropagatedToEmbeddedBeans() { + + EasyRandomParameters parameters = new EasyRandomParameters() + .collectionSizeRange(11, 15) + .stringLengthRange(16, 20); + easyRandom = new EasyRandom(parameters); + BeanValidationAnnotatedBean bean = easyRandom.nextObject(BeanValidationAnnotatedBean.class); + + assertThat(bean.getSizedListEmbeddedBean().size()).isBetween(2, 10); // @Size constraint + assertThat(bean.getSizedListEmbeddedBean()).allSatisfy(embeddedBean -> { + assertThat(embeddedBean.getItems().size()).isBetween(11, 15); + assertThat(embeddedBean.getItems()).allSatisfy(stringItem -> assertThat(stringItem.length()).isBetween(16, 20)); + }); + } + @Test void generatedValuesShouldBeValidAccordingToValidationConstraints() { BeanValidationAnnotatedBean bean = easyRandom.nextObject(BeanValidationAnnotatedBean.class); @@ -202,7 +217,7 @@ void generatedValuesShouldBeValidAccordingToValidationConstraintsOnMethod() { @Test void generatedValuesForBeanWithoutReadMethod() { BeanValidationWithoutReadMethodBean bean = easyRandom.nextObject(BeanValidationWithoutReadMethodBean.class); - + assertThat(bean).hasNoNullFieldsOrProperties(); } @@ -210,7 +225,7 @@ void generatedValuesForBeanWithoutReadMethod() { void shouldGenerateTheSameValueForTheSameSeed() { EasyRandomParameters parameters = new EasyRandomParameters().seed(123L); EasyRandom random = new EasyRandom(parameters); - + BeanValidationAnnotatedBean bean = random.nextObject(BeanValidationAnnotatedBean.class); assertThat(bean.getUsername()).isEqualTo("eOMtThyhVNLWUZNRcBaQKxI"); @@ -252,14 +267,14 @@ class Salary { @Digits(integer = 2, fraction = 2) // OSS developer salary.. :-) private BigDecimal amount; } - + EasyRandomParameters parameters = new EasyRandomParameters() .randomizerRegistry(new MyCustomBeanValidationRandomizerRegistry()); EasyRandom easyRandom = new EasyRandom(parameters); - + // when Salary salary = easyRandom.nextObject(Salary.class); - + // then assertThat(salary).isNotNull(); assertThat(salary.amount).isLessThanOrEqualTo(new BigDecimal("99.99")); diff --git a/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/EmbeddedBean.java b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/EmbeddedBean.java new file mode 100644 index 00000000..ec65b4b9 --- /dev/null +++ b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/EmbeddedBean.java @@ -0,0 +1,16 @@ +package org.jeasy.random.validation; + +import java.util.List; + +public class EmbeddedBean { + + private List items; + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +}