use of jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.
the class ConstructorConstraintMappingTest method testMultipleParameterConstraintsAtDifferentParameters.
@Test
public void testMultipleParameterConstraintsAtDifferentParameters() throws Exception {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).constructor(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);
Constructor<GreetingService> constructor = GreetingService.class.getConstructor(String.class, String.class);
Object[] parameterValues = new Object[] { "", "" };
ExecutableValidator executableValidator = getConfiguredExecutableValidator();
Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)), violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("anotherMessage", 1)));
}
use of jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.
the class ConstructorConstraintMappingTest method testProgrammaticAndAnnotationParameterConstraintsAddUp.
@Test
public void testProgrammaticAndAnnotationParameterConstraintsAddUp() throws Exception {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).constructor(CharSequence.class).parameter(0).constraint(new SizeDef().min(2).max(10));
config.addMapping(mapping);
Constructor<GreetingService> constructor = GreetingService.class.getConstructor(CharSequence.class);
Object[] parameterValues = new Object[] { "" };
ExecutableValidator executableValidator = getConfiguredExecutableValidator();
Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)));
}
use of jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.
the class ConstructorConstraintMappingTest method shouldDetermineConstraintTargetForReturnValueConstraint.
@Test
public void shouldDetermineConstraintTargetForReturnValueConstraint() throws Exception {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).constructor(String.class, String.class).returnValue().constraint(new GenericConstraintDef<>(GenericAndCrossParameterConstraint.class));
config.addMapping(mapping);
Constructor<GreetingService> constructor = GreetingService.class.getConstructor(String.class, String.class);
ExecutableValidator executableValidator = getConfiguredExecutableValidator();
Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorReturnValue(constructor, new GreetingService("", ""));
assertThat(violations).containsOnlyViolations(violationOf(GenericAndCrossParameterConstraint.class).withMessage("default message").withPropertyPath(pathWith().constructor(GreetingService.class).returnValue()));
}
use of jakarta.validation.ConstraintViolation 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)));
}
use of jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.
the class ConstructorConstraintMappingTest method crossParameterConstraint.
@Test
public void crossParameterConstraint() throws Exception {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).constructor(String.class, String.class).crossParameter().constraint(new GenericConstraintDef<>(GenericAndCrossParameterConstraint.class));
config.addMapping(mapping);
Constructor<GreetingService> constructor = GreetingService.class.getConstructor(String.class, String.class);
Object[] parameterValues = new Object[] { "", "" };
ExecutableValidator executableValidator = getConfiguredExecutableValidator();
Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
assertThat(violations).containsOnlyViolations(violationOf(GenericAndCrossParameterConstraint.class).withMessage("default message").withPropertyPath(pathWith().constructor(GreetingService.class).crossParameter()));
}
Aggregations