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();
}
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"));
}
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;
}
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));
}
Aggregations