Search in sources :

Example 31 with Validator

use of jakarta.validation.Validator in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForReturnValueTest method canDeclareContainerElementCascadesForReturnValueProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementCascadesForReturnValueProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test4").returnValue().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();
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) 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) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 32 with Validator

use of jakarta.validation.Validator in project hibernate-validator by hibernate.

the class CompositeConstraintTest method testCorrectAnnotationTypeForWithReportAsSingleViolation.

@Test
@TestForIssue(jiraKey = "HV-182")
public void testCorrectAnnotationTypeForWithReportAsSingleViolation() {
    Validator currentValidator = ValidatorUtil.getValidator();
    for (int i = 0; i < 100; i++) {
        Set<ConstraintViolation<Person>> constraintViolations = currentValidator.validate(new Person(null, "Gerhard"));
        assertThat(constraintViolations).containsOnlyViolations(violationOf(ValidNameSingleViolation.class).withMessage("invalid name"));
        constraintViolations = currentValidator.validate(new Person("G", "Gerhard"));
        assertThat(constraintViolations).containsOnlyViolations(violationOf(ValidNameSingleViolation.class).withMessage("invalid name"));
    }
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 33 with Validator

use of jakarta.validation.Validator in project hibernate-validator by hibernate.

the class EagerEvaluationOfComposingConstraintsTest method testComposedConstraintWithoutReportAsSingleViolationEvaluatesAllConstraints.

@Test
@TestForIssue(jiraKey = "HV-639")
public void testComposedConstraintWithoutReportAsSingleViolationEvaluatesAllConstraints() {
    Validator validator = ValidatorUtil.getValidator();
    Foo foo = new Foo();
    validator.validate(foo);
    assertEquals(InvocationCounter.getInvocationCount(foo), 3, "Each constraint should be evaluated since w/o @ReportAsSingleViolation there might be several violations");
}
Also used : Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 34 with Validator

use of jakarta.validation.Validator in project hibernate-validator by hibernate.

the class ValidatorResolutionTest method testResolutionOfMultipleSizeValidators.

@Test
public void testResolutionOfMultipleSizeValidators() {
    Validator validator = ValidatorUtil.getValidator();
    Suburb suburb = new Suburb();
    List<Integer> postcodes = new ArrayList<Integer>();
    postcodes.add(12345);
    suburb.setIncludedPostCodes(postcodes);
    // all values are null and should pass
    Set<ConstraintViolation<Suburb>> constraintViolations = validator.validate(suburb);
    assertNoViolations(constraintViolations);
    suburb.setName("");
    constraintViolations = validator.validate(suburb);
    assertThat(constraintViolations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 5 and 10").withProperty("name").withRootBeanClass(Suburb.class).withInvalidValue(""));
    suburb.setName("Hoegsbo");
    constraintViolations = validator.validate(suburb);
    assertNoViolations(constraintViolations);
    suburb.addFacility(Suburb.Facility.SHOPPING_MALL, false);
    constraintViolations = validator.validate(suburb);
    assertThat(constraintViolations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 2 and 2").withProperty("facilities").withRootBeanClass(Suburb.class).withInvalidValue(suburb.getFacilities()));
    suburb.addFacility(Suburb.Facility.BUS_TERMINAL, true);
    constraintViolations = validator.validate(suburb);
    assertNoViolations(constraintViolations);
    suburb.addStreetName("Sikelsgatan");
    constraintViolations = validator.validate(suburb);
    assertThat(constraintViolations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 2 and 2147483647").withProperty("streetNames").withRootBeanClass(Suburb.class).withInvalidValue(suburb.getStreetNames()));
    suburb.addStreetName("Marklandsgatan");
    constraintViolations = validator.validate(suburb);
    assertNoViolations(constraintViolations);
    Coordinate[] boundingBox = new Coordinate[3];
    boundingBox[0] = new Coordinate(0L, 0L);
    boundingBox[1] = new Coordinate(0L, 1L);
    boundingBox[2] = new Coordinate(1L, 0L);
    suburb.setBoundingBox(boundingBox);
    constraintViolations = validator.validate(suburb);
    assertThat(constraintViolations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 4 and 1000").withProperty("boundingBox").withRootBeanClass(Suburb.class).withInvalidValue(suburb.getBoundingBox()));
    boundingBox = new Coordinate[4];
    boundingBox[0] = new Coordinate(0L, 0L);
    boundingBox[1] = new Coordinate(0L, 1L);
    boundingBox[2] = new Coordinate(1L, 0L);
    boundingBox[3] = new Coordinate(1L, 1L);
    suburb.setBoundingBox(boundingBox);
    constraintViolations = validator.validate(suburb);
    assertNoViolations(constraintViolations);
}
Also used : Size(jakarta.validation.constraints.Size) ConstraintViolation(jakarta.validation.ConstraintViolation) ArrayList(java.util.ArrayList) Validator(jakarta.validation.Validator) Test(org.testng.annotations.Test)

Example 35 with Validator

use of jakarta.validation.Validator in project hibernate-validator by hibernate.

the class ValidatorResolutionTest method testSubTypeArrayIsSubtypeOfSuperTypeArray.

@Test
@TestForIssue(jiraKey = "HV-233")
public void testSubTypeArrayIsSubtypeOfSuperTypeArray() {
    Validator validator = ValidatorUtil.getValidator();
    SubTypeEntity testEntity = new SubTypeEntity(new SubType[] {});
    Set<ConstraintViolation<SubTypeEntity>> constraintViolations = validator.validate(testEntity);
    assertNoViolations(constraintViolations);
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Aggregations

Validator (jakarta.validation.Validator)413 Test (org.testng.annotations.Test)340 ConstraintViolation (jakarta.validation.ConstraintViolation)258 HibernateValidator (org.hibernate.validator.HibernateValidator)215 TestForIssue (org.hibernate.validator.testutil.TestForIssue)181 ConstraintValidator (jakarta.validation.ConstraintValidator)114 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)88 ValidatorFactory (jakarta.validation.ValidatorFactory)79 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)71 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)43 Test (org.junit.Test)38 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)33 ConstraintViolationException (jakarta.validation.ConstraintViolationException)32 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)28 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)27 NotNull (jakarta.validation.constraints.NotNull)26 Path (jakarta.validation.Path)19 Size (jakarta.validation.constraints.Size)18 CountValidationCallsValidator (org.hibernate.validator.testutil.CountValidationCallsValidator)17 List (java.util.List)13