Search in sources :

Example 16 with SizeDef

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

the class MethodConstraintMappingTest method testReturnValueConstraint.

@Test
public void testReturnValueConstraint() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", String.class).returnValue().constraint(new SizeDef().min(1).max(10));
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.greet("Hello");
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").returnValue()));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test)

Example 17 with SizeDef

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

the class MethodConstraintMappingTest method testMultipleReturnValueConstraints.

@Test
public void testMultipleReturnValueConstraints() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", String.class).returnValue().constraint(new SizeDef().min(1).max(10)).constraint(new SizeDef().min(2).max(10));
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.greet("Hello");
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").returnValue()), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().method("greet").returnValue()));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test)

Example 18 with SizeDef

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

the class MethodConstraintMappingTest method testProgrammaticAndAnnotationReturnValueConstraintsAddUp.

@Test
public void testProgrammaticAndAnnotationReturnValueConstraintsAddUp() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", String.class, String.class).returnValue().constraint(new SizeDef().min(2).max(10));
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.greet("Hello", "World");
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").returnValue()), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().method("greet").returnValue()));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test)

Example 19 with SizeDef

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

the class MethodConstraintMappingTest method testMultipleParameterConstraintsAtDifferentParameters.

@Test
public void testMultipleParameterConstraintsAtDifferentParameters() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", 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);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.greet("", "");
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").parameter("string1", 0)), violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").parameter("string2", 1)));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test)

Example 20 with SizeDef

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

the class MethodConstraintMappingTest method testMultipleParameterConstraintsAtSameParameter.

@Test
public void testMultipleParameterConstraintsAtSameParameter() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", String.class).parameter(0).constraint(new SizeDef().min(1).max(10)).constraint(new SizeDef().min(2).max(10));
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.greet("");
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").parameter("string", 0)), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().method("greet").parameter("string", 0)));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test)

Aggregations

SizeDef (org.hibernate.validator.cfg.defs.SizeDef)40 Test (org.testng.annotations.Test)37 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)34 HibernateValidator (org.hibernate.validator.HibernateValidator)29 Validator (jakarta.validation.Validator)27 TestForIssue (org.hibernate.validator.testutil.TestForIssue)21 ConstraintViolation (jakarta.validation.ConstraintViolation)18 ConstraintViolationException (jakarta.validation.ConstraintViolationException)15 Size (jakarta.validation.constraints.Size)11 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 MinDef (org.hibernate.validator.cfg.defs.MinDef)4 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)3 Test (org.junit.Test)3 Min (jakarta.validation.constraints.Min)2 GenericConstraintDef (org.hibernate.validator.cfg.GenericConstraintDef)2 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1