Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dk131072
spring-framework
提交
60078015
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,发现更多精彩内容 >>
提交
60078015
编写于
6月 19, 2011
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-7812 Add CustomRequestCondition, PatternsRequestCondition, and other condition related tests
上级
4d27cde6
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
609 addition
and
403 deletion
+609
-403
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java
...ork/web/servlet/handler/AbstractHandlerMethodMapping.java
+6
-4
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/condition/AbstractRequestCondition.java
...k/web/servlet/mvc/condition/AbstractRequestCondition.java
+4
-4
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/CustomRequestCondition.java
...mework/web/servlet/mvc/method/CustomRequestCondition.java
+14
-12
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java
...gframework/web/servlet/mvc/method/RequestMappingInfo.java
+23
-39
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java
.../servlet/mvc/method/RequestMappingInfoHandlerMapping.java
+12
-18
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java
...t/mvc/method/annotation/RequestMappingHandlerMapping.java
+1
-1
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/handler/HandlerMethodMappingTests.java
...mework/web/servlet/handler/HandlerMethodMappingTests.java
+1
-1
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/condition/PatternsRequestConditionTests.java
.../servlet/mvc/condition/PatternsRequestConditionTests.java
+154
-0
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/condition/ProducesRequestConditionTests.java
.../servlet/mvc/condition/ProducesRequestConditionTests.java
+37
-8
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java
...et/mvc/condition/RequestMethodsRequestConditionTests.java
+13
-4
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/CustomRequestConditionTests.java
...k/web/servlet/mvc/method/CustomRequestConditionTests.java
+111
-0
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoComparatorTests.java
...servlet/mvc/method/RequestMappingInfoComparatorTests.java
+0
-167
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java
...let/mvc/method/RequestMappingInfoHandlerMappingTests.java
+16
-2
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoTests.java
...ework/web/servlet/mvc/method/RequestMappingInfoTests.java
+217
-143
未找到文件。
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java
浏览文件 @
60078015
...
...
@@ -204,16 +204,18 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
logger
.
info
(
"Mapped \""
+
mapping
+
"\" onto "
+
handlerMethod
);
}
Set
<
String
>
paths
=
getMappingPaths
(
mapping
);
for
(
String
path
:
paths
)
{
urlMap
.
add
(
path
,
mapping
);
Set
<
String
>
patterns
=
getMappingPathPatterns
(
mapping
);
for
(
String
pattern
:
patterns
)
{
if
(!
getPathMatcher
().
isPattern
(
pattern
))
{
urlMap
.
add
(
pattern
,
mapping
);
}
}
}
/**
* Get the URL paths associated with the given mapping.
*/
protected
abstract
Set
<
String
>
getMappingPaths
(
T
mapping
);
protected
abstract
Set
<
String
>
getMappingPath
Pattern
s
(
T
mapping
);
@Override
protected
HandlerMethod
getHandlerInternal
(
HttpServletRequest
request
)
throws
Exception
{
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/condition/AbstractRequestCondition.java
浏览文件 @
60078015
...
...
@@ -25,10 +25,10 @@ import java.util.Iterator;
* @author Rossen Stoyanchev
* @since 3.1
*/
abstract
class
AbstractRequestCondition
<
T
extends
AbstractRequestCondition
<
T
>>
implements
RequestCondition
<
T
>
{
public
abstract
class
AbstractRequestCondition
<
T
extends
AbstractRequestCondition
<
T
>>
implements
RequestCondition
<
T
>
{
/**
* Returns the discrete
expression
s a request condition is composed of such as URL patterns,
* Returns the discrete
item
s a request condition is composed of such as URL patterns,
* HTTP request methods, parameter expressions, etc.
*/
protected
abstract
Collection
<?>
getContent
();
...
...
@@ -65,8 +65,8 @@ abstract class AbstractRequestCondition<T extends AbstractRequestCondition<T>> i
}
/**
*
Returns {@code true} if the individual expressions of the condition are combined via logical
*
conjunction (" && "); or {@code false} otherwise
.
*
The notation to use when printing discrete items of content in the toString() method.
*
For example URL patterns use " || " while parameter expressions use " && "
.
*/
protected
abstract
String
getToStringInfix
();
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/
condition
/CustomRequestCondition.java
→
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/
method
/CustomRequestCondition.java
浏览文件 @
60078015
...
...
@@ -14,42 +14,44 @@
* limitations under the License.
*/
package
org.springframework.web.servlet.mvc.
condition
;
package
org.springframework.web.servlet.mvc.
method
;
import
java.util.Collection
;
import
java.util.Collections
;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.web.servlet.mvc.condition.AbstractRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.RequestCondition
;
/**
* Wraps and delegates operations to a custom {@link RequestCondition} whose type is not known and even its
* presence is not guaranteed ahead of time. The main purpose of this class is to ensure a type-safe and
* null-safe way of combining and comparing custom request conditions.
* Wraps and delegates operations to a {@link RequestCondition} whose type is not known ahead of time. The main goal
* is to provide type-safe and null-safe way of comparing and combining optional custom {@link RequestCondition}s.
*
* @author Rossen Stoyanchev
* @since 3.1
*/
public
final
class
CustomRequestCondition
extends
AbstractRequestCondition
<
CustomRequestCondition
>
{
final
class
CustomRequestCondition
extends
AbstractRequestCondition
<
CustomRequestCondition
>
{
@SuppressWarnings
(
"rawtypes"
)
private
final
RequestCondition
customCondition
;
/**
* Creates a
{@link CustomRequestCondition} that wraps the given {@link RequestCondition} instance
.
* @param requestCondition the custom request condition
to wrap
* Creates a
n instance that wraps the given custom request condition
.
* @param requestCondition the custom request condition
*/
public
CustomRequestCondition
(
RequestCondition
<?>
requestCondition
)
{
CustomRequestCondition
(
RequestCondition
<?>
requestCondition
)
{
this
.
customCondition
=
requestCondition
;
}
/**
* Creates an
empty {@link CustomRequestCondition}
.
* Creates an
instance that does not wrap any custom request condition
.
*/
public
CustomRequestCondition
()
{
CustomRequestCondition
()
{
this
(
null
);
}
public
RequestCondition
<?>
get
Request
Condition
()
{
public
RequestCondition
<?>
getCondition
()
{
return
customCondition
;
}
...
...
@@ -60,7 +62,7 @@ public final class CustomRequestCondition extends AbstractRequestCondition<Custo
@Override
protected
String
getToStringInfix
()
{
return
""
;
return
"
"
;
}
/**
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java
浏览文件 @
60078015
...
...
@@ -18,9 +18,7 @@ package org.springframework.web.servlet.mvc.method;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.CustomRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.HeadersRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.ParamsRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.PatternsRequestCondition
;
...
...
@@ -39,8 +37,7 @@ import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondit
* <li>{@link ProducesRequestCondition}</li>
* </ul>
*
* Optionally a custom request condition may also be provided by wrapping it in an instance
* of {@link CustomRequestCondition}.
* Optionally a custom request condition may also be provided.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
...
...
@@ -60,95 +57,82 @@ public final class RequestMappingInfo {
private
final
ProducesRequestCondition
producesCondition
;
private
CustomRequestCondition
customCondition
=
new
CustomRequestCondition
()
;
private
final
CustomRequestCondition
customCondition
;
private
int
hash
;
/**
* Creates a new {@code RequestMappingInfo} instance.
*/
public
RequestMappingInfo
(
PatternsRequestCondition
patterns
,
RequestMethodsRequestCondition
methods
,
ParamsRequestCondition
params
,
HeadersRequestCondition
headers
,
ConsumesRequestCondition
consumes
,
ProducesRequestCondition
produces
)
{
this
(
patterns
,
methods
,
params
,
headers
,
consumes
,
produces
,
null
);
}
/**
* Creates a new {@code RequestMappingInfo} instance also providing a custom {@link RequestCondition}.
*/
public
RequestMappingInfo
(
PatternsRequestCondition
patterns
,
RequestMethodsRequestCondition
methods
,
ParamsRequestCondition
params
,
HeadersRequestCondition
headers
,
ConsumesRequestCondition
consumes
,
ProducesRequestCondition
produces
,
CustomRequestCondition
custom
)
{
RequestCondition
<?>
custom
)
{
this
.
patternsCondition
=
patterns
!=
null
?
patterns
:
new
PatternsRequestCondition
();
this
.
methodsCondition
=
methods
!=
null
?
methods
:
new
RequestMethodsRequestCondition
();
this
.
paramsCondition
=
params
!=
null
?
params
:
new
ParamsRequestCondition
();
this
.
headersCondition
=
headers
!=
null
?
headers
:
new
HeadersRequestCondition
();
this
.
consumesCondition
=
consumes
!=
null
?
consumes
:
new
ConsumesRequestCondition
();
this
.
producesCondition
=
produces
!=
null
?
produces
:
new
ProducesRequestCondition
();
this
.
customCondition
=
custom
!=
null
?
custom
:
new
CustomRequestCondition
();
}
/**
* Package protected constructor for tests.
*/
RequestMappingInfo
(
String
[]
patterns
,
RequestMethod
...
methods
)
{
this
(
new
PatternsRequestCondition
(
patterns
),
new
RequestMethodsRequestCondition
(
methods
),
null
,
null
,
null
,
null
);
this
.
customCondition
=
custom
!=
null
?
new
CustomRequestCondition
(
custom
)
:
new
CustomRequestCondition
();
}
/**
* Returns the URL patterns of this request mapping info.
* Returns the URL patterns of this {@link RequestMappingInfo};
* or instance with 0 patterns, never {@code null}
*/
public
PatternsRequestCondition
getPatternsCondition
()
{
return
patternsCondition
;
}
/**
* Returns the HTTP request methods of this {@link RequestMappingInfo}.
* Returns the HTTP request methods of this {@link RequestMappingInfo};
* or instance with 0 request methods, never {@code null}
*/
public
RequestMethodsRequestCondition
getMethodsCondition
()
{
return
methodsCondition
;
}
/**
* Returns the "parameters" condition of this {@link RequestMappingInfo}.
* Returns the "parameters" condition of this {@link RequestMappingInfo};
* or instance with 0 parameter expressions, never {@code null}
*/
public
ParamsRequestCondition
getParamsCondition
()
{
return
paramsCondition
;
}
/**
* Returns the "headers" condition of this {@link RequestMappingInfo}.
* Returns the "headers" condition of this {@link RequestMappingInfo};
* or instance with 0 header expressions, never {@code null}
*/
public
HeadersRequestCondition
getHeadersCondition
()
{
return
headersCondition
;
}
/**
* Returns the "consumes" condition of this {@link RequestMappingInfo}.
* Returns the "consumes" condition of this {@link RequestMappingInfo};
* or instance with 0 consumes expressions, never {@code null}
*/
public
ConsumesRequestCondition
getConsumesCondition
()
{
return
consumesCondition
;
}
/**
* Returns the "produces" condition of this {@link RequestMappingInfo}.
* Returns the "produces" condition of this {@link RequestMappingInfo};
* or instance with 0 produces expressions, never {@code null}
*/
public
ProducesRequestCondition
getProducesCondition
()
{
return
producesCondition
;
}
/**
*
Sets a custom request condition.
*
Returns the "custom" condition of this {@link RequestMappingInfo}; or {@code null}
*/
public
void
setCustomCondition
(
CustomRequestCondition
customCondition
)
{
this
.
customCondition
=
customCondition
;
public
RequestCondition
<?>
getCustomCondition
(
)
{
return
customCondition
.
getCondition
()
;
}
/**
...
...
@@ -165,7 +149,7 @@ public final class RequestMappingInfo {
ProducesRequestCondition
produces
=
this
.
producesCondition
.
combine
(
other
.
producesCondition
);
CustomRequestCondition
custom
=
this
.
customCondition
.
combine
(
other
.
customCondition
);
return
new
RequestMappingInfo
(
patterns
,
methods
,
params
,
headers
,
consumes
,
produces
,
custom
);
return
new
RequestMappingInfo
(
patterns
,
methods
,
params
,
headers
,
consumes
,
produces
,
custom
.
getCondition
()
);
}
/**
...
...
@@ -175,7 +159,7 @@ public final class RequestMappingInfo {
* the current request, sorted with best matching patterns on top.
* @return a new instance in case all conditions match; or {@code null} otherwise
*/
public
RequestMappingInfo
getMatching
RequestMapping
Info
(
HttpServletRequest
request
)
{
public
RequestMappingInfo
getMatchingInfo
(
HttpServletRequest
request
)
{
RequestMethodsRequestCondition
methods
=
methodsCondition
.
getMatchingCondition
(
request
);
ParamsRequestCondition
params
=
paramsCondition
.
getMatchingCondition
(
request
);
HeadersRequestCondition
headers
=
headersCondition
.
getMatchingCondition
(
request
);
...
...
@@ -196,13 +180,13 @@ public final class RequestMappingInfo {
return
null
;
}
return
new
RequestMappingInfo
(
patterns
,
methods
,
params
,
headers
,
consumes
,
produces
,
custom
);
return
new
RequestMappingInfo
(
patterns
,
methods
,
params
,
headers
,
consumes
,
produces
,
custom
.
getCondition
()
);
}
/**
* Compares "this" info (i.e. the current instance) with another info in the context of a request.
* <p>Note: it is assumed both instances have been obtained via
* {@link #getMatching
RequestMapping
Info(HttpServletRequest)} to ensure they have conditions with
* {@link #getMatchingInfo(HttpServletRequest)} to ensure they have conditions with
* content relevant to current request.
*/
public
int
compareTo
(
RequestMappingInfo
other
,
HttpServletRequest
request
)
{
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java
浏览文件 @
60078015
...
...
@@ -48,27 +48,21 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
@Override
protected
void
handlerMethodsInitialized
(
Map
<
RequestMappingInfo
,
HandlerMethod
>
handlerMethods
)
{
List
<
RequestMappingInfo
>
mapping
s
=
new
ArrayList
<
RequestMappingInfo
>(
handlerMethods
.
keySet
());
while
(
mapping
s
.
size
()
>
1
)
{
RequestMappingInfo
mapping
=
mapping
s
.
remove
(
0
);
for
(
RequestMappingInfo
otherMapping
:
mapping
s
)
{
// TODO:
further validate mapping condition
s
List
<
RequestMappingInfo
>
info
s
=
new
ArrayList
<
RequestMappingInfo
>(
handlerMethods
.
keySet
());
while
(
info
s
.
size
()
>
1
)
{
RequestMappingInfo
info1
=
info
s
.
remove
(
0
);
for
(
RequestMappingInfo
info2
:
info
s
)
{
// TODO:
validate duplicate consumable and producible media type
s
}
}
}
/**
* Get the URL paths associated with this {@link RequestMappingInfo}.
* Get the URL path
pattern
s associated with this {@link RequestMappingInfo}.
*/
@Override
protected
Set
<
String
>
getMappingPaths
(
RequestMappingInfo
mapping
)
{
Set
<
String
>
paths
=
new
HashSet
<
String
>();
for
(
String
pattern
:
mapping
.
getPatternsCondition
().
getPatterns
())
{
if
(!
getPathMatcher
().
isPattern
(
pattern
))
{
paths
.
add
(
pattern
);
}
}
return
paths
;
protected
Set
<
String
>
getMappingPathPatterns
(
RequestMappingInfo
info
)
{
return
info
.
getPatternsCondition
().
getPatterns
();
}
/**
...
...
@@ -79,8 +73,8 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
* @returns a RequestMappingInfo instance in case of a match; or {@code null} in case of no match.
*/
@Override
protected
RequestMappingInfo
getMatchingMapping
(
RequestMappingInfo
mapping
,
HttpServletRequest
request
)
{
return
mapping
.
getMatchingRequestMapp
ingInfo
(
request
);
protected
RequestMappingInfo
getMatchingMapping
(
RequestMappingInfo
info
,
HttpServletRequest
request
)
{
return
info
.
getMatch
ingInfo
(
request
);
}
/**
...
...
@@ -89,8 +83,8 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
@Override
protected
Comparator
<
RequestMappingInfo
>
getMappingComparator
(
final
HttpServletRequest
request
)
{
return
new
Comparator
<
RequestMappingInfo
>()
{
public
int
compare
(
RequestMappingInfo
info
,
RequestMappingInfo
otherInfo
)
{
return
info
.
compareTo
(
otherInfo
,
request
);
public
int
compare
(
RequestMappingInfo
info
1
,
RequestMappingInfo
info2
)
{
return
info
1
.
compareTo
(
info2
,
request
);
}
};
}
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java
浏览文件 @
60078015
...
...
@@ -84,7 +84,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
new
ParamsRequestCondition
(
annotation
.
params
()),
new
HeadersRequestCondition
(
annotation
.
headers
()),
new
ConsumesRequestCondition
(
annotation
.
consumes
(),
annotation
.
headers
()),
new
ProducesRequestCondition
(
annotation
.
produces
(),
annotation
.
headers
()));
new
ProducesRequestCondition
(
annotation
.
produces
(),
annotation
.
headers
())
,
null
);
}
}
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/handler/HandlerMethodMappingTests.java
浏览文件 @
60078015
...
...
@@ -118,7 +118,7 @@ public class HandlerMethodMappingTests {
}
@Override
protected
Set
<
String
>
getMappingPaths
(
String
key
)
{
protected
Set
<
String
>
getMappingPath
Pattern
s
(
String
key
)
{
return
new
HashSet
<
String
>();
}
}
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/condition/PatternsRequestConditionTests.java
0 → 100644
浏览文件 @
60078015
/*
* 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.web.servlet.mvc.condition
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
javax.servlet.http.HttpServletRequest
;
import
org.junit.Test
;
import
org.springframework.mock.web.MockHttpServletRequest
;
/**
* @author Rossen Stoyanchev
*/
public
class
PatternsRequestConditionTests
{
@Test
public
void
prependSlash
()
{
PatternsRequestCondition
c
=
new
PatternsRequestCondition
(
"foo"
);
assertEquals
(
"/foo"
,
c
.
getPatterns
().
iterator
().
next
());
}
@Test
public
void
prependNonEmptyPatternsOnly
()
{
PatternsRequestCondition
c
=
new
PatternsRequestCondition
(
""
);
assertEquals
(
"Do not prepend empty patterns (SPR-8255)"
,
""
,
c
.
getPatterns
().
iterator
().
next
());
}
@Test
public
void
combineEmptySets
()
{
PatternsRequestCondition
c1
=
new
PatternsRequestCondition
();
PatternsRequestCondition
c2
=
new
PatternsRequestCondition
();
assertEquals
(
new
PatternsRequestCondition
(
""
),
c1
.
combine
(
c2
));
}
@Test
public
void
combineOnePatternWithEmptySet
()
{
PatternsRequestCondition
c1
=
new
PatternsRequestCondition
(
"/type1"
,
"/type2"
);
PatternsRequestCondition
c2
=
new
PatternsRequestCondition
();
assertEquals
(
new
PatternsRequestCondition
(
"/type1"
,
"/type2"
),
c1
.
combine
(
c2
));
c1
=
new
PatternsRequestCondition
();
c2
=
new
PatternsRequestCondition
(
"/method1"
,
"/method2"
);
assertEquals
(
new
PatternsRequestCondition
(
"/method1"
,
"/method2"
),
c1
.
combine
(
c2
));
}
@Test
public
void
combineMultiplePatterns
()
{
PatternsRequestCondition
c1
=
new
PatternsRequestCondition
(
"/t1"
,
"/t2"
);
PatternsRequestCondition
c2
=
new
PatternsRequestCondition
(
"/m1"
,
"/m2"
);
assertEquals
(
new
PatternsRequestCondition
(
"/t1/m1"
,
"/t1/m2"
,
"/t2/m1"
,
"/t2/m2"
),
c1
.
combine
(
c2
));
}
@Test
public
void
matchDirectPath
()
{
PatternsRequestCondition
condition
=
new
PatternsRequestCondition
(
"/foo"
);
PatternsRequestCondition
match
=
condition
.
getMatchingCondition
(
new
MockHttpServletRequest
(
"GET"
,
"/foo"
));
assertNotNull
(
match
);
}
@Test
public
void
matchPattern
()
{
PatternsRequestCondition
condition
=
new
PatternsRequestCondition
(
"/foo/*"
);
PatternsRequestCondition
match
=
condition
.
getMatchingCondition
(
new
MockHttpServletRequest
(
"GET"
,
"/foo/bar"
));
assertNotNull
(
match
);
}
@Test
public
void
matchSortPatterns
()
{
PatternsRequestCondition
condition
=
new
PatternsRequestCondition
(
"/**"
,
"/foo/bar"
,
"/foo/*"
);
PatternsRequestCondition
match
=
condition
.
getMatchingCondition
(
new
MockHttpServletRequest
(
"GET"
,
"/foo/bar"
));
PatternsRequestCondition
expected
=
new
PatternsRequestCondition
(
"/foo/bar"
,
"/foo/*"
,
"/**"
);
assertEquals
(
expected
,
match
);
}
@Test
public
void
matchImplicitByExtension
()
{
PatternsRequestCondition
condition
=
new
PatternsRequestCondition
(
"/foo"
);
PatternsRequestCondition
match
=
condition
.
getMatchingCondition
(
new
MockHttpServletRequest
(
"GET"
,
"/foo.html"
));
assertNotNull
(
match
);
assertEquals
(
"/foo.*"
,
match
.
getPatterns
().
iterator
().
next
());
}
@Test
public
void
matchImplicitTrailingSlash
()
{
PatternsRequestCondition
condition
=
new
PatternsRequestCondition
(
"/foo"
);
PatternsRequestCondition
match
=
condition
.
getMatchingCondition
(
new
MockHttpServletRequest
(
"GET"
,
"/foo/"
));
assertNotNull
(
match
);
assertEquals
(
"/foo/"
,
match
.
getPatterns
().
iterator
().
next
());
}
@Test
public
void
matchPatternContainsExtension
()
{
PatternsRequestCondition
condition
=
new
PatternsRequestCondition
(
"/foo.jpg"
);
PatternsRequestCondition
match
=
condition
.
getMatchingCondition
(
new
MockHttpServletRequest
(
"GET"
,
"/foo.html"
));
assertNull
(
match
);
}
@Test
public
void
compareEqualPatterns
()
{
PatternsRequestCondition
c1
=
new
PatternsRequestCondition
(
"/foo*"
);
PatternsRequestCondition
c2
=
new
PatternsRequestCondition
(
"/foo*"
);
assertEquals
(
0
,
c1
.
compareTo
(
c2
,
new
MockHttpServletRequest
(
"GET"
,
"/foo"
)));
}
@Test
public
void
comparePatternSpecificity
()
{
PatternsRequestCondition
c1
=
new
PatternsRequestCondition
(
"/fo*"
);
PatternsRequestCondition
c2
=
new
PatternsRequestCondition
(
"/foo"
);
assertEquals
(
1
,
c1
.
compareTo
(
c2
,
new
MockHttpServletRequest
(
"GET"
,
"/foo"
)));
}
@Test
public
void
compareNumberOfMatchingPatterns
()
throws
Exception
{
HttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo.html"
);
PatternsRequestCondition
c1
=
new
PatternsRequestCondition
(
"/foo"
,
"*.jpeg"
);
PatternsRequestCondition
c2
=
new
PatternsRequestCondition
(
"/foo"
,
"*.html"
);
PatternsRequestCondition
match1
=
c1
.
getMatchingCondition
(
request
);
PatternsRequestCondition
match2
=
c2
.
getMatchingCondition
(
request
);
assertEquals
(
1
,
match1
.
compareTo
(
match2
,
request
));
}
}
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/condition/ProducesRequestConditionTests.java
浏览文件 @
60078015
...
...
@@ -26,7 +26,6 @@ import java.util.Collection;
import
org.junit.Test
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.web.servlet.mvc.condition.ProducesRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.ProducesRequestCondition.ProduceMediaTypeExpression
;
/**
...
...
@@ -84,6 +83,42 @@ public class ProducesRequestConditionTests {
assertNull
(
condition
.
getMatchingCondition
(
request
));
}
@Test
public
void
compareTo
()
{
ProducesRequestCondition
html
=
new
ProducesRequestCondition
(
"text/html"
);
ProducesRequestCondition
xml
=
new
ProducesRequestCondition
(
"application/xml"
);
ProducesRequestCondition
none
=
new
ProducesRequestCondition
();
MockHttpServletRequest
request
=
new
MockHttpServletRequest
();
request
.
addHeader
(
"Accept"
,
"application/xml, text/html"
);
assertTrue
(
html
.
compareTo
(
xml
,
request
)
>
0
);
assertTrue
(
xml
.
compareTo
(
html
,
request
)
<
0
);
assertTrue
(
xml
.
compareTo
(
none
,
request
)
<
0
);
assertTrue
(
none
.
compareTo
(
xml
,
request
)
>
0
);
assertTrue
(
html
.
compareTo
(
none
,
request
)
<
0
);
assertTrue
(
none
.
compareTo
(
html
,
request
)
>
0
);
request
=
new
MockHttpServletRequest
();
request
.
addHeader
(
"Accept"
,
"application/xml, text/*"
);
assertTrue
(
html
.
compareTo
(
xml
,
request
)
>
0
);
assertTrue
(
xml
.
compareTo
(
html
,
request
)
<
0
);
request
=
new
MockHttpServletRequest
();
request
.
addHeader
(
"Accept"
,
"application/pdf"
);
assertTrue
(
html
.
compareTo
(
xml
,
request
)
==
0
);
assertTrue
(
xml
.
compareTo
(
html
,
request
)
==
0
);
// See SPR-7000
request
=
new
MockHttpServletRequest
();
request
.
addHeader
(
"Accept"
,
"text/html;q=0.9,application/xml"
);
assertTrue
(
html
.
compareTo
(
xml
,
request
)
>
0
);
assertTrue
(
xml
.
compareTo
(
html
,
request
)
<
0
);
}
@Test
public
void
compareToSingle
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
();
...
...
@@ -151,7 +186,7 @@ public class ProducesRequestConditionTests {
result
=
condition2
.
compareTo
(
condition1
,
request
);
assertTrue
(
"Invalid comparison result: "
+
result
,
result
<
0
);
}
@Test
public
void
combine
()
{
ProducesRequestCondition
condition1
=
new
ProducesRequestCondition
(
"text/plain"
);
...
...
@@ -212,12 +247,6 @@ public class ProducesRequestConditionTests {
fail
(
"Condition ["
+
s
+
"] not found"
);
}
}
}
}
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java
浏览文件 @
60078015
...
...
@@ -16,13 +16,14 @@
package
org.springframework.web.servlet.mvc.condition
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
org.junit.Test
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition
;
import
static
org
.
junit
.
Assert
.*;
/**
* @author Arjen Poutsma
...
...
@@ -56,6 +57,14 @@ public class RequestMethodsRequestConditionTests {
assertNotNull
(
condition
.
getMatchingCondition
(
request
));
}
@Test
public
void
noMethodsMatchAll
()
{
RequestMethodsRequestCondition
condition
=
new
RequestMethodsRequestCondition
();
assertNotNull
(
condition
.
getMatchingCondition
(
new
MockHttpServletRequest
(
"GET"
,
""
)));
assertNotNull
(
condition
.
getMatchingCondition
(
new
MockHttpServletRequest
(
"POST"
,
""
)));
assertNotNull
(
condition
.
getMatchingCondition
(
new
MockHttpServletRequest
(
"HEAD"
,
""
)));
}
@Test
public
void
compareTo
()
{
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/CustomRequestConditionTests.java
0 → 100644
浏览文件 @
60078015
/*
* 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.web.servlet.mvc.method
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertSame
;
import
javax.servlet.http.HttpServletRequest
;
import
org.junit.Test
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.servlet.mvc.condition.HeadersRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.ParamsRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition
;
/**
* @author Rossen Stoyanchev
*/
public
class
CustomRequestConditionTests
{
@Test
public
void
combineEmpty
()
{
CustomRequestCondition
empty
=
new
CustomRequestCondition
();
CustomRequestCondition
custom
=
new
CustomRequestCondition
(
new
ParamsRequestCondition
(
"name"
));
assertSame
(
empty
,
empty
.
combine
(
new
CustomRequestCondition
()));
assertSame
(
custom
,
custom
.
combine
(
empty
));
assertSame
(
custom
,
empty
.
combine
(
custom
));
}
@Test
public
void
combine
()
{
CustomRequestCondition
params1
=
new
CustomRequestCondition
(
new
ParamsRequestCondition
(
"name1"
));
CustomRequestCondition
params2
=
new
CustomRequestCondition
(
new
ParamsRequestCondition
(
"name2"
));
CustomRequestCondition
expected
=
new
CustomRequestCondition
(
new
ParamsRequestCondition
(
"name1"
,
"name2"
));
assertEquals
(
expected
,
params1
.
combine
(
params2
));
}
@Test
(
expected
=
ClassCastException
.
class
)
public
void
combineIncompatible
()
{
CustomRequestCondition
params
=
new
CustomRequestCondition
(
new
ParamsRequestCondition
(
"name"
));
CustomRequestCondition
headers
=
new
CustomRequestCondition
(
new
HeadersRequestCondition
(
"name"
));
params
.
combine
(
headers
);
}
@Test
public
void
match
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/"
);
request
.
setParameter
(
"name1"
,
"value1"
);
RequestMethodsRequestCondition
rm
=
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
,
RequestMethod
.
POST
);
CustomRequestCondition
custom
=
new
CustomRequestCondition
(
rm
);
RequestMethodsRequestCondition
expected
=
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
);
assertEquals
(
expected
,
custom
.
getMatchingCondition
(
request
).
getCondition
());
}
@Test
public
void
matchEmpty
()
{
CustomRequestCondition
empty
=
new
CustomRequestCondition
();
assertSame
(
empty
,
empty
.
getMatchingCondition
(
new
MockHttpServletRequest
()));
}
@Test
public
void
compare
()
{
HttpServletRequest
request
=
new
MockHttpServletRequest
();
CustomRequestCondition
params11
=
new
CustomRequestCondition
(
new
ParamsRequestCondition
(
"1"
));
CustomRequestCondition
params12
=
new
CustomRequestCondition
(
new
ParamsRequestCondition
(
"1"
,
"2"
));
assertEquals
(
1
,
params11
.
compareTo
(
params12
,
request
));
assertEquals
(-
1
,
params12
.
compareTo
(
params11
,
request
));
}
@Test
public
void
compareEmpty
()
{
HttpServletRequest
request
=
new
MockHttpServletRequest
();
CustomRequestCondition
empty
=
new
CustomRequestCondition
();
CustomRequestCondition
empty2
=
new
CustomRequestCondition
();
CustomRequestCondition
custom
=
new
CustomRequestCondition
(
new
ParamsRequestCondition
(
"name"
));
assertEquals
(
0
,
empty
.
compareTo
(
empty2
,
request
));
assertEquals
(-
1
,
custom
.
compareTo
(
empty
,
request
));
assertEquals
(
1
,
empty
.
compareTo
(
custom
,
request
));
}
@Test
(
expected
=
ClassCastException
.
class
)
public
void
compareIncompatible
()
{
CustomRequestCondition
params
=
new
CustomRequestCondition
(
new
ParamsRequestCondition
(
"name"
));
CustomRequestCondition
headers
=
new
CustomRequestCondition
(
new
HeadersRequestCondition
(
"name"
));
params
.
compareTo
(
headers
,
new
MockHttpServletRequest
());
}
}
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoComparatorTests.java
已删除
100644 → 0
浏览文件 @
4d27cde6
/*
* 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.web.servlet.mvc.method
;
import
static
java
.
util
.
Arrays
.
asList
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertSame
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.lang.reflect.Method
;
import
java.util.Collections
;
import
java.util.Comparator
;
import
java.util.List
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.servlet.mvc.condition.ParamsRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.ProducesRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition
;
import
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
;
/**
* Test fixture with {@link RequestMappingHandlerMapping} testing its {@link RequestMappingInfo} comparator.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
public
class
RequestMappingInfoComparatorTests
{
private
TestRequestMappingInfoHandlerMapping
handlerMapping
;
private
MockHttpServletRequest
request
;
@Before
public
void
setup
()
{
this
.
handlerMapping
=
new
TestRequestMappingInfoHandlerMapping
();
this
.
request
=
new
MockHttpServletRequest
();
}
@Test
public
void
moreSpecificPatternWins
()
{
request
.
setRequestURI
(
"/foo"
);
Comparator
<
RequestMappingInfo
>
comparator
=
handlerMapping
.
getMappingComparator
(
request
);
RequestMappingInfo
key1
=
new
RequestMappingInfo
(
new
String
[]{
"/fo*"
});
RequestMappingInfo
key2
=
new
RequestMappingInfo
(
new
String
[]{
"/foo"
});
assertEquals
(
1
,
comparator
.
compare
(
key1
,
key2
));
}
@Test
public
void
equalPatterns
()
{
request
.
setRequestURI
(
"/foo"
);
Comparator
<
RequestMappingInfo
>
comparator
=
handlerMapping
.
getMappingComparator
(
request
);
RequestMappingInfo
key1
=
new
RequestMappingInfo
(
new
String
[]{
"/foo*"
});
RequestMappingInfo
key2
=
new
RequestMappingInfo
(
new
String
[]{
"/foo*"
});
assertEquals
(
0
,
comparator
.
compare
(
key1
,
key2
));
}
@Test
public
void
greaterNumberOfMatchingPatternsWins
()
throws
Exception
{
request
.
setRequestURI
(
"/foo.html"
);
RequestMappingInfo
key1
=
new
RequestMappingInfo
(
new
String
[]{
"/foo"
,
"*.jpeg"
});
RequestMappingInfo
key2
=
new
RequestMappingInfo
(
new
String
[]{
"/foo"
,
"*.html"
});
RequestMappingInfo
match1
=
handlerMapping
.
getMatchingMapping
(
key1
,
request
);
RequestMappingInfo
match2
=
handlerMapping
.
getMatchingMapping
(
key2
,
request
);
List
<
RequestMappingInfo
>
matches
=
asList
(
match1
,
match2
);
Collections
.
sort
(
matches
,
handlerMapping
.
getMappingComparator
(
request
));
assertSame
(
match2
.
getPatternsCondition
(),
matches
.
get
(
0
).
getPatternsCondition
());
}
@Test
public
void
oneMethodWinsOverNone
()
{
Comparator
<
RequestMappingInfo
>
comparator
=
handlerMapping
.
getMappingComparator
(
request
);
RequestMappingInfo
key1
=
new
RequestMappingInfo
(
null
);
RequestMappingInfo
key2
=
new
RequestMappingInfo
(
null
,
new
RequestMethod
[]
{
RequestMethod
.
GET
});
assertEquals
(
1
,
comparator
.
compare
(
key1
,
key2
));
}
@Test
public
void
methodsAndParams
()
{
RequestMappingInfo
empty
=
new
RequestMappingInfo
(
null
);
RequestMappingInfo
oneMethod
=
new
RequestMappingInfo
(
null
,
new
RequestMethod
[]
{
RequestMethod
.
GET
});
RequestMappingInfo
oneMethodOneParam
=
new
RequestMappingInfo
(
null
,
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
),
new
ParamsRequestCondition
(
"foo"
),
null
,
null
,
null
);
List
<
RequestMappingInfo
>
list
=
asList
(
empty
,
oneMethod
,
oneMethodOneParam
);
Collections
.
shuffle
(
list
);
Collections
.
sort
(
list
,
handlerMapping
.
getMappingComparator
(
request
));
assertEquals
(
oneMethodOneParam
,
list
.
get
(
0
));
assertEquals
(
oneMethod
,
list
.
get
(
1
));
assertEquals
(
empty
,
list
.
get
(
2
));
}
@Test
public
void
produces
()
{
RequestMappingInfo
html
=
new
RequestMappingInfo
(
null
,
null
,
null
,
null
,
null
,
new
ProducesRequestCondition
(
"text/html"
));
RequestMappingInfo
xml
=
new
RequestMappingInfo
(
null
,
null
,
null
,
null
,
null
,
new
ProducesRequestCondition
(
"application/xml"
));
RequestMappingInfo
none
=
new
RequestMappingInfo
(
null
);
request
.
addHeader
(
"Accept"
,
"application/xml, text/html"
);
Comparator
<
RequestMappingInfo
>
comparator
=
handlerMapping
.
getMappingComparator
(
request
);
int
result
=
comparator
.
compare
(
html
,
xml
);
assertTrue
(
"Invalid comparison result: "
+
result
,
result
>
0
);
assertTrue
(
comparator
.
compare
(
xml
,
html
)
<
0
);
assertTrue
(
comparator
.
compare
(
xml
,
none
)
<
0
);
assertTrue
(
comparator
.
compare
(
none
,
xml
)
>
0
);
assertTrue
(
comparator
.
compare
(
html
,
none
)
<
0
);
assertTrue
(
comparator
.
compare
(
none
,
html
)
>
0
);
request
=
new
MockHttpServletRequest
();
request
.
addHeader
(
"Accept"
,
"application/xml, text/*"
);
comparator
=
handlerMapping
.
getMappingComparator
(
request
);
assertTrue
(
comparator
.
compare
(
html
,
xml
)
>
0
);
assertTrue
(
comparator
.
compare
(
xml
,
html
)
<
0
);
request
=
new
MockHttpServletRequest
();
request
.
addHeader
(
"Accept"
,
"application/pdf"
);
comparator
=
handlerMapping
.
getMappingComparator
(
request
);
assertTrue
(
comparator
.
compare
(
html
,
xml
)
==
0
);
assertTrue
(
comparator
.
compare
(
xml
,
html
)
==
0
);
// See SPR-7000
request
=
new
MockHttpServletRequest
();
request
.
addHeader
(
"Accept"
,
"text/html;q=0.9,application/xml"
);
comparator
=
handlerMapping
.
getMappingComparator
(
request
);
assertTrue
(
comparator
.
compare
(
html
,
xml
)
>
0
);
assertTrue
(
comparator
.
compare
(
xml
,
html
)
<
0
);
}
private
static
class
TestRequestMappingInfoHandlerMapping
extends
RequestMappingInfoHandlerMapping
{
@Override
protected
boolean
isHandler
(
Class
<?>
beanType
)
{
return
false
;
}
@Override
protected
RequestMappingInfo
getMappingForMethod
(
Method
method
,
Class
<?>
handlerType
)
{
return
null
;
}
}
}
\ No newline at end of file
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java
浏览文件 @
60078015
...
...
@@ -24,7 +24,10 @@ import static org.junit.Assert.assertSame;
import
static
org
.
junit
.
Assert
.
fail
;
import
java.lang.reflect.Method
;
import
java.util.Arrays
;
import
java.util.HashSet
;
import
java.util.Map
;
import
java.util.Set
;
import
org.junit.Before
;
import
org.junit.Test
;
...
...
@@ -85,6 +88,16 @@ public class RequestMappingInfoHandlerMappingTests {
mapping
.
setApplicationContext
(
context
);
}
@Test
public
void
getMappingPathPatterns
()
throws
Exception
{
RequestMappingInfo
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo/*"
,
"/foo"
,
"/bar/*"
,
"/bar"
),
null
,
null
,
null
,
null
,
null
,
null
);
Set
<
String
>
paths
=
mapping
.
getMappingPathPatterns
(
info
);
HashSet
<
String
>
expected
=
new
HashSet
<
String
>(
Arrays
.
asList
(
"/foo/*"
,
"/foo"
,
"/bar/*"
,
"/bar"
));
assertEquals
(
expected
,
paths
);
}
@Test
public
void
directMatch
()
throws
Exception
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo"
);
...
...
@@ -132,7 +145,8 @@ public class RequestMappingInfoHandlerMappingTests {
@Test
public
void
uriTemplateVariables
()
{
RequestMappingInfo
key
=
new
RequestMappingInfo
(
new
String
[]
{
"/{path1}/{path2}"
});
PatternsRequestCondition
patterns
=
new
PatternsRequestCondition
(
"/{path1}/{path2}"
);
RequestMappingInfo
key
=
new
RequestMappingInfo
(
patterns
,
null
,
null
,
null
,
null
,
null
,
null
);
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/1/2"
);
String
lookupPath
=
new
UrlPathHelper
().
getLookupPathForRequest
(
request
);
...
...
@@ -206,7 +220,7 @@ public class RequestMappingInfoHandlerMappingTests {
new
ParamsRequestCondition
(
annotation
.
params
()),
new
HeadersRequestCondition
(
annotation
.
headers
()),
new
ConsumesRequestCondition
(
annotation
.
consumes
(),
annotation
.
headers
()),
new
ProducesRequestCondition
(
annotation
.
produces
(),
annotation
.
headers
()));
new
ProducesRequestCondition
(
annotation
.
produces
(),
annotation
.
headers
())
,
null
);
}
}
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoTests.java
浏览文件 @
60078015
...
...
@@ -16,19 +16,26 @@
package
org.springframework.web.servlet.mvc.method
;
import
static
java
.
util
.
Arrays
.
asList
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
springframework
.
web
.
bind
.
annotation
.
RequestMethod
.
GET
;
import
static
org
.
springframework
.
web
.
bind
.
annotation
.
RequestMethod
.
POST
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.util.Collections
;
import
java.util.Comparator
;
import
java.util.List
;
import
org.junit.Test
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.HeadersRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.ParamsRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.PatternsRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.ProducesRequestCondition
;
import
org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition
;
/**
* Test fixture for {@link RequestMappingInfo} tests.
...
...
@@ -39,209 +46,276 @@ import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
public
class
RequestMappingInfoTests
{
@Test
public
void
equals
()
{
RequestMappingInfo
key1
=
new
RequestMappingInfo
(
new
String
[]
{
"/foo"
},
GET
);
RequestMappingInfo
key2
=
new
RequestMappingInfo
(
new
String
[]
{
"/foo"
},
GET
);
assertEquals
(
key1
,
key2
);
assertEquals
(
key1
.
hashCode
(),
key2
.
hashCode
());
}
@Test
public
void
equalsPrependSlash
()
{
RequestMappingInfo
key1
=
new
RequestMappingInfo
(
new
String
[]
{
"/foo"
},
GET
);
RequestMappingInfo
key2
=
new
RequestMappingInfo
(
new
String
[]
{
"foo"
},
GET
);
assertEquals
(
key1
,
key2
);
assertEquals
(
key1
.
hashCode
(),
key2
.
hashCode
());
}
@Test
public
void
combinePatterns
()
{
RequestMappingInfo
key1
=
createFromPatterns
(
"/t1"
,
"/t2"
);
RequestMappingInfo
key2
=
createFromPatterns
(
"/m1"
,
"/m2"
);
RequestMappingInfo
key3
=
createFromPatterns
(
"/t1/m1"
,
"/t1/m2"
,
"/t2/m1"
,
"/t2/m2"
);
assertEquals
(
key3
.
getPatternsCondition
(),
key1
.
combine
(
key2
).
getPatternsCondition
());
key1
=
createFromPatterns
(
"/t1"
);
key2
=
createFromPatterns
();
key3
=
createFromPatterns
(
"/t1"
);
assertEquals
(
key3
.
getPatternsCondition
(),
key1
.
combine
(
key2
).
getPatternsCondition
());
key1
=
createFromPatterns
();
key2
=
createFromPatterns
(
"/m1"
);
key3
=
createFromPatterns
(
"/m1"
);
assertEquals
(
key3
.
getPatternsCondition
(),
key1
.
combine
(
key2
).
getPatternsCondition
());
key1
=
createFromPatterns
();
key2
=
createFromPatterns
();
key3
=
createFromPatterns
(
""
);
assertEquals
(
key3
.
getPatternsCondition
(),
key1
.
combine
(
key2
).
getPatternsCondition
());
key1
=
createFromPatterns
(
"/t1"
);
key2
=
createFromPatterns
(
""
);
key3
=
createFromPatterns
(
"/t1"
);
assertEquals
(
key3
.
getPatternsCondition
(),
key1
.
combine
(
key2
).
getPatternsCondition
());
}
@Test
public
void
matchPatternsToRequest
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo"
);
RequestMappingInfo
match
=
createFromPatterns
(
"/foo"
).
getMatchingRequestMappingInfo
(
request
);
assertNotNull
(
match
);
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo/bar"
);
match
=
createFromPatterns
(
"/foo/*"
).
getMatchingRequestMappingInfo
(
request
);
assertNotNull
(
"Pattern match"
,
match
);
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo.html"
);
match
=
createFromPatterns
(
"/foo"
).
getMatchingRequestMappingInfo
(
request
);
assertNotNull
(
"Implicit match by extension"
,
match
);
assertEquals
(
"Contains matched pattern"
,
"/foo.*"
,
match
.
getPatternsCondition
().
getPatterns
().
iterator
().
next
());
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo/"
);
match
=
createFromPatterns
(
"/foo"
).
getMatchingRequestMappingInfo
(
request
);
assertNotNull
(
"Implicit match by trailing slash"
,
match
);
assertEquals
(
"Contains matched pattern"
,
"/foo/"
,
match
.
getPatternsCondition
().
getPatterns
().
iterator
().
next
());
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo.html"
);
match
=
createFromPatterns
(
"/foo.jpg"
).
getMatchingRequestMappingInfo
(
request
);
assertNull
(
"Implicit match ignored if pattern has extension"
,
match
);
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo.html"
);
match
=
createFromPatterns
(
"/foo.jpg"
).
getMatchingRequestMappingInfo
(
request
);
assertNull
(
"Implicit match ignored on pattern with trailing slash"
,
match
);
}
@Test
public
void
matchRequestMethods
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo"
);
RequestMappingInfo
key
=
createFromPatterns
(
"/foo"
);
RequestMappingInfo
match
=
createFromPatterns
(
"/foo"
).
getMatchingRequestMappingInfo
(
request
);
assertNotNull
(
"No method matches any method"
,
match
);
key
=
new
RequestMappingInfo
(
new
String
[]{
"/foo"
},
GET
);
match
=
key
.
getMatchingRequestMappingInfo
(
request
);
assertNotNull
(
"Exact match"
,
match
);
key
=
new
RequestMappingInfo
(
new
String
[]{
"/foo"
},
POST
);
match
=
key
.
getMatchingRequestMappingInfo
(
request
);
assertNull
(
"No match"
,
match
);
public
void
createEmpty
()
{
RequestMappingInfo
info
=
new
RequestMappingInfo
(
null
,
null
,
null
,
null
,
null
,
null
,
null
);
assertEquals
(
0
,
info
.
getPatternsCondition
().
getPatterns
().
size
());
assertEquals
(
0
,
info
.
getMethodsCondition
().
getMethods
().
size
());
assertEquals
(
0
,
info
.
getConsumesCondition
().
getMediaTypes
().
size
());
assertEquals
(
0
,
info
.
getProducesCondition
().
getMediaTypes
().
size
());
assertNotNull
(
info
.
getParamsCondition
());
assertNotNull
(
info
.
getHeadersCondition
());
assertNull
(
info
.
getCustomCondition
());
}
@Test
public
void
match
ingKeyContent
()
{
public
void
match
PatternsCondition
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo"
);
RequestMappingInfo
key
=
new
RequestMappingInfo
(
new
String
[]
{
"/foo*"
,
"/bar"
},
GET
,
POST
);
RequestMappingInfo
match
=
key
.
getMatchingRequestMappingInfo
(
request
);
RequestMappingInfo
expected
=
new
RequestMappingInfo
(
new
String
[]
{
"/foo*"
},
GET
);
assertEquals
(
"Matching RequestKey contains matched patterns and methods only"
,
expected
,
match
);
key
=
createFromPatterns
(
"/**"
,
"/foo*"
,
"/foo"
);
match
=
key
.
getMatchingRequestMappingInfo
(
request
);
expected
=
createFromPatterns
(
"/foo"
,
"/foo*"
,
"/**"
);
assertEquals
(
"Matched patterns are sorted with best match at the top"
,
expected
,
match
);
RequestMappingInfo
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo*"
,
"/bar"
),
null
,
null
,
null
,
null
,
null
,
null
);
RequestMappingInfo
expected
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo*"
),
null
,
null
,
null
,
null
,
null
,
null
);
assertEquals
(
expected
,
info
.
getMatchingInfo
(
request
));
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/**"
,
"/foo*"
,
"/foo"
),
null
,
null
,
null
,
null
,
null
,
null
);
expected
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
,
"/foo*"
,
"/**"
),
null
,
null
,
null
,
null
,
null
,
null
);
assertEquals
(
expected
,
info
.
getMatchingInfo
(
request
));
}
@Test
public
void
p
aramsCondition
()
{
public
void
matchP
aramsCondition
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo"
);
request
.
setParameter
(
"foo"
,
"bar"
);
RequestMappingInfo
key
=
RequestMappingInfo
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
null
,
new
ParamsRequestCondition
(
"foo=bar"
),
null
,
null
,
null
);
RequestMappingInfo
match
=
key
.
getMatchingRequestMapp
ingInfo
(
request
);
new
ParamsRequestCondition
(
"foo=bar"
),
null
,
null
,
null
,
null
);
RequestMappingInfo
match
=
info
.
getMatch
ingInfo
(
request
);
assertNotNull
(
match
);
key
=
new
RequestMappingInfo
(
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
null
,
new
ParamsRequestCondition
(
"foo!=bar"
),
null
,
null
,
null
);
match
=
key
.
getMatchingRequestMapp
ingInfo
(
request
);
new
ParamsRequestCondition
(
"foo!=bar"
),
null
,
null
,
null
,
null
);
match
=
info
.
getMatch
ingInfo
(
request
);
assertNull
(
match
);
}
@Test
public
void
h
eadersCondition
()
{
public
void
matchH
eadersCondition
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo"
);
request
.
addHeader
(
"foo"
,
"bar"
);
RequestMappingInfo
key
=
RequestMappingInfo
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
null
,
null
,
new
HeadersRequestCondition
(
"foo=bar"
),
null
,
null
);
RequestMappingInfo
match
=
key
.
getMatchingRequestMapp
ingInfo
(
request
);
new
HeadersRequestCondition
(
"foo=bar"
),
null
,
null
,
null
);
RequestMappingInfo
match
=
info
.
getMatch
ingInfo
(
request
);
assertNotNull
(
match
);
key
=
new
RequestMappingInfo
(
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
null
,
null
,
new
HeadersRequestCondition
(
"foo!=bar"
),
null
,
null
);
match
=
key
.
getMatchingRequestMapp
ingInfo
(
request
);
new
HeadersRequestCondition
(
"foo!=bar"
),
null
,
null
,
null
);
match
=
info
.
getMatch
ingInfo
(
request
);
assertNull
(
match
);
}
@Test
public
void
c
onsumesCondition
()
{
public
void
matchC
onsumesCondition
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo"
);
request
.
setContentType
(
"text/plain"
);
RequestMappingInfo
key
=
RequestMappingInfo
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
null
,
null
,
null
,
new
ConsumesRequestCondition
(
"text/plain"
),
null
);
RequestMappingInfo
match
=
key
.
getMatchingRequestMapp
ingInfo
(
request
);
new
ConsumesRequestCondition
(
"text/plain"
),
null
,
null
);
RequestMappingInfo
match
=
info
.
getMatch
ingInfo
(
request
);
assertNotNull
(
match
);
key
=
new
RequestMappingInfo
(
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
null
,
null
,
null
,
new
ConsumesRequestCondition
(
"application/xml"
),
null
);
match
=
key
.
getMatchingRequestMapp
ingInfo
(
request
);
new
ConsumesRequestCondition
(
"application/xml"
),
null
,
null
);
match
=
info
.
getMatch
ingInfo
(
request
);
assertNull
(
match
);
}
@Test
public
void
p
roducesCondition
()
{
public
void
matchP
roducesCondition
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo"
);
request
.
addHeader
(
"Accept"
,
"text/plain"
);
RequestMappingInfo
key
=
RequestMappingInfo
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
null
,
null
,
null
,
null
,
new
ProducesRequestCondition
(
"text/plain"
));
RequestMappingInfo
match
=
key
.
getMatchingRequestMapp
ingInfo
(
request
);
new
ProducesRequestCondition
(
"text/plain"
)
,
null
);
RequestMappingInfo
match
=
info
.
getMatch
ingInfo
(
request
);
assertNotNull
(
match
);
key
=
new
RequestMappingInfo
(
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
null
,
null
,
null
,
null
,
new
ProducesRequestCondition
(
"application/xml"
));
match
=
key
.
getMatchingRequestMappingInfo
(
request
);
new
ProducesRequestCondition
(
"application/xml"
),
null
);
match
=
info
.
getMatchingInfo
(
request
);
assertNull
(
match
);
}
@Test
public
void
matchCustomCondition
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo"
);
request
.
setParameter
(
"foo"
,
"bar"
);
RequestMappingInfo
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
null
,
null
,
null
,
null
,
null
,
new
ParamsRequestCondition
(
"foo=bar"
));
RequestMappingInfo
match
=
info
.
getMatchingInfo
(
request
);
assertNotNull
(
match
);
info
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
null
,
new
ParamsRequestCondition
(
"foo!=bar"
),
null
,
null
,
null
,
new
ParamsRequestCondition
(
"foo!=bar"
));
match
=
info
.
getMatchingInfo
(
request
);
assertNull
(
match
);
}
@Test
public
void
compareTwoHttpMethodsOneParam
()
{
RequestMappingInfo
none
=
new
RequestMappingInfo
(
null
,
null
,
null
,
null
,
null
,
null
,
null
);
RequestMappingInfo
oneMethod
=
new
RequestMappingInfo
(
null
,
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
),
null
,
null
,
null
,
null
,
null
);
RequestMappingInfo
oneMethodOneParam
=
new
RequestMappingInfo
(
null
,
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
),
new
ParamsRequestCondition
(
"foo"
),
null
,
null
,
null
,
null
);
Comparator
<
RequestMappingInfo
>
comparator
=
new
Comparator
<
RequestMappingInfo
>()
{
public
int
compare
(
RequestMappingInfo
info
,
RequestMappingInfo
otherInfo
)
{
return
info
.
compareTo
(
otherInfo
,
new
MockHttpServletRequest
());
}
};
List
<
RequestMappingInfo
>
list
=
asList
(
none
,
oneMethod
,
oneMethodOneParam
);
Collections
.
shuffle
(
list
);
Collections
.
sort
(
list
,
comparator
);
assertEquals
(
oneMethodOneParam
,
list
.
get
(
0
));
assertEquals
(
oneMethod
,
list
.
get
(
1
));
assertEquals
(
none
,
list
.
get
(
2
));
}
private
RequestMappingInfo
createFromPatterns
(
String
...
patterns
)
{
return
new
RequestMappingInfo
(
patterns
);
@Test
public
void
equals
()
{
RequestMappingInfo
info1
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
),
new
ParamsRequestCondition
(
"foo=bar"
),
new
HeadersRequestCondition
(
"foo=bar"
),
new
ConsumesRequestCondition
(
"text/plain"
),
new
ProducesRequestCondition
(
"text/plain"
),
new
ParamsRequestCondition
(
"customFoo=customBar"
));
RequestMappingInfo
info2
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
),
new
ParamsRequestCondition
(
"foo=bar"
),
new
HeadersRequestCondition
(
"foo=bar"
),
new
ConsumesRequestCondition
(
"text/plain"
),
new
ProducesRequestCondition
(
"text/plain"
),
new
ParamsRequestCondition
(
"customFoo=customBar"
));
assertEquals
(
info1
,
info2
);
assertEquals
(
info1
.
hashCode
(),
info2
.
hashCode
());
info2
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
,
"/NOOOOOO"
),
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
),
new
ParamsRequestCondition
(
"foo=bar"
),
new
HeadersRequestCondition
(
"foo=bar"
),
new
ConsumesRequestCondition
(
"text/plain"
),
new
ProducesRequestCondition
(
"text/plain"
),
new
ParamsRequestCondition
(
"customFoo=customBar"
));
assertFalse
(
info1
.
equals
(
info2
));
assertTrue
(
info1
.
hashCode
()
!=
info2
.
hashCode
());
info2
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
,
RequestMethod
.
POST
),
new
ParamsRequestCondition
(
"foo=bar"
),
new
HeadersRequestCondition
(
"foo=bar"
),
new
ConsumesRequestCondition
(
"text/plain"
),
new
ProducesRequestCondition
(
"text/plain"
),
new
ParamsRequestCondition
(
"customFoo=customBar"
));
assertFalse
(
info1
.
equals
(
info2
));
assertTrue
(
info1
.
hashCode
()
!=
info2
.
hashCode
());
info2
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
),
new
ParamsRequestCondition
(
"/NOOOOOO"
),
new
HeadersRequestCondition
(
"foo=bar"
),
new
ConsumesRequestCondition
(
"text/plain"
),
new
ProducesRequestCondition
(
"text/plain"
),
new
ParamsRequestCondition
(
"customFoo=customBar"
));
assertFalse
(
info1
.
equals
(
info2
));
assertTrue
(
info1
.
hashCode
()
!=
info2
.
hashCode
());
info2
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
),
new
ParamsRequestCondition
(
"foo=bar"
),
new
HeadersRequestCondition
(
"/NOOOOOO"
),
new
ConsumesRequestCondition
(
"text/plain"
),
new
ProducesRequestCondition
(
"text/plain"
),
new
ParamsRequestCondition
(
"customFoo=customBar"
));
assertFalse
(
info1
.
equals
(
info2
));
assertTrue
(
info1
.
hashCode
()
!=
info2
.
hashCode
());
info2
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
),
new
ParamsRequestCondition
(
"foo=bar"
),
new
HeadersRequestCondition
(
"foo=bar"
),
new
ConsumesRequestCondition
(
"text/NOOOOOO"
),
new
ProducesRequestCondition
(
"text/plain"
),
new
ParamsRequestCondition
(
"customFoo=customBar"
));
assertFalse
(
info1
.
equals
(
info2
));
assertTrue
(
info1
.
hashCode
()
!=
info2
.
hashCode
());
info2
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
),
new
ParamsRequestCondition
(
"foo=bar"
),
new
HeadersRequestCondition
(
"foo=bar"
),
new
ConsumesRequestCondition
(
"text/plain"
),
new
ProducesRequestCondition
(
"text/NOOOOOO"
),
new
ParamsRequestCondition
(
"customFoo=customBar"
));
assertFalse
(
info1
.
equals
(
info2
));
assertTrue
(
info1
.
hashCode
()
!=
info2
.
hashCode
());
info2
=
new
RequestMappingInfo
(
new
PatternsRequestCondition
(
"/foo"
),
new
RequestMethodsRequestCondition
(
RequestMethod
.
GET
),
new
ParamsRequestCondition
(
"foo=bar"
),
new
HeadersRequestCondition
(
"foo=bar"
),
new
ConsumesRequestCondition
(
"text/plain"
),
new
ProducesRequestCondition
(
"text/plain"
),
new
ParamsRequestCondition
(
"customFoo=NOOOOOO"
));
assertFalse
(
info1
.
equals
(
info2
));
assertTrue
(
info1
.
hashCode
()
!=
info2
.
hashCode
());
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录