Skip to content

Commit

Permalink
HV-1821 Support using a non-Hibernate Validator configuration class
Browse files Browse the repository at this point in the history
It was supported before and got broken. It stayed unnoticed because we
didn't have a test for that so adding a proper test too.
  • Loading branch information
gsmet committed Dec 16, 2020
1 parent 9c61b95 commit 3304647
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ static Object determineConstraintValidatorPayload(ConfigurationState configurati

static ExpressionLanguageFeatureLevel determineConstraintExpressionLanguageFeatureLevel(AbstractConfigurationImpl<?> hibernateSpecificConfig,
Map<String, String> properties) {
if ( hibernateSpecificConfig.getConstraintExpressionLanguageFeatureLevel() != null ) {
if ( hibernateSpecificConfig != null && hibernateSpecificConfig.getConstraintExpressionLanguageFeatureLevel() != null ) {
LOG.logConstraintExpressionLanguageFeatureLevel( hibernateSpecificConfig.getConstraintExpressionLanguageFeatureLevel() );
return ExpressionLanguageFeatureLevel.interpretDefaultForConstraints( hibernateSpecificConfig.getConstraintExpressionLanguageFeatureLevel() );
}
Expand All @@ -284,7 +284,7 @@ static ExpressionLanguageFeatureLevel determineConstraintExpressionLanguageFeatu

static ExpressionLanguageFeatureLevel determineCustomViolationExpressionLanguageFeatureLevel(AbstractConfigurationImpl<?> hibernateSpecificConfig,
Map<String, String> properties) {
if ( hibernateSpecificConfig.getCustomViolationExpressionLanguageFeatureLevel() != null ) {
if ( hibernateSpecificConfig != null && hibernateSpecificConfig.getCustomViolationExpressionLanguageFeatureLevel() != null ) {
LOG.logCustomViolationExpressionLanguageFeatureLevel( hibernateSpecificConfig.getCustomViolationExpressionLanguageFeatureLevel() );
return ExpressionLanguageFeatureLevel.interpretDefaultForCustomViolations( hibernateSpecificConfig.getCustomViolationExpressionLanguageFeatureLevel() );
}
Expand All @@ -306,7 +306,7 @@ static ExpressionLanguageFeatureLevel determineCustomViolationExpressionLanguage

static GetterPropertySelectionStrategy determineGetterPropertySelectionStrategy(AbstractConfigurationImpl<?> hibernateSpecificConfig, Map<String, String> properties,
ClassLoader externalClassLoader) {
if ( hibernateSpecificConfig.getGetterPropertySelectionStrategy() != null ) {
if ( hibernateSpecificConfig != null && hibernateSpecificConfig.getGetterPropertySelectionStrategy() != null ) {
LOG.usingGetterPropertySelectionStrategy( hibernateSpecificConfig.getGetterPropertySelectionStrategy().getClass() );
return hibernateSpecificConfig.getGetterPropertySelectionStrategy();
}
Expand All @@ -332,7 +332,7 @@ static GetterPropertySelectionStrategy determineGetterPropertySelectionStrategy(
}

static BeanMetaDataClassNormalizer determineBeanMetaDataClassNormalizer(AbstractConfigurationImpl<?> hibernateSpecificConfig) {
if ( hibernateSpecificConfig.getBeanMetaDataClassNormalizer() != null ) {
if ( hibernateSpecificConfig != null && hibernateSpecificConfig.getBeanMetaDataClassNormalizer() != null ) {
return hibernateSpecificConfig.getBeanMetaDataClassNormalizer();
}

Expand All @@ -341,7 +341,7 @@ static BeanMetaDataClassNormalizer determineBeanMetaDataClassNormalizer(Abstract

static PropertyNodeNameProvider determinePropertyNodeNameProvider(AbstractConfigurationImpl<?> hibernateSpecificConfig, Map<String, String> properties,
ClassLoader externalClassLoader) {
if ( hibernateSpecificConfig.getPropertyNodeNameProvider() != null ) {
if ( hibernateSpecificConfig != null && hibernateSpecificConfig.getPropertyNodeNameProvider() != null ) {
LOG.usingPropertyNodeNameProvider( hibernateSpecificConfig.getPropertyNodeNameProvider().getClass() );

return hibernateSpecificConfig.getPropertyNodeNameProvider();
Expand All @@ -367,7 +367,7 @@ static PropertyNodeNameProvider determinePropertyNodeNameProvider(AbstractConfig

static LocaleResolver determineLocaleResolver(AbstractConfigurationImpl<?> hibernateSpecificConfig, Map<String, String> properties,
ClassLoader externalClassLoader) {
if ( hibernateSpecificConfig.getLocaleResolver() != null ) {
if ( hibernateSpecificConfig != null && hibernateSpecificConfig.getLocaleResolver() != null ) {
LOG.usingLocaleResolver( hibernateSpecificConfig.getLocaleResolver().getClass() );

return hibernateSpecificConfig.getLocaleResolver();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.cfg;

import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertThat;
import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf;

import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import javax.validation.BootstrapConfiguration;
import javax.validation.ClockProvider;
import javax.validation.Configuration;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.MessageInterpolator;
import javax.validation.ParameterNameProvider;
import javax.validation.TraversableResolver;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.NotNull;
import javax.validation.spi.BootstrapState;
import javax.validation.spi.ConfigurationState;
import javax.validation.spi.ValidationProvider;
import javax.validation.valueextraction.ValueExtractor;

import org.hibernate.validator.internal.engine.DefaultClockProvider;
import org.hibernate.validator.internal.engine.DefaultParameterNameProvider;
import org.hibernate.validator.internal.engine.ValidatorFactoryImpl;
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorFactoryImpl;
import org.hibernate.validator.internal.engine.resolver.TraversableResolvers;
import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator;
import org.hibernate.validator.testutil.TestForIssue;
import org.testng.annotations.Test;

/**
* @author Guillaume Smet
*/
public class NonHibernateValidatorConfigTest {

@Test
@TestForIssue(jiraKey = "HV-1821")
public void testNonHibernateValidatorConfig() {
ValidatorFactory validatorFactory = Validation.byProvider( NonHibernateValidatorProvider.class )
.configure()
.buildValidatorFactory();
Validator validator = validatorFactory.getValidator();
assertThat( validator.validate( new Bean() ) ).containsOnlyViolations( violationOf( NotNull.class ) );
}

public static final class NonHibernateValidatorProvider implements ValidationProvider<NonHibernateValidatorConfiguration> {

@Override
public NonHibernateValidatorConfiguration createSpecializedConfiguration(BootstrapState state) {
return new NonHibernateValidatorConfiguration();
}

@Override
public Configuration<?> createGenericConfiguration(BootstrapState state) {
return new NonHibernateValidatorConfiguration();
}

@Override
public ValidatorFactory buildValidatorFactory(ConfigurationState configurationState) {
return new ValidatorFactoryImpl( configurationState );
}
}

public static final class NonHibernateValidatorConfiguration implements Configuration<NonHibernateValidatorConfiguration>, ConfigurationState {

private final MessageInterpolator defaultMessageInterpolator;
private final TraversableResolver defaultTraversableResolver;
private final ConstraintValidatorFactory defaultConstraintValidatorFactory;
private final ParameterNameProvider defaultParameterNameProvider;
private final ClockProvider defaultClockProvider;

public NonHibernateValidatorConfiguration() {
this.defaultMessageInterpolator = new ParameterMessageInterpolator();
this.defaultTraversableResolver = TraversableResolvers.getDefault();
this.defaultConstraintValidatorFactory = new ConstraintValidatorFactoryImpl();
this.defaultParameterNameProvider = new DefaultParameterNameProvider();
this.defaultClockProvider = DefaultClockProvider.INSTANCE;
}

@Override
public NonHibernateValidatorConfiguration ignoreXmlConfiguration() {
return this;
}

@Override
public NonHibernateValidatorConfiguration messageInterpolator(MessageInterpolator interpolator) {
return this;
}

@Override
public NonHibernateValidatorConfiguration traversableResolver(TraversableResolver resolver) {
return this;
}

@Override
public NonHibernateValidatorConfiguration constraintValidatorFactory(ConstraintValidatorFactory constraintValidatorFactory) {
return this;
}

@Override
public NonHibernateValidatorConfiguration parameterNameProvider(ParameterNameProvider parameterNameProvider) {
return this;
}

@Override
public NonHibernateValidatorConfiguration clockProvider(ClockProvider clockProvider) {
return this;
}

@Override
public NonHibernateValidatorConfiguration addValueExtractor(ValueExtractor<?> extractor) {
return this;
}

@Override
public NonHibernateValidatorConfiguration addMapping(InputStream stream) {
return this;
}

@Override
public NonHibernateValidatorConfiguration addProperty(String name, String value) {
return this;
}

@Override
public MessageInterpolator getDefaultMessageInterpolator() {
return defaultMessageInterpolator;
}

@Override
public TraversableResolver getDefaultTraversableResolver() {
return defaultTraversableResolver;
}

@Override
public ConstraintValidatorFactory getDefaultConstraintValidatorFactory() {
return defaultConstraintValidatorFactory;
}

@Override
public ParameterNameProvider getDefaultParameterNameProvider() {
return defaultParameterNameProvider;
}

@Override
public ClockProvider getDefaultClockProvider() {
return defaultClockProvider;
}

@Override
public BootstrapConfiguration getBootstrapConfiguration() {
return null;
}

@Override
public ValidatorFactory buildValidatorFactory() {
return new NonHibernateValidatorProvider().buildValidatorFactory( this );
}

@Override
public boolean isIgnoreXmlConfiguration() {
return true;
}

@Override
public MessageInterpolator getMessageInterpolator() {
return defaultMessageInterpolator;
}

@Override
public Set<InputStream> getMappingStreams() {
return Collections.emptySet();
}

@Override
public Set<ValueExtractor<?>> getValueExtractors() {
return Collections.emptySet();
}

@Override
public ConstraintValidatorFactory getConstraintValidatorFactory() {
return defaultConstraintValidatorFactory;
}

@Override
public TraversableResolver getTraversableResolver() {
return defaultTraversableResolver;
}

@Override
public ParameterNameProvider getParameterNameProvider() {
return defaultParameterNameProvider;
}

@Override
public ClockProvider getClockProvider() {
return defaultClockProvider;
}

@Override
public Map<String, String> getProperties() {
return Collections.emptyMap();
}
}

public static final class Bean {

@NotNull
public String property;
}
}

0 comments on commit 3304647

Please sign in to comment.