Broadly remove deprecated core classes and methods

Issue: SPR-14430
This commit is contained in:
Juergen Hoeller
2016-07-05 15:52:48 +02:00
parent 0fc0ce78ae
commit b5db5d3aac
119 changed files with 145 additions and 6086 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -397,9 +397,8 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext {
public static class ComplexLocaleChecker implements MyHandler {
@Override
@SuppressWarnings("deprecation")
public void doSomething(HttpServletRequest request) throws ServletException, IllegalAccessException {
WebApplicationContext wac = RequestContextUtils.getWebApplicationContext(request);
WebApplicationContext wac = RequestContextUtils.findWebApplicationContext(request);
if (!(wac instanceof ComplexWebApplicationContext)) {
throw new ServletException("Incorrect WebApplicationContext");
}

View File

@@ -699,38 +699,6 @@ public class DispatcherServletTests {
assertNull(myServlet.getServletConfig());
}
@Test
@SuppressWarnings("deprecation")
public void webApplicationContextLookup() {
MockServletContext servletContext = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/invalid.do");
try {
RequestContextUtils.getWebApplicationContext(request);
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
try {
RequestContextUtils.getWebApplicationContext(request, servletContext);
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
new StaticWebApplicationContext());
try {
RequestContextUtils.getWebApplicationContext(request, servletContext);
}
catch (IllegalStateException ex) {
fail("Should not have thrown IllegalStateException: " + ex.getMessage());
}
}
@Test
public void withNoView() throws Exception {
MockServletContext servletContext = new MockServletContext();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -74,10 +74,9 @@ public class SimpleWebApplicationContext extends StaticWebApplicationContext {
public static class LocaleChecker implements Controller, LastModified {
@Override
@SuppressWarnings("deprecation")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (!(RequestContextUtils.getWebApplicationContext(request) instanceof SimpleWebApplicationContext)) {
if (!(RequestContextUtils.findWebApplicationContext(request) instanceof SimpleWebApplicationContext)) {
throw new ServletException("Incorrect WebApplicationContext");
}
if (!(RequestContextUtils.getLocaleResolver(request) instanceof AcceptHeaderLocaleResolver)) {

View File

@@ -1,27 +0,0 @@
/*
* Copyright 2002-2008 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.annotation;
import org.springframework.stereotype.Controller;
/**
* @author Juergen Hoeller
*/
@Controller
public class AdminController {
}

View File

@@ -1,52 +0,0 @@
/*
* Copyright 2002-2009 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.annotation;
import java.io.IOException;
import java.io.Writer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Used for testing the combination of ControllerClassNameHandlerMapping/SimpleUrlHandlerMapping with @RequestParam in
* {@link ServletAnnotationControllerTests}. Implemented as a top-level class (rather than an inner class) to make the
* ControllerClassNameHandlerMapping work.
*
* @author Arjen Poutsma
*/
@Controller
public class BookController {
@RequestMapping("list")
public void list(Writer writer) throws IOException {
writer.write("list");
}
@RequestMapping("show")
public void show(@RequestParam(required = true) Long id, Writer writer) throws IOException {
writer.write("show-id=" + id);
}
@RequestMapping(method = RequestMethod.POST)
public void create(Writer writer) throws IOException {
writer.write("create");
}
}

View File

@@ -1,27 +0,0 @@
/*
* Copyright 2002-2008 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.annotation;
import org.springframework.stereotype.Controller;
/**
* @author Juergen Hoeller
*/
@Controller
public class BuyForm {
}

View File

@@ -25,7 +25,6 @@ import org.junit.Test;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.interceptor.SimpleTraceInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
@@ -42,10 +41,11 @@ import static org.junit.Assert.*;
* @author Arjen Poutsma
* @since 3.0
*/
public class CgLibProxyServletAnnotationTests {
public class CglibProxyControllerTests {
private DispatcherServlet servlet;
@Test
public void typeLevel() throws Exception {
initServlet(TypeLevelImpl.class);
@@ -78,13 +78,12 @@ public class CgLibProxyServletAnnotationTests {
@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerclass) throws ServletException {
private void initServlet(final Class<?> controllerClass) throws ServletException {
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
throws BeansException {
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerclass));
wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass));
DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
autoProxyCreator.setProxyTargetClass(true);
autoProxyCreator.setBeanFactory(wac.getBeanFactory());
@@ -97,9 +96,6 @@ public class CgLibProxyServletAnnotationTests {
servlet.init(new MockServletConfig());
}
/*
* Controllers
*/
@Controller
@RequestMapping("/test")
@@ -111,6 +107,7 @@ public class CgLibProxyServletAnnotationTests {
}
}
@Controller
public static class MethodLevelImpl {
@@ -120,6 +117,7 @@ public class CgLibProxyServletAnnotationTests {
}
}
@Controller
@RequestMapping("/hotels")
public static class TypeAndMethodLevelImpl {

View File

@@ -1,40 +0,0 @@
/*
* Copyright 2002-2010 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.annotation;
import java.io.IOException;
import java.io.Writer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/** @author Arjen Poutsma */
@Controller
public class ControllerClassNameController {
@RequestMapping(value = {"{id}", "{id}.*"}, method = RequestMethod.GET)
public void plain(Writer writer, @PathVariable("id") String id) throws IOException {
writer.write("plain-" + id);
}
@RequestMapping(value = "{id}.pdf", method = RequestMethod.GET)
public void pdf(Writer writer, @PathVariable("id") String id) throws IOException {
writer.write("pdf-" + id);
}
}

View File

@@ -1,128 +0,0 @@
/*
* Copyright 2002-2015 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.annotation;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockServletContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import static org.junit.Assert.*;
/**
* @author Juergen Hoeller
*/
public class ControllerClassNameHandlerMappingTests {
private static final String LOCATION = "/org/springframework/web/servlet/mvc/annotation/class-mapping.xml";
private final XmlWebApplicationContext wac = new XmlWebApplicationContext();
private HandlerMapping hm, hm2, hm3, hm4;
@Before
public void setUp() throws Exception {
this.wac.setServletContext(new MockServletContext(""));
this.wac.setConfigLocations(LOCATION);
this.wac.refresh();
this.hm = (HandlerMapping) this.wac.getBean("mapping");
this.hm2 = (HandlerMapping) this.wac.getBean("mapping2");
this.hm3 = (HandlerMapping) this.wac.getBean("mapping3");
this.hm4 = (HandlerMapping) this.wac.getBean("mapping4");
}
@After
public void closeWac() {
this.wac.close();
}
@Test
public void indexUri() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("index"), chain.getHandler());
request = new MockHttpServletRequest("GET", "/index/product");
chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("index"), chain.getHandler());
}
@Test
public void mapSimpleUri() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
request = new MockHttpServletRequest("GET", "/welcome/product");
chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
@Test
public void withContextPath() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/welcome");
request.setContextPath("/myapp");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
@Test
public void withoutControllerSuffix() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/buyform");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("buy"), chain.getHandler());
request = new MockHttpServletRequest("GET", "/buyform/product");
chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("buy"), chain.getHandler());
}
@Test
public void withBasePackage() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/mvc/annotation/welcome");
HandlerExecutionChain chain = this.hm2.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
@Test
public void withBasePackageAndCaseSensitive() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/mvc/annotation/buyForm");
HandlerExecutionChain chain = this.hm2.getHandler(request);
assertEquals(this.wac.getBean("buy"), chain.getHandler());
}
@Test
public void withFullBasePackage() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/welcome");
HandlerExecutionChain chain = this.hm3.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
@Test
public void withRootAsBasePackage() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/org/springframework/web/servlet/mvc/annotation/welcome");
HandlerExecutionChain chain = this.hm4.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
}

View File

@@ -1,37 +0,0 @@
/*
* Copyright 2002-2008 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.annotation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Juergen Hoeller
*/
@Controller
public class IndexController {
@RequestMapping
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView("indexView");
}
}

View File

@@ -25,7 +25,6 @@ import org.junit.Test;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.interceptor.SimpleTraceInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
@@ -42,10 +41,11 @@ import static org.junit.Assert.*;
* @author Arjen Poutsma
* @since 3.0
*/
public class JdkProxyServletAnnotationTests {
public class JdkProxyControllerTests {
private DispatcherServlet servlet;
@Test
public void typeLevel() throws Exception {
initServlet(TypeLevelImpl.class);
@@ -81,8 +81,7 @@ public class JdkProxyServletAnnotationTests {
private void initServlet(final Class<?> controllerclass) throws ServletException {
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
throws BeansException {
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerclass));
DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
@@ -96,9 +95,6 @@ public class JdkProxyServletAnnotationTests {
servlet.init(new MockServletConfig());
}
/*
* Controllers
*/
@Controller
@RequestMapping("/test")
@@ -106,9 +102,9 @@ public class JdkProxyServletAnnotationTests {
@RequestMapping
void doIt(Writer writer) throws IOException;
}
public static class TypeLevelImpl implements TypeLevel {
@Override
@@ -123,9 +119,9 @@ public class JdkProxyServletAnnotationTests {
@RequestMapping("/test")
void doIt(Writer writer) throws IOException;
}
public static class MethodLevelImpl implements MethodLevel {
@Override
@@ -134,15 +130,16 @@ public class JdkProxyServletAnnotationTests {
}
}
@Controller
@RequestMapping("/hotels")
public interface TypeAndMethodLevel {
@RequestMapping("/bookings")
void doIt(Writer writer) throws IOException;
}
public static class TypeAndMethodLevelImpl implements TypeAndMethodLevel {
@Override

View File

@@ -1,53 +0,0 @@
/*
* Copyright 2002-2008 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.annotation;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author Juergen Hoeller
*/
@Controller
@RequestMapping("/*.do")
public class MethodNameDispatchingController {
@RequestMapping
public void myHandle(HttpServletResponse response) throws IOException {
response.getWriter().write("myView");
}
@RequestMapping
public void myOtherHandle(HttpServletResponse response) throws IOException {
response.getWriter().write("myOtherView");
}
@RequestMapping(method = RequestMethod.POST)
public void myLangHandle(HttpServletResponse response) throws IOException {
response.getWriter().write("myLangView");
}
@RequestMapping(method = RequestMethod.POST)
public void mySurpriseHandle(HttpServletResponse response) throws IOException {
response.getWriter().write("mySurpriseView");
}
}

View File

@@ -1,37 +0,0 @@
/*
* Copyright 2002-2006 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.annotation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Juergen Hoeller
*/
@Controller
public class WelcomeController {
@RequestMapping
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView("welcomeView");
}
}

View File

@@ -1,26 +0,0 @@
/*
* Copyright 2002-2006 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.mapping;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
/**
* @author Rob Harrop
*/
public class AdminController extends MultiActionController {
}

View File

@@ -1,32 +0,0 @@
/*
* Copyright 2002-2013 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.mapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class BuyForm implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
return null;
}
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright 2002-2012 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.mapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Juergen Hoeller
*/
public class Controller implements org.springframework.web.servlet.mvc.Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView("indexView");
}
}

View File

@@ -1,92 +0,0 @@
/*
* Copyright 2002-2015 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.mapping;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockServletContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import static org.junit.Assert.*;
/**
* @author Juergen Hoeller
*/
public class ControllerBeanNameHandlerMappingTests {
private static final String LOCATION = "/org/springframework/web/servlet/mvc/mapping/name-mapping.xml";
private final XmlWebApplicationContext wac = new XmlWebApplicationContext();
private HandlerMapping hm;
@Before
public void setUp() throws Exception {
this.wac.setServletContext(new MockServletContext(""));
this.wac.setConfigLocations(LOCATION);
this.wac.refresh();
this.hm = this.wac.getBean(HandlerMapping.class);
}
@After
public void closeWac() {
this.wac.close();
}
@Test
public void indexUri() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("index"), chain.getHandler());
}
@Test
public void mapSimpleUri() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
@Test
public void withContextPath() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/welcome");
request.setContextPath("/myapp");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
@Test
public void withMultiActionControllerMapping() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/admin");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("admin"), chain.getHandler());
}
@Test
public void withoutControllerSuffix() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/buy");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("buy"), chain.getHandler());
}
}

View File

@@ -1,130 +0,0 @@
/*
* Copyright 2002-2015 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.mapping;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockServletContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import static org.junit.Assert.*;
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
public class ControllerClassNameHandlerMappingTests {
public static final String LOCATION = "/org/springframework/web/servlet/mvc/mapping/class-mapping.xml";
private XmlWebApplicationContext wac;
private HandlerMapping hm;
private HandlerMapping hm2;
private HandlerMapping hm3;
private HandlerMapping hm4;
@Before
public void setUp() throws Exception {
MockServletContext sc = new MockServletContext("");
this.wac = new XmlWebApplicationContext();
this.wac.setServletContext(sc);
this.wac.setConfigLocations(new String[] {LOCATION});
this.wac.refresh();
this.hm = (HandlerMapping) this.wac.getBean("mapping");
this.hm2 = (HandlerMapping) this.wac.getBean("mapping2");
this.hm3 = (HandlerMapping) this.wac.getBean("mapping3");
this.hm4 = (HandlerMapping) this.wac.getBean("mapping4");
}
@Test
public void indexUri() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("index"), chain.getHandler());
}
@Test
public void mapSimpleUri() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
@Test
public void withContextPath() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/welcome");
request.setContextPath("/myapp");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
@Test
public void withMultiActionControllerMapping() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/admin/user");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("admin"), chain.getHandler());
request = new MockHttpServletRequest("GET", "/admin/product");
chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("admin"), chain.getHandler());
}
@Test
public void withoutControllerSuffix() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/buyform");
HandlerExecutionChain chain = this.hm.getHandler(request);
assertEquals(this.wac.getBean("buy"), chain.getHandler());
}
@Test
public void withBasePackage() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/mvc/mapping/welcome");
HandlerExecutionChain chain = this.hm2.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
@Test
public void withBasePackageAndCaseSensitive() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/mvc/mapping/buyForm");
HandlerExecutionChain chain = this.hm2.getHandler(request);
assertEquals(this.wac.getBean("buy"), chain.getHandler());
}
@Test
public void withFullBasePackage() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/welcome");
HandlerExecutionChain chain = this.hm3.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
@Test
public void withRootAsBasePackage() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/org/springframework/web/servlet/mvc/mapping/welcome");
HandlerExecutionChain chain = this.hm4.getHandler(request);
assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
}

View File

@@ -1,35 +0,0 @@
/*
* Copyright 2002-2012 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.mapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
* @author Rob Harrop
*/
public class WelcomeController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView("welcomeView");
}
}

View File

@@ -53,7 +53,6 @@ import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
import static org.junit.Assert.*;
@@ -103,12 +102,6 @@ public class ResponseEntityExceptionHandlerTests {
}
}
@Test
public void noSuchRequestHandlingMethod() {
Exception ex = new NoSuchRequestHandlingMethodException("GET", TestController.class);
testException(ex);
}
@Test
public void httpRequestMethodNotSupported() {
List<String> supported = Arrays.asList("POST", "DELETE");
@@ -116,7 +109,6 @@ public class ResponseEntityExceptionHandlerTests {
ResponseEntity<Object> responseEntity = testException(ex);
assertEquals(EnumSet.of(HttpMethod.POST, HttpMethod.DELETE), responseEntity.getHeaders().getAllow());
}
@Test

View File

@@ -128,7 +128,6 @@ import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.multipart.support.StringMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;
@@ -1786,10 +1785,6 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
}
/*
* Controllers
*/
@Controller
static class ControllerWithEmptyValueMapping {
@@ -2501,7 +2496,6 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
@Retention(RetentionPolicy.RUNTIME)
@Controller
public @interface MyControllerAnnotation {
}
@MyControllerAnnotation
@@ -2935,7 +2929,6 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
}
public static class MyEntity {
}
@Controller
@@ -2964,8 +2957,7 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
}
@RequestMapping("/multiValueMap")
public void multiValueMap(@RequestParam MultiValueMap<String, String> params, Writer writer)
throws IOException {
public void multiValueMap(@RequestParam MultiValueMap<String, String> params, Writer writer) throws IOException {
for (Iterator<Map.Entry<String, List<String>>> it1 = params.entrySet().iterator(); it1.hasNext();) {
Map.Entry<String, List<String>> entry = it1.next();
writer.write(entry.getKey() + "=[");
@@ -3135,7 +3127,6 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
setValue(null);
}
}
}
@Controller
@@ -3174,6 +3165,7 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
@Controller
@RequestMapping("/t1")
protected static class NoPathGetAndM2PostController {
@RequestMapping(method = RequestMethod.GET)
public void handle1(Writer writer) throws IOException {
writer.write("handle1");
@@ -3309,37 +3301,4 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
}
}
// Test cases deleted from the original ServletAnnotationControllerTests:
// @Ignore("Controller interface => no method-level @RequestMapping annotation")
// public void standardHandleMethod() throws Exception {
// @Ignore("ControllerClassNameHandlerMapping")
// public void emptyRequestMapping() throws Exception {
// @Ignore("Controller interface => no method-level @RequestMapping annotation")
// public void proxiedStandardHandleMethod() throws Exception {
// @Ignore("ServletException no longer thrown for unmatched parameter constraints")
// public void constrainedParameterDispatchingController() throws Exception {
// @Ignore("Method name dispatching")
// public void methodNameDispatchingController() throws Exception {
// @Ignore("Method name dispatching")
// public void methodNameDispatchingControllerWithSuffix() throws Exception {
// @Ignore("ControllerClassNameHandlerMapping")
// public void controllerClassNamePlusMethodNameDispatchingController() throws Exception {
// @Ignore("Method name dispatching")
// public void postMethodNameDispatchingController() throws Exception {
// @Ignore("ControllerClassNameHandlerMapping")
// public void controllerClassNameNoTypeLevelAnn() throws Exception {
// @Ignore("SimpleUrlHandlerMapping")
// public void simpleUrlHandlerMapping() throws Exception {
}

View File

@@ -1,746 +0,0 @@
/*
* Copyright 2002-2013 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.multiaction;
import org.junit.Test;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import static org.junit.Assert.*;
import org.springframework.beans.FatalBeanException;
import org.springframework.context.ApplicationContextException;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.mock.web.test.MockHttpSession;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.web.HttpSessionRequiredException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Rod Johnson
* @author Juergen Hoeller
* @author Colin Sampaleanu
* @author Rob Harrop
* @author Sam Brannen
*/
public class MultiActionControllerTests {
@Test
public void defaultInternalPathMethodNameResolver() throws Exception {
doDefaultTestInternalPathMethodNameResolver("/foo.html", "foo");
doDefaultTestInternalPathMethodNameResolver("/foo/bar.html", "bar");
doDefaultTestInternalPathMethodNameResolver("/bugal.xyz", "bugal");
doDefaultTestInternalPathMethodNameResolver("/x/y/z/q/foo.html", "foo");
doDefaultTestInternalPathMethodNameResolver("qqq.q", "qqq");
}
private void doDefaultTestInternalPathMethodNameResolver(String in, String expected) throws Exception {
MultiActionController rc = new MultiActionController();
HttpServletRequest request = new MockHttpServletRequest("GET", in);
String actual = rc.getMethodNameResolver().getHandlerMethodName(request);
assertEquals("Wrong method name resolved", expected, actual);
}
@Test
public void customizedInternalPathMethodNameResolver() throws Exception {
doTestCustomizedInternalPathMethodNameResolver("/foo.html", "my", null, "myfoo");
doTestCustomizedInternalPathMethodNameResolver("/foo/bar.html", null, "Handler", "barHandler");
doTestCustomizedInternalPathMethodNameResolver("/Bugal.xyz", "your", "Method", "yourBugalMethod");
}
private void doTestCustomizedInternalPathMethodNameResolver(String in, String prefix, String suffix, String expected)
throws Exception {
MultiActionController rc = new MultiActionController();
InternalPathMethodNameResolver resolver = new InternalPathMethodNameResolver();
if (prefix != null) {
resolver.setPrefix(prefix);
}
if (suffix != null) {
resolver.setSuffix(suffix);
}
rc.setMethodNameResolver(resolver);
HttpServletRequest request = new MockHttpServletRequest("GET", in);
String actual = rc.getMethodNameResolver().getHandlerMethodName(request);
assertEquals("Wrong method name resolved", expected, actual);
}
@Test
public void parameterMethodNameResolver() throws NoSuchRequestHandlingMethodException {
ParameterMethodNameResolver mnr = new ParameterMethodNameResolver();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html");
request.addParameter("action", "bar");
assertEquals("bar", mnr.getHandlerMethodName(request));
request = new MockHttpServletRequest("GET", "/foo.html");
try {
mnr.getHandlerMethodName(request);
fail("Should have thrown NoSuchRequestHandlingMethodException");
}
catch (NoSuchRequestHandlingMethodException expected) {
}
request = new MockHttpServletRequest("GET", "/foo.html");
request.addParameter("action", "");
try {
mnr.getHandlerMethodName(request);
fail("Should have thrown NoSuchRequestHandlingMethodException");
}
catch (NoSuchRequestHandlingMethodException expected) {
}
request = new MockHttpServletRequest("GET", "/foo.html");
request.addParameter("action", " ");
try {
mnr.getHandlerMethodName(request);
fail("Should have thrown NoSuchRequestHandlingMethodException");
}
catch (NoSuchRequestHandlingMethodException expected) {
}
}
@Test
public void parameterMethodNameResolverWithCustomParamName() throws NoSuchRequestHandlingMethodException {
ParameterMethodNameResolver mnr = new ParameterMethodNameResolver();
mnr.setParamName("myparam");
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html");
request.addParameter("myparam", "bar");
assertEquals("bar", mnr.getHandlerMethodName(request));
}
@Test
public void parameterMethodNameResolverWithParamNames() throws NoSuchRequestHandlingMethodException {
ParameterMethodNameResolver resolver = new ParameterMethodNameResolver();
resolver.setDefaultMethodName("default");
resolver.setMethodParamNames(new String[] { "hello", "spring", "colin" });
Properties logicalMappings = new Properties();
logicalMappings.setProperty("hello", "goodbye");
logicalMappings.setProperty("nina", "colin");
resolver.setLogicalMappings(logicalMappings);
// verify default handler
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("this will not match anything", "whatever");
assertEquals("default", resolver.getHandlerMethodName(request));
// verify first resolution strategy (action=method)
request = new MockHttpServletRequest();
request.addParameter("action", "reset");
assertEquals("reset", resolver.getHandlerMethodName(request));
// this one also tests logical mapping
request = new MockHttpServletRequest();
request.addParameter("action", "nina");
assertEquals("colin", resolver.getHandlerMethodName(request));
// now validate second resolution strategy (parameter existence)
// this also tests logical mapping
request = new MockHttpServletRequest();
request.addParameter("hello", "whatever");
assertEquals("goodbye", resolver.getHandlerMethodName(request));
request = new MockHttpServletRequest();
request.addParameter("spring", "whatever");
assertEquals("spring", resolver.getHandlerMethodName(request));
request = new MockHttpServletRequest();
request.addParameter("hello", "whatever");
request.addParameter("spring", "whatever");
assertEquals("goodbye", resolver.getHandlerMethodName(request));
request = new MockHttpServletRequest();
request.addParameter("colin", "whatever");
request.addParameter("spring", "whatever");
assertEquals("spring", resolver.getHandlerMethodName(request));
// validate image button handling
request = new MockHttpServletRequest();
request.addParameter("spring.x", "whatever");
assertEquals("spring", resolver.getHandlerMethodName(request));
request = new MockHttpServletRequest();
request.addParameter("hello.x", "whatever");
request.addParameter("spring", "whatever");
assertEquals("goodbye", resolver.getHandlerMethodName(request));
}
@Test
public void parameterMethodNameResolverWithDefaultMethodName() throws NoSuchRequestHandlingMethodException {
ParameterMethodNameResolver mnr = new ParameterMethodNameResolver();
mnr.setDefaultMethodName("foo");
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html");
request.addParameter("action", "bar");
assertEquals("bar", mnr.getHandlerMethodName(request));
request = new MockHttpServletRequest("GET", "/foo.html");
assertEquals("foo", mnr.getHandlerMethodName(request));
}
@Test
public void invokesCorrectMethod() throws Exception {
TestMaController mc = new TestMaController();
HttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
HttpServletResponse response = new MockHttpServletResponse();
Properties p = new Properties();
p.put("/welcome.html", "welcome");
PropertiesMethodNameResolver mnr = new PropertiesMethodNameResolver();
mnr.setMappings(p);
mc.setMethodNameResolver(mnr);
ModelAndView mv = mc.handleRequest(request, response);
assertTrue("Invoked welcome method", mc.wasInvoked("welcome"));
assertTrue("view name is welcome", mv.getViewName().equals("welcome"));
assertTrue("Only one method invoked", mc.getInvokedMethods() == 1);
mc = new TestMaController();
request = new MockHttpServletRequest("GET", "/subdir/test.html");
response = new MockHttpServletResponse();
mv = mc.handleRequest(request, response);
assertTrue("Invoked test method", mc.wasInvoked("test"));
assertTrue("view name is subdir_test", mv.getViewName().equals("test"));
assertTrue("Only one method invoked", mc.getInvokedMethods() == 1);
}
@Test
public void pathMatching() throws Exception {
TestMaController mc = new TestMaController();
HttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
HttpServletResponse response = new MockHttpServletResponse();
Properties p = new Properties();
p.put("/welc*.html", "welcome");
PropertiesMethodNameResolver mn = new PropertiesMethodNameResolver();
mn.setMappings(p);
mc.setMethodNameResolver(mn);
ModelAndView mv = mc.handleRequest(request, response);
assertTrue("Invoked welcome method", mc.wasInvoked("welcome"));
assertTrue("view name is welcome", mv.getViewName().equals("welcome"));
assertTrue("Only one method invoked", mc.getInvokedMethods() == 1);
mc = new TestMaController();
mc.setMethodNameResolver(mn);
request = new MockHttpServletRequest("GET", "/nomatch");
response = new MockHttpServletResponse();
try {
mv = mc.handleRequest(request, response);
}
catch (Exception expected) {
}
assertFalse("Not invoking welcome method", mc.wasInvoked("welcome"));
assertTrue("No method invoked", mc.getInvokedMethods() == 0);
}
@Test
public void invokesCorrectMethodOnDelegate() throws Exception {
MultiActionController mac = new MultiActionController();
TestDelegate d = new TestDelegate();
mac.setDelegate(d);
HttpServletRequest request = new MockHttpServletRequest("GET", "/test.html");
HttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = mac.handleRequest(request, response);
assertTrue("view name is test", mv.getViewName().equals("test"));
assertTrue("Delegate was invoked", d.invoked);
}
@Test
public void invokesCorrectMethodWithSession() throws Exception {
TestMaController mc = new TestMaController();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/inSession.html");
request.setSession(new MockHttpSession(null));
HttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = mc.handleRequest(request, response);
assertTrue("Invoked inSession method", mc.wasInvoked("inSession"));
assertTrue("view name is welcome", mv.getViewName().equals("inSession"));
assertTrue("Only one method invoked", mc.getInvokedMethods() == 1);
request = new MockHttpServletRequest("GET", "/inSession.html");
response = new MockHttpServletResponse();
try {
mc.handleRequest(request, response);
fail("Must have rejected request without session");
}
catch (ServletException expected) {
}
}
@Test
public void invokesCommandMethodNoSession() throws Exception {
TestMaController mc = new TestMaController();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/commandNoSession.html");
request.addParameter("name", "rod");
request.addParameter("age", "32");
HttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = mc.handleRequest(request, response);
assertTrue("Invoked commandNoSession method", mc.wasInvoked("commandNoSession"));
assertTrue("view name is commandNoSession", mv.getViewName().equals("commandNoSession"));
assertTrue("Only one method invoked", mc.getInvokedMethods() == 1);
}
@Test
public void invokesCommandMethodWithSession() throws Exception {
TestMaController mc = new TestMaController();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/commandInSession.html");
request.addParameter("name", "rod");
request.addParameter("age", "32");
request.setSession(new MockHttpSession(null));
HttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = mc.handleRequest(request, response);
assertTrue("Invoked commandInSession method", mc.wasInvoked("commandInSession"));
assertTrue("view name is commandInSession", mv.getViewName().equals("commandInSession"));
assertTrue("Only one method invoked", mc.getInvokedMethods() == 1);
request = new MockHttpServletRequest("GET", "/commandInSession.html");
response = new MockHttpServletResponse();
try {
mc.handleRequest(request, response);
fail("Must have rejected request without session");
}
catch (ServletException expected) {
}
}
@Test
public void sessionRequiredCatchable() throws Exception {
HttpServletRequest request = new MockHttpServletRequest("GET", "/testSession.html");
HttpServletResponse response = new MockHttpServletResponse();
TestMaController contr = new TestSessionRequiredController();
try {
contr.handleRequest(request, response);
fail("Should have thrown exception");
}
catch (HttpSessionRequiredException ex) {
// assertTrue("session required", ex.equals(t));
}
request = new MockHttpServletRequest("GET", "/testSession.html");
response = new MockHttpServletResponse();
contr = new TestSessionRequiredExceptionHandler();
ModelAndView mv = contr.handleRequest(request, response);
assertTrue("Name is ok", mv.getViewName().equals("handle(SRE)"));
}
private void testExceptionNoHandler(TestMaController mc, Throwable t) throws Exception {
HttpServletRequest request = new MockHttpServletRequest("GET", "/testException.html");
request.setAttribute(TestMaController.THROWABLE_ATT, t);
HttpServletResponse response = new MockHttpServletResponse();
try {
mc.handleRequest(request, response);
fail("Should have thrown exception");
}
catch (Throwable ex) {
assertTrue(ex.equals(t));
}
}
private void testExceptionNoHandler(Throwable t) throws Exception {
testExceptionNoHandler(new TestMaController(), t);
}
@Test
public void exceptionNoHandler() throws Exception {
testExceptionNoHandler(new Exception());
// must go straight through
testExceptionNoHandler(new ServletException());
// subclass of servlet exception
testExceptionNoHandler(new ServletRequestBindingException("foo"));
testExceptionNoHandler(new RuntimeException());
testExceptionNoHandler(new Error());
}
@Test
public void lastModifiedDefault() throws Exception {
TestMaController mc = new TestMaController();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
long lastMod = mc.getLastModified(request);
assertTrue("default last modified is -1", lastMod == -1L);
}
@Test
public void lastModifiedWithMethod() throws Exception {
LastModController mc = new LastModController();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
long lastMod = mc.getLastModified(request);
assertTrue("last modified with method is > -1", lastMod == mc.getLastModified(request));
}
private ModelAndView testHandlerCaughtException(TestMaController mc, Throwable t) throws Exception {
HttpServletRequest request = new MockHttpServletRequest("GET", "/testException.html");
request.setAttribute(TestMaController.THROWABLE_ATT, t);
HttpServletResponse response = new MockHttpServletResponse();
return mc.handleRequest(request, response);
}
@Test
public void handlerCaughtException() throws Exception {
TestMaController mc = new TestExceptionHandler();
ModelAndView mv = testHandlerCaughtException(mc, new Exception());
assertNotNull("ModelAndView must not be null", mv);
assertTrue("mv name is handle(Exception)", "handle(Exception)".equals(mv.getViewName()));
assertTrue("Invoked correct method", mc.wasInvoked("handle(Exception)"));
// WILL GET RUNTIME EXCEPTIONS TOO
testExceptionNoHandler(mc, new Error());
mc = new TestServletExceptionHandler();
mv = testHandlerCaughtException(mc, new ServletException());
assertTrue(mv.getViewName().equals("handle(ServletException)"));
assertTrue("Invoke correct method", mc.wasInvoked("handle(ServletException)"));
mv = testHandlerCaughtException(mc, new ServletRequestBindingException("foo"));
assertTrue(mv.getViewName().equals("handle(ServletException)"));
assertTrue("Invoke correct method", mc.wasInvoked("handle(ServletException)"));
// Check it doesn't affect unknown exceptions
testExceptionNoHandler(mc, new RuntimeException());
testExceptionNoHandler(mc, new Error());
testExceptionNoHandler(mc, new SQLException());
testExceptionNoHandler(mc, new Exception());
mc = new TestRuntimeExceptionHandler();
mv = testHandlerCaughtException(mc, new RuntimeException());
assertTrue(mv.getViewName().equals("handle(RTE)"));
assertTrue("Invoke correct method", mc.wasInvoked("handle(RTE)"));
mv = testHandlerCaughtException(mc, new FatalBeanException(null, null));
assertTrue(mv.getViewName().equals("handle(RTE)"));
assertTrue("Invoke correct method", mc.wasInvoked("handle(RTE)"));
testExceptionNoHandler(mc, new SQLException());
testExceptionNoHandler(mc, new Exception());
}
@Test
public void handlerReturnsMap() throws Exception {
Map model = new HashMap();
model.put("message", "Hello World!");
MultiActionController mac = new ModelOnlyMultiActionController(model);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = mac.handleRequest(request, response);
assertNotNull("ModelAndView cannot be null", mav);
assertFalse("ModelAndView should not have a view", mav.hasView());
assertEquals(model, mav.getModel());
}
@Test
public void exceptionHandlerReturnsMap() throws Exception {
Map model = new HashMap();
MultiActionController mac = new ModelOnlyMultiActionController(model);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = mac.handleRequest(request, response);
assertNotNull("ModelAndView cannot be null", mav);
assertFalse("ModelAndView should not have a view", mav.hasView());
assertTrue(model.containsKey("exception"));
}
@Test
public void cannotCallExceptionHandlerDirectly() throws Exception {
Map model = new HashMap();
MultiActionController mac = new ModelOnlyMultiActionController(model);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/handleIllegalStateException.html");
MockHttpServletResponse response = new MockHttpServletResponse();
mac.handleRequest(request, response);
assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
}
@Test
public void handlerReturnsVoid() throws Exception {
MultiActionController mac = new VoidMultiActionController();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = mac.handleRequest(request, response);
assertNull("ModelAndView must be null", mav);
}
@Test
public void exceptionHandlerReturnsVoid() throws Exception {
MultiActionController mac = new VoidMultiActionController();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = mac.handleRequest(request, response);
assertNull("ModelAndView must be null", mav);
assertEquals("exception", response.getContentAsString());
}
@Test
public void handlerReturnsString() throws Exception {
MultiActionController mac = new StringMultiActionController();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = mac.handleRequest(request, response);
assertNotNull("ModelAndView cannot be null", mav);
assertTrue("ModelAndView must have a view", mav.hasView());
assertEquals("Verifying view name", "welcomeString", mav.getViewName());
}
@Test
public void exceptionHandlerReturnsString() throws Exception {
MultiActionController mac = new StringMultiActionController();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = mac.handleRequest(request, response);
assertNotNull("ModelAndView cannot be null", mav);
assertTrue("ModelAndView must have a view", mav.hasView());
assertEquals("Verifying view name", "handleIllegalStateExceptionString", mav.getViewName());
}
/** No error handlers */
public static class TestMaController extends MultiActionController {
public static final String THROWABLE_ATT = "throwable";
/** Method name -> object */
protected Map invoked = new HashMap();
public void clear() {
this.invoked.clear();
}
public ModelAndView welcome(HttpServletRequest request, HttpServletResponse response) {
this.invoked.put("welcome", Boolean.TRUE);
return new ModelAndView("welcome");
}
public ModelAndView commandNoSession(HttpServletRequest request, HttpServletResponse response, TestBean command) {
this.invoked.put("commandNoSession", Boolean.TRUE);
String pname = request.getParameter("name");
// ALLOW FOR NULL
if (pname == null) {
assertTrue("name null", command.getName() == null);
}
else {
assertTrue("name param set", pname.equals(command.getName()));
}
//String page = request.getParameter("age");
// if (page == null)
// assertTrue("age default", command.getAge() == 0);
// else
// assertTrue("age set", command.getName().equals(pname));
// assertTrue("a",
// command.getAge().equals(request.getParameter("name")));
return new ModelAndView("commandNoSession");
}
public ModelAndView inSession(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
this.invoked.put("inSession", Boolean.TRUE);
assertTrue("session non null", session != null);
return new ModelAndView("inSession");
}
public ModelAndView commandInSession(HttpServletRequest request, HttpServletResponse response,
HttpSession session, TestBean command) {
this.invoked.put("commandInSession", Boolean.TRUE);
assertTrue("session non null", session != null);
return new ModelAndView("commandInSession");
}
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
this.invoked.put("test", Boolean.TRUE);
return new ModelAndView("test");
}
public ModelAndView testException(HttpServletRequest request, HttpServletResponse response) throws Throwable {
this.invoked.put("testException", Boolean.TRUE);
Throwable t = (Throwable) request.getAttribute(THROWABLE_ATT);
if (t != null) {
throw t;
}
else {
return new ModelAndView("no throwable");
}
}
public boolean wasInvoked(String method) {
return this.invoked.get(method) != null;
}
public int getInvokedMethods() {
return this.invoked.size();
}
}
public static class TestDelegate {
boolean invoked;
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
this.invoked = true;
return new ModelAndView("test");
}
}
public static class TestExceptionHandler extends TestMaController {
public ModelAndView handleAnyException(HttpServletRequest request, HttpServletResponse response, Exception ex) {
this.invoked.put("handle(Exception)", Boolean.TRUE);
return new ModelAndView("handle(Exception)");
}
}
public static class TestRuntimeExceptionHandler extends TestMaController {
public ModelAndView handleRuntimeProblem(HttpServletRequest request, HttpServletResponse response,
RuntimeException ex) {
this.invoked.put("handle(RTE)", Boolean.TRUE);
return new ModelAndView("handle(RTE)");
}
}
public static class TestSessionRequiredController extends TestMaController {
public ModelAndView testSession(HttpServletRequest request, HttpServletResponse response, HttpSession sess) {
return null;
}
}
/** Extends previous to handle exception */
public static class TestSessionRequiredExceptionHandler extends TestSessionRequiredController {
public ModelAndView handleServletException(HttpServletRequest request, HttpServletResponse response,
HttpSessionRequiredException ex) {
this.invoked.put("handle(SRE)", Boolean.TRUE);
return new ModelAndView("handle(SRE)");
}
}
public static class TestServletExceptionHandler extends TestMaController {
public ModelAndView handleServletException(HttpServletRequest request, HttpServletResponse response,
ServletException ex) {
this.invoked.put("handle(ServletException)", Boolean.TRUE);
return new ModelAndView("handle(ServletException)");
}
}
public static class LastModController extends MultiActionController {
public static final String THROWABLE_ATT = "throwable";
/** Method name -> object */
protected HashMap invoked = new HashMap();
public void clear() {
this.invoked.clear();
}
public ModelAndView welcome(HttpServletRequest request, HttpServletResponse response) {
this.invoked.put("welcome", Boolean.TRUE);
return new ModelAndView("welcome");
}
/** Always says content is up to date */
public long welcomeLastModified(HttpServletRequest request) {
return 1111L;
}
}
public static class ModelOnlyMultiActionController extends MultiActionController {
private final Map model;
public ModelOnlyMultiActionController(Map model) throws ApplicationContextException {
this.model = model;
}
public Map welcome(HttpServletRequest request, HttpServletResponse response) {
return this.model;
}
public Map index(HttpServletRequest request, HttpServletResponse response) {
throw new IllegalStateException();
}
public Map handleIllegalStateException(HttpServletRequest request, HttpServletResponse response,
IllegalStateException ex) {
this.model.put("exception", ex);
return this.model;
}
}
public static class VoidMultiActionController extends MultiActionController {
public void welcome(HttpServletRequest request, HttpServletResponse response) {
}
public void index(HttpServletRequest request, HttpServletResponse response) {
throw new IllegalStateException();
}
public void handleIllegalStateException(HttpServletRequest request, HttpServletResponse response,
IllegalStateException ex) throws IOException {
response.getWriter().write("exception");
}
}
public static class StringMultiActionController extends MultiActionController {
public String welcome(HttpServletRequest request, HttpServletResponse response) {
return "welcomeString";
}
public String index(HttpServletRequest request, HttpServletResponse response) {
throw new IllegalStateException();
}
public String handleIllegalStateException(HttpServletRequest request, HttpServletResponse response,
IllegalStateException ex) throws IOException {
return "handleIllegalStateExceptionString";
}
}
}

View File

@@ -43,7 +43,6 @@ import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
import static org.junit.Assert.*;
@@ -67,15 +66,6 @@ public class DefaultHandlerExceptionResolverTests {
request.setMethod("GET");
}
@Test
public void handleNoSuchRequestHandlingMethod() {
NoSuchRequestHandlingMethodException ex = new NoSuchRequestHandlingMethodException(request);
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
assertNotNull("No ModelAndView returned", mav);
assertTrue("No Empty ModelAndView returned", mav.isEmpty());
assertEquals("Invalid status code", 404, response.getStatus());
}
@Test
public void handleHttpRequestMethodNotSupported() {
HttpRequestMethodNotSupportedException ex =

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -1039,7 +1039,7 @@ public class BindTagTests extends AbstractTagTests {
formTag.setCssClass(cssClass);
formTag.setCssStyle(cssStyle);
formTag.setAction(action);
formTag.setCommandName(commandName);
formTag.setModelAttribute(commandName);
formTag.setEnctype(enctype);
formTag.setMethod(method);
formTag.setOnsubmit(onsubmit);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -386,7 +386,7 @@ public class MessageTagTests extends AbstractTagTests {
public void nullMessageSource() throws JspException {
PageContext pc = createPageContext();
ConfigurableWebApplicationContext ctx = (ConfigurableWebApplicationContext)
RequestContextUtils.getWebApplicationContext(pc.getRequest(), pc.getServletContext());
RequestContextUtils.findWebApplicationContext((HttpServletRequest) pc.getRequest(), pc.getServletContext());
ctx.close();
MessageTag tag = new MessageTag();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 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.
@@ -40,7 +40,7 @@ public abstract class AbstractFormTagTests extends AbstractHtmlElementTagTests {
@Override
protected void extendPageContext(MockPageContext pageContext) throws JspException {
this.formTag.setCommandName(COMMAND_NAME);
this.formTag.setModelAttribute(COMMAND_NAME);
this.formTag.setAction("myAction");
this.formTag.setPageContext(pageContext);
this.formTag.doStartTag();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -20,7 +20,6 @@ import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
@@ -106,8 +105,8 @@ public abstract class AbstractHtmlElementTagTests extends AbstractTagTests {
@SuppressWarnings("deprecation")
protected RequestDataValueProcessor getMockRequestDataValueProcessor() {
RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
ServletRequest request = getPageContext().getRequest();
StaticWebApplicationContext wac = (StaticWebApplicationContext) RequestContextUtils.getWebApplicationContext(request);
HttpServletRequest request = (HttpServletRequest) getPageContext().getRequest();
StaticWebApplicationContext wac = (StaticWebApplicationContext) RequestContextUtils.findWebApplicationContext(request);
wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
return mockProcessor;
}
@@ -128,18 +127,18 @@ public abstract class AbstractHtmlElementTagTests extends AbstractTagTests {
assertTrue("Expected to find attribute '" + attributeName +
"' with value '" + attributeValue +
"' in output + '" + output + "'",
output.indexOf(attributeString) > -1);
output.contains(attributeString));
}
protected final void assertAttributeNotPresent(String output, String attributeName) {
assertTrue("Unexpected attribute '" + attributeName + "' in output '" + output + "'.",
output.indexOf(attributeName + "=\"") < 0);
!output.contains(attributeName + "=\""));
}
protected final void assertBlockTagContains(String output, String desiredContents) {
String contents = output.substring(output.indexOf(">") + 1, output.lastIndexOf("<"));
assertTrue("Expected to find '" + desiredContents + "' in the contents of block tag '" + output + "'",
contents.indexOf(desiredContents) > -1);
contents.contains(desiredContents));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -86,7 +86,7 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
this.tag.setName(name);
this.tag.setCssClass(cssClass);
this.tag.setCssStyle(cssStyle);
this.tag.setCommandName(commandName);
this.tag.setModelAttribute(commandName);
this.tag.setAction(action);
this.tag.setMethod(method);
this.tag.setTarget(target);
@@ -138,7 +138,7 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
String onsubmit = "onsubmit";
String onreset = "onreset";
this.tag.setCommandName(commandName);
this.tag.setModelAttribute(commandName);
this.tag.setMethod(method);
this.tag.setEnctype(enctype);
this.tag.setOnsubmit(onsubmit);
@@ -182,7 +182,7 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
String onsubmit = "onsubmit";
String onreset = "onreset";
this.tag.setCommandName(commandName);
this.tag.setModelAttribute(commandName);
this.tag.setServletRelativeAction(action);
this.tag.setMethod(method);
this.tag.setEnctype(enctype);
@@ -216,7 +216,7 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
@Test
public void withNullResolvedCommand() throws Exception {
try {
tag.setCommandName(null);
tag.setModelAttribute(null);
tag.doStartTag();
fail("Must not be able to have a command name that resolves to null");
}