1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
35
35
import java .util .Locale ;
36
36
import java .util .Map ;
37
37
import java .util .Set ;
38
-
39
38
import javax .servlet .RequestDispatcher ;
40
39
import javax .servlet .ServletContext ;
41
40
import javax .servlet .ServletException ;
@@ -97,8 +96,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
97
96
98
97
private static final String CHARSET_PREFIX = "charset=" ;
99
98
99
+
100
100
private boolean active = true ;
101
101
102
+
102
103
// ---------------------------------------------------------------------
103
104
// ServletRequest properties
104
105
// ---------------------------------------------------------------------
@@ -140,6 +141,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
140
141
141
142
private int localPort = DEFAULT_SERVER_PORT ;
142
143
144
+
143
145
// ---------------------------------------------------------------------
144
146
// HttpServletRequest properties
145
147
// ---------------------------------------------------------------------
@@ -235,6 +237,7 @@ public MockHttpServletRequest(ServletContext servletContext, String method, Stri
235
237
this .locales .add (Locale .ENGLISH );
236
238
}
237
239
240
+
238
241
// ---------------------------------------------------------------------
239
242
// Lifecycle methods
240
243
// ---------------------------------------------------------------------
@@ -279,6 +282,7 @@ protected void checkActive() throws IllegalStateException {
279
282
}
280
283
}
281
284
285
+
282
286
// ---------------------------------------------------------------------
283
287
// ServletRequest interface
284
288
// ---------------------------------------------------------------------
@@ -351,7 +355,7 @@ public ServletInputStream getInputStream() {
351
355
* parameter name, they will be replaced.
352
356
*/
353
357
public void setParameter (String name , String value ) {
354
- setParameter (name , new String [] { value });
358
+ setParameter (name , new String [] {value });
355
359
}
356
360
357
361
/**
@@ -373,7 +377,8 @@ public void setParameter(String name, String[] values) {
373
377
public void setParameters (Map params ) {
374
378
Assert .notNull (params , "Parameter map must not be null" );
375
379
for (Object key : params .keySet ()) {
376
- Assert .isInstanceOf (String .class , key , "Parameter map key must be of type [" + String .class .getName () + "]" );
380
+ Assert .isInstanceOf (String .class , key ,
381
+ "Parameter map key must be of type [" + String .class .getName () + "]" );
377
382
Object value = params .get (key );
378
383
if (value instanceof String ) {
379
384
this .setParameter ((String ) key , (String ) value );
@@ -382,8 +387,8 @@ else if (value instanceof String[]) {
382
387
this .setParameter ((String ) key , (String []) value );
383
388
}
384
389
else {
385
- throw new IllegalArgumentException ("Parameter map value must be single value " + " or array of type ["
386
- + String .class .getName () + "]" );
390
+ throw new IllegalArgumentException (
391
+ "Parameter map value must be single value " + " or array of type [" + String .class .getName () + "]" );
387
392
}
388
393
}
389
394
}
@@ -394,7 +399,7 @@ else if (value instanceof String[]) {
394
399
* parameter name, the given value will be added to the end of the list.
395
400
*/
396
401
public void addParameter (String name , String value ) {
397
- addParameter (name , new String [] { value });
402
+ addParameter (name , new String [] {value });
398
403
}
399
404
400
405
/**
@@ -425,7 +430,8 @@ public void addParameter(String name, String[] values) {
425
430
public void addParameters (Map params ) {
426
431
Assert .notNull (params , "Parameter map must not be null" );
427
432
for (Object key : params .keySet ()) {
428
- Assert .isInstanceOf (String .class , key , "Parameter map key must be of type [" + String .class .getName () + "]" );
433
+ Assert .isInstanceOf (String .class , key ,
434
+ "Parameter map key must be of type [" + String .class .getName () + "]" );
429
435
Object value = params .get (key );
430
436
if (value instanceof String ) {
431
437
this .addParameter ((String ) key , (String ) value );
@@ -434,8 +440,8 @@ else if (value instanceof String[]) {
434
440
this .addParameter ((String ) key , (String []) value );
435
441
}
436
442
else {
437
- throw new IllegalArgumentException ("Parameter map value must be single value " + " or array of type ["
438
- + String .class .getName () + "]" );
443
+ throw new IllegalArgumentException ("Parameter map value must be single value " +
444
+ " or array of type [" + String .class .getName () + "]" );
439
445
}
440
446
}
441
447
}
@@ -456,8 +462,7 @@ public void removeAllParameters() {
456
462
}
457
463
458
464
public String getParameter (String name ) {
459
- Assert .notNull (name , "Parameter name must not be null" );
460
- String [] arr = this .parameters .get (name );
465
+ String [] arr = (name != null ? this .parameters .get (name ) : null );
461
466
return (arr != null && arr .length > 0 ? arr [0 ] : null );
462
467
}
463
468
@@ -466,8 +471,7 @@ public Enumeration<String> getParameterNames() {
466
471
}
467
472
468
473
public String [] getParameterValues (String name ) {
469
- Assert .notNull (name , "Parameter name must not be null" );
470
- return this .parameters .get (name );
474
+ return (name != null ? this .parameters .get (name ) : null );
471
475
}
472
476
473
477
public Map <String , String []> getParameterMap () {
@@ -509,8 +513,8 @@ public int getServerPort() {
509
513
public BufferedReader getReader () throws UnsupportedEncodingException {
510
514
if (this .content != null ) {
511
515
InputStream sourceStream = new ByteArrayInputStream (this .content );
512
- Reader sourceReader = (this .characterEncoding != null ) ? new InputStreamReader ( sourceStream ,
513
- this .characterEncoding ) : new InputStreamReader (sourceStream );
516
+ Reader sourceReader = (this .characterEncoding != null ) ?
517
+ new InputStreamReader ( sourceStream , this .characterEncoding ) : new InputStreamReader (sourceStream );
514
518
return new BufferedReader (sourceReader );
515
519
}
516
520
else {
@@ -574,7 +578,7 @@ public void addPreferredLocale(Locale locale) {
574
578
* @since 3.2
575
579
*/
576
580
public void setPreferredLocales (List <Locale > locales ) {
577
- Assert .notEmpty (locales , "preferred locales list must not be empty" );
581
+ Assert .notEmpty (locales , "Locale list must not be empty" );
578
582
this .locales .clear ();
579
583
this .locales .addAll (locales );
580
584
}
@@ -635,6 +639,7 @@ public int getLocalPort() {
635
639
return this .localPort ;
636
640
}
637
641
642
+
638
643
// ---------------------------------------------------------------------
639
644
// HttpServletRequest interface
640
645
// ---------------------------------------------------------------------
@@ -797,8 +802,8 @@ public void addUserRole(String role) {
797
802
}
798
803
799
804
public boolean isUserInRole (String role ) {
800
- return (this .userRoles .contains (role ) || (this .servletContext instanceof MockServletContext && (( MockServletContext ) this . servletContext ). getDeclaredRoles (). contains (
801
- role )));
805
+ return (this .userRoles .contains (role ) || (this .servletContext instanceof MockServletContext &&
806
+ (( MockServletContext ) this . servletContext ). getDeclaredRoles (). contains ( role )));
802
807
}
803
808
804
809
public void setUserPrincipal (Principal userPrincipal ) {
0 commit comments