Add FilteredClassLoader

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

Fixes gh-10303
This commit is contained in:
Phillip Webb
2017-11-18 22:39:52 -08:00
parent 74c48767a1
commit e147982045
16 changed files with 225 additions and 143 deletions

View File

@@ -0,0 +1,62 @@
/*
* 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.test.context;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link FilteredClassLoader}.
*
* @author Phillip Webb
*/
public class FilteredClassLoaderTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@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();
}
@Test
public void loadClassWhenFilteredOnClassShouldThrowClassNotFound() throws Exception {
FilteredClassLoader classLoader = new FilteredClassLoader(
FilteredClassLoaderTests.class);
this.thrown.expect(ClassNotFoundException.class);
classLoader.loadClass(getClass().getName());
classLoader.close();
}
@Test
public void loadClassWhenNotFilteredShouldLoadClass() throws Exception {
FilteredClassLoader classLoader = new FilteredClassLoader((className) -> false);
Class<?> loaded = classLoader.loadClass(getClass().getName());
assertThat(loaded.getName()).isEqualTo(getClass().getName());
classLoader.close();
}
}

View File

@@ -25,7 +25,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
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.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -34,7 +34,7 @@ import org.springframework.core.env.Environment;
import org.springframework.util.ClassUtils;
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.
@@ -148,8 +148,7 @@ public abstract class AbstractApplicationContextRunnerTests<T extends AbstractAp
@Test
public void runWithClassLoaderShouldSetClassLoader() throws Exception {
get().withClassLoader(
new HidePackagesClassLoader(Gson.class.getPackage().getName()))
get().withClassLoader(new FilteredClassLoader(Gson.class.getPackage().getName()))
.run((context) -> {
try {
ClassUtils.forName(Gson.class.getName(),