-
Notifications
You must be signed in to change notification settings - Fork 79
/
RequestTests.java
2685 lines (2268 loc) · 83.2 KB
/
RequestTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2007, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
/*
* $Id$
*/
package com.sun.ts.tests.servlet.common.servlets;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import java.util.Vector;
import com.sun.ts.tests.servlet.common.util.Data;
import com.sun.ts.tests.servlet.common.util.ServletTestUtil;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletRequestWrapper;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
public class RequestTests {
static String savedSessionId = null;
static String savedRequestedSessionId = null;
static HttpSession savedSession = null;
// ------------------------ jakarta_servlet.RequestRequest
// -------------------------------
public static void requestWrapperConstructorTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
ServletRequestWrapper srw = new ServletRequestWrapper(request);
if (srw != null) {
ServletTestUtil.printResult(pw, true);
} else {
ServletTestUtil.printResult(pw, false);
}
}
public static void requestWrapperGetRequestTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
ServletRequestWrapper srw = new ServletRequestWrapper(request);
if (srw != null) {
ServletRequest sr = srw.getRequest();
if (!request.equals(sr)) {
passed = false;
pw.println("getRequest failed to return the same request object");
} else {
passed = true;
}
} else {
passed = false;
pw.println("constructor returned a null");
}
ServletTestUtil.printResult(pw, passed);
}
public static void requestWrapperSetRequestTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = true;
ServletRequestWrapper srw = new ServletRequestWrapper(request);
if (srw != null) {
try {
srw.setRequest(request);
} catch (Throwable t) {
passed = false;
pw.println("Error: setRequest generated a Throwable");
pw.println("Exception:" + t.getMessage());
}
if (passed) {
ServletRequest sr = srw.getRequest();
if (!request.equals(sr)) {
passed = false;
pw.println("getRequest failed to return the same request object");
}
}
} else {
passed = false;
pw.println("constructor returned a null");
}
ServletTestUtil.printResult(pw, passed);
}
public static void requestWrapperSetRequestIllegalArgumentExceptionTest(
PrintWriter pw, ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
ServletRequestWrapper srw = new ServletRequestWrapper(request);
if (srw != null) {
try {
srw.setRequest(null);
passed = false;
pw.println(
"Error: an IllegalArgumentException should have been generated");
} catch (Throwable t) {
if (t instanceof IllegalArgumentException) {
passed = true;
} else {
passed = false;
pw.println(
"Exception thrown, but was not an instance of IllegalArgumentException.");
pw.println("instead received: " + t.getClass().getName());
}
}
} else {
passed = false;
pw.println("constructor returned a null");
}
ServletTestUtil.printResult(pw, passed);
}
public static void getAttributeNamesTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
;
Enumeration e = request.getAttributeNames();
Object[] attr = ServletTestUtil.getAsArray(e);
for (int i = 0, len = attr.length; i < len; i++) {
request.removeAttribute((String) attr[i]);
}
String attribute1 = "attribute1";
String attribute2 = "attribute2";
request.setAttribute(attribute1, "value1");
request.setAttribute(attribute2, "value2");
String[] expected = { attribute1, attribute2 };
e = request.getAttributeNames();
if (!ServletTestUtil.checkEnumeration(e, expected)) {
passed = false;
ServletTestUtil.printFailureData(pw, e, expected);
} else {
passed = true;
}
ServletTestUtil.printResult(pw, passed);
}
public static void getAttributeNamesEmptyEnumTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
Enumeration e = request.getAttributeNames();
Object[] attr = ServletTestUtil.getAsArray(e);
for (int i = 0, len = attr.length; i < len; i++) {
request.removeAttribute((String) attr[i]);
}
e = request.getAttributeNames();
if (!e.hasMoreElements()) {
passed = true;
} else {
passed = false;
pw.println(
"getAttributeNames() returned a non empty enumeration after all attributes were removed");
pw.println("The values returned were:");
while (e.hasMoreElements()) {
pw.println(" " + (String) e.nextElement());
}
}
ServletTestUtil.printResult(pw, passed);
}
public static void getAttributeTest(PrintWriter pw, ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
request.setAttribute("attribute1", "value1");
Object o = request.getAttribute("attribute1");
if (o != null) {
if (o instanceof String) {
String attr = (String) o;
if (!attr.equals("value1")) {
passed = false;
pw.println("getAttribute() returned incorrect value");
pw.println("Expected Attribute Value -> value1");
pw.println("Actual Attribute value returned -> + attr ");
} else {
passed = true;
}
} else {
passed = false;
pw.println("getAttribute() returned an Object of type String");
}
} else {
passed = false;
pw.println("getAttribute() returned a null attribute");
}
ServletTestUtil.printResult(pw, passed);
}
public static void getAttributeDoesNotExistTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
Object o = request.getAttribute("doesnotexist");
// No attribute was set. Expecting null value
if (o != null) {
passed = false;
pw.println("getAttribute() returned incorrect value");
pw.println("Expected Attribute Value -> null");
pw.println("Actual Attribute value returned -> + o ");
} else {
passed = true;
}
ServletTestUtil.printResult(pw, passed);
}
public static void getCharacterEncodingTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
// getting char encoding
String expectedResult = "ISO-8859-1";
String encoding = request.getCharacterEncoding();
if (encoding != null) {
if (!encoding.equals(expectedResult)) {
passed = false;
pw.println(
"getCharacterEncoding() did not receive the proper encoding");
pw.println("Expected result = " + expectedResult);
pw.println("Actual result = |" + encoding + "|");
} else {
passed = true;
}
} else {
passed = false;
pw.println("getCharacterEncoding() returned a null result");
}
ServletTestUtil.printResult(pw, passed);
}
public static void getContentLengthTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
// get the content length
int contentLength = request.getContentLength();
if (contentLength > 0) {
int len = 0;
// getting input stream
ServletInputStream sin = request.getInputStream();
// read from the stream
while (sin.read() != -1) {
len++;
}
// did we get what we wrote
if ((contentLength != len) && (contentLength != -1)) {
passed = false;
pw.println("getContentLength() method FAILED");
pw.println("Expected Value returned ->" + contentLength);
pw.println("Actual Value returned -> " + len);
} else {
passed = true;
}
} else {
passed = false;
pw.println("getContentLength() returned an incorrect length");
pw.println("Expected length = > 0");
pw.println("Actual length = " + contentLength);
}
ServletTestUtil.printResult(pw, passed);
}
public static void getContentTypeTest(PrintWriter pw, ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
String expectedResult = "text/plain";
String contentType = request.getContentType();
if (contentType != null) {
if (!expectedResult.equals(contentType)) {
passed = false;
pw.println("getContentType() did not receive the proper content type");
pw.println("Expected result=" + expectedResult);
pw.println("Actual result=" + contentType);
} else {
passed = true;
}
} else {
passed = false;
pw.println("getContentType() returned a null");
}
ServletTestUtil.printResult(pw, passed);
}
public static void getContentTypeNullTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
String contentType = request.getContentType();
if (contentType != null) {
passed = false;
pw.println("getContentType() did not a return a null");
pw.println("Actual result=" + contentType);
} else {
passed = true;
}
ServletTestUtil.printResult(pw, passed);
}
public static void getInputStreamTest(PrintWriter pw, ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
String expected = "calling getInputStreamTest";
StringBuffer sb = new StringBuffer();
ServletInputStream sin = request.getInputStream();
int tmp = 0;
try {
tmp = sin.read();
while (tmp != -1) {
sb.append((char) tmp);
tmp = sin.read();
}
String actual = sb.toString();
if (actual != null) {
if (!actual.equals(expected)) {
passed = false;
pw.println("getInputStream returned the wrong body content");
pw.println("expected =" + expected);
pw.println("actual =" + actual);
} else {
passed = true;
}
} else {
passed = false;
pw.println("getInputStream returned null body content");
}
} catch (Throwable t) {
passed = false;
pw.println("read() generated an exception");
t.printStackTrace(pw);
}
ServletTestUtil.printResult(pw, passed);
}
public static void getInputStreamIllegalStateExceptionTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
// getting Reader object
request.getReader();
try {
// we already got reader object
// IllegalStateException should be thrown
ServletInputStream sin = request.getInputStream();
passed = false;
pw.println(
"getInputStream should have thrown an IllegalStateException exception");
} catch (Throwable t) {
if (t instanceof IllegalStateException) {
passed = true;
} else {
passed = false;
pw.println(
"Exception thrown, but was not an instance of IllegalStateException .");
pw.println("instead received: " + t.getClass().getName());
}
}
ServletTestUtil.printResult(pw, passed);
}
public static void getLocaleTest(PrintWriter pw, ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
Locale expectedResult = new Locale("en", "US");
Locale actual = request.getLocale();
if (actual != null) {
if (!actual.equals(expectedResult)) {
passed = false;
pw.println("getLocale() did not receive the proper locale");
pw.println("Expected result = " + expectedResult.getLanguage() + "-"
+ expectedResult.getCountry());
pw.println("Actual result = |" + actual.getLanguage() + "-"
+ actual.getCountry() + "|");
} else {
passed = true;
}
} else {
passed = false;
pw.println("getLocale() returned a null result");
}
ServletTestUtil.printResult(pw, passed);
}
public static void getLocaleDefaultTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
Locale actual = request.getLocale();
if (actual != null) {
if ((actual.getLanguage() == null) || (actual.getCountry() == null)) {
if (actual.getLanguage() == null) {
passed = false;
pw.println("Locale.getLanguage() returned a null");
}
if (actual.getCountry() == null) {
passed = false;
pw.println("Locale.getCountry() returned a null");
}
} else {
passed = true;
}
} else {
passed = false;
pw.println("getLocale() returned a null result");
}
ServletTestUtil.printResult(pw, passed);
}
public static void getLocalesDefaultTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
Enumeration e = request.getLocales();
Object[] result = ServletTestUtil.getAsArray(e);
if (result.length == 1) {
Locale locale = ((Locale) result[0]);
if (locale != null) {
if ((locale.getLanguage() == null) || (locale.getCountry() == null)) {
if (locale.getLanguage() == null) {
passed = false;
pw.println("Locale.getLanguage() returned a null");
}
if (locale.getCountry() == null) {
passed = false;
pw.println("Locale.getCountry() returned a null");
}
} else {
passed = true;
}
} else {
passed = false;
pw.println("getLocales() returned a null result");
}
} else if (result.length <= 0) {
passed = false;
pw.println("getLocales() did not return any locales");
} else {
passed = false;
pw.println("getLocales() returned more than 1 locale");
pw.println("The locales received were :");
for (int i = 0; i < result.length; i++) {
pw.println(" " + ((Locale) result[i]).getLanguage() + "-"
+ ((Locale) result[i]).getCountry());
}
}
ServletTestUtil.printResult(pw, passed);
}
public static void getLocalesTest(PrintWriter pw, ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = true;
Enumeration e = request.getLocales();
Locale expectedResult1 = new Locale("en", "US");
boolean expectedResult1Found = false;
int expectedResult1order = 1;
Locale expectedResult2 = new Locale("en", "GB");
boolean expectedResult2Found = false;
int expectedResult2order = 2;
int expectedCount = 2;
int count = 0;
if (e.hasMoreElements()) {
Vector v = new Vector();
while (e.hasMoreElements()) {
Locale result = ((Locale) e.nextElement());
if (result.equals(expectedResult1)) {
if (!expectedResult1Found) {
count++;
expectedResult1Found = true;
if (count != expectedResult1order) {
passed = false;
pw.println(
"getLocales() did not return the locale in the correct order");
pw.println("expected order=" + expectedResult1order);
pw.println("actual order=" + count);
}
} else {
passed = false;
pw.println("getLocales() method return the same locale name twice");
pw.println("The locale already received was "
+ expectedResult1.getLanguage() + "-"
+ expectedResult1.getCountry());
}
} else if (result.equals(expectedResult2)) {
if (!expectedResult2Found) {
count++;
expectedResult2Found = true;
if (count != expectedResult2order) {
passed = false;
pw.println(
"getLocales() did not return the locale in the correct order");
pw.println("expected order=" + expectedResult2order);
pw.println("actual order=" + count);
}
} else {
passed = false;
pw.println("getLocales() method return the same locale name twice");
pw.println("The locale already received was "
+ expectedResult2.getLanguage() + "-"
+ expectedResult2.getCountry());
}
} else {
v.add(result);
}
}
if (count != expectedCount) {
passed = false;
pw.println("getLocales() did not return the proper number of locales");
pw.println("Expected count = " + expectedCount);
pw.println("Actual count = |" + count + "|");
pw.println("The expected locales received were :");
if (expectedResult1Found) {
pw.println(" " + expectedResult1.getLanguage() + "-"
+ expectedResult1.getCountry());
}
if (expectedResult2Found) {
pw.println(" " + expectedResult2.getLanguage() + "-"
+ expectedResult2.getCountry());
}
pw.println("Other locales received were :");
for (int i = 0; i < v.size(); i++) {
pw.println(" " + ((Locale) v.elementAt(i)).getLanguage() + "-"
+ ((Locale) v.elementAt(i)).getCountry());
}
}
} else {
passed = false;
pw.println("getLocales() returned an empty enumeration");
}
ServletTestUtil.printResult(pw, passed);
}
public static void getParameterMapTest(PrintWriter pw, ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = true;
Map map = request.getParameterMap();
int count = 0;
int expectedCount = 2;
String expectedResult1 = "parameter1";
String expectedResult1A = "value1";
int expectedCount1 = 1;
String expectedResult2 = "parameter2";
String expectedResult2A = "value2";
int expectedCount2 = 1;
if (map.containsKey(expectedResult1)) {
String result[] = (String[]) map.get(expectedResult1);
int count2 = result.length;
if (count2 == expectedCount1) {
if (result[0].equals(expectedResult1A)) {
count++;
} else {
passed = false;
pw.println(
"getParameterMap() did not return the proper value for the specified key");
pw.println("Expected result = " + expectedResult1A);
pw.println("Actual result = " + result[0]);
}
} else {
passed = false;
pw.println(
"Map.get() returned the wrong number of parameter values for key "
+ expectedResult1);
pw.println("Expected number = " + expectedCount1);
pw.println("Actual number = " + count2);
}
}
if (map.containsKey(expectedResult2)) {
String result[] = (String[]) map.get(expectedResult2);
int count2 = result.length;
if (count2 == expectedCount1) {
if (result[0].equals(expectedResult2A)) {
count++;
} else {
passed = false;
pw.println(
"getParameterMap() did not return the proper value for the specified key");
pw.println("Expected result = " + expectedResult2A);
pw.println("Actual result = " + result[0]);
}
} else {
passed = false;
pw.println(
"Map.get() returned the wrong number of parameter values for key "
+ expectedResult2);
pw.println("Expected number = " + expectedCount2);
pw.println("Actual number = " + count2);
}
}
if (count != expectedCount) {
passed = false;
pw.println("getParameterMap() return the proper keys and values ");
pw.println("Expected count = " + expectedCount);
pw.println("Actual count = |" + count + "|");
}
ServletTestUtil.printResult(pw, passed);
}
public static void getParameterNamesTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
String parameter1 = "parameter1";
String parameter2 = "parameter2";
String[] expected = { parameter1, parameter2 };
Enumeration e = request.getParameterNames();
if (!ServletTestUtil.checkEnumeration(e, expected, false, false)) {
passed = false;
ServletTestUtil.printFailureData(pw, e, expected);
} else {
passed = true;
}
ServletTestUtil.printResult(pw, passed);
}
public static void getParameterNamesEmptyEnumTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
// no parameter was set in the client side
Enumeration e = request.getParameterNames();
if (e.hasMoreElements()) {
passed = false;
pw.println(
"getParameterNames() return an enumerated list when it should have been empty");
pw.println("The list contains the following items:");
ServletTestUtil.getAsString(e);
} else {
passed = true;
}
ServletTestUtil.printResult(pw, passed);
}
public static void getParameterTest(PrintWriter pw, ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
// getting the request parameter
String param = "parameter1";
String actual = request.getParameter(param);
String expectedResult = "value1";
if (actual != null) {
// is param an instance of java.lang.String
if (!actual.equals(expectedResult)) {
passed = false;
pw.println("getParameter() did not return the correct value");
pw.println("Expected result = " + expectedResult);
pw.println("Actual result = |" + actual + "|");
} else {
passed = true;
}
} else {
passed = false;
pw.println("getParameter() did not return the correct value");
pw.println("Expected result = " + expectedResult);
pw.println("Actual result = |null|");
}
ServletTestUtil.printResult(pw, passed);
}
public static void getParameterValuesTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = true;
String[] names = request.getParameterValues("Names");
int enumlength = names.length;
String expectedResult1 = "value1";
String expectedResult2 = "value2";
boolean expectedResult1Found = false;
boolean expectedResult2Found = false;
int expectedCount = 2;
int count = 0;
int arraycount = 0;
if (enumlength > 0) {
Vector v = new Vector();
while (arraycount < enumlength) {
String result = names[arraycount++];
if (result.equals(expectedResult1)) {
if (!expectedResult1Found) {
count++;
expectedResult1Found = true;
} else {
passed = false;
pw.println("getParameterValues() return the same value twice ");
pw.println("The value already received was " + expectedResult1);
}
}
if (result.equals(expectedResult2)) {
if (!expectedResult2Found) {
count++;
expectedResult2Found = true;
} else {
passed = false;
pw.println("getParameterValues() return the same value twice ");
pw.println("The value already received was " + expectedResult2);
}
} else {
v.add(result);
}
}
if (count != expectedCount) {
passed = false;
pw.println(
"getParameterValues() did not return the proper number of parameter values");
pw.println("Expected count = " + expectedCount);
pw.println("Actual count = |" + count + "|");
pw.println("The expected parameter values received were :");
if (expectedResult1Found) {
pw.println(expectedResult1);
}
if (expectedResult2Found) {
pw.println(expectedResult2);
}
pw.println("Other parameter values received were :");
for (int i = 0; i < v.size(); i++) {
pw.println(" " + v.elementAt(i).toString());
}
}
} else {
passed = false;
pw.println("getParameterValues() returned an empty array");
}
ServletTestUtil.printResult(pw, passed);
}
public static void getParameterValuesDoesNotExistTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
// no parameter was set with name zero
String[] vals = request.getParameterValues("doesnotexit");
if (vals != null) {
passed = false;
pw.println(
"getParameterValues() returned a null list of parameter values");
} else {
passed = true;
}
ServletTestUtil.printResult(pw, passed);
}
public static void getParameterDoesNotExistTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
String expectedResult = null;
String actual = request.getParameter("doesnotexist");
// we are not settting any parameter in the client side so we should get
// null
if (actual != expectedResult) {
passed = false;
pw.println("getParameter() did not return the correct result");
pw.println("Expected result = " + expectedResult);
pw.println("Actual result = |" + actual + "|");
} else {
passed = true;
}
ServletTestUtil.printResult(pw, passed);
}
public static void getProtocolTest(PrintWriter pw, ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
String proto = request.getProtocol();
String expectedResult = "HTTP/1.";
// looking for HTTP
int actual = proto.indexOf(expectedResult);
if (actual == -1) {
passed = false;
pw.println("getProtocol() did not return the correct value");
pw.println("Expected result = " + expectedResult);
pw.println("Actual count = |" + proto + "|");
} else {
passed = true;
}
ServletTestUtil.printResult(pw, passed);
}
public static void getReaderTest(PrintWriter pw, ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
String expected = "calling getReaderTest";
BufferedReader br = request.getReader();
StringBuffer sb = new StringBuffer();
try {
String tmp = br.readLine();
while (tmp != null) {
sb.append(tmp);
tmp = br.readLine();
}
String actual = sb.toString();
if (actual != null) {
if (!actual.equals(expected)) {
passed = false;
pw.println("getReaderTest returned the wrong body content");
pw.println("expected =" + expected);
pw.println("actual =" + actual);
} else {
passed = true;
}
} else {
passed = false;
pw.println("getReaderTest returned null body content");
}
} catch (Throwable t) {
passed = false;
pw.println("readLine() generated an exception");
t.printStackTrace(pw);
}
ServletTestUtil.printResult(pw, passed);
}
public static void getReaderIllegalStateExceptionTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
request.getInputStream();
try {
BufferedReader br = request.getReader();
passed = false;
pw.println("getReader() method did not throw IllegalStateException");
} catch (Throwable t) {
if (t instanceof IllegalStateException) {
passed = true;
} else {
passed = false;
pw.println(
"Exception thrown, but was not an instance of IllegalStateException.");
pw.println("instead received: " + t.getClass().getName());
}
}
ServletTestUtil.printResult(pw, passed);
}
public static void getReaderUnsupportedEncodingExceptionTest(PrintWriter pw,
ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
try {
BufferedReader br = request.getReader();
passed = false;
pw.println(
"getReader() method did not throw UnsupportedEncodingException");
} catch (Throwable t) {