use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForReturnValueTest method canDeclareContainerElementCascadesForReturnValueProgrammatically.
@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementCascadesForReturnValueProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test4").returnValue().containerElementType().valid().type(Fish.class).field("name").constraint(new NotNullDef());
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
fishTank.test4();
fail("Expected exception wasn't raised");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
}
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class CompositeConstraintTest method testCorrectAnnotationTypeForWithReportAsSingleViolation.
@Test
@TestForIssue(jiraKey = "HV-182")
public void testCorrectAnnotationTypeForWithReportAsSingleViolation() {
Validator currentValidator = ValidatorUtil.getValidator();
for (int i = 0; i < 100; i++) {
Set<ConstraintViolation<Person>> constraintViolations = currentValidator.validate(new Person(null, "Gerhard"));
assertThat(constraintViolations).containsOnlyViolations(violationOf(ValidNameSingleViolation.class).withMessage("invalid name"));
constraintViolations = currentValidator.validate(new Person("G", "Gerhard"));
assertThat(constraintViolations).containsOnlyViolations(violationOf(ValidNameSingleViolation.class).withMessage("invalid name"));
}
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class EagerEvaluationOfComposingConstraintsTest method testComposedConstraintWithoutReportAsSingleViolationEvaluatesAllConstraints.
@Test
@TestForIssue(jiraKey = "HV-639")
public void testComposedConstraintWithoutReportAsSingleViolationEvaluatesAllConstraints() {
Validator validator = ValidatorUtil.getValidator();
Foo foo = new Foo();
validator.validate(foo);
assertEquals(InvocationCounter.getInvocationCount(foo), 3, "Each constraint should be evaluated since w/o @ReportAsSingleViolation there might be several violations");
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class ValidatorResolutionTest method testResolutionOfMultipleSizeValidators.
@Test
public void testResolutionOfMultipleSizeValidators() {
Validator validator = ValidatorUtil.getValidator();
Suburb suburb = new Suburb();
List<Integer> postcodes = new ArrayList<Integer>();
postcodes.add(12345);
suburb.setIncludedPostCodes(postcodes);
// all values are null and should pass
Set<ConstraintViolation<Suburb>> constraintViolations = validator.validate(suburb);
assertNoViolations(constraintViolations);
suburb.setName("");
constraintViolations = validator.validate(suburb);
assertThat(constraintViolations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 5 and 10").withProperty("name").withRootBeanClass(Suburb.class).withInvalidValue(""));
suburb.setName("Hoegsbo");
constraintViolations = validator.validate(suburb);
assertNoViolations(constraintViolations);
suburb.addFacility(Suburb.Facility.SHOPPING_MALL, false);
constraintViolations = validator.validate(suburb);
assertThat(constraintViolations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 2 and 2").withProperty("facilities").withRootBeanClass(Suburb.class).withInvalidValue(suburb.getFacilities()));
suburb.addFacility(Suburb.Facility.BUS_TERMINAL, true);
constraintViolations = validator.validate(suburb);
assertNoViolations(constraintViolations);
suburb.addStreetName("Sikelsgatan");
constraintViolations = validator.validate(suburb);
assertThat(constraintViolations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 2 and 2147483647").withProperty("streetNames").withRootBeanClass(Suburb.class).withInvalidValue(suburb.getStreetNames()));
suburb.addStreetName("Marklandsgatan");
constraintViolations = validator.validate(suburb);
assertNoViolations(constraintViolations);
Coordinate[] boundingBox = new Coordinate[3];
boundingBox[0] = new Coordinate(0L, 0L);
boundingBox[1] = new Coordinate(0L, 1L);
boundingBox[2] = new Coordinate(1L, 0L);
suburb.setBoundingBox(boundingBox);
constraintViolations = validator.validate(suburb);
assertThat(constraintViolations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 4 and 1000").withProperty("boundingBox").withRootBeanClass(Suburb.class).withInvalidValue(suburb.getBoundingBox()));
boundingBox = new Coordinate[4];
boundingBox[0] = new Coordinate(0L, 0L);
boundingBox[1] = new Coordinate(0L, 1L);
boundingBox[2] = new Coordinate(1L, 0L);
boundingBox[3] = new Coordinate(1L, 1L);
suburb.setBoundingBox(boundingBox);
constraintViolations = validator.validate(suburb);
assertNoViolations(constraintViolations);
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class ValidatorResolutionTest method testSubTypeArrayIsSubtypeOfSuperTypeArray.
@Test
@TestForIssue(jiraKey = "HV-233")
public void testSubTypeArrayIsSubtypeOfSuperTypeArray() {
Validator validator = ValidatorUtil.getValidator();
SubTypeEntity testEntity = new SubTypeEntity(new SubType[] {});
Set<ConstraintViolation<SubTypeEntity>> constraintViolations = validator.validate(testEntity);
assertNoViolations(constraintViolations);
}
Aggregations