use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class MethodConstraintMappingTest method cascadeConfiguredOnPropertyIsEvaluatedByMethodValidation.
@Test
public void cascadeConfiguredOnPropertyIsEvaluatedByMethodValidation() {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).getter("user").valid();
config.addMapping(mapping);
try {
GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
service.getUser();
fail("Expected exception wasn't thrown.");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().method("getUser").returnValue().property("name")));
}
}
use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class ProgrammaticConstraintDefinitionsTest method doExecutableProgrammaticTest.
private void doExecutableProgrammaticTest(ParameterScriptAssertDef parameterScriptAssertDef, String content, boolean error) throws NoSuchMethodException {
HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(Foo.class).ignoreAllAnnotations().method("setSource", String.class).crossParameter().constraint(parameterScriptAssertDef);
Validator validator = config.addMapping(mapping).parameterNameProvider(new PrefixableParameterNameProvider("param")).buildValidatorFactory().getValidator();
Foo bar = getValidatingProxy(new Bar(""), validator);
try {
bar.setSource(content);
if (error) {
fail("Should throw an exception");
}
} catch (ConstraintViolationException e) {
if (!error) {
fail("Should not throw an exception");
}
}
}
use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareContainerElementCascadesForParameterProgrammatically.
@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementCascadesForParameterProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test4", Optional.class).parameter(0).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(Optional.of(new Fish()));
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.ConstraintViolationException in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareContainerElementConstraintsForListContainingArrayTypeParameterProgrammatically.
// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForListContainingArrayTypeParameterProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test6", List.class).parameter(0).containerElementType(0, 0).constraint(new SizeDef().max(5));
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
List<String[]> fishNamesByMonth = new ArrayList<>();
fishNamesByMonth.add(new String[] { "Too Long" });
fishTank.test6(fishNamesByMonth);
fail("Expected exception wasn't raised");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
}
}
use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeParameterProgrammatically.
// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeParameterProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test7", String[][].class).parameter(0).containerElementType(0, 0).constraint(new SizeDef().max(5));
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
String[][] fishNamesByMonthAsArray = new String[][] { new String[] { "Too Long" } };
fishTank.test7(fishNamesByMonthAsArray);
fail("Expected exception wasn't raised");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
}
}
Aggregations