|
22 | 22 |
|
23 | 23 | import java.io.IOException;
|
24 | 24 | import java.io.PrintWriter;
|
| 25 | +import java.io.UnsupportedEncodingException; |
25 | 26 | import java.util.Collection;
|
26 | 27 | import java.util.Iterator;
|
27 | 28 | import java.util.Locale;
|
@@ -225,28 +226,106 @@ public static void getOutputStreamIllegalStateExceptionTest(
|
225 | 226 | }
|
226 | 227 |
|
227 | 228 | 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"); |
231 | 308 | final String ENCODING = "ISO-8859-7";
|
232 | 309 | response.setContentType("text/html");
|
233 | 310 | response.setCharacterEncoding(ENCODING);
|
234 | 311 | String type = response.getContentType();
|
235 | 312 |
|
236 |
| - PrintWriter pw = response.getWriter(); |
237 | 313 | if (type != null) {
|
238 | 314 | if ((type.toLowerCase().indexOf("text/html") > -1)
|
239 | 315 | && (type.toLowerCase().indexOf("charset") > -1)
|
240 | 316 | && (type.toLowerCase().indexOf("iso-8859-7") > -1)) {
|
241 |
| - pass = true; |
242 | 317 | } 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); |
245 | 321 | }
|
246 | 322 | } else {
|
247 |
| - pw.println("getContentType return null"); |
| 323 | + pass = false; |
| 324 | + report.append(" getContentType return null"); |
248 | 325 | }
|
249 | 326 |
|
| 327 | + PrintWriter pw = response.getWriter(); |
| 328 | + pw.print(report.toString()); |
250 | 329 | ServletTestUtil.printResult(pw, pass);
|
251 | 330 | }
|
252 | 331 |
|
|
0 commit comments