Skip to content

Commit 274c40b

Browse files
committed
Update setCharacterEncoding() tests
Changes due to jakartaee/servlet#377
1 parent f2bda48 commit 274c40b

File tree

1 file changed

+87
-8
lines changed

1 file changed

+87
-8
lines changed

src/com/sun/ts/tests/servlet/api/common/response/ResponseTests.java

+87-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.io.IOException;
2424
import java.io.PrintWriter;
25+
import java.io.UnsupportedEncodingException;
2526
import java.util.Collection;
2627
import java.util.Iterator;
2728
import java.util.Locale;
@@ -225,28 +226,106 @@ public static void getOutputStreamIllegalStateExceptionTest(
225226
}
226227

227228
public static void setCharacterEncodingTest(ServletRequest request,
228-
ServletResponse response) throws ServletException, IOException {
229-
boolean pass = false;
230-
229+
ServletResponse response) throws IOException {
230+
boolean pass = true;
231+
StringBuilder report = new StringBuilder();
232+
233+
// First need to know the default
234+
String defaultEncoding = response.getCharacterEncoding();
235+
236+
report.append("Test 1: Direct UTF-8 then null:\n");
237+
response.setCharacterEncoding("UTF-8");
238+
if ("UTF-8".equalsIgnoreCase(response.getCharacterEncoding())) {
239+
report.append(" Set with UTF-8 Pass\n");
240+
} else {
241+
pass = false;
242+
report.append(" Set with UTF-8 Fail\n");
243+
}
244+
response.setCharacterEncoding(null);
245+
if ((defaultEncoding == null && response.getCharacterEncoding() == null) ||
246+
defaultEncoding != null && defaultEncoding.equalsIgnoreCase(response.getCharacterEncoding())) {
247+
report.append(" Set with null Pass\n");
248+
} else {
249+
pass = false;
250+
report.append(" Set with null Fail\n");
251+
}
252+
response.reset();
253+
254+
report.append("Test 2: Content-Type UTF-8 then null:\n");
255+
response.setContentType("text/plain; charset=UTF-8");
256+
if ("UTF-8".equalsIgnoreCase(response.getCharacterEncoding())) {
257+
report.append(" Set via Content-Type Pass\n");
258+
} else {
259+
pass = false;
260+
report.append(" Set via Content-Type Fail\n");
261+
}
262+
response.setCharacterEncoding(null);
263+
if ((defaultEncoding == null && response.getCharacterEncoding() == null) ||
264+
defaultEncoding != null && defaultEncoding.equalsIgnoreCase(response.getCharacterEncoding())) {
265+
report.append(" Set with null Pass\n");
266+
} else {
267+
pass = false;
268+
report.append(" Set with null Fail\n");
269+
}
270+
response.reset();
271+
272+
report.append("Test 3: Locale Shift_Jis then null:\n");
273+
response.setLocale(new Locale("ja"));
274+
if ("Shift_Jis".equalsIgnoreCase(response.getCharacterEncoding())) {
275+
report.append(" Set via Locale Pass\n");
276+
} else {
277+
pass = false;
278+
report.append(" Set via Locale Fail\n");
279+
}
280+
response.setCharacterEncoding(null);
281+
if ((defaultEncoding == null && response.getCharacterEncoding() == null) ||
282+
defaultEncoding != null && defaultEncoding.equalsIgnoreCase(response.getCharacterEncoding())) {
283+
report.append(" Set with null Pass\n");
284+
} else {
285+
pass = false;
286+
report.append(" Set with null Fail\n");
287+
}
288+
response.reset();
289+
290+
report.append("Test 4: Invalid then getWriter():\n");
291+
response.setCharacterEncoding("does-not-exist");
292+
if ("does-not-exist".equalsIgnoreCase(response.getCharacterEncoding())) {
293+
report.append(" Set with invalid Pass\n");
294+
} else {
295+
pass = false;
296+
report.append(" Set with invalid Fail\n");
297+
}
298+
try {
299+
response.getWriter();
300+
pass = false;
301+
report.append(" getWriter() did not throw UnsupportedEncodingException Fail\n");
302+
} catch (UnsupportedEncodingException uee) {
303+
report.append(" getWriter() throw UnsupportedEncodingException Pass\n");
304+
}
305+
response.reset();
306+
307+
report.append("Test 5: Check getContentType():\n");
231308
final String ENCODING = "ISO-8859-7";
232309
response.setContentType("text/html");
233310
response.setCharacterEncoding(ENCODING);
234311
String type = response.getContentType();
235312

236-
PrintWriter pw = response.getWriter();
237313
if (type != null) {
238314
if ((type.toLowerCase().indexOf("text/html") > -1)
239315
&& (type.toLowerCase().indexOf("charset") > -1)
240316
&& (type.toLowerCase().indexOf("iso-8859-7") > -1)) {
241-
pass = true;
242317
} else {
243-
pw.println("Expecting text/html; charset=ISO-8859-7");
244-
pw.println("getContentType returns incorrect type: " + type);
318+
pass = false;
319+
report.append(" Expecting text/html; charset=ISO-8859-7");
320+
report.append(" getContentType returns incorrect type: " + type);
245321
}
246322
} else {
247-
pw.println("getContentType return null");
323+
pass = false;
324+
report.append(" getContentType return null");
248325
}
249326

327+
PrintWriter pw = response.getWriter();
328+
pw.print(report.toString());
250329
ServletTestUtil.printResult(pw, pass);
251330
}
252331

0 commit comments

Comments
 (0)