Skip to content

Commit 591aa01

Browse files
committed
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
1 parent 7d9c823 commit 591aa01

File tree

6 files changed

+278
-169
lines changed

6 files changed

+278
-169
lines changed

spring-orm/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@
3535
import java.util.Locale;
3636
import java.util.Map;
3737
import java.util.Set;
38-
import java.util.Vector;
38+
3939
import javax.servlet.RequestDispatcher;
4040
import javax.servlet.ServletContext;
4141
import javax.servlet.ServletException;
@@ -58,6 +58,7 @@
5858
* @author Rod Johnson
5959
* @author Rick Evans
6060
* @author Mark Fisher
61+
* @author Sam Brannen
6162
* @since 1.0.2
6263
*/
6364
public class MockHttpServletRequest implements HttpServletRequest {
@@ -93,13 +94,11 @@ public class MockHttpServletRequest implements HttpServletRequest {
9394
public static final String DEFAULT_REMOTE_HOST = "localhost";
9495

9596
private static final String CONTENT_TYPE_HEADER = "Content-Type";
96-
97+
9798
private static final String CHARSET_PREFIX = "charset=";
9899

99-
100100
private boolean active = true;
101101

102-
103102
// ---------------------------------------------------------------------
104103
// ServletRequest properties
105104
// ---------------------------------------------------------------------
@@ -141,7 +140,6 @@ public class MockHttpServletRequest implements HttpServletRequest {
141140

142141
private int localPort = DEFAULT_SERVER_PORT;
143142

144-
145143
// ---------------------------------------------------------------------
146144
// HttpServletRequest properties
147145
// ---------------------------------------------------------------------
@@ -186,46 +184,49 @@ public class MockHttpServletRequest implements HttpServletRequest {
186184
// ---------------------------------------------------------------------
187185

188186
/**
189-
* Create a new MockHttpServletRequest with a default
190-
* {@link org.springframework.mock.web.MockServletContext}.
191-
* @see org.springframework.mock.web.MockServletContext
187+
* Create a new {@code MockHttpServletRequest} with a default
188+
* {@link MockServletContext}.
189+
* @see #MockHttpServletRequest(ServletContext, String, String)
192190
*/
193191
public MockHttpServletRequest() {
194192
this(null, "", "");
195193
}
196194

197195
/**
198-
* Create a new MockHttpServletRequest with a default
199-
* {@link org.springframework.mock.web.MockServletContext}.
196+
* Create a new {@code MockHttpServletRequest} with a default
197+
* {@link MockServletContext}.
200198
* @param method the request method (may be <code>null</code>)
201199
* @param requestURI the request URI (may be <code>null</code>)
202200
* @see #setMethod
203201
* @see #setRequestURI
204-
* @see org.springframework.mock.web.MockServletContext
202+
* @see #MockHttpServletRequest(ServletContext, String, String)
205203
*/
206204
public MockHttpServletRequest(String method, String requestURI) {
207205
this(null, method, requestURI);
208206
}
209207

210208
/**
211-
* Create a new MockHttpServletRequest.
209+
* Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext}.
212210
* @param servletContext the ServletContext that the request runs in (may be
213-
* <code>null</code> to use a default MockServletContext)
214-
* @see org.springframework.mock.web.MockServletContext
211+
* <code>null</code> to use a default {@link MockServletContext})
212+
* @see #MockHttpServletRequest(ServletContext, String, String)
215213
*/
216214
public MockHttpServletRequest(ServletContext servletContext) {
217215
this(servletContext, "", "");
218216
}
219217

220218
/**
221-
* Create a new MockHttpServletRequest.
219+
* Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext},
220+
* {@code method}, and {@code requestURI}.
221+
* <p>The preferred locale will be set to {@link Locale#ENGLISH}.
222222
* @param servletContext the ServletContext that the request runs in (may be
223-
* <code>null</code> to use a default MockServletContext)
223+
* <code>null</code> to use a default {@link MockServletContext})
224224
* @param method the request method (may be <code>null</code>)
225225
* @param requestURI the request URI (may be <code>null</code>)
226226
* @see #setMethod
227227
* @see #setRequestURI
228-
* @see org.springframework.mock.web.MockServletContext
228+
* @see #setPreferredLocales
229+
* @see MockServletContext
229230
*/
230231
public MockHttpServletRequest(ServletContext servletContext, String method, String requestURI) {
231232
this.servletContext = (servletContext != null ? servletContext : new MockServletContext());
@@ -234,7 +235,6 @@ public MockHttpServletRequest(ServletContext servletContext, String method, Stri
234235
this.locales.add(Locale.ENGLISH);
235236
}
236237

237-
238238
// ---------------------------------------------------------------------
239239
// Lifecycle methods
240240
// ---------------------------------------------------------------------
@@ -279,7 +279,6 @@ protected void checkActive() throws IllegalStateException {
279279
}
280280
}
281281

282-
283282
// ---------------------------------------------------------------------
284283
// ServletRequest interface
285284
// ---------------------------------------------------------------------
@@ -291,7 +290,7 @@ public Object getAttribute(String name) {
291290

292291
public Enumeration<String> getAttributeNames() {
293292
checkActive();
294-
return new Vector<String>(this.attributes.keySet()).elements();
293+
return Collections.enumeration(this.attributes.keySet());
295294
}
296295

297296
public String getCharacterEncoding() {
@@ -302,11 +301,11 @@ public void setCharacterEncoding(String characterEncoding) {
302301
this.characterEncoding = characterEncoding;
303302
updateContentTypeHeader();
304303
}
305-
304+
306305
private void updateContentTypeHeader() {
307306
if (this.contentType != null) {
308307
StringBuilder sb = new StringBuilder(this.contentType);
309-
if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.characterEncoding != null) {
308+
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.characterEncoding != null) {
310309
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
311310
}
312311
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
@@ -348,8 +347,7 @@ public ServletInputStream getInputStream() {
348347

349348
/**
350349
* Set a single value for the specified HTTP parameter.
351-
* <p>
352-
* If there are already one or more values registered for the given
350+
* <p>If there are already one or more values registered for the given
353351
* parameter name, they will be replaced.
354352
*/
355353
public void setParameter(String name, String value) {
@@ -358,8 +356,7 @@ public void setParameter(String name, String value) {
358356

359357
/**
360358
* Set an array of values for the specified HTTP parameter.
361-
* <p>
362-
* If there are already one or more values registered for the given
359+
* <p>If there are already one or more values registered for the given
363360
* parameter name, they will be replaced.
364361
*/
365362
public void setParameter(String name, String[] values) {
@@ -368,7 +365,7 @@ public void setParameter(String name, String[] values) {
368365
}
369366

370367
/**
371-
* Sets all provided parameters <emphasis>replacing</emphasis> any existing
368+
* Sets all provided parameters <strong>replacing</strong> any existing
372369
* values for the provided parameter names. To add without replacing
373370
* existing values, use {@link #addParameters(java.util.Map)}.
374371
*/
@@ -393,8 +390,7 @@ else if (value instanceof String[]) {
393390

394391
/**
395392
* Add a single value for the specified HTTP parameter.
396-
* <p>
397-
* If there are already one or more values registered for the given
393+
* <p>If there are already one or more values registered for the given
398394
* parameter name, the given value will be added to the end of the list.
399395
*/
400396
public void addParameter(String name, String value) {
@@ -403,8 +399,7 @@ public void addParameter(String name, String value) {
403399

404400
/**
405401
* Add an array of values for the specified HTTP parameter.
406-
* <p>
407-
* If there are already one or more values registered for the given
402+
* <p>If there are already one or more values registered for the given
408403
* parameter name, the given values will be added to the end of the list.
409404
*/
410405
public void addParameter(String name, String[] values) {
@@ -422,7 +417,7 @@ public void addParameter(String name, String[] values) {
422417
}
423418

424419
/**
425-
* Adds all provided parameters <emphasis>without</emphasis> replacing any
420+
* Adds all provided parameters <strong>without</strong> replacing any
426421
* existing values. To replace existing values, use
427422
* {@link #setParameters(java.util.Map)}.
428423
*/
@@ -566,12 +561,25 @@ public void clearAttributes() {
566561

567562
/**
568563
* Add a new preferred locale, before any existing locales.
564+
* @see #setPreferredLocales
569565
*/
570566
public void addPreferredLocale(Locale locale) {
571567
Assert.notNull(locale, "Locale must not be null");
572568
this.locales.add(0, locale);
573569
}
574570

571+
/**
572+
* Set the list of preferred locales, in descending order, effectively replacing
573+
* any existing locales.
574+
* @see #addPreferredLocale
575+
* @since 3.2
576+
*/
577+
public void setPreferredLocales(List<Locale> locales) {
578+
Assert.notEmpty(locales, "preferred locales list must not be empty");
579+
this.locales.clear();
580+
this.locales.addAll(locales);
581+
}
582+
575583
public Locale getLocale() {
576584
return this.locales.get(0);
577585
}
@@ -628,7 +636,6 @@ public int getLocalPort() {
628636
return this.localPort;
629637
}
630638

631-
632639
// ---------------------------------------------------------------------
633640
// HttpServletRequest interface
634641
// ---------------------------------------------------------------------
@@ -673,7 +680,7 @@ public void addHeader(String name, Object value) {
673680
}
674681
doAddHeaderValue(name, value, false);
675682
}
676-
683+
677684
@SuppressWarnings("rawtypes")
678685
private void doAddHeaderValue(String name, Object value, boolean replace) {
679686
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
@@ -711,6 +718,20 @@ else if (value != null) {
711718
}
712719
}
713720

721+
public String getHeader(String name) {
722+
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
723+
return (header != null ? header.getStringValue() : null);
724+
}
725+
726+
public Enumeration<String> getHeaders(String name) {
727+
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
728+
return Collections.enumeration(header != null ? header.getStringValues() : new LinkedList<String>());
729+
}
730+
731+
public Enumeration<String> getHeaderNames() {
732+
return Collections.enumeration(this.headers.keySet());
733+
}
734+
714735
public int getIntHeader(String name) {
715736
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
716737
Object value = (header != null ? header.getValue() : null);
@@ -728,20 +749,6 @@ else if (value != null) {
728749
}
729750
}
730751

731-
public String getHeader(String name) {
732-
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
733-
return (header != null ? header.getStringValue() : null);
734-
}
735-
736-
public Enumeration<String> getHeaders(String name) {
737-
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
738-
return Collections.enumeration(header != null ? header.getStringValues() : new LinkedList<String>());
739-
}
740-
741-
public Enumeration<String> getHeaderNames() {
742-
return Collections.enumeration(this.headers.keySet());
743-
}
744-
745752
public void setMethod(String method) {
746753
this.method = method;
747754
}
@@ -791,8 +798,8 @@ public void addUserRole(String role) {
791798
}
792799

793800
public boolean isUserInRole(String role) {
794-
return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext &&
795-
((MockServletContext) this.servletContext).getDeclaredRoles().contains(role)));
801+
return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext && ((MockServletContext) this.servletContext).getDeclaredRoles().contains(
802+
role)));
796803
}
797804

798805
public void setUserPrincipal(Principal userPrincipal) {

0 commit comments

Comments
 (0)