Search in sources :

Example 26 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.

the class MethodConstraintMappingTest method testParameterConstraint.

@Test
public void testParameterConstraint() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", User.class).parameter(0).constraint(new NotNullDef());
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.greet((User) null);
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().method("greet").parameter("user", 0)));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Test(org.testng.annotations.Test)

Example 27 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.

the class MethodConstraintMappingTest method constraintConfiguredOnMethodIsEvaluatedByPropertyValidation.

@Test
public void constraintConfiguredOnMethodIsEvaluatedByPropertyValidation() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("getHello").returnValue().constraint(new NotNullDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<GreetingServiceImpl>> violations = validator.validateProperty(new GreetingServiceImpl(), "hello");
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().property("hello")));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) NotNull(jakarta.validation.constraints.NotNull) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 28 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.

the class MultipleConstraintMappingsTest method testSamePropertyConfiguredSeveralTimesCausesException.

@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000172.*")
public void testSamePropertyConfiguredSeveralTimesCausesException() {
    ConstraintMapping marathonMapping = config.createConstraintMapping();
    marathonMapping.type(Marathon.class).getter("name").constraint(new NotNullDef()).getter("name");
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Test(org.testng.annotations.Test)

Example 29 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.

the class MultipleConstraintMappingsTest method testSameParameterConfiguredSeveralTimesCausesException.

@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000174.*")
public void testSameParameterConfiguredSeveralTimesCausesException() {
    ConstraintMapping marathonMapping = config.createConstraintMapping();
    marathonMapping.type(Marathon.class).method("setTournamentDate", Date.class).parameter(0).constraint(new NotNullDef()).parameter(0);
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 30 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.

the class MultipleConstraintMappingsTest method testSameMethodConfiguredSeveralTimesCausesException.

@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000173.*")
public void testSameMethodConfiguredSeveralTimesCausesException() {
    ConstraintMapping marathonMapping = config.createConstraintMapping();
    marathonMapping.type(Marathon.class).method("setTournamentDate", Date.class).parameter(0).constraint(new NotNullDef()).method("setTournamentDate", Date.class);
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)47 Test (org.testng.annotations.Test)42 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)37 HibernateValidator (org.hibernate.validator.HibernateValidator)31 Validator (jakarta.validation.Validator)28 TestForIssue (org.hibernate.validator.testutil.TestForIssue)22 ConstraintViolation (jakarta.validation.ConstraintViolation)19 ConstraintViolationException (jakarta.validation.ConstraintViolationException)10 NotNull (jakarta.validation.constraints.NotNull)9 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 NotEmptyDef (org.hibernate.validator.cfg.defs.NotEmptyDef)4 Test (org.junit.Test)4 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)3 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 MinDef (org.hibernate.validator.cfg.defs.MinDef)2