Search in sources :

Example 1 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project spring-boot by spring-projects.

the class MessageInterpolatorFactoryTests method getObjectShouldReturnMessageSourceMessageInterpolatorDelegateWithResourceBundleMessageInterpolator.

@Test
void getObjectShouldReturnMessageSourceMessageInterpolatorDelegateWithResourceBundleMessageInterpolator() {
    MessageSource messageSource = mock(MessageSource.class);
    MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(messageSource);
    MessageInterpolator interpolator = interpolatorFactory.getObject();
    assertThat(interpolator).isInstanceOf(MessageSourceMessageInterpolator.class);
    assertThat(interpolator).hasFieldOrPropertyWithValue("messageSource", messageSource);
    assertThat(ReflectionTestUtils.getField(interpolator, "messageInterpolator")).isInstanceOf(ResourceBundleMessageInterpolator.class);
}
Also used : MessageSource(org.springframework.context.MessageSource) MessageInterpolator(jakarta.validation.MessageInterpolator) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) Test(org.junit.jupiter.api.Test)

Example 2 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project spring-boot by spring-projects.

the class MessageInterpolatorFactoryWithoutElIntegrationTests method getObjectShouldUseMessageSourceMessageInterpolatorDelegateWithFallback.

@Test
void getObjectShouldUseMessageSourceMessageInterpolatorDelegateWithFallback() {
    MessageSource messageSource = mock(MessageSource.class);
    MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(messageSource);
    MessageInterpolator interpolator = interpolatorFactory.getObject();
    assertThat(interpolator).isInstanceOf(MessageSourceMessageInterpolator.class);
    assertThat(interpolator).hasFieldOrPropertyWithValue("messageSource", messageSource);
    assertThat(ReflectionTestUtils.getField(interpolator, "messageInterpolator")).isInstanceOf(ParameterMessageInterpolator.class);
}
Also used : MessageSource(org.springframework.context.MessageSource) MessageInterpolator(jakarta.validation.MessageInterpolator) ParameterMessageInterpolator(org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator) Test(org.junit.jupiter.api.Test)

Example 3 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project spring-boot by spring-projects.

the class MessageInterpolatorFactoryWithoutElIntegrationTests method getObjectShouldUseFallback.

@Test
void getObjectShouldUseFallback() {
    MessageInterpolator interpolator = new MessageInterpolatorFactory().getObject();
    assertThat(interpolator).isInstanceOf(ParameterMessageInterpolator.class);
}
Also used : MessageInterpolator(jakarta.validation.MessageInterpolator) ParameterMessageInterpolator(org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator) Test(org.junit.jupiter.api.Test)

Example 4 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project spring-framework by spring-projects.

the class LocalValidatorFactoryBean method afterPropertiesSet.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void afterPropertiesSet() {
    Configuration<?> configuration;
    if (this.providerClass != null) {
        ProviderSpecificBootstrap bootstrap = Validation.byProvider(this.providerClass);
        if (this.validationProviderResolver != null) {
            bootstrap = bootstrap.providerResolver(this.validationProviderResolver);
        }
        configuration = bootstrap.configure();
    } else {
        GenericBootstrap bootstrap = Validation.byDefaultProvider();
        if (this.validationProviderResolver != null) {
            bootstrap = bootstrap.providerResolver(this.validationProviderResolver);
        }
        configuration = bootstrap.configure();
    }
    // Try Hibernate Validator 5.2's externalClassLoader(ClassLoader) method
    if (this.applicationContext != null) {
        try {
            Method eclMethod = configuration.getClass().getMethod("externalClassLoader", ClassLoader.class);
            ReflectionUtils.invokeMethod(eclMethod, configuration, this.applicationContext.getClassLoader());
        } catch (NoSuchMethodException ex) {
        // Ignore - no Hibernate Validator 5.2+ or similar provider
        }
    }
    MessageInterpolator targetInterpolator = this.messageInterpolator;
    if (targetInterpolator == null) {
        targetInterpolator = configuration.getDefaultMessageInterpolator();
    }
    configuration.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator));
    if (this.traversableResolver != null) {
        configuration.traversableResolver(this.traversableResolver);
    }
    ConstraintValidatorFactory targetConstraintValidatorFactory = this.constraintValidatorFactory;
    if (targetConstraintValidatorFactory == null && this.applicationContext != null) {
        targetConstraintValidatorFactory = new SpringConstraintValidatorFactory(this.applicationContext.getAutowireCapableBeanFactory());
    }
    if (targetConstraintValidatorFactory != null) {
        configuration.constraintValidatorFactory(targetConstraintValidatorFactory);
    }
    if (this.parameterNameDiscoverer != null) {
        configureParameterNameProvider(this.parameterNameDiscoverer, configuration);
    }
    List<InputStream> mappingStreams = null;
    if (this.mappingLocations != null) {
        mappingStreams = new ArrayList<>(this.mappingLocations.length);
        for (Resource location : this.mappingLocations) {
            try {
                InputStream stream = location.getInputStream();
                mappingStreams.add(stream);
                configuration.addMapping(stream);
            } catch (IOException ex) {
                closeMappingStreams(mappingStreams);
                throw new IllegalStateException("Cannot read mapping resource: " + location);
            }
        }
    }
    this.validationPropertyMap.forEach(configuration::addProperty);
    // Allow for custom post-processing before we actually build the ValidatorFactory.
    postProcessConfiguration(configuration);
    try {
        this.validatorFactory = configuration.buildValidatorFactory();
        setTargetValidator(this.validatorFactory.getValidator());
    } finally {
        closeMappingStreams(mappingStreams);
    }
}
Also used : ConstraintValidatorFactory(jakarta.validation.ConstraintValidatorFactory) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) Method(java.lang.reflect.Method) IOException(java.io.IOException) ProviderSpecificBootstrap(jakarta.validation.bootstrap.ProviderSpecificBootstrap) GenericBootstrap(jakarta.validation.bootstrap.GenericBootstrap) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator)

Example 5 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project hibernate-orm by hibernate.

the class BeanValidationProvidedFactoryTest method configure.

@Override
protected void configure(Configuration cfg) {
    super.configure(cfg);
    final MessageInterpolator messageInterpolator = new MessageInterpolator() {

        public String interpolate(String s, Context context) {
            return "Oops";
        }

        public String interpolate(String s, Context context, Locale locale) {
            return interpolate(s, context);
        }
    };
    final jakarta.validation.Configuration<?> configuration = Validation.byDefaultProvider().configure();
    configuration.messageInterpolator(messageInterpolator);
    ValidatorFactory vf = configuration.buildValidatorFactory();
    cfg.getProperties().put("jakarta.persistence.validation.factory", vf);
    cfg.setProperty("javax.persistence.validation.mode", "AUTO");
}
Also used : Locale(java.util.Locale) ValidatorFactory(jakarta.validation.ValidatorFactory) MessageInterpolator(jakarta.validation.MessageInterpolator)

Aggregations

MessageInterpolator (jakarta.validation.MessageInterpolator)33 ValidatorFactory (jakarta.validation.ValidatorFactory)14 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)13 Test (org.testng.annotations.Test)11 Test (org.junit.jupiter.api.Test)8 Locale (java.util.Locale)7 Validator (jakarta.validation.Validator)5 ValidatorContext (jakarta.validation.ValidatorContext)5 ConstraintViolation (jakarta.validation.ConstraintViolation)4 ConstraintValidatorFactory (jakarta.validation.ConstraintValidatorFactory)3 GenericBootstrap (jakarta.validation.bootstrap.GenericBootstrap)3 ProviderSpecificBootstrap (jakarta.validation.bootstrap.ProviderSpecificBootstrap)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Method (java.lang.reflect.Method)3 MessageSource (cn.taketoday.context.MessageSource)2 Resource (cn.taketoday.core.io.Resource)2 HibernateValidator (org.hibernate.validator.HibernateValidator)2 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)2 PredefinedScopeHibernateValidator (org.hibernate.validator.PredefinedScopeHibernateValidator)2