use of org.hibernate.validator.cfg.GenericConstraintDef 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.GenericConstraintDef in project hibernate-validator by hibernate.
the class ConstraintApiTest method executableConfiguration.
@Test
public void executableConfiguration() {
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
// tag::executableConfiguration[]
ConstraintMapping constraintMapping = configuration.createConstraintMapping();
constraintMapping.type(Car.class).constructor(String.class).parameter(0).constraint(new SizeDef().min(3).max(50)).returnValue().valid().method("drive", int.class).parameter(0).constraint(new MaxDef().value(75)).method("load", List.class, List.class).crossParameter().constraint(new GenericConstraintDef<>(LuggageCountMatchesPassengerCount.class).param("piecesOfLuggagePerPassenger", 2)).method("getDriver").returnValue().constraint(new NotNullDef()).valid();
// end::executableConfiguration[]
}
use of org.hibernate.validator.cfg.GenericConstraintDef in project hibernate-validator by hibernate.
the class ConstraintApiTest method genericConstraintDef.
@Test
public void genericConstraintDef() {
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
// tag::genericConstraintDef[]
ConstraintMapping constraintMapping = configuration.createConstraintMapping();
constraintMapping.type(Car.class).field("licensePlate").constraint(new GenericConstraintDef<>(CheckCase.class).param("value", CaseMode.UPPER));
// end::genericConstraintDef[]
}
use of org.hibernate.validator.cfg.GenericConstraintDef in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testDefConstraintFollowedByGenericConstraint.
@Test
public void testDefConstraintFollowedByGenericConstraint() {
mapping.type(Marathon.class).field("numberOfHelpers").constraint(new MinDef().value(1)).constraint(new GenericConstraintDef<>(Min.class).param("value", 2L));
BeanConfiguration<Marathon> beanConfiguration = getBeanConfiguration(Marathon.class);
assertNotNull(beanConfiguration);
assertEquals(getConstrainedField(beanConfiguration, "numberOfHelpers").getConstraints().size(), 2);
}
use of org.hibernate.validator.cfg.GenericConstraintDef in project hibernate-validator by hibernate.
the class ConstructorConstraintMappingTest method testProgrammaticAndAnnotationReturnValueConstraintsAddUp.
@Test
public void testProgrammaticAndAnnotationReturnValueConstraintsAddUp() throws Exception {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).constructor(CharSequence.class).returnValue().constraint(new GenericConstraintDef<>(ValidGreetingService.class).message("invalid 2"));
config.addMapping(mapping);
Constructor<GreetingService> constructor = GreetingService.class.getConstructor(CharSequence.class);
ExecutableValidator executableValidator = getConfiguredExecutableValidator();
Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorReturnValue(constructor, new GreetingService(""));
assertThat(violations).containsOnlyViolations(violationOf(ValidGreetingService.class).withMessage("invalid 1").withPropertyPath(pathWith().constructor(GreetingService.class).returnValue()), violationOf(ValidGreetingService.class).withMessage("invalid 2").withPropertyPath(pathWith().constructor(GreetingService.class).returnValue()));
}
Aggregations