Search in sources :

Example 16 with NotNullDef

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

the class ConstraintMappingTest method testSingleConstraint.

@Test
public void testSingleConstraint() {
    mapping.type(Marathon.class).getter("name").constraint(new NotNullDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<Marathon>> violations = validator.validate(new Marathon());
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
}
Also used : 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)

Example 17 with NotNullDef

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

the class ConstraintMappingTest method testDefaultGroupSequenceProvider.

@Test
public void testDefaultGroupSequenceProvider() {
    mapping.type(Marathon.class).defaultGroupSequenceProviderClass(MarathonDefaultGroupSequenceProvider.class).getter("name").constraint(new NotNullDef().groups(Foo.class)).getter("runners").constraint(new NotEmptyDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
    marathon.setName("Stockholm Marathon");
    violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotEmpty.class).withMessage("must not be empty"));
}
Also used : NotEmptyDef(org.hibernate.validator.cfg.defs.NotEmptyDef) 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)

Example 18 with NotNullDef

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

the class ConstraintMappingTest method testInheritedConstraint.

@Test
public void testInheritedConstraint() {
    mapping.type(Marathon.class).getter("name").constraint(new NotNullDef()).type(Tournament.class).getter("tournamentDate").constraint(new FutureDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    marathon.setName("New York Marathon");
    Calendar cal = GregorianCalendar.getInstance();
    cal.set(Calendar.YEAR, -1);
    marathon.setTournamentDate(cal.getTime());
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(Future.class).withMessage("must be a future date"));
}
Also used : Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) FutureDef(org.hibernate.validator.cfg.defs.FutureDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 19 with NotNullDef

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

the class ConstraintMappingTest method testProgrammaticDefaultGroupSequenceProviderDefinedOnClassWithGroupSequenceAnnotation.

@Test(expectedExceptions = GroupDefinitionException.class, expectedExceptionsMessageRegExp = "HV[0-9]*: Default group sequence and default group sequence provider cannot be defined at the same time.")
public void testProgrammaticDefaultGroupSequenceProviderDefinedOnClassWithGroupSequenceAnnotation() {
    mapping.type(A.class).defaultGroupSequenceProviderClass(ADefaultGroupSequenceProvider.class).field("a").constraint(new NotNullDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    validator.validate(new A());
}
Also used : NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 20 with NotNullDef

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

the class ConstraintMappingTest method testDefaultGroupSequence.

@Test
public void testDefaultGroupSequence() {
    mapping.type(Marathon.class).defaultGroupSequence(Foo.class, Marathon.class).getter("name").constraint(new NotNullDef().groups(Foo.class)).getter("runners").constraint(new NotEmptyDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
    marathon.setName("Stockholm Marathon");
    violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotEmpty.class).withMessage("must not be empty"));
}
Also used : NotEmptyDef(org.hibernate.validator.cfg.defs.NotEmptyDef) 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)

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