From ebce6c564aa5bed79f5d059b01f09eee5287bd5d Mon Sep 17 00:00:00 2001 From: Sebastian Daschner Date: Fri, 21 Aug 2020 08:54:10 +0200 Subject: [PATCH] Added constraint validators as singleton beans; resolves #6094; --- .../HibernateValidatorProcessor.java | 12 ++--- .../test/ValidatorBeanInjectionTest.java | 44 +++++++++++++++++++ .../validator/test/injection/TestBean.java | 8 ++++ .../test/injection/TestConstraint.java | 27 ++++++++++++ .../test/injection/TestInjectedBean.java | 15 +++++++ .../injection/TestInjectionValidator.java | 17 +++++++ 6 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/ValidatorBeanInjectionTest.java create mode 100644 extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestBean.java create mode 100644 extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestConstraint.java create mode 100644 extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestInjectedBean.java create mode 100644 extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestInjectionValidator.java diff --git a/extensions/hibernate-validator/deployment/src/main/java/io/quarkus/hibernate/validator/deployment/HibernateValidatorProcessor.java b/extensions/hibernate-validator/deployment/src/main/java/io/quarkus/hibernate/validator/deployment/HibernateValidatorProcessor.java index 4a84bb290ad2a..ac6f4f7ca1dcf 100644 --- a/extensions/hibernate-validator/deployment/src/main/java/io/quarkus/hibernate/validator/deployment/HibernateValidatorProcessor.java +++ b/extensions/hibernate-validator/deployment/src/main/java/io/quarkus/hibernate/validator/deployment/HibernateValidatorProcessor.java @@ -39,12 +39,9 @@ import org.jboss.jandex.MethodInfo; import org.jboss.jandex.Type; -import io.quarkus.arc.deployment.AdditionalBeanBuildItem; -import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem; -import io.quarkus.arc.deployment.BeanArchiveIndexBuildItem; -import io.quarkus.arc.deployment.BeanContainerListenerBuildItem; -import io.quarkus.arc.deployment.UnremovableBeanBuildItem; +import io.quarkus.arc.deployment.*; import io.quarkus.arc.processor.BeanInfo; +import io.quarkus.arc.processor.BuiltinScope; import io.quarkus.deployment.Capabilities; import io.quarkus.deployment.Capability; import io.quarkus.deployment.Feature; @@ -114,6 +111,7 @@ LogCleanupFilterBuildItem logCleanup() { @BuildStep void registerAdditionalBeans(BuildProducer additionalBeans, BuildProducer unremovableBean, + BuildProducer autoScopes, Capabilities capabilities) { // The bean encapsulating the Validator and ValidatorFactory additionalBeans.produce(new AdditionalBeanBuildItem(ValidatorProvider.class)); @@ -129,6 +127,10 @@ void registerAdditionalBeans(BuildProducer additionalBe "io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyContextLocaleResolver")); } + // A constraint validator with an injection point but no scope is added as @Singleton + autoScopes.produce(AutoAddScopeBuildItem.builder().implementsInterface(CONSTRAINT_VALIDATOR).requiresContainerServices() + .defaultScope(BuiltinScope.SINGLETON).build()); + // Do not remove the Bean Validation beans unremovableBean.produce(new UnremovableBeanBuildItem(new Predicate() { @Override diff --git a/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/ValidatorBeanInjectionTest.java b/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/ValidatorBeanInjectionTest.java new file mode 100644 index 0000000000000..0abec442499b2 --- /dev/null +++ b/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/ValidatorBeanInjectionTest.java @@ -0,0 +1,44 @@ +package io.quarkus.hibernate.validator.test; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Set; + +import javax.inject.Inject; +import javax.validation.ConstraintViolation; +import javax.validation.Validator; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.hibernate.validator.test.injection.TestBean; +import io.quarkus.hibernate.validator.test.injection.TestConstraint; +import io.quarkus.hibernate.validator.test.injection.TestInjectedBean; +import io.quarkus.hibernate.validator.test.injection.TestInjectionValidator; +import io.quarkus.test.QuarkusUnitTest; + +public class ValidatorBeanInjectionTest { + + @RegisterExtension + static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap + .create(JavaArchive.class) + .addClasses(TestBean.class, TestConstraint.class, TestInjectedBean.class, TestInjectionValidator.class)); + + @Inject + Validator validator; + + @Test + public void testValidationWithInjection() { + Set> constraintViolations = validator.validate(new TestBean()); + + assertThat(constraintViolations).isNotEmpty(); + + TestBean bean = new TestBean(); + bean.name = "Alpha"; + constraintViolations = validator.validate(bean); + assertThat(constraintViolations).isEmpty(); + } + +} diff --git a/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestBean.java b/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestBean.java new file mode 100644 index 0000000000000..0dfee727d9957 --- /dev/null +++ b/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestBean.java @@ -0,0 +1,8 @@ +package io.quarkus.hibernate.validator.test.injection; + +public class TestBean { + + @TestConstraint + public String name; + +} \ No newline at end of file diff --git a/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestConstraint.java b/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestConstraint.java new file mode 100644 index 0000000000000..64d6d3d86527b --- /dev/null +++ b/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestConstraint.java @@ -0,0 +1,27 @@ +package io.quarkus.hibernate.validator.test.injection; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.validation.Constraint; +import javax.validation.Payload; +import javax.validation.constraints.NotNull; + +@Retention(RUNTIME) +@Target({ FIELD, METHOD, ANNOTATION_TYPE, PARAMETER }) +@NotNull +@Constraint(validatedBy = TestInjectionValidator.class) +@Documented +public @interface TestConstraint { + + String message() default ""; + + Class[] groups() default {}; + + Class[] payload() default {}; + +} diff --git a/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestInjectedBean.java b/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestInjectedBean.java new file mode 100644 index 0000000000000..e2b62c73eef6d --- /dev/null +++ b/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestInjectedBean.java @@ -0,0 +1,15 @@ +package io.quarkus.hibernate.validator.test.injection; + +import java.util.Collections; +import java.util.List; + +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class TestInjectedBean { + + public List allowedStrings() { + return Collections.singletonList("Alpha"); + } + +} diff --git a/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestInjectionValidator.java b/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestInjectionValidator.java new file mode 100644 index 0000000000000..208111599efb5 --- /dev/null +++ b/extensions/hibernate-validator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestInjectionValidator.java @@ -0,0 +1,17 @@ +package io.quarkus.hibernate.validator.test.injection; + +import javax.inject.Inject; +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +public class TestInjectionValidator implements ConstraintValidator { + + @Inject + TestInjectedBean testInjectedBean; + + @Override + public boolean isValid(String string, ConstraintValidatorContext context) { + return testInjectedBean.allowedStrings().contains(string); + } + +}