Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
a7257172
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,发现更多精彩内容 >>
提交
a7257172
编写于
11月 11, 2009
作者:
K
Keith Donald
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
tests for custom conversion service / validator
上级
cb8b603b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
115 addition
and
3 deletion
+115
-3
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java
...springframework/web/servlet/config/MvcNamespaceTests.java
+91
-3
org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-conversion-service.xml
...b/servlet/config/mvc-config-custom-conversion-service.xml
+12
-0
org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-validator.xml
...mework/web/servlet/config/mvc-config-custom-validator.xml
+12
-0
未找到文件。
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java
浏览文件 @
a7257172
package
org.springframework.web.servlet.config
;
package
org.springframework.web.servlet.config
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.Locale
;
import
java.util.Locale
;
import
javax.validation.Valid
;
import
javax.validation.constraints.NotNull
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.beans.ConversionNotSupportedException
;
import
org.springframework.beans.factory.xml.XmlBeanDefinitionReader
;
import
org.springframework.beans.factory.xml.XmlBeanDefinitionReader
;
import
org.springframework.context.i18n.LocaleContextHolder
;
import
org.springframework.context.i18n.LocaleContextHolder
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.ClassPathResource
;
...
@@ -17,6 +23,9 @@ import org.springframework.mock.web.MockHttpServletRequest;
...
@@ -17,6 +23,9 @@ import org.springframework.mock.web.MockHttpServletRequest;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.mock.web.MockServletContext
;
import
org.springframework.mock.web.MockServletContext
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.Errors
;
import
org.springframework.validation.Validator
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.context.support.GenericWebApplicationContext
;
import
org.springframework.web.context.support.GenericWebApplicationContext
;
...
@@ -52,14 +61,93 @@ public class MvcNamespaceTests {
...
@@ -52,14 +61,93 @@ public class MvcNamespaceTests {
request
.
addParameter
(
"date"
,
"2009-10-31"
);
request
.
addParameter
(
"date"
,
"2009-10-31"
);
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
adapter
.
handle
(
request
,
response
,
handler
);
adapter
.
handle
(
request
,
response
,
handler
);
assertTrue
(
handler
.
recordedValidationError
);
}
}
@Test
(
expected
=
ConversionNotSupportedException
.
class
)
public
void
testCustomConversionService
()
throws
Exception
{
XmlBeanDefinitionReader
reader
=
new
XmlBeanDefinitionReader
(
container
);
reader
.
loadBeanDefinitions
(
new
ClassPathResource
(
"mvc-config-custom-conversion-service.xml"
,
getClass
()));
assertEquals
(
3
,
container
.
getBeanDefinitionCount
());
DefaultAnnotationHandlerMapping
mapping
=
container
.
getBean
(
DefaultAnnotationHandlerMapping
.
class
);
assertNotNull
(
mapping
);
assertEquals
(
0
,
mapping
.
getOrder
());
AnnotationMethodHandlerAdapter
adapter
=
container
.
getBean
(
AnnotationMethodHandlerAdapter
.
class
);
assertNotNull
(
adapter
);
TestController
handler
=
new
TestController
();
// default web binding initializer behavior test
MockHttpServletRequest
request
=
new
MockHttpServletRequest
();
request
.
addParameter
(
"date"
,
"2009-10-31"
);
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
adapter
.
handle
(
request
,
response
,
handler
);
}
@Test
public
void
testCustomValidator
()
throws
Exception
{
XmlBeanDefinitionReader
reader
=
new
XmlBeanDefinitionReader
(
container
);
reader
.
loadBeanDefinitions
(
new
ClassPathResource
(
"mvc-config-custom-validator.xml"
,
getClass
()));
assertEquals
(
3
,
container
.
getBeanDefinitionCount
());
DefaultAnnotationHandlerMapping
mapping
=
container
.
getBean
(
DefaultAnnotationHandlerMapping
.
class
);
assertNotNull
(
mapping
);
assertEquals
(
0
,
mapping
.
getOrder
());
AnnotationMethodHandlerAdapter
adapter
=
container
.
getBean
(
AnnotationMethodHandlerAdapter
.
class
);
assertNotNull
(
adapter
);
TestController
handler
=
new
TestController
();
// default web binding initializer behavior test
MockHttpServletRequest
request
=
new
MockHttpServletRequest
();
request
.
addParameter
(
"date"
,
"2009-10-31"
);
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
adapter
.
handle
(
request
,
response
,
handler
);
assertTrue
(
container
.
getBean
(
TestValidator
.
class
).
validatorInvoked
);
assertFalse
(
handler
.
recordedValidationError
);
}
@Controller
@Controller
public
static
class
TestController
{
public
static
class
TestController
{
private
boolean
recordedValidationError
;
@RequestMapping
@RequestMapping
public
void
testBind
(
@RequestParam
@DateTimeFormat
(
iso
=
ISO
.
DATE
)
Date
date
)
{
public
void
testBind
(
@RequestParam
@DateTimeFormat
(
iso
=
ISO
.
DATE
)
Date
date
,
@Valid
TestBean
bean
,
BindingResult
result
)
{
if
(
result
.
getErrorCount
()
==
1
)
{
this
.
recordedValidationError
=
true
;
}
else
{
this
.
recordedValidationError
=
false
;
}
}
}
}
}
public
static
class
TestValidator
implements
Validator
{
boolean
validatorInvoked
;
public
boolean
supports
(
Class
<?>
clazz
)
{
return
true
;
}
public
void
validate
(
Object
target
,
Errors
errors
)
{
this
.
validatorInvoked
=
true
;
}
}
private
static
class
TestBean
{
@NotNull
private
String
field
;
public
String
getField
()
{
return
field
;
}
public
void
setField
(
String
field
)
{
this
.
field
=
field
;
}
}
}
}
org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-conversion-service.xml
0 → 100644
浏览文件 @
a7257172
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns=
"http://www.springframework.org/schema/beans"
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
<mvc:annotation-driven
conversion-service=
"conversionService"
/>
<bean
id=
"conversionService"
class=
"org.springframework.core.convert.support.DefaultConversionService"
/>
</beans>
org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-validator.xml
0 → 100644
浏览文件 @
a7257172
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns=
"http://www.springframework.org/schema/beans"
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
<mvc:annotation-driven
validator=
"validator"
/>
<bean
id=
"validator"
class=
"org.springframework.web.servlet.config.MvcNamespaceTests$TestValidator"
/>
</beans>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录