use of jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.
the class GroupTest method driveAway.
@Test
public void driveAway() {
// tag::driveAway[]
// create a car and check that everything is ok with it.
Car car = new Car("Morris", "DD-AB-123", 2);
Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car);
assertEquals(0, constraintViolations.size());
// but has it passed the vehicle inspection?
constraintViolations = validator.validate(car, CarChecks.class);
assertEquals(1, constraintViolations.size());
assertEquals("The car has to pass the vehicle inspection first", constraintViolations.iterator().next().getMessage());
// let's go to the vehicle inspection
car.setPassedVehicleInspection(true);
assertEquals(0, validator.validate(car, CarChecks.class).size());
// now let's add a driver. He is 18, but has not passed the driving test yet
Driver john = new Driver("John Doe");
john.setAge(18);
car.setDriver(john);
constraintViolations = validator.validate(car, DriverChecks.class);
assertEquals(1, constraintViolations.size());
assertEquals("You first have to pass the driving test", constraintViolations.iterator().next().getMessage());
// ok, John passes the test
john.passedDrivingTest(true);
assertEquals(0, validator.validate(car, DriverChecks.class).size());
// just checking that everything is in order now
assertEquals(0, validator.validate(car, Default.class, CarChecks.class, DriverChecks.class).size());
// end::driveAway[]
}
use of jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.
the class CarTest method returnValueConstraintsAddUp.
@Test
public void returnValueConstraintsAddUp() throws Exception {
Car object = new Car();
Method method = Car.class.getMethod("getPassengers");
Object returnValue = Collections.<Person>emptyList();
Set<ConstraintViolation<Car>> violations = executableValidator.validateReturnValue(object, method, returnValue);
assertEquals(1, violations.size());
assertEquals(Size.class, violations.iterator().next().getConstraintDescriptor().getAnnotation().annotationType());
}
use of jakarta.validation.ConstraintViolation 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 jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.
the class ConstructorConstraintMappingTest method testGenericParameterConstraint.
@Test
public void testGenericParameterConstraint() throws Exception {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).constructor(String.class).parameter(0).constraint(new GenericConstraintDef<>(Size.class).param("min", 1).param("max", 10));
config.addMapping(mapping);
Constructor<GreetingService> constructor = GreetingService.class.getConstructor(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)));
}
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)));
}
Aggregations