Configurable locales in MockHttpServletRequest
Prior to this commit the MockHttpServletRequest constructor chain set the preferred local to Locale.ENGLISH. Furthermore, it was possible to add additional preferred locales "in front" of ENGLISH; however, it was not possible to delete ENGLISH from the list of preferred locales. This commit documents the fact that ENGLISH is the default preferred locale and makes it possible to set the list of preferred locales via a new setPreferredLocales(List<Locale> locales) method. Issue: SPR-9724
This commit is contained in:
@@ -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.
|
||||
*
|
||||
* <p>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 <code>null</code>)
|
||||
* @param requestURI the request URI (may be <code>null</code>)
|
||||
* @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 <code>null</code> 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
|
||||
* <code>null</code> 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 <code>null</code> to use a default MockServletContext)
|
||||
* Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext},
|
||||
* {@code method}, and {@code requestURI}.
|
||||
* <p>The preferred locale will be set to {@link Locale#ENGLISH}.
|
||||
* @param servletContext the ServletContext that the request runs in (may be
|
||||
* <code>null</code> to use a default {@link MockServletContext})
|
||||
* @param method the request method (may be <code>null</code>)
|
||||
* @param requestURI the request URI (may be <code>null</code>)
|
||||
* @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 <emphasis>replacing</emphasis> any
|
||||
* Sets all provided parameters, <strong>replacing</strong> 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 <emphasis>without</emphasis> replacing
|
||||
* any existing values. To replace existing values, use
|
||||
* Adds all provided parameters <strong>without</strong> 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<Locale> 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) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user