Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
f30d33d3
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 搜索 >>
提交
f30d33d3
编写于
11月 26, 2012
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add spring-test-mvc tests with Spring HATEOAS links
Issue: SPR-9886
上级
6e3c3c58
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
87 addition
and
18 deletion
+87
-18
build.gradle
build.gradle
+1
-1
spring-test-mvc/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java
...springframework/test/util/JsonPathExpectationsHelper.java
+13
-2
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java
...ork/test/web/servlet/samples/context/JavaConfigTests.java
+2
-2
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java
...ples/standalone/resultmatchers/ContentAssertionTests.java
+69
-11
spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/servlet-context.xml
...work/test/web/servlet/samples/context/servlet-context.xml
+2
-2
未找到文件。
build.gradle
浏览文件 @
f30d33d3
...
...
@@ -595,7 +595,6 @@ project('spring-test-mvc') {
exclude
group:
'com.sun.jmx'
,
module:
'jmxri'
}
testCompile
"javax.servlet:jstl:1.2"
testCompile
"org.apache.tiles:tiles-jsp:2.2.2"
testCompile
"org.hibernate:hibernate-validator:4.2.0.Final"
testCompile
"org.codehaus.jackson:jackson-mapper-asl:1.4.2"
testCompile
project
(
":spring-oxm"
)
...
...
@@ -613,6 +612,7 @@ project('spring-test-mvc') {
testCompile
(
"org.springframework.security:spring-security-config:3.1.2.RELEASE"
)
{
exclude
group:
'org.springframework'
}
testCompile
(
"org.springframework.hateoas:spring-hateoas:0.3.0.RELEASE"
)
}
}
...
...
spring-test-mvc/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java
浏览文件 @
f30d33d3
...
...
@@ -17,7 +17,7 @@
package
org.springframework.test.util
;
import
static
org
.
springframework
.
test
.
util
.
AssertionErrors
.
assertEquals
;
import
static
org
.
springframework
.
test
.
util
.
AssertionErrors
.
assertTrue
;
import
static
org
.
springframework
.
test
.
util
.
AssertionErrors
.
*
;
import
static
org
.
springframework
.
test
.
util
.
MatcherAssertionErrors
.
assertThat
;
import
java.text.ParseException
;
...
...
@@ -85,6 +85,17 @@ public class JsonPathExpectationsHelper {
*/
public
void
assertValue
(
String
responseContent
,
Object
expectedValue
)
throws
ParseException
{
Object
actualValue
=
evaluateJsonPath
(
responseContent
);
if
((
actualValue
instanceof
List
)
&&
!(
expectedValue
instanceof
List
))
{
@SuppressWarnings
(
"rawtypes"
)
List
actualValueList
=
(
List
)
actualValue
;
if
(
actualValueList
.
size
()
==
0
)
{
fail
(
"No matching value for JSON path \""
+
this
.
expression
+
"\""
);
}
if
(
actualValueList
.
size
()
!=
1
)
{
fail
(
"Got a list of values "
+
actualValue
+
" instead of the value "
+
expectedValue
);
}
actualValue
=
actualValueList
.
get
(
0
);
}
assertEquals
(
"JSON path"
+
this
.
expression
,
expectedValue
,
actualValue
);
}
...
...
@@ -93,7 +104,7 @@ public class JsonPathExpectationsHelper {
*/
public
void
assertValueIsArray
(
String
responseContent
)
throws
ParseException
{
Object
actualValue
=
evaluateJsonPath
(
responseContent
);
assertTrue
(
"No value for JSON path
"
+
this
.
expression
,
actualValue
!=
null
);
assertTrue
(
"No value for JSON path
\""
+
this
.
expression
+
"\""
,
actualValue
!=
null
);
String
reason
=
"Expected array at JSON path "
+
this
.
expression
+
" but found "
+
actualValue
;
assertTrue
(
reason
,
actualValue
instanceof
List
);
}
...
...
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java
浏览文件 @
f30d33d3
...
...
@@ -39,8 +39,8 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import
org.springframework.web.servlet.config.annotation.ViewControllerRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
;
import
org.springframework.web.servlet.view.UrlBasedViewResolver
;
import
org.springframework.web.servlet.view.tiles
2
.TilesConfigurer
;
import
org.springframework.web.servlet.view.tiles
2
.TilesView
;
import
org.springframework.web.servlet.view.tiles
3
.TilesConfigurer
;
import
org.springframework.web.servlet.view.tiles
3
.TilesView
;
/**
* Tests with Java configuration.
...
...
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java
浏览文件 @
f30d33d3
...
...
@@ -18,12 +18,24 @@ package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.*;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.*;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultHandlers
.
print
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
content
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
jsonPath
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
xpath
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
setup
.
MockMvcBuilders
.
standaloneSetup
;
import
java.nio.charset.Charset
;
import
java.util.Collections
;
import
java.util.Map
;
import
javax.xml.bind.annotation.XmlRootElement
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.hateoas.Link
;
import
org.springframework.hateoas.ResourceSupport
;
import
org.springframework.http.MediaType
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.test.web.servlet.MockMvc
;
...
...
@@ -42,6 +54,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
*/
public
class
ContentAssertionTests
{
public
static
final
MediaType
TEXT_PLAIN_UTF8
=
new
MediaType
(
"text"
,
"plain"
,
Charset
.
forName
(
"UTF-8"
));
private
MockMvc
mockMvc
;
@Before
...
...
@@ -51,7 +65,7 @@ public class ContentAssertionTests {
@Test
public
void
testContentType
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/handle"
))
this
.
mockMvc
.
perform
(
get
(
"/handle"
)
.
accept
(
MediaType
.
TEXT_PLAIN
)
)
.
andExpect
(
content
().
contentType
(
MediaType
.
TEXT_PLAIN
))
.
andExpect
(
content
().
contentType
(
"text/plain"
));
...
...
@@ -62,29 +76,58 @@ public class ContentAssertionTests {
@Test
public
void
testContentAsString
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/handle"
)).
andExpect
(
content
().
string
(
"Hello world!"
));
this
.
mockMvc
.
perform
(
get
(
"/handleUtf8"
)).
andExpect
(
content
().
string
(
"\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"
));
this
.
mockMvc
.
perform
(
get
(
"/handle"
).
accept
(
MediaType
.
TEXT_PLAIN
))
.
andExpect
(
content
().
string
(
"Hello world!"
));
this
.
mockMvc
.
perform
(
get
(
"/handleUtf8"
))
.
andExpect
(
content
().
string
(
"\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"
));
// Hamcrest matchers...
this
.
mockMvc
.
perform
(
get
(
"/handle"
)).
andExpect
(
content
().
string
(
equalTo
(
"Hello world!"
)));
this
.
mockMvc
.
perform
(
get
(
"/handle"
)
.
accept
(
MediaType
.
TEXT_PLAIN
)
).
andExpect
(
content
().
string
(
equalTo
(
"Hello world!"
)));
this
.
mockMvc
.
perform
(
get
(
"/handleUtf8"
)).
andExpect
(
content
().
string
(
equalTo
(
"\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"
)));
}
@Test
public
void
testContentAsBytes
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/handle"
)).
andExpect
(
content
().
bytes
(
"Hello world!"
.
getBytes
(
"ISO-8859-1"
)));
this
.
mockMvc
.
perform
(
get
(
"/handleUtf8"
)).
andExpect
(
content
().
bytes
(
"\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"
.
getBytes
(
"UTF-8"
)));
this
.
mockMvc
.
perform
(
get
(
"/handle"
).
accept
(
MediaType
.
TEXT_PLAIN
))
.
andExpect
(
content
().
bytes
(
"Hello world!"
.
getBytes
(
"ISO-8859-1"
)));
this
.
mockMvc
.
perform
(
get
(
"/handleUtf8"
))
.
andExpect
(
content
().
bytes
(
"\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"
.
getBytes
(
"UTF-8"
)));
}
@Test
public
void
testContentStringMatcher
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/handle"
)).
andExpect
(
content
().
string
(
containsString
(
"world"
)));
this
.
mockMvc
.
perform
(
get
(
"/handle"
).
accept
(
MediaType
.
TEXT_PLAIN
))
.
andExpect
(
content
().
string
(
containsString
(
"world"
)));
}
@Test
public
void
testCharacterEncoding
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/handle"
)).
andExpect
(
content
().
encoding
(
"ISO-8859-1"
));
this
.
mockMvc
.
perform
(
get
(
"/handleUtf8"
)).
andExpect
(
content
().
encoding
(
"UTF-8"
));
this
.
mockMvc
.
perform
(
get
(
"/handle"
).
accept
(
MediaType
.
TEXT_PLAIN
))
.
andExpect
(
content
().
encoding
(
"ISO-8859-1"
))
.
andExpect
(
content
().
string
(
containsString
(
"world"
)));
this
.
mockMvc
.
perform
(
get
(
"/handleUtf8"
))
.
andExpect
(
content
().
encoding
(
"UTF-8"
))
.
andExpect
(
content
().
bytes
(
"\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"
.
getBytes
(
"UTF-8"
)));
}
@Test
public
void
testSpringHateoasJsonLink
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/handle"
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
jsonPath
(
"$.links[?(@.rel == 'self')].href"
).
value
(
"http://myhost/people"
));
}
@Test
public
void
testSpringHateoasXmlLink
()
throws
Exception
{
Map
<
String
,
String
>
ns
=
Collections
.
singletonMap
(
"ns"
,
"http://www.w3.org/2005/Atom"
);
this
.
mockMvc
.
perform
(
get
(
"/handle"
).
accept
(
MediaType
.
APPLICATION_XML
))
.
andDo
(
print
())
.
andExpect
(
xpath
(
"/person/ns:link[@rel='self']/@href"
,
ns
).
string
(
"http://myhost/people"
));
}
...
...
@@ -102,5 +145,20 @@ public class ContentAssertionTests {
public
String
handleWithCharset
()
{
return
"\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"
;
// "Hello world! (Japanese)
}
@RequestMapping
(
value
=
"/handle"
,
produces
={
"application/json"
,
"application/xml"
})
@ResponseBody
public
PersonResource
handleJsonOrXml
()
{
PersonResource
resource
=
new
PersonResource
();
resource
.
name
=
"Joe"
;
resource
.
add
(
new
Link
(
"http://myhost/people"
));
return
resource
;
}
}
@XmlRootElement
(
name
=
"person"
)
static
class
PersonResource
extends
ResourceSupport
{
String
name
;
}
}
spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/servlet-context.xml
浏览文件 @
f30d33d3
...
...
@@ -16,11 +16,11 @@
<bean
id=
"viewResolver"
class=
"org.springframework.web.servlet.view.UrlBasedViewResolver"
>
<property
name=
"viewClass"
value=
"org.springframework.web.servlet.view.tiles
2
.TilesView"
/>
value=
"org.springframework.web.servlet.view.tiles
3
.TilesView"
/>
</bean>
<bean
id=
"tilesConfigurer"
class=
"org.springframework.web.servlet.view.tiles
2
.TilesConfigurer"
>
class=
"org.springframework.web.servlet.view.tiles
3
.TilesConfigurer"
>
<property
name=
"definitions"
>
<value>
/WEB-INF/**/tiles.xml
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录