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
2c856124
Commit
2c856124
authored
Nov 21, 2016
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish ModifiedClassPathRunner
parent
f4b119cb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
23 deletions
+30
-23
ModifiedClassPathRunner.java
.../boot/junit/runner/classpath/ModifiedClassPathRunner.java
+30
-23
No files found.
spring-boot-junit-runners/src/main/java/org/springframework/boot/junit/runner/classpath/ModifiedClassPathRunner.java
View file @
2c856124
...
...
@@ -72,7 +72,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
protected
TestClass
createTestClass
(
Class
<?>
testClass
)
{
try
{
ClassLoader
classLoader
=
createTestClassLoader
(
testClass
);
return
new
Filtered
TestClass
(
classLoader
,
testClass
.
getName
());
return
new
ModifiedClassPath
TestClass
(
classLoader
,
testClass
.
getName
());
}
catch
(
Exception
ex
)
{
throw
new
IllegalStateException
(
ex
);
...
...
@@ -81,9 +81,9 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
@Override
protected
Object
createTest
()
throws
Exception
{
FilteredTestClass
testClass
=
(
Filtered
TestClass
)
getTestClass
();
return
testClass
.
doWith
Filtere
dContextClassLoader
(
new
FilteredTestClass
.
Filtered
TcclAction
<
Object
,
Exception
>()
{
ModifiedClassPathTestClass
testClass
=
(
ModifiedClassPath
TestClass
)
getTestClass
();
return
testClass
.
doWith
ModifiedClassPathThrea
dContextClassLoader
(
new
ModifiedClassPathTestClass
.
ModifiedClassPath
TcclAction
<
Object
,
Exception
>()
{
@Override
public
Object
perform
()
throws
Exception
{
...
...
@@ -94,8 +94,9 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
private
URLClassLoader
createTestClassLoader
(
Class
<?>
testClass
)
throws
Exception
{
URLClassLoader
classLoader
=
(
URLClassLoader
)
this
.
getClass
().
getClassLoader
();
return
new
FilteredClassLoader
(
processUrls
(
extractUrls
(
classLoader
),
testClass
),
classLoader
.
getParent
(),
classLoader
);
return
new
ModifiedClassPathClassLoader
(
processUrls
(
extractUrls
(
classLoader
),
testClass
),
classLoader
.
getParent
(),
classLoader
);
}
private
URL
[]
extractUrls
(
URLClassLoader
classLoader
)
throws
Exception
{
...
...
@@ -222,13 +223,13 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
}
/**
*
Filtered version of JUnit's {@link TestClass}
.
*
Custom {@link TestClass} that uses a modified class path
.
*/
private
static
final
class
Filtered
TestClass
extends
TestClass
{
private
static
final
class
ModifiedClassPath
TestClass
extends
TestClass
{
private
final
ClassLoader
classLoader
;
Filtered
TestClass
(
ClassLoader
classLoader
,
String
testClassName
)
ModifiedClassPath
TestClass
(
ClassLoader
classLoader
,
String
testClassName
)
throws
ClassNotFoundException
{
super
(
classLoader
.
loadClass
(
testClassName
));
this
.
classLoader
=
classLoader
;
...
...
@@ -259,13 +260,14 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
List
<
FrameworkMethod
>
wrapped
=
new
ArrayList
<
FrameworkMethod
>(
methods
.
size
());
for
(
FrameworkMethod
frameworkMethod
:
methods
)
{
wrapped
.
add
(
new
FilteredFrameworkMethod
(
frameworkMethod
.
getMethod
()));
wrapped
.
add
(
new
ModifiedClassPathFrameworkMethod
(
frameworkMethod
.
getMethod
()));
}
return
wrapped
;
}
private
<
T
,
E
extends
Throwable
>
T
doWith
Filtere
dContextClassLoader
(
Filtered
TcclAction
<
T
,
E
>
action
)
throws
E
{
private
<
T
,
E
extends
Throwable
>
T
doWith
ModifiedClassPathThrea
dContextClassLoader
(
ModifiedClassPath
TcclAction
<
T
,
E
>
action
)
throws
E
{
ClassLoader
originalClassLoader
=
Thread
.
currentThread
()
.
getContextClassLoader
();
Thread
.
currentThread
().
setContextClassLoader
(
this
.
classLoader
);
...
...
@@ -278,33 +280,34 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
}
/**
* An action to be performed with the {@link
FilteredClassLoader} set as the
* thread context class loader.
* An action to be performed with the {@link
ModifiedClassPathClassLoader} set as
* th
e th
read context class loader.
*/
private
interface
Filtered
TcclAction
<
T
,
E
extends
Throwable
>
{
private
interface
ModifiedClassPath
TcclAction
<
T
,
E
extends
Throwable
>
{
T
perform
()
throws
E
;
}
/**
* Filtered version of JUnit's {@link FrameworkMethod}.
* Custom {@link FrameworkMethod} that runs methods with
* {@link ModifiedClassPathClassLoader} as the thread context class loader.
*/
private
final
class
Filtered
FrameworkMethod
extends
FrameworkMethod
{
private
final
class
ModifiedClassPath
FrameworkMethod
extends
FrameworkMethod
{
private
Filtered
FrameworkMethod
(
Method
method
)
{
private
ModifiedClassPath
FrameworkMethod
(
Method
method
)
{
super
(
method
);
}
@Override
public
Object
invokeExplosively
(
final
Object
target
,
final
Object
...
params
)
throws
Throwable
{
return
doWith
Filtere
dContextClassLoader
(
new
Filtered
TcclAction
<
Object
,
Throwable
>()
{
return
doWith
ModifiedClassPathThrea
dContextClassLoader
(
new
ModifiedClassPath
TcclAction
<
Object
,
Throwable
>()
{
@Override
public
Object
perform
()
throws
Throwable
{
return
Filtered
FrameworkMethod
.
super
.
invokeExplosively
(
return
ModifiedClassPath
FrameworkMethod
.
super
.
invokeExplosively
(
target
,
params
);
}
...
...
@@ -315,11 +318,15 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
}
private
static
final
class
FilteredClassLoader
extends
URLClassLoader
{
/**
* Custom {@link URLClassLoader} that modifies the class path.
*/
private
static
final
class
ModifiedClassPathClassLoader
extends
URLClassLoader
{
private
final
ClassLoader
junitLoader
;
FilteredClassLoader
(
URL
[]
urls
,
ClassLoader
parent
,
ClassLoader
junitLoader
)
{
ModifiedClassPathClassLoader
(
URL
[]
urls
,
ClassLoader
parent
,
ClassLoader
junitLoader
)
{
super
(
urls
,
parent
);
this
.
junitLoader
=
junitLoader
;
}
...
...
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