Commit e1479820 authored by Phillip Webb's avatar Phillip Webb

Add FilteredClassLoader

Add `FilteredClassLoader` to replace `HideClassesClassLoader` and
`HidePackagesClassLoader`.

Fixes gh-10303
parent 74c48767
...@@ -25,7 +25,7 @@ import org.junit.Test; ...@@ -25,7 +25,7 @@ import org.junit.Test;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range; import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range;
import org.springframework.boot.system.JavaVersion; import org.springframework.boot.system.JavaVersion;
import org.springframework.boot.test.context.HideClassesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.Assume; import org.springframework.boot.testsupport.Assume;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -105,7 +105,7 @@ public class ConditionalOnJavaTests { ...@@ -105,7 +105,7 @@ public class ConditionalOnJavaTests {
} }
private String getJavaVersion(Class<?>... hiddenClasses) throws Exception { private String getJavaVersion(Class<?>... hiddenClasses) throws Exception {
HideClassesClassLoader classLoader = new HideClassesClassLoader(hiddenClasses); FilteredClassLoader classLoader = new FilteredClassLoader(hiddenClasses);
Class<?> javaVersionClass = classLoader.loadClass(JavaVersion.class.getName()); Class<?> javaVersionClass = classLoader.loadClass(JavaVersion.class.getName());
Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass, Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass,
"getJavaVersion"); "getJavaVersion");
......
...@@ -28,7 +28,7 @@ import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration; ...@@ -28,7 +28,7 @@ import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration.MappingJackson2HttpMessageConverterConfiguration; import org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration.MappingJackson2HttpMessageConverterConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration; import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration;
import org.springframework.boot.test.context.HidePackagesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.ContextConsumer;
...@@ -237,7 +237,7 @@ public class HttpMessageConvertersAutoConfigurationTests { ...@@ -237,7 +237,7 @@ public class HttpMessageConvertersAutoConfigurationTests {
@Test @Test
public void gsonIsPreferredIfJacksonIsNotAvailable() { public void gsonIsPreferredIfJacksonIsNotAvailable() {
allOptionsRunner().withClassLoader( allOptionsRunner().withClassLoader(
new HidePackagesClassLoader(ObjectMapper.class.getPackage().getName())) new FilteredClassLoader(ObjectMapper.class.getPackage().getName()))
.run((context) -> { .run((context) -> {
assertConverterBeanExists(context, GsonHttpMessageConverter.class, assertConverterBeanExists(context, GsonHttpMessageConverter.class,
"gsonHttpMessageConverter"); "gsonHttpMessageConverter");
...@@ -250,9 +250,9 @@ public class HttpMessageConvertersAutoConfigurationTests { ...@@ -250,9 +250,9 @@ public class HttpMessageConvertersAutoConfigurationTests {
@Test @Test
public void jsonbIsPreferredIfJacksonAndGsonAreNotAvailable() { public void jsonbIsPreferredIfJacksonAndGsonAreNotAvailable() {
allOptionsRunner() allOptionsRunner()
.withClassLoader(new HidePackagesClassLoader( .withClassLoader(
ObjectMapper.class.getPackage().getName(), new FilteredClassLoader(ObjectMapper.class.getPackage().getName(),
Gson.class.getPackage().getName())) Gson.class.getPackage().getName()))
.run(assertConverter(JsonbHttpMessageConverter.class, .run(assertConverter(JsonbHttpMessageConverter.class,
"jsonbHttpMessageConverter")); "jsonbHttpMessageConverter"));
} }
......
...@@ -42,7 +42,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -42,7 +42,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.jdbc.DatabaseDriver; import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection; import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.context.HidePackagesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -166,9 +166,9 @@ public class DataSourceAutoConfigurationTests { ...@@ -166,9 +166,9 @@ public class DataSourceAutoConfigurationTests {
@Test @Test
public void explicitTypeNoSupportedDataSource() { public void explicitTypeNoSupportedDataSource() {
this.contextRunner this.contextRunner
.withClassLoader(new HidePackagesClassLoader("org.apache.tomcat", .withClassLoader(
"com.zaxxer.hikari", "org.apache.commons.dbcp", new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
"org.apache.commons.dbcp2")) "org.apache.commons.dbcp", "org.apache.commons.dbcp2"))
.withPropertyValues( .withPropertyValues(
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver", "spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
"spring.datasource.url:jdbc:hsqldb:mem:testdb", "spring.datasource.url:jdbc:hsqldb:mem:testdb",
...@@ -227,7 +227,7 @@ public class DataSourceAutoConfigurationTests { ...@@ -227,7 +227,7 @@ public class DataSourceAutoConfigurationTests {
private <T extends DataSource> void assertDataSource(Class<T> expectedType, private <T extends DataSource> void assertDataSource(Class<T> expectedType,
List<String> hiddenPackages, Consumer<T> consumer) { List<String> hiddenPackages, Consumer<T> consumer) {
HidePackagesClassLoader classLoader = new HidePackagesClassLoader( FilteredClassLoader classLoader = new FilteredClassLoader(
hiddenPackages.toArray(new String[hiddenPackages.size()])); hiddenPackages.toArray(new String[hiddenPackages.size()]));
this.contextRunner.withClassLoader(classLoader).run((context) -> { this.contextRunner.withClassLoader(classLoader).run((context) -> {
DataSource bean = context.getBean(DataSource.class); DataSource bean = context.getBean(DataSource.class);
......
...@@ -21,7 +21,7 @@ import org.junit.Test; ...@@ -21,7 +21,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection; import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.context.HidePackagesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -69,7 +69,7 @@ public class DataSourcePropertiesTests { ...@@ -69,7 +69,7 @@ public class DataSourcePropertiesTests {
public void determineUrlWithNoEmbeddedSupport() throws Exception { public void determineUrlWithNoEmbeddedSupport() throws Exception {
DataSourceProperties properties = new DataSourceProperties(); DataSourceProperties properties = new DataSourceProperties();
properties.setBeanClassLoader( properties.setBeanClassLoader(
new HidePackagesClassLoader("org.h2", "org.apache.derby", "org.hsqldb")); new FilteredClassLoader("org.h2", "org.apache.derby", "org.hsqldb"));
properties.afterPropertiesSet(); properties.afterPropertiesSet();
this.thrown.expect(DataSourceProperties.DataSourceBeanCreationException.class); this.thrown.expect(DataSourceProperties.DataSourceBeanCreationException.class);
this.thrown.expectMessage("Cannot determine embedded database url"); this.thrown.expectMessage("Cannot determine embedded database url");
......
...@@ -25,7 +25,7 @@ import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoCo ...@@ -25,7 +25,7 @@ import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoCo
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.boot.test.context.HideClassesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext; import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext;
import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
...@@ -59,7 +59,7 @@ public class ReactiveSessionAutoConfigurationMongoTests ...@@ -59,7 +59,7 @@ public class ReactiveSessionAutoConfigurationMongoTests
@Test @Test
public void defaultConfigWithUniqueStoreImplementation() { public void defaultConfigWithUniqueStoreImplementation() {
this.contextRunner this.contextRunner
.withClassLoader(new HideClassesClassLoader( .withClassLoader(new FilteredClassLoader(
ReactiveRedisOperationsSessionRepository.class)) ReactiveRedisOperationsSessionRepository.class))
.withConfiguration(AutoConfigurations.of( .withConfiguration(AutoConfigurations.of(
EmbeddedMongoAutoConfiguration.class, EmbeddedMongoAutoConfiguration.class,
......
...@@ -25,7 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionEvaluationRepor ...@@ -25,7 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionEvaluationRepor
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration;
import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportMessage; import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportMessage;
import org.springframework.boot.test.context.HideClassesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext; import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext;
import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
...@@ -62,7 +62,7 @@ public class ReactiveSessionAutoConfigurationRedisTests ...@@ -62,7 +62,7 @@ public class ReactiveSessionAutoConfigurationRedisTests
@Test @Test
public void defaultConfigWithUniqueStoreImplementation() { public void defaultConfigWithUniqueStoreImplementation() {
this.contextRunner this.contextRunner
.withClassLoader(new HideClassesClassLoader( .withClassLoader(new FilteredClassLoader(
ReactiveMongoOperationsSessionRepository.class)) ReactiveMongoOperationsSessionRepository.class))
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class, .withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class,
RedisReactiveAutoConfiguration.class)) RedisReactiveAutoConfiguration.class))
......
...@@ -22,7 +22,7 @@ import org.junit.Test; ...@@ -22,7 +22,7 @@ import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.HideClassesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -61,7 +61,7 @@ public class SessionAutoConfigurationHazelcastTests ...@@ -61,7 +61,7 @@ public class SessionAutoConfigurationHazelcastTests
public void defaultConfigWithUniqueStoreImplementation() { public void defaultConfigWithUniqueStoreImplementation() {
this.contextRunner this.contextRunner
.withClassLoader( .withClassLoader(
new HideClassesClassLoader(JdbcOperationsSessionRepository.class, new FilteredClassLoader(JdbcOperationsSessionRepository.class,
RedisOperationsSessionRepository.class, RedisOperationsSessionRepository.class,
MongoOperationsSessionRepository.class)) MongoOperationsSessionRepository.class))
.run(this::validateDefaultConfig); .run(this::validateDefaultConfig);
......
...@@ -27,7 +27,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerA ...@@ -27,7 +27,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerA
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.boot.autoconfigure.session.JdbcSessionConfiguration.SpringBootJdbcHttpSessionConfiguration; import org.springframework.boot.autoconfigure.session.JdbcSessionConfiguration.SpringBootJdbcHttpSessionConfiguration;
import org.springframework.boot.jdbc.DataSourceInitializationMode; import org.springframework.boot.jdbc.DataSourceInitializationMode;
import org.springframework.boot.test.context.HideClassesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
...@@ -67,10 +67,9 @@ public class SessionAutoConfigurationJdbcTests ...@@ -67,10 +67,9 @@ public class SessionAutoConfigurationJdbcTests
@Test @Test
public void defaultConfigWithUniqueStoreImplementation() { public void defaultConfigWithUniqueStoreImplementation() {
this.contextRunner this.contextRunner
.withClassLoader( .withClassLoader(new FilteredClassLoader(HazelcastSessionRepository.class,
new HideClassesClassLoader(HazelcastSessionRepository.class, MongoOperationsSessionRepository.class,
MongoOperationsSessionRepository.class, RedisOperationsSessionRepository.class))
RedisOperationsSessionRepository.class))
.run(this::validateDefaultConfig); .run(this::validateDefaultConfig);
} }
......
...@@ -23,7 +23,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; ...@@ -23,7 +23,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.boot.test.context.HideClassesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
...@@ -57,10 +57,9 @@ public class SessionAutoConfigurationMongoTests ...@@ -57,10 +57,9 @@ public class SessionAutoConfigurationMongoTests
@Test @Test
public void defaultConfigWithUniqueStoreImplementation() { public void defaultConfigWithUniqueStoreImplementation() {
this.contextRunner this.contextRunner
.withClassLoader( .withClassLoader(new FilteredClassLoader(HazelcastSessionRepository.class,
new HideClassesClassLoader(HazelcastSessionRepository.class, JdbcOperationsSessionRepository.class,
JdbcOperationsSessionRepository.class, RedisOperationsSessionRepository.class))
RedisOperationsSessionRepository.class))
.withConfiguration(AutoConfigurations.of( .withConfiguration(AutoConfigurations.of(
EmbeddedMongoAutoConfiguration.class, EmbeddedMongoAutoConfiguration.class,
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class)) MongoAutoConfiguration.class, MongoDataAutoConfiguration.class))
......
...@@ -23,7 +23,7 @@ import org.springframework.beans.DirectFieldAccessor; ...@@ -23,7 +23,7 @@ import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.session.RedisSessionConfiguration.SpringBootRedisHttpSessionConfiguration; import org.springframework.boot.autoconfigure.session.RedisSessionConfiguration.SpringBootRedisHttpSessionConfiguration;
import org.springframework.boot.test.context.HideClassesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
...@@ -62,10 +62,9 @@ public class SessionAutoConfigurationRedisTests ...@@ -62,10 +62,9 @@ public class SessionAutoConfigurationRedisTests
@Test @Test
public void defaultConfigWithUniqueStoreImplementation() { public void defaultConfigWithUniqueStoreImplementation() {
this.contextRunner this.contextRunner
.withClassLoader( .withClassLoader(new FilteredClassLoader(HazelcastSessionRepository.class,
new HideClassesClassLoader(HazelcastSessionRepository.class, JdbcOperationsSessionRepository.class,
JdbcOperationsSessionRepository.class, MongoOperationsSessionRepository.class))
MongoOperationsSessionRepository.class))
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
.run(validateSpringSessionUsesRedis("spring:session:event:created:", .run(validateSpringSessionUsesRedis("spring:session:event:created:",
RedisFlushMode.ON_SAVE, "0 * * * * *")); RedisFlushMode.ON_SAVE, "0 * * * * *"));
......
...@@ -18,37 +18,109 @@ package org.springframework.boot.test.context; ...@@ -18,37 +18,109 @@ package org.springframework.boot.test.context;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.function.Predicate;
/** /**
* Test {@link URLClassLoader} that hides configurable packages. No class in one of those * Test {@link URLClassLoader} that can filter the classes it can load.
* packages or sub-packages are visible.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Phillip Webb
* @since 2.0.0 * @since 2.0.0
*/ */
public final class HidePackagesClassLoader extends URLClassLoader { public class FilteredClassLoader extends URLClassLoader {
private final String[] hiddenPackages; private final Predicate<String>[] filters;
/** /**
* Create a new instance with the packages to hide. * Create a {@link FilteredClassLoader} that hides the given classes.
* @param hiddenClasses the classes to hide
*/
public FilteredClassLoader(Class<?>... hiddenClasses) {
this(ClassFilter.of(hiddenClasses));
}
/**
* Create a {@link FilteredClassLoader} that hides classes from the given packages.
* @param hiddenPackages the packages to hide * @param hiddenPackages the packages to hide
*/ */
public HidePackagesClassLoader(String... hiddenPackages) { public FilteredClassLoader(String... hiddenPackages) {
super(new URL[0], HidePackagesClassLoader.class.getClassLoader()); this(PackageFilter.of(hiddenPackages));
this.hiddenPackages = hiddenPackages; }
/**
* Create a {@link FilteredClassLoader} that filters based on the given predicate.
* @param filters a set of filters to determine when a class name should be hidden. A
* {@link Predicate#test(Object) result} of {@code true} indicates a filtered class.
*/
@SafeVarargs
public FilteredClassLoader(Predicate<String>... filters) {
super(new URL[0], FilteredClassLoader.class.getClassLoader());
this.filters = filters;
} }
@Override @Override
protected Class<?> loadClass(String name, boolean resolve) protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException { throws ClassNotFoundException {
for (String hiddenPackage : this.hiddenPackages) { for (Predicate<String> filter : this.filters) {
if (name.startsWith(hiddenPackage)) { if (filter.test(name)) {
throw new ClassNotFoundException(); throw new ClassNotFoundException();
} }
} }
return super.loadClass(name, resolve); return super.loadClass(name, resolve);
} }
/**
* Filter to restrict the classes that can be loaded.
*/
public final static class ClassFilter implements Predicate<String> {
private Class<?>[] hiddenClasses;
private ClassFilter(Class<?>[] hiddenClasses) {
this.hiddenClasses = hiddenClasses;
}
@Override
public boolean test(String className) {
for (Class<?> hiddenClass : this.hiddenClasses) {
if (className.equals(hiddenClass.getName())) {
return true;
}
}
return false;
}
public static ClassFilter of(Class<?>... hiddenClasses) {
return new ClassFilter(hiddenClasses);
}
}
/**
* Filter to restrict the packages that can be loaded.
*/
public final static class PackageFilter implements Predicate<String> {
private final String[] hiddenPackages;
private PackageFilter(String[] hiddenPackages) {
this.hiddenPackages = hiddenPackages;
}
@Override
public boolean test(String className) {
for (String hiddenPackage : this.hiddenPackages) {
if (className.startsWith(hiddenPackage)) {
return true;
}
}
return false;
}
public static PackageFilter of(String... hiddenPackages) {
return new PackageFilter(hiddenPackages);
}
}
} }
...@@ -23,7 +23,7 @@ import java.util.function.Supplier; ...@@ -23,7 +23,7 @@ import java.util.function.Supplier;
import org.springframework.boot.context.annotation.Configurations; import org.springframework.boot.context.annotation.Configurations;
import org.springframework.boot.context.annotation.UserConfigurations; import org.springframework.boot.context.annotation.UserConfigurations;
import org.springframework.boot.test.context.HidePackagesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.ApplicationContextAssert; import org.springframework.boot.test.context.assertj.ApplicationContextAssert;
import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider; import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
...@@ -180,7 +180,7 @@ abstract class AbstractApplicationContextRunner<SELF extends AbstractApplication ...@@ -180,7 +180,7 @@ abstract class AbstractApplicationContextRunner<SELF extends AbstractApplication
* the classpath. * the classpath.
* @param classLoader the classloader to use (can be null to use the default) * @param classLoader the classloader to use (can be null to use the default)
* @return a new instance with the updated class loader * @return a new instance with the updated class loader
* @see HidePackagesClassLoader * @see FilteredClassLoader
*/ */
public SELF withClassLoader(ClassLoader classLoader) { public SELF withClassLoader(ClassLoader classLoader) {
return newInstance(this.contextFactory, this.environmentProperties, return newInstance(this.contextFactory, this.environmentProperties,
......
...@@ -16,33 +16,47 @@ ...@@ -16,33 +16,47 @@
package org.springframework.boot.test.context; package org.springframework.boot.test.context;
import java.net.URL; import org.junit.Rule;
import java.net.URLClassLoader; import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Test {@link URLClassLoader} that hides configurable classes. * Tests for {@link FilteredClassLoader}.
* *
* @author Stephane Nicoll * @author Phillip Webb
* @since 2.0.0
*/ */
public class HideClassesClassLoader extends URLClassLoader { public class FilteredClassLoaderTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private final Class<?>[] hiddenClasses; @Test
public void loadClassWhenFilteredOnPackageShouldThrowClassNotFound()
throws Exception {
FilteredClassLoader classLoader = new FilteredClassLoader(
FilteredClassLoaderTests.class.getPackage().getName());
this.thrown.expect(ClassNotFoundException.class);
classLoader.loadClass(getClass().getName());
classLoader.close();
}
public HideClassesClassLoader(Class<?>... hiddenClasses) { @Test
super(new URL[0], HideClassesClassLoader.class.getClassLoader()); public void loadClassWhenFilteredOnClassShouldThrowClassNotFound() throws Exception {
this.hiddenClasses = hiddenClasses; FilteredClassLoader classLoader = new FilteredClassLoader(
FilteredClassLoaderTests.class);
this.thrown.expect(ClassNotFoundException.class);
classLoader.loadClass(getClass().getName());
classLoader.close();
} }
@Override @Test
protected Class<?> loadClass(String name, boolean resolve) public void loadClassWhenNotFilteredShouldLoadClass() throws Exception {
throws ClassNotFoundException { FilteredClassLoader classLoader = new FilteredClassLoader((className) -> false);
for (Class<?> hiddenClass : this.hiddenClasses) { Class<?> loaded = classLoader.loadClass(getClass().getName());
if (name.equals(hiddenClass.getName())) { assertThat(loaded.getName()).isEqualTo(getClass().getName());
throw new ClassNotFoundException(); classLoader.close();
}
}
return super.loadClass(name, resolve);
} }
} }
...@@ -25,7 +25,7 @@ import org.junit.Test; ...@@ -25,7 +25,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.boot.context.annotation.UserConfigurations; import org.springframework.boot.context.annotation.UserConfigurations;
import org.springframework.boot.test.context.HidePackagesClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider; import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -34,7 +34,7 @@ import org.springframework.core.env.Environment; ...@@ -34,7 +34,7 @@ import org.springframework.core.env.Environment;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail; import static org.junit.Assert.fail;
/** /**
* Abstract tests for {@link AbstractApplicationContextRunner} implementations. * Abstract tests for {@link AbstractApplicationContextRunner} implementations.
...@@ -148,8 +148,7 @@ public abstract class AbstractApplicationContextRunnerTests<T extends AbstractAp ...@@ -148,8 +148,7 @@ public abstract class AbstractApplicationContextRunnerTests<T extends AbstractAp
@Test @Test
public void runWithClassLoaderShouldSetClassLoader() throws Exception { public void runWithClassLoaderShouldSetClassLoader() throws Exception {
get().withClassLoader( get().withClassLoader(new FilteredClassLoader(Gson.class.getPackage().getName()))
new HidePackagesClassLoader(Gson.class.getPackage().getName()))
.run((context) -> { .run((context) -> {
try { try {
ClassUtils.forName(Gson.class.getName(), ClassUtils.forName(Gson.class.getName(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment