Search in sources :

Example 1 with ConstraintValidator

use of jakarta.validation.ConstraintValidator 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 ConstraintValidator

use of jakarta.validation.ConstraintValidator in project hibernate-validator by hibernate.

the class ConstraintValidatorManagerTest method testValidatorsAreCachedPerConstraint.

@Test
@TestForIssue(jiraKey = "HV-662")
public void testValidatorsAreCachedPerConstraint() {
    Validator validator = getConfiguration().addMapping(ConstraintValidatorManagerTest.class.getResourceAsStream("hv-662-mapping.xml")).buildValidatorFactory().getValidator();
    ConstraintDescriptorImpl<?> notNullOnFirstNameDescriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "firstName");
    ConstraintDescriptorImpl<?> notNullOnLastNameDescriptor = getSingleConstraintDescriptorForProperty(validator, User.class, "lastName");
    ConstraintValidator<?, ?> notNullValidatorForFirstName1 = constraintValidatorManager.getInitializedValidator(String.class, notNullOnFirstNameDescriptor, constraintValidatorFactory, getDummyConstraintValidatorInitializationContext());
    ConstraintValidator<?, ?> notNullValidatorForFirstName2 = constraintValidatorManager.getInitializedValidator(String.class, notNullOnFirstNameDescriptor, constraintValidatorFactory, getDummyConstraintValidatorInitializationContext());
    ConstraintValidator<?, ?> notNullValidatorForLastName = constraintValidatorManager.getInitializedValidator(String.class, notNullOnLastNameDescriptor, constraintValidatorFactory, getDummyConstraintValidatorInitializationContext());
    assertThat(notNullValidatorForFirstName1).isSameAs(notNullValidatorForFirstName2);
    assertThat(notNullValidatorForFirstName1).isSameAs(notNullValidatorForLastName);
}
Also used : 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 ConstraintValidator

use of jakarta.validation.ConstraintValidator 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 4 with ConstraintValidator

use of jakarta.validation.ConstraintValidator in project hibernate-validator by hibernate.

the class ConstraintValidatorManagerTest method testValidatorsAreCachedPerConstraintAndAnnotationMembers.

@Test
@TestForIssue(jiraKey = "HV-662")
public void testValidatorsAreCachedPerConstraintAndAnnotationMembers() {
    Validator validator = getConfiguration().addMapping(ConstraintValidatorManagerTest.class.getResourceAsStream("hv-662-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");
    ConstraintValidator<?, ?> sizeValidatorForMiddleName = constraintValidatorManager.getInitializedValidator(String.class, sizeOnMiddleNameDescriptor, constraintValidatorFactory, getDummyConstraintValidatorInitializationContext());
    ConstraintValidator<?, ?> sizeValidatorForAddress1 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress1Descriptor, constraintValidatorFactory, getDummyConstraintValidatorInitializationContext());
    ConstraintValidator<?, ?> sizeValidatorForAddress2 = constraintValidatorManager.getInitializedValidator(String.class, sizeOnAddress2Descriptor, constraintValidatorFactory, getDummyConstraintValidatorInitializationContext());
    assertThat(sizeValidatorForMiddleName).isNotSameAs(sizeValidatorForAddress1);
    assertThat(sizeValidatorForAddress1).isSameAs(sizeValidatorForAddress2);
}
Also used : 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 5 with ConstraintValidator

use of jakarta.validation.ConstraintValidator in project hibernate-validator by hibernate.

the class SignValidatorForNumberTest method testNegativeOrZero.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void testNegativeOrZero(NegativeOrZero m) {
    ConstraintValidator validator = new NegativeOrZeroValidatorForNumber();
    validator.initialize(m);
    testSignNumber(validator, false, false);
    validator = new NegativeOrZeroValidatorForBigDecimal();
    validator.initialize(m);
    testSignBigDecimal(validator, false, false);
    validator = new NegativeOrZeroValidatorForBigInteger();
    validator.initialize(m);
    testSignBigInteger(validator, false, false);
    validator = new NegativeOrZeroValidatorForLong();
    validator.initialize(m);
    testSignLong(validator, false, false);
    validator = new NegativeOrZeroValidatorForFloat();
    validator.initialize(m);
    testSignFloat(validator, false, false);
    validator = new NegativeOrZeroValidatorForDouble();
    validator.initialize(m);
    testSignDouble(validator, false, false);
    validator = new NegativeOrZeroValidatorForShort();
    validator.initialize(m);
    testSignShort(validator, false, false);
    validator = new NegativeOrZeroValidatorForByte();
    validator.initialize(m);
    testSignByte(validator, false, false);
    validator = new NegativeOrZeroValidatorForInteger();
    validator.initialize(m);
    testSignInteger(validator, false, false);
}
Also used : NegativeOrZeroValidatorForBigDecimal(org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForBigDecimal) NegativeOrZeroValidatorForByte(org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForByte) ConstraintValidator(jakarta.validation.ConstraintValidator) NegativeOrZeroValidatorForNumber(org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForNumber) NegativeOrZeroValidatorForBigInteger(org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForBigInteger) NegativeOrZeroValidatorForShort(org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForShort) NegativeOrZeroValidatorForLong(org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForLong) NegativeOrZeroValidatorForDouble(org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForDouble) NegativeOrZeroValidatorForInteger(org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForInteger) NegativeOrZeroValidatorForFloat(org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForFloat)

Aggregations

ConstraintValidator (jakarta.validation.ConstraintValidator)12 Validator (jakarta.validation.Validator)6 NotNullValidator (org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator)5 Test (org.testng.annotations.Test)5 TestForIssue (org.hibernate.validator.testutil.TestForIssue)4 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)4 HibernateConstraintValidatorInitializationContext (org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext)3 DefaultScriptEvaluatorFactory (org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory)3 ScriptEvaluatorFactory (org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory)2 ClockProvider (jakarta.validation.ClockProvider)1 ConstraintValidatorFactory (jakarta.validation.ConstraintValidatorFactory)1 ConstraintViolation (jakarta.validation.ConstraintViolation)1 ValidatorFactory (jakarta.validation.ValidatorFactory)1 Size (jakarta.validation.constraints.Size)1 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 HibernateValidator (org.hibernate.validator.HibernateValidator)1 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)1