-
Notifications
You must be signed in to change notification settings - Fork 78
/
TestServlet.java
991 lines (846 loc) · 31.4 KB
/
TestServlet.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
/*
* Copyright (c) 2009, 2021 Oracle and/or its affiliates and others.
* 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 servlet.tck.api.jakarta_servlet.servletcontext;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import servlet.tck.common.servlets.GenericTCKServlet;
import servlet.tck.common.util.Data;
import servlet.tck.common.util.ServletTestUtil;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
public class TestServlet extends GenericTCKServlet {
/**
* Test for ServletContext.getMajorVersion method
*/
public void getMajorVersion(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
// Version should be 6 for Servlet 6.0 release
if (context.getMajorVersion() == 6) {
passed = true;
} else {
passed = false;
pw.println("getMajorVersion() returned " + context.getMajorVersion());
pw.println("Expected ServletContext.getMajorVersion() -> 6 ");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test for ServletContext.getMinorVersion method
*/
public void getMinorVersion(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
int expectedResult = 0;
int result = context.getMinorVersion();
if (result == expectedResult) {
passed = true;
} else {
passed = false;
pw.println("getMinorVersion() returned incorrect result");
pw.println("Expected result = " + expectedResult + " ");
pw.println("Actual result = " + result + " ");
}
ServletTestUtil.printResult(pw, passed);
}
private static final String EXPECTED_MIME_TYPE = "application/x-java-class";
private static final String MIME_OBJECT = "/WEB-INF/classes/tests/jakarta_servlet/ServletContext/GetMimeTypeTestServlet.class";
public void getMimeType(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
String result = context.getMimeType(MIME_OBJECT);
if (result != null) {
if (result.equals(EXPECTED_MIME_TYPE)) {
passed = true;
} else {
passed = false;
pw.println("getMimeType(" + MIME_OBJECT
+ ") returned incorrect result\n\t" + "Expected result = "
+ EXPECTED_MIME_TYPE + "\n\t" + "Actual result = " + result);
}
} else {
passed = false;
pw.println("getMimeType( " + MIME_OBJECT + " ) returned a null result\n\t"
+ "Expected result = " + EXPECTED_MIME_TYPE);
}
ServletTestUtil.printResult(pw, passed);
}
/**
* A Negative Test for ServletContext.getMimeType(String) method
*/
/**
* We set improper mime type We should get null as the result
*/
public void getMimeType_1(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
// abraca.dabra? i hope there is no such mime type so far
String param = "/META-INF/MANIFEST.MF";
String mimeType = context.getMimeType(param);
if (mimeType == null) {
passed = true;
} else {
passed = false;
pw.println("getMimeType(" + param + ") did not return null");
pw.println("Actual result = " + mimeType + " ");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test for ServletContext.getRealPath(String) method
*/
public void getRealPath(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = true;
PrintWriter pw = response.getWriter();
String path = "/servlet_js_ServletContext/Test";
String win32Path = new StringBuffer(45).append(File.separatorChar)
.append("servlet_js_ServletContext").append(File.separatorChar)
.append("Test").toString();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
String realPath = context.getRealPath(path);
// a null will be returned if running directly from a jar file
if ((realPath == null) || (realPath.indexOf(path) > -1) || // UNIX path
(realPath.indexOf(win32Path) > -1)) // Win32 path
{
pw.println("realPath = " + realPath);
} else {
passed = false;
pw.println("getRealPath(" + path + ") did not contain the named files");
pw.println("Actual result = " + realPath + " ");
}
// Leading '/' is optional. Ensure the result is the same with or without it
String pathNoSolidus = path.substring(1);
String realPathNoSolidus = context.getRealPath(pathNoSolidus);
if (realPath == null && realPathNoSolidus == null ||
realPath != null && realPath.equals(realPathNoSolidus)) {
pw.println("realPathNoSolidus = " + realPathNoSolidus);
} else {
passed = false;
pw.println("getRealPath(" + path + ") returned [" + realPath + "]");
pw.println("getRealPath(" + pathNoSolidus + ") returned [" + realPathNoSolidus + "]");
pw.println("The return values should be the same");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test for ServletContext.getResourcePaths(String) method
*/
public void getResourcePaths(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
Iterator resources = null;
String tmp = null;
String path = "/WEB-INF/";
Set realPath = context.getResourcePaths(path);
pw.println("GetResourcePathsTest: Start test for path=" + path);
if (realPath != null) {
if (!realPath.isEmpty()) {
resources = realPath.iterator();
tmp = (String) resources.next();
pw.println("GetResourcePathsTest: " + tmp);
while (resources.hasNext()) {
pw.println("GetResourcePathsTest: " + resources.next());
}
} else {
passed = false;
}
} else {
passed = false;
pw.println("GetResourcePathsTest: returned null resourcepath");
}
path = "/WEB-INF/classes/servlet/tck/api/jakarta_servlet/servletcontext/";
realPath = context.getResourcePaths(path);
pw.println("GetResourcePathsTest: Start test for path=" + path);
if (realPath != null) {
if (!realPath.isEmpty()) {
resources = realPath.iterator();
tmp = (String) resources.next();
pw.println("GetResourcePathsTest: " + tmp);
while (resources.hasNext()) {
pw.println("GetResourcePathsTest: " + resources.next());
}
} else {
passed = false;
pw.println("Error: missing contents");
}
} else {
passed = false;
pw.println("Error: null resourcepath");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* A Test for getResourceAs Stream method
*/
public void getResourceAsStream(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
// get this servlet itself
String path = "/WEB-INF/web.xml";
InputStream in = context.getResourceAsStream(path);
if (in != null) {
passed = true;
} else {
passed = false;
pw.println("getResourceAsStream(" + path + ") returned a null");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* A Negative Test for ServletContext.getResourceAsStream(String)
*/
/**
* We will supply a non existent URL and should get null
*/
public void getResourceAsStream_1(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
// dummy URL
String path = "/dummy";
InputStream in = context.getResourceAsStream(path);
if (in == null) {
passed = true;
} else {
passed = false;
pw.println("getResourceAsStream(" + path + ") returned a non-null ");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* We will use this servlet itself as a resource
*/
public void getResource(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
String path = "/WEB-INF/web.xml";
URL resourceURL = context.getResource(path);
if (resourceURL != null) {
String result = resourceURL.toString();
if (result.indexOf(path) > -1) {
passed = true;
} else {
passed = false;
pw.println("Could not locate " + path + " in the returned URL");
pw.println("Actual result = " + result + " ");
}
} else {
passed = false;
pw.println("getResource(" + path + ") returned a null");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* A Negative Test for ServletContext.getResourse(String) method We will give
* a fake URL we should get null
*/
public void getResource_1(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
String path = "/doesnotexist";
URL resourceURL = context.getResource(path);
if (resourceURL == null) {
passed = true;
} else {
passed = false;
pw.println("ServletContext.getResource(" + path
+ ") did not return correct result");
pw.println("Expected result = null");
pw.println("Actual result = |" + resourceURL + "|");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* A Negative Test for ServletContext.getResource(String path) We get
* MalformedURLException if path does not start with /.
*/
public void getResource_2(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
try {
URL resourceURL = context.getResource("invalid/contextPath");
passed = false;
pw.println("Test FAILED. Expected a MalformedURLException to "
+ "be thrown when ServletContext.getResource(String) "
+ " is provided a value that does not start with a '/'");
} catch (java.net.MalformedURLException ex) {
passed = true;
pw.println("GetResource_2Test " + Data.PASSED);
pw.println("Expected java.net.MalformedURLException to be thrown");
} catch (Throwable t) {
passed = false;
pw.println("GetResource_2Test " + Data.FAILED);
pw.println("Unexpected Exception is thrown: " + t.toString());
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test for ServletContext.getAttribute(String) method
*/
public void servletContextGetAttribute(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
String param = "ManKind";
String param2 = "humane";
// set attribute
context.setAttribute(param, param2);
// then get attribute
Object attr = context.getAttribute(param);
if (attr != null) {
// attr should also be an instance of java.lang.String
if (attr.getClass().getName().equals("java.lang.String")) {
String sAttr = (String) attr;
if (sAttr.equals(param2)) {
passed = true;
} else {
passed = false;
pw.println("getAttribute(" + param + ") returned incorrect results");
pw.println("Expected result = " + param2 + " ");
pw.println("Actual result = |" + sAttr + "| ");
}
} else {
passed = false;
pw.println("getAttribute(" + param
+ ") did not return an attribute of type String");
}
} else {
passed = false;
pw.println("getAttribute(" + param + ") returned a null result ");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* A Negative Test for ServletContext.getAttribute(String) method
*/
/**
* We'll try to get the associated value for a non existent attribute name. We
* should get null as the result
*/
public void servletContextGetAttribute_1(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
Object attr = context.getAttribute("Womankind");
if (attr == null) {
passed = true;
} else {
passed = false;
pw.println("getAttribute(String) did not return null");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test for ServletContext.getContext(String) method
*/
public void servletContextGetContext(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
String alternateServlet = request.getServletContext().getContextPath()
+ "/GetNamedDispatcherTest";
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
// we expect ServletContext object that corresponds to the named URL or Null
ServletContext context2 = context.getContext(alternateServlet);
if ((context2 == context) || (context2 == null)) {
passed = true;
} else {
passed = false;
pw.println("getContext(String) returned incorrect result");
pw.println(
"Expected getServletContext(String) to return the same context for another servlet ");
pw.println(
" in the same context as the current test servlet, or to return a null value.");
pw.println(
"Context returned from getServletConfig().getServletContext(): "
+ context);
pw.println(
"Context returned from context.getContext(String): " + context2);
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test for ServletContext.getInitParameterNames() method
*/
public void servletContextGetInitParameterNames(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = true;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
// We attributed VI to EDITOR and OS to ULTASPARC
String expectedResult1 = "EDITOR";
boolean expectedResult1Found = false;
String expectedResult2 = "OS";
boolean expectedResult2Found = false;
Enumeration initP = context.getInitParameterNames();
if (initP.hasMoreElements()) {
while (initP.hasMoreElements()) {
String name = (String) initP.nextElement();
pw.println("Initialization parameter: " + name);
if (name.equals(expectedResult1)) {
if (!expectedResult1Found) {
expectedResult1Found = true;
} else {
passed = false;
pw.println(
"getInitParameterNames() method return an attribute name twice ");
pw.println(
"The attribute already specified was " + expectedResult1 + " ");
}
} else if (name.equals(expectedResult2)) {
if (!expectedResult2Found) {
expectedResult2Found = true;
} else {
passed = false;
pw.println(
"getInitParameterNames() method return an attribute name twice ");
pw.println(
"The attribute already specified was " + expectedResult2 + " ");
}
}
}
if (!expectedResult1Found && expectedResult2Found) {
passed = false;
pw.println(
"getInitParameterNames() method did not return all the init parameters");
}
} else {
passed = false;
pw.println("getInitParameterNames() returned and empty enumeration");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test for ServletContext.getInitParameter(String) method
*/
public void servletContextGetInitParameter(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
// Init parameters specified in web.xml for
// ServletContextGetInitParameterTestServlet servlet
String param = "EDITOR";
String expectedResult = "VI";
String result = context.getInitParameter(param);
if (result != null) {
if (result.equals("VI")) {
passed = true;
} else {
passed = false;
pw.println("getInitParameter(String) gave incorrect results");
pw.println("Expected result = " + expectedResult + " ");
pw.println("Actual result = |" + result + "| ");
}
} else {
passed = false;
pw.println("getInitParameter(" + param + ") returned a null");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* A Negative Test for ServletContext.getInitParameter(String) method
*/
public void servletContextGetInitParameterNull(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletContext context = getServletContext();
String initParam = context
.getInitParameter("nothing_is_set_here_negative_compatibility_test");
if (initParam == null) {
passed = true;
} else {
passed = false;
pw.println("getInitParameter(String) returned incorrect result");
pw.println("Expected result = null ");
pw.println("Actual result =|" + initParam + "| ");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test for ServletContext.RemoveAttribute(String) method
*/
public void servletContextRemoveAttribute(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
String param = "Mankind";
// first set attribute
context.setAttribute(param, "humane");
// try removing it
context.removeAttribute(param);
Object result = null;
// try getting it,should get null
result = context.getAttribute(param);
if (result == null) {
passed = true;
} else {
passed = false;
pw.println("removeAttribute(" + param + ") returned incorrect result ");
pw.println("Expected result = null ");
pw.println("Actual result = |" + result + "| ");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test for setAttribute(String,Object) method
*/
public void servletContextSetAttribute(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
// set and get
String param1 = "Mankind";
String param2 = "humane";
context.setAttribute(param1, param2);
Object attr = context.getAttribute(param1);
if (attr != null) {
if (attr.getClass().getName().equals("java.lang.String")) {
String sAttr = (String) attr;
if (sAttr.equals(param2)) {
passed = true;
} else {
passed = false;
pw.println("setAttribute(" + param1 + "," + param2
+ ") did not set the attribute properly");
pw.println("Expected result = " + param2 + " ");
pw.println("Actual result = |" + sAttr + "| ");
}
} else {
passed = false;
pw.println("setAttribute(" + param1 + "," + param2
+ ") did not set an attribute of type String properly");
}
}
ServletTestUtil.printResult(pw, passed);
}
public void servletContextSetAttribute_1(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
String param1 = "Mankind";
String param2 = "humane";
String param3 = "being";
// set Attribute param1 twice with param2 and param3 respectively
context.setAttribute(param1, param2);
context.setAttribute(param1, param3);
Object attr = context.getAttribute(param1);
if (attr != null) {
if (attr.getClass().getName().equals("java.lang.String")) {
String sAttr = (String) attr;
if (sAttr.equals(param3)) {
passed = true;
} else if (sAttr.equals(param2)) {
passed = false;
pw.println("Failed to override set value");
} else {
passed = false;
pw.println("Expected result = " + param3 + " ");
pw.println("Actual result = |" + sAttr + "| ");
}
} else {
passed = false;
pw.println("setAttribute(" + param1 + "," + param2 + ") "
+ "didnot set an attribute of type String properly");
}
} else {
passed = false;
pw.println("setAttribute(String, Object) " + "didnot set attribute");
}
ServletTestUtil.printResult(pw, passed);
}
public void servletContextSetAttribute_2(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
String param = "Mankind";
// first set attribute to null
context.setAttribute(param, null);
Object result = null;
// try getting it,should get null
result = context.getAttribute(param);
if (result == null) {
passed = true;
} else {
passed = false;
pw.println("getAttribute(" + param + ") returned incorrect result ");
pw.println("Expected result = null ");
pw.println("Actual result = |" + result + "| ");
}
ServletTestUtil.printResult(pw, passed);
}
public void servletContextGetAttributeNames(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
// first we have to Set Attributes
context.setAttribute("Chef", "expert");
context.setAttribute("chief", "commanding");
// then get Attributes
int count = 0;
String expectedResult1 = "Chef";
boolean expectedResult1Found = false;
String expectedResult2 = "chief";
boolean expectedResult2Found = false;
int expectedCount = 2;
Enumeration attrs = context.getAttributeNames();
if (attrs.hasMoreElements()) {
Vector v = new Vector();
while (attrs.hasMoreElements()) {
String name = (String) attrs.nextElement();
if (name.equals(expectedResult1)) {
if (!expectedResult1Found) {
count++;
expectedResult1Found = true;
} else {
passed = false;
pw.println(
"getAttributeNames() method return an attribute name twice ");
pw.println(" The attribute already specified was "
+ expectedResult1 + " ");
}
} else if (name.equals(expectedResult2)) {
if (!expectedResult2Found) {
count++;
expectedResult2Found = true;
} else {
passed = false;
pw.println(
"getAttributeNames() method return an attribute name twice ");
pw.println(" The attribute already specified was "
+ expectedResult2 + " ");
}
} else {
v.add(name);
}
}
if (count != expectedCount) {
passed = false;
pw.println(
"getAttributeNames() method did not return the correct number of init parameters");
pw.println("Expected count = " + expectedCount);
pw.println("Actual count = " + count);
pw.println("The expected attribute names received were :");
if (expectedResult1Found) {
pw.println(expectedResult1);
}
if (expectedResult2Found) {
pw.println(expectedResult2);
}
pw.println(" Other attribute names received were :");
for (int i = 0; i <= v.size() - 1; i++) {
pw.println("" + v.elementAt(i).toString());
}
} else {
passed = true;
}
} else {
passed = false;
pw.println("getAttributeNames() returned an empty enumeration");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test for ServletContext.getRequestDispatcher(String) method
*/
public void servletContextGetRequestDispatcher(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
/**
* We will try to get the RequestDispatcher for this servlet itself
*/
boolean passed = false;
PrintWriter pw = response.getWriter();
String path = "/WEB-INF/web.xml";
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
RequestDispatcher rd = context.getRequestDispatcher(path);
if (rd != null) {
passed = true;
} else {
passed = false;
pw.println(
"getRequestDispatcher(" + path + ") returned incorrect result ");
pw.println("Expected result != null ");
pw.println("Actual result = |null| ");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test for ServletContext.getNamedDispatcher(String) method
*/
/**
* We will try to get the RequestDispatcher for the servlet Registered as
* config
*/
public void getNamedDispatcher(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
String path = "/GetNamedDispatcherTest";
RequestDispatcher rd = getServletContext().getRequestDispatcher(path);
rd.forward(request, response);
}
/**
* Negative test for ServletContext.getNamedDispatcher(String) method
*/
public void getNamedDispatcher_1(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
String path = "/DoesNotExistGetNamedDispatcherTest";
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
RequestDispatcher tmp = context.getNamedDispatcher(path);
if (tmp != null) {
passed = false;
pw.println("non-null return");
} else {
passed = true;
}
ServletTestUtil.printResult(pw, passed);
}
/**
* Test For getServerInfo mehotd
*/
public void getServerInfo(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
String info = context.getServerInfo();
// it just needs to be a not null value
if (info != null) {
passed = true;
pw.println("ServerInfo = " + info);
} else {
passed = false;
pw.println("getServerInfo() returned a null ");
}
ServletTestUtil.printResult(pw, passed);
}
/**
* A Test for ServletContext.getServletContextName() method
*/
public void getServletNameTest(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
String name = context.getServletContextName();
if (name.equals("SerJaxSerServletContext")) {
passed = true;
pw.println("ame = " + name);
} else {
passed = false;
pw.println("getServletContextName() returned " + name);
}
ServletTestUtil.printResult(pw, passed);
}
public void getServletTempDir(ServletRequest request,
ServletResponse response) throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
ServletConfig config = this.getServletConfig();
ServletContext context = config.getServletContext();
try {
java.io.File tmp = (java.io.File) context
.getAttribute("jakarta.servlet.context.tempdir");
if (tmp != null) {
pw.println("jakarta.servlet.context.tempdir=" + tmp.getAbsolutePath());
if (tmp.exists()) {
passed = true;
} else {
passed = false;
pw.println(
"jakarta.servlet.context.tempdir points to a non-existing dir");
}
} else {
passed = false;
pw.println("jakarta.servlet.temp.dir attribute value is null");
}
} catch (Exception ex) {
passed = false;
pw.println(
"Got Exception when retrieving jakarta.servlet.temp.dir attribute:"
+ ex.toString());
}
ServletTestUtil.printResult(pw, passed);
}
}