Extract base class from RequestMappingHandlerMapping, one that allows for discovering request mappings from something other than annotations

This commit is contained in:
Rossen Stoyanchev
2011-06-14 09:20:07 +00:00
parent b2be59c2e4
commit fa0b683161
7 changed files with 285 additions and 212 deletions

View File

@@ -14,8 +14,9 @@
* limitations under the License.
*/
package org.springframework.web.servlet.mvc.method.annotation;
package org.springframework.web.servlet.mvc.method;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@@ -25,6 +26,8 @@ import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.mvc.method.condition.RequestConditionFactory;
import org.springframework.web.util.UrlPathHelper;
@@ -39,13 +42,13 @@ import static org.junit.Assert.*;
*/
public class RequestMappingInfoComparatorTests {
private RequestMappingHandlerMapping handlerMapping;
private TestRequestMappingInfoHandlerMapping handlerMapping;
private MockHttpServletRequest request;
@Before
public void setup() {
this.handlerMapping = new RequestMappingHandlerMapping();
this.handlerMapping = new TestRequestMappingInfoHandlerMapping();
this.request = new MockHttpServletRequest();
}
@@ -148,5 +151,18 @@ public class RequestMappingInfoComparatorTests {
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;
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.web.servlet.mvc.method.annotation;
package org.springframework.web.servlet.mvc.method;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
@@ -23,12 +23,14 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestMethodNotSupportedException;
@@ -40,6 +42,9 @@ import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.handler.MappedInterceptor;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.mvc.method.condition.RequestConditionFactory;
import org.springframework.web.util.UrlPathHelper;
/**
@@ -48,9 +53,9 @@ import org.springframework.web.util.UrlPathHelper;
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
public class RequestMappingHandlerMappingTests {
public class RequestMappingInfoHandlerMappingTests {
private RequestMappingHandlerMapping mapping;
private TestRequestMappingInfoHandlerMapping mapping;
private Handler handler;
@@ -73,7 +78,7 @@ public class RequestMappingHandlerMappingTests {
StaticApplicationContext context = new StaticApplicationContext();
context.registerSingleton("handler", handler.getClass());
mapping = new RequestMappingHandlerMapping();
mapping = new TestRequestMappingInfoHandlerMapping();
mapping.setApplicationContext(context);
}
@@ -148,7 +153,7 @@ public class RequestMappingHandlerMappingTests {
StaticApplicationContext context = new StaticApplicationContext();
context.registerSingleton("handler", handler.getClass());
mapping = new RequestMappingHandlerMapping();
mapping = new TestRequestMappingInfoHandlerMapping();
mapping.setInterceptors(new Object[] { mappedInterceptor });
mapping.setApplicationContext(context);
@@ -163,7 +168,6 @@ public class RequestMappingHandlerMappingTests {
@SuppressWarnings("unused")
@Controller
@RequestMapping
private static class Handler {
@RequestMapping(value = "/foo", method = RequestMethod.GET)
@@ -182,5 +186,24 @@ public class RequestMappingHandlerMappingTests {
public void empty() {
}
}
private static class TestRequestMappingInfoHandlerMapping extends RequestMappingInfoHandlerMapping {
@Override
protected boolean isHandler(Class<?> beanType) {
return AnnotationUtils.findAnnotation(beanType, Controller.class) != null;
}
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMapping annotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
return new RequestMappingInfo(Arrays.asList(annotation.value()),
RequestConditionFactory.parseMethods(annotation.method()),
RequestConditionFactory.parseParams(annotation.params()),
RequestConditionFactory.parseHeaders(annotation.headers()),
RequestConditionFactory.parseConsumes(annotation.consumes(), annotation.headers()),
RequestConditionFactory.parseProduces(annotation.produces(), annotation.headers()));
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.web.servlet.mvc.method.annotation;
package org.springframework.web.servlet.mvc.method;
import org.junit.Test;
@@ -22,6 +22,7 @@ import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.condition.RequestConditionFactory;
import org.springframework.web.util.UrlPathHelper;