SPR-8532 Upgrade org.springframework.web.servlet to Servlet 3.0 (as provided dependency) and add support for javax.servlet.Part parameter
This commit is contained in:
@@ -28,6 +28,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
@@ -145,6 +146,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
private int localPort = DEFAULT_SERVER_PORT;
|
||||
|
||||
private Map<String, Part> parts = new HashMap<String, Part>();
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// HttpServletRequest properties
|
||||
@@ -890,13 +892,17 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
public boolean authenticate(HttpServletResponse arg0) throws IOException, ServletException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public void addPart(Part part) {
|
||||
parts.put(part.getName(), part);
|
||||
}
|
||||
|
||||
public Part getPart(String arg0) throws IOException, IllegalStateException, ServletException {
|
||||
throw new UnsupportedOperationException();
|
||||
public Part getPart(String key) throws IOException, IllegalStateException, ServletException {
|
||||
return parts.get(key);
|
||||
}
|
||||
|
||||
public Collection<Part> getParts() throws IOException, IllegalStateException, ServletException {
|
||||
throw new UnsupportedOperationException();
|
||||
return parts.values();
|
||||
}
|
||||
|
||||
public void login(String arg0, String arg1) throws ServletException {
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.mock.web;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
* Mock implementation of the {@link Part} interface.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
* @see MockHttpServletRequest
|
||||
*/
|
||||
public class MockPart implements Part {
|
||||
|
||||
private static final String CONTENT_TYPE = "Content-Type";
|
||||
|
||||
private final String name;
|
||||
|
||||
private String contentType;
|
||||
|
||||
private final byte[] content;
|
||||
|
||||
/**
|
||||
* Create a new MockPart with the given content.
|
||||
* @param name the name of the part
|
||||
* @param content the content for the part
|
||||
*/
|
||||
public MockPart(String name, byte[] content) {
|
||||
this(name, "", content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new MockPart with the given content.
|
||||
* @param name the name of the part
|
||||
* @param contentStream the content of the part as stream
|
||||
* @throws IOException if reading from the stream failed
|
||||
*/
|
||||
public MockPart(String name, InputStream contentStream) throws IOException {
|
||||
this(name, "", FileCopyUtils.copyToByteArray(contentStream));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new MockPart with the given content.
|
||||
* @param name the name of the file
|
||||
* @param contentType the content type (if known)
|
||||
* @param content the content of the file
|
||||
*/
|
||||
public MockPart(String name, String contentType, byte[] content) {
|
||||
Assert.hasLength(name, "Name must not be null");
|
||||
this.name = name;
|
||||
this.contentType = contentType;
|
||||
this.content = (content != null ? content : new byte[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new MockPart with the given content.
|
||||
* @param name the name of the file
|
||||
* @param contentType the content type (if known)
|
||||
* @param contentStream the content of the part as stream
|
||||
* @throws IOException if reading from the stream failed
|
||||
*/
|
||||
public MockPart(String name, String contentType, InputStream contentStream)
|
||||
throws IOException {
|
||||
|
||||
this(name, contentType, FileCopyUtils.copyToByteArray(contentStream));
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
return this.contentType;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return this.content.length;
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(this.content);
|
||||
}
|
||||
|
||||
public String getHeader(String name) {
|
||||
if (CONTENT_TYPE.equalsIgnoreCase(name)) {
|
||||
return this.contentType;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<String> getHeaders(String name) {
|
||||
if (CONTENT_TYPE.equalsIgnoreCase(name)) {
|
||||
return Collections.singleton(this.contentType);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<String> getHeaderNames() {
|
||||
return Collections.singleton(CONTENT_TYPE);
|
||||
}
|
||||
|
||||
public void write(String fileName) throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public void delete() throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,6 +24,8 @@ import static org.junit.Assert.assertTrue;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
||||
@@ -32,6 +34,7 @@ import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.mock.web.MockMultipartHttpServletRequest;
|
||||
import org.springframework.mock.web.MockPart;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
@@ -54,6 +57,8 @@ public class RequestParamMethodArgumentResolverTests {
|
||||
private MethodParameter paramMultiPartFile;
|
||||
private MethodParameter paramMap;
|
||||
private MethodParameter paramStringNotAnnot;
|
||||
private MethodParameter paramMultipartFileNotAnnot;
|
||||
private MethodParameter paramPartNotAnnot;
|
||||
|
||||
private NativeWebRequest webRequest;
|
||||
|
||||
@@ -63,8 +68,8 @@ public class RequestParamMethodArgumentResolverTests {
|
||||
public void setUp() throws Exception {
|
||||
resolver = new RequestParamMethodArgumentResolver(null, true);
|
||||
|
||||
Method method = getClass().getMethod("params",
|
||||
String.class, String[].class, Map.class, MultipartFile.class, Map.class, String.class);
|
||||
Method method = getClass().getMethod("params", String.class, String[].class, Map.class, MultipartFile.class,
|
||||
Map.class, String.class, MultipartFile.class, Part.class);
|
||||
|
||||
paramNamedDefaultValueString = new MethodParameter(method, 0);
|
||||
paramNamedStringArray = new MethodParameter(method, 1);
|
||||
@@ -73,6 +78,10 @@ public class RequestParamMethodArgumentResolverTests {
|
||||
paramMap = new MethodParameter(method, 4);
|
||||
paramStringNotAnnot = new MethodParameter(method, 5);
|
||||
paramStringNotAnnot.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
|
||||
paramMultipartFileNotAnnot = new MethodParameter(method, 6);
|
||||
paramMultipartFileNotAnnot.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
|
||||
paramPartNotAnnot = new MethodParameter(method, 7);
|
||||
paramPartNotAnnot.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
|
||||
@@ -87,6 +96,8 @@ public class RequestParamMethodArgumentResolverTests {
|
||||
assertTrue("MultipartFile parameter not supported", resolver.supportsParameter(paramMultiPartFile));
|
||||
assertFalse("non-@RequestParam parameter supported", resolver.supportsParameter(paramMap));
|
||||
assertTrue("Simple type params supported w/o annotations", resolver.supportsParameter(paramStringNotAnnot));
|
||||
assertTrue("MultipartFile parameter not supported", resolver.supportsParameter(paramMultipartFileNotAnnot));
|
||||
assertTrue("Part parameter not supported", resolver.supportsParameter(paramPartNotAnnot));
|
||||
|
||||
resolver = new RequestParamMethodArgumentResolver(null, false);
|
||||
assertFalse(resolver.supportsParameter(paramStringNotAnnot));
|
||||
@@ -127,6 +138,32 @@ public class RequestParamMethodArgumentResolverTests {
|
||||
assertEquals("Invalid result", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMultipartFileNotAnnotArgument() throws Exception {
|
||||
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
|
||||
MultipartFile expected = new MockMultipartFile("paramMultipartFileNotAnnot", "Hello World".getBytes());
|
||||
request.addFile(expected);
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
Object result = resolver.resolveArgument(paramMultipartFileNotAnnot, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof MultipartFile);
|
||||
assertEquals("Invalid result", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvePartArgument() throws Exception {
|
||||
MockPart expected = new MockPart("paramPartNotAnnot", "Hello World".getBytes());
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addPart(expected);
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
Object result = resolver.resolveArgument(paramPartNotAnnot, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof Part);
|
||||
assertEquals("Invalid result", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveDefaultValue() throws Exception {
|
||||
Object result = resolver.resolveArgument(paramNamedDefaultValueString, null, webRequest, null);
|
||||
@@ -157,7 +194,9 @@ public class RequestParamMethodArgumentResolverTests {
|
||||
@RequestParam("name") Map<?, ?> param3,
|
||||
@RequestParam(value = "file") MultipartFile param4,
|
||||
@RequestParam Map<?, ?> param5,
|
||||
String paramStringNotAnnot) {
|
||||
String paramStringNotAnnot,
|
||||
MultipartFile paramMultipartFileNotAnnot,
|
||||
Part paramPartNotAnnot) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user