Add @SessionAttribute with Servlet-based support

Issue: SPR-13894
This commit is contained in:
Rossen Stoyanchev
2016-01-26 15:45:38 -05:00
parent 7df3a327f6
commit 698f923fc3
9 changed files with 390 additions and 38 deletions

View File

@@ -68,6 +68,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.bind.support.SessionStatus;
@@ -148,12 +149,13 @@ public class RequestMappingHandlerAdapterIntegrationTests {
public void handle() throws Exception {
Class<?>[] parameterTypes = new Class<?>[] { int.class, String.class, String.class, String.class, Map.class,
Date.class, Map.class, String.class, String.class, TestBean.class, Errors.class, TestBean.class,
Color.class, HttpServletRequest.class, HttpServletResponse.class, User.class, OtherUser.class,
Model.class, UriComponentsBuilder.class };
Color.class, HttpServletRequest.class, HttpServletResponse.class, TestBean.class,
User.class, OtherUser.class, Model.class, UriComponentsBuilder.class };
String datePattern = "yyyy.MM.dd";
String formattedDate = "2011.03.16";
Date date = new GregorianCalendar(2011, Calendar.MARCH, 16).getTime();
TestBean sessionAttribute = new TestBean();
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.addHeader("header", "headerValue");
@@ -171,6 +173,7 @@ public class RequestMappingHandlerAdapterIntegrationTests {
Map<String, String> uriTemplateVars = new HashMap<String, String>();
uriTemplateVars.put("pathvar", "pathvarValue");
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
request.getSession().setAttribute("sessionAttribute", sessionAttribute);
HandlerMethod handlerMethod = handlerMethod("handle", parameterTypes);
ModelAndView mav = handlerAdapter.handle(request, response, handlerMethod);
@@ -215,6 +218,8 @@ public class RequestMappingHandlerAdapterIntegrationTests {
assertEquals(User.class, model.get("user").getClass());
assertEquals(OtherUser.class, model.get("otherUser").getClass());
assertSame(sessionAttribute, model.get("sessionAttribute"));
assertEquals(new URI("http://localhost/contextPath/main/path"), model.get("url"));
}
@@ -363,6 +368,7 @@ public class RequestMappingHandlerAdapterIntegrationTests {
Color customArg,
HttpServletRequest request,
HttpServletResponse response,
@SessionAttribute TestBean sessionAttribute,
User user,
@ModelAttribute OtherUser otherUser,
Model model,
@@ -373,6 +379,7 @@ public class RequestMappingHandlerAdapterIntegrationTests {
.addAttribute("dateParam", dateParam).addAttribute("paramMap", paramMap)
.addAttribute("paramByConvention", paramByConvention).addAttribute("value", value)
.addAttribute("customArg", customArg).addAttribute(user)
.addAttribute("sessionAttribute", sessionAttribute)
.addAttribute("url", builder.path("/path").build().toUri());
assertNotNull(request);

View File

@@ -0,0 +1,168 @@
/*
* 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.
* 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.method.annotation;
import java.lang.reflect.Method;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.SynthesizingMethodParameter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.bind.support.WebRequestDataBinder;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Unit tests for {@link SessionAttributeMethodArgumentResolver}.
* @author Rossen Stoyanchev
*/
public class SessionAttributeMethodArgumentResolverTests {
private ServletWebRequest webRequest;
private MockHttpServletRequest servletRequest;
private SessionAttributeMethodArgumentResolver resolver;
private Method handleMethod;
@Before
public void setUp() throws Exception {
this.servletRequest = new MockHttpServletRequest();
this.webRequest = new ServletWebRequest(this.servletRequest, new MockHttpServletResponse());
this.resolver = new SessionAttributeMethodArgumentResolver();
this.handleMethod = getClass().getDeclaredMethod("handle", Foo.class, Foo.class,
Foo.class, Optional.class, Foo.class);
}
@Test
public void supportsParameter() throws Exception {
assertTrue(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 0)));
assertFalse(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 4)));
}
@Test
public void resolve() throws Exception {
MethodParameter param = initMethodParameter(0);
try {
testResolveArgument(param);
fail("Should be required by default");
}
catch (ServletRequestBindingException ex) {
assertTrue(ex.getMessage().startsWith("Missing session attribute"));
}
Foo foo = new Foo();
this.servletRequest.getSession().setAttribute("foo", foo);
assertSame(foo, testResolveArgument(param));
}
@Test
public void resolveWithName() throws Exception {
MethodParameter param = initMethodParameter(1);
Foo foo = new Foo();
this.servletRequest.getSession().setAttribute("specialFoo", foo);
assertSame(foo, testResolveArgument(param));
}
@Test
public void resolveNotRequired() throws Exception {
MethodParameter param = initMethodParameter(2);
assertNull(testResolveArgument(param));
Foo foo = new Foo();
this.servletRequest.getSession().setAttribute("foo", foo);
assertSame(foo, testResolveArgument(param));
}
@Test
public void resolveOptional() throws Exception {
WebDataBinder dataBinder = new WebRequestDataBinder(null);
dataBinder.setConversionService(new DefaultConversionService());
WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
given(factory.createBinder(this.webRequest, null, "foo")).willReturn(dataBinder);
MethodParameter param = initMethodParameter(3);
Object actual = testResolveArgument(param, factory);
assertNotNull(actual);
assertEquals(Optional.class, actual.getClass());
assertFalse(((Optional) actual).isPresent());
Foo foo = new Foo();
this.servletRequest.getSession().setAttribute("foo", foo);
actual = testResolveArgument(param, factory);
assertNotNull(actual);
assertEquals(Optional.class, actual.getClass());
assertTrue(((Optional) actual).isPresent());
assertSame(foo, ((Optional) actual).get());
}
private Object testResolveArgument(MethodParameter param) throws Exception {
return testResolveArgument(param, null);
}
private Object testResolveArgument(MethodParameter param, WebDataBinderFactory factory) throws Exception {
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
return this.resolver.resolveArgument(param, mavContainer, this.webRequest, factory);
}
private MethodParameter initMethodParameter(int parameterIndex) {
MethodParameter param = new SynthesizingMethodParameter(this.handleMethod, parameterIndex);
param.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
GenericTypeResolver.resolveParameterType(param, SessionAttributeMethodArgumentResolver.class);
return param;
}
@SuppressWarnings("unused")
private void handle(
@SessionAttribute Foo foo,
@SessionAttribute("specialFoo") Foo namedFoo,
@SessionAttribute(name="foo", required = false) Foo notRequiredFoo,
@SessionAttribute(name="foo") Optional<Foo> optionalFoo,
Foo notAnnotatedFoo) {
}
private static class Foo {
}
}