SPR-8247 review changes

This commit is contained in:
Rossen Stoyanchev
2011-04-21 11:40:24 +00:00
parent ab654a7a06
commit acb9433e5c
8 changed files with 198 additions and 211 deletions

View File

@@ -90,18 +90,18 @@ public class HandlerMethodMappingTests {
private PathMatcher pathMatcher = new AntPathMatcher();
@Override
protected String getMatchingMappingKey(String pattern, String lookupPath, HttpServletRequest request) {
protected String getMatchingMapping(String pattern, String lookupPath, HttpServletRequest request) {
return pathMatcher.match(pattern, lookupPath) ? pattern : null;
}
@Override
protected String getMappingKeyForMethod(String beanName, Method method) {
protected String getMappingForMethod(String beanName, Method method) {
String methodName = method.getName();
return methodName.startsWith("handler") ? methodName : null;
}
@Override
protected Comparator<String> getMappingKeyComparator(String lookupPath, HttpServletRequest request) {
protected Comparator<String> getMappingComparator(String lookupPath, HttpServletRequest request) {
return pathMatcher.getPatternComparator(lookupPath);
}

View File

@@ -33,7 +33,7 @@ import static java.util.Arrays.*;
import static org.junit.Assert.*;
/**
* Test fixture with {@link RequestMappingHandlerMethodMapping} testing its {@link RequestMappingKey} comparator.
* Test fixture with {@link RequestMappingHandlerMethodMapping} testing its {@link RequestMappingInfo} comparator.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
@@ -54,9 +54,9 @@ public class RequestKeyComparatorTests {
public void moreSpecificPatternWins() {
request.setRequestURI("/foo");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
Comparator<RequestMappingKey> comparator = handlerMapping.getMappingKeyComparator(lookupPath, request);
RequestMappingKey key1 = new RequestMappingKey(asList("/fo*"), null);
RequestMappingKey key2 = new RequestMappingKey(asList("/foo"), null);
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(lookupPath, request);
RequestMappingInfo key1 = new RequestMappingInfo(asList("/fo*"), null);
RequestMappingInfo key2 = new RequestMappingInfo(asList("/foo"), null);
assertEquals(1, comparator.compare(key1, key2));
}
@@ -65,9 +65,9 @@ public class RequestKeyComparatorTests {
public void equalPatterns() {
request.setRequestURI("/foo");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
Comparator<RequestMappingKey> comparator = handlerMapping.getMappingKeyComparator(lookupPath, request);
RequestMappingKey key1 = new RequestMappingKey(asList("/foo*"), null);
RequestMappingKey key2 = new RequestMappingKey(asList("/foo*"), null);
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator(lookupPath, request);
RequestMappingInfo key1 = new RequestMappingInfo(asList("/foo*"), null);
RequestMappingInfo key2 = new RequestMappingInfo(asList("/foo*"), null);
assertEquals(0, comparator.compare(key1, key2));
}
@@ -76,34 +76,34 @@ public class RequestKeyComparatorTests {
public void greaterNumberOfMatchingPatternsWins() throws Exception {
request.setRequestURI("/foo.html");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
RequestMappingKey key1 = new RequestMappingKey(asList("/foo", "*.jpeg"), null);
RequestMappingKey key2 = new RequestMappingKey(asList("/foo", "*.html"), null);
RequestMappingKey match1 = handlerMapping.getMatchingMappingKey(key1, lookupPath, request);
RequestMappingKey match2 = handlerMapping.getMatchingMappingKey(key2, lookupPath, request);
List<RequestMappingKey> matches = asList(match1, match2);
Collections.sort(matches, handlerMapping.getMappingKeyComparator(lookupPath, request));
RequestMappingInfo key1 = new RequestMappingInfo(asList("/foo", "*.jpeg"), null);
RequestMappingInfo key2 = new RequestMappingInfo(asList("/foo", "*.html"), null);
RequestMappingInfo match1 = handlerMapping.getMatchingMapping(key1, lookupPath, request);
RequestMappingInfo match2 = handlerMapping.getMatchingMapping(key2, lookupPath, request);
List<RequestMappingInfo> matches = asList(match1, match2);
Collections.sort(matches, handlerMapping.getMappingComparator(lookupPath, request));
assertSame(match2.getPatterns(), matches.get(0).getPatterns());
}
@Test
public void oneMethodWinsOverNone() {
Comparator<RequestMappingKey> comparator = handlerMapping.getMappingKeyComparator("", request);
RequestMappingKey key1 = new RequestMappingKey(null, null);
RequestMappingKey key2 = new RequestMappingKey(null, asList(RequestMethod.GET));
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator("", request);
RequestMappingInfo key1 = new RequestMappingInfo(null, null);
RequestMappingInfo key2 = new RequestMappingInfo(null, asList(RequestMethod.GET));
assertEquals(1, comparator.compare(key1, key2));
}
@Test
public void methodsAndParams() {
RequestMappingKey empty = new RequestMappingKey(null, null);
RequestMappingKey oneMethod = new RequestMappingKey(null, asList(RequestMethod.GET));
RequestMappingKey oneMethodOneParam =
new RequestMappingKey(null, asList(RequestMethod.GET), RequestConditionFactory.parseParams("foo"), null, null);
List<RequestMappingKey> list = asList(empty, oneMethod, oneMethodOneParam);
RequestMappingInfo empty = new RequestMappingInfo(null, null);
RequestMappingInfo oneMethod = new RequestMappingInfo(null, asList(RequestMethod.GET));
RequestMappingInfo oneMethodOneParam =
new RequestMappingInfo(null, asList(RequestMethod.GET), RequestConditionFactory.parseParams("foo"), null);
List<RequestMappingInfo> list = asList(empty, oneMethod, oneMethodOneParam);
Collections.shuffle(list);
Collections.sort(list, handlerMapping.getMappingKeyComparator("", request));
Collections.sort(list, handlerMapping.getMappingComparator("", request));
assertEquals(oneMethodOneParam, list.get(0));
assertEquals(oneMethod, list.get(1));
@@ -113,12 +113,12 @@ public class RequestKeyComparatorTests {
@Test
@Ignore // TODO : remove ignore
public void acceptHeaders() {
RequestMappingKey html = new RequestMappingKey(null, null, null, RequestConditionFactory.parseHeaders("accept=text/html"), null);
RequestMappingKey xml = new RequestMappingKey(null, null, null, RequestConditionFactory.parseHeaders("accept=application/xml"), null);
RequestMappingKey none = new RequestMappingKey(null, null);
RequestMappingInfo html = new RequestMappingInfo(null, null, null, RequestConditionFactory.parseHeaders("accept=text/html"));
RequestMappingInfo xml = new RequestMappingInfo(null, null, null, RequestConditionFactory.parseHeaders("accept=application/xml"));
RequestMappingInfo none = new RequestMappingInfo(null, null);
request.addHeader("Accept", "application/xml, text/html");
Comparator<RequestMappingKey> comparator = handlerMapping.getMappingKeyComparator("", request);
Comparator<RequestMappingInfo> comparator = handlerMapping.getMappingComparator("", request);
assertTrue(comparator.compare(html, xml) > 0);
assertTrue(comparator.compare(xml, html) < 0);
@@ -129,14 +129,14 @@ public class RequestKeyComparatorTests {
request = new MockHttpServletRequest();
request.addHeader("Accept", "application/xml, text/*");
comparator = handlerMapping.getMappingKeyComparator("", request);
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.getMappingKeyComparator("", request);
comparator = handlerMapping.getMappingComparator("", request);
assertTrue(comparator.compare(html, xml) == 0);
assertTrue(comparator.compare(xml, html) == 0);
@@ -144,7 +144,7 @@ public class RequestKeyComparatorTests {
// See SPR-7000
request = new MockHttpServletRequest();
request.addHeader("Accept", "text/html;q=0.9,application/xml");
comparator = handlerMapping.getMappingKeyComparator("", request);
comparator = handlerMapping.getMappingComparator("", request);
assertTrue(comparator.compare(html, xml) > 0);
assertTrue(comparator.compare(xml, html) < 0);

View File

@@ -32,7 +32,7 @@ import org.springframework.web.servlet.mvc.method.condition.RequestConditionFact
import org.springframework.web.util.UrlPathHelper;
/**
* Test fixture for {@link RequestMappingKey} tests.
* Test fixture for {@link RequestMappingInfo} tests.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
@@ -41,8 +41,8 @@ public class RequestKeyTests {
@Test
public void equals() {
RequestMappingKey key1 = new RequestMappingKey(singleton("/foo"), singleton(GET));
RequestMappingKey key2 = new RequestMappingKey(singleton("/foo"), singleton(GET));
RequestMappingInfo key1 = new RequestMappingInfo(singleton("/foo"), singleton(GET));
RequestMappingInfo key2 = new RequestMappingInfo(singleton("/foo"), singleton(GET));
assertEquals(key1, key2);
assertEquals(key1.hashCode(), key2.hashCode());
@@ -50,8 +50,8 @@ public class RequestKeyTests {
@Test
public void equalsPrependSlash() {
RequestMappingKey key1 = new RequestMappingKey(singleton("/foo"), singleton(GET));
RequestMappingKey key2 = new RequestMappingKey(singleton("foo"), singleton(GET));
RequestMappingInfo key1 = new RequestMappingInfo(singleton("/foo"), singleton(GET));
RequestMappingInfo key2 = new RequestMappingInfo(singleton("foo"), singleton(GET));
assertEquals(key1, key2);
assertEquals(key1.hashCode(), key2.hashCode());
@@ -61,9 +61,9 @@ public class RequestKeyTests {
public void combinePatterns() {
AntPathMatcher pathMatcher = new AntPathMatcher();
RequestMappingKey key1 = createKeyFromPatterns("/t1", "/t2");
RequestMappingKey key2 = createKeyFromPatterns("/m1", "/m2");
RequestMappingKey key3 = createKeyFromPatterns("/t1/m1", "/t1/m2", "/t2/m1", "/t2/m2");
RequestMappingInfo key1 = createKeyFromPatterns("/t1", "/t2");
RequestMappingInfo key2 = createKeyFromPatterns("/m1", "/m2");
RequestMappingInfo key3 = createKeyFromPatterns("/t1/m1", "/t1/m2", "/t2/m1", "/t2/m2");
assertEquals(key3.getPatterns(), key1.combine(key2, pathMatcher).getPatterns());
key1 = createKeyFromPatterns("/t1");
@@ -93,40 +93,40 @@ public class RequestKeyTests {
PathMatcher pathMatcher = new AntPathMatcher();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
RequestMappingKey key = new RequestMappingKey(singleton("/foo"), null);
RequestMappingKey match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null);
RequestMappingInfo match = key.getMatchingRequestMapping(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
assertNotNull(match);
request = new MockHttpServletRequest("GET", "/foo/bar");
key = new RequestMappingKey(singleton("/foo/*"), null);
match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
key = new RequestMappingInfo(singleton("/foo/*"), null);
match = key.getMatchingRequestMapping(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
assertNotNull("Pattern match", match);
request = new MockHttpServletRequest("GET", "/foo.html");
key = new RequestMappingKey(singleton("/foo"), null);
match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
key = new RequestMappingInfo(singleton("/foo"), null);
match = key.getMatchingRequestMapping(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
assertNotNull("Implicit match by extension", match);
assertEquals("Contains matched pattern", "/foo.*", match.getPatterns().iterator().next());
request = new MockHttpServletRequest("GET", "/foo/");
key = new RequestMappingKey(singleton("/foo"), null);
match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
key = new RequestMappingInfo(singleton("/foo"), null);
match = key.getMatchingRequestMapping(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
assertNotNull("Implicit match by trailing slash", match);
assertEquals("Contains matched pattern", "/foo/", match.getPatterns().iterator().next());
request = new MockHttpServletRequest("GET", "/foo.html");
key = new RequestMappingKey(singleton("/foo.jpg"), null);
match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
key = new RequestMappingInfo(singleton("/foo.jpg"), null);
match = key.getMatchingRequestMapping(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
assertNull("Implicit match ignored if pattern has extension", match);
request = new MockHttpServletRequest("GET", "/foo.html");
key = new RequestMappingKey(singleton("/foo.jpg"), null);
match = key.getMatchingKey(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
key = new RequestMappingInfo(singleton("/foo.jpg"), null);
match = key.getMatchingRequestMapping(pathHelper.getLookupPathForRequest(request), request, pathMatcher);
assertNull("Implicit match ignored on pattern with trailing slash", match);
}
@@ -137,18 +137,18 @@ public class RequestKeyTests {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
RequestMappingKey key = new RequestMappingKey(singleton("/foo"), null);
RequestMappingKey match = key.getMatchingKey(lookupPath, request, pathMatcher);
RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null);
RequestMappingInfo match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNotNull("No method matches any method", match);
key = new RequestMappingKey(singleton("/foo"), singleton(GET));
match = key.getMatchingKey(lookupPath, request, pathMatcher);
key = new RequestMappingInfo(singleton("/foo"), singleton(GET));
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNotNull("Exact match", match);
key = new RequestMappingKey(singleton("/foo"), singleton(POST));
match = key.getMatchingKey(lookupPath, request, pathMatcher);
key = new RequestMappingInfo(singleton("/foo"), singleton(POST));
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNull("No match", match);
}
@@ -159,15 +159,15 @@ public class RequestKeyTests {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
RequestMappingKey key = new RequestMappingKey(asList("/foo*", "/bar"), asList(GET, POST));
RequestMappingKey match = key.getMatchingKey(lookupPath, request, pathMatcher);
RequestMappingKey expected = new RequestMappingKey(singleton("/foo*"), singleton(GET));
RequestMappingInfo key = new RequestMappingInfo(asList("/foo*", "/bar"), asList(GET, POST));
RequestMappingInfo match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
RequestMappingInfo expected = new RequestMappingInfo(singleton("/foo*"), singleton(GET));
assertEquals("Matching RequestKey contains matched patterns and methods only", expected, match);
key = new RequestMappingKey(asList("/**", "/foo*", "/foo"), null);
match = key.getMatchingKey(lookupPath, request, pathMatcher);
expected = new RequestMappingKey(asList("/foo", "/foo*", "/**"), null);
key = new RequestMappingInfo(asList("/**", "/foo*", "/foo"), null);
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
expected = new RequestMappingInfo(asList("/foo", "/foo*", "/**"), null);
assertEquals("Matched patterns are sorted with best match at the top", expected, match);
}
@@ -179,13 +179,13 @@ public class RequestKeyTests {
request.setParameter("foo", "bar");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
RequestMappingKey key = new RequestMappingKey(asList("/foo"), null, RequestConditionFactory.parseParams("foo=bar"), null, null);
RequestMappingKey match = key.getMatchingKey(lookupPath, request, pathMatcher);
RequestMappingInfo key = new RequestMappingInfo(asList("/foo"), null, RequestConditionFactory.parseParams("foo=bar"), null);
RequestMappingInfo match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNotNull(match);
key = new RequestMappingKey(singleton("/foo"), null, RequestConditionFactory.parseParams("foo!=bar"), null, null);
match = key.getMatchingKey(lookupPath, request, pathMatcher);
key = new RequestMappingInfo(singleton("/foo"), null, RequestConditionFactory.parseParams("foo!=bar"), null);
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNull(match);
}
@@ -197,39 +197,39 @@ public class RequestKeyTests {
request.addHeader("foo", "bar");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
RequestMappingKey key = new RequestMappingKey(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo=bar"), null);
RequestMappingKey match = key.getMatchingKey(lookupPath, request, pathMatcher);
RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo=bar"));
RequestMappingInfo match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNotNull(match);
key = new RequestMappingKey(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo!=bar"), null);
match = key.getMatchingKey(lookupPath, request, pathMatcher);
key = new RequestMappingInfo(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo!=bar"));
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNull(match);
}
@Test
public void consumesCondition() {
PathMatcher pathMatcher = new AntPathMatcher();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.setContentType("text/plain");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
// @Test
// public void consumesCondition() {
// PathMatcher pathMatcher = new AntPathMatcher();
// MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
// request.setContentType("text/plain");
// String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
//
// RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
// "text/plain"));
// RequestMappingInfo match = key.getMatchingKey(lookupPath, request, pathMatcher);
//
// assertNotNull(match);
//
// key = new RequestMappingInfo(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
// "application/xml"));
// match = key.getMatchingKey(lookupPath, request, pathMatcher);
//
// assertNull(match);
// }
RequestMappingKey key = new RequestMappingKey(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
"text/plain"));
RequestMappingKey match = key.getMatchingKey(lookupPath, request, pathMatcher);
assertNotNull(match);
key = new RequestMappingKey(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
"application/xml"));
match = key.getMatchingKey(lookupPath, request, pathMatcher);
assertNull(match);
}
private RequestMappingKey createKeyFromPatterns(String... patterns) {
return new RequestMappingKey(asList(patterns), null);
private RequestMappingInfo createKeyFromPatterns(String... patterns) {
return new RequestMappingInfo(asList(patterns), null);
}
}

View File

@@ -124,7 +124,7 @@ public class RequestMappingHandlerMethodMappingTests {
@Test
public void uriTemplateVariables() {
RequestMappingKey key = new RequestMappingKey(Arrays.asList("/{path1}/{path2}"), null);
RequestMappingInfo key = new RequestMappingInfo(Arrays.asList("/{path1}/{path2}"), null);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);