Search in sources :

Example 1 with ExecutableValidator

use of javax.validation.executable.ExecutableValidator in project dropwizard by dropwizard.

the class ConstraintViolationBenchmark method prepare.

@Setup
public void prepare() {
    final Validator validator = Validators.newValidator();
    final ExecutableValidator execValidator = validator.forExecutables();
    final Set<ConstraintViolation<ConstraintViolationBenchmark.Resource>> paramViolations = execValidator.validateParameters(new Resource(), getAccessibleMethod(ConstraintViolationBenchmark.Resource.class, "paramFunc", String.class), // the parameter value
    new Object[] { "" });
    paramViolation = paramViolations.iterator().next();
    final Set<ConstraintViolation<ConstraintViolationBenchmark.Resource>> objViolations = execValidator.validateParameters(new Resource(), getAccessibleMethod(ConstraintViolationBenchmark.Resource.class, "objectFunc", Foo.class), // the parameter value
    new Object[] { new Foo() });
    objViolation = objViolations.iterator().next();
}
Also used : ExecutableValidator(javax.validation.executable.ExecutableValidator) ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) ExecutableValidator(javax.validation.executable.ExecutableValidator) Setup(org.openjdk.jmh.annotations.Setup)

Example 2 with ExecutableValidator

use of javax.validation.executable.ExecutableValidator in project javaee7-samples by javaee-samples.

the class ConstructorParametersConstraintsTest method constructorViolationsWhenNullParameters.

@Test
public void constructorViolationsWhenNullParameters() throws NoSuchMethodException, SecurityException {
    final MyParameter parameter = new MyParameter();
    ExecutableValidator methodValidator = validator.forExecutables();
    Constructor<MyBean2> constructor = MyBean2.class.getConstructor(parameter.getClass());
    Set<ConstraintViolation<MyBean2>> constraints = methodValidator.validateConstructorParameters(constructor, new Object[] { parameter });
    ConstraintViolation<MyBean2> violation = constraints.iterator().next();
    assertThat(constraints.size(), equalTo(1));
    assertThat(violation.getMessageTemplate(), equalTo("{javax.validation.constraints.NotNull.message}"));
    assertThat(violation.getPropertyPath().toString(), equalTo("MyBean2.arg0.value"));
}
Also used : ExecutableValidator(javax.validation.executable.ExecutableValidator) ConstraintViolation(javax.validation.ConstraintViolation) Test(org.junit.Test)

Example 3 with ExecutableValidator

use of javax.validation.executable.ExecutableValidator in project spring-framework by spring-projects.

the class MethodValidationInterceptor method invoke.

@Override
@SuppressWarnings("unchecked")
public Object invoke(MethodInvocation invocation) throws Throwable {
    Class<?>[] groups = determineValidationGroups(invocation);
    // Standard Bean Validation 1.1 API
    ExecutableValidator execVal = this.validator.forExecutables();
    Method methodToValidate = invocation.getMethod();
    Set<ConstraintViolation<Object>> result;
    try {
        result = execVal.validateParameters(invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
    } catch (IllegalArgumentException ex) {
        // Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
        // Let's try to find the bridged method on the implementation class...
        methodToValidate = BridgeMethodResolver.findBridgedMethod(ClassUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()));
        result = execVal.validateParameters(invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
    }
    if (!result.isEmpty()) {
        throw new ConstraintViolationException(result);
    }
    Object returnValue = invocation.proceed();
    result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups);
    if (!result.isEmpty()) {
        throw new ConstraintViolationException(result);
    }
    return returnValue;
}
Also used : ExecutableValidator(javax.validation.executable.ExecutableValidator) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Method(java.lang.reflect.Method)

Example 4 with ExecutableValidator

use of javax.validation.executable.ExecutableValidator in project javaee7-samples by javaee-samples.

the class ConstructorParametersConstraintsTest method constructorViolationsWhenNotNullParameters.

@Test
public void constructorViolationsWhenNotNullParameters() throws NoSuchMethodException, SecurityException {
    final MyParameter parameter = new MyParameter();
    parameter.setValue("foo");
    ExecutableValidator methodValidator = validator.forExecutables();
    Constructor<MyBean2> constructor = MyBean2.class.getConstructor(parameter.getClass());
    Set<ConstraintViolation<MyBean2>> constraints = methodValidator.validateConstructorParameters(constructor, new Object[] { parameter });
    assertThat(constraints.isEmpty(), equalTo(true));
}
Also used : ExecutableValidator(javax.validation.executable.ExecutableValidator) ConstraintViolation(javax.validation.ConstraintViolation) Test(org.junit.Test)

Aggregations

ConstraintViolation (javax.validation.ConstraintViolation)4 ExecutableValidator (javax.validation.executable.ExecutableValidator)4 Test (org.junit.Test)2 Method (java.lang.reflect.Method)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 Validator (javax.validation.Validator)1 Setup (org.openjdk.jmh.annotations.Setup)1