1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.text;
18
19 import static org.apache.commons.text.StringEscapeUtils.escapeXSI;
20 import static org.apache.commons.text.StringEscapeUtils.unescapeXSI;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.junit.jupiter.api.Assertions.fail;
28
29 import java.io.IOException;
30 import java.io.StringWriter;
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.Modifier;
33 import java.nio.charset.StandardCharsets;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36
37 import org.junit.jupiter.api.Test;
38
39
40
41
42
43
44
45
46 public class StringEscapeUtilsTest {
47 private static final String FOO = "foo";
48
49 private static final String[][] HTML_ESCAPES = {
50 {"no escaping", "plain text", "plain text"},
51 {"no escaping", "plain text", "plain text"},
52 {"empty string", "", ""},
53 {"null", null, null},
54 {"ampersand", "bread & butter", "bread & butter"},
55 {"quotes", ""bread" & butter", "\"bread\" & butter"},
56 {"final character only", "greater than >", "greater than >"},
57 {"first character only", "< less than", "< less than"},
58 {"apostrophe", "Huntington's chorea", "Huntington's chorea"},
59 {"languages", "English,Français,\u65E5\u672C\u8A9E (nihongo)",
60 "English,Fran\u00E7ais,\u65E5\u672C\u8A9E (nihongo)"},
61 {"8-bit ascii shouldn't number-escape", "\u0080\u009F", "\u0080\u009F"},
62 };
63
64 private void assertEscapeJava(final String escaped, final String original) throws IOException {
65 assertEscapeJava(escaped, original, null);
66 }
67
68 private void assertEscapeJava(final String expected, final String original, String message) throws IOException {
69 final String converted = StringEscapeUtils.escapeJava(original);
70 message = "escapeJava(String) failed" + (message == null ? "" : ": " + message);
71 assertEquals(expected, converted, message);
72
73 final StringWriter writer = new StringWriter();
74 StringEscapeUtils.ESCAPE_JAVA.translate(original, writer);
75 assertEquals(expected, writer.toString());
76 }
77
78 private void assertUnescapeJava(final String unescaped, final String original) throws IOException {
79 assertUnescapeJava(unescaped, original, null);
80 }
81
82 private void assertUnescapeJava(final String unescaped, final String original, final String message)
83 throws IOException {
84 final String actual = StringEscapeUtils.unescapeJava(original);
85
86 assertEquals(unescaped, actual, "unescape(String) failed"
87 + (message == null ? "" : ": " + message)
88 + ": expected '" + StringEscapeUtils.escapeJava(unescaped)
89
90 + "' actual '" + StringEscapeUtils.escapeJava(actual) + "'");
91
92 final StringWriter writer = new StringWriter();
93 StringEscapeUtils.UNESCAPE_JAVA.translate(original, writer);
94 assertEquals(unescaped, writer.toString());
95 }
96
97 private void checkCsvEscapeWriter(final String expected, final String value) throws IOException {
98 final StringWriter writer = new StringWriter();
99 StringEscapeUtils.ESCAPE_CSV.translate(value, writer);
100 assertEquals(expected, writer.toString());
101 }
102
103 private void checkCsvUnescapeWriter(final String expected, final String value) throws IOException {
104 final StringWriter writer = new StringWriter();
105 StringEscapeUtils.UNESCAPE_CSV.translate(value, writer);
106 assertEquals(expected, writer.toString());
107 }
108
109 @Test
110 public void testBuilder() {
111 final String result =
112 StringEscapeUtils.builder(StringEscapeUtils.ESCAPE_XML10).escape("<").append(">").toString();
113 assertEquals("<>", result);
114 }
115
116 @Test
117 public void testConstructor() {
118 assertNotNull(new StringEscapeUtils());
119 final Constructor<?>[] cons = StringEscapeUtils.class.getDeclaredConstructors();
120 assertEquals(1, cons.length);
121 assertTrue(Modifier.isPublic(cons[0].getModifiers()));
122 assertTrue(Modifier.isPublic(StringEscapeUtils.class.getModifiers()));
123 assertFalse(Modifier.isFinal(StringEscapeUtils.class.getModifiers()));
124 }
125
126
127 @Test
128 public void testDeleteCharacter() {
129 final String deleteString = "Delete: \u007F";
130 assertEquals("Delete: \\u007F", StringEscapeUtils.escapeJson(deleteString));
131 }
132
133 @Test
134 public void testEscapeCsvString() {
135 assertEquals("foo.bar", StringEscapeUtils.escapeCsv("foo.bar"));
136 assertEquals("\"foo,bar\"", StringEscapeUtils.escapeCsv("foo,bar"));
137 assertEquals("\"foo\nbar\"", StringEscapeUtils.escapeCsv("foo\nbar"));
138 assertEquals("\"foo\rbar\"", StringEscapeUtils.escapeCsv("foo\rbar"));
139 assertEquals("\"foo\"\"bar\"", StringEscapeUtils.escapeCsv("foo\"bar"));
140 assertEquals("foo\uD84C\uDFB4bar", StringEscapeUtils.escapeCsv("foo\uD84C\uDFB4bar"));
141 assertEquals("", StringEscapeUtils.escapeCsv(""));
142 assertNull(StringEscapeUtils.escapeCsv(null));
143 }
144
145 @Test
146 public void testEscapeCsvWriter() throws IOException {
147 checkCsvEscapeWriter("foo.bar", "foo.bar");
148 checkCsvEscapeWriter("\"foo,bar\"", "foo,bar");
149 checkCsvEscapeWriter("\"foo\nbar\"", "foo\nbar");
150 checkCsvEscapeWriter("\"foo\rbar\"", "foo\rbar");
151 checkCsvEscapeWriter("\"foo\"\"bar\"", "foo\"bar");
152 checkCsvEscapeWriter("foo\uD84C\uDFB4bar", "foo\uD84C\uDFB4bar");
153 checkCsvEscapeWriter("", null);
154 checkCsvEscapeWriter("", "");
155 }
156
157 @Test
158 public void testEscapeEcmaScript() {
159 assertNull(StringEscapeUtils.escapeEcmaScript(null));
160 try {
161 StringEscapeUtils.ESCAPE_ECMASCRIPT.translate(null, null);
162 fail("Exception expected!");
163 } catch (final IOException ex) {
164 fail("Exception expected!");
165 } catch (final IllegalArgumentException ex) {
166
167 }
168 try {
169 StringEscapeUtils.ESCAPE_ECMASCRIPT.translate("", null);
170 fail("Exception expected!");
171 } catch (final IOException ex) {
172 fail("Exception expected!");
173 } catch (final IllegalArgumentException ex) {
174
175 }
176
177 assertEquals("He didn\\'t say, \\\"stop!\\\"", StringEscapeUtils.escapeEcmaScript("He didn't say, \"stop!\""));
178 assertEquals("document.getElementById(\\\"test\\\").value = \\'<script>alert(\\'aaa\\');<\\/script>\\';",
179 StringEscapeUtils.escapeEcmaScript(
180 "document.getElementById(\"test\").value = '<script>alert('aaa');</script>';"));
181 }
182
183
184
185
186 @Test
187 public void testEscapeHiragana() {
188
189 final String original = "\u304B\u304C\u3068";
190 final String escaped = StringEscapeUtils.escapeHtml4(original);
191 assertEquals(original, escaped,
192 "Hiragana character Unicode behavior should not be being escaped by escapeHtml4");
193
194 final String unescaped = StringEscapeUtils.unescapeHtml4(escaped);
195
196 assertEquals(escaped, unescaped, "Hiragana character Unicode behavior has changed - expected no unescaping");
197 }
198
199 @Test
200 public void testEscapeHtml3() {
201 for (final String[] element : HTML_ESCAPES) {
202 final String message = element[0];
203 final String expected = element[1];
204 final String original = element[2];
205 assertEquals(expected, StringEscapeUtils.escapeHtml4(original), message);
206 final StringWriter sw = new StringWriter();
207 try {
208 StringEscapeUtils.ESCAPE_HTML3.translate(original, sw);
209 } catch (final IOException e) {
210
211 }
212 final String actual = original == null ? null : sw.toString();
213 assertEquals(expected, actual, message);
214 }
215 }
216
217 @Test
218 public void testEscapeHtml4() {
219 for (final String[] element : HTML_ESCAPES) {
220 final String message = element[0];
221 final String expected = element[1];
222 final String original = element[2];
223 assertEquals(expected, StringEscapeUtils.escapeHtml4(original), message);
224 final StringWriter sw = new StringWriter();
225 try {
226 StringEscapeUtils.ESCAPE_HTML4.translate(original, sw);
227 } catch (final IOException e) {
228
229 }
230 final String actual = original == null ? null : sw.toString();
231 assertEquals(expected, actual, message);
232 }
233 }
234
235
236
237
238 @Test
239 public void testEscapeHtmlHighUnicode() {
240
241
242
243
244 final byte[] data = {(byte) 0xF0, (byte) 0x9D, (byte) 0x8D, (byte) 0xA2};
245
246 final String original = new String(data, StandardCharsets.UTF_8);
247
248 final String escaped = StringEscapeUtils.escapeHtml4(original);
249 assertEquals(original, escaped, "High Unicode should not have been escaped");
250
251 final String unescaped = StringEscapeUtils.unescapeHtml4(escaped);
252 assertEquals(original, unescaped, "High Unicode should have been unchanged");
253
254
255
256
257 }
258
259 @Test
260 public void testEscapeHtmlThree() {
261 assertNull(StringEscapeUtils.escapeHtml3(null));
262 assertEquals("a", StringEscapeUtils.escapeHtml3("a"));
263 assertEquals("<b>a", StringEscapeUtils.escapeHtml3("<b>a"));
264 }
265
266 @Test
267 public void testEscapeHtmlVersions() {
268 assertEquals("Β", StringEscapeUtils.escapeHtml4("\u0392"));
269 assertEquals("\u0392", StringEscapeUtils.unescapeHtml4("Β"));
270
271
272 }
273
274 @Test
275 public void testEscapeJava() throws IOException {
276 assertNull(StringEscapeUtils.escapeJava(null));
277 try {
278 StringEscapeUtils.ESCAPE_JAVA.translate(null, null);
279 fail("Exception expected!");
280 } catch (final IOException ex) {
281 fail("Exception expected!");
282 } catch (final IllegalArgumentException ex) {
283
284 }
285 try {
286 StringEscapeUtils.ESCAPE_JAVA.translate("", null);
287 fail("Exception expected!");
288 } catch (final IOException ex) {
289 fail("Exception expected!");
290 } catch (final IllegalArgumentException ex) {
291
292 }
293
294 assertEscapeJava("", "", "empty string");
295 assertEscapeJava(FOO, FOO);
296 assertEscapeJava("\\t", "\t", "tab");
297 assertEscapeJava("\\\\", "\\", "backslash");
298 assertEscapeJava("'", "'", "single quote should not be escaped");
299 assertEscapeJava("\\\\\\b\\t\\r", "\\\b\t\r");
300 assertEscapeJava("\\u1234", "\u1234");
301 assertEscapeJava("\\u0234", "\u0234");
302 assertEscapeJava("\\u00EF", "\u00ef");
303 assertEscapeJava("\\u0001", "\u0001");
304 assertEscapeJava("\\uABCD", "\uabcd", "Should use capitalized Unicode hex");
305
306 assertEscapeJava("He didn't say, \\\"stop!\\\"",
307 "He didn't say, \"stop!\"");
308 assertEscapeJava("This space is non-breaking:" + "\\u00A0", "This space is non-breaking:\u00a0",
309 "non-breaking space");
310 assertEscapeJava("\\uABCD\\u1234\\u012C",
311 "\uABCD\u1234\u012C");
312 }
313
314
315
316
317 @Test
318 public void testEscapeJavaWithSlash() {
319 final String input = "String with a slash (/) in it";
320
321 final String actual = StringEscapeUtils.escapeJava(input);
322
323
324
325
326
327 assertEquals(input, actual);
328 }
329
330 @Test
331 public void testEscapeJson() {
332 assertNull(StringEscapeUtils.escapeJson(null));
333 try {
334 StringEscapeUtils.ESCAPE_JSON.translate(null, null);
335 fail("Exception expected!");
336 } catch (final IOException ex) {
337 fail("Exception expected!");
338 } catch (final IllegalArgumentException ex) {
339
340 }
341 try {
342 StringEscapeUtils.ESCAPE_JSON.translate("", null);
343 fail("Exception expected!");
344 } catch (final IOException ex) {
345 fail("Exception expected!");
346 } catch (final IllegalArgumentException ex) {
347
348 }
349
350 assertEquals("He didn't say, \\\"stop!\\\"", StringEscapeUtils.escapeJson("He didn't say, \"stop!\""));
351
352 final String expected = "\\\"foo\\\" isn't \\\"bar\\\". specials: \\b\\r\\n\\f\\t\\\\\\/";
353 final String input = "\"foo\" isn't \"bar\". specials: \b\r\n\f\t\\/";
354
355 assertEquals(expected, StringEscapeUtils.escapeJson(input));
356 }
357
358 @Test
359 public void testEscapeXml10() {
360 assertEquals("a<b>c"d'e&f", StringEscapeUtils.escapeXml10("a<b>c\"d'e&f"));
361 assertEquals("a\tb\rc\nd", StringEscapeUtils.escapeXml10("a\tb\rc\nd"),
362 "XML 1.0 should not escape \t \n \r");
363 assertEquals("ab", StringEscapeUtils.escapeXml10("a\u0000\u0001\u0008\u000b\u000c\u000e\u001fb"),
364 "XML 1.0 should omit most #x0-x8 | #xb | #xc | #xe-#x19");
365 assertEquals("a\ud7ff \ue000b", StringEscapeUtils.escapeXml10("a\ud7ff\ud800 \udfff \ue000b"),
366 "XML 1.0 should omit #xd800-#xdfff");
367 assertEquals("a\ufffdb", StringEscapeUtils.escapeXml10("a\ufffd\ufffe\uffffb"),
368 "XML 1.0 should omit #xfffe | #xffff");
369 assertEquals("a\u007e„\u0085†Ÿ\u00a0b",
370 StringEscapeUtils.escapeXml10("a\u007e\u007f\u0084\u0085\u0086\u009f\u00a0b"),
371 "XML 1.0 should escape #x7f-#x84 | #x86 - #x9f, for XML 1.1 compatibility");
372 }
373
374 @Test
375 public void testEscapeXml11() {
376 assertEquals("a<b>c"d'e&f", StringEscapeUtils.escapeXml11("a<b>c\"d'e&f"));
377 assertEquals("a\tb\rc\nd", StringEscapeUtils.escapeXml11("a\tb\rc\nd"),
378 "XML 1.1 should not escape \t \n \r");
379 assertEquals("ab", StringEscapeUtils.escapeXml11("a\u0000b"),
380 "XML 1.1 should omit #x0");
381 assertEquals("ab",
382 StringEscapeUtils.escapeXml11("a\u0001\u0008\u000b\u000c\u000e\u001fb"),
383 "XML 1.1 should escape #x1-x8 | #xb | #xc | #xe-#x19");
384 assertEquals("a\u007e„\u0085†Ÿ\u00a0b",
385 StringEscapeUtils.escapeXml11("a\u007e\u007f\u0084\u0085\u0086\u009f\u00a0b"),
386 "XML 1.1 should escape #x7F-#x84 | #x86-#x9F");
387 assertEquals("a\ud7ff \ue000b", StringEscapeUtils.escapeXml11("a\ud7ff\ud800 \udfff \ue000b"),
388 "XML 1.1 should omit #xd800-#xdfff");
389 assertEquals("a\ufffdb", StringEscapeUtils.escapeXml11("a\ufffd\ufffe\uffffb"),
390 "XML 1.1 should omit #xfffe | #xffff");
391 }
392
393 @Test
394 public void testEscapeXSI() {
395 assertNull(null, escapeXSI(null));
396 assertEquals("He\\ didn\\'t\\ say,\\ \\\"Stop!\\\"", escapeXSI("He didn't say, \"Stop!\""));
397 assertEquals("\\\\", escapeXSI("\\"));
398 assertEquals("", escapeXSI("\n"));
399 }
400
401 @Test
402 public void testLang313() {
403 assertEquals("& &", StringEscapeUtils.unescapeHtml4("& &"));
404 }
405
406
407
408
409
410
411
412 @Test
413 public void testLang708() throws IOException {
414 final byte[] inputBytes = Files.readAllBytes(Paths.get("src/test/resources/org/apache/commons/text/stringEscapeUtilsTestData.txt"));
415 final String input = new String(inputBytes, StandardCharsets.UTF_8);
416 final String escaped = StringEscapeUtils.escapeEcmaScript(input);
417
418 assertTrue(escaped.endsWith("}]"), escaped);
419
420 assertTrue(escaped.endsWith("\"valueCode\\\":\\\"\\\"}]"), escaped);
421 }
422
423
424
425
426 @Test
427 public void testLang911() {
428 final String bellsTest = "\ud83d\udc80\ud83d\udd14";
429 final String value = StringEscapeUtils.escapeJava(bellsTest);
430 final String valueTest = StringEscapeUtils.unescapeJava(value);
431 assertEquals(bellsTest, valueTest);
432 }
433
434
435
436 @Test
437 public void testStandaloneAmphersand() {
438 assertEquals("<P&O>", StringEscapeUtils.unescapeHtml4("<P&O>"));
439 assertEquals("test & <", StringEscapeUtils.unescapeHtml4("test & <"));
440 assertEquals("<P&O>", StringEscapeUtils.unescapeXml("<P&O>"));
441 assertEquals("test & <", StringEscapeUtils.unescapeXml("test & <"));
442 }
443
444 @Test
445 public void testUnescapeCsvString() {
446 assertEquals("foo.bar", StringEscapeUtils.unescapeCsv("foo.bar"));
447 assertEquals("foo,bar", StringEscapeUtils.unescapeCsv("\"foo,bar\""));
448 assertEquals("foo\nbar", StringEscapeUtils.unescapeCsv("\"foo\nbar\""));
449 assertEquals("foo\rbar", StringEscapeUtils.unescapeCsv("\"foo\rbar\""));
450 assertEquals("foo\"bar", StringEscapeUtils.unescapeCsv("\"foo\"\"bar\""));
451 assertEquals("foo\uD84C\uDFB4bar", StringEscapeUtils.unescapeCsv("foo\uD84C\uDFB4bar"));
452 assertEquals("", StringEscapeUtils.unescapeCsv(""));
453 assertNull(StringEscapeUtils.unescapeCsv(null));
454
455 assertEquals("foo.bar", StringEscapeUtils.unescapeCsv("\"foo.bar\""));
456 }
457
458 @Test
459 public void testUnescapeCsvWriter() throws IOException {
460 checkCsvUnescapeWriter("foo.bar", "foo.bar");
461 checkCsvUnescapeWriter("foo,bar", "\"foo,bar\"");
462 checkCsvUnescapeWriter("foo\nbar", "\"foo\nbar\"");
463 checkCsvUnescapeWriter("foo\rbar", "\"foo\rbar\"");
464 checkCsvUnescapeWriter("foo\"bar", "\"foo\"\"bar\"");
465 checkCsvUnescapeWriter("foo\uD84C\uDFB4bar", "foo\uD84C\uDFB4bar");
466 checkCsvUnescapeWriter("", null);
467 checkCsvUnescapeWriter("", "");
468
469 checkCsvUnescapeWriter("foo.bar", "\"foo.bar\"");
470 }
471
472 @Test
473 public void testUnescapeEcmaScript() {
474 assertNull(StringEscapeUtils.unescapeEcmaScript(null));
475 assertEquals("8lvc1u+6B#-I", StringEscapeUtils.unescapeEcmaScript("8lvc1u+6B#-I"));
476 assertEquals("<script src=\"build/main.bundle.js\"></script>",
477 StringEscapeUtils.unescapeEcmaScript("<script src=\"build/main.bundle.js\"></script>"));
478 assertEquals("<script src=\"build/main.bundle.js\"></script>>",
479 StringEscapeUtils.unescapeEcmaScript("<script src=\"build/main.bundle.js\"></script>>"));
480 }
481
482 @Test
483 public void testUnescapeHexCharsHtml() {
484
485 assertEquals("\u0080\u009F", StringEscapeUtils.unescapeHtml4("€Ÿ"), "hex number unescape");
486 assertEquals("\u0080\u009F", StringEscapeUtils.unescapeHtml4("€Ÿ"), "hex number unescape");
487
488 for (char i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
489 final char c2 = (char) (i + 1);
490 final String expected = Character.toString(i) + Character.toString(c2);
491 final String escapedC1 = "&#x" + Integer.toHexString(i) + ";";
492 final String escapedC2 = "&#x" + Integer.toHexString(c2) + ";";
493 assertEquals(expected, StringEscapeUtils.unescapeHtml4(escapedC1 + escapedC2),
494 "hex number unescape index " + i);
495 }
496 }
497
498 @Test
499 public void testUnescapeHtml3() {
500 for (final String[] element : HTML_ESCAPES) {
501 final String message = element[0];
502 final String expected = element[2];
503 final String original = element[1];
504 assertEquals(expected, StringEscapeUtils.unescapeHtml3(original), message);
505
506 final StringWriter sw = new StringWriter();
507 try {
508 StringEscapeUtils.UNESCAPE_HTML3.translate(original, sw);
509 } catch (final IOException e) {
510
511 }
512 final String actual = original == null ? null : sw.toString();
513 assertEquals(expected, actual, message);
514 }
515
516
517
518 assertEquals("Fran\u00E7ais", StringEscapeUtils.unescapeHtml3("Fran\u00E7ais"), "funny chars pass through OK");
519
520 assertEquals("Hello&;World", StringEscapeUtils.unescapeHtml3("Hello&;World"));
521 assertEquals("Hello&#;World", StringEscapeUtils.unescapeHtml3("Hello&#;World"));
522 assertEquals("Hello&# ;World", StringEscapeUtils.unescapeHtml3("Hello&# ;World"));
523 assertEquals("Hello&##;World", StringEscapeUtils.unescapeHtml3("Hello&##;World"));
524 }
525
526 @Test
527 public void testUnescapeHtml4() {
528 for (final String[] element : HTML_ESCAPES) {
529 final String message = element[0];
530 final String expected = element[2];
531 final String original = element[1];
532 assertEquals(expected, StringEscapeUtils.unescapeHtml4(original), message);
533
534 final StringWriter sw = new StringWriter();
535 try {
536 StringEscapeUtils.UNESCAPE_HTML4.translate(original, sw);
537 } catch (final IOException e) {
538
539 }
540 final String actual = original == null ? null : sw.toString();
541 assertEquals(expected, actual, message);
542 }
543
544
545
546 assertEquals("Fran\u00E7ais", StringEscapeUtils.unescapeHtml4("Fran\u00E7ais"), "funny chars pass through OK");
547
548 assertEquals("Hello&;World", StringEscapeUtils.unescapeHtml4("Hello&;World"));
549 assertEquals("Hello&#;World", StringEscapeUtils.unescapeHtml4("Hello&#;World"));
550 assertEquals("Hello&# ;World", StringEscapeUtils.unescapeHtml4("Hello&# ;World"));
551 assertEquals("Hello&##;World", StringEscapeUtils.unescapeHtml4("Hello&##;World"));
552 }
553
554 @Test
555 public void testUnescapeJava() throws IOException {
556 assertNull(StringEscapeUtils.unescapeJava(null));
557 try {
558 StringEscapeUtils.UNESCAPE_JAVA.translate(null, null);
559 fail("Exception expected!");
560 } catch (final IOException ex) {
561 fail("Exception expected!");
562 } catch (final IllegalArgumentException ex) {
563
564 }
565 try {
566 StringEscapeUtils.UNESCAPE_JAVA.translate("", null);
567 fail("Exception expected!");
568 } catch (final IOException ex) {
569 fail("Exception expected!");
570 } catch (final IllegalArgumentException ex) {
571
572 }
573 assertThrows(RuntimeException.class, () -> StringEscapeUtils.unescapeJava("\\u02-3"));
574
575 assertUnescapeJava("", "");
576 assertUnescapeJava("test", "test");
577 assertUnescapeJava("\ntest\b", "\\ntest\\b");
578 assertUnescapeJava("\u123425foo\ntest\b", "\\u123425foo\\ntest\\b");
579 assertUnescapeJava("'\foo\teste\r", "\\'\\foo\\teste\\r");
580 assertUnescapeJava("", "\\");
581
582 assertUnescapeJava("\uABCDx", "\\uabcdx", "lowercase Unicode");
583 assertUnescapeJava("\uABCDx", "\\uABCDx", "uppercase Unicode");
584 assertUnescapeJava("\uABCD", "\\uabcd", "Unicode as final character");
585 }
586
587 @Test
588 public void testUnescapeJson() {
589 final String jsonString =
590 "{\"age\":100,\"name\":\"kyong.com\n\",\"messages\":[\"msg 1\",\"msg 2\",\"msg 3\"]}";
591
592 assertEquals("", StringEscapeUtils.unescapeJson(""));
593 assertEquals(" ", StringEscapeUtils.unescapeJson(" "));
594 assertEquals("a:b", StringEscapeUtils.unescapeJson("a:b"));
595 assertEquals(jsonString, StringEscapeUtils.unescapeJson(jsonString));
596 }
597
598 @Test
599 public void testUnescapeJsonDoubleQuoteAndForwardSlash() {
600 final String escapedJsonString = "double quote: \\\" and a forward slash: \\/";
601 final String jsonString = "double quote: \" and a forward slash: /";
602
603 assertEquals(jsonString, StringEscapeUtils.unescapeJson(escapedJsonString));
604 }
605
606 @Test
607 public void testUnescapeUnknownEntity() {
608 assertEquals("&zzzz;", StringEscapeUtils.unescapeHtml4("&zzzz;"));
609 }
610
611
612
613
614
615
616 @Test
617 public void testUnescapeXmlSupplementaryCharacters() {
618 assertEquals("\uD84C\uDFB4", StringEscapeUtils.unescapeXml("𣎴"),
619 "Supplementary character must be represented using a single escape");
620
621 assertEquals("a b c \uD84C\uDFB4", StringEscapeUtils.unescapeXml("a b c 𣎴"),
622 "Supplementary characters mixed with basic characters should be decoded correctly");
623 }
624
625 @Test
626 public void testUnscapeXSI() {
627 assertNull(null, unescapeXSI(null));
628 assertEquals("\"", unescapeXSI("\\\""));
629 assertEquals("He didn't say, \"Stop!\"", unescapeXSI("He\\ didn\\'t\\ say,\\ \\\"Stop!\\\""));
630 assertEquals("\\", unescapeXSI("\\\\"));
631 assertEquals("", unescapeXSI("\\"));
632 }
633 }