Browse Source
Add `FilteredClassLoader` to replace `HideClassesClassLoader` and `HidePackagesClassLoader`. Fixes gh-10303pull/11066/merge
16 changed files with 225 additions and 143 deletions
@ -0,0 +1,126 @@
@@ -0,0 +1,126 @@
|
||||
/* |
||||
* 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 java.net.URL; |
||||
import java.net.URLClassLoader; |
||||
import java.util.function.Predicate; |
||||
|
||||
/** |
||||
* Test {@link URLClassLoader} that can filter the classes it can load. |
||||
* |
||||
* @author Andy Wilkinson |
||||
* @author Stephane Nicoll |
||||
* @author Phillip Webb |
||||
* @since 2.0.0 |
||||
*/ |
||||
public class FilteredClassLoader extends URLClassLoader { |
||||
|
||||
private final Predicate<String>[] filters; |
||||
|
||||
/** |
||||
* 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 |
||||
*/ |
||||
public FilteredClassLoader(String... hiddenPackages) { |
||||
this(PackageFilter.of(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 |
||||
protected Class<?> loadClass(String name, boolean resolve) |
||||
throws ClassNotFoundException { |
||||
for (Predicate<String> filter : this.filters) { |
||||
if (filter.test(name)) { |
||||
throw new ClassNotFoundException(); |
||||
} |
||||
} |
||||
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); |
||||
} |
||||
|
||||
} |
||||
} |
||||
@ -1,48 +0,0 @@
@@ -1,48 +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.test.context; |
||||
|
||||
import java.net.URL; |
||||
import java.net.URLClassLoader; |
||||
|
||||
/** |
||||
* Test {@link URLClassLoader} that hides configurable classes. |
||||
* |
||||
* @author Stephane Nicoll |
||||
* @since 2.0.0 |
||||
*/ |
||||
public class HideClassesClassLoader extends URLClassLoader { |
||||
|
||||
private final Class<?>[] hiddenClasses; |
||||
|
||||
public HideClassesClassLoader(Class<?>... hiddenClasses) { |
||||
super(new URL[0], HideClassesClassLoader.class.getClassLoader()); |
||||
this.hiddenClasses = hiddenClasses; |
||||
} |
||||
|
||||
@Override |
||||
protected Class<?> loadClass(String name, boolean resolve) |
||||
throws ClassNotFoundException { |
||||
for (Class<?> hiddenClass : this.hiddenClasses) { |
||||
if (name.equals(hiddenClass.getName())) { |
||||
throw new ClassNotFoundException(); |
||||
} |
||||
} |
||||
return super.loadClass(name, resolve); |
||||
} |
||||
|
||||
} |
||||
@ -1,54 +0,0 @@
@@ -1,54 +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.test.context; |
||||
|
||||
import java.net.URL; |
||||
import java.net.URLClassLoader; |
||||
|
||||
/** |
||||
* Test {@link URLClassLoader} that hides configurable packages. No class in one of those |
||||
* packages or sub-packages are visible. |
||||
* |
||||
* @author Andy Wilkinson |
||||
* @author Stephane Nicoll |
||||
* @since 2.0.0 |
||||
*/ |
||||
public final class HidePackagesClassLoader extends URLClassLoader { |
||||
|
||||
private final String[] hiddenPackages; |
||||
|
||||
/** |
||||
* Create a new instance with the packages to hide. |
||||
* @param hiddenPackages the packages to hide |
||||
*/ |
||||
public HidePackagesClassLoader(String... hiddenPackages) { |
||||
super(new URL[0], HidePackagesClassLoader.class.getClassLoader()); |
||||
this.hiddenPackages = hiddenPackages; |
||||
} |
||||
|
||||
@Override |
||||
protected Class<?> loadClass(String name, boolean resolve) |
||||
throws ClassNotFoundException { |
||||
for (String hiddenPackage : this.hiddenPackages) { |
||||
if (name.startsWith(hiddenPackage)) { |
||||
throw new ClassNotFoundException(); |
||||
} |
||||
} |
||||
return super.loadClass(name, resolve); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,62 @@
@@ -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(); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue