use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class MethodConstraintMappingTest method testReturnValueConstraint.
@Test
public void testReturnValueConstraint() {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).method("greet", String.class).returnValue().constraint(new SizeDef().min(1).max(10));
config.addMapping(mapping);
try {
GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
service.greet("Hello");
fail("Expected exception wasn't thrown.");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").returnValue()));
}
}
use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class MethodConstraintMappingTest method testMultipleReturnValueConstraints.
@Test
public void testMultipleReturnValueConstraints() {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).method("greet", String.class).returnValue().constraint(new SizeDef().min(1).max(10)).constraint(new SizeDef().min(2).max(10));
config.addMapping(mapping);
try {
GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
service.greet("Hello");
fail("Expected exception wasn't thrown.");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").returnValue()), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().method("greet").returnValue()));
}
}
use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class MethodConstraintMappingTest method testProgrammaticAndAnnotationReturnValueConstraintsAddUp.
@Test
public void testProgrammaticAndAnnotationReturnValueConstraintsAddUp() {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).method("greet", String.class, String.class).returnValue().constraint(new SizeDef().min(2).max(10));
config.addMapping(mapping);
try {
GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
service.greet("Hello", "World");
fail("Expected exception wasn't thrown.");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").returnValue()), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().method("greet").returnValue()));
}
}
use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class MethodConstraintMappingTest method testMultipleParameterConstraintsAtDifferentParameters.
@Test
public void testMultipleParameterConstraintsAtDifferentParameters() {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).method("greet", String.class, String.class).parameter(0).constraint(new SizeDef().min(1).max(10)).parameter(1).constraint(new SizeDef().min(1).max(10));
config.addMapping(mapping);
try {
GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
service.greet("", "");
fail("Expected exception wasn't thrown.");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").parameter("string1", 0)), violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").parameter("string2", 1)));
}
}
use of org.hibernate.validator.cfg.defs.SizeDef in project hibernate-validator by hibernate.
the class MethodConstraintMappingTest method testMultipleParameterConstraintsAtSameParameter.
@Test
public void testMultipleParameterConstraintsAtSameParameter() {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).method("greet", String.class).parameter(0).constraint(new SizeDef().min(1).max(10)).constraint(new SizeDef().min(2).max(10));
config.addMapping(mapping);
try {
GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
service.greet("");
fail("Expected exception wasn't thrown.");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").parameter("string", 0)), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().method("greet").parameter("string", 0)));
}
}
Aggregations