Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
52022501
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
52022501
编写于
9月 01, 2016
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish SpringClassRule and SpringMethodRule
上级
749ee606
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
55 addition
and
56 deletion
+55
-56
spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringClassRule.java
...gframework/test/context/junit4/rules/SpringClassRule.java
+24
-26
spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java
...framework/test/context/junit4/rules/SpringMethodRule.java
+31
-30
未找到文件。
spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringClassRule.java
浏览文件 @
52022501
...
...
@@ -18,7 +18,9 @@ package org.springframework.test.context.junit4.rules;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Modifier
;
import
java.util.Arrays
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.concurrent.ConcurrentHashMap
;
import
org.apache.commons.logging.Log
;
...
...
@@ -57,7 +59,7 @@ import org.springframework.util.ClassUtils;
* <pre><code> public class ExampleSpringIntegrationTest {
*
* @ClassRule
* public static final SpringClassRule
SPRING_CLASS_RULE
= new SpringClassRule();
* public static final SpringClassRule
springClassRule
= new SpringClassRule();
*
* @Rule
* public final SpringMethodRule springMethodRule = new SpringMethodRule();
...
...
@@ -144,35 +146,35 @@ public class SpringClassRule implements TestRule {
}
/**
* Wrap the supplied {@
code s
tatement} with a {@code RunBeforeTestClassCallbacks} statement.
* Wrap the supplied {@
link S
tatement} with a {@code RunBeforeTestClassCallbacks} statement.
* @see RunBeforeTestClassCallbacks
*/
private
Statement
withBeforeTestClassCallbacks
(
Statement
statemen
t
,
TestContextManager
testContextManager
)
{
return
new
RunBeforeTestClassCallbacks
(
statemen
t
,
testContextManager
);
private
Statement
withBeforeTestClassCallbacks
(
Statement
nex
t
,
TestContextManager
testContextManager
)
{
return
new
RunBeforeTestClassCallbacks
(
nex
t
,
testContextManager
);
}
/**
* Wrap the supplied {@
code s
tatement} with a {@code RunAfterTestClassCallbacks} statement.
* Wrap the supplied {@
link S
tatement} with a {@code RunAfterTestClassCallbacks} statement.
* @see RunAfterTestClassCallbacks
*/
private
Statement
withAfterTestClassCallbacks
(
Statement
statemen
t
,
TestContextManager
testContextManager
)
{
return
new
RunAfterTestClassCallbacks
(
statemen
t
,
testContextManager
);
private
Statement
withAfterTestClassCallbacks
(
Statement
nex
t
,
TestContextManager
testContextManager
)
{
return
new
RunAfterTestClassCallbacks
(
nex
t
,
testContextManager
);
}
/**
* Wrap the supplied {@
code s
tatement} with a {@code ProfileValueChecker} statement.
* Wrap the supplied {@
link S
tatement} with a {@code ProfileValueChecker} statement.
* @see ProfileValueChecker
*/
private
Statement
withProfileValueCheck
(
Statement
statemen
t
,
Class
<?>
testClass
)
{
return
new
ProfileValueChecker
(
statemen
t
,
testClass
,
null
);
private
Statement
withProfileValueCheck
(
Statement
nex
t
,
Class
<?>
testClass
)
{
return
new
ProfileValueChecker
(
nex
t
,
testClass
,
null
);
}
/**
* Wrap the supplied {@
code s
tatement} with a {@code TestContextManagerCacheEvictor} statement.
* Wrap the supplied {@
link S
tatement} with a {@code TestContextManagerCacheEvictor} statement.
* @see TestContextManagerCacheEvictor
*/
private
Statement
withTestContextManagerCacheEviction
(
Statement
statemen
t
,
Class
<?>
testClass
)
{
return
new
TestContextManagerCacheEvictor
(
statemen
t
,
testClass
);
private
Statement
withTestContextManagerCacheEviction
(
Statement
nex
t
,
Class
<?>
testClass
)
{
return
new
TestContextManagerCacheEvictor
(
nex
t
,
testClass
);
}
...
...
@@ -182,26 +184,22 @@ public class SpringClassRule implements TestRule {
* annotated with {@code @Rule}.
*/
private
static
void
validateSpringMethodRuleConfiguration
(
Class
<?>
testClass
)
{
Field
ruleField
=
findSpringMethodRuleField
(
testClass
);
Assert
.
state
(
ruleField
!=
null
,
()
->
String
.
format
(
Field
ruleField
=
findSpringMethodRuleField
(
testClass
).
orElseThrow
(()
->
new
IllegalStateException
(
String
.
format
(
"Failed to find 'public SpringMethodRule' field in test class [%s]. "
+
"Consult the javadoc for SpringClassRule for details."
,
testClass
.
getName
()));
"Consult the javadoc for SpringClassRule for details."
,
testClass
.
getName
()))
)
;
Assert
.
state
(
ruleField
.
isAnnotationPresent
(
Rule
.
class
),
()
->
String
.
format
(
"SpringMethodRule field [%s] must be annotated with JUnit's @Rule annotation. "
+
"Consult the javadoc for SpringClassRule for details."
,
ruleField
));
}
private
static
Field
findSpringMethodRuleField
(
Class
<?>
testClass
)
{
for
(
Field
field
:
testClass
.
getFields
())
{
int
modifiers
=
field
.
getModifiers
();
if
(!
Modifier
.
isStatic
(
modifiers
)
&&
Modifier
.
isPublic
(
modifiers
)
&&
SpringMethodRule
.
class
.
isAssignableFrom
(
field
.
getType
()))
{
return
field
;
}
}
return
null
;
private
static
Optional
<
Field
>
findSpringMethodRuleField
(
Class
<?>
testClass
)
{
return
Arrays
.
stream
(
testClass
.
getFields
())
.
filter
(
field
->
!
Modifier
.
isStatic
(
field
.
getModifiers
()))
.
filter
(
field
->
Modifier
.
isPublic
(
field
.
getModifiers
()))
.
filter
(
field
->
SpringMethodRule
.
class
.
isAssignableFrom
(
field
.
getType
()))
.
findFirst
();
}
/**
...
...
spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java
浏览文件 @
52022501
...
...
@@ -17,6 +17,9 @@
package
org.springframework.test.context.junit4.rules
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
import
java.util.Arrays
;
import
java.util.Optional
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
...
...
@@ -136,20 +139,21 @@ public class SpringMethodRule implements MethodRule {
*/
@Override
public
Statement
apply
(
Statement
base
,
FrameworkMethod
frameworkMethod
,
Object
testInstance
)
{
Method
testMethod
=
frameworkMethod
.
getMethod
();
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
"Applying SpringMethodRule to test method ["
+
frameworkMethod
.
getMethod
()
+
"]"
);
logger
.
debug
(
"Applying SpringMethodRule to test method ["
+
testMethod
+
"]"
);
}
Class
<?>
testClass
=
testInstance
.
getClass
();
validateSpringClassRuleConfiguration
(
testClass
);
TestContextManager
testContextManager
=
SpringClassRule
.
getTestContextManager
(
testClass
);
Statement
statement
=
base
;
statement
=
withBeforeTestMethodCallbacks
(
statement
,
framework
Method
,
testInstance
,
testContextManager
);
statement
=
withAfterTestMethodCallbacks
(
statement
,
framework
Method
,
testInstance
,
testContextManager
);
statement
=
withBeforeTestMethodCallbacks
(
statement
,
test
Method
,
testInstance
,
testContextManager
);
statement
=
withAfterTestMethodCallbacks
(
statement
,
test
Method
,
testInstance
,
testContextManager
);
statement
=
withTestInstancePreparation
(
statement
,
testInstance
,
testContextManager
);
statement
=
withPotentialRepeat
(
statement
,
framework
Method
,
testInstance
);
statement
=
withPotentialTimeout
(
statement
,
framework
Method
,
testInstance
);
statement
=
withProfileValueCheck
(
statement
,
framework
Method
,
testInstance
);
statement
=
withPotentialRepeat
(
statement
,
test
Method
,
testInstance
);
statement
=
withPotentialTimeout
(
statement
,
test
Method
,
testInstance
);
statement
=
withProfileValueCheck
(
statement
,
test
Method
,
testInstance
);
return
statement
;
}
...
...
@@ -157,32 +161,32 @@ public class SpringMethodRule implements MethodRule {
* Wrap the supplied {@link Statement} with a {@code RunBeforeTestMethodCallbacks} statement.
* @see RunBeforeTestMethodCallbacks
*/
private
Statement
withBeforeTestMethodCallbacks
(
Statement
statement
,
FrameworkMethod
framework
Method
,
private
Statement
withBeforeTestMethodCallbacks
(
Statement
next
,
Method
test
Method
,
Object
testInstance
,
TestContextManager
testContextManager
)
{
return
new
RunBeforeTestMethodCallbacks
(
statement
,
testInstance
,
frameworkMethod
.
getMethod
()
,
testContextManager
);
next
,
testInstance
,
testMethod
,
testContextManager
);
}
/**
* Wrap the supplied {@link Statement} with a {@code RunAfterTestMethodCallbacks} statement.
* @see RunAfterTestMethodCallbacks
*/
private
Statement
withAfterTestMethodCallbacks
(
Statement
statement
,
FrameworkMethod
framework
Method
,
private
Statement
withAfterTestMethodCallbacks
(
Statement
next
,
Method
test
Method
,
Object
testInstance
,
TestContextManager
testContextManager
)
{
return
new
RunAfterTestMethodCallbacks
(
statement
,
testInstance
,
frameworkMethod
.
getMethod
()
,
testContextManager
);
next
,
testInstance
,
testMethod
,
testContextManager
);
}
/**
* Wrap the supplied {@link Statement} with a {@code RunPrepareTestInstanceCallbacks} statement.
* @see RunPrepareTestInstanceCallbacks
*/
private
Statement
withTestInstancePreparation
(
Statement
statemen
t
,
Object
testInstance
,
private
Statement
withTestInstancePreparation
(
Statement
nex
t
,
Object
testInstance
,
TestContextManager
testContextManager
)
{
return
new
RunPrepareTestInstanceCallbacks
(
statemen
t
,
testInstance
,
testContextManager
);
return
new
RunPrepareTestInstanceCallbacks
(
nex
t
,
testInstance
,
testContextManager
);
}
/**
...
...
@@ -191,8 +195,8 @@ public class SpringMethodRule implements MethodRule {
* annotation.
* @see SpringRepeat
*/
private
Statement
withPotentialRepeat
(
Statement
next
,
FrameworkMethod
framework
Method
,
Object
testInstance
)
{
return
new
SpringRepeat
(
next
,
frameworkMethod
.
getMethod
()
);
private
Statement
withPotentialRepeat
(
Statement
next
,
Method
test
Method
,
Object
testInstance
)
{
return
new
SpringRepeat
(
next
,
testMethod
);
}
/**
...
...
@@ -201,16 +205,16 @@ public class SpringMethodRule implements MethodRule {
* annotation.
* @see SpringFailOnTimeout
*/
private
Statement
withPotentialTimeout
(
Statement
next
,
FrameworkMethod
framework
Method
,
Object
testInstance
)
{
return
new
SpringFailOnTimeout
(
next
,
frameworkMethod
.
getMethod
()
);
private
Statement
withPotentialTimeout
(
Statement
next
,
Method
test
Method
,
Object
testInstance
)
{
return
new
SpringFailOnTimeout
(
next
,
testMethod
);
}
/**
* Wrap the supplied {@link Statement} with a {@code ProfileValueChecker} statement.
* @see ProfileValueChecker
*/
private
Statement
withProfileValueCheck
(
Statement
statement
,
FrameworkMethod
framework
Method
,
Object
testInstance
)
{
return
new
ProfileValueChecker
(
statement
,
testInstance
.
getClass
(),
frameworkMethod
.
getMethod
()
);
private
Statement
withProfileValueCheck
(
Statement
next
,
Method
test
Method
,
Object
testInstance
)
{
return
new
ProfileValueChecker
(
next
,
testInstance
.
getClass
(),
testMethod
);
}
...
...
@@ -220,11 +224,10 @@ public class SpringMethodRule implements MethodRule {
* that is annotated with {@code @ClassRule}.
*/
private
static
SpringClassRule
validateSpringClassRuleConfiguration
(
Class
<?>
testClass
)
{
Field
ruleField
=
findSpringClassRuleField
(
testClass
);
Assert
.
state
(
ruleField
!=
null
,
()
->
String
.
format
(
"Failed to find 'public static final SpringClassRule' field in test class [%s]. "
+
"Consult the javadoc for SpringClassRule for details."
,
testClass
.
getName
()));
Field
ruleField
=
findSpringClassRuleField
(
testClass
).
orElseThrow
(()
->
new
IllegalStateException
(
String
.
format
(
"Failed to find 'public static final SpringClassRule' field in test class [%s]. "
+
"Consult the javadoc for SpringClassRule for details."
,
testClass
.
getName
())));
Assert
.
state
(
ruleField
.
isAnnotationPresent
(
ClassRule
.
class
),
()
->
String
.
format
(
"SpringClassRule field [%s] must be annotated with JUnit's @ClassRule annotation. "
+
...
...
@@ -233,13 +236,11 @@ public class SpringMethodRule implements MethodRule {
return
(
SpringClassRule
)
ReflectionUtils
.
getField
(
ruleField
,
null
);
}
private
static
Field
findSpringClassRuleField
(
Class
<?>
testClass
)
{
for
(
Field
field
:
testClass
.
getFields
())
{
if
(
ReflectionUtils
.
isPublicStaticFinal
(
field
)
&&
SpringClassRule
.
class
.
isAssignableFrom
(
field
.
getType
()))
{
return
field
;
}
}
return
null
;
private
static
Optional
<
Field
>
findSpringClassRuleField
(
Class
<?>
testClass
)
{
return
Arrays
.
stream
(
testClass
.
getFields
())
.
filter
(
ReflectionUtils:
:
isPublicStaticFinal
)
.
filter
(
field
->
SpringClassRule
.
class
.
isAssignableFrom
(
field
.
getType
()))
.
findFirst
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录