use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class XmlMappingTest method testConstraintsFromXmlAndProgrammaticApiAddUp.
@Test
@TestForIssue(jiraKey = "HV-480")
public void testConstraintsFromXmlAndProgrammaticApiAddUp() {
final HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(HibernateValidator.class);
// given
final ConstraintMapping programmaticMapping = configuration.createConstraintMapping();
programmaticMapping.type(Customer.class).field("firstName").constraint(new SizeDef().min(2).max(10));
final InputStream xmlMapping = XmlMappingTest.class.getResourceAsStream("hv-480-mapping.xml");
configuration.addMapping(programmaticMapping);
configuration.addMapping(xmlMapping);
final Customer customer = new Customer();
customer.setFirstName("");
// when
final Set<ConstraintViolation<Customer>> violations = configuration.buildValidatorFactory().getValidator().validate(customer);
// then
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 ConstraintApiTest method constraintMapping.
@Test
public void constraintMapping() {
// tag::constraintMapping[]
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
ConstraintMapping constraintMapping = configuration.createConstraintMapping();
constraintMapping.type(Car.class).field("manufacturer").constraint(new NotNullDef()).field("licensePlate").ignoreAnnotations(true).constraint(new NotNullDef()).constraint(new SizeDef().min(2).max(14)).type(RentalCar.class).getter("rentalStation").constraint(new NotNullDef());
Validator validator = configuration.addMapping(constraintMapping).buildValidatorFactory().getValidator();
// end::constraintMapping[]
}
use of org.hibernate.validator.cfg.defs.SizeDef 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.defs.SizeDef in project hibernate-validator by hibernate.
the class ConstraintApiTest method nestedContainerElementConstraint.
@Test
public void nestedContainerElementConstraint() {
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
// tag::nestedContainerElementConstraint[]
ConstraintMapping constraintMapping = configuration.createConstraintMapping();
constraintMapping.type(Car.class).field("manufacturer").constraint(new NotNullDef()).field("licensePlate").ignoreAnnotations(true).constraint(new NotNullDef()).constraint(new SizeDef().min(2).max(14)).field("partManufacturers").containerElementType(0).constraint(new NotNullDef()).containerElementType(1, 0).constraint(new NotNullDef()).type(RentalCar.class).getter("rentalStation").constraint(new NotNullDef());
// end::nestedContainerElementConstraint[]
}
use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class MethodConstraintMappingTest method testOverridingMethodMayDefineSameConstraintsAsOverriddenMethod.
@Test
public void testOverridingMethodMayDefineSameConstraintsAsOverriddenMethod() {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).method("greet", String.class).parameter(0).constraint(new SizeDef().min(5).max(10)).type(GreetingServiceImpl.class).method("greet", String.class).parameter(0).constraint(new SizeDef().min(5).max(10));
config.addMapping(mapping);
GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
try {
service.greet("Hi");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 5 and 10").withPropertyPath(pathWith().method("greet").parameter("string", 0)));
}
}
Aggregations