Search in sources :

Example 21 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project glassfish-hk2 by eclipse-ee4j.

the class ValidationTest method checkMultiException.

private static void checkMultiException(MultiException me, String expectedConstraintMessage) {
    ConstraintViolationException found = null;
    for (Throwable th : me.getErrors()) {
        if (th instanceof ConstraintViolationException) {
            found = (ConstraintViolationException) th;
            break;
        }
    }
    Assert.assertNotNull(found);
    boolean foundMessage = false;
    Set<ConstraintViolation<?>> violations = found.getConstraintViolations();
    for (ConstraintViolation<?> violation : violations) {
        if (expectedConstraintMessage.equals(violation.getMessage())) {
            foundMessage = true;
            break;
        }
    }
    Assert.assertTrue("Did not find expected exception in " + me + " was looking for " + expectedConstraintMessage, foundMessage);
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException)

Example 22 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project glassfish-hk2 by eclipse-ee4j.

the class XmlRootHandleImpl method startValidating.

@Override
public void startValidating() {
    if (changeControl == null)
        throw new IllegalStateException();
    Validator validator = changeControl.findOrCreateValidator();
    if (root == null)
        return;
    Set<ConstraintViolation<Object>> violations = validator.<Object>validate(root);
    if (violations == null || violations.isEmpty())
        return;
    throw new ConstraintViolationException(violations);
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Validator(jakarta.validation.Validator)

Example 23 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project glassfish-hk2 by eclipse-ee4j.

the class DynamicChangeInfo method endOrDeferChange.

/**
 * Write lock MUST be held!
 *
 * @return
 */
public void endOrDeferChange(boolean success) throws MultiException {
    changeDepth--;
    if (changeDepth < 0)
        changeDepth = 0;
    if (!success)
        globalSuccess = false;
    if (changeDepth > 0)
        return;
    List<BaseHK2JAXBBean> localParticipants = new ArrayList<BaseHK2JAXBBean>(participants);
    participants.clear();
    XmlDynamicChange localDynamicChange = dynamicChange;
    dynamicChange = null;
    if (localDynamicChange == null)
        return;
    ConstraintViolationException validationException = null;
    if (globalSuccess && (validator != null) && (root != null) && (root.getRoot() != null)) {
        // Validate root if validation is on
        Set<ConstraintViolation<Object>> violations = validator.<Object>validate(root.getRoot());
        if (violations != null && !violations.isEmpty()) {
            validationException = new ConstraintViolationException(violations);
        }
    }
    if (!globalSuccess || (validationException != null)) {
        for (BaseHK2JAXBBean participant : localParticipants) {
            participant.__rollbackChange();
        }
        if (validationException != null) {
            throw new MultiException(validationException);
        }
        return;
    }
    for (BaseHK2JAXBBean participant : localParticipants) {
        participant.__activateChange();
    }
    DynamicConfiguration systemChange = localDynamicChange.getSystemDynamicConfiguration();
    WriteableBeanDatabase wbd = localDynamicChange.getBeanDatabase();
    if (systemChange == null && wbd != null) {
        // Weird case, can it happen?
        wbd.commit(new XmlHubCommitMessage() {
        });
        return;
    }
    if (systemChange == null)
        return;
    // Can definitely throw, for example if a listener fails
    systemChange.commit();
}
Also used : XmlHubCommitMessage(org.glassfish.hk2.xml.api.XmlHubCommitMessage) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) BaseHK2JAXBBean(org.glassfish.hk2.xml.jaxb.internal.BaseHK2JAXBBean) ConstraintViolation(jakarta.validation.ConstraintViolation) ArrayList(java.util.ArrayList) ConstraintViolationException(jakarta.validation.ConstraintViolationException) MultiException(org.glassfish.hk2.api.MultiException) WriteableBeanDatabase(org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase)

Example 24 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project today-framework by TAKETODAY.

the class ValidatorFactoryTests method testSimpleValidationWithCustomProvider.

@Test
public void testSimpleValidationWithCustomProvider() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.setProviderClass(HibernateValidator.class);
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
    assertThat(result.size()).isEqualTo(2);
    for (ConstraintViolation<ValidPerson> cv : result) {
        String path = cv.getPropertyPath().toString();
        assertThat(path).matches(actual -> "name".equals(actual) || "address.street".equals(actual));
        assertThat(cv.getConstraintDescriptor().getAnnotation()).isInstanceOf(NotNull.class);
    }
    Validator nativeValidator = validator.unwrap(Validator.class);
    assertThat(nativeValidator.getClass().getName().startsWith("org.hibernate")).isTrue();
    assertThat(validator.unwrap(ValidatorFactory.class)).isInstanceOf(HibernateValidatorFactory.class);
    assertThat(validator.unwrap(HibernateValidatorFactory.class)).isInstanceOf(HibernateValidatorFactory.class);
    validator.destroy();
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.junit.jupiter.api.Test)

Example 25 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project mojarra by eclipse-ee4j.

the class WholeBeanValidator method doBeanValidation.

private Set<ConstraintViolation<?>> doBeanValidation(jakarta.validation.Validator beanValidator, Object wholeBeanCopy, Class<?>[] validationGroupArray, ValueExpression wholeBeanVE) {
    @SuppressWarnings("rawtypes") Set violationsRaw = null;
    try {
        violationsRaw = beanValidator.validate(wholeBeanCopy, validationGroupArray);
    } catch (IllegalArgumentException iae) {
        LOGGER.fine("Unable to validate expression " + wholeBeanVE.getExpressionString() + " using Bean Validation.  Unable to get value of expression. " + " Message from Bean Validation: " + iae.getMessage());
    }
    @SuppressWarnings("unchecked") Set<ConstraintViolation<?>> violations = violationsRaw;
    return violations;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ConstraintViolation(jakarta.validation.ConstraintViolation)

Aggregations

ConstraintViolation (jakarta.validation.ConstraintViolation)397 Test (org.testng.annotations.Test)309 Validator (jakarta.validation.Validator)258 TestForIssue (org.hibernate.validator.testutil.TestForIssue)134 HibernateValidator (org.hibernate.validator.HibernateValidator)123 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)70 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)60 ConstraintValidator (jakarta.validation.ConstraintValidator)57 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)54 Method (java.lang.reflect.Method)46 ValidatorFactory (jakarta.validation.ValidatorFactory)41 Test (org.junit.Test)39 ConstraintViolationException (jakarta.validation.ConstraintViolationException)35 Path (jakarta.validation.Path)35 NotNull (jakarta.validation.constraints.NotNull)32 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)27 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)19 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)18 Size (jakarta.validation.constraints.Size)17 PropertyNode (org.hibernate.validator.path.PropertyNode)14