Override missing methods in FilteredClassLoader
Closes gh-16404
This commit is contained in:
parent
23d237928b
commit
c592e71449
|
|
@ -16,11 +16,14 @@
|
|||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
|
@ -109,6 +112,26 @@ public class FilteredClassLoader extends URLClassLoader {
|
|||
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.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -16,7 +16,9 @@
|
|||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Enumeration;
|
||||
|
||||
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