Skip to content

Commit

Permalink
HV-819 Making validated value unwrappers configurable via property
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Jan 10, 2014
1 parent 8529306 commit 1064790
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
Expand Up @@ -38,6 +38,12 @@ public interface HibernateValidatorConfiguration extends Configuration<Hibernate
*/
String FAIL_FAST = "hibernate.validator.fail_fast";

/**
* Property corresponding to the {@link #addValidatedValueHandler(ValidatedValueUnwrapper)} method. Accepts a String
* with the comma-separated fully-qualified names of one or more {@link ValidatedValueUnwrapper} implementations.
*/
String VALIDATED_VALUE_HANDLERS = "hibernate.validator.validated_value_handlers";

/**
* <p>
* Returns the {@link ResourceBundleLocator} used by the
Expand Down
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintValidatorFactory;
import javax.validation.MessageInterpolator;
import javax.validation.ParameterNameProvider;
Expand All @@ -40,6 +39,7 @@
import org.hibernate.validator.internal.metadata.provider.ProgrammaticMetaDataProvider;
import org.hibernate.validator.internal.metadata.provider.XmlMetaDataProvider;
import org.hibernate.validator.internal.util.ExecutableHelper;
import org.hibernate.validator.internal.util.ReflectionHelper;
import org.hibernate.validator.internal.util.TypeResolutionHelper;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
Expand All @@ -49,7 +49,7 @@
import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;

/**
* Factory returning initialized {@code Validator} instances. This is Hibernate Validator default
* Factory returning initialized {@code Validator} instances. This is the Hibernate Validator default
* implementation of the {@code ValidatorFactory} interface.
*
* @author Emmanuel Bernard
Expand Down Expand Up @@ -124,7 +124,7 @@ public class ValidatorFactoryImpl implements HibernateValidatorFactory {
/**
* Contains handlers to be applied to the validated value when validating elements.
*/
private List<ValidatedValueUnwrapper<?>> validatedValueHandlers;
private final List<ValidatedValueUnwrapper<?>> validatedValueHandlers;

public ValidatorFactoryImpl(ConfigurationState configurationState) {
this.messageInterpolator = configurationState.getMessageInterpolator();
Expand All @@ -149,6 +149,8 @@ public ValidatorFactoryImpl(ConfigurationState configurationState) {
Map<String, String> properties = configurationState.getProperties();

boolean tmpFailFast = false;
List<ValidatedValueUnwrapper<?>> tmpValidatedValueHandlers = newArrayList( 5 );

if ( configurationState instanceof ConfigurationImpl ) {
ConfigurationImpl hibernateSpecificConfig = (ConfigurationImpl) configurationState;

Expand All @@ -158,13 +160,17 @@ public ValidatorFactoryImpl(ConfigurationState configurationState) {
// check whether fail fast is programmatically enabled
tmpFailFast = hibernateSpecificConfig.getFailFast();

this.validatedValueHandlers = hibernateSpecificConfig.getValidatedValueHandlers();
tmpValidatedValueHandlers.addAll( hibernateSpecificConfig.getValidatedValueHandlers() );

}
tmpFailFast = checkPropertiesForFailFast(
properties, tmpFailFast
);
this.failFast = tmpFailFast;

tmpValidatedValueHandlers.addAll( getPropertyConfiguredValidatedValueHandlers( properties ) );
this.validatedValueHandlers = Collections.unmodifiableList( tmpValidatedValueHandlers );

this.constraintValidatorManager = new ConstraintValidatorManager( configurationState.getConstraintValidatorFactory() );
}

Expand Down Expand Up @@ -293,4 +299,34 @@ private boolean checkPropertiesForFailFast(Map<String, String> properties, boole
}
return failFast;
}

/**
* Returns a list with {@link ValidatedValueUnwrapper}s configured via the
* {@link HibernateValidatorConfiguration#VALIDATED_VALUE_HANDLERS} property.
*
* @param properties the properties used to bootstrap the factory
*
* @return a list with property-configured {@link ValidatedValueUnwrapper}s; May be empty but never {@code null}
*/
private List<ValidatedValueUnwrapper<?>> getPropertyConfiguredValidatedValueHandlers(
Map<String, String> properties) {
String propertyValue = properties.get( HibernateValidatorConfiguration.VALIDATED_VALUE_HANDLERS );

if ( propertyValue == null || propertyValue.isEmpty() ) {
return Collections.emptyList();
}

String[] handlerNames = propertyValue.split( "," );
List<ValidatedValueUnwrapper<?>> handlers = newArrayList( handlerNames.length );

for ( String handlerName : handlerNames ) {
@SuppressWarnings("unchecked")
Class<? extends ValidatedValueUnwrapper<?>> handlerType = (Class<? extends ValidatedValueUnwrapper<?>>) ReflectionHelper
.loadClass( handlerName, ValidatorFactoryImpl.class );
handlers.add( ReflectionHelper.newInstance( handlerType, "validated value handler class" ) );
}

return handlers;
}

}
Expand Up @@ -164,4 +164,18 @@ public void shouldUnwrapReturnValueBasedOnProgrammaticConfiguration() throws Exc

assertEquals( violations.size(), 1 );
}

@BeforeMethod
public void shouldUnwrapPropertyValuesUsingUnwrapperGivenViaProperty() {
Validator validator = ValidatorUtil.getConfiguration()
.addProperty(
HibernateValidatorConfiguration.VALIDATED_VALUE_HANDLERS,
PropertyValueUnwrapper.class.getName() + "," + UiInputValueUnwrapper.class.getName()
)
.buildValidatorFactory()
.getValidator();

Set<ConstraintViolation<Customer>> violations = validator.validate( new Customer() );
assertEquals( violations.size(), 3 );
}
}

0 comments on commit 1064790

Please sign in to comment.