Polish "Use MessageSource to interpolate bean validation messages"
See gh-17530
This commit is contained in:
parent
bbb8367afb
commit
621844abda
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2020 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -57,8 +57,7 @@ public class ValidationAutoConfiguration {
|
||||||
@ConditionalOnMissingBean(Validator.class)
|
@ConditionalOnMissingBean(Validator.class)
|
||||||
public static LocalValidatorFactoryBean defaultValidator(ApplicationContext applicationContext) {
|
public static LocalValidatorFactoryBean defaultValidator(ApplicationContext applicationContext) {
|
||||||
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
|
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
|
||||||
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
|
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(applicationContext);
|
||||||
interpolatorFactory.setMessageSource(applicationContext);
|
|
||||||
factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
|
factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
|
||||||
return factoryBean;
|
return factoryBean;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -25,6 +25,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
import org.springframework.boot.validation.MessageInterpolatorFactory;
|
import org.springframework.boot.validation.MessageInterpolatorFactory;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.context.MessageSource;
|
||||||
import org.springframework.validation.Errors;
|
import org.springframework.validation.Errors;
|
||||||
import org.springframework.validation.SmartValidator;
|
import org.springframework.validation.SmartValidator;
|
||||||
import org.springframework.validation.Validator;
|
import org.springframework.validation.Validator;
|
||||||
|
@ -130,11 +131,10 @@ public class ValidatorAdapter implements SmartValidator, ApplicationContextAware
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Validator create(ApplicationContext applicationContext) {
|
private static Validator create(MessageSource messageSource) {
|
||||||
OptionalValidatorFactoryBean validator = new OptionalValidatorFactoryBean();
|
OptionalValidatorFactoryBean validator = new OptionalValidatorFactoryBean();
|
||||||
try {
|
try {
|
||||||
MessageInterpolatorFactory factory = new MessageInterpolatorFactory();
|
MessageInterpolatorFactory factory = new MessageInterpolatorFactory(messageSource);
|
||||||
factory.setMessageSource(applicationContext);
|
|
||||||
validator.setMessageInterpolator(factory.getObject());
|
validator.setMessageInterpolator(factory.getObject());
|
||||||
}
|
}
|
||||||
catch (ValidationException ex) {
|
catch (ValidationException ex) {
|
||||||
|
|
|
@ -10,3 +10,7 @@ For instance, the following service triggers the validation of the first argumen
|
||||||
----
|
----
|
||||||
include::{docs-java}/features/validation/MyBean.java[]
|
include::{docs-java}/features/validation/MyBean.java[]
|
||||||
----
|
----
|
||||||
|
|
||||||
|
The application's `MessageSource` is used when resolving +`{parameters}`+ in constraint messages.
|
||||||
|
This allows you to use <<features.adoc#features.internationalization,your application's `messages.properties` files>> for Bean Validation messages.
|
||||||
|
Once the parameters have been resolved, message interpolation is completed using Bean Validation's default interpolator.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -65,9 +65,7 @@ final class ConfigurationPropertiesJsr303Validator implements Validator {
|
||||||
|
|
||||||
Delegate(ApplicationContext applicationContext) {
|
Delegate(ApplicationContext applicationContext) {
|
||||||
setApplicationContext(applicationContext);
|
setApplicationContext(applicationContext);
|
||||||
MessageInterpolatorFactory factory = new MessageInterpolatorFactory();
|
setMessageInterpolator(new MessageInterpolatorFactory(applicationContext).getObject());
|
||||||
factory.setMessageSource(applicationContext);
|
|
||||||
setMessageInterpolator(factory.getObject());
|
|
||||||
afterPropertiesSet();
|
afterPropertiesSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -28,7 +28,6 @@ import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.ObjectFactory;
|
import org.springframework.beans.factory.ObjectFactory;
|
||||||
import org.springframework.context.MessageSource;
|
import org.springframework.context.MessageSource;
|
||||||
import org.springframework.context.MessageSourceAware;
|
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,7 +38,7 @@ import org.springframework.util.ClassUtils;
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @since 1.5.0
|
* @since 1.5.0
|
||||||
*/
|
*/
|
||||||
public class MessageInterpolatorFactory implements ObjectFactory<MessageInterpolator>, MessageSourceAware {
|
public class MessageInterpolatorFactory implements ObjectFactory<MessageInterpolator> {
|
||||||
|
|
||||||
private static final Set<String> FALLBACKS;
|
private static final Set<String> FALLBACKS;
|
||||||
|
|
||||||
|
@ -49,25 +48,28 @@ public class MessageInterpolatorFactory implements ObjectFactory<MessageInterpol
|
||||||
FALLBACKS = Collections.unmodifiableSet(fallbacks);
|
FALLBACKS = Collections.unmodifiableSet(fallbacks);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MessageSource messageSource;
|
private final MessageSource messageSource;
|
||||||
|
|
||||||
|
public MessageInterpolatorFactory() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the {@link MessageSource} used to create
|
* Creates a new {@link MessageInterpolatorFactory} that will produce a
|
||||||
* {@link MessageSourceInterpolatorDelegate}.
|
* {@link MessageInterpolator} that uses the given {@code messageSource} to resolve
|
||||||
* @param messageSource the message source that resolves any message parameters before
|
* any message parameters before final interpolation.
|
||||||
* final interpolation
|
* @param messageSource message source to be used by the interpolator
|
||||||
|
* @since 2.6.0
|
||||||
*/
|
*/
|
||||||
@Override
|
public MessageInterpolatorFactory(MessageSource messageSource) {
|
||||||
public void setMessageSource(MessageSource messageSource) {
|
|
||||||
this.messageSource = messageSource;
|
this.messageSource = messageSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MessageInterpolator getObject() throws BeansException {
|
public MessageInterpolator getObject() throws BeansException {
|
||||||
MessageInterpolator messageInterpolator = getMessageInterpolator();
|
MessageInterpolator messageInterpolator = getMessageInterpolator();
|
||||||
MessageSource messageSource = this.messageSource;
|
if (this.messageSource != null) {
|
||||||
if (messageSource != null) {
|
return new MessageSourceMessageInterpolator(this.messageSource, messageInterpolator);
|
||||||
return new MessageSourceInterpolatorDelegate(messageSource, messageInterpolator);
|
|
||||||
}
|
}
|
||||||
return messageInterpolator;
|
return messageInterpolator;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,105 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.validation;
|
|
||||||
|
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Utility class to extract message parameters ({@code {param}}) from the message. These
|
|
||||||
* parameters can be substituted for supplied values.
|
|
||||||
*
|
|
||||||
* @author Dmytro Nosan
|
|
||||||
*/
|
|
||||||
final class MessageParameterPlaceholderHelper {
|
|
||||||
|
|
||||||
private static final char PREFIX = '{';
|
|
||||||
|
|
||||||
private static final char SUFFIX = '}';
|
|
||||||
|
|
||||||
private static final char ESCAPE = '\\';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Replaces all message parameters using the given {@code parameterResolver}.
|
|
||||||
* <p>
|
|
||||||
* If returned value has other message parameters, they will be replaced recursively
|
|
||||||
* until no replacement is performed;
|
|
||||||
* <p>
|
|
||||||
* Resolver can return {@code null} to signal that no further actions need to be done
|
|
||||||
* and replacement should be omitted;
|
|
||||||
* <p>
|
|
||||||
* The message parameter can be escaped by the {@code '\'} symbol;
|
|
||||||
* @param message the value containing the parameters to be replaced
|
|
||||||
* @param parameterResolver the {@code parameterResolver} to use for replacement
|
|
||||||
* @return the replaced message
|
|
||||||
*/
|
|
||||||
String replaceParameters(String message, Function<String, String> parameterResolver) {
|
|
||||||
return replaceParameters(message, parameterResolver, new LinkedHashSet<>(4));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String replaceParameters(String message, Function<String, String> parameterResolver,
|
|
||||||
Set<String> visitedParameters) {
|
|
||||||
StringBuilder buf = new StringBuilder(message);
|
|
||||||
int parentheses = 0;
|
|
||||||
int startIndex = -1;
|
|
||||||
int endIndex = -1;
|
|
||||||
for (int i = 0; i < buf.length(); i++) {
|
|
||||||
if (buf.charAt(i) == ESCAPE) {
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
else if (buf.charAt(i) == PREFIX) {
|
|
||||||
if (startIndex == -1) {
|
|
||||||
startIndex = i;
|
|
||||||
}
|
|
||||||
parentheses++;
|
|
||||||
}
|
|
||||||
else if (buf.charAt(i) == SUFFIX) {
|
|
||||||
if (parentheses > 0) {
|
|
||||||
parentheses--;
|
|
||||||
}
|
|
||||||
endIndex = i;
|
|
||||||
}
|
|
||||||
if (parentheses == 0 && startIndex < endIndex) {
|
|
||||||
String parameter = buf.substring(startIndex + 1, endIndex);
|
|
||||||
if (!visitedParameters.add(parameter)) {
|
|
||||||
throw new IllegalArgumentException("Circular reference '{" + parameter + "}'");
|
|
||||||
}
|
|
||||||
String value = replaceParameter(parameter, parameterResolver, visitedParameters);
|
|
||||||
if (value != null) {
|
|
||||||
buf.replace(startIndex, endIndex + 1, value);
|
|
||||||
i = startIndex + value.length() - 1;
|
|
||||||
}
|
|
||||||
visitedParameters.remove(parameter);
|
|
||||||
startIndex = -1;
|
|
||||||
endIndex = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return buf.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String replaceParameter(String parameter, Function<String, String> parameterResolver,
|
|
||||||
Set<String> visitedParameters) {
|
|
||||||
parameter = replaceParameters(parameter, parameterResolver, visitedParameters);
|
|
||||||
String value = parameterResolver.apply(parameter);
|
|
||||||
if (value != null) {
|
|
||||||
return replaceParameters(value, parameterResolver, visitedParameters);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,57 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.validation;
|
|
||||||
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
import javax.validation.MessageInterpolator;
|
|
||||||
|
|
||||||
import org.springframework.context.MessageSource;
|
|
||||||
import org.springframework.context.i18n.LocaleContextHolder;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolves any message parameters via {@link MessageSource} and then interpolates a
|
|
||||||
* message using the underlying {@link MessageInterpolator}.
|
|
||||||
*
|
|
||||||
* @author Dmytro Nosan
|
|
||||||
*/
|
|
||||||
class MessageSourceInterpolatorDelegate implements MessageInterpolator {
|
|
||||||
|
|
||||||
private static final MessageParameterPlaceholderHelper helper = new MessageParameterPlaceholderHelper();
|
|
||||||
|
|
||||||
private final MessageSource messageSource;
|
|
||||||
|
|
||||||
private final MessageInterpolator messageInterpolator;
|
|
||||||
|
|
||||||
MessageSourceInterpolatorDelegate(MessageSource messageSource, MessageInterpolator messageInterpolator) {
|
|
||||||
this.messageSource = messageSource;
|
|
||||||
this.messageInterpolator = messageInterpolator;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String interpolate(String messageTemplate, Context context) {
|
|
||||||
return interpolate(messageTemplate, context, LocaleContextHolder.getLocale());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String interpolate(String messageTemplate, Context context, Locale locale) {
|
|
||||||
String message = helper.replaceParameters(messageTemplate,
|
|
||||||
(parameter) -> this.messageSource.getMessage(parameter, null, null, locale));
|
|
||||||
return this.messageInterpolator.interpolate(message, context, locale);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,121 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.validation;
|
||||||
|
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.validation.MessageInterpolator;
|
||||||
|
|
||||||
|
import org.springframework.context.MessageSource;
|
||||||
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves any message parameters via {@link MessageSource} and then interpolates a
|
||||||
|
* message using the underlying {@link MessageInterpolator}.
|
||||||
|
*
|
||||||
|
* @author Dmytro Nosan
|
||||||
|
*/
|
||||||
|
class MessageSourceMessageInterpolator implements MessageInterpolator {
|
||||||
|
|
||||||
|
private static final char PREFIX = '{';
|
||||||
|
|
||||||
|
private static final char SUFFIX = '}';
|
||||||
|
|
||||||
|
private static final char ESCAPE = '\\';
|
||||||
|
|
||||||
|
private final MessageSource messageSource;
|
||||||
|
|
||||||
|
private final MessageInterpolator messageInterpolator;
|
||||||
|
|
||||||
|
MessageSourceMessageInterpolator(MessageSource messageSource, MessageInterpolator messageInterpolator) {
|
||||||
|
this.messageSource = messageSource;
|
||||||
|
this.messageInterpolator = messageInterpolator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String interpolate(String messageTemplate, Context context) {
|
||||||
|
return interpolate(messageTemplate, context, LocaleContextHolder.getLocale());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String interpolate(String messageTemplate, Context context, Locale locale) {
|
||||||
|
String message = replaceParameters(messageTemplate, locale);
|
||||||
|
return this.messageInterpolator.interpolate(message, context, locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursively replaces all message parameters.
|
||||||
|
* <p>
|
||||||
|
* The message parameter prefix <code>{</code> and suffix <code>}</code> can
|
||||||
|
* be escaped using {@code \}, e.g. <code>\{escaped\}</code>.
|
||||||
|
* @param message the message containing the parameters to be replaced
|
||||||
|
* @param locale the locale to use when resolving replacements
|
||||||
|
* @return the message with parameters replaced
|
||||||
|
*/
|
||||||
|
private String replaceParameters(String message, Locale locale) {
|
||||||
|
return replaceParameters(message, locale, new LinkedHashSet<>(4));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String replaceParameters(String message, Locale locale, Set<String> visitedParameters) {
|
||||||
|
StringBuilder buf = new StringBuilder(message);
|
||||||
|
int parentheses = 0;
|
||||||
|
int startIndex = -1;
|
||||||
|
int endIndex = -1;
|
||||||
|
for (int i = 0; i < buf.length(); i++) {
|
||||||
|
if (buf.charAt(i) == ESCAPE) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else if (buf.charAt(i) == PREFIX) {
|
||||||
|
if (startIndex == -1) {
|
||||||
|
startIndex = i;
|
||||||
|
}
|
||||||
|
parentheses++;
|
||||||
|
}
|
||||||
|
else if (buf.charAt(i) == SUFFIX) {
|
||||||
|
if (parentheses > 0) {
|
||||||
|
parentheses--;
|
||||||
|
}
|
||||||
|
endIndex = i;
|
||||||
|
}
|
||||||
|
if (parentheses == 0 && startIndex < endIndex) {
|
||||||
|
String parameter = buf.substring(startIndex + 1, endIndex);
|
||||||
|
if (!visitedParameters.add(parameter)) {
|
||||||
|
throw new IllegalArgumentException("Circular reference '{" + String.join(" -> ", visitedParameters)
|
||||||
|
+ " -> " + parameter + "}'");
|
||||||
|
}
|
||||||
|
String value = replaceParameter(parameter, locale, visitedParameters);
|
||||||
|
if (value != null) {
|
||||||
|
buf.replace(startIndex, endIndex + 1, value);
|
||||||
|
i = startIndex + value.length() - 1;
|
||||||
|
}
|
||||||
|
visitedParameters.remove(parameter);
|
||||||
|
startIndex = -1;
|
||||||
|
endIndex = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buf.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String replaceParameter(String parameter, Locale locale, Set<String> visitedParameters) {
|
||||||
|
parameter = replaceParameters(parameter, locale, visitedParameters);
|
||||||
|
String value = this.messageSource.getMessage(parameter, null, null, locale);
|
||||||
|
return (value != null) ? replaceParameters(value, locale, visitedParameters) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -42,11 +42,10 @@ class MessageInterpolatorFactoryTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getObjectShouldReturnMessageSourceMessageInterpolatorDelegateWithResourceBundleMessageInterpolator() {
|
void getObjectShouldReturnMessageSourceMessageInterpolatorDelegateWithResourceBundleMessageInterpolator() {
|
||||||
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
|
|
||||||
MessageSource messageSource = mock(MessageSource.class);
|
MessageSource messageSource = mock(MessageSource.class);
|
||||||
interpolatorFactory.setMessageSource(messageSource);
|
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(messageSource);
|
||||||
MessageInterpolator interpolator = interpolatorFactory.getObject();
|
MessageInterpolator interpolator = interpolatorFactory.getObject();
|
||||||
assertThat(interpolator).isInstanceOf(MessageSourceInterpolatorDelegate.class);
|
assertThat(interpolator).isInstanceOf(MessageSourceMessageInterpolator.class);
|
||||||
assertThat(interpolator).hasFieldOrPropertyWithValue("messageSource", messageSource);
|
assertThat(interpolator).hasFieldOrPropertyWithValue("messageSource", messageSource);
|
||||||
assertThat(ReflectionTestUtils.getField(interpolator, "messageInterpolator"))
|
assertThat(ReflectionTestUtils.getField(interpolator, "messageInterpolator"))
|
||||||
.isInstanceOf(ResourceBundleMessageInterpolator.class);
|
.isInstanceOf(ResourceBundleMessageInterpolator.class);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -55,11 +55,10 @@ class MessageInterpolatorFactoryWithoutElIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getObjectShouldUseMessageSourceMessageInterpolatorDelegateWithFallback() {
|
void getObjectShouldUseMessageSourceMessageInterpolatorDelegateWithFallback() {
|
||||||
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
|
|
||||||
MessageSource messageSource = mock(MessageSource.class);
|
MessageSource messageSource = mock(MessageSource.class);
|
||||||
interpolatorFactory.setMessageSource(messageSource);
|
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(messageSource);
|
||||||
MessageInterpolator interpolator = interpolatorFactory.getObject();
|
MessageInterpolator interpolator = interpolatorFactory.getObject();
|
||||||
assertThat(interpolator).isInstanceOf(MessageSourceInterpolatorDelegate.class);
|
assertThat(interpolator).isInstanceOf(MessageSourceMessageInterpolator.class);
|
||||||
assertThat(interpolator).hasFieldOrPropertyWithValue("messageSource", messageSource);
|
assertThat(interpolator).hasFieldOrPropertyWithValue("messageSource", messageSource);
|
||||||
assertThat(ReflectionTestUtils.getField(interpolator, "messageInterpolator"))
|
assertThat(ReflectionTestUtils.getField(interpolator, "messageInterpolator"))
|
||||||
.isInstanceOf(ParameterMessageInterpolator.class);
|
.isInstanceOf(ParameterMessageInterpolator.class);
|
||||||
|
|
|
@ -1,100 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.validation;
|
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests for {@link MessageParameterPlaceholderHelper}.
|
|
||||||
*
|
|
||||||
* @author Dmytro Nosan
|
|
||||||
*/
|
|
||||||
class MessageParameterPlaceholderHelperTests {
|
|
||||||
|
|
||||||
private final MessageParameterPlaceholderHelper resolver = new MessageParameterPlaceholderHelper();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void recursionInParameters() {
|
|
||||||
Map<String, String> props = new LinkedHashMap<>();
|
|
||||||
props.put("a", "{b}");
|
|
||||||
props.put("b", "{c}");
|
|
||||||
props.put("c", "{a}");
|
|
||||||
assertThatIllegalArgumentException().isThrownBy(() -> this.resolver.replaceParameters("{a}", props::get))
|
|
||||||
.withMessage("Circular reference '{a}'");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void replaceParameters() {
|
|
||||||
Map<String, String> props = new LinkedHashMap<>();
|
|
||||||
props.put("foo", "fooValue");
|
|
||||||
props.put("bar", "");
|
|
||||||
assertThat(this.resolver.replaceParameters("{foo}{bar}", props::get)).isEqualTo("fooValue");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void replaceNestedParameters() {
|
|
||||||
Map<String, String> props = new LinkedHashMap<>();
|
|
||||||
props.put("top", "{child}+{child}");
|
|
||||||
props.put("child", "{{differentiator}.grandchild}");
|
|
||||||
props.put("differentiator", "first");
|
|
||||||
props.put("first.grandchild", "actualValue");
|
|
||||||
assertThat(this.resolver.replaceParameters("{top}", props::get)).isEqualTo("actualValue+actualValue");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void unresolvedParameters() {
|
|
||||||
Map<String, String> props = new LinkedHashMap<>();
|
|
||||||
props.put("top", "{child}+{child}");
|
|
||||||
assertThat(this.resolver.replaceParameters("{foo}{top}{bar}", props::get))
|
|
||||||
.isEqualTo("{foo}{child}+{child}{bar}");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void unbalancedParentheses() {
|
|
||||||
Map<String, String> props = new LinkedHashMap<>();
|
|
||||||
props.put("top", "topValue");
|
|
||||||
assertThat(this.resolver.replaceParameters("\\{top}", props::get)).isEqualTo("\\{top}");
|
|
||||||
assertThat(this.resolver.replaceParameters("{top\\}", props::get)).isEqualTo("{top\\}");
|
|
||||||
assertThat(this.resolver.replaceParameters("{{top}", props::get)).isEqualTo("{{top}");
|
|
||||||
assertThat(this.resolver.replaceParameters("{top}}", props::get)).isEqualTo("topValue}");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void resolveEscapeParameters() {
|
|
||||||
Map<String, String> props = new LinkedHashMap<>();
|
|
||||||
props.put("foo", "fooValue");
|
|
||||||
props.put("bar", "\\{foo}");
|
|
||||||
props.put("bazz\\}", "bazzValue");
|
|
||||||
assertThat(this.resolver.replaceParameters("{foo}", props::get)).isEqualTo("fooValue");
|
|
||||||
assertThat(this.resolver.replaceParameters("{foo}\\a", props::get)).isEqualTo("fooValue\\a");
|
|
||||||
assertThat(this.resolver.replaceParameters("\\\\{foo}", props::get)).isEqualTo("\\\\fooValue");
|
|
||||||
assertThat(this.resolver.replaceParameters("\\\\\\{foo}", props::get)).isEqualTo("\\\\\\{foo}");
|
|
||||||
assertThat(this.resolver.replaceParameters("\\{foo}", props::get)).isEqualTo("\\{foo}");
|
|
||||||
assertThat(this.resolver.replaceParameters("{foo\\}", props::get)).isEqualTo("{foo\\}");
|
|
||||||
assertThat(this.resolver.replaceParameters("\\{foo\\}", props::get)).isEqualTo("\\{foo\\}");
|
|
||||||
assertThat(this.resolver.replaceParameters("{foo}\\", props::get)).isEqualTo("fooValue\\");
|
|
||||||
assertThat(this.resolver.replaceParameters("{bar}", props::get)).isEqualTo("\\{foo}");
|
|
||||||
assertThat(this.resolver.replaceParameters("{bazz\\}}", props::get)).isEqualTo("bazzValue");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -35,14 +35,17 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link MessageSourceInterpolatorDelegate}.
|
* Integration tests for {@link MessageSourceMessageInterpolator}.
|
||||||
*
|
*
|
||||||
* @author Dmytro Nosan
|
* @author Dmytro Nosan
|
||||||
*/
|
*/
|
||||||
class MessageSourceInterpolatorDelegateTests {
|
class MessageSourceMessageInterpolatorIntegrationTests {
|
||||||
|
|
||||||
private final Validator validator = buildValidator();
|
private final Validator validator = buildValidator();
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private String defaultMessage;
|
||||||
|
|
||||||
@NotNull(message = "{null}")
|
@NotNull(message = "{null}")
|
||||||
private String nullable;
|
private String nullable;
|
||||||
|
|
||||||
|
@ -67,6 +70,11 @@ class MessageSourceInterpolatorDelegateTests {
|
||||||
@NotNull(message = "\\\\{null}")
|
@NotNull(message = "\\\\{null}")
|
||||||
private String escapeEscape;
|
private String escapeEscape;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void defaultMessage() {
|
||||||
|
assertThat(validate("defaultMessage")).containsExactly("must not be null");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void nullable() {
|
void nullable() {
|
||||||
assertThat(validate("nullable")).containsExactly("must not be null");
|
assertThat(validate("nullable")).containsExactly("must not be null");
|
||||||
|
@ -79,7 +87,8 @@ class MessageSourceInterpolatorDelegateTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void recursion() {
|
void recursion() {
|
||||||
assertThatThrownBy(() -> validate("recursion")).hasStackTraceContaining("Circular reference '{recursion}'");
|
assertThatThrownBy(() -> validate("recursion"))
|
||||||
|
.hasStackTraceContaining("Circular reference '{recursion -> middle -> recursion}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -121,13 +130,14 @@ class MessageSourceInterpolatorDelegateTests {
|
||||||
StaticMessageSource messageSource = new StaticMessageSource();
|
StaticMessageSource messageSource = new StaticMessageSource();
|
||||||
messageSource.addMessage("blank", locale, "{null} or {javax.validation.constraints.NotBlank.message}");
|
messageSource.addMessage("blank", locale, "{null} or {javax.validation.constraints.NotBlank.message}");
|
||||||
messageSource.addMessage("null", locale, "{javax.validation.constraints.NotNull.message}");
|
messageSource.addMessage("null", locale, "{javax.validation.constraints.NotNull.message}");
|
||||||
messageSource.addMessage("recursion", locale, "{recursion}");
|
messageSource.addMessage("recursion", locale, "{middle}");
|
||||||
MessageInterpolatorFactory messageInterpolatorFactory = new MessageInterpolatorFactory();
|
messageSource.addMessage("middle", locale, "{recursion}");
|
||||||
messageInterpolatorFactory.setMessageSource(messageSource);
|
MessageInterpolatorFactory messageInterpolatorFactory = new MessageInterpolatorFactory(messageSource);
|
||||||
LocalValidatorFactoryBean validatorFactory = new LocalValidatorFactoryBean();
|
try (LocalValidatorFactoryBean validatorFactory = new LocalValidatorFactoryBean()) {
|
||||||
validatorFactory.setMessageInterpolator(messageInterpolatorFactory.getObject());
|
validatorFactory.setMessageInterpolator(messageInterpolatorFactory.getObject());
|
||||||
validatorFactory.afterPropertiesSet();
|
validatorFactory.afterPropertiesSet();
|
||||||
return validatorFactory.getValidator();
|
return validatorFactory.getValidator();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,119 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.validation;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import javax.validation.MessageInterpolator;
|
||||||
|
import javax.validation.MessageInterpolator.Context;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.context.support.StaticMessageSource;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link MessageSourceMessageInterpolator}.
|
||||||
|
*
|
||||||
|
* @author Dmytro Nosan
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
*/
|
||||||
|
class MessageSourceMessageInterpolatorTests {
|
||||||
|
|
||||||
|
private final Context context = mock(Context.class);
|
||||||
|
|
||||||
|
private final StaticMessageSource messageSource = new StaticMessageSource();
|
||||||
|
|
||||||
|
private final MessageSourceMessageInterpolator interpolator = new MessageSourceMessageInterpolator(
|
||||||
|
this.messageSource, new IdentityMessageInterpolator());
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void interpolateShouldReplaceParameters() {
|
||||||
|
this.messageSource.addMessage("foo", Locale.getDefault(), "fooValue");
|
||||||
|
this.messageSource.addMessage("bar", Locale.getDefault(), "");
|
||||||
|
assertThat(this.interpolator.interpolate("{foo}{bar}", this.context)).isEqualTo("fooValue");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void interpolateWhenParametersAreUnknownShouldLeaveThemUnchanged() {
|
||||||
|
this.messageSource.addMessage("top", Locale.getDefault(), "{child}+{child}");
|
||||||
|
assertThat(this.interpolator.interpolate("{foo}{top}{bar}", this.context))
|
||||||
|
.isEqualTo("{foo}{child}+{child}{bar}");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void interpolateWhenParametersAreNestedShouldFullyReplaceAllParameters() {
|
||||||
|
this.messageSource.addMessage("top", Locale.getDefault(), "{child}+{child}");
|
||||||
|
this.messageSource.addMessage("child", Locale.getDefault(), "{{differentiator}.grandchild}");
|
||||||
|
this.messageSource.addMessage("differentiator", Locale.getDefault(), "first");
|
||||||
|
this.messageSource.addMessage("first.grandchild", Locale.getDefault(), "actualValue");
|
||||||
|
assertThat(this.interpolator.interpolate("{top}", this.context)).isEqualTo("actualValue+actualValue");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void interpolateWhenParameterBracesAreUnbalancedShouldLeaveThemUnchanged() {
|
||||||
|
this.messageSource.addMessage("top", Locale.getDefault(), "topValue");
|
||||||
|
assertThat(this.interpolator.interpolate("\\{top}", this.context)).isEqualTo("\\{top}");
|
||||||
|
assertThat(this.interpolator.interpolate("{top\\}", this.context)).isEqualTo("{top\\}");
|
||||||
|
assertThat(this.interpolator.interpolate("{{top}", this.context)).isEqualTo("{{top}");
|
||||||
|
assertThat(this.interpolator.interpolate("{top}}", this.context)).isEqualTo("topValue}");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void interpolateWhenBracesAreEscapedShouldIgnore() {
|
||||||
|
this.messageSource.addMessage("foo", Locale.getDefault(), "fooValue");
|
||||||
|
this.messageSource.addMessage("bar", Locale.getDefault(), "\\{foo}");
|
||||||
|
this.messageSource.addMessage("bazz\\}", Locale.getDefault(), "bazzValue");
|
||||||
|
assertThat(this.interpolator.interpolate("{foo}", this.context)).isEqualTo("fooValue");
|
||||||
|
assertThat(this.interpolator.interpolate("{foo}\\a", this.context)).isEqualTo("fooValue\\a");
|
||||||
|
assertThat(this.interpolator.interpolate("\\\\{foo}", this.context)).isEqualTo("\\\\fooValue");
|
||||||
|
assertThat(this.interpolator.interpolate("\\\\\\{foo}", this.context)).isEqualTo("\\\\\\{foo}");
|
||||||
|
assertThat(this.interpolator.interpolate("\\{foo}", this.context)).isEqualTo("\\{foo}");
|
||||||
|
assertThat(this.interpolator.interpolate("{foo\\}", this.context)).isEqualTo("{foo\\}");
|
||||||
|
assertThat(this.interpolator.interpolate("\\{foo\\}", this.context)).isEqualTo("\\{foo\\}");
|
||||||
|
assertThat(this.interpolator.interpolate("{foo}\\", this.context)).isEqualTo("fooValue\\");
|
||||||
|
assertThat(this.interpolator.interpolate("{bar}", this.context)).isEqualTo("\\{foo}");
|
||||||
|
assertThat(this.interpolator.interpolate("{bazz\\}}", this.context)).isEqualTo("bazzValue");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void interpolateWhenParametersContainACycleShouldThrow() {
|
||||||
|
this.messageSource.addMessage("a", Locale.getDefault(), "{b}");
|
||||||
|
this.messageSource.addMessage("b", Locale.getDefault(), "{c}");
|
||||||
|
this.messageSource.addMessage("c", Locale.getDefault(), "{a}");
|
||||||
|
assertThatIllegalArgumentException().isThrownBy(() -> this.interpolator.interpolate("{a}", this.context))
|
||||||
|
.withMessage("Circular reference '{a -> b -> c -> a}'");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class IdentityMessageInterpolator implements MessageInterpolator {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String interpolate(String messageTemplate, Context context) {
|
||||||
|
return messageTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String interpolate(String messageTemplate, Context context, Locale locale) {
|
||||||
|
return messageTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue