com.oneandone.iocunit.validate.ValidationInitializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ioc-unit-validate Show documentation
Show all versions of ioc-unit-validate Show documentation
Allow the definition of beans as to be validated in the testcontainer
package com.oneandone.iocunit.validate;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Set;
import javax.enterprise.inject.Produces;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.executable.ExecutableValidator;
import javax.validation.metadata.BeanDescriptor;
/**
* @author aschoerk
*/
public class ValidationInitializer {
public static class CombinedValidators implements Validator, ExecutableValidator {
Validator validator;
ExecutableValidator executableValidator;
public CombinedValidators() {
}
@Override
public Set> validateParameters(final T t, final Method method, final Object[] objects, final Class>... classes) {
return executableValidator.validateParameters(t, method, objects, classes);
}
@Override
public Set> validateReturnValue(final T t, final Method method, final Object o, final Class>... classes) {
return executableValidator.validateReturnValue(t, method, o, classes);
}
@Override
public Set> validateConstructorReturnValue(final Constructor extends T> constructor, final T t, final Class>... classes) {
return executableValidator.validateConstructorReturnValue(constructor, t, classes);
}
public CombinedValidators(final Validator validator, final ExecutableValidator executableValidator) {
this.validator = validator;
this.executableValidator = executableValidator;
}
@Override
public Set> validateConstructorParameters(final Constructor extends T> constructor, final Object[] objects, final Class>... classes) {
return executableValidator.validateConstructorParameters(constructor, objects, classes);
}
@Override
public ExecutableValidator forExecutables() {
return validator.forExecutables();
}
@Override
public Set> validate(final T t, final Class>... classes) {
return validator.validate(t, classes);
}
@Override
public Set> validateProperty(final T t, final String s, final Class>... classes) {
return validator.validateProperty(t, s, classes);
}
@Override
public Set> validateValue(final Class aClass, final String s, final Object o, final Class>... classes) {
return validator.validateValue(aClass, s, o, classes);
}
@Override
public BeanDescriptor getConstraintsForClass(final Class> aClass) {
return validator.getConstraintsForClass(aClass);
}
@Override
public T unwrap(final Class aClass) {
return validator.unwrap(aClass);
}
}
@Produces
@javax.enterprise.context.ApplicationScoped
CombinedValidators getInstanceValidator() {
javax.validation.ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
final Validator validator = factory.getValidator();
return new CombinedValidators(validator, validator.forExecutables());
}
}