Search in sources :

Example 51 with Validator

use of javax.validation.Validator in project podam by devopsfolks.

the class ValidatedPojoTest method whenDigitsAndDecimalMinOrDecimalMaxAreAttachedToTheSameFieldPodamShouldUseMinimumRequirement.

@Test
@Title("When the @Digits and @DecimalMin and @DecimalMax applied to the same field, Podam should use minimum requirements")
public void whenDigitsAndDecimalMinOrDecimalMaxAreAttachedToTheSameFieldPodamShouldUseMinimumRequirement() throws Exception {
    PodamFactory podamFactory = podamFactorySteps.givenAStandardPodamFactory();
    ValidatedPojoMultipleConstraints pojo = podamInvocationSteps.whenIInvokeTheFactoryForClass(ValidatedPojoMultipleConstraints.class, podamFactory);
    podamValidationSteps.theObjectShouldNotBeNull(pojo);
    Validator validator = podamFactorySteps.givenAJavaxValidator();
    validatorSteps.thePojoShouldNotViolateAnyValidations(validator, pojo);
}
Also used : PodamFactory(uk.co.jemos.podam.api.PodamFactory) ValidatedPojoMultipleConstraints(uk.co.jemos.podam.test.dto.ValidatedPojoMultipleConstraints) Validator(javax.validation.Validator) Test(org.junit.Test) Title(net.thucydides.core.annotations.Title)

Example 52 with Validator

use of javax.validation.Validator in project podam by devopsfolks.

the class ValidatedPojoTest method podamShouldAllowValidationAnnotationsCustomization.

@Test
@Title("Podam should allow validation annotations customization")
public void podamShouldAllowValidationAnnotationsCustomization() throws Exception {
    AttributeStrategy<?> strategy = new PatternStrategy();
    PodamFactory podamFactory = podamFactorySteps.givenAPodamFactoryWithCustomStrategy(Pattern.class, strategy);
    ValidatedPatternPojo pojo = podamInvocationSteps.whenIInvokeTheFactoryForClass(ValidatedPatternPojo.class, podamFactory);
    podamValidationSteps.thePojoMustBeOfTheType(pojo, ValidatedPatternPojo.class);
    podamValidationSteps.thePojoMustBeOfTheType(pojo.getNumber(), String.class);
    podamValidationSteps.thePojoMustBeOfTheType(pojo.getIdentifier(), String.class);
    Validator validator = podamFactorySteps.givenAJavaxValidator();
    validatorSteps.thePojoShouldNotViolateAnyValidations(validator, pojo);
    podamFactorySteps.removeCustomStrategy(podamFactory, Pattern.class);
}
Also used : PodamFactory(uk.co.jemos.podam.api.PodamFactory) ValidatedPatternPojo(uk.co.jemos.podam.test.dto.ValidatedPatternPojo) PatternStrategy(uk.co.jemos.podam.test.strategies.PatternStrategy) Validator(javax.validation.Validator) Test(org.junit.Test) Title(net.thucydides.core.annotations.Title)

Example 53 with Validator

use of javax.validation.Validator in project jo-client-platform by jo-source.

the class BeanPropertyValidatorAdapter method validate.

@SuppressWarnings("unchecked")
@Override
public IValidationResult validate(final Object value) {
    if (propertyValidatable) {
        final Set<ConstraintViolation<Object>> beanValidationResult;
        try {
            final Validator beanValidator = CapCommonToolkit.beanValidator();
            beanValidationResult = beanValidator.validateValue((Class<Object>) beanType, propertyName, value);
        } catch (final Exception e) {
            propertyValidatable = false;
            return ValidationResult.ok();
        }
        for (final ConstraintViolation<Object> violation : beanValidationResult) {
            return ValidationResult.error(violation.getMessage());
        }
    }
    return ValidationResult.ok();
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) IValidator(org.jowidgets.validation.IValidator)

Example 54 with Validator

use of javax.validation.Validator in project Payara by payara.

the class BVIntegrationTestServlet method configureValidation.

private Validator configureValidation(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    PrintWriter out = resp.getWriter();
    GenericBootstrap bootstrap = Validation.byDefaultProvider();
    Configuration config = bootstrap.configure();
    InputStream mappingsXml = new ByteArrayInputStream(validationXml.getBytes());
    config.addMapping(mappingsXml);
    ValidatorFactory factory = config.buildValidatorFactory();
    ValidatorContext validatorContext = factory.usingContext();
    Validator validator = validatorContext.getValidator();
    if (validator == null) {
        factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
        validator = factory.getValidator();
    }
    out.print("<p>");
    out.print("Obtained ValidatorFactory: " + factory + ".");
    out.print("</p>");
    return validator;
}
Also used : Configuration(javax.validation.Configuration) ValidatorFactory(javax.validation.ValidatorFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) ValidatorContext(javax.validation.ValidatorContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GenericBootstrap(javax.validation.bootstrap.GenericBootstrap) Validator(javax.validation.Validator) PrintWriter(java.io.PrintWriter)

Aggregations

Validator (javax.validation.Validator)54 Test (org.junit.Test)30 ConstraintViolation (javax.validation.ConstraintViolation)26 ValidatorFactory (javax.validation.ValidatorFactory)17 InitialContext (javax.naming.InitialContext)8 ArrayList (java.util.ArrayList)5 NamingException (javax.naming.NamingException)5 ConstraintViolationException (javax.validation.ConstraintViolationException)5 HibernateValidator (org.hibernate.validator.HibernateValidator)5 File (java.io.File)4 Date (java.util.Date)4 Title (net.thucydides.core.annotations.Title)4 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 PodamFactory (uk.co.jemos.podam.api.PodamFactory)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HashSet (java.util.HashSet)3 BeanManager (javax.enterprise.inject.spi.BeanManager)3 Context (javax.naming.Context)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 HibernateValidatorFactory (org.hibernate.validator.HibernateValidatorFactory)3