From c1a4f5c0fe5bf9defb252b30197c08d138be42da Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 21 Jan 2013 11:31:28 +0100 Subject: [PATCH] MockHttpServletRequest's getParameter(Values) returns null for null parameter name Issue: SPR-10192 --- .../mock/web/MockHttpServletRequest.java | 43 ++++++++++-------- .../mock/web/MockHttpServletRequestTests.java | 12 +++-- .../mock/web/test/MockHttpServletRequest.java | 44 +++++++++---------- 3 files changed, 54 insertions(+), 45 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index 441a53e4ce..d609f39e8f 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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 javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -97,8 +96,10 @@ public class MockHttpServletRequest implements HttpServletRequest { private static final String CHARSET_PREFIX = "charset="; + private boolean active = true; + // --------------------------------------------------------------------- // ServletRequest properties // --------------------------------------------------------------------- @@ -140,6 +141,7 @@ public class MockHttpServletRequest implements HttpServletRequest { private int localPort = DEFAULT_SERVER_PORT; + // --------------------------------------------------------------------- // HttpServletRequest properties // --------------------------------------------------------------------- @@ -235,6 +237,7 @@ public class MockHttpServletRequest implements HttpServletRequest { this.locales.add(Locale.ENGLISH); } + // --------------------------------------------------------------------- // Lifecycle methods // --------------------------------------------------------------------- @@ -279,6 +282,7 @@ public class MockHttpServletRequest implements HttpServletRequest { } } + // --------------------------------------------------------------------- // ServletRequest interface // --------------------------------------------------------------------- @@ -351,7 +355,7 @@ public class MockHttpServletRequest implements HttpServletRequest { * parameter name, they will be replaced. */ public void setParameter(String name, String value) { - setParameter(name, new String[] { value }); + setParameter(name, new String[] {value}); } /** @@ -373,7 +377,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); @@ -382,8 +387,8 @@ 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() + "]"); } } } @@ -394,7 +399,7 @@ public class MockHttpServletRequest implements HttpServletRequest { * 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}); } /** @@ -425,7 +430,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); @@ -434,8 +440,8 @@ 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() + "]"); } } } @@ -456,8 +462,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); } @@ -466,8 +471,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() { @@ -509,8 +513,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 { @@ -574,7 +578,7 @@ public class MockHttpServletRequest implements HttpServletRequest { * @since 3.2 */ public void setPreferredLocales(List locales) { - Assert.notEmpty(locales, "preferred locales list must not be empty"); + Assert.notEmpty(locales, "Locale list must not be empty"); this.locales.clear(); this.locales.addAll(locales); } @@ -635,6 +639,7 @@ public class MockHttpServletRequest implements HttpServletRequest { return this.localPort; } + // --------------------------------------------------------------------- // HttpServletRequest interface // --------------------------------------------------------------------- @@ -797,8 +802,8 @@ public class MockHttpServletRequest implements HttpServletRequest { } public boolean isUserInRole(String role) { - return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext && ((MockServletContext) this.servletContext).getDeclaredRoles().contains( - role))); + return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext && + ((MockServletContext) this.servletContext).getDeclaredRoles().contains(role))); } public void setUserPrincipal(Principal userPrincipal) { diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java index 085bbb8b7d..a237bb00d1 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -16,8 +16,6 @@ package org.springframework.mock.web; -import static org.junit.Assert.*; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -29,6 +27,8 @@ import java.util.Map; import org.junit.Test; +import static org.junit.Assert.*; + /** * Unit tests for {@link MockHttpServletRequest}. * @@ -105,6 +105,12 @@ public class MockHttpServletRequestTests { assertEquals("HTTP header casing not being preserved", headerName, requestHeaders.nextElement()); } + @Test + public void nullParameterName() { + assertNull(request.getParameter(null)); + assertNull(request.getParameterValues(null)); + } + @Test public void setMultipleParameters() { request.setParameter("key1", "value1"); diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java index 5694b66610..c5dc6d7601 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -36,7 +36,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; - import javax.servlet.AsyncContext; import javax.servlet.DispatcherType; import javax.servlet.RequestDispatcher; @@ -110,9 +109,9 @@ public class MockHttpServletRequest implements HttpServletRequest { private boolean active = true; - //--------------------------------------------------------------------- + // --------------------------------------------------------------------- // ServletRequest properties - //--------------------------------------------------------------------- + // --------------------------------------------------------------------- private final Map attributes = new LinkedHashMap(); @@ -151,11 +150,12 @@ public class MockHttpServletRequest implements HttpServletRequest { private int localPort = DEFAULT_SERVER_PORT; - private Map parts = new HashMap(); + private final Map parts = new HashMap(); - //--------------------------------------------------------------------- + + // --------------------------------------------------------------------- // HttpServletRequest properties - //--------------------------------------------------------------------- + // --------------------------------------------------------------------- private String authType; @@ -200,9 +200,9 @@ public class MockHttpServletRequest implements HttpServletRequest { private DispatcherType dispatcherType = DispatcherType.REQUEST; - //--------------------------------------------------------------------- + // --------------------------------------------------------------------- // Constructors - //--------------------------------------------------------------------- + // --------------------------------------------------------------------- /** * Create a new {@code MockHttpServletRequest} with a default @@ -256,9 +256,10 @@ public class MockHttpServletRequest implements HttpServletRequest { this.locales.add(Locale.ENGLISH); } - //--------------------------------------------------------------------- + + // --------------------------------------------------------------------- // Lifecycle methods - //--------------------------------------------------------------------- + // --------------------------------------------------------------------- /** * Return the ServletContext that this request is associated with. (Not @@ -302,9 +303,9 @@ public class MockHttpServletRequest implements HttpServletRequest { } - //--------------------------------------------------------------------- + // --------------------------------------------------------------------- // ServletRequest interface - //--------------------------------------------------------------------- + // --------------------------------------------------------------------- @Override public Object getAttribute(String name) { @@ -414,8 +415,7 @@ public class MockHttpServletRequest implements HttpServletRequest { } else { throw new IllegalArgumentException( - "Parameter map value must be single value " + " or array of type [" + String.class.getName() + - "]"); + "Parameter map value must be single value " + " or array of type [" + String.class.getName() + "]"); } } } @@ -490,8 +490,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @Override 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); } @@ -502,8 +501,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @Override 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); } @Override @@ -620,7 +618,7 @@ public class MockHttpServletRequest implements HttpServletRequest { * @since 3.2 */ public void setPreferredLocales(List locales) { - Assert.notEmpty(locales, "preferred locales list must not be empty"); + Assert.notEmpty(locales, "Locale list must not be empty"); this.locales.clear(); this.locales.addAll(locales); } @@ -779,7 +777,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @Override public String getHeader(String name) { HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name); - return (header != null ? header.getValue().toString() : null); + return (header != null ? header.getStringValue() : null); } @Override @@ -867,8 +865,8 @@ public class MockHttpServletRequest implements HttpServletRequest { @Override public boolean isUserInRole(String role) { - return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext && ((MockServletContext) this.servletContext).getDeclaredRoles().contains( - role))); + return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext && + ((MockServletContext) this.servletContext).getDeclaredRoles().contains(role))); } public void setUserPrincipal(Principal userPrincipal) { -- GitLab