Search in sources :

Example 71 with ConstraintViolationException

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

the class FailFastTest method testFailFastMethodValidationSetOnValidatorFactory.

@Test
@TestForIssue(jiraKey = "HV-381")
public void testFailFastMethodValidationSetOnValidatorFactory() {
    final HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(HibernateValidator.class);
    final ValidatorFactory factory = configuration.buildValidatorFactory();
    final Validator validator = factory.unwrap(HibernateValidatorFactory.class).usingContext().failFast(true).getValidator();
    TestService service = getValidatingProxy(new TestServiceImpl(), validator);
    try {
        service.testMethod(" ", null);
        fail();
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOneOfViolations(violationOf(NotBlank.class), violationOf(Min.class));
    }
}
Also used : HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) ValidatorFactory(jakarta.validation.ValidatorFactory) HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) ConstraintViolationException(jakarta.validation.ConstraintViolationException) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 72 with ConstraintViolationException

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

the class FailFastTest method testFailFastMethodValidationOnConfiguration.

@Test
@TestForIssue(jiraKey = "HV-381")
public void testFailFastMethodValidationOnConfiguration() {
    final HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(HibernateValidator.class);
    final ValidatorFactory factory = configuration.failFast(true).buildValidatorFactory();
    final Validator validator = factory.getValidator();
    TestService service = getValidatingProxy(new TestServiceImpl(), validator);
    try {
        service.testMethod("a", null);
        fail();
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Min.class));
    }
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) ConstraintViolationException(jakarta.validation.ConstraintViolationException) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 73 with ConstraintViolationException

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

the class FailFastTest method testFailFastMethodValidationSetWithProperty.

@Test
@TestForIssue(jiraKey = "HV-381")
public void testFailFastMethodValidationSetWithProperty() {
    final HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(HibernateValidator.class);
    final ValidatorFactory factory = configuration.addProperty(HibernateValidatorConfiguration.FAIL_FAST, "true").buildValidatorFactory();
    final Validator validator = factory.getValidator();
    TestService service = getValidatingProxy(new TestServiceImpl(), validator);
    try {
        service.testMethod(" ", null);
        fail();
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOneOfViolations(violationOf(NotBlank.class), violationOf(Min.class));
    }
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) ConstraintViolationException(jakarta.validation.ConstraintViolationException) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 74 with ConstraintViolationException

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

the class MethodConstraintMappingTest method testParameterConstraint.

@Test
public void testParameterConstraint() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", User.class).parameter(0).constraint(new NotNullDef());
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.greet((User) null);
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().method("greet").parameter("user", 0)));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Test(org.testng.annotations.Test)

Example 75 with ConstraintViolationException

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

the class MethodConstraintMappingTest method testOverridingMethodMayDefineSameConstraintsAsOverriddenMethod.

@Test
public void testOverridingMethodMayDefineSameConstraintsAsOverriddenMethod() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", String.class).parameter(0).constraint(new SizeDef().min(5).max(10)).type(GreetingServiceImpl.class).method("greet", String.class).parameter(0).constraint(new SizeDef().min(5).max(10));
    config.addMapping(mapping);
    GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
    try {
        service.greet("Hi");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 5 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

ConstraintViolationException (jakarta.validation.ConstraintViolationException)114 Test (org.testng.annotations.Test)55 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)35 ConstraintViolation (jakarta.validation.ConstraintViolation)32 Validator (jakarta.validation.Validator)29 TestForIssue (org.hibernate.validator.testutil.TestForIssue)26 HibernateValidator (org.hibernate.validator.HibernateValidator)19 Size (jakarta.validation.constraints.Size)16 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)15 Test (org.junit.Test)14 Session (org.hibernate.Session)10 Transaction (org.hibernate.Transaction)10 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)10 CustomerRepositoryImpl (org.hibernate.validator.test.internal.engine.methodvalidation.service.CustomerRepositoryImpl)10 Customer (org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer)9 FacesMessage (javax.faces.application.FacesMessage)8 NotNull (jakarta.validation.constraints.NotNull)7 BigDecimal (java.math.BigDecimal)7 Set (java.util.Set)7 EntityManager (jakarta.persistence.EntityManager)6