use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testMultipleConstraintOfTheSameType.
@Test
public void testMultipleConstraintOfTheSameType() {
mapping.type(Marathon.class).getter("name").constraint(new SizeDef().min(5)).constraint(new SizeDef().min(10));
config.addMapping(mapping);
Validator validator = config.buildValidatorFactory().getValidator();
Marathon marathon = new Marathon();
marathon.setName("Foo");
Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 10 and 2147483647"), violationOf(Size.class).withMessage("size must be between 5 and 2147483647"));
marathon.setName("Foobar");
violations = validator.validate(marathon);
assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 10 and 2147483647"));
marathon.setName("Stockholm Marathon");
violations = validator.validate(marathon);
assertNoViolations(violations);
}
use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testDeprecatedPropertyMethodForFieldAndGetterProgrammaticConstraintDefinition.
@Test
@SuppressWarnings("deprecation")
public void testDeprecatedPropertyMethodForFieldAndGetterProgrammaticConstraintDefinition() {
mapping.type(Marathon.class).property("name", ElementType.METHOD).constraint(new SizeDef().min(5)).constraint(new SizeDef().min(10)).property("runners", ElementType.FIELD).constraint(new SizeDef().max(10).min(1));
config.addMapping(mapping);
Validator validator = config.buildValidatorFactory().getValidator();
Marathon marathon = new Marathon();
marathon.setName("Foo");
Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 10 and 2147483647"), violationOf(Size.class).withMessage("size must be between 5 and 2147483647"), violationOf(Size.class).withMessage("size must be between 1 and 10"));
}
use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testThatSpecificParameterCanBeSetAfterAddingGenericConstraintDef.
@Test(description = "HV-404: Introducing ConstraintsForType#genericConstraint(Class) allows to set specific parameters on following specific constraints.")
public void testThatSpecificParameterCanBeSetAfterAddingGenericConstraintDef() {
mapping.type(Marathon.class).constraint(new GenericConstraintDef<>(MarathonConstraint.class).param("minRunner", 1)).getter("name").constraint(new SizeDef().message("name too short").min(3));
config.addMapping(mapping);
Validator validator = config.buildValidatorFactory().getValidator();
Marathon marathon = new Marathon();
marathon.setName("NY");
marathon.addRunner(new Runner());
Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("name too short"));
}
use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testProgrammaticAndAnnotationFieldConstraintsAddUp.
@Test
public void testProgrammaticAndAnnotationFieldConstraintsAddUp() {
mapping.type(User.class).field("firstName").constraint(new SizeDef().min(2).max(10));
config.addMapping(mapping);
Validator validator = config.buildValidatorFactory().getValidator();
Set<ConstraintViolation<User>> violations = validator.validateProperty(new User("", ""), "firstName");
assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10"), violationOf(Size.class).withMessage("size must be between 2 and 10"));
}
use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class ConstructorConstraintMappingTest method testMultipleParameterConstraintsAtSameParameter.
@Test
public void testMultipleParameterConstraintsAtSameParameter() throws Exception {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).constructor(String.class).parameter(0).constraint(new SizeDef().min(1).max(10)).constraint(new SizeDef().min(2).max(10));
config.addMapping(mapping);
Constructor<GreetingService> constructor = GreetingService.class.getConstructor(String.class);
Object[] parameterValues = new Object[] { "" };
ExecutableValidator executableValidator = getConfiguredExecutableValidator();
Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)));
}
Aggregations