Search in sources :

Example 6 with TestForIssue

use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareDeeplyNestedContainerElementConstraintsOnMultipleDifferentTypeArgumentsForFieldProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1614")
public void canDeclareDeeplyNestedContainerElementConstraintsOnMultipleDifferentTypeArgumentsForFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("tagsOfFishOfTheMonth").containerElementType(0, 0).constraint(new LengthDef().min(10).max(20)).containerElementType(0, 1, 0).constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<FishTank>> violations = validator.validate(new FishTank());
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"), violationOf(Length.class).withMessage("length must be between 10 and 20"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) LengthDef(org.hibernate.validator.cfg.defs.LengthDef) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 7 with TestForIssue

use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeFieldProgrammatically.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("fishNamesByMonthAsArray").containerElementType(0, 0).constraint(new SizeDef().max(5));
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<FishTank>> violations = validator.validate(new FishTank());
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolation(jakarta.validation.ConstraintViolation) 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 8 with TestForIssue

use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForFieldTest method declaringContainerElementConstraintForNonExistingTypeArgumentIndexOnFieldCausesException.

@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000212.*")
@TestForIssue(jiraKey = "HV-1239")
public void declaringContainerElementConstraintForNonExistingTypeArgumentIndexOnFieldCausesException() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("model").containerElementType(2);
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 9 with TestForIssue

use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareNestedContainerElementConstraintsForFieldProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareNestedContainerElementConstraintsForFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("fishOfTheMonth").containerElementType(1, 0).constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<FishTank>> violations = validator.validate(new FishTank());
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 10 with TestForIssue

use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareDeeplyNestedContainerElementConstraintsForFieldProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareDeeplyNestedContainerElementConstraintsForFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("tagsOfFishOfTheMonth").containerElementType(0, 1, 0).constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<FishTank>> violations = validator.validate(new FishTank());
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Aggregations

TestForIssue (org.hibernate.validator.testutil.TestForIssue)286 Test (org.testng.annotations.Test)286 Validator (jakarta.validation.Validator)180 ConstraintViolation (jakarta.validation.ConstraintViolation)134 HibernateValidator (org.hibernate.validator.HibernateValidator)80 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)80 ConstraintValidator (jakarta.validation.ConstraintValidator)64 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)55 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)33 ValidatorFactory (jakarta.validation.ValidatorFactory)29 ConstraintViolationException (jakarta.validation.ConstraintViolationException)26 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)22 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)21 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)21 NotNull (jakarta.validation.constraints.NotNull)17 Method (java.lang.reflect.Method)17 Size (jakarta.validation.constraints.Size)15 CountValidationCallsValidator (org.hibernate.validator.testutil.CountValidationCallsValidator)13 File (java.io.File)12 HibernateConstraintViolation (org.hibernate.validator.engine.HibernateConstraintViolation)12