diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
index a10eab0b972..21d0a1125e3 100644
--- a/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
+++ b/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2011 the original author or authors.
+ * 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.
@@ -35,7 +35,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
-import java.util.Vector;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
@@ -52,7 +51,7 @@ import org.springframework.util.LinkedCaseInsensitiveMap;
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface.
*
*
Compatible with Servlet 2.5 and partially with Servlet 3.0 (notable exceptions:
- * the getPart(s) and startAsync families of methods).
+ * the {@code getPart(s)} and {@code startAsync} families of methods).
*
* @author Juergen Hoeller
* @author Rod Johnson
@@ -93,7 +92,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
public static final String DEFAULT_REMOTE_HOST = "localhost";
private static final String CONTENT_TYPE_HEADER = "Content-Type";
-
+
private static final String CHARSET_PREFIX = "charset=";
@@ -186,43 +185,45 @@ public class MockHttpServletRequest implements HttpServletRequest {
// ---------------------------------------------------------------------
/**
- * Create a new MockHttpServletRequest with a default
+ * Create a new {@code MockHttpServletRequest} with a default
* {@link MockServletContext}.
- * @see MockServletContext
+ * @see #MockHttpServletRequest(ServletContext, String, String)
*/
public MockHttpServletRequest() {
this(null, "", "");
}
/**
- * Create a new MockHttpServletRequest with a default
+ * Create a new {@code MockHttpServletRequest} with a default
* {@link MockServletContext}.
- * @param method the request method (may be null)
- * @param requestURI the request URI (may be null)
+ * @param method the request method (may be {@code null})
+ * @param requestURI the request URI (may be {@code null})
* @see #setMethod
* @see #setRequestURI
- * @see MockServletContext
+ * @see #MockHttpServletRequest(ServletContext, String, String)
*/
public MockHttpServletRequest(String method, String requestURI) {
this(null, method, requestURI);
}
/**
- * Create a new MockHttpServletRequest.
+ * Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext}.
* @param servletContext the ServletContext that the request runs in (may be
- * null to use a default MockServletContext)
- * @see MockServletContext
+ * {@code null} to use a default {@link MockServletContext})
+ * @see #MockHttpServletRequest(ServletContext, String, String)
*/
public MockHttpServletRequest(ServletContext servletContext) {
this(servletContext, "", "");
}
/**
- * Create a new MockHttpServletRequest.
+ * Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext},
+ * {@code method}, and {@code requestURI}.
+ *
The preferred locale will be set to {@link Locale#ENGLISH}.
* @param servletContext the ServletContext that the request runs in (may be
- * null to use a default MockServletContext)
- * @param method the request method (may be null)
- * @param requestURI the request URI (may be null)
+ * {@code null} to use a default {@link MockServletContext})
+ * @param method the request method (may be {@code null})
+ * @param requestURI the request URI (may be {@code null})
* @see #setMethod
* @see #setRequestURI
* @see MockServletContext
@@ -291,7 +292,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
public Enumeration getAttributeNames() {
checkActive();
- return new Vector(this.attributes.keySet()).elements();
+ return Collections.enumeration(this.attributes.keySet());
}
public String getCharacterEncoding() {
@@ -302,7 +303,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
this.characterEncoding = characterEncoding;
updateContentTypeHeader();
}
-
+
private void updateContentTypeHeader() {
if (this.contentType != null) {
StringBuilder sb = new StringBuilder(this.contentType);
@@ -348,18 +349,16 @@ public class MockHttpServletRequest implements HttpServletRequest {
/**
* Set a single value for the specified HTTP parameter.
- *
- * If there are already one or more values registered for the given
+ *
If there are already one or more values registered for the given
* parameter name, they will be replaced.
*/
public void setParameter(String name, String value) {
- setParameter(name, new String[] { value });
+ setParameter(name, new String[] {value});
}
/**
* Set an array of values for the specified HTTP parameter.
- *
- * If there are already one or more values registered for the given
+ *
If there are already one or more values registered for the given
* parameter name, they will be replaced.
*/
public void setParameter(String name, String[] values) {
@@ -368,7 +367,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
/**
- * Sets all provided parameters replacing any existing
+ * Sets all provided parameters replacing any existing
* values for the provided parameter names. To add without replacing
* existing values, use {@link #addParameters(java.util.Map)}.
*/
@@ -376,7 +375,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
public void setParameters(Map params) {
Assert.notNull(params, "Parameter map must not be null");
for (Object key : params.keySet()) {
- Assert.isInstanceOf(String.class, key, "Parameter map key must be of type [" + String.class.getName() + "]");
+ Assert.isInstanceOf(String.class, key,
+ "Parameter map key must be of type [" + String.class.getName() + "]");
Object value = params.get(key);
if (value instanceof String) {
this.setParameter((String) key, (String) value);
@@ -385,26 +385,24 @@ public class MockHttpServletRequest implements HttpServletRequest {
this.setParameter((String) key, (String[]) value);
}
else {
- throw new IllegalArgumentException("Parameter map value must be single value " + " or array of type ["
- + String.class.getName() + "]");
+ throw new IllegalArgumentException(
+ "Parameter map value must be single value " + " or array of type [" + String.class.getName() + "]");
}
}
}
/**
* Add a single value for the specified HTTP parameter.
- *
- * If there are already one or more values registered for the given
+ *
If there are already one or more values registered for the given
* parameter name, the given value will be added to the end of the list.
*/
public void addParameter(String name, String value) {
- addParameter(name, new String[] { value });
+ addParameter(name, new String[] {value});
}
/**
* Add an array of values for the specified HTTP parameter.
- *
- * If there are already one or more values registered for the given
+ *
If there are already one or more values registered for the given
* parameter name, the given values will be added to the end of the list.
*/
public void addParameter(String name, String[] values) {
@@ -422,7 +420,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
/**
- * Adds all provided parameters without replacing any
+ * Adds all provided parameters without replacing any
* existing values. To replace existing values, use
* {@link #setParameters(java.util.Map)}.
*/
@@ -430,7 +428,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
public void addParameters(Map params) {
Assert.notNull(params, "Parameter map must not be null");
for (Object key : params.keySet()) {
- Assert.isInstanceOf(String.class, key, "Parameter map key must be of type [" + String.class.getName() + "]");
+ Assert.isInstanceOf(String.class, key,
+ "Parameter map key must be of type [" + String.class.getName() + "]");
Object value = params.get(key);
if (value instanceof String) {
this.addParameter((String) key, (String) value);
@@ -439,15 +438,14 @@ public class MockHttpServletRequest implements HttpServletRequest {
this.addParameter((String) key, (String[]) value);
}
else {
- throw new IllegalArgumentException("Parameter map value must be single value " + " or array of type ["
- + String.class.getName() + "]");
+ throw new IllegalArgumentException("Parameter map value must be single value " +
+ " or array of type [" + String.class.getName() + "]");
}
}
}
/**
- * Remove already registered values for the specified HTTP parameter, if
- * any.
+ * Remove already registered values for the specified HTTP parameter, if any.
*/
public void removeParameter(String name) {
Assert.notNull(name, "Parameter name must not be null");
@@ -462,8 +460,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
public String getParameter(String name) {
- Assert.notNull(name, "Parameter name must not be null");
- String[] arr = this.parameters.get(name);
+ String[] arr = (name != null ? this.parameters.get(name) : null);
return (arr != null && arr.length > 0 ? arr[0] : null);
}
@@ -472,8 +469,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
public String[] getParameterValues(String name) {
- Assert.notNull(name, "Parameter name must not be null");
- return this.parameters.get(name);
+ return (name != null ? this.parameters.get(name) : null);
}
public Map getParameterMap() {
@@ -515,8 +511,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
public BufferedReader getReader() throws UnsupportedEncodingException {
if (this.content != null) {
InputStream sourceStream = new ByteArrayInputStream(this.content);
- Reader sourceReader = (this.characterEncoding != null) ? new InputStreamReader(sourceStream,
- this.characterEncoding) : new InputStreamReader(sourceStream);
+ Reader sourceReader = (this.characterEncoding != null) ?
+ new InputStreamReader(sourceStream, this.characterEncoding) : new InputStreamReader(sourceStream);
return new BufferedReader(sourceReader);
}
else {
@@ -656,8 +652,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
* adding the given value (more specifically, its toString representation)
* as further element.
* Multiple values can only be stored as list of Strings, following the
- * Servlet spec (see getHeaders accessor). As alternative to
- * repeated addHeader calls for individual elements, you can
+ * Servlet spec (see {@code getHeaders} accessor). As alternative to
+ * repeated {@code addHeader} calls for individual elements, you can
* use a single call with an entire array or Collection of values as
* parameter.
* @see #getHeaderNames
@@ -673,7 +669,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
doAddHeaderValue(name, value, false);
}
-
+
@SuppressWarnings("rawtypes")
private void doAddHeaderValue(String name, Object value, boolean replace) {
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
@@ -711,6 +707,20 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
}
+ public String getHeader(String name) {
+ HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
+ return (header != null ? header.getStringValue() : null);
+ }
+
+ public Enumeration getHeaders(String name) {
+ HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
+ return Collections.enumeration(header != null ? header.getStringValues() : new LinkedList());
+ }
+
+ public Enumeration getHeaderNames() {
+ return Collections.enumeration(this.headers.keySet());
+ }
+
public int getIntHeader(String name) {
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
Object value = (header != null ? header.getValue() : null);
@@ -728,20 +738,6 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
}
- public String getHeader(String name) {
- HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
- return (header != null ? header.getStringValue() : null);
- }
-
- public Enumeration getHeaders(String name) {
- HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
- return Collections.enumeration(header != null ? header.getStringValues() : new LinkedList());
- }
-
- public Enumeration getHeaderNames() {
- return Collections.enumeration(this.headers.keySet());
- }
-
public void setMethod(String method) {
this.method = method;
}
diff --git a/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java b/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java
index 4bad4f0d273..824d998a017 100644
--- a/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java
+++ b/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2011 the original author or authors.
+ * 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.
@@ -92,6 +92,12 @@ public class MockHttpServletRequestTests extends TestCase {
assertEquals("HTTP header casing not being preserved", headerName, requestHeaders.nextElement());
}
+ public void testNullParameterName() {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ assertNull(request.getParameter(null));
+ assertNull(request.getParameterValues(null));
+ }
+
public void testSetMultipleParameters() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setParameter("key1", "value1");