WICKET-5656 PropertyResolver does not scan for NotNull in annotation tree
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/bc4a55fc Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/bc4a55fc Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/bc4a55fc Branch: refs/heads/WICKET-5677 Commit: bc4a55fccea6810d0970d924fdd9d84392960995 Parents: 45ad072 Author: adelbene <an.delb...@gmail.com> Authored: Fri Aug 22 13:08:30 2014 +0200 Committer: adelbene <an.delb...@gmail.com> Committed: Fri Aug 22 14:10:27 2014 +0200 ---------------------------------------------------------------------- .../bean/validation/PropertyValidator.java | 29 +++++++++++++------- .../PropertyValidatorRequiredTest.java | 21 ++++++++++++-- .../PasswordConstraintAnnotation.java | 2 ++ 3 files changed, 40 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/bc4a55fc/wicket-bean-validation/src/main/java/org/apache/wicket/bean/validation/PropertyValidator.java ---------------------------------------------------------------------- diff --git a/wicket-bean-validation/src/main/java/org/apache/wicket/bean/validation/PropertyValidator.java b/wicket-bean-validation/src/main/java/org/apache/wicket/bean/validation/PropertyValidator.java index e8e6376..a2486c7 100644 --- a/wicket-bean-validation/src/main/java/org/apache/wicket/bean/validation/PropertyValidator.java +++ b/wicket-bean-validation/src/main/java/org/apache/wicket/bean/validation/PropertyValidator.java @@ -10,6 +10,7 @@ import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validator; import javax.validation.constraints.NotNull; +import javax.validation.groups.Default; import javax.validation.metadata.ConstraintDescriptor; import org.apache.wicket.Component; @@ -100,11 +101,10 @@ public class PropertyValidator<T> extends Behavior implements IValidator<T> if (property_ == null) { throw new IllegalStateException( - "Could not resolve Property from component: " + - component + - ". Either specify the Property in the constructor or use a model that works in combination with a " + - IPropertyResolver.class.getSimpleName() + - " to resolve the Property automatically"); + "Could not resolve Property from component: " + component + + ". Either specify the Property in the constructor or use a model that works in combination with a " + + IPropertyResolver.class.getSimpleName() + + " to resolve the Property automatically"); } } return property_; @@ -126,14 +126,15 @@ public class PropertyValidator<T> extends Behavior implements IValidator<T> if (this.component != null) { throw new IllegalStateException( // - "This validator has already been added to component: " + this.component + - ". This validator does not support reusing instances, please create a new one"); + "This validator has already been added to component: " + + this.component + + ". This validator does not support reusing instances, please create a new one"); } if (!(component instanceof FormComponent)) { - throw new IllegalStateException(getClass().getSimpleName() + - " can only be added to FormComponents"); + throw new IllegalStateException(getClass().getSimpleName() + + " can only be added to FormComponents"); } // TODO add a validation key that appends the type so we can have @@ -208,7 +209,7 @@ public class PropertyValidator<T> extends Behavior implements IValidator<T> for (NotNull constraint : constraints) { - if (constraint.groups().length == 0 && validatorGroups.isEmpty()) + if (canApplyToDefaultGroup(constraint) && validatorGroups.isEmpty()) { return true; } @@ -225,6 +226,14 @@ public class PropertyValidator<T> extends Behavior implements IValidator<T> return false; } + private boolean canApplyToDefaultGroup(NotNull constraint) + { + List<Class<?>> groups = Arrays.asList(constraint.groups()); + //the constraint can be applied to default group either if its group array is empty + //or if it contains javax.validation.groups.Default + return groups.size() == 0 || groups.contains(Default.class); + } + @Override @SuppressWarnings({ "rawtypes", "unchecked" }) public void onComponentTag(Component component, ComponentTag tag) http://git-wip-us.apache.org/repos/asf/wicket/blob/bc4a55fc/wicket-bean-validation/src/test/java/org/apache/wicket/bean/validation/PropertyValidatorRequiredTest.java ---------------------------------------------------------------------- diff --git a/wicket-bean-validation/src/test/java/org/apache/wicket/bean/validation/PropertyValidatorRequiredTest.java b/wicket-bean-validation/src/test/java/org/apache/wicket/bean/validation/PropertyValidatorRequiredTest.java index fcdb007..8edea4c 100644 --- a/wicket-bean-validation/src/test/java/org/apache/wicket/bean/validation/PropertyValidatorRequiredTest.java +++ b/wicket-bean-validation/src/test/java/org/apache/wicket/bean/validation/PropertyValidatorRequiredTest.java @@ -1,6 +1,7 @@ package org.apache.wicket.bean.validation; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import javax.validation.constraints.NotNull; @@ -31,7 +32,7 @@ public class PropertyValidatorRequiredTest }; @Test - public void test() + public void testNotNullFields() { TestPage page = scope.getTester().startPage(TestPage.class); @@ -66,6 +67,22 @@ public class PropertyValidatorRequiredTest assertFalse(page.input20.isRequired()); } + + /** + * https://issues.apache.org/jira/browse/WICKET-5656 + * + * Annotation NotNull must be effective even if is not directly applied. + * + * @throws Exception + */ + @Test + public void testResolveComposedConstraints() throws Exception + { + Property property = new Property(DefaultPropertyResolverTest.BeanWithPassword.class, "password"); + PropertyValidator<DefaultPropertyResolverTest.BeanWithPassword> propertyValidator = new PropertyValidator<>(property); + + assertTrue(propertyValidator.isRequired()); + } public static class TestApplication extends MockApplication { http://git-wip-us.apache.org/repos/asf/wicket/blob/bc4a55fc/wicket-bean-validation/src/test/java/org/apache/wicket/bean/validation/customconstraint/PasswordConstraintAnnotation.java ---------------------------------------------------------------------- diff --git a/wicket-bean-validation/src/test/java/org/apache/wicket/bean/validation/customconstraint/PasswordConstraintAnnotation.java b/wicket-bean-validation/src/test/java/org/apache/wicket/bean/validation/customconstraint/PasswordConstraintAnnotation.java index 73f0ab1..21fdbb5 100644 --- a/wicket-bean-validation/src/test/java/org/apache/wicket/bean/validation/customconstraint/PasswordConstraintAnnotation.java +++ b/wicket-bean-validation/src/test/java/org/apache/wicket/bean/validation/customconstraint/PasswordConstraintAnnotation.java @@ -27,7 +27,9 @@ import java.lang.annotation.Target; import javax.validation.Constraint; import javax.validation.Payload; +import javax.validation.constraints.NotNull; +@NotNull @Target({ METHOD, FIELD, ANNOTATION_TYPE }) @Retention(RUNTIME) @Constraint(validatedBy = PasswordValidator.class)