Improve MappingRegistry tests and polish

Issue: SPR-11541
This commit is contained in:
Rossen Stoyanchev
2015-05-05 10:49:01 -04:00
parent 16b189cc1a
commit 4a8baebf59
2 changed files with 150 additions and 72 deletions

View File

@@ -19,8 +19,9 @@ package org.springframework.web.servlet.handler;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
@@ -34,14 +35,17 @@ import org.springframework.stereotype.Controller;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.util.UrlPathHelper;
/**
* Test for {@link AbstractHandlerMethodMapping}.
*
* @author Arjen Poutsma
*/
@SuppressWarnings("unused")
public class HandlerMethodMappingTests {
private AbstractHandlerMethodMapping<String> mapping;
@@ -52,44 +56,46 @@ public class HandlerMethodMappingTests {
private Method method2;
@Before
public void setUp() throws Exception {
mapping = new MyHandlerMethodMapping();
handler = new MyHandler();
method1 = handler.getClass().getMethod("handlerMethod1");
method2 = handler.getClass().getMethod("handlerMethod2");
this.mapping = new MyHandlerMethodMapping();
this.handler = new MyHandler();
this.method1 = handler.getClass().getMethod("handlerMethod1");
this.method2 = handler.getClass().getMethod("handlerMethod2");
}
@Test(expected = IllegalStateException.class)
public void registerDuplicates() {
mapping.registerMapping("foo", handler, method1);
mapping.registerMapping("foo", handler, method2);
this.mapping.registerMapping("foo", this.handler, this.method1);
this.mapping.registerMapping("foo", this.handler, this.method2);
}
@Test
public void directMatch() throws Exception {
String key = "foo";
mapping.registerMapping(key, handler, method1);
this.mapping.registerMapping(key, this.handler, this.method1);
HandlerMethod result = mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
HandlerMethod result = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
assertEquals(method1, result.getMethod());
}
@Test
public void patternMatch() throws Exception {
mapping.registerMapping("/fo*", handler, method1);
mapping.registerMapping("/f*", handler, method2);
this.mapping.registerMapping("/fo*", this.handler, this.method1);
this.mapping.registerMapping("/f*", this.handler, this.method2);
HandlerMethod result = mapping.getHandlerInternal(new MockHttpServletRequest("GET", "/foo"));
HandlerMethod result = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", "/foo"));
assertEquals(method1, result.getMethod());
}
@Test(expected = IllegalStateException.class)
public void ambiguousMatch() throws Exception {
mapping.registerMapping("/f?o", handler, method1);
mapping.registerMapping("/fo?", handler, method2);
this.mapping.registerMapping("/f?o", this.handler, this.method1);
this.mapping.registerMapping("/fo?", this.handler, this.method2);
mapping.getHandlerInternal(new MockHttpServletRequest("GET", "/foo"));
this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", "/foo"));
}
@Test
@@ -112,15 +118,62 @@ public class HandlerMethodMappingTests {
}
@Test
public void unregister() throws Exception {
public void registerMapping() throws Exception {
String key1 = "/foo";
String key2 = "/foo*";
this.mapping.registerMapping(key1, this.handler, this.method1);
this.mapping.registerMapping(key2, this.handler, this.method2);
// Direct URL lookup
List directUrlMatches = this.mapping.getMappingRegistry().getMappingsByUrl(key1);
assertNotNull(directUrlMatches);
assertEquals(1, directUrlMatches.size());
assertEquals(key1, directUrlMatches.get(0));
// Mapping name lookup
HandlerMethod handlerMethod1 = new HandlerMethod(this.handler, this.method1);
HandlerMethod handlerMethod2 = new HandlerMethod(this.handler, this.method2);
String name1 = this.method1.getName();
List<HandlerMethod> handlerMethods = this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(name1);
assertNotNull(handlerMethods);
assertEquals(1, handlerMethods.size());
assertEquals(handlerMethod1, handlerMethods.get(0));
String name2 = this.method2.getName();
handlerMethods = this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(name2);
assertNotNull(handlerMethods);
assertEquals(1, handlerMethods.size());
assertEquals(handlerMethod2, handlerMethods.get(0));
// CORS lookup
CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod1);
assertNotNull(config);
assertEquals("http://" + name1, config.getAllowedOrigins().get(0));
config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod2);
assertNotNull(config);
assertEquals("http://" + name2, config.getAllowedOrigins().get(0));
}
@Test
public void unregisterMapping() throws Exception {
String key = "foo";
mapping.registerMapping(key, handler, method1);
HandlerMethod handlerMethod = new HandlerMethod(this.handler, this.method1);
HandlerMethod handlerMethod = mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
assertEquals(method1, handlerMethod.getMethod());
this.mapping.registerMapping(key, this.handler, this.method1);
assertNotNull(this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key)));
mapping.unregisterMapping(key);
this.mapping.unregisterMapping(key);
assertNull(mapping.getHandlerInternal(new MockHttpServletRequest("GET", key)));
assertNull(this.mapping.getMappingRegistry().getMappingsByUrl(key));
assertNull(this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(this.method1.getName()));
assertNull(this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod));
}
@@ -130,10 +183,14 @@ public class HandlerMethodMappingTests {
private PathMatcher pathMatcher = new AntPathMatcher();
public MyHandlerMethodMapping() {
setHandlerMethodMappingNamingStrategy(new SimpleMappingNamingStrategy());
}
@Override
protected String getMatchingMapping(String pattern, HttpServletRequest request) {
String lookupPath = pathHelper.getLookupPathForRequest(request);
return pathMatcher.match(pattern, lookupPath) ? pattern : null;
protected boolean isHandler(Class<?> beanType) {
return true;
}
@Override
@@ -142,24 +199,40 @@ public class HandlerMethodMappingTests {
return methodName.startsWith("handler") ? methodName : null;
}
@Override
protected Comparator<String> getMappingComparator(HttpServletRequest request) {
String lookupPath = pathHelper.getLookupPathForRequest(request);
return pathMatcher.getPatternComparator(lookupPath);
}
@Override
protected boolean isHandler(Class<?> beanType) {
return true;
}
@Override
protected Set<String> getMappingPathPatterns(String key) {
return new HashSet<String>();
return (this.pathMatcher.isPattern(key) ? Collections.<String>emptySet() : Collections.singleton(key));
}
@Override
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, String mapping) {
CorsConfiguration corsConfig = new CorsConfiguration();
corsConfig.setAllowedOrigins(Collections.singletonList("http://" + method.getName()));
return corsConfig;
}
@Override
protected String getMatchingMapping(String pattern, HttpServletRequest request) {
String lookupPath = this.pathHelper.getLookupPathForRequest(request);
return this.pathMatcher.match(pattern, lookupPath) ? pattern : null;
}
@Override
protected Comparator<String> getMappingComparator(HttpServletRequest request) {
String lookupPath = this.pathHelper.getLookupPathForRequest(request);
return this.pathMatcher.getPatternComparator(lookupPath);
}
}
private static class SimpleMappingNamingStrategy implements HandlerMethodMappingNamingStrategy<String> {
@Override
public String getName(HandlerMethod handlerMethod, String mapping) {
return handlerMethod.getMethod().getName();
}
}
@SuppressWarnings("unused")
@Controller
static class MyHandler {