-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added constraint validators as singleton beans; resolves #6094;
- Loading branch information
Showing
6 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...loyment/src/test/java/io/quarkus/hibernate/validator/test/ValidatorBeanInjectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ConstraintViolation<TestBean>> constraintViolations = validator.validate(new TestBean()); | ||
|
||
assertThat(constraintViolations).isNotEmpty(); | ||
|
||
TestBean bean = new TestBean(); | ||
bean.name = "Alpha"; | ||
constraintViolations = validator.validate(bean); | ||
assertThat(constraintViolations).isEmpty(); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...ator/deployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.quarkus.hibernate.validator.test.injection; | ||
|
||
public class TestBean { | ||
|
||
@TestConstraint | ||
public String name; | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...eployment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestConstraint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.hibernate.validator.test.injection; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
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<? extends Payload>[] payload() default {}; | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...loyment/src/test/java/io/quarkus/hibernate/validator/test/injection/TestInjectedBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> allowedStrings() { | ||
return Collections.singletonList("Alpha"); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...t/src/test/java/io/quarkus/hibernate/validator/test/injection/TestInjectionValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TestConstraint, String> { | ||
|
||
@Inject | ||
TestInjectedBean testInjectedBean; | ||
|
||
@Override | ||
public boolean isValid(String string, ConstraintValidatorContext context) { | ||
return testInjectedBean.allowedStrings().contains(string); | ||
} | ||
|
||
} |