35
35
import java .util .Map ;
36
36
import java .util .Set ;
37
37
import java .util .Vector ;
38
+
38
39
import javax .servlet .RequestDispatcher ;
39
40
import javax .servlet .ServletContext ;
40
41
import javax .servlet .ServletInputStream ;
48
49
/**
49
50
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest}
50
51
* interface. Supports the Servlet 2.5 API level.
51
- *
52
- * <p> Used for testing the web framework; also useful for testing
53
- * application controllers.
54
- *
52
+ * <p>
53
+ * Used for testing the web framework; also useful for testing application
54
+ * controllers.
55
+ *
55
56
* @author Juergen Hoeller
56
57
* @author Rod Johnson
57
58
* @author Rick Evans
@@ -90,13 +91,11 @@ public class MockHttpServletRequest implements HttpServletRequest {
90
91
*/
91
92
public static final String DEFAULT_REMOTE_HOST = "localhost" ;
92
93
93
-
94
94
private boolean active = true ;
95
95
96
-
97
- //---------------------------------------------------------------------
96
+ // ---------------------------------------------------------------------
98
97
// ServletRequest properties
99
- //---------------------------------------------------------------------
98
+ // ---------------------------------------------------------------------
100
99
101
100
private final Map <String , Object > attributes = new LinkedHashMap <String , Object >();
102
101
@@ -135,10 +134,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
135
134
136
135
private int localPort = DEFAULT_SERVER_PORT ;
137
136
138
-
139
- //---------------------------------------------------------------------
137
+ // ---------------------------------------------------------------------
140
138
// HttpServletRequest properties
141
- //---------------------------------------------------------------------
139
+ // ---------------------------------------------------------------------
142
140
143
141
private String authType ;
144
142
@@ -175,13 +173,14 @@ public class MockHttpServletRequest implements HttpServletRequest {
175
173
private boolean requestedSessionIdFromURL = false ;
176
174
177
175
178
- //---------------------------------------------------------------------
176
+ // ---------------------------------------------------------------------
179
177
// Constructors
180
- //---------------------------------------------------------------------
178
+ // ---------------------------------------------------------------------
181
179
182
180
/**
183
181
* Create a new MockHttpServletRequest with a default
184
182
* {@link MockServletContext}.
183
+ *
185
184
* @see MockServletContext
186
185
*/
187
186
public MockHttpServletRequest () {
@@ -191,6 +190,7 @@ public MockHttpServletRequest() {
191
190
/**
192
191
* Create a new MockHttpServletRequest with a default
193
192
* {@link MockServletContext}.
193
+ *
194
194
* @param method the request method (may be <code>null</code>)
195
195
* @param requestURI the request URI (may be <code>null</code>)
196
196
* @see #setMethod
@@ -203,8 +203,9 @@ public MockHttpServletRequest(String method, String requestURI) {
203
203
204
204
/**
205
205
* Create a new MockHttpServletRequest.
206
- * @param servletContext the ServletContext that the request runs in
207
- * (may be <code>null</code> to use a default MockServletContext)
206
+ *
207
+ * @param servletContext the ServletContext that the request runs in (may be
208
+ * <code>null</code> to use a default MockServletContext)
208
209
* @see MockServletContext
209
210
*/
210
211
public MockHttpServletRequest (ServletContext servletContext ) {
@@ -213,8 +214,9 @@ public MockHttpServletRequest(ServletContext servletContext) {
213
214
214
215
/**
215
216
* Create a new MockHttpServletRequest.
216
- * @param servletContext the ServletContext that the request runs in
217
- * (may be <code>null</code> to use a default MockServletContext)
217
+ *
218
+ * @param servletContext the ServletContext that the request runs in (may be
219
+ * <code>null</code> to use a default MockServletContext)
218
220
* @param method the request method (may be <code>null</code>)
219
221
* @param requestURI the request URI (may be <code>null</code>)
220
222
* @see #setMethod
@@ -228,14 +230,13 @@ public MockHttpServletRequest(ServletContext servletContext, String method, Stri
228
230
this .locales .add (Locale .ENGLISH );
229
231
}
230
232
231
-
232
- //---------------------------------------------------------------------
233
+ // ---------------------------------------------------------------------
233
234
// Lifecycle methods
234
- //---------------------------------------------------------------------
235
+ // ---------------------------------------------------------------------
235
236
236
237
/**
237
- * Return the ServletContext that this request is associated with.
238
- * (Not available in the standard HttpServletRequest interface for some reason.)
238
+ * Return the ServletContext that this request is associated with. (Not
239
+ * available in the standard HttpServletRequest interface for some reason.)
239
240
*/
240
241
public ServletContext getServletContext () {
241
242
return this .servletContext ;
@@ -273,10 +274,9 @@ protected void checkActive() throws IllegalStateException {
273
274
}
274
275
}
275
276
276
-
277
- //---------------------------------------------------------------------
277
+ // ---------------------------------------------------------------------
278
278
// ServletRequest interface
279
- //---------------------------------------------------------------------
279
+ // ---------------------------------------------------------------------
280
280
281
281
public Object getAttribute (String name ) {
282
282
checkActive ();
@@ -323,16 +323,18 @@ public ServletInputStream getInputStream() {
323
323
324
324
/**
325
325
* Set a single value for the specified HTTP parameter.
326
- * <p>If there are already one or more values registered for the given
326
+ * <p>
327
+ * If there are already one or more values registered for the given
327
328
* parameter name, they will be replaced.
328
329
*/
329
330
public void setParameter (String name , String value ) {
330
- setParameter (name , new String [] {value });
331
+ setParameter (name , new String [] { value });
331
332
}
332
333
333
334
/**
334
335
* Set an array of values for the specified HTTP parameter.
335
- * <p>If there are already one or more values registered for the given
336
+ * <p>
337
+ * If there are already one or more values registered for the given
336
338
* parameter name, they will be replaced.
337
339
*/
338
340
public void setParameter (String name , String [] values ) {
@@ -341,15 +343,15 @@ public void setParameter(String name, String[] values) {
341
343
}
342
344
343
345
/**
344
- * Sets all provided parameters <emphasis>replacing</emphasis> any
345
- * existing values for the provided parameter names. To add without
346
- * replacing existing values, use {@link #addParameters(java.util.Map)}.
346
+ * Sets all provided parameters <emphasis>replacing</emphasis> any existing
347
+ * values for the provided parameter names. To add without replacing
348
+ * existing values, use {@link #addParameters(java.util.Map)}.
347
349
*/
350
+ @ SuppressWarnings ("rawtypes" )
348
351
public void setParameters (Map params ) {
349
352
Assert .notNull (params , "Parameter map must not be null" );
350
353
for (Object key : params .keySet ()) {
351
- Assert .isInstanceOf (String .class , key ,
352
- "Parameter map key must be of type [" + String .class .getName () + "]" );
354
+ Assert .isInstanceOf (String .class , key , "Parameter map key must be of type [" + String .class .getName () + "]" );
353
355
Object value = params .get (key );
354
356
if (value instanceof String ) {
355
357
this .setParameter ((String ) key , (String ) value );
@@ -358,25 +360,26 @@ else if (value instanceof String[]) {
358
360
this .setParameter ((String ) key , (String []) value );
359
361
}
360
362
else {
361
- throw new IllegalArgumentException (
362
- "Parameter map value must be single value " + " or array of type [" + String .class .getName () +
363
- "]" );
363
+ throw new IllegalArgumentException ("Parameter map value must be single value " + " or array of type ["
364
+ + String .class .getName () + "]" );
364
365
}
365
366
}
366
367
}
367
368
368
369
/**
369
370
* Add a single value for the specified HTTP parameter.
370
- * <p>If there are already one or more values registered for the given
371
+ * <p>
372
+ * If there are already one or more values registered for the given
371
373
* parameter name, the given value will be added to the end of the list.
372
374
*/
373
375
public void addParameter (String name , String value ) {
374
- addParameter (name , new String [] {value });
376
+ addParameter (name , new String [] { value });
375
377
}
376
378
377
379
/**
378
380
* Add an array of values for the specified HTTP parameter.
379
- * <p>If there are already one or more values registered for the given
381
+ * <p>
382
+ * If there are already one or more values registered for the given
380
383
* parameter name, the given values will be added to the end of the list.
381
384
*/
382
385
public void addParameter (String name , String [] values ) {
@@ -394,15 +397,15 @@ public void addParameter(String name, String[] values) {
394
397
}
395
398
396
399
/**
397
- * Adds all provided parameters <emphasis>without</emphasis> replacing
398
- * any existing values. To replace existing values, use
400
+ * Adds all provided parameters <emphasis>without</emphasis> replacing any
401
+ * existing values. To replace existing values, use
399
402
* {@link #setParameters(java.util.Map)}.
400
403
*/
404
+ @ SuppressWarnings ("rawtypes" )
401
405
public void addParameters (Map params ) {
402
406
Assert .notNull (params , "Parameter map must not be null" );
403
407
for (Object key : params .keySet ()) {
404
- Assert .isInstanceOf (String .class , key ,
405
- "Parameter map key must be of type [" + String .class .getName () + "]" );
408
+ Assert .isInstanceOf (String .class , key , "Parameter map key must be of type [" + String .class .getName () + "]" );
406
409
Object value = params .get (key );
407
410
if (value instanceof String ) {
408
411
this .addParameter ((String ) key , (String ) value );
@@ -411,14 +414,15 @@ else if (value instanceof String[]) {
411
414
this .addParameter ((String ) key , (String []) value );
412
415
}
413
416
else {
414
- throw new IllegalArgumentException ("Parameter map value must be single value " +
415
- " or array of type [" + String .class .getName () + "]" );
417
+ throw new IllegalArgumentException ("Parameter map value must be single value " + " or array of type ["
418
+ + String .class .getName () + "]" );
416
419
}
417
420
}
418
421
}
419
422
420
423
/**
421
- * Remove already registered values for the specified HTTP parameter, if any.
424
+ * Remove already registered values for the specified HTTP parameter, if
425
+ * any.
422
426
*/
423
427
public void removeParameter (String name ) {
424
428
Assert .notNull (name , "Parameter name must not be null" );
@@ -486,8 +490,8 @@ public int getServerPort() {
486
490
public BufferedReader getReader () throws UnsupportedEncodingException {
487
491
if (this .content != null ) {
488
492
InputStream sourceStream = new ByteArrayInputStream (this .content );
489
- Reader sourceReader = (this .characterEncoding != null ) ?
490
- new InputStreamReader ( sourceStream , this .characterEncoding ) : new InputStreamReader (sourceStream );
493
+ Reader sourceReader = (this .characterEncoding != null ) ? new InputStreamReader ( sourceStream ,
494
+ this .characterEncoding ) : new InputStreamReader (sourceStream );
491
495
return new BufferedReader (sourceReader );
492
496
}
493
497
else {
@@ -599,10 +603,9 @@ public int getLocalPort() {
599
603
return this .localPort ;
600
604
}
601
605
602
-
603
- //---------------------------------------------------------------------
606
+ // ---------------------------------------------------------------------
604
607
// HttpServletRequest interface
605
- //---------------------------------------------------------------------
608
+ // ---------------------------------------------------------------------
606
609
607
610
public void setAuthType (String authType ) {
608
611
this .authType = authType ;
@@ -622,21 +625,25 @@ public Cookie[] getCookies() {
622
625
623
626
/**
624
627
* Add a header entry for the given name.
625
- * <p>If there was no entry for that header name before,
626
- * the value will be used as-is. In case of an existing entry,
627
- * a String array will be created, adding the given value (more
628
- * specifically, its toString representation) as further element.
629
- * <p>Multiple values can only be stored as list of Strings,
630
- * following the Servlet spec (see <code>getHeaders</code> accessor).
631
- * As alternative to repeated <code>addHeader</code> calls for
632
- * individual elements, you can use a single call with an entire
633
- * array or Collection of values as parameter.
628
+ * <p>
629
+ * If there was no entry for that header name before, the value will be used
630
+ * as-is. In case of an existing entry, a String array will be created,
631
+ * adding the given value (more specifically, its toString representation)
632
+ * as further element.
633
+ * <p>
634
+ * Multiple values can only be stored as list of Strings, following the
635
+ * Servlet spec (see <code>getHeaders</code> accessor). As alternative to
636
+ * repeated <code>addHeader</code> calls for individual elements, you can
637
+ * use a single call with an entire array or Collection of values as
638
+ * parameter.
639
+ *
634
640
* @see #getHeaderNames
635
641
* @see #getHeader
636
642
* @see #getHeaders
637
643
* @see #getDateHeader
638
644
* @see #getIntHeader
639
645
*/
646
+ @ SuppressWarnings ("rawtypes" )
640
647
public void addHeader (String name , Object value ) {
641
648
HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
642
649
Assert .notNull (value , "Header value must not be null" );
@@ -665,8 +672,8 @@ else if (value instanceof Number) {
665
672
return ((Number ) value ).longValue ();
666
673
}
667
674
else if (value != null ) {
668
- throw new IllegalArgumentException (
669
- "Value for header '" + name + "' is neither a Date nor a Number: " + value );
675
+ throw new IllegalArgumentException ("Value for header '" + name + "' is neither a Date nor a Number: "
676
+ + value );
670
677
}
671
678
else {
672
679
return -1L ;
0 commit comments