use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testThatSpecificParameterCanBeSetAfterInvokingMethodFromBaseType.
@Test
public void testThatSpecificParameterCanBeSetAfterInvokingMethodFromBaseType() {
mapping.type(Marathon.class).getter("name").constraint(new SizeDef().message("too short").min(3));
config.addMapping(mapping);
Validator validator = config.buildValidatorFactory().getValidator();
Marathon marathon = new Marathon();
marathon.setName("NY");
Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("too short"));
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testDefaultGroupSequenceProvider.
@Test
public void testDefaultGroupSequenceProvider() {
mapping.type(Marathon.class).defaultGroupSequenceProviderClass(MarathonDefaultGroupSequenceProvider.class).getter("name").constraint(new NotNullDef().groups(Foo.class)).getter("runners").constraint(new NotEmptyDef());
config.addMapping(mapping);
Validator validator = config.buildValidatorFactory().getValidator();
Marathon marathon = new Marathon();
Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
marathon.setName("Stockholm Marathon");
violations = validator.validate(marathon);
assertThat(violations).containsOnlyViolations(violationOf(NotEmpty.class).withMessage("must not be empty"));
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testRangeDef.
@Test(description = "HV-355 (parameter names of RangeDef wrong)")
public void testRangeDef() {
mapping.type(Runner.class).getter("age").constraint(new RangeDef().min(12).max(99));
config.addMapping(mapping);
Validator validator = config.buildValidatorFactory().getValidator();
Set<ConstraintViolation<Runner>> violations = validator.validate(new Runner());
assertThat(violations).containsOnlyViolations(violationOf(Range.class).withMessage("must be between 12 and 99"));
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testInheritedConstraint.
@Test
public void testInheritedConstraint() {
mapping.type(Marathon.class).getter("name").constraint(new NotNullDef()).type(Tournament.class).getter("tournamentDate").constraint(new FutureDef());
config.addMapping(mapping);
Validator validator = config.buildValidatorFactory().getValidator();
Marathon marathon = new Marathon();
marathon.setName("New York Marathon");
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.YEAR, -1);
marathon.setTournamentDate(cal.getTime());
Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
assertThat(violations).containsOnlyViolations(violationOf(Future.class).withMessage("must be a future date"));
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testCustomConstraintType.
@Test
public void testCustomConstraintType() {
mapping.type(Marathon.class).constraint(new GenericConstraintDef<>(MarathonConstraint.class).param("minRunner", 100).message("Needs more runners"));
config.addMapping(mapping);
Validator validator = config.buildValidatorFactory().getValidator();
Marathon marathon = new Marathon();
marathon.setName("Stockholm Marathon");
Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
assertThat(violations).containsOnlyViolations(violationOf(MarathonConstraint.class).withMessage("Needs more runners"));
for (int i = 0; i < 100; i++) {
marathon.addRunner(new Runner());
}
violations = validator.validate(marathon);
assertNoViolations(violations);
}
Aggregations