Search in sources :

Example 96 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project beanvalidation-tck by eclipse-ee4j.

the class TraversableResolverTest method testCorrectCallsToIsReachableAndIsCascadableForReturnValueValidation.

@Test
@SpecAssertions({ @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TRAVERSABLE, id = "d"), @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TRAVERSABLE, id = "e"), @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TRAVERSABLE, id = "f"), @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TRAVERSABLE, id = "g"), @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TRAVERSABLE, id = "h"), @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TRAVERSABLE, id = "k"), @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TRAVERSABLE, id = "l") })
public void testCorrectCallsToIsReachableAndIsCascadableForReturnValueValidation() throws Exception {
    Suit suit = new Suit();
    suit.setTrousers(new Trousers());
    suit.setJacket(new Jacket());
    suit.setSize(3333);
    suit.getTrousers().setLength(32321);
    suit.getJacket().setWidth(432432);
    Set<Call> expectedReachCalls = new HashSet<Call>();
    Set<Call> expectedCascadeCalls = new HashSet<Call>();
    expectedReachCalls.add(new Call(suit, "size", Gentleman.class, ElementType.FIELD, new String[] { "undress", RETURN_VALUE_NODE_NAME }));
    expectedReachCalls.add(new Call(suit, "trousers", Gentleman.class, ElementType.FIELD, new String[] { "undress", RETURN_VALUE_NODE_NAME }));
    expectedCascadeCalls.add(new Call(suit, "trousers", Gentleman.class, ElementType.FIELD, new String[] { "undress", RETURN_VALUE_NODE_NAME }));
    expectedReachCalls.add(new Call(suit.getTrousers(), "length", Gentleman.class, ElementType.FIELD, new String[] { "undress", RETURN_VALUE_NODE_NAME, "trousers" }));
    expectedReachCalls.add(new Call(suit, "jacket", Gentleman.class, ElementType.METHOD, new String[] { "undress", RETURN_VALUE_NODE_NAME }));
    expectedCascadeCalls.add(new Call(suit, "jacket", Gentleman.class, ElementType.METHOD, new String[] { "undress", RETURN_VALUE_NODE_NAME }));
    expectedReachCalls.add(new Call(suit.getJacket(), "width", Gentleman.class, ElementType.METHOD, new String[] { "undress", RETURN_VALUE_NODE_NAME, "jacket" }));
    SnifferTraversableResolver resolver = new SnifferTraversableResolver(expectedReachCalls, expectedCascadeCalls);
    Configuration<?> config = TestUtil.getConfigurationUnderTest().traversableResolver(resolver);
    ValidatorFactory factory = config.buildValidatorFactory();
    ExecutableValidator executableValidator = factory.getValidator().forExecutables();
    Gentleman gentleman = new Gentleman();
    gentleman.wearSuit(suit);
    Method method = Gentleman.class.getMethod("undress");
    executableValidator.validateReturnValue(gentleman, method, suit);
    assertEquals(resolver.getReachableCallCount(), 5);
    assertEquals(resolver.getCascadableCallCount(), 2);
}
Also used : ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ValidatorFactory(jakarta.validation.ValidatorFactory) Method(java.lang.reflect.Method) HashSet(java.util.HashSet) SpecAssertions(org.jboss.test.audit.annotations.SpecAssertions) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test)

Example 97 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project beanvalidation-tck by eclipse-ee4j.

the class TraversableResolverTest method testResolverExceptionsGetWrappedInValidationException.

@Test(expectedExceptions = ValidationException.class)
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TRAVERSABLE, id = "j")
public void testResolverExceptionsGetWrappedInValidationException() {
    ExceptionThrowingTraversableResolver resolver = new ExceptionThrowingTraversableResolver();
    Configuration<?> config = TestUtil.getConfigurationUnderTest().traversableResolver(resolver);
    ValidatorFactory factory = config.buildValidatorFactory();
    Validator v = factory.getValidator();
    v.validate(new Suit());
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) Validator(jakarta.validation.Validator) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test) SpecAssertion(org.jboss.test.audit.annotations.SpecAssertion)

Example 98 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project beanvalidation-tck by eclipse-ee4j.

the class CustomConstraintValidatorTest method testGetConstraintValidatorFactoryFromValidatorFactory.

@Test
@SpecAssertion(section = Sections.VALIDATIONAPI_BOOTSTRAPPING_VALIDATORFACTORY, id = "d")
public void testGetConstraintValidatorFactoryFromValidatorFactory() {
    CustomConstraintValidatorFactory constraintValidatorFactory = new CustomConstraintValidatorFactory();
    ValidatorFactory validatorFactory = TestUtil.getConfigurationUnderTest().constraintValidatorFactory(constraintValidatorFactory).buildValidatorFactory();
    assertSame(validatorFactory.getConstraintValidatorFactory(), constraintValidatorFactory, "getConstraintValidatorFactory() should return the parameter name provider set via configuration");
}
Also used : ConstraintValidatorFactory(jakarta.validation.ConstraintValidatorFactory) ValidatorFactory(jakarta.validation.ValidatorFactory) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test) SpecAssertion(org.jboss.test.audit.annotations.SpecAssertion)

Example 99 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project beanvalidation-tck by eclipse-ee4j.

the class ValidationTest method testCustomValidationProviderResolution.

@Test
@SpecAssertions({ @SpecAssertion(section = Sections.VALIDATIONAPI_BOOTSTRAPPING_VALIDATION, id = "b"), @SpecAssertion(section = Sections.VALIDATIONAPI_BOOTSTRAPPING_VALIDATION, id = "e") })
public void testCustomValidationProviderResolution() {
    ValidationProviderResolver resolver = new ValidationProviderResolver() {

        public List<ValidationProvider<?>> getValidationProviders() {
            List<ValidationProvider<?>> list = new ArrayList<ValidationProvider<?>>();
            list.add(new TCKValidationProvider());
            return list;
        }
    };
    GenericBootstrap bootstrap = Validation.byDefaultProvider();
    Configuration<?> config = bootstrap.providerResolver(resolver).configure();
    ValidatorFactory factory = config.buildValidatorFactory();
    assertTrue(factory instanceof TCKValidationProvider.DummyValidatorFactory);
}
Also used : ValidationProvider(jakarta.validation.spi.ValidationProvider) TCKValidationProvider(org.hibernate.beanvalidation.tck.common.TCKValidationProvider) ValidatorFactory(jakarta.validation.ValidatorFactory) TCKValidationProvider(org.hibernate.beanvalidation.tck.common.TCKValidationProvider) ValidationProviderResolver(jakarta.validation.ValidationProviderResolver) ArrayList(java.util.ArrayList) GenericBootstrap(jakarta.validation.bootstrap.GenericBootstrap) SpecAssertions(org.jboss.test.audit.annotations.SpecAssertions) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test)

Example 100 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project beanvalidation-tck by eclipse-ee4j.

the class ValidationTest method testBuildDefaultValidatorFactory.

@Test
@SpecAssertion(section = Sections.VALIDATIONAPI_BOOTSTRAPPING_VALIDATION, id = "a")
public void testBuildDefaultValidatorFactory() {
    ValidatorFactory defaultFactory = Validation.buildDefaultValidatorFactory();
    assertNotNull(defaultFactory, "We should be able to get a factory.");
    ValidatorFactory defaultProviderFactory = Validation.byDefaultProvider().configure().buildValidatorFactory();
    assertNotNull(defaultProviderFactory, "We should be able to get a factory.");
    assertEquals(defaultFactory.getClass(), defaultFactory.getClass(), "The factories have to be identical.");
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test) SpecAssertion(org.jboss.test.audit.annotations.SpecAssertion)

Aggregations

ValidatorFactory (jakarta.validation.ValidatorFactory)200 Test (org.testng.annotations.Test)115 Validator (jakarta.validation.Validator)101 HibernateValidator (org.hibernate.validator.HibernateValidator)74 ConstraintViolation (jakarta.validation.ConstraintViolation)59 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)39 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)32 TestForIssue (org.hibernate.validator.testutil.TestForIssue)30 ConstraintValidator (jakarta.validation.ConstraintValidator)25 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)24 BeforeClass (org.junit.BeforeClass)23 Test (org.junit.Test)17 MessageInterpolator (jakarta.validation.MessageInterpolator)14 HibernateValidatorFactory (org.hibernate.validator.HibernateValidatorFactory)14 NotNull (jakarta.validation.constraints.NotNull)13 ConstraintValidatorFactory (jakarta.validation.ConstraintValidatorFactory)10 HashSet (java.util.HashSet)9 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)8 TCKValidationProvider (org.hibernate.beanvalidation.tck.common.TCKValidationProvider)8 ValidationException (jakarta.validation.ValidationException)7