Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dk131072
spring-framework
提交
3f58da1c
S
spring-framework
项目概览
dk131072
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
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,发现更多精彩内容 >>
提交
3f58da1c
编写于
6月 03, 2011
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[SPR-7326] Added unit tests to verify proper semantics of TestContext's cache key generation.
上级
72420c79
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
140 addition
and
8 deletion
+140
-8
org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java
.../org/springframework/test/context/ContextLoaderUtils.java
+7
-4
org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java
...in/java/org/springframework/test/context/TestContext.java
+12
-0
org.springframework.test/src/test/java/org/springframework/test/context/SpringRunnerContextCacheTests.java
...framework/test/context/SpringRunnerContextCacheTests.java
+19
-4
org.springframework.test/src/test/java/org/springframework/test/context/TestContextCacheKeyTests.java
...pringframework/test/context/TestContextCacheKeyTests.java
+102
-0
未找到文件。
org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java
浏览文件 @
3f58da1c
...
...
@@ -18,9 +18,9 @@ package org.springframework.test.context;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.SortedSet
;
import
java.util.TreeSet
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
...
...
@@ -243,13 +243,16 @@ abstract class ContextLoaderUtils {
annotationType
,
clazz
));
}
final
Set
<
String
>
activeProfiles
=
new
LinkedHashSet
<
String
>();
// Active profiles must be sorted due to cache key generation in
// TestContext. Specifically, profile sets {foo,bar} and {bar,foo}
// must both result in the same array (e.g., [bar,foo]).
final
SortedSet
<
String
>
activeProfiles
=
new
TreeSet
<
String
>();
while
(
declaringClass
!=
null
)
{
ActiveProfiles
annotation
=
declaringClass
.
getAnnotation
(
annotationType
);
if
(
logger
.
isTraceEnabled
())
{
logger
.
trace
(
String
.
format
(
"Retrieved @ActiveProfiles [%s] for declaring class [%s]."
,
a
ctiveProfiles
,
logger
.
trace
(
String
.
format
(
"Retrieved @ActiveProfiles [%s] for declaring class [%s]."
,
a
nnotation
,
declaringClass
));
}
...
...
org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java
浏览文件 @
3f58da1c
...
...
@@ -170,12 +170,24 @@ public class TestContext extends AttributeAccessorSupport {
if
(
context
==
null
)
{
try
{
context
=
loadApplicationContext
();
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
String
.
format
(
"Storing ApplicationContext for test class [%s] in cache under key [%s]."
,
testClass
,
contextKey
));
}
contextCache
.
put
(
contextKey
,
context
);
}
catch
(
Exception
ex
)
{
throw
new
IllegalStateException
(
"Failed to load ApplicationContext"
,
ex
);
}
}
else
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
String
.
format
(
"Retrieved ApplicationContext for test class [%s] from cache with key [%s]."
,
testClass
,
contextKey
));
}
}
return
context
;
}
}
...
...
org.springframework.test/src/test/java/org/springframework/test/context/SpringRunnerContextCacheTests.java
浏览文件 @
3f58da1c
/*
* Copyright 2002-20
09
the original author or authors.
* Copyright 2002-20
11
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -39,6 +39,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Sam Brannen
* @author Juergen Hoeller
* @since 2.5
* @see TestContextCacheKeyTests
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@ContextConfiguration
(
"/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml"
)
...
...
@@ -50,18 +51,32 @@ public class SpringRunnerContextCacheTests {
protected
ApplicationContext
applicationContext
;
/**
* Asserts the statistics of the context cache in {@link TestContextManager}.
*
* @param usageScenario the scenario in which the statistics are used
* @param expectedSize the expected number of contexts in the cache
* @param expectedHitCount the expected hit count
* @param expectedMissCount the expected miss count
*/
private
static
final
void
assertContextCacheStatistics
(
String
usageScenario
,
int
expectedSize
,
int
expectedHitCount
,
int
expectedMissCount
)
{
assertContextCacheStatistics
(
TestContextManager
.
contextCache
,
usageScenario
,
expectedSize
,
expectedHitCount
,
expectedMissCount
);
}
/**
* Asserts the statistics of the supplied context cache.
*
* @param contextCache the cache to assert against
* @param usageScenario the scenario in which the statistics are used
* @param expectedSize the expected number of contexts in the cache
* @param expectedHitCount the expected hit count
* @param expectedMissCount the expected miss count
*/
public
static
final
void
assertContextCacheStatistics
(
String
usageScenario
,
int
expectedSize
,
int
expectedHitCount
,
int
expectedMissCount
)
{
public
static
final
void
assertContextCacheStatistics
(
ContextCache
contextCache
,
String
usageScenario
,
int
expected
Size
,
int
expectedHitCount
,
int
expected
MissCount
)
{
ContextCache
contextCache
=
TestContextManager
.
contextCache
;
assertEquals
(
"Verifying number of contexts in cache ("
+
usageScenario
+
")."
,
expectedSize
,
contextCache
.
size
());
assertEquals
(
"Verifying number of cache hits ("
+
usageScenario
+
")."
,
expectedHitCount
,
...
...
org.springframework.test/src/test/java/org/springframework/test/context/TestContextCacheKeyTests.java
0 → 100644
浏览文件 @
3f58da1c
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.test.context
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
springframework
.
test
.
context
.
SpringRunnerContextCacheTests
.
assertContextCacheStatistics
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.test.context.support.AnnotationConfigContextLoader
;
/**
* Unit tests for verifying proper behavior of the {@link ContextCache} in
* conjunction with cache keys generated in {@link TestContext}.
*
* @author Sam Brannen
* @since 3.1
* @see SpringRunnerContextCacheTests
*/
public
class
TestContextCacheKeyTests
{
private
ContextCache
contextCache
=
new
ContextCache
();
@Before
public
void
initialCacheState
()
{
assertContextCacheStatistics
(
contextCache
,
"initial state"
,
0
,
0
,
0
);
}
private
void
loadAppCtxAndAssertCacheStats
(
Class
<?>
testClass
,
int
expectedSize
,
int
expectedHitCount
,
int
expectedMissCount
)
{
TestContext
testContext
=
new
TestContext
(
testClass
,
contextCache
);
ApplicationContext
context
=
testContext
.
getApplicationContext
();
assertNotNull
(
context
);
assertContextCacheStatistics
(
contextCache
,
testClass
.
getName
(),
expectedSize
,
expectedHitCount
,
expectedMissCount
);
}
@Test
public
void
verifyCacheKeyIsBasedOnContextLoader
()
{
loadAppCtxAndAssertCacheStats
(
AnnotationConfigContextLoaderTestCase
.
class
,
1
,
0
,
1
);
loadAppCtxAndAssertCacheStats
(
AnnotationConfigContextLoaderTestCase
.
class
,
1
,
1
,
1
);
loadAppCtxAndAssertCacheStats
(
CustomAnnotationConfigContextLoaderTestCase
.
class
,
2
,
1
,
2
);
loadAppCtxAndAssertCacheStats
(
CustomAnnotationConfigContextLoaderTestCase
.
class
,
2
,
2
,
2
);
loadAppCtxAndAssertCacheStats
(
AnnotationConfigContextLoaderTestCase
.
class
,
2
,
3
,
2
);
loadAppCtxAndAssertCacheStats
(
CustomAnnotationConfigContextLoaderTestCase
.
class
,
2
,
4
,
2
);
}
@Test
public
void
verifyCacheKeyIsBasedOnActiveProfiles
()
{
loadAppCtxAndAssertCacheStats
(
FooBarProfilesTestCase
.
class
,
1
,
0
,
1
);
loadAppCtxAndAssertCacheStats
(
FooBarProfilesTestCase
.
class
,
1
,
1
,
1
);
// Profiles {foo, bar} should hash to the same as {bar,foo}
loadAppCtxAndAssertCacheStats
(
BarFooProfilesTestCase
.
class
,
1
,
2
,
1
);
loadAppCtxAndAssertCacheStats
(
FooBarProfilesTestCase
.
class
,
1
,
3
,
1
);
loadAppCtxAndAssertCacheStats
(
FooBarProfilesTestCase
.
class
,
1
,
4
,
1
);
loadAppCtxAndAssertCacheStats
(
BarFooProfilesTestCase
.
class
,
1
,
5
,
1
);
}
@Configuration
static
class
Config
{
}
@ContextConfiguration
(
classes
=
Config
.
class
,
loader
=
AnnotationConfigContextLoader
.
class
)
private
static
class
AnnotationConfigContextLoaderTestCase
{
}
@ContextConfiguration
(
classes
=
Config
.
class
,
loader
=
CustomAnnotationConfigContextLoader
.
class
)
private
static
class
CustomAnnotationConfigContextLoaderTestCase
{
}
private
static
class
CustomAnnotationConfigContextLoader
extends
AnnotationConfigContextLoader
{
}
@ActiveProfiles
({
"foo"
,
"bar"
})
@ContextConfiguration
(
classes
=
Config
.
class
,
loader
=
AnnotationConfigContextLoader
.
class
)
private
static
class
FooBarProfilesTestCase
{
}
@ActiveProfiles
({
"bar"
,
"foo"
})
@ContextConfiguration
(
classes
=
Config
.
class
,
loader
=
AnnotationConfigContextLoader
.
class
)
private
static
class
BarFooProfilesTestCase
{
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录