use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testSingleConstraint.
@Test
public void testSingleConstraint() {
mapping.type(Marathon.class).getter("name").constraint(new NotNullDef());
config.addMapping(mapping);
Validator validator = config.buildValidatorFactory().getValidator();
Set<ConstraintViolation<Marathon>> violations = validator.validate(new Marathon());
assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
}
use of org.hibernate.validator.cfg.defs.NotNullDef 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 org.hibernate.validator.cfg.defs.NotNullDef 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 org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testProgrammaticDefaultGroupSequenceProviderDefinedOnClassWithGroupSequenceAnnotation.
@Test(expectedExceptions = GroupDefinitionException.class, expectedExceptionsMessageRegExp = "HV[0-9]*: Default group sequence and default group sequence provider cannot be defined at the same time.")
public void testProgrammaticDefaultGroupSequenceProviderDefinedOnClassWithGroupSequenceAnnotation() {
mapping.type(A.class).defaultGroupSequenceProviderClass(ADefaultGroupSequenceProvider.class).field("a").constraint(new NotNullDef());
config.addMapping(mapping);
Validator validator = config.buildValidatorFactory().getValidator();
validator.validate(new A());
}
use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.
the class ConstraintMappingTest method testDefaultGroupSequence.
@Test
public void testDefaultGroupSequence() {
mapping.type(Marathon.class).defaultGroupSequence(Foo.class, Marathon.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"));
}
Aggregations