diff --git a/spring-orm/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-orm/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java index 12a952ac06c..bd1009c2c40 100644 --- a/spring-orm/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-orm/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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,7 @@ 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; @@ -58,6 +58,7 @@ import org.springframework.util.LinkedCaseInsensitiveMap; * @author Rod Johnson * @author Rick Evans * @author Mark Fisher + * @author Sam Brannen * @since 1.0.2 */ public class MockHttpServletRequest implements HttpServletRequest { @@ -93,13 +94,11 @@ 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="; - private boolean active = true; - // --------------------------------------------------------------------- // ServletRequest properties // --------------------------------------------------------------------- @@ -141,7 +140,6 @@ public class MockHttpServletRequest implements HttpServletRequest { private int localPort = DEFAULT_SERVER_PORT; - // --------------------------------------------------------------------- // HttpServletRequest properties // --------------------------------------------------------------------- @@ -186,46 +184,49 @@ public class MockHttpServletRequest implements HttpServletRequest { // --------------------------------------------------------------------- /** - * Create a new MockHttpServletRequest with a default - * {@link org.springframework.mock.web.MockServletContext}. - * @see org.springframework.mock.web.MockServletContext + * Create a new {@code MockHttpServletRequest} with a default + * {@link MockServletContext}. + * @see #MockHttpServletRequest(ServletContext, String, String) */ public MockHttpServletRequest() { this(null, "", ""); } /** - * Create a new MockHttpServletRequest with a default - * {@link org.springframework.mock.web.MockServletContext}. + * 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) * @see #setMethod * @see #setRequestURI - * @see org.springframework.mock.web.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 org.springframework.mock.web.MockServletContext + * 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) + * null to use a default {@link MockServletContext}) * @param method the request method (may be null) * @param requestURI the request URI (may be null) * @see #setMethod * @see #setRequestURI - * @see org.springframework.mock.web.MockServletContext + * @see #setPreferredLocales + * @see MockServletContext */ public MockHttpServletRequest(ServletContext servletContext, String method, String requestURI) { this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); @@ -234,7 +235,6 @@ public class MockHttpServletRequest implements HttpServletRequest { this.locales.add(Locale.ENGLISH); } - // --------------------------------------------------------------------- // Lifecycle methods // --------------------------------------------------------------------- @@ -279,7 +279,6 @@ public class MockHttpServletRequest implements HttpServletRequest { } } - // --------------------------------------------------------------------- // ServletRequest interface // --------------------------------------------------------------------- @@ -291,7 +290,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,11 +301,11 @@ public class MockHttpServletRequest implements HttpServletRequest { this.characterEncoding = characterEncoding; updateContentTypeHeader(); } - + private void updateContentTypeHeader() { if (this.contentType != null) { StringBuilder sb = new StringBuilder(this.contentType); - if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.characterEncoding != null) { + if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.characterEncoding != null) { sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); } doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true); @@ -348,8 +347,7 @@ 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) { @@ -358,8 +356,7 @@ public class MockHttpServletRequest implements HttpServletRequest { /** * 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 +365,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)}. */ @@ -393,8 +390,7 @@ public class MockHttpServletRequest implements HttpServletRequest { /** * 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) { @@ -403,8 +399,7 @@ public class MockHttpServletRequest implements HttpServletRequest { /** * 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 +417,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)}. */ @@ -566,12 +561,25 @@ public class MockHttpServletRequest implements HttpServletRequest { /** * Add a new preferred locale, before any existing locales. + * @see #setPreferredLocales */ public void addPreferredLocale(Locale locale) { Assert.notNull(locale, "Locale must not be null"); this.locales.add(0, locale); } + /** + * Set the list of preferred locales, in descending order, effectively replacing + * any existing locales. + * @see #addPreferredLocale + * @since 3.2 + */ + public void setPreferredLocales(List locales) { + Assert.notEmpty(locales, "preferred locales list must not be empty"); + this.locales.clear(); + this.locales.addAll(locales); + } + public Locale getLocale() { return this.locales.get(0); } @@ -628,7 +636,6 @@ public class MockHttpServletRequest implements HttpServletRequest { return this.localPort; } - // --------------------------------------------------------------------- // HttpServletRequest interface // --------------------------------------------------------------------- @@ -673,7 +680,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 +718,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 +749,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; } @@ -791,8 +798,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/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index a10eab0b972..973db4189fc 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-2011 the original author or authors. + * Copyright 2002-2012 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,7 @@ 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; @@ -58,6 +58,7 @@ import org.springframework.util.LinkedCaseInsensitiveMap; * @author Rod Johnson * @author Rick Evans * @author Mark Fisher + * @author Sam Brannen * @since 1.0.2 */ public class MockHttpServletRequest implements HttpServletRequest { @@ -93,13 +94,11 @@ 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="; + private static final String CHARSET_PREFIX = "charset="; private boolean active = true; - // --------------------------------------------------------------------- // ServletRequest properties // --------------------------------------------------------------------- @@ -141,7 +140,6 @@ public class MockHttpServletRequest implements HttpServletRequest { private int localPort = DEFAULT_SERVER_PORT; - // --------------------------------------------------------------------- // HttpServletRequest properties // --------------------------------------------------------------------- @@ -186,45 +184,48 @@ 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) * @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 + * 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) + * null to use a default {@link MockServletContext}) * @param method the request method (may be null) * @param requestURI the request URI (may be null) * @see #setMethod * @see #setRequestURI + * @see #setPreferredLocales * @see MockServletContext */ public MockHttpServletRequest(ServletContext servletContext, String method, String requestURI) { @@ -234,7 +235,6 @@ public class MockHttpServletRequest implements HttpServletRequest { this.locales.add(Locale.ENGLISH); } - // --------------------------------------------------------------------- // Lifecycle methods // --------------------------------------------------------------------- @@ -279,7 +279,6 @@ public class MockHttpServletRequest implements HttpServletRequest { } } - // --------------------------------------------------------------------- // ServletRequest interface // --------------------------------------------------------------------- @@ -291,7 +290,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 +301,7 @@ public class MockHttpServletRequest implements HttpServletRequest { this.characterEncoding = characterEncoding; updateContentTypeHeader(); } - + private void updateContentTypeHeader() { if (this.contentType != null) { StringBuilder sb = new StringBuilder(this.contentType); @@ -348,8 +347,7 @@ 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) { @@ -358,8 +356,7 @@ public class MockHttpServletRequest implements HttpServletRequest { /** * 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 +365,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)}. */ @@ -393,8 +390,7 @@ public class MockHttpServletRequest implements HttpServletRequest { /** * 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) { @@ -403,8 +399,7 @@ public class MockHttpServletRequest implements HttpServletRequest { /** * 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 +417,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)}. */ @@ -446,8 +441,7 @@ public class MockHttpServletRequest implements HttpServletRequest { } /** - * 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"); @@ -566,12 +560,25 @@ public class MockHttpServletRequest implements HttpServletRequest { /** * Add a new preferred locale, before any existing locales. + * @see #setPreferredLocales */ public void addPreferredLocale(Locale locale) { Assert.notNull(locale, "Locale must not be null"); this.locales.add(0, locale); } + /** + * Set the list of preferred locales, in descending order, effectively replacing + * any existing locales. + * @see #addPreferredLocale + * @since 3.2 + */ + public void setPreferredLocales(List locales) { + Assert.notEmpty(locales, "preferred locales list must not be empty"); + this.locales.clear(); + this.locales.addAll(locales); + } + public Locale getLocale() { return this.locales.get(0); } @@ -628,7 +635,6 @@ public class MockHttpServletRequest implements HttpServletRequest { return this.localPort; } - // --------------------------------------------------------------------- // HttpServletRequest interface // --------------------------------------------------------------------- @@ -673,7 +679,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 +717,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); @@ -727,21 +747,7 @@ public class MockHttpServletRequest implements HttpServletRequest { return -1; } } - - 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; } @@ -791,8 +797,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 4bad4f0d273..085bbb8b7db 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-2011 the original author or authors. + * Copyright 2002-2012 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,57 +16,70 @@ package org.springframework.mock.web; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; +import java.util.List; +import java.util.Locale; import java.util.Map; -import junit.framework.TestCase; +import org.junit.Test; /** + * Unit tests for {@link MockHttpServletRequest}. + * * @author Rick Evans * @author Mark Fisher * @author Rossen Stoyanchev + * @author Sam Brannen */ -public class MockHttpServletRequestTests extends TestCase { +public class MockHttpServletRequestTests { + + private MockHttpServletRequest request = new MockHttpServletRequest(); + - public void testSetContentType() { + @Test + public void setContentType() { String contentType = "test/plain"; - MockHttpServletRequest request = new MockHttpServletRequest(); request.setContentType(contentType); assertEquals(contentType, request.getContentType()); assertEquals(contentType, request.getHeader("Content-Type")); assertNull(request.getCharacterEncoding()); } - public void testSetContentTypeUTF8() { + @Test + public void setContentTypeUTF8() { String contentType = "test/plain;charset=UTF-8"; - MockHttpServletRequest request = new MockHttpServletRequest(); request.setContentType(contentType); assertEquals(contentType, request.getContentType()); assertEquals(contentType, request.getHeader("Content-Type")); assertEquals("UTF-8", request.getCharacterEncoding()); } - public void testContentTypeHeader() { + @Test + public void contentTypeHeader() { String contentType = "test/plain"; - MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Content-Type", contentType); assertEquals(contentType, request.getContentType()); assertEquals(contentType, request.getHeader("Content-Type")); assertNull(request.getCharacterEncoding()); } - public void testContentTypeHeaderUTF8() { + @Test + public void contentTypeHeaderUTF8() { String contentType = "test/plain;charset=UTF-8"; - MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Content-Type", contentType); assertEquals(contentType, request.getContentType()); assertEquals(contentType, request.getHeader("Content-Type")); assertEquals("UTF-8", request.getCharacterEncoding()); } - public void testSetContentTypeThenCharacterEncoding() { - MockHttpServletRequest request = new MockHttpServletRequest(); + @Test + public void setContentTypeThenCharacterEncoding() { request.setContentType("test/plain"); request.setCharacterEncoding("UTF-8"); assertEquals("test/plain", request.getContentType()); @@ -74,8 +87,8 @@ public class MockHttpServletRequestTests extends TestCase { assertEquals("UTF-8", request.getCharacterEncoding()); } - public void testSetCharacterEncodingThenContentType() { - MockHttpServletRequest request = new MockHttpServletRequest(); + @Test + public void setCharacterEncodingThenContentType() { request.setCharacterEncoding("UTF-8"); request.setContentType("test/plain"); assertEquals("test/plain", request.getContentType()); @@ -83,17 +96,17 @@ public class MockHttpServletRequestTests extends TestCase { assertEquals("UTF-8", request.getCharacterEncoding()); } - public void testHttpHeaderNameCasingIsPreserved() throws Exception { + @Test + public void httpHeaderNameCasingIsPreserved() throws Exception { String headerName = "Header1"; - MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader(headerName, "value1"); Enumeration requestHeaders = request.getHeaderNames(); assertNotNull(requestHeaders); assertEquals("HTTP header casing not being preserved", headerName, requestHeaders.nextElement()); } - public void testSetMultipleParameters() { - MockHttpServletRequest request = new MockHttpServletRequest(); + @Test + public void setMultipleParameters() { request.setParameter("key1", "value1"); request.setParameter("key2", "value2"); Map params = new HashMap(2); @@ -110,8 +123,8 @@ public class MockHttpServletRequestTests extends TestCase { assertEquals("value3B", values3[1]); } - public void testAddMultipleParameters() { - MockHttpServletRequest request = new MockHttpServletRequest(); + @Test + public void addMultipleParameters() { request.setParameter("key1", "value1"); request.setParameter("key2", "value2"); Map params = new HashMap(2); @@ -129,8 +142,8 @@ public class MockHttpServletRequestTests extends TestCase { assertEquals("value3B", values3[1]); } - public void testRemoveAllParameters() { - MockHttpServletRequest request = new MockHttpServletRequest(); + @Test + public void removeAllParameters() { request.setParameter("key1", "value1"); Map params = new HashMap(2); params.put("key2", "value2"); @@ -141,4 +154,47 @@ public class MockHttpServletRequestTests extends TestCase { assertEquals(0, request.getParameterMap().size()); } + @Test + public void defaultLocale() { + Locale originalDefaultLocale = Locale.getDefault(); + try { + Locale newDefaultLocale = originalDefaultLocale.equals(Locale.GERMANY) ? Locale.FRANCE : Locale.GERMANY; + Locale.setDefault(newDefaultLocale); + // Create the request after changing the default locale. + MockHttpServletRequest request = new MockHttpServletRequest(); + assertFalse(newDefaultLocale.equals(request.getLocale())); + assertEquals(Locale.ENGLISH, request.getLocale()); + } + finally { + Locale.setDefault(originalDefaultLocale); + } + } + + @Test(expected = IllegalArgumentException.class) + public void setPreferredLocalesWithNullList() { + request.setPreferredLocales(null); + } + + @Test(expected = IllegalArgumentException.class) + public void setPreferredLocalesWithEmptyList() { + request.setPreferredLocales(new ArrayList()); + } + + @Test + public void setPreferredLocales() { + List preferredLocales = Arrays.asList(Locale.ITALY, Locale.CHINA); + request.setPreferredLocales(preferredLocales); + assertEqualEnumerations(Collections.enumeration(preferredLocales), request.getLocales()); + } + + private void assertEqualEnumerations(Enumeration enum1, Enumeration enum2) { + assertNotNull(enum1); + assertNotNull(enum2); + int count = 0; + while (enum1.hasMoreElements()) { + assertTrue("enumerations must be equal in length", enum2.hasMoreElements()); + assertEquals("enumeration element #" + ++count, enum1.nextElement(), enum2.nextElement()); + } + } + } diff --git a/spring-web/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java index d08fa5ce8ec..64d1c48db53 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -56,7 +56,7 @@ import org.springframework.util.LinkedCaseInsensitiveMap; /** * Mock implementation of the {@link javax.servlet.http.HttpServletRequest} - * interface. Supports the Servlet 2.5 API leve. Throws + * interface. Supports the Servlet 2.5 API level; throws * {@link UnsupportedOperationException} for some methods introduced in Servlet 3.0. * *

Used for testing the web framework; also useful for testing @@ -67,6 +67,7 @@ import org.springframework.util.LinkedCaseInsensitiveMap; * @author Rick Evans * @author Mark Fisher * @author Chris Beams + * @author Sam Brannen * @since 1.0.2 */ public class MockHttpServletRequest implements HttpServletRequest { @@ -198,50 +199,54 @@ public class MockHttpServletRequest implements HttpServletRequest { private DispatcherType dispatcherType = DispatcherType.REQUEST; + //--------------------------------------------------------------------- // Constructors //--------------------------------------------------------------------- /** - * 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) * @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. - * @param servletContext the ServletContext that the request runs in - * (may be null to use a default MockServletContext) - * @see MockServletContext + * 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 {@link MockServletContext}) + * @see #MockHttpServletRequest(ServletContext, String, String) */ public MockHttpServletRequest(ServletContext servletContext) { this(servletContext, "", ""); } /** - * Create a new MockHttpServletRequest. - * @param servletContext the ServletContext that the request runs in - * (may be null to use a default MockServletContext) + * 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 {@link MockServletContext}) * @param method the request method (may be null) * @param requestURI the request URI (may be null) * @see #setMethod * @see #setRequestURI + * @see #setPreferredLocales * @see MockServletContext */ public MockHttpServletRequest(ServletContext servletContext, String method, String requestURI) { @@ -251,14 +256,13 @@ public class MockHttpServletRequest implements HttpServletRequest { this.locales.add(Locale.ENGLISH); } - //--------------------------------------------------------------------- // Lifecycle methods //--------------------------------------------------------------------- /** - * Return the ServletContext that this request is associated with. - * (Not available in the standard HttpServletRequest interface for some reason.) + * Return the ServletContext that this request is associated with. (Not + * available in the standard HttpServletRequest interface for some reason.) */ public ServletContext getServletContext() { return this.servletContext; @@ -323,7 +327,7 @@ public class MockHttpServletRequest implements HttpServletRequest { private void updateContentTypeHeader() { if (this.contentType != null) { StringBuilder sb = new StringBuilder(this.contentType); - if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.characterEncoding != null) { + if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.characterEncoding != null) { sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); } doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true); @@ -383,10 +387,11 @@ public class MockHttpServletRequest implements HttpServletRequest { } /** - * 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)}. + * 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)}. */ + @SuppressWarnings("rawtypes") public void setParameters(Map params) { Assert.notNull(params, "Parameter map must not be null"); for (Object key : params.keySet()) { @@ -436,10 +441,11 @@ public class MockHttpServletRequest implements HttpServletRequest { } /** - * Adds all provided parameters without replacing - * any existing values. To replace existing values, use + * Adds all provided parameters without replacing any + * existing values. To replace existing values, use * {@link #setParameters(java.util.Map)}. */ + @SuppressWarnings("rawtypes") public void addParameters(Map params) { Assert.notNull(params, "Parameter map must not be null"); for (Object key : params.keySet()) { @@ -579,12 +585,25 @@ public class MockHttpServletRequest implements HttpServletRequest { /** * Add a new preferred locale, before any existing locales. + * @see #setPreferredLocales */ public void addPreferredLocale(Locale locale) { Assert.notNull(locale, "Locale must not be null"); this.locales.add(0, locale); } + /** + * Set the list of preferred locales, in descending order, effectively replacing + * any existing locales. + * @see #addPreferredLocale + * @since 3.2 + */ + public void setPreferredLocales(List locales) { + Assert.notEmpty(locales, "preferred locales list must not be empty"); + this.locales.clear(); + this.locales.addAll(locales); + } + public Locale getLocale() { return this.locales.get(0); } @@ -804,7 +823,8 @@ public class MockHttpServletRequest implements HttpServletRequest { } public boolean isUserInRole(String role) { - return this.userRoles.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-webmvc/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-webmvc/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java index debe0165e82..e95a99feea7 100644 --- a/spring-webmvc/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-webmvc/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -57,7 +57,7 @@ import org.springframework.util.LinkedCaseInsensitiveMap; /** * Mock implementation of the {@link javax.servlet.http.HttpServletRequest} * interface. Supports the Servlet 2.5 API level; throws - * {@link UnsupportedOperationException} for all methods introduced in Servlet 3.0. + * {@link UnsupportedOperationException} for some methods introduced in Servlet 3.0. * *

Used for testing the web framework; also useful for testing * application controllers. @@ -67,6 +67,7 @@ import org.springframework.util.LinkedCaseInsensitiveMap; * @author Rick Evans * @author Mark Fisher * @author Chris Beams + * @author Sam Brannen * @since 1.0.2 */ public class MockHttpServletRequest implements HttpServletRequest { @@ -204,45 +205,48 @@ 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) * @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. - * @param servletContext the ServletContext that the request runs in - * (may be null to use a default MockServletContext) - * @see MockServletContext + * 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 {@link MockServletContext}) + * @see #MockHttpServletRequest(ServletContext, String, String) */ public MockHttpServletRequest(ServletContext servletContext) { this(servletContext, "", ""); } /** - * Create a new MockHttpServletRequest. - * @param servletContext the ServletContext that the request runs in - * (may be null to use a default MockServletContext) + * 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 {@link MockServletContext}) * @param method the request method (may be null) * @param requestURI the request URI (may be null) * @see #setMethod * @see #setRequestURI + * @see #setPreferredLocales * @see MockServletContext */ public MockHttpServletRequest(ServletContext servletContext, String method, String requestURI) { @@ -252,7 +256,6 @@ public class MockHttpServletRequest implements HttpServletRequest { this.locales.add(Locale.ENGLISH); } - //--------------------------------------------------------------------- // Lifecycle methods //--------------------------------------------------------------------- @@ -324,7 +327,7 @@ public class MockHttpServletRequest implements HttpServletRequest { private void updateContentTypeHeader() { if (this.contentType != null) { StringBuilder sb = new StringBuilder(this.contentType); - if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.characterEncoding != null) { + if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.characterEncoding != null) { sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); } doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true); @@ -384,10 +387,11 @@ public class MockHttpServletRequest implements HttpServletRequest { } /** - * Sets all provided parameters replacing any + * 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)}. */ + @SuppressWarnings("rawtypes") public void setParameters(Map params) { Assert.notNull(params, "Parameter map must not be null"); for (Object key : params.keySet()) { @@ -437,10 +441,11 @@ public class MockHttpServletRequest implements HttpServletRequest { } /** - * Adds all provided parameters without replacing - * any existing values. To replace existing values, use + * Adds all provided parameters without replacing any + * existing values. To replace existing values, use * {@link #setParameters(java.util.Map)}. */ + @SuppressWarnings("rawtypes") public void addParameters(Map params) { Assert.notNull(params, "Parameter map must not be null"); for (Object key : params.keySet()) { @@ -580,12 +585,25 @@ public class MockHttpServletRequest implements HttpServletRequest { /** * Add a new preferred locale, before any existing locales. + * @see #setPreferredLocales */ public void addPreferredLocale(Locale locale) { Assert.notNull(locale, "Locale must not be null"); this.locales.add(0, locale); } + /** + * Set the list of preferred locales, in descending order, effectively replacing + * any existing locales. + * @see #addPreferredLocale + * @since 3.2 + */ + public void setPreferredLocales(List locales) { + Assert.notEmpty(locales, "preferred locales list must not be empty"); + this.locales.clear(); + this.locales.addAll(locales); + } + public Locale getLocale() { return this.locales.get(0); } @@ -805,7 +823,8 @@ public class MockHttpServletRequest implements HttpServletRequest { } public boolean isUserInRole(String role) { - return this.userRoles.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-webmvc/src/test/java/org/springframework/web/servlet/tags/AbstractTagTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/AbstractTagTests.java index ea5b7511e35..4c7c0dfea93 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/AbstractTagTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/AbstractTagTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2012 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. @@ -31,13 +31,14 @@ import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; import org.springframework.web.servlet.theme.FixedThemeResolver; /** - * Abstract base class for testing tags: provides createPageContext. + * Abstract base class for testing tags; provides {@link #createPageContext()}. * * @author Alef Arendsen * @author Juergen Hoeller + * @author Sam Brannen */ public abstract class AbstractTagTests extends TestCase { - + protected MockPageContext createPageContext() { MockServletContext sc = new MockServletContext(); SimpleWebApplicationContext wac = new SimpleWebApplicationContext();