use of jakarta.validation.Validator in project org.openntf.xsp.jakartaee by OpenNTF.
the class TestValidationBasics method testExampleBean.
@Test
public void testExampleBean() {
ExampleBean bean = new ExampleBean();
Validator validator = XPagesValidationUtil.constructGenericValidator();
{
Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
assertEquals("Bean should have two violations", 2, violations.size());
}
bean.setFoo("");
{
Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
assertEquals("Bean should have two violations", 2, violations.size());
}
bean.setFoo("hey");
{
Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
assertEquals("Bean should have one violation", 1, violations.size());
}
bean.setBar("h");
{
Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
assertEquals("Bean should have one violation", 1, violations.size());
}
bean.setBar("h12345678910");
{
Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
assertEquals("Bean should have one violation", 1, violations.size());
}
bean.setBar("hey there");
{
Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
assertEquals("Bean should have no violations", 0, violations.size());
}
}
use of jakarta.validation.Validator in project glassfish-hk2 by eclipse-ee4j.
the class XmlRootHandleImpl method startValidating.
@Override
public void startValidating() {
if (changeControl == null)
throw new IllegalStateException();
Validator validator = changeControl.findOrCreateValidator();
if (root == null)
return;
Set<ConstraintViolation<Object>> violations = validator.<Object>validate(root);
if (violations == null || violations.isEmpty())
return;
throw new ConstraintViolationException(violations);
}
use of jakarta.validation.Validator in project today-framework by TAKETODAY.
the class ValidatorFactoryTests method testSimpleValidationWithCustomProvider.
@Test
public void testSimpleValidationWithCustomProvider() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setProviderClass(HibernateValidator.class);
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
assertThat(result.size()).isEqualTo(2);
for (ConstraintViolation<ValidPerson> cv : result) {
String path = cv.getPropertyPath().toString();
assertThat(path).matches(actual -> "name".equals(actual) || "address.street".equals(actual));
assertThat(cv.getConstraintDescriptor().getAnnotation()).isInstanceOf(NotNull.class);
}
Validator nativeValidator = validator.unwrap(Validator.class);
assertThat(nativeValidator.getClass().getName().startsWith("org.hibernate")).isTrue();
assertThat(validator.unwrap(ValidatorFactory.class)).isInstanceOf(HibernateValidatorFactory.class);
assertThat(validator.unwrap(HibernateValidatorFactory.class)).isInstanceOf(HibernateValidatorFactory.class);
validator.destroy();
}
use of jakarta.validation.Validator in project today-framework by TAKETODAY.
the class ValidatorAdapterTests method testUnwrap.
@Test
public void testUnwrap() {
Validator nativeValidator = validatorAdapter.unwrap(Validator.class);
assertThat(nativeValidator).isSameAs(this.nativeValidator);
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareDeeplyNestedContainerElementConstraintsOnMultipleDifferentTypeArgumentsForFieldProgrammatically.
@Test
@TestForIssue(jiraKey = "HV-1614")
public void canDeclareDeeplyNestedContainerElementConstraintsOnMultipleDifferentTypeArgumentsForFieldProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(FishTank.class).field("tagsOfFishOfTheMonth").containerElementType(0, 0).constraint(new LengthDef().min(10).max(20)).containerElementType(0, 1, 0).constraint(new NotNullDef());
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
Set<ConstraintViolation<FishTank>> violations = validator.validate(new FishTank());
assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"), violationOf(Length.class).withMessage("length must be between 10 and 20"));
}
Aggregations