Search in sources :

Example 21 with Size

use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForReturnValueTest method canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeReturnValueProgrammatically.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeReturnValueProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test7").returnValue().containerElementType(0, 0).constraint(new SizeDef().max(5));
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        fishTank.test7();
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) Size(jakarta.validation.constraints.Size) ConstraintViolationException(jakarta.validation.ConstraintViolationException) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 22 with Size

use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForReturnValueTest method canDeclareContainerElementConstraintsForReturnValueProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForReturnValueProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test1").returnValue().containerElementType(0).constraint(new SizeDef().min(3).max(10)).containerElementType(1).constraint(new MinDef().value(1));
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        fishTank.test1();
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 3 and 10"), violationOf(Size.class).withMessage("size must be between 3 and 10"), violationOf(Min.class).withMessage("must be greater than or equal to 1"), violationOf(Min.class).withMessage("must be greater than or equal to 1"));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) Min(jakarta.validation.constraints.Min) Size(jakarta.validation.constraints.Size) ConstraintViolationException(jakarta.validation.ConstraintViolationException) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) MinDef(org.hibernate.validator.cfg.defs.MinDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 23 with Size

use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForReturnValueTest method canDeclareContainerElementConstraintsForArrayTypedReturnValueProgrammatically.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForArrayTypedReturnValueProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test5").returnValue().containerElementType().constraint(new SizeDef().max(5));
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        fishTank.test5();
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) Size(jakarta.validation.constraints.Size) ConstraintViolationException(jakarta.validation.ConstraintViolationException) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 24 with Size

use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.

the class ContainerElementTypeConstraintsForParameterXmlMappingTest method canDeclareContainerElementTypeConstraintsForArrayTypeParameterWithXmlMapping.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1291")
public void canDeclareContainerElementTypeConstraintsForArrayTypeParameterWithXmlMapping() {
    Validator validator = getValidator("parameter-canDeclareContainerElementTypeConstraintsForArrayType-mapping.xml");
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        fishTank.test5(new String[] { "Too Long" });
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
    }
}
Also used : Size(jakarta.validation.constraints.Size) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Validator(jakarta.validation.Validator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 25 with Size

use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.

the class ContainerElementTypeConstraintsForParameterXmlMappingTest method canDeclareContainerElementTypeConstraintsForListContainingArrayTypeParameterWithXmlMapping.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1291")
public void canDeclareContainerElementTypeConstraintsForListContainingArrayTypeParameterWithXmlMapping() {
    Validator validator = getValidator("parameter-canDeclareContainerElementTypeConstraintsForListContainingArrayType-mapping.xml");
    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"));
    }
}
Also used : Size(jakarta.validation.constraints.Size) ArrayList(java.util.ArrayList) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Validator(jakarta.validation.Validator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Aggregations

Size (jakarta.validation.constraints.Size)29 Test (org.testng.annotations.Test)22 ConstraintViolationException (jakarta.validation.ConstraintViolationException)16 Validator (jakarta.validation.Validator)15 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)14 TestForIssue (org.hibernate.validator.testutil.TestForIssue)14 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)11 HibernateValidator (org.hibernate.validator.HibernateValidator)8 ConstraintViolation (jakarta.validation.ConstraintViolation)5 ArrayList (java.util.ArrayList)5 Min (jakarta.validation.constraints.Min)4 Pattern (jakarta.validation.constraints.Pattern)4 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)4 NotNull (jakarta.validation.constraints.NotNull)3 List (java.util.List)3 AssertFalse (jakarta.validation.constraints.AssertFalse)2 AssertTrue (jakarta.validation.constraints.AssertTrue)2 DecimalMax (jakarta.validation.constraints.DecimalMax)2 DecimalMin (jakarta.validation.constraints.DecimalMin)2 Max (jakarta.validation.constraints.Max)2