Moved tests from testsuite to web.servlet

This commit is contained in:
Arjen Poutsma
2008-11-03 11:51:55 +00:00
parent e31ab5a09b
commit 74733d0d1d
9 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,192 @@
/*
* Copyright 2002-2007 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.handler;
import javax.servlet.ServletException;
import junit.framework.TestCase;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
/**
* @author Rod Johnson
* @author Juergen Hoeller
*/
public class BeanNameUrlHandlerMappingTests extends TestCase {
public static final String CONF = "/org/springframework/web/servlet/handler/map1.xml";
private ConfigurableWebApplicationContext wac;
public void setUp() throws Exception {
MockServletContext sc = new MockServletContext("");
wac = new XmlWebApplicationContext();
wac.setServletContext(sc);
wac.setConfigLocations(new String[] {CONF});
wac.refresh();
}
public void testRequestsWithoutHandlers() throws Exception {
HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/nonsense.html");
req.setContextPath("/myapp");
Object h = hm.getHandler(req);
assertTrue("Handler is null", h == null);
req = new MockHttpServletRequest("GET", "/foo/bar/baz.html");
h = hm.getHandler(req);
assertTrue("Handler is null", h == null);
}
public void testRequestsWithSubPaths() throws Exception {
HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
doTestRequestsWithSubPaths(hm);
}
public void testRequestsWithSubPathsInParentContext() throws Exception {
BeanNameUrlHandlerMapping hm = new BeanNameUrlHandlerMapping();
hm.setDetectHandlersInAncestorContexts(true);
hm.setApplicationContext(new StaticApplicationContext(wac));
doTestRequestsWithSubPaths(hm);
}
private void doTestRequestsWithSubPaths(HandlerMapping hm) throws Exception {
Object bean = wac.getBean("godCtrl");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/welcome.html");
HandlerExecutionChain hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/myapp/mypath/welcome.html");
req.setContextPath("/myapp");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/myapp/mypath/welcome.html");
req.setContextPath("/myapp");
req.setServletPath("/mypath/welcome.html");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/myapp/myservlet/mypath/welcome.html");
req.setContextPath("/myapp");
req.setServletPath("/myservlet");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/myapp/myapp/mypath/welcome.html");
req.setContextPath("/myapp");
req.setServletPath("/myapp");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/mypath/show.html");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/mypath/bookseats.html");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
}
public void testRequestsWithFullPaths() throws Exception {
BeanNameUrlHandlerMapping hm = new BeanNameUrlHandlerMapping();
hm.setAlwaysUseFullPath(true);
hm.setApplicationContext(wac);
Object bean = wac.getBean("godCtrl");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/welcome.html");
HandlerExecutionChain hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/myapp/mypath/welcome.html");
req.setContextPath("/myapp");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/mypath/welcome.html");
req.setContextPath("");
req.setServletPath("/mypath");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/Myapp/mypath/welcome.html");
req.setContextPath("/myapp");
req.setServletPath("/mypath");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
}
public void testAsteriskMatches() throws Exception {
HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
Object bean = wac.getBean("godCtrl");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html");
HandlerExecutionChain hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/mypath/testarossa");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/mypath/tes");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec == null);
}
public void testOverlappingMappings() throws Exception {
BeanNameUrlHandlerMapping hm = (BeanNameUrlHandlerMapping) wac.getBean("handlerMapping");
Object anotherHandler = new Object();
hm.registerHandler("/mypath/testaross*", anotherHandler);
Object bean = wac.getBean("godCtrl");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html");
HandlerExecutionChain hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/mypath/testarossa");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == anotherHandler);
req = new MockHttpServletRequest("GET", "/mypath/tes");
hec = hm.getHandler(req);
assertTrue("Handler is correct bean", hec == null);
}
public void testDoubleMappings() throws ServletException {
BeanNameUrlHandlerMapping hm = (BeanNameUrlHandlerMapping) wac.getBean("handlerMapping");
try {
hm.registerHandler("/mypath/welcome.html", new Object());
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
}
}

View File

@@ -0,0 +1,251 @@
/*
* Copyright 2002-2007 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.handler;
import junit.framework.TestCase;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
/**
* @author Alef Arendsen
* @author Juergen Hoeller
*/
public class PathMatchingUrlHandlerMappingTests extends TestCase {
public static final String CONF = "/org/springframework/web/servlet/handler/map3.xml";
private HandlerMapping hm;
private ConfigurableWebApplicationContext wac;
public void setUp() throws Exception {
MockServletContext sc = new MockServletContext("");
wac = new XmlWebApplicationContext();
wac.setServletContext(sc);
wac.setConfigLocations(new String[] {CONF});
wac.refresh();
hm = (HandlerMapping) wac.getBean("urlMapping");
}
public void testRequestsWithHandlers() throws Exception {
Object bean = wac.getBean("mainController");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html");
HandlerExecutionChain hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/show.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/bookseats.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
}
public void testActualPathMatching() throws Exception {
// there a couple of mappings defined with which we can test the
// path matching, let's do that...
Object bean = wac.getBean("mainController");
Object defaultBean = wac.getBean("starController");
// testing some normal behavior
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/pathmatchingTest.html");
HandlerExecutionChain hec = getHandler(req);
assertTrue("Handler is null", hec != null);
assertTrue("Handler is correct bean", hec.getHandler() == bean);
assertEquals("pathmatchingTest.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
// no match, no forward slash included
req = new MockHttpServletRequest("GET", "welcome.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
assertEquals("welcome.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
// testing some ????? behavior
req = new MockHttpServletRequest("GET", "/pathmatchingAA.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
assertEquals("pathmatchingAA.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
// testing some ????? behavior
req = new MockHttpServletRequest("GET", "/pathmatchingA.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
assertEquals("/pathmatchingA.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
// testing some ????? behavior
req = new MockHttpServletRequest("GET", "/administrator/pathmatching.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
// testing simple /**/behavior
req = new MockHttpServletRequest("GET", "/administrator/test/pathmatching.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
// this should not match because of the administratorT
req = new MockHttpServletRequest("GET", "/administratort/pathmatching.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
// this should match because of *.jsp
req = new MockHttpServletRequest("GET", "/bla.jsp");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
// this as well, because there's a **/in there as well
req = new MockHttpServletRequest("GET", "/testing/bla.jsp");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
// should match because because exact pattern is there
req = new MockHttpServletRequest("GET", "/administrator/another/bla.xml");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
// should not match, because there's not .gif extension in there
req = new MockHttpServletRequest("GET", "/administrator/another/bla.gif");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
// should match because there testlast* in there
req = new MockHttpServletRequest("GET", "/administrator/test/testlastbit");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
// but this not, because it's testlast and not testla
req = new MockHttpServletRequest("GET", "/administrator/test/testla");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
req = new MockHttpServletRequest("GET", "/administrator/testing/longer/bla");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/administrator/testing/longer/test.jsp");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/administrator/testing/longer2/notmatching/notmatching");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
req = new MockHttpServletRequest("GET", "/shortpattern/testing/toolong");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
req = new MockHttpServletRequest("GET", "/XXpathXXmatching.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/pathXXmatching.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/XpathXXmatching.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
req = new MockHttpServletRequest("GET", "/XXpathmatching.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
req = new MockHttpServletRequest("GET", "/show12.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/show123.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/show1.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/reallyGood-test-is-this.jpeg");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/reallyGood-tst-is-this.jpeg");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
req = new MockHttpServletRequest("GET", "/testing/test.jpeg");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/testing/test.jpg");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
req = new MockHttpServletRequest("GET", "/anotherTest");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/stillAnotherTest");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
// there outofpattern*yeah in the pattern, so this should fail
req = new MockHttpServletRequest("GET", "/outofpattern*ye");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
req = new MockHttpServletRequest("GET", "/test't est/path'm atching.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
req = new MockHttpServletRequest("GET", "/test%26t%20est/path%26m%20atching.html");
hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
}
public void testDefaultMapping() throws Exception {
Object bean = wac.getBean("starController");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/goggog.html");
HandlerExecutionChain hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
}
public void testMappingExposedInRequest() throws Exception {
Object bean = wac.getBean("mainController");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/show.html");
HandlerExecutionChain hec = getHandler(req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
assertEquals("Mapping not exposed", "show.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
}
private HandlerExecutionChain getHandler(MockHttpServletRequest req) throws Exception {
HandlerExecutionChain hec = hm.getHandler(req);
HandlerInterceptor[] interceptors = hec.getInterceptors();
if (interceptors != null) {
for (int i = 0; i < interceptors.length; i++) {
interceptors[i].preHandle(req, null, hec.getHandler());
}
}
return hec;
}
}

View File

@@ -0,0 +1,243 @@
/*
* Copyright 2002-2007 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.handler;
import java.util.Collections;
import java.util.Properties;
import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;
/**
* @author Seth Ladd
* @author Juergen Hoeller
*/
public class SimpleMappingExceptionResolverTests extends TestCase {
private SimpleMappingExceptionResolver exceptionResolver;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private Object handler1;
private Object handler2;
private Exception genericException;
protected void setUp() throws Exception {
exceptionResolver = new SimpleMappingExceptionResolver();
handler1 = new String();
handler2 = new Object();
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
request.setMethod("GET");
genericException = new Exception();
}
public void testSetOrder() {
exceptionResolver.setOrder(2);
assertEquals(2, exceptionResolver.getOrder());
}
public void testDefaultErrorView() {
exceptionResolver.setDefaultErrorView("default-view");
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals("default-view", mav.getViewName());
assertEquals(genericException, mav.getModel().get(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE));
}
public void testDefaultErrorViewDifferentHandler() {
exceptionResolver.setDefaultErrorView("default-view");
exceptionResolver.setMappedHandlers(Collections.singleton(handler1));
ModelAndView mav = exceptionResolver.resolveException(request, response, handler2, genericException);
assertNull(mav);
}
public void testDefaultErrorViewDifferentHandlerClass() {
exceptionResolver.setDefaultErrorView("default-view");
exceptionResolver.setMappedHandlerClasses(new Class[] {String.class});
ModelAndView mav = exceptionResolver.resolveException(request, response, handler2, genericException);
assertNull(mav);
}
public void testNullExceptionAttribute() {
exceptionResolver.setDefaultErrorView("default-view");
exceptionResolver.setExceptionAttribute(null);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals("default-view", mav.getViewName());
assertNull(mav.getModel().get(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE));
}
public void testNullExceptionMappings() {
exceptionResolver.setExceptionMappings(null);
exceptionResolver.setDefaultErrorView("default-view");
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals("default-view", mav.getViewName());
}
public void testNoDefaultStatusCode() {
exceptionResolver.setDefaultErrorView("default-view");
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
}
public void testSetDefaultStatusCode() {
exceptionResolver.setDefaultErrorView("default-view");
exceptionResolver.setDefaultStatusCode(HttpServletResponse.SC_BAD_REQUEST);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
}
public void testNoDefaultStatusCodeInInclude() {
exceptionResolver.setDefaultErrorView("default-view");
exceptionResolver.setDefaultStatusCode(HttpServletResponse.SC_BAD_REQUEST);
request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "some path");
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
}
public void testSimpleExceptionMapping() {
Properties props = new Properties();
props.setProperty("Exception", "error");
exceptionResolver.setWarnLogCategory("HANDLER_EXCEPTION");
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals("error", mav.getViewName());
}
public void testExactExceptionMappingWithHandlerSpecified() {
Properties props = new Properties();
props.setProperty("java.lang.Exception", "error");
exceptionResolver.setMappedHandlers(Collections.singleton(handler1));
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals("error", mav.getViewName());
}
public void testExactExceptionMappingWithHandlerClassSpecified() {
Properties props = new Properties();
props.setProperty("java.lang.Exception", "error");
exceptionResolver.setMappedHandlerClasses(new Class[] {String.class});
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals("error", mav.getViewName());
}
public void testExactExceptionMappingWithHandlerInterfaceSpecified() {
Properties props = new Properties();
props.setProperty("java.lang.Exception", "error");
exceptionResolver.setMappedHandlerClasses(new Class[] {Comparable.class});
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals("error", mav.getViewName());
}
public void testSimpleExceptionMappingWithHandlerSpecifiedButWrongHandler() {
Properties props = new Properties();
props.setProperty("Exception", "error");
exceptionResolver.setMappedHandlers(Collections.singleton(handler1));
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler2, genericException);
assertNull(mav);
}
public void testSimpleExceptionMappingWithHandlerClassSpecifiedButWrongHandler() {
Properties props = new Properties();
props.setProperty("Exception", "error");
exceptionResolver.setMappedHandlerClasses(new Class[] {String.class});
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler2, genericException);
assertNull(mav);
}
public void testMissingExceptionInMapping() {
Properties props = new Properties();
props.setProperty("SomeFooThrowable", "error");
exceptionResolver.setWarnLogCategory("HANDLER_EXCEPTION");
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertNull(mav);
}
public void testTwoMappings() {
Properties props = new Properties();
props.setProperty("java.lang.Exception", "error");
props.setProperty("AnotherException", "another-error");
exceptionResolver.setMappedHandlers(Collections.singleton(handler1));
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals("error", mav.getViewName());
}
public void testTwoMappingsOneShortOneLong() {
Properties props = new Properties();
props.setProperty("Exception", "error");
props.setProperty("AnotherException", "another-error");
exceptionResolver.setMappedHandlers(Collections.singleton(handler1));
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
assertEquals("error", mav.getViewName());
}
public void testTwoMappingsOneShortOneLongThrowOddException() {
Exception oddException = new SomeOddException();
Properties props = new Properties();
props.setProperty("Exception", "error");
props.setProperty("SomeOddException", "another-error");
exceptionResolver.setMappedHandlers(Collections.singleton(handler1));
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, oddException);
assertEquals("error", mav.getViewName());
}
public void testTwoMappingsThrowOddExceptionUseLongExceptionMapping() {
Exception oddException = new SomeOddException();
Properties props = new Properties();
props.setProperty("java.lang.Exception", "error");
props.setProperty("SomeOddException", "another-error");
exceptionResolver.setMappedHandlers(Collections.singleton(handler1));
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, oddException);
assertEquals("another-error", mav.getViewName());
}
public void testThreeMappings() {
Exception oddException = new AnotherOddException();
Properties props = new Properties();
props.setProperty("java.lang.Exception", "error");
props.setProperty("SomeOddException", "another-error");
props.setProperty("AnotherOddException", "another-some-error");
exceptionResolver.setMappedHandlers(Collections.singleton(handler1));
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, oddException);
assertEquals("another-some-error", mav.getViewName());
}
private static class SomeOddException extends Exception {
}
private static class AnotherOddException extends Exception {
}
}

View File

@@ -0,0 +1,142 @@
/*
* Copyright 2002-2007 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.handler;
import junit.framework.TestCase;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.WebUtils;
/**
* @author Rod Johnson
* @author Juergen Hoeller
*/
public class SimpleUrlHandlerMappingTests extends TestCase {
public void testHandlerBeanNotFound() throws Exception {
MockServletContext sc = new MockServletContext("");
XmlWebApplicationContext root = new XmlWebApplicationContext();
root.setServletContext(sc);
root.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map1.xml"});
root.refresh();
XmlWebApplicationContext wac = new XmlWebApplicationContext();
wac.setParent(root);
wac.setServletContext(sc);
wac.setNamespace("map2err");
wac.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map2err.xml"});
try {
wac.refresh();
fail("Should have thrown NoSuchBeanDefinitionException");
}
catch (FatalBeanException ex) {
NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause();
assertEquals("mainControlle", nestedEx.getBeanName());
}
}
public void testUrlMappingWithUrlMap() throws Exception {
checkMappings("urlMapping");
}
public void testUrlMappingWithProps() throws Exception {
checkMappings("urlMappingWithProps");
}
private void checkMappings(String beanName) throws Exception {
MockServletContext sc = new MockServletContext("");
XmlWebApplicationContext wac = new XmlWebApplicationContext();
wac.setServletContext(sc);
wac.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map2.xml"});
wac.refresh();
Object bean = wac.getBean("mainController");
Object otherBean = wac.getBean("otherController");
Object defaultBean = wac.getBean("starController");
HandlerMapping hm = (HandlerMapping) wac.getBean(beanName);
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html");
HandlerExecutionChain hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
assertEquals("/welcome.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
req = new MockHttpServletRequest("GET", "/welcome.x");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
assertEquals("welcome.x", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
req = new MockHttpServletRequest("GET", "/");
req.setServletPath("/welcome.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/welcome.html");
req.setContextPath("/app");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/show.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/bookseats.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/original-welcome.html");
req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/welcome.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/original-show.html");
req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/show.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/original-bookseats.html");
req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/bookseats.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
assertEquals("/", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
req = new MockHttpServletRequest("GET", "/somePath");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
assertEquals("/somePath", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
}
private HandlerExecutionChain getHandler(HandlerMapping hm, MockHttpServletRequest req) throws Exception {
HandlerExecutionChain hec = hm.getHandler(req);
HandlerInterceptor[] interceptors = hec.getInterceptors();
if (interceptors != null) {
for (int i = 0; i < interceptors.length; i++) {
interceptors[i].preHandle(req, null, hec.getHandler());
}
}
return hec;
}
}

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean id="godCtrl" name="/ /mypath/welcome.html /mypath/show.html /mypath/bookseats.html /mypath/reservation.html /mypath/payment.html /mypath/confirmation.html /mypath/test*"
class="java.lang.Object"/>
</beans>

View File

@@ -0,0 +1,7 @@
/welcome*=otherController
/welcome.html= mainController \n
/show.html=mainController
/bookseats.html=mainController
/reservation.html=mainController
/payment.html=mainController
/confirmation.html=mainController

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="defaultHandler"><ref local="starController"/></property>
<property name="rootHandler"><ref local="mainController"/></property>
<property name="urlMap">
<map>
<entry key="/welcome*"><ref local="otherController"/></entry>
<entry key="/welcome.html"><ref local="mainController"/></entry>
<entry key="/show.html"><ref local="mainController"/></entry>
<entry key="/bookseats.html"><ref local="mainController"/></entry>
<entry key="/reservation.html"><ref local="mainController"/></entry>
<entry key="/payment.html"><ref local="mainController"/></entry>
<entry key="/confirmation.html"><ref local="mainController"/></entry>
</map>
</property>
</bean>
<bean id="urlMappingWithProps" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="defaultHandler"><ref local="starController"/></property>
<property name="rootHandler"><ref local="mainController"/></property>
<property name="mappings"><ref local="propsForUrlMapping2"/></property>
</bean>
<bean id="propsForUrlMapping2" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location"><value>/org/springframework/web/servlet/handler/map2.properties</value></property>
</bean>
<bean id="mainController" class="java.lang.Object"/>
<bean id="otherController" class="java.lang.Object"/>
<bean id="starController" class="java.lang.Object"/>
</beans>

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="defaultHandler"><ref local="starController"/></property>
<property name="urlMap">
<map>
<entry key="/welcome.html"><ref local="mainController"/></entry>
<entry key="/show.html"><ref local="mainController"/></entry>
<entry key="/bookseats.html"><ref local="mainController"/></entry>
<entry key="/reservation.html"><ref bean="mainControlle"/></entry>
<entry key="/payment.html"><ref local="mainController"/></entry>
<entry key="/confirmation.html"><ref local="mainController"/></entry>
</map>
</property>
</bean>
<bean id="urlMappingWithProps" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="defaultHandler"><ref local="starController"/></property>
<property name="mappings"><ref local="propsForUrlMapping2"/></property>
</bean>
<bean id="propsForUrlMapping2" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location"><value>/org/springframework/web/servlet/handler/map2.properties</value></property>
</bean>
<bean id="mainController" class="java.lang.Object"/>
<bean id="starController" class="java.lang.Object"/>
</beans>

View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlDecode"><value>true</value></property>
<property name="mappings">
<value>
welcome.html=mainController
/**/pathmatchingTest.html=mainController
/**/pathmatching??.html=mainController
/**/path??matching.html=mainController
/**/??path??matching.html=mainController
/**/*.jsp=mainController
/administrator/**/pathmatching.html=mainController
/administrator/**/testlast*=mainController
/administrator/another/bla.xml=mainController
/administrator/testing/longer/**/**/**/**/**=mainController
/administrator/testing/longer2/**/**/bla/**=mainController
/*test*.jpeg=mainController
/*/test.jpeg=mainController
/outofpattern*yeah=mainController
/anotherTest*=mainController
/stillAnotherTestYeah=mainController
/shortpattern/testing=mainController
/show123.html=mainController
/sho*=mainController
/bookseats.html=mainController
/reservation.html=mainController
/payment.html=mainController
/confirmation.html=mainController
/test%26t%20est/path%26m%20atching.html=mainController
*=starController
</value>
</property>
</bean>
<bean id="mainController" class="java.lang.Object"/>
<bean id="starController" class="java.lang.Object"/>
<bean id="anotherController" class="java.lang.Object"/>
</beans>