use of jakarta.validation.ConstraintValidator in project hibernate-validator by hibernate.
the class SizeValidatorTest method getValidatorMin1Max2.
private <T> ConstraintValidator<Size, T> getValidatorMin1Max2(Class<?> validatorClass) throws Exception {
ConstraintAnnotationDescriptor.Builder<Size> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Size.class);
descriptorBuilder.setAttribute("min", 1);
descriptorBuilder.setAttribute("max", 2);
descriptorBuilder.setMessage("{validator.max}");
Size m = descriptorBuilder.build().getAnnotation();
@SuppressWarnings("unchecked") ConstraintValidator<Size, T> validator = (ConstraintValidator<Size, T>) validatorClass.getConstructor().newInstance();
validator.initialize(m);
return validator;
}
use of jakarta.validation.ConstraintValidator 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);
}
Aggregations