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
27267a70
Commit
27267a70
authored
Jun 01, 2018
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure that condition evaluator uses runner’s class loader
Closes gh-13319
parent
152ce145
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
9 deletions
+54
-9
AbstractApplicationContextRunner.java
...test/context/runner/AbstractApplicationContextRunner.java
+26
-8
AbstractApplicationContextRunnerTests.java
...context/runner/AbstractApplicationContextRunnerTests.java
+28
-1
No files found.
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java
View file @
27267a70
...
...
@@ -196,9 +196,8 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
}
/**
* Customize the {@link ClassLoader} that the {@link ApplicationContext} should use.
* Customizing the {@link ClassLoader} is an effective manner to hide resources from
* the classpath.
* Customize the {@link ClassLoader} that the {@link ApplicationContext} should use
* for resource loading and bean class loading.
* @param classLoader the classloader to use (can be null to use the default)
* @return a new instance with the updated class loader
* @see FilteredClassLoader
...
...
@@ -274,15 +273,34 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
*/
@SuppressWarnings
(
"unchecked"
)
public
SELF
run
(
ContextConsumer
<?
super
A
>
consumer
)
{
this
.
systemProperties
.
applyToSystemProperties
(()
->
{
try
(
A
context
=
createAssertableContext
())
{
accept
(
consumer
,
context
);
}
return
null
;
withContextClassLoader
(
this
.
classLoader
,
()
->
{
this
.
systemProperties
.
applyToSystemProperties
(()
->
{
try
(
A
context
=
createAssertableContext
())
{
accept
(
consumer
,
context
);
}
return
null
;
});
});
return
(
SELF
)
this
;
}
private
void
withContextClassLoader
(
ClassLoader
classLoader
,
Runnable
action
)
{
if
(
classLoader
==
null
)
{
action
.
run
();
}
else
{
Thread
currentThread
=
Thread
.
currentThread
();
ClassLoader
previous
=
currentThread
.
getContextClassLoader
();
currentThread
.
setContextClassLoader
(
classLoader
);
try
{
action
.
run
();
}
finally
{
currentThread
.
setContextClassLoader
(
previous
);
}
}
}
@SuppressWarnings
(
"unchecked"
)
private
A
createAssertableContext
()
{
ResolvableType
resolvableType
=
ResolvableType
...
...
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java
View file @
27267a70
...
...
@@ -30,8 +30,12 @@ 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
;
import
org.springframework.context.annotation.Condition
;
import
org.springframework.context.annotation.ConditionContext
;
import
org.springframework.context.annotation.Conditional
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.env.Environment
;
import
org.springframework.core.type.AnnotatedTypeMetadata
;
import
org.springframework.util.ClassUtils
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
...
@@ -150,7 +154,7 @@ public abstract class AbstractApplicationContextRunnerTests<T extends AbstractAp
}
@Test
public
void
runWithClassLoaderShouldSetClassLoader
()
{
public
void
runWithClassLoaderShouldSetClassLoader
OnContext
()
{
get
().
withClassLoader
(
new
FilteredClassLoader
(
Gson
.
class
.
getPackage
().
getName
()))
.
run
((
context
)
->
{
try
{
...
...
@@ -164,6 +168,14 @@ public abstract class AbstractApplicationContextRunnerTests<T extends AbstractAp
});
}
@Test
public
void
runWithClassLoaderShouldSetClassLoaderOnConditionContext
()
{
get
().
withClassLoader
(
new
FilteredClassLoader
(
Gson
.
class
.
getPackage
().
getName
()))
.
withUserConfiguration
(
ConditionalConfig
.
class
)
.
run
((
context
)
->
assertThat
(
context
)
.
hasSingleBean
(
ConditionalConfig
.
class
));
}
@Test
public
void
thrownRuleWorksWithCheckedException
()
{
get
().
run
((
context
)
->
{
...
...
@@ -209,4 +221,19 @@ public abstract class AbstractApplicationContextRunnerTests<T extends AbstractAp
}
@Configuration
@Conditional
(
FilteredClassLoaderCondition
.
class
)
static
class
ConditionalConfig
{
}
static
class
FilteredClassLoaderCondition
implements
Condition
{
@Override
public
boolean
matches
(
ConditionContext
context
,
AnnotatedTypeMetadata
metadata
)
{
return
context
.
getClassLoader
()
instanceof
FilteredClassLoader
;
}
}
}
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