use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.
the class MethodConstraintMappingTest method constraintConfiguredOnPropertyIsEvaluatedByMethodValidation.
@Test
public void constraintConfiguredOnPropertyIsEvaluatedByMethodValidation() {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).getter("hello").constraint(new NotNullDef());
config.addMapping(mapping);
try {
GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
service.getHello();
fail("Expected exception wasn't thrown.");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().method("getHello").returnValue()));
}
}
use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.
the class MultipleConstraintMappingsTest method testMultipleConstraintMappings.
@Test
@TestForIssue(jiraKey = "HV-500")
public void testMultipleConstraintMappings() {
ConstraintMapping marathonMapping = config.createConstraintMapping();
marathonMapping.type(Marathon.class).getter("name").constraint(new NotNullDef());
ConstraintMapping runnerMapping = config.createConstraintMapping();
runnerMapping.type(Runner.class).getter("name").constraint(new NotNullDef());
config.addMapping(marathonMapping);
config.addMapping(runnerMapping);
Validator validator = config.buildValidatorFactory().getValidator();
BeanDescriptor beanDescriptor = validator.getConstraintsForClass(Marathon.class);
assertTrue(beanDescriptor.isBeanConstrained(), "There should be constraints defined on the Marathon class");
assertEquals(beanDescriptor.getConstrainedProperties().iterator().next().getPropertyName(), "name", "The property name should be constrained");
beanDescriptor = validator.getConstraintsForClass(Runner.class);
assertTrue(beanDescriptor.isBeanConstrained(), "There should be constraints defined on the Runner class");
assertEquals(beanDescriptor.getConstrainedProperties().iterator().next().getPropertyName(), "name", "The property name should be constrained");
}
use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForFieldTest method configuringSameContainerElementTwiceCausesException.
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000214.*")
@TestForIssue(jiraKey = "HV-1239")
public void configuringSameContainerElementTwiceCausesException() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(FishTank.class).field("tagsOfFishOfTheMonth").containerElementType(0, 1, 0).constraint(new NotNullDef()).containerElementType(0, 1, 0);
}
use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.
the class ConstructorConstraintMappingTest method testCascadingConstructorParameterDefinitionWithGroupConversion.
@Test
public void testCascadingConstructorParameterDefinitionWithGroupConversion() throws Exception {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).constructor(User.class).parameter(0).valid().convertGroup(Default.class).to(TestGroup.class).type(User.class).field("name").constraint(new NotNullDef().message("name must not be null").groups(TestGroup.class));
config.addMapping(mapping);
Constructor<GreetingService> constructor = GreetingService.class.getConstructor(User.class);
Object[] parameterValues = new Object[] { new User(null) };
ExecutableValidator executableValidator = getConfiguredExecutableValidator();
Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("name must not be null").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("user", 0).property("name")));
}
use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.
the class ConstructorConstraintMappingTest method testParameterConstraint.
@Test
public void testParameterConstraint() throws Exception {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).constructor(User.class).parameter(0).constraint(new NotNullDef());
config.addMapping(mapping);
Constructor<GreetingService> constructor = GreetingService.class.getConstructor(User.class);
Object[] parameterValues = new Object[] { null };
ExecutableValidator executableValidator = getConfiguredExecutableValidator();
Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("user", 0)));
}
Aggregations