Search in sources :

Example 1 with HibernateConstraintValidatorInitializationContext

use of org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext in project hibernate-validator by hibernate.

the class ConstraintValidatorManagerTest method testValidatorsAreCachedPerConstraintAndAnnotationMembersAndScriptEvaluatorFactory.

@Test
@TestForIssue(jiraKey = "HV-1589")
public void testValidatorsAreCachedPerConstraintAndAnnotationMembersAndScriptEvaluatorFactory() {
    Validator validator = getConfiguration().addMapping(ConstraintValidatorManagerTest.class.getResourceAsStream("hv-1589-mapping.xml")).buildValidatorFactory().getValidator();
    ConstraintDescriptorImpl<?> sizeOnMiddleNameDescriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "middleName");
    ConstraintDescriptorImpl<?> sizeOnAddress1Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address1");
    ConstraintDescriptorImpl<?> sizeOnAddress2Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address2");
    ScriptEvaluatorFactory scriptEvaluatorFactory1 = new DefaultScriptEvaluatorFactory(null);
    ScriptEvaluatorFactory scriptEvaluatorFactory2 = new DefaultScriptEvaluatorFactory(null);
    HibernateConstraintValidatorInitializationContext initializationContext1 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory1, DefaultClockProvider.INSTANCE, Duration.ZERO);
    HibernateConstraintValidatorInitializationContext initializationContext2 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory2, DefaultClockProvider.INSTANCE, Duration.ZERO);
    ConstraintValidator<?, ?> sizeValidatorForMiddleNameCtx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnMiddleNameDescriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext2);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext2);
    assertThat(sizeValidatorForMiddleNameCtx1).isNotSameAs(sizeValidatorForAddress1Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isSameAs(sizeValidatorForAddress2Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isNotSameAs(sizeValidatorForAddress2Ctx2);
    assertThat(sizeValidatorForAddress2Ctx2).isSameAs(sizeValidatorForAddress1Ctx2);
}
Also used : DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) HibernateConstraintValidatorInitializationContext(org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext) DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) ScriptEvaluatorFactory(org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) ValidatorUtil.getValidator(org.hibernate.validator.testutils.ValidatorUtil.getValidator) NotNullValidator(org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 2 with HibernateConstraintValidatorInitializationContext

use of org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext in project hibernate-validator by hibernate.

the class ConstraintValidatorManagerTest method testValidatorsAreCachedPerConstraintAndAnnotationMembersAndTemporalValidationTolerance.

@Test
@TestForIssue(jiraKey = "HV-1589")
public void testValidatorsAreCachedPerConstraintAndAnnotationMembersAndTemporalValidationTolerance() {
    Validator validator = getConfiguration().addMapping(ConstraintValidatorManagerTest.class.getResourceAsStream("hv-1589-mapping.xml")).buildValidatorFactory().getValidator();
    ConstraintDescriptorImpl<?> sizeOnMiddleNameDescriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "middleName");
    ConstraintDescriptorImpl<?> sizeOnAddress1Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address1");
    ConstraintDescriptorImpl<?> sizeOnAddress2Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address2");
    ScriptEvaluatorFactory scriptEvaluatorFactory = new DefaultScriptEvaluatorFactory(null);
    HibernateConstraintValidatorInitializationContext initializationContext1 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory, DefaultClockProvider.INSTANCE, Duration.ofDays(1));
    HibernateConstraintValidatorInitializationContext initializationContext2 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory, DefaultClockProvider.INSTANCE, Duration.ofDays(999));
    ConstraintValidator<?, ?> sizeValidatorForMiddleNameCtx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnMiddleNameDescriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext2);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext2);
    assertThat(sizeValidatorForMiddleNameCtx1).isNotSameAs(sizeValidatorForAddress1Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isSameAs(sizeValidatorForAddress2Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isNotSameAs(sizeValidatorForAddress2Ctx2);
    assertThat(sizeValidatorForAddress2Ctx2).isSameAs(sizeValidatorForAddress1Ctx2);
}
Also used : DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) HibernateConstraintValidatorInitializationContext(org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext) DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) ScriptEvaluatorFactory(org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) ValidatorUtil.getValidator(org.hibernate.validator.testutils.ValidatorUtil.getValidator) NotNullValidator(org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 3 with HibernateConstraintValidatorInitializationContext

use of org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext in project hibernate-validator by hibernate.

the class ConstraintValidatorManagerTest method testValidatorsAreCachedPerConstraintAndAnnotationMembersAndClockProvider.

@Test
@TestForIssue(jiraKey = "HV-1589")
public void testValidatorsAreCachedPerConstraintAndAnnotationMembersAndClockProvider() {
    Validator validator = getConfiguration().addMapping(ConstraintValidatorManagerTest.class.getResourceAsStream("hv-1589-mapping.xml")).buildValidatorFactory().getValidator();
    ConstraintDescriptorImpl<?> sizeOnMiddleNameDescriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "middleName");
    ConstraintDescriptorImpl<?> sizeOnAddress1Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address1");
    ConstraintDescriptorImpl<?> sizeOnAddress2Descriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "address2");
    ScriptEvaluatorFactory scriptEvaluatorFactory = new DefaultScriptEvaluatorFactory(null);
    ClockProvider clockProvider1 = new ClockProvider() {

        @Override
        public Clock getClock() {
            return null;
        }
    };
    ClockProvider clockProvider2 = new ClockProvider() {

        @Override
        public Clock getClock() {
            return null;
        }
    };
    HibernateConstraintValidatorInitializationContext initializationContext1 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory, clockProvider1, Duration.ZERO);
    HibernateConstraintValidatorInitializationContext initializationContext2 = getConstraintValidatorInitializationContext(scriptEvaluatorFactory, clockProvider2, Duration.ZERO);
    ConstraintValidator<?, ?> sizeValidatorForMiddleNameCtx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnMiddleNameDescriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext1);
    ConstraintValidator<?, ?> sizeValidatorForAddress2Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, initializationContext2);
    ConstraintValidator<?, ?> sizeValidatorForAddress1Ctx2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, initializationContext2);
    assertThat(sizeValidatorForMiddleNameCtx1).isNotSameAs(sizeValidatorForAddress1Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isSameAs(sizeValidatorForAddress2Ctx1);
    assertThat(sizeValidatorForAddress1Ctx1).isNotSameAs(sizeValidatorForAddress2Ctx2);
    assertThat(sizeValidatorForAddress2Ctx2).isSameAs(sizeValidatorForAddress1Ctx2);
}
Also used : DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) HibernateConstraintValidatorInitializationContext(org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext) ClockProvider(jakarta.validation.ClockProvider) DefaultClockProvider(org.hibernate.validator.internal.engine.DefaultClockProvider) DefaultScriptEvaluatorFactory(org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory) ScriptEvaluatorFactory(org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) ValidatorUtil.getValidator(org.hibernate.validator.testutils.ValidatorUtil.getValidator) NotNullValidator(org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Aggregations

ConstraintValidator (jakarta.validation.ConstraintValidator)3 Validator (jakarta.validation.Validator)3 HibernateConstraintValidatorInitializationContext (org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext)3 NotNullValidator (org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator)3 DefaultScriptEvaluatorFactory (org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory)3 ScriptEvaluatorFactory (org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory)3 TestForIssue (org.hibernate.validator.testutil.TestForIssue)3 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)3 Test (org.testng.annotations.Test)3 ClockProvider (jakarta.validation.ClockProvider)1 DefaultClockProvider (org.hibernate.validator.internal.engine.DefaultClockProvider)1