|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.web.servlet.mvc.method.annotation; |
| 17 | + |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.util.Optional; |
| 20 | +import javax.servlet.http.HttpServletRequest; |
| 21 | +import javax.servlet.http.HttpServletResponse; |
| 22 | + |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import org.springframework.core.DefaultParameterNameDiscoverer; |
| 27 | +import org.springframework.core.GenericTypeResolver; |
| 28 | +import org.springframework.core.MethodParameter; |
| 29 | +import org.springframework.core.annotation.SynthesizingMethodParameter; |
| 30 | +import org.springframework.core.convert.support.DefaultConversionService; |
| 31 | +import org.springframework.mock.web.test.MockHttpServletRequest; |
| 32 | +import org.springframework.mock.web.test.MockHttpServletResponse; |
| 33 | +import org.springframework.web.bind.ServletRequestBindingException; |
| 34 | +import org.springframework.web.bind.WebDataBinder; |
| 35 | +import org.springframework.web.bind.annotation.RequestAttribute; |
| 36 | +import org.springframework.web.bind.annotation.SessionAttribute; |
| 37 | +import org.springframework.web.bind.support.WebDataBinderFactory; |
| 38 | +import org.springframework.web.bind.support.WebRequestDataBinder; |
| 39 | +import org.springframework.web.context.request.ServletWebRequest; |
| 40 | +import org.springframework.web.method.support.HandlerMethodArgumentResolver; |
| 41 | +import org.springframework.web.method.support.ModelAndViewContainer; |
| 42 | + |
| 43 | +import static org.junit.Assert.assertEquals; |
| 44 | +import static org.junit.Assert.assertFalse; |
| 45 | +import static org.junit.Assert.assertNotNull; |
| 46 | +import static org.junit.Assert.assertNull; |
| 47 | +import static org.junit.Assert.assertSame; |
| 48 | +import static org.junit.Assert.assertTrue; |
| 49 | +import static org.junit.Assert.fail; |
| 50 | +import static org.mockito.BDDMockito.given; |
| 51 | +import static org.mockito.Mockito.mock; |
| 52 | + |
| 53 | +/** |
| 54 | + * Base class for {@code @RequestAttribute} and {@code @SessionAttribute} method |
| 55 | + * method argument resolution tests. |
| 56 | + * |
| 57 | + * @author Rossen Stoyanchev |
| 58 | + * @since 4.3 |
| 59 | + */ |
| 60 | +public abstract class AbstractRequestAttributesArgumentResolverTests { |
| 61 | + |
| 62 | + private ServletWebRequest webRequest; |
| 63 | + |
| 64 | + private HandlerMethodArgumentResolver resolver; |
| 65 | + |
| 66 | + private Method handleMethod; |
| 67 | + |
| 68 | + |
| 69 | + @Before |
| 70 | + public void setUp() throws Exception { |
| 71 | + HttpServletRequest request = new MockHttpServletRequest(); |
| 72 | + HttpServletResponse response = new MockHttpServletResponse(); |
| 73 | + this.webRequest = new ServletWebRequest(request, response); |
| 74 | + this.resolver = createResolver(); |
| 75 | + this.handleMethod = AbstractRequestAttributesArgumentResolverTests.class |
| 76 | + .getDeclaredMethod(getHandleMethodName(), Foo.class, Foo.class, Foo.class, Optional.class); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + protected abstract HandlerMethodArgumentResolver createResolver(); |
| 81 | + |
| 82 | + protected abstract String getHandleMethodName(); |
| 83 | + |
| 84 | + protected abstract int getScope(); |
| 85 | + |
| 86 | + |
| 87 | + @Test |
| 88 | + public void supportsParameter() throws Exception { |
| 89 | + assertTrue(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 0))); |
| 90 | + assertFalse(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 4))); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void resolve() throws Exception { |
| 95 | + MethodParameter param = initMethodParameter(0); |
| 96 | + try { |
| 97 | + testResolveArgument(param); |
| 98 | + fail("Should be required by default"); |
| 99 | + } |
| 100 | + catch (ServletRequestBindingException ex) { |
| 101 | + assertTrue(ex.getMessage().startsWith("Missing ")); |
| 102 | + } |
| 103 | + |
| 104 | + Foo foo = new Foo(); |
| 105 | + this.webRequest.setAttribute("foo", foo, getScope()); |
| 106 | + assertSame(foo, testResolveArgument(param)); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void resolveWithName() throws Exception { |
| 111 | + MethodParameter param = initMethodParameter(1); |
| 112 | + Foo foo = new Foo(); |
| 113 | + this.webRequest.setAttribute("specialFoo", foo, getScope()); |
| 114 | + assertSame(foo, testResolveArgument(param)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void resolveNotRequired() throws Exception { |
| 119 | + MethodParameter param = initMethodParameter(2); |
| 120 | + assertNull(testResolveArgument(param)); |
| 121 | + |
| 122 | + Foo foo = new Foo(); |
| 123 | + this.webRequest.setAttribute("foo", foo, getScope()); |
| 124 | + assertSame(foo, testResolveArgument(param)); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void resolveOptional() throws Exception { |
| 129 | + WebDataBinder dataBinder = new WebRequestDataBinder(null); |
| 130 | + dataBinder.setConversionService(new DefaultConversionService()); |
| 131 | + WebDataBinderFactory factory = mock(WebDataBinderFactory.class); |
| 132 | + given(factory.createBinder(this.webRequest, null, "foo")).willReturn(dataBinder); |
| 133 | + |
| 134 | + MethodParameter param = initMethodParameter(3); |
| 135 | + Object actual = testResolveArgument(param, factory); |
| 136 | + assertNotNull(actual); |
| 137 | + assertEquals(Optional.class, actual.getClass()); |
| 138 | + assertFalse(((Optional) actual).isPresent()); |
| 139 | + |
| 140 | + Foo foo = new Foo(); |
| 141 | + this.webRequest.setAttribute("foo", foo, getScope()); |
| 142 | + |
| 143 | + actual = testResolveArgument(param, factory); |
| 144 | + assertNotNull(actual); |
| 145 | + assertEquals(Optional.class, actual.getClass()); |
| 146 | + assertTrue(((Optional) actual).isPresent()); |
| 147 | + assertSame(foo, ((Optional) actual).get()); |
| 148 | + } |
| 149 | + |
| 150 | + private Object testResolveArgument(MethodParameter param) throws Exception { |
| 151 | + return testResolveArgument(param, null); |
| 152 | + } |
| 153 | + |
| 154 | + private Object testResolveArgument(MethodParameter param, WebDataBinderFactory factory) throws Exception { |
| 155 | + ModelAndViewContainer mavContainer = new ModelAndViewContainer(); |
| 156 | + return this.resolver.resolveArgument(param, mavContainer, this.webRequest, factory); |
| 157 | + } |
| 158 | + |
| 159 | + private MethodParameter initMethodParameter(int parameterIndex) { |
| 160 | + MethodParameter param = new SynthesizingMethodParameter(this.handleMethod, parameterIndex); |
| 161 | + param.initParameterNameDiscovery(new DefaultParameterNameDiscoverer()); |
| 162 | + GenericTypeResolver.resolveParameterType(param, this.resolver.getClass()); |
| 163 | + return param; |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + @SuppressWarnings("unused") |
| 168 | + private void handleWithRequestAttribute( |
| 169 | + @RequestAttribute Foo foo, |
| 170 | + @RequestAttribute("specialFoo") Foo namedFoo, |
| 171 | + @RequestAttribute(name="foo", required = false) Foo notRequiredFoo, |
| 172 | + @RequestAttribute(name="foo") Optional<Foo> optionalFoo) { |
| 173 | + } |
| 174 | + |
| 175 | + @SuppressWarnings("unused") |
| 176 | + private void handleWithSessionAttribute( |
| 177 | + @SessionAttribute Foo foo, |
| 178 | + @SessionAttribute("specialFoo") Foo namedFoo, |
| 179 | + @SessionAttribute(name="foo", required = false) Foo notRequiredFoo, |
| 180 | + @SessionAttribute(name="foo") Optional<Foo> optionalFoo) { |
| 181 | + } |
| 182 | + |
| 183 | + private static class Foo { |
| 184 | + } |
| 185 | +} |
0 commit comments