Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
d3ca1a7b
Commit
d3ca1a7b
authored
Oct 10, 2018
by
Roy Jacobs
Committed by
Stephane Nicoll
Oct 12, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow ClassPathResources to be filtered by FilteredClassLoader
See gh-14774
parent
29c0aa44
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
3 deletions
+80
-3
FilteredClassLoader.java
...pringframework/boot/test/context/FilteredClassLoader.java
+53
-3
FilteredClassLoaderTests.java
...framework/boot/test/context/FilteredClassLoaderTests.java
+26
-0
FilteredClassLoaderTestResource.txt
...ork/boot/test/context/FilteredClassLoaderTestResource.txt
+1
-0
No files found.
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/FilteredClassLoader.java
View file @
d3ca1a7b
...
...
@@ -20,12 +20,15 @@ import java.net.URL;
import
java.net.URLClassLoader
;
import
java.util.function.Predicate
;
import
org.springframework.core.io.ClassPathResource
;
/**
* Test {@link URLClassLoader} that can filter the classes it can load.
* Test {@link URLClassLoader} that can filter the classes
and resources
it can load.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Phillip Webb
* @author Roy Jacobs
* @since 2.0.0
*/
public
class
FilteredClassLoader
extends
URLClassLoader
{
...
...
@@ -48,10 +51,19 @@ public class FilteredClassLoader extends URLClassLoader {
this
(
PackageFilter
.
of
(
hiddenPackages
));
}
/**
* Create a {@link FilteredClassLoader} that hides resources from the given packages.
* @param hiddenResources the resources to hide
*/
public
FilteredClassLoader
(
ClassPathResource
...
hiddenResources
)
{
this
(
ClassPathResourceFilter
.
of
(
hiddenResources
));
}
/**
* 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.
* @param filters a set of filters to determine when a class name or resource should
* be hidden. A {@link Predicate#test(Object) result} of {@code true} indicates a
* filtered class or resource.
*/
@SafeVarargs
public
FilteredClassLoader
(
Predicate
<
String
>...
filters
)
{
...
...
@@ -70,6 +82,16 @@ public class FilteredClassLoader extends URLClassLoader {
return
super
.
loadClass
(
name
,
resolve
);
}
@Override
public
URL
getResource
(
String
name
)
{
for
(
Predicate
<
String
>
filter
:
this
.
filters
)
{
if
(
filter
.
test
(
name
))
{
return
null
;
}
}
return
super
.
getResource
(
name
);
}
/**
* Filter to restrict the classes that can be loaded.
*/
...
...
@@ -124,4 +146,32 @@ public class FilteredClassLoader extends URLClassLoader {
}
/**
* Filter to restrict the resources that can be loaded.
*/
public
static
final
class
ClassPathResourceFilter
implements
Predicate
<
String
>
{
private
final
ClassPathResource
[]
hiddenResources
;
private
ClassPathResourceFilter
(
ClassPathResource
[]
hiddenResources
)
{
this
.
hiddenResources
=
hiddenResources
;
}
@Override
public
boolean
test
(
String
resourceName
)
{
for
(
ClassPathResource
hiddenResource
:
this
.
hiddenResources
)
{
if
(
hiddenResource
.
getFilename
()
!=
null
&&
resourceName
.
equals
(
hiddenResource
.
getPath
()))
{
return
true
;
}
}
return
false
;
}
public
static
ClassPathResourceFilter
of
(
ClassPathResource
...
hiddenResources
)
{
return
new
ClassPathResourceFilter
(
hiddenResources
);
}
}
}
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/FilteredClassLoaderTests.java
View file @
d3ca1a7b
...
...
@@ -16,8 +16,12 @@
package
org
.
springframework
.
boot
.
test
.
context
;
import
java.net.URL
;
import
org.junit.Test
;
import
org.springframework.core.io.ClassPathResource
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatExceptionOfType
;
...
...
@@ -25,9 +29,13 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* Tests for {@link FilteredClassLoader}.
*
* @author Phillip Webb
* @author Roy Jacobs
*/
public
class
FilteredClassLoaderTests
{
private
static
ClassPathResource
TEST_RESOURCE
=
new
ClassPathResource
(
"org/springframework/boot/test/context/FilteredClassLoaderTestResource.txt"
);
@Test
public
void
loadClassWhenFilteredOnPackageShouldThrowClassNotFound
()
throws
Exception
{
...
...
@@ -55,4 +63,22 @@ public class FilteredClassLoaderTests {
classLoader
.
close
();
}
@Test
public
void
loadResourceWhenFilteredOnResourceShouldReturnNotFound
()
throws
Exception
{
try
(
FilteredClassLoader
classLoader
=
new
FilteredClassLoader
(
TEST_RESOURCE
))
{
final
URL
loaded
=
classLoader
.
getResource
(
TEST_RESOURCE
.
getPath
());
assertThat
(
loaded
).
isNull
();
}
}
@Test
public
void
loadResourceWhenNotFilteredShouldLoadResource
()
throws
Exception
{
try
(
FilteredClassLoader
classLoader
=
new
FilteredClassLoader
(
(
resourceName
)
->
false
))
{
final
URL
loaded
=
classLoader
.
getResource
(
TEST_RESOURCE
.
getPath
());
assertThat
(
loaded
).
isNotNull
();
}
}
}
spring-boot-project/spring-boot-test/src/test/resources/org/springframework/boot/test/context/FilteredClassLoaderTestResource.txt
0 → 100644
View file @
d3ca1a7b
Used by FilteredClassLoaderTests.java
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment