-
Notifications
You must be signed in to change notification settings - Fork 78
/
TestServlet.java
144 lines (127 loc) · 4.83 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
/*
* 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.api.jakarta_servlet.requestdispatcher;
import java.io.IOException;
import java.io.PrintWriter;
import com.sun.ts.tests.servlet.common.servlets.GenericTCKServlet;
import com.sun.ts.tests.servlet.common.util.ServletTestUtil;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
public class TestServlet extends GenericTCKServlet {
/**
* Test for forward(ServletRequest,ServletResponse) method
*/
public void forwardTest(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
String path = "/ForwardedServlet";
RequestDispatcher rd = getServletContext().getRequestDispatcher(path);
rd.forward(request, response);
}
public void forward_1Test(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
// response is committed here
pw.println("Committing some content to buffer");
pw.flush();
String path = "/ForwardedServlet";
RequestDispatcher rd = getServletContext().getRequestDispatcher(path);
try {
rd.forward(request, response);
passed = false;
pw.println("IllegalStateException should have been generated");
} 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);
}
/**
* A Test for Include(ServletRequest,ServletResponse) method
*/
public void includeTest(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
String path = "/ForwardedServlet";
RequestDispatcher rd = getServletContext().getRequestDispatcher(path);
rd.include(request, response);
}
/**
* The included servlet is not supposed to change the Response Headers. Our
* included servlet changes it we will check whether that changed header value
* gets reflected in the client side or not
*/
public void include_1Test(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/sgml");
String path = "/IncludedServlet";
RequestDispatcher rd = getServletContext().getRequestDispatcher(path);
rd.include(request, response);
}
public void include_2Test(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
String path = "/ForwardedServlet2";
RequestDispatcher rd = getServletContext().getRequestDispatcher(path);
try {
rd.include(request, response);
passed = false;
pw.println("ServletException should have been generated");
} catch (Throwable t) {
if (t instanceof ServletException) {
passed = true;
} else {
passed = false;
pw.println(
"Exception thrown, but was not an instance of ServletException.");
pw.println("instead received: " + t.getClass().getName());
}
}
ServletTestUtil.printResult(pw, passed);
}
public void include_3Test(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
boolean passed = false;
PrintWriter pw = response.getWriter();
String path = "/ForwardedServlet3";
RequestDispatcher rd = getServletContext().getRequestDispatcher(path);
try {
rd.include(request, response);
passed = false;
pw.println("IOException should have been generated");
} catch (Throwable t) {
if (t instanceof IOException) {
passed = true;
} else {
passed = false;
pw.println("Exception thrown, but was not an instance of IOException.");
pw.println("instead received: " + t.getClass().getName());
}
}
ServletTestUtil.printResult(pw, passed);
}
}