Merge branch '2.1.x'
This commit is contained in:
commit
8daa8c48fb
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.validation;
|
package org.springframework.boot.autoconfigure.validation;
|
||||||
|
|
||||||
|
import javax.validation.ValidationException;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.DisposableBean;
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
|
@ -135,7 +137,13 @@ public class ValidatorAdapter implements SmartValidator, ApplicationContextAware
|
||||||
|
|
||||||
private static Validator create() {
|
private static Validator create() {
|
||||||
OptionalValidatorFactoryBean validator = new OptionalValidatorFactoryBean();
|
OptionalValidatorFactoryBean validator = new OptionalValidatorFactoryBean();
|
||||||
validator.setMessageInterpolator(new MessageInterpolatorFactory().getObject());
|
try {
|
||||||
|
validator
|
||||||
|
.setMessageInterpolator(new MessageInterpolatorFactory().getObject());
|
||||||
|
}
|
||||||
|
catch (ValidationException ex) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
return wrap(validator, false);
|
return wrap(validator, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,13 +20,14 @@ import java.util.HashMap;
|
||||||
|
|
||||||
import javax.validation.constraints.Min;
|
import javax.validation.constraints.Min;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.validation.MapBindingResult;
|
import org.springframework.validation.MapBindingResult;
|
||||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||||
|
|
||||||
|
|
@ -41,60 +42,65 @@ import static org.mockito.Mockito.verify;
|
||||||
* Tests for {@link ValidatorAdapter}.
|
* Tests for {@link ValidatorAdapter}.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Madhura Bhave
|
||||||
*/
|
*/
|
||||||
public class ValidatorAdapterTests {
|
public class ValidatorAdapterTests {
|
||||||
|
|
||||||
private AnnotationConfigApplicationContext context;
|
private ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||||
|
|
||||||
@After
|
|
||||||
public void close() {
|
|
||||||
if (this.context != null) {
|
|
||||||
this.context.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void wrapLocalValidatorFactoryBean() {
|
public void wrapLocalValidatorFactoryBean() {
|
||||||
ValidatorAdapter wrapper = load(LocalValidatorFactoryBeanConfig.class);
|
this.contextRunner.withUserConfiguration(LocalValidatorFactoryBeanConfig.class)
|
||||||
assertThat(wrapper.supports(SampleData.class)).isTrue();
|
.run((context) -> {
|
||||||
MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(),
|
ValidatorAdapter wrapper = context.getBean(ValidatorAdapter.class);
|
||||||
"test");
|
assertThat(wrapper.supports(SampleData.class)).isTrue();
|
||||||
wrapper.validate(new SampleData(40), errors);
|
MapBindingResult errors = new MapBindingResult(
|
||||||
assertThat(errors.getErrorCount()).isEqualTo(1);
|
new HashMap<String, Object>(), "test");
|
||||||
|
wrapper.validate(new SampleData(40), errors);
|
||||||
|
assertThat(errors.getErrorCount()).isEqualTo(1);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void wrapperInvokesCallbackOnNonManagedBean() {
|
public void wrapperInvokesCallbackOnNonManagedBean() {
|
||||||
load(NonManagedBeanConfig.class);
|
this.contextRunner.withUserConfiguration(NonManagedBeanConfig.class)
|
||||||
LocalValidatorFactoryBean validator = this.context
|
.run((context) -> {
|
||||||
.getBean(NonManagedBeanConfig.class).validator;
|
LocalValidatorFactoryBean validator = context
|
||||||
verify(validator, times(1)).setApplicationContext(any(ApplicationContext.class));
|
.getBean(NonManagedBeanConfig.class).validator;
|
||||||
verify(validator, times(1)).afterPropertiesSet();
|
verify(validator, times(1))
|
||||||
verify(validator, never()).destroy();
|
.setApplicationContext(any(ApplicationContext.class));
|
||||||
this.context.close();
|
verify(validator, times(1)).afterPropertiesSet();
|
||||||
this.context = null;
|
verify(validator, never()).destroy();
|
||||||
verify(validator, times(1)).destroy();
|
context.close();
|
||||||
|
verify(validator, times(1)).destroy();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void wrapperDoesNotInvokeCallbackOnManagedBean() {
|
public void wrapperDoesNotInvokeCallbackOnManagedBean() {
|
||||||
load(ManagedBeanConfig.class);
|
this.contextRunner.withUserConfiguration(ManagedBeanConfig.class)
|
||||||
LocalValidatorFactoryBean validator = this.context
|
.run((context) -> {
|
||||||
.getBean(ManagedBeanConfig.class).validator;
|
LocalValidatorFactoryBean validator = context
|
||||||
verify(validator, never()).setApplicationContext(any(ApplicationContext.class));
|
.getBean(ManagedBeanConfig.class).validator;
|
||||||
verify(validator, never()).afterPropertiesSet();
|
verify(validator, never())
|
||||||
verify(validator, never()).destroy();
|
.setApplicationContext(any(ApplicationContext.class));
|
||||||
this.context.close();
|
verify(validator, never()).afterPropertiesSet();
|
||||||
this.context = null;
|
verify(validator, never()).destroy();
|
||||||
verify(validator, never()).destroy();
|
context.close();
|
||||||
|
verify(validator, never()).destroy();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private ValidatorAdapter load(Class<?> config) {
|
@Test
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
public void wrapperWhenValidationProviderNotPresentShouldNotThrowException() {
|
||||||
ctx.register(config);
|
ClassPathResource hibernateValidator = new ClassPathResource(
|
||||||
ctx.refresh();
|
"META-INF/services/javax.validation.spi.ValidationProvider");
|
||||||
this.context = ctx;
|
this.contextRunner
|
||||||
return this.context.getBean(ValidatorAdapter.class);
|
.withClassLoader(new FilteredClassLoader(
|
||||||
|
FilteredClassLoader.ClassPathResourceFilter
|
||||||
|
.of(hibernateValidator),
|
||||||
|
FilteredClassLoader.PackageFilter.of("org.hibernate.validator")))
|
||||||
|
.run((context) -> ValidatorAdapter.get(context, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,14 @@
|
||||||
|
|
||||||
package org.springframework.boot.test.context;
|
package org.springframework.boot.test.context;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Enumeration;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
|
@ -109,6 +112,26 @@ public class FilteredClassLoader extends URLClassLoader {
|
||||||
return super.getResource(name);
|
return super.getResource(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Enumeration<URL> getResources(String name) throws IOException {
|
||||||
|
for (Predicate<String> filter : this.resourcesFilters) {
|
||||||
|
if (filter.test(name)) {
|
||||||
|
return Collections.emptyEnumeration();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return super.getResources(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getResourceAsStream(String name) {
|
||||||
|
for (Predicate<String> filter : this.resourcesFilters) {
|
||||||
|
if (filter.test(name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return super.getResourceAsStream(name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter to restrict the classes that can be loaded.
|
* Filter to restrict the classes that can be loaded.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,9 @@
|
||||||
|
|
||||||
package org.springframework.boot.test.context;
|
package org.springframework.boot.test.context;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
@ -81,4 +83,44 @@ public class FilteredClassLoaderTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loadResourcesWhenFilteredOnResourceShouldReturnNotFound()
|
||||||
|
throws Exception {
|
||||||
|
try (FilteredClassLoader classLoader = new FilteredClassLoader(TEST_RESOURCE)) {
|
||||||
|
final Enumeration<URL> loaded = classLoader
|
||||||
|
.getResources(TEST_RESOURCE.getPath());
|
||||||
|
assertThat(loaded.hasMoreElements()).isFalse();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loadResourcesWhenNotFilteredShouldLoadResource() throws Exception {
|
||||||
|
try (FilteredClassLoader classLoader = new FilteredClassLoader(
|
||||||
|
(resourceName) -> false)) {
|
||||||
|
final Enumeration<URL> loaded = classLoader
|
||||||
|
.getResources(TEST_RESOURCE.getPath());
|
||||||
|
assertThat(loaded.hasMoreElements()).isTrue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loadResourceAsStreamWhenFilteredOnResourceShouldReturnNotFound()
|
||||||
|
throws Exception {
|
||||||
|
try (FilteredClassLoader classLoader = new FilteredClassLoader(TEST_RESOURCE)) {
|
||||||
|
final InputStream loaded = classLoader
|
||||||
|
.getResourceAsStream(TEST_RESOURCE.getPath());
|
||||||
|
assertThat(loaded).isNull();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loadResourceAsStreamWhenNotFilteredShouldLoadResource() throws Exception {
|
||||||
|
try (FilteredClassLoader classLoader = new FilteredClassLoader(
|
||||||
|
(resourceName) -> false)) {
|
||||||
|
final InputStream loaded = classLoader
|
||||||
|
.getResourceAsStream(TEST_RESOURCE.getPath());
|
||||||
|
assertThat(loaded).isNotNull();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue