Browse Source
Update `ValidationAutoConfiguration` and `WebMvcAutoConfiguration` to ensure as much as possible that only a single Validator bean of each type is registered. Validation auto-configuration now does the following: - If no validator is found: Registers a `LocalValidatorFactoryBean` (providing both Spring and JSR validation) - If the user defines a Spring & JSR validator: Backs off - If the user defines only a JSR validator: Adapts it to a Spring validator (without exposing another JSR implementation) WebMvcAutoConfiguration auto-configuration has been updated to make MVC validation follow common Spring Boot patterns: - If not validator beans are found (due to the user excluding ValidationAutoConfiguration) a new `mvcValidator` bean will be registered. - If a single validator bean is found it will be used for MVC validation. - If multiple validator beans are defined it will either use the one named `mvcValidator` or it will register a new `mvcValidator` bean Any automatically registered `mvcValidator` bean will not implement the JSR validator interface. Finally, it is no longer possible to provide an MVC validator via a `WebMvcConfigurer`. Fixes gh-8495pull/7954/merge
14 changed files with 774 additions and 431 deletions
@ -0,0 +1,47 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2017 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 |
||||||
|
* |
||||||
|
* http://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.autoconfigure.validation; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinition; |
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||||
|
import org.springframework.boot.validation.MessageInterpolatorFactory; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.context.annotation.Role; |
||||||
|
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
||||||
|
|
||||||
|
/** |
||||||
|
* Default validator configuration imported by {@link ValidationAutoConfiguration}. |
||||||
|
* |
||||||
|
* @author Stephane Nicoll |
||||||
|
* @author Phillip Webb |
||||||
|
*/ |
||||||
|
@Configuration |
||||||
|
class DefaultValidatorConfiguration { |
||||||
|
|
||||||
|
@Bean |
||||||
|
@ConditionalOnMissingBean(type = { "javax.validation.Validator", |
||||||
|
"org.springframework.validation.Validator" }) |
||||||
|
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
||||||
|
public static LocalValidatorFactoryBean defaultValidator() { |
||||||
|
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean(); |
||||||
|
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(); |
||||||
|
factoryBean.setMessageInterpolator(interpolatorFactory.getObject()); |
||||||
|
return factoryBean; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,78 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2017 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 |
||||||
|
* |
||||||
|
* http://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.autoconfigure.validation; |
||||||
|
|
||||||
|
import org.springframework.util.Assert; |
||||||
|
import org.springframework.validation.Errors; |
||||||
|
import org.springframework.validation.SmartValidator; |
||||||
|
import org.springframework.validation.Validator; |
||||||
|
import org.springframework.validation.beanvalidation.SpringValidatorAdapter; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@link Validator} implementation that delegates calls to another {@link Validator}. |
||||||
|
* This {@link Validator} implements Spring's {@link SmartValidator} interface but does |
||||||
|
* not implement the JSR-303 {@code javax.validator.Validator} interface. |
||||||
|
* |
||||||
|
* @author Phillip Webb |
||||||
|
* @since 1.5.3 |
||||||
|
*/ |
||||||
|
public class DelegatingValidator implements SmartValidator { |
||||||
|
|
||||||
|
private final Validator delegate; |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new {@link DelegatingValidator} instance. |
||||||
|
* @param targetValidator the target JSR validator |
||||||
|
*/ |
||||||
|
public DelegatingValidator(javax.validation.Validator targetValidator) { |
||||||
|
this.delegate = new SpringValidatorAdapter(targetValidator); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new {@link DelegatingValidator} instance. |
||||||
|
* @param targetValidator the target validator |
||||||
|
*/ |
||||||
|
public DelegatingValidator(Validator targetValidator) { |
||||||
|
Assert.notNull(targetValidator, "Target Validator must not be null"); |
||||||
|
this.delegate = targetValidator; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean supports(Class<?> clazz) { |
||||||
|
return this.delegate.supports(clazz); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void validate(Object target, Errors errors) { |
||||||
|
this.delegate.validate(target, errors); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void validate(Object target, Errors errors, Object... validationHints) { |
||||||
|
if (this.delegate instanceof SmartValidator) { |
||||||
|
((SmartValidator) this.delegate).validate(target, errors, validationHints); |
||||||
|
} |
||||||
|
else { |
||||||
|
this.delegate.validate(target, errors); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
protected final Validator getDelegate() { |
||||||
|
return this.delegate; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,46 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2017 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 |
||||||
|
* |
||||||
|
* http://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.autoconfigure.validation; |
||||||
|
|
||||||
|
import javax.validation.Validator; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinition; |
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.context.annotation.Role; |
||||||
|
import org.springframework.validation.SmartValidator; |
||||||
|
|
||||||
|
/** |
||||||
|
* JSR 303 adapter configration imported by {@link ValidationAutoConfiguration}. |
||||||
|
* |
||||||
|
* @author Stephane Nicoll |
||||||
|
* @author Phillip Webb |
||||||
|
*/ |
||||||
|
@Configuration |
||||||
|
class Jsr303ValidatorAdapterConfiguration { |
||||||
|
|
||||||
|
@Bean |
||||||
|
@ConditionalOnSingleCandidate(Validator.class) |
||||||
|
@ConditionalOnMissingBean(org.springframework.validation.Validator.class) |
||||||
|
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
||||||
|
public SmartValidator jsr303ValidatorAdapter(Validator validator) { |
||||||
|
return new DelegatingValidator(validator); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -1,144 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-2017 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 |
|
||||||
* |
|
||||||
* http://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.autoconfigure.web; |
|
||||||
|
|
||||||
import org.springframework.beans.BeansException; |
|
||||||
import org.springframework.beans.factory.DisposableBean; |
|
||||||
import org.springframework.beans.factory.InitializingBean; |
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
|
||||||
import org.springframework.boot.validation.MessageInterpolatorFactory; |
|
||||||
import org.springframework.context.ApplicationContext; |
|
||||||
import org.springframework.context.ApplicationContextAware; |
|
||||||
import org.springframework.validation.Errors; |
|
||||||
import org.springframework.validation.SmartValidator; |
|
||||||
import org.springframework.validation.Validator; |
|
||||||
import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean; |
|
||||||
import org.springframework.validation.beanvalidation.SpringValidatorAdapter; |
|
||||||
|
|
||||||
/** |
|
||||||
* A {@link SmartValidator} exposed as a bean for WebMvc use. Wraps existing |
|
||||||
* {@link SpringValidatorAdapter} instances so that only the Spring's {@link Validator} |
|
||||||
* type is exposed. This prevents such a bean to expose both the Spring and JSR-303 |
|
||||||
* validator contract at the same time. |
|
||||||
* |
|
||||||
* @author Stephane Nicoll |
|
||||||
* @author Phillip Webb |
|
||||||
*/ |
|
||||||
class WebMvcValidator implements SmartValidator, ApplicationContextAware, |
|
||||||
InitializingBean, DisposableBean { |
|
||||||
|
|
||||||
private final SpringValidatorAdapter target; |
|
||||||
|
|
||||||
private final boolean existingBean; |
|
||||||
|
|
||||||
WebMvcValidator(SpringValidatorAdapter target, boolean existingBean) { |
|
||||||
this.target = target; |
|
||||||
this.existingBean = existingBean; |
|
||||||
} |
|
||||||
|
|
||||||
SpringValidatorAdapter getTarget() { |
|
||||||
return this.target; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean supports(Class<?> clazz) { |
|
||||||
return this.target.supports(clazz); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void validate(Object target, Errors errors) { |
|
||||||
this.target.validate(target, errors); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void validate(Object target, Errors errors, Object... validationHints) { |
|
||||||
this.target.validate(target, errors, validationHints); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void setApplicationContext(ApplicationContext applicationContext) |
|
||||||
throws BeansException { |
|
||||||
if (!this.existingBean && this.target instanceof ApplicationContextAware) { |
|
||||||
((ApplicationContextAware) this.target) |
|
||||||
.setApplicationContext(applicationContext); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void afterPropertiesSet() throws Exception { |
|
||||||
if (!this.existingBean && this.target instanceof InitializingBean) { |
|
||||||
((InitializingBean) this.target).afterPropertiesSet(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void destroy() throws Exception { |
|
||||||
if (!this.existingBean && this.target instanceof DisposableBean) { |
|
||||||
((DisposableBean) this.target).destroy(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public static Validator get(ApplicationContext applicationContext, |
|
||||||
Validator validator) { |
|
||||||
if (validator != null) { |
|
||||||
return wrap(validator, false); |
|
||||||
} |
|
||||||
return getExistingOrCreate(applicationContext); |
|
||||||
} |
|
||||||
|
|
||||||
private static Validator getExistingOrCreate(ApplicationContext applicationContext) { |
|
||||||
Validator existing = getExisting(applicationContext); |
|
||||||
if (existing != null) { |
|
||||||
return wrap(existing, true); |
|
||||||
} |
|
||||||
return create(); |
|
||||||
} |
|
||||||
|
|
||||||
private static Validator getExisting(ApplicationContext applicationContext) { |
|
||||||
try { |
|
||||||
javax.validation.Validator validator = applicationContext |
|
||||||
.getBean(javax.validation.Validator.class); |
|
||||||
if (validator instanceof Validator) { |
|
||||||
return (Validator) validator; |
|
||||||
} |
|
||||||
return new SpringValidatorAdapter(validator); |
|
||||||
} |
|
||||||
catch (NoSuchBeanDefinitionException ex) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
private static Validator create() { |
|
||||||
OptionalValidatorFactoryBean validator = new OptionalValidatorFactoryBean(); |
|
||||||
validator.setMessageInterpolator(new MessageInterpolatorFactory().getObject()); |
|
||||||
return wrap(validator, false); |
|
||||||
} |
|
||||||
|
|
||||||
private static Validator wrap(Validator validator, boolean existingBean) { |
|
||||||
if (validator instanceof javax.validation.Validator) { |
|
||||||
if (validator instanceof SpringValidatorAdapter) { |
|
||||||
return new WebMvcValidator((SpringValidatorAdapter) validator, |
|
||||||
existingBean); |
|
||||||
} |
|
||||||
return new WebMvcValidator( |
|
||||||
new SpringValidatorAdapter((javax.validation.Validator) validator), |
|
||||||
existingBean); |
|
||||||
} |
|
||||||
return validator; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -0,0 +1,117 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2017 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 |
||||||
|
* |
||||||
|
* http://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.autoconfigure.validation; |
||||||
|
|
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Rule; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.rules.ExpectedException; |
||||||
|
import org.mockito.Mock; |
||||||
|
import org.mockito.MockitoAnnotations; |
||||||
|
|
||||||
|
import org.springframework.validation.BeanPropertyBindingResult; |
||||||
|
import org.springframework.validation.Errors; |
||||||
|
import org.springframework.validation.SmartValidator; |
||||||
|
import org.springframework.validation.Validator; |
||||||
|
|
||||||
|
import static org.mockito.Matchers.any; |
||||||
|
import static org.mockito.Mockito.mock; |
||||||
|
import static org.mockito.Mockito.verify; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link DelegatingValidator}. |
||||||
|
* |
||||||
|
* @author Phillip Webb |
||||||
|
*/ |
||||||
|
public class DelegatingValidatorTests { |
||||||
|
|
||||||
|
@Rule |
||||||
|
public ExpectedException thrown = ExpectedException.none(); |
||||||
|
|
||||||
|
@Mock |
||||||
|
private SmartValidator delegate; |
||||||
|
|
||||||
|
private DelegatingValidator delegating; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setup() { |
||||||
|
MockitoAnnotations.initMocks(this); |
||||||
|
this.delegating = new DelegatingValidator(this.delegate); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void createWhenJsrValidatorIsNullShouldThrowException() throws Exception { |
||||||
|
this.thrown.expect(IllegalArgumentException.class); |
||||||
|
this.thrown.expectMessage("Target Validator must not be null"); |
||||||
|
new DelegatingValidator((javax.validation.Validator) null); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void createWithJsrValidatorShouldAdapt() throws Exception { |
||||||
|
javax.validation.Validator delegate = mock(javax.validation.Validator.class); |
||||||
|
Validator delegating = new DelegatingValidator(delegate); |
||||||
|
Object target = new Object(); |
||||||
|
Errors errors = new BeanPropertyBindingResult(target, "foo"); |
||||||
|
delegating.validate(target, errors); |
||||||
|
verify(delegate).validate(any()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void createWithSpringValidatorWhenValidatorIsNullShouldThrowException() |
||||||
|
throws Exception { |
||||||
|
this.thrown.expect(IllegalArgumentException.class); |
||||||
|
this.thrown.expectMessage("Target Validator must not be null"); |
||||||
|
new DelegatingValidator((Validator) null); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void supportsShouldDelegateToValidator() throws Exception { |
||||||
|
this.delegating.supports(Object.class); |
||||||
|
verify(this.delegate).supports(Object.class); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void validateShouldDelegateToValidator() throws Exception { |
||||||
|
Object target = new Object(); |
||||||
|
Errors errors = new BeanPropertyBindingResult(target, "foo"); |
||||||
|
this.delegating.validate(target, errors); |
||||||
|
verify(this.delegate).validate(target, errors); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void validateWithHintsShouldDelegateToValidator() throws Exception { |
||||||
|
Object target = new Object(); |
||||||
|
Errors errors = new BeanPropertyBindingResult(target, "foo"); |
||||||
|
Object[] hints = { "foo", "bar" }; |
||||||
|
this.delegating.validate(target, errors, hints); |
||||||
|
verify(this.delegate).validate(target, errors, hints); |
||||||
|
; |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void validateWithHintsWhenDelegateIsNotSmartShouldDelegateToSimpleValidator() |
||||||
|
throws Exception { |
||||||
|
Validator delegate = mock(Validator.class); |
||||||
|
DelegatingValidator delegating = new DelegatingValidator(delegate); |
||||||
|
Object target = new Object(); |
||||||
|
Errors errors = new BeanPropertyBindingResult(target, "foo"); |
||||||
|
Object[] hints = { "foo", "bar" }; |
||||||
|
delegating.validate(target, errors, hints); |
||||||
|
verify(delegate).validate(target, errors); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -1,152 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-2017 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 |
|
||||||
* |
|
||||||
* http://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.autoconfigure.web; |
|
||||||
|
|
||||||
import java.util.HashMap; |
|
||||||
|
|
||||||
import javax.validation.constraints.Min; |
|
||||||
|
|
||||||
import org.junit.After; |
|
||||||
import org.junit.Test; |
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext; |
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
|
||||||
import org.springframework.context.annotation.Bean; |
|
||||||
import org.springframework.context.annotation.Configuration; |
|
||||||
import org.springframework.validation.MapBindingResult; |
|
||||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat; |
|
||||||
import static org.mockito.Matchers.any; |
|
||||||
import static org.mockito.Mockito.mock; |
|
||||||
import static org.mockito.Mockito.times; |
|
||||||
import static org.mockito.Mockito.verify; |
|
||||||
|
|
||||||
/** |
|
||||||
* Tests for {@link WebMvcValidator}. |
|
||||||
* |
|
||||||
* @author Stephane Nicoll |
|
||||||
*/ |
|
||||||
public class WebMvcValidatorTests { |
|
||||||
|
|
||||||
private AnnotationConfigApplicationContext context; |
|
||||||
|
|
||||||
@After |
|
||||||
public void close() { |
|
||||||
if (this.context != null) { |
|
||||||
this.context.close(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void wrapLocalValidatorFactoryBean() { |
|
||||||
WebMvcValidator wrapper = load( |
|
||||||
LocalValidatorFactoryBeanConfig.class); |
|
||||||
assertThat(wrapper.supports(SampleData.class)).isTrue(); |
|
||||||
MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), |
|
||||||
"test"); |
|
||||||
wrapper.validate(new SampleData(40), errors); |
|
||||||
assertThat(errors.getErrorCount()).isEqualTo(1); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void wrapperInvokesCallbackOnNonManagedBean() { |
|
||||||
load(NonManagedBeanConfig.class); |
|
||||||
LocalValidatorFactoryBean validator = this.context |
|
||||||
.getBean(NonManagedBeanConfig.class).validator; |
|
||||||
verify(validator, times(1)).setApplicationContext(any(ApplicationContext.class)); |
|
||||||
verify(validator, times(1)).afterPropertiesSet(); |
|
||||||
verify(validator, times(0)).destroy(); |
|
||||||
this.context.close(); |
|
||||||
this.context = null; |
|
||||||
verify(validator, times(1)).destroy(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void wrapperDoesNotInvokeCallbackOnManagedBean() { |
|
||||||
load(ManagedBeanConfig.class); |
|
||||||
LocalValidatorFactoryBean validator = this.context |
|
||||||
.getBean(ManagedBeanConfig.class).validator; |
|
||||||
verify(validator, times(0)).setApplicationContext(any(ApplicationContext.class)); |
|
||||||
verify(validator, times(0)).afterPropertiesSet(); |
|
||||||
verify(validator, times(0)).destroy(); |
|
||||||
this.context.close(); |
|
||||||
this.context = null; |
|
||||||
verify(validator, times(0)).destroy(); |
|
||||||
} |
|
||||||
|
|
||||||
private WebMvcValidator load(Class<?> config) { |
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
|
||||||
ctx.register(config); |
|
||||||
ctx.refresh(); |
|
||||||
this.context = ctx; |
|
||||||
return this.context.getBean(WebMvcValidator.class); |
|
||||||
} |
|
||||||
|
|
||||||
@Configuration |
|
||||||
static class LocalValidatorFactoryBeanConfig { |
|
||||||
|
|
||||||
@Bean |
|
||||||
public LocalValidatorFactoryBean validator() { |
|
||||||
return new LocalValidatorFactoryBean(); |
|
||||||
} |
|
||||||
|
|
||||||
@Bean |
|
||||||
public WebMvcValidator wrapper() { |
|
||||||
return new WebMvcValidator(validator(), true); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@Configuration |
|
||||||
static class NonManagedBeanConfig { |
|
||||||
|
|
||||||
private final LocalValidatorFactoryBean validator = mock( |
|
||||||
LocalValidatorFactoryBean.class); |
|
||||||
|
|
||||||
@Bean |
|
||||||
public WebMvcValidator wrapper() { |
|
||||||
return new WebMvcValidator(this.validator, false); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@Configuration |
|
||||||
static class ManagedBeanConfig { |
|
||||||
|
|
||||||
private final LocalValidatorFactoryBean validator = mock( |
|
||||||
LocalValidatorFactoryBean.class); |
|
||||||
|
|
||||||
@Bean |
|
||||||
public WebMvcValidator wrapper() { |
|
||||||
return new WebMvcValidator(this.validator, true); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
static class SampleData { |
|
||||||
|
|
||||||
@Min(42) |
|
||||||
private int counter; |
|
||||||
|
|
||||||
SampleData(int counter) { |
|
||||||
this.counter = counter; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
Loading…
Reference in new issue