forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#20382 from yrodiere/i20347-valueextractor
Hibernate Validator: Correctly detect custom ValueExtractor beans
- Loading branch information
Showing
5 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...us/hibernate/validator/test/valueextractor/ApplicationScopedCustomValueExtractorTest.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,57 @@ | ||
package io.quarkus.hibernate.validator.test.valueextractor; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import javax.validation.ValidatorFactory; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.valueextraction.ExtractedValue; | ||
import javax.validation.valueextraction.ValueExtractor; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
@Disabled("Reproduces https://github.com/quarkusio/quarkus/issues/20375, not yet fixed") | ||
public class ApplicationScopedCustomValueExtractorTest { | ||
|
||
@Inject | ||
ValidatorFactory validatorFactory; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap | ||
.create(JavaArchive.class) | ||
.addClasses(TestBean.class, Container.class, ApplicationScopedContainerValueExtractor.class)); | ||
|
||
@Test | ||
public void testApplicationScopedCustomValueExtractor() { | ||
assertThat(validatorFactory.getValidator().validate(new TestBean())).hasSize(1); | ||
} | ||
|
||
public static class TestBean { | ||
public Container<@NotBlank String> constrainedContainer; | ||
|
||
public TestBean() { | ||
Container<String> invalidContainer = new Container<>(); | ||
invalidContainer.value = " "; | ||
|
||
this.constrainedContainer = invalidContainer; | ||
} | ||
} | ||
|
||
@ApplicationScoped | ||
public static class ApplicationScopedContainerValueExtractor | ||
implements ValueExtractor<Container<@ExtractedValue ?>> { | ||
|
||
@Override | ||
public void extractValues(Container<?> originalValue, ValueReceiver receiver) { | ||
receiver.value("someName", originalValue.value); | ||
} | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...eployment/src/test/java/io/quarkus/hibernate/validator/test/valueextractor/Container.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,10 @@ | ||
package io.quarkus.hibernate.validator.test.valueextractor; | ||
|
||
// TODO for some reason, this must be a top-level type, not a nested/inner type, | ||
// in order for annotations on type parameters to be detected. | ||
// See NestedContainerTypeCustomValueExtractorTest | ||
public class Container<T> { | ||
|
||
public T value; | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
.../hibernate/validator/test/valueextractor/NestedContainerTypeCustomValueExtractorTest.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,62 @@ | ||
package io.quarkus.hibernate.validator.test.valueextractor; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import javax.validation.ValidatorFactory; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.valueextraction.ExtractedValue; | ||
import javax.validation.valueextraction.ValueExtractor; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
@Disabled("Reproduces https://github.com/quarkusio/quarkus/issues/20377, not yet fixed") | ||
public class NestedContainerTypeCustomValueExtractorTest { | ||
|
||
@Inject | ||
ValidatorFactory validatorFactory; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap | ||
.create(JavaArchive.class) | ||
.addClasses(TestBean.class, NestedContainerType.class, NestedContainerClassValueExtractor.class)); | ||
|
||
@Test | ||
public void testNestedContainerTypeValueExtractor() { | ||
assertThat(validatorFactory.getValidator().validate(new TestBean())).hasSize(1); | ||
} | ||
|
||
public static class TestBean { | ||
public NestedContainerType<@NotBlank String> constrainedContainer; | ||
|
||
public TestBean() { | ||
NestedContainerType<String> invalidContainer = new NestedContainerType<>(); | ||
invalidContainer.value = " "; | ||
|
||
this.constrainedContainer = invalidContainer; | ||
} | ||
} | ||
|
||
public static class NestedContainerType<T> { | ||
|
||
public T value; | ||
|
||
} | ||
|
||
@Singleton | ||
public static class NestedContainerClassValueExtractor | ||
implements ValueExtractor<NestedContainerType<@ExtractedValue ?>> { | ||
|
||
@Override | ||
public void extractValues(NestedContainerType<?> originalValue, ValueReceiver receiver) { | ||
receiver.value("someName", originalValue.value); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...io/quarkus/hibernate/validator/test/valueextractor/SingletonCustomValueExtractorTest.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,55 @@ | ||
package io.quarkus.hibernate.validator.test.valueextractor; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import javax.validation.ValidatorFactory; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.valueextraction.ExtractedValue; | ||
import javax.validation.valueextraction.ValueExtractor; | ||
|
||
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.test.QuarkusUnitTest; | ||
|
||
public class SingletonCustomValueExtractorTest { | ||
|
||
@Inject | ||
ValidatorFactory validatorFactory; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap | ||
.create(JavaArchive.class) | ||
.addClasses(TestBean.class, Container.class, SingletonContainerValueExtractor.class)); | ||
|
||
@Test | ||
public void testSingletonCustomValueExtractor() { | ||
assertThat(validatorFactory.getValidator().validate(new TestBean())).hasSize(1); | ||
} | ||
|
||
public static class TestBean { | ||
public Container<@NotBlank String> constrainedContainer; | ||
|
||
public TestBean() { | ||
Container<String> invalidContainer = new Container<>(); | ||
invalidContainer.value = " "; | ||
|
||
this.constrainedContainer = invalidContainer; | ||
} | ||
} | ||
|
||
@Singleton | ||
public static class SingletonContainerValueExtractor | ||
implements ValueExtractor<Container<@ExtractedValue ?>> { | ||
|
||
@Override | ||
public void extractValues(Container<?> originalValue, ValueReceiver receiver) { | ||
receiver.value("someName", originalValue.value); | ||
} | ||
} | ||
|
||
} |
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