View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.io.IOException;
27  import java.io.StringWriter;
28  import java.lang.reflect.Constructor;
29  import java.lang.reflect.Modifier;
30  import java.nio.charset.StandardCharsets;
31  import java.nio.file.Files;
32  import java.nio.file.Paths;
33  
34  import org.apache.commons.lang3.text.translate.CharSequenceTranslator;
35  import org.apache.commons.lang3.text.translate.NumericEntityEscaper;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Unit tests for {@link StringEscapeUtils}.
40   */
41  @Deprecated
42  public class StringEscapeUtilsTest {
43      private static final String FOO = "foo";
44  
45      @Test
46      public void testConstructor() {
47          assertNotNull(new StringEscapeUtils());
48          final Constructor<?>[] cons = StringEscapeUtils.class.getDeclaredConstructors();
49          assertEquals(1, cons.length);
50          assertTrue(Modifier.isPublic(cons[0].getModifiers()));
51          assertTrue(Modifier.isPublic(StringEscapeUtils.class.getModifiers()));
52          assertFalse(Modifier.isFinal(StringEscapeUtils.class.getModifiers()));
53      }
54  
55      @Test
56      public void testEscapeJava() throws IOException {
57          assertNull(StringEscapeUtils.escapeJava(null));
58          assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_JAVA.translate(null, null));
59          assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_JAVA.translate("", null));
60  
61          assertEscapeJava("empty string", "", "");
62          assertEscapeJava(FOO, FOO);
63          assertEscapeJava("tab", "\\t", "\t");
64          assertEscapeJava("backslash", "\\\\", "\\");
65          assertEscapeJava("single quote should not be escaped", "'", "'");
66          assertEscapeJava("\\\\\\b\\t\\r", "\\\b\t\r");
67          assertEscapeJava("\\u1234", "\u1234");
68          assertEscapeJava("\\u0234", "\u0234");
69          assertEscapeJava("\\u00EF", "\u00ef");
70          assertEscapeJava("\\u0001", "\u0001");
71          assertEscapeJava("Should use capitalized Unicode hex", "\\uABCD", "\uabcd");
72  
73          assertEscapeJava("He didn't say, \\\"stop!\\\"",
74                  "He didn't say, \"stop!\"");
75          assertEscapeJava("non-breaking space", "This space is non-breaking:" + "\\u00A0",
76                  "This space is non-breaking:\u00a0");
77          assertEscapeJava("\\uABCD\\u1234\\u012C",
78                  "\uABCD\u1234\u012C");
79      }
80  
81      /**
82       * Tests https://issues.apache.org/jira/browse/LANG-421
83       */
84      @Test
85      public void testEscapeJavaWithSlash() {
86          final String input = "String with a slash (/) in it";
87  
88          final String expected = input;
89          final String actual = StringEscapeUtils.escapeJava(input);
90  
91          /*
92           * In 2.4 StringEscapeUtils.escapeJava(String) escapes '/' characters, which are not a valid character to escape
93           * in a Java string.
94           */
95          assertEquals(expected, actual);
96      }
97  
98      private void assertEscapeJava(final String escaped, final String original) throws IOException {
99          assertEscapeJava(null, escaped, original);
100     }
101 
102     private void assertEscapeJava(String message, final String expected, final String original) throws IOException {
103         final String converted = StringEscapeUtils.escapeJava(original);
104         message = "escapeJava(String) failed" + (message == null ? "" : (": " + message));
105         assertEquals(expected, converted, message);
106 
107         final StringWriter writer = new StringWriter();
108         StringEscapeUtils.ESCAPE_JAVA.translate(original, writer);
109         assertEquals(expected, writer.toString());
110     }
111 
112     @Test
113     public void testUnescapeJava() throws IOException {
114         assertNull(StringEscapeUtils.unescapeJava(null));
115         assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_JAVA.translate(null, null));
116         assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_JAVA.translate("", null));
117         assertThrows(RuntimeException.class, () -> StringEscapeUtils.unescapeJava("\\u02-3"));
118 
119         assertUnescapeJava("", "");
120         assertUnescapeJava("test", "test");
121         assertUnescapeJava("\ntest\b", "\\ntest\\b");
122         assertUnescapeJava("\u123425foo\ntest\b", "\\u123425foo\\ntest\\b");
123         assertUnescapeJava("'\foo\teste\r", "\\'\\foo\\teste\\r");
124         assertUnescapeJava("", "\\");
125         //foo
126         assertUnescapeJava("lowercase Unicode", "\uABCDx", "\\uabcdx");
127         assertUnescapeJava("uppercase Unicode", "\uABCDx", "\\uABCDx");
128         assertUnescapeJava("Unicode as final character", "\uABCD", "\\uabcd");
129     }
130 
131     private void assertUnescapeJava(final String unescaped, final String original) throws IOException {
132         assertUnescapeJava(null, unescaped, original);
133     }
134 
135     private void assertUnescapeJava(final String message, final String unescaped, final String original) throws IOException {
136         final String expected = unescaped;
137         final String actual = StringEscapeUtils.unescapeJava(original);
138 
139         assertEquals(expected, actual,
140                 "unescape(String) failed" +
141                 (message == null ? "" : (": " + message)) +
142                 ": expected '" + StringEscapeUtils.escapeJava(expected) +
143                 // we escape this so we can see it in the error message
144                 "' actual '" + StringEscapeUtils.escapeJava(actual) + "'");
145 
146         final StringWriter writer = new StringWriter();
147         StringEscapeUtils.UNESCAPE_JAVA.translate(original, writer);
148         assertEquals(unescaped, writer.toString());
149 
150     }
151 
152     @Test
153     public void testEscapeEcmaScript() {
154         assertNull(StringEscapeUtils.escapeEcmaScript(null));
155         assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_ECMASCRIPT.translate(null, null));
156         assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_ECMASCRIPT.translate("", null));
157 
158         assertEquals("He didn\\'t say, \\\"stop!\\\"", StringEscapeUtils.escapeEcmaScript("He didn't say, \"stop!\""));
159         assertEquals("document.getElementById(\\\"test\\\").value = \\'<script>alert(\\'aaa\\');<\\/script>\\';",
160                 StringEscapeUtils.escapeEcmaScript("document.getElementById(\"test\").value = '<script>alert('aaa');</script>';"));
161     }
162 
163     @Test
164     public void testUnescapeEcmaScript() {
165         assertNull(StringEscapeUtils.escapeEcmaScript(null));
166         assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_ECMASCRIPT.translate(null, null));
167         assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_ECMASCRIPT.translate("", null));
168 
169         assertEquals("He didn't say, \"stop!\"", StringEscapeUtils.unescapeEcmaScript("He didn\\'t say, \\\"stop!\\\""));
170         assertEquals("document.getElementById(\"test\").value = '<script>alert('aaa');</script>';",
171                 StringEscapeUtils.unescapeEcmaScript("document.getElementById(\\\"test\\\").value = \\'<script>alert(\\'aaa\\');<\\/script>\\';"));
172     }
173 
174 
175     // HTML and XML
176     //--------------------------------------------------------------
177 
178     private static final String[][] HTML_ESCAPES = {
179         {"no escaping", "plain text", "plain text"},
180         {"no escaping", "plain text", "plain text"},
181         {"empty string", "", ""},
182         {"null", null, null},
183         {"ampersand", "bread &amp; butter", "bread & butter"},
184         {"quotes", "&quot;bread&quot; &amp; butter", "\"bread\" & butter"},
185         {"final character only", "greater than &gt;", "greater than >"},
186         {"first character only", "&lt; less than", "< less than"},
187         {"apostrophe", "Huntington's chorea", "Huntington's chorea"},
188         {"languages", "English,Fran&ccedil;ais,\u65E5\u672C\u8A9E (nihongo)", "English,Fran\u00E7ais,\u65E5\u672C\u8A9E (nihongo)"},
189         {"8-bit ascii shouldn't number-escape", "\u0080\u009F", "\u0080\u009F"},
190     };
191 
192     @Test
193     public void testEscapeHtml() throws IOException {
194         for (final String[] element : HTML_ESCAPES) {
195             final String message = element[0];
196             final String expected = element[1];
197             final String original = element[2];
198             assertEquals(expected, StringEscapeUtils.escapeHtml4(original), message);
199             final StringWriter sw = new StringWriter();
200             StringEscapeUtils.ESCAPE_HTML4.translate(original, sw);
201             final String actual = original == null ? null : sw.toString();
202             assertEquals(expected, actual, message);
203         }
204     }
205 
206     @Test
207     public void testUnescapeHtml4() throws IOException {
208         for (final String[] element : HTML_ESCAPES) {
209             final String message = element[0];
210             final String expected = element[2];
211             final String original = element[1];
212             assertEquals(expected, StringEscapeUtils.unescapeHtml4(original), message);
213 
214             final StringWriter sw = new StringWriter();
215             StringEscapeUtils.UNESCAPE_HTML4.translate(original, sw);
216             final String actual = original == null ? null : sw.toString();
217             assertEquals(expected, actual, message);
218         }
219         // \u00E7 is a cedilla (c with wiggle under)
220         // note that the test string must be 7-bit-clean (Unicode escaped) or else it will compile incorrectly
221         // on some locales
222         assertEquals("Fran\u00E7ais", StringEscapeUtils.unescapeHtml4("Fran\u00E7ais"), "funny chars pass through OK");
223 
224         assertEquals("Hello&;World", StringEscapeUtils.unescapeHtml4("Hello&;World"));
225         assertEquals("Hello&#;World", StringEscapeUtils.unescapeHtml4("Hello&#;World"));
226         assertEquals("Hello&# ;World", StringEscapeUtils.unescapeHtml4("Hello&# ;World"));
227         assertEquals("Hello&##;World", StringEscapeUtils.unescapeHtml4("Hello&##;World"));
228     }
229 
230     @Test
231     public void testUnescapeHexCharsHtml() {
232         // Simple easy to grok test
233         assertEquals("\u0080\u009F", StringEscapeUtils.unescapeHtml4("&#x80;&#x9F;"), "hex number unescape");
234         assertEquals("\u0080\u009F", StringEscapeUtils.unescapeHtml4("&#X80;&#X9F;"), "hex number unescape");
235         // Test all Character values:
236         for (char i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
237             final Character c1 = Character.valueOf(i);
238             final Character c2 = Character.valueOf((char) (i+1));
239             final String expected = c1.toString() + c2.toString();
240             final String escapedC1 = "&#x" + Integer.toHexString((c1.charValue())) + ";";
241             final String escapedC2 = "&#x" + Integer.toHexString((c2.charValue())) + ";";
242             assertEquals(expected, StringEscapeUtils.unescapeHtml4(escapedC1 + escapedC2), "hex number unescape index " + (int) i);
243         }
244     }
245 
246     @Test
247     public void testUnescapeUnknownEntity() {
248         assertEquals("&zzzz;", StringEscapeUtils.unescapeHtml4("&zzzz;"));
249     }
250 
251     @Test
252     public void testEscapeHtmlVersions() {
253         assertEquals("&Beta;", StringEscapeUtils.escapeHtml4("\u0392"));
254         assertEquals("\u0392", StringEscapeUtils.unescapeHtml4("&Beta;"));
255 
256         // TODO: refine API for escaping/unescaping specific HTML versions
257     }
258 
259     @Test
260     public void testEscapeXml() throws Exception {
261         assertEquals("&lt;abc&gt;", StringEscapeUtils.escapeXml("<abc>"));
262         assertEquals("<abc>", StringEscapeUtils.unescapeXml("&lt;abc&gt;"));
263 
264         assertEquals("\u00A1", StringEscapeUtils.escapeXml("\u00A1"), "XML should not escape >0x7f values");
265         assertEquals("\u00A0", StringEscapeUtils.unescapeXml("&#160;"), "XML should be able to unescape >0x7f values");
266         assertEquals("\u00A0", StringEscapeUtils.unescapeXml("&#0160;"),
267                 "XML should be able to unescape >0x7f values with one leading 0");
268         assertEquals("\u00A0", StringEscapeUtils.unescapeXml("&#00160;"),
269                 "XML should be able to unescape >0x7f values with two leading 0s");
270         assertEquals("\u00A0", StringEscapeUtils.unescapeXml("&#000160;"),
271                 "XML should be able to unescape >0x7f values with three leading 0s");
272 
273         assertEquals("ain't", StringEscapeUtils.unescapeXml("ain&apos;t"));
274         assertEquals("ain&apos;t", StringEscapeUtils.escapeXml("ain't"));
275         assertEquals("", StringEscapeUtils.escapeXml(""));
276         assertNull(StringEscapeUtils.escapeXml(null));
277         assertNull(StringEscapeUtils.unescapeXml(null));
278 
279         StringWriter sw = new StringWriter();
280         StringEscapeUtils.ESCAPE_XML.translate("<abc>", sw);
281         assertEquals("&lt;abc&gt;", sw.toString(), "XML was escaped incorrectly");
282 
283         sw = new StringWriter();
284         StringEscapeUtils.UNESCAPE_XML.translate("&lt;abc&gt;", sw);
285         assertEquals("<abc>", sw.toString(), "XML was unescaped incorrectly");
286     }
287 
288     @Test
289     public void testEscapeXml10() {
290         assertEquals("a&lt;b&gt;c&quot;d&apos;e&amp;f", StringEscapeUtils.escapeXml10("a<b>c\"d'e&f"));
291         assertEquals("a\tb\rc\nd", StringEscapeUtils.escapeXml10("a\tb\rc\nd"), "XML 1.0 should not escape \t \n \r");
292         assertEquals("ab", StringEscapeUtils.escapeXml10("a\u0000\u0001\u0008\u000b\u000c\u000e\u001fb"),
293                 "XML 1.0 should omit most #x0-x8 | #xb | #xc | #xe-#x19");
294         assertEquals("a\ud7ff  \ue000b", StringEscapeUtils.escapeXml10("a\ud7ff\ud800 \udfff \ue000b"),
295                 "XML 1.0 should omit #xd800-#xdfff");
296         assertEquals("a\ufffdb", StringEscapeUtils.escapeXml10("a\ufffd\ufffe\uffffb"),
297                 "XML 1.0 should omit #xfffe | #xffff");
298         assertEquals("a\u007e&#127;&#132;\u0085&#134;&#159;\u00a0b",
299                 StringEscapeUtils.escapeXml10("a\u007e\u007f\u0084\u0085\u0086\u009f\u00a0b"),
300                 "XML 1.0 should escape #x7f-#x84 | #x86 - #x9f, for XML 1.1 compatibility");
301     }
302 
303     @Test
304     public void testEscapeXml11() {
305         assertEquals("a&lt;b&gt;c&quot;d&apos;e&amp;f", StringEscapeUtils.escapeXml11("a<b>c\"d'e&f"));
306         assertEquals("a\tb\rc\nd", StringEscapeUtils.escapeXml11("a\tb\rc\nd"), "XML 1.1 should not escape \t \n \r");
307         assertEquals("ab", StringEscapeUtils.escapeXml11("a\u0000b"), "XML 1.1 should omit #x0");
308         assertEquals("a&#1;&#8;&#11;&#12;&#14;&#31;b",
309                 StringEscapeUtils.escapeXml11("a\u0001\u0008\u000b\u000c\u000e\u001fb"),
310                 "XML 1.1 should escape #x1-x8 | #xb | #xc | #xe-#x19");
311         assertEquals("a\u007e&#127;&#132;\u0085&#134;&#159;\u00a0b",
312                 StringEscapeUtils.escapeXml11("a\u007e\u007f\u0084\u0085\u0086\u009f\u00a0b"),
313                 "XML 1.1 should escape #x7F-#x84 | #x86-#x9F");
314         assertEquals("a\ud7ff  \ue000b", StringEscapeUtils.escapeXml11("a\ud7ff\ud800 \udfff \ue000b"),
315                 "XML 1.1 should omit #xd800-#xdfff");
316         assertEquals("a\ufffdb", StringEscapeUtils.escapeXml11("a\ufffd\ufffe\uffffb"),
317                 "XML 1.1 should omit #xfffe | #xffff");
318     }
319 
320     /**
321      * Tests Supplementary characters.
322      * <p>
323      * From http://www.w3.org/International/questions/qa-escapes
324      * </p>
325      * <blockquote>
326      * Supplementary characters are those Unicode characters that have code points higher than the characters in
327      * the Basic Multilingual Plane (BMP). In UTF-16 a supplementary character is encoded using two 16-bit surrogate code points from the
328      * BMP. Because of this, some people think that supplementary characters need to be represented using two escapes, but this is incorrect
329      * - you must use the single, code point value for that character. For example, use &amp;&#35;x233B4&#59; rather than
330      * &amp;&#35;xD84C&#59;&amp;&#35;xDFB4&#59;.
331      * </blockquote>
332      * @see <a href="http://www.w3.org/International/questions/qa-escapes">Using character escapes in markup and CSS</a>
333      * @see <a href="https://issues.apache.org/jira/browse/LANG-728">LANG-728</a>
334      */
335     @Test
336     public void testEscapeXmlSupplementaryCharacters() {
337         final CharSequenceTranslator escapeXml =
338             StringEscapeUtils.ESCAPE_XML.with( NumericEntityEscaper.between(0x7f, Integer.MAX_VALUE) );
339 
340         assertEquals("&#144308;", escapeXml.translate("\uD84C\uDFB4"),
341                 "Supplementary character must be represented using a single escape");
342 
343         assertEquals("a b c &#144308;", escapeXml.translate("a b c \uD84C\uDFB4"),
344                 "Supplementary characters mixed with basic characters should be encoded correctly");
345     }
346 
347     @Test
348     public void testEscapeXmlAllCharacters() {
349         // http://www.w3.org/TR/xml/#charsets says:
350         // Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character,
351         // excluding the surrogate blocks, FFFE, and FFFF. */
352         final CharSequenceTranslator escapeXml = StringEscapeUtils.ESCAPE_XML
353                 .with(NumericEntityEscaper.below(9), NumericEntityEscaper.between(0xB, 0xC), NumericEntityEscaper.between(0xE, 0x19),
354                         NumericEntityEscaper.between(0xD800, 0xDFFF), NumericEntityEscaper.between(0xFFFE, 0xFFFF), NumericEntityEscaper.above(0x110000));
355 
356         assertEquals("&#0;&#1;&#2;&#3;&#4;&#5;&#6;&#7;&#8;", escapeXml.translate("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008"));
357         assertEquals("\t", escapeXml.translate("\t")); // 0x9
358         assertEquals("\n", escapeXml.translate("\n")); // 0xA
359         assertEquals("&#11;&#12;", escapeXml.translate("\u000B\u000C"));
360         assertEquals("\r", escapeXml.translate("\r")); // 0xD
361         assertEquals("Hello World! Ain&apos;t this great?", escapeXml.translate("Hello World! Ain't this great?"));
362         assertEquals("&#14;&#15;&#24;&#25;", escapeXml.translate("\u000E\u000F\u0018\u0019"));
363     }
364 
365     /**
366      * Reverse of the above.
367      *
368      * @see <a href="https://issues.apache.org/jira/browse/LANG-729">LANG-729</a>
369      */
370     @Test
371     public void testUnescapeXmlSupplementaryCharacters() {
372         assertEquals("\uD84C\uDFB4", StringEscapeUtils.unescapeXml("&#144308;"),
373                 "Supplementary character must be represented using a single escape");
374 
375         assertEquals("a b c \uD84C\uDFB4", StringEscapeUtils.unescapeXml("a b c &#144308;"),
376                 "Supplementary characters mixed with basic characters should be decoded correctly");
377     }
378 
379     // Tests issue #38569
380     // https://issues.apache.org/bugzilla/show_bug.cgi?id=38569
381     @Test
382     public void testStandaloneAmphersand() {
383         assertEquals("<P&O>", StringEscapeUtils.unescapeHtml4("&lt;P&O&gt;"));
384         assertEquals("test & <", StringEscapeUtils.unescapeHtml4("test & &lt;"));
385         assertEquals("<P&O>", StringEscapeUtils.unescapeXml("&lt;P&O&gt;"));
386         assertEquals("test & <", StringEscapeUtils.unescapeXml("test & &lt;"));
387     }
388 
389     @Test
390     public void testLang313() {
391         assertEquals("& &", StringEscapeUtils.unescapeHtml4("& &amp;"));
392     }
393 
394     @Test
395     public void testEscapeCsvString() {
396         assertEquals("foo.bar",            StringEscapeUtils.escapeCsv("foo.bar"));
397         assertEquals("\"foo,bar\"",        StringEscapeUtils.escapeCsv("foo,bar"));
398         assertEquals("\"foo\nbar\"",       StringEscapeUtils.escapeCsv("foo\nbar"));
399         assertEquals("\"foo\rbar\"",       StringEscapeUtils.escapeCsv("foo\rbar"));
400         assertEquals("\"foo\"\"bar\"",     StringEscapeUtils.escapeCsv("foo\"bar"));
401         assertEquals("foo\uD84C\uDFB4bar", StringEscapeUtils.escapeCsv("foo\uD84C\uDFB4bar"));
402         assertEquals("",   StringEscapeUtils.escapeCsv(""));
403         assertNull(StringEscapeUtils.escapeCsv(null));
404     }
405 
406     @Test
407     public void testEscapeCsvWriter() throws Exception {
408         checkCsvEscapeWriter("foo.bar",            "foo.bar");
409         checkCsvEscapeWriter("\"foo,bar\"",        "foo,bar");
410         checkCsvEscapeWriter("\"foo\nbar\"",       "foo\nbar");
411         checkCsvEscapeWriter("\"foo\rbar\"",       "foo\rbar");
412         checkCsvEscapeWriter("\"foo\"\"bar\"",     "foo\"bar");
413         checkCsvEscapeWriter("foo\uD84C\uDFB4bar", "foo\uD84C\uDFB4bar");
414         checkCsvEscapeWriter("", null);
415         checkCsvEscapeWriter("", "");
416     }
417 
418     private void checkCsvEscapeWriter(final String expected, final String value) throws IOException {
419         final StringWriter writer = new StringWriter();
420         StringEscapeUtils.ESCAPE_CSV.translate(value, writer);
421         assertEquals(expected, writer.toString());
422     }
423 
424     @Test
425     public void testEscapeCsvIllegalStateException() {
426         final StringWriter writer = new StringWriter();
427         assertThrows(IllegalStateException.class, () -> StringEscapeUtils.ESCAPE_CSV.translate("foo", -1, writer));
428     }
429 
430     @Test
431     public void testUnescapeCsvString() {
432         assertEquals("foo.bar",              StringEscapeUtils.unescapeCsv("foo.bar"));
433         assertEquals("foo,bar",              StringEscapeUtils.unescapeCsv("\"foo,bar\""));
434         assertEquals("foo\nbar",             StringEscapeUtils.unescapeCsv("\"foo\nbar\""));
435         assertEquals("foo\rbar",             StringEscapeUtils.unescapeCsv("\"foo\rbar\""));
436         assertEquals("foo\"bar",             StringEscapeUtils.unescapeCsv("\"foo\"\"bar\""));
437         assertEquals("foo\uD84C\uDFB4bar",   StringEscapeUtils.unescapeCsv("foo\uD84C\uDFB4bar"));
438         assertEquals("",   StringEscapeUtils.unescapeCsv(""));
439         assertNull(StringEscapeUtils.unescapeCsv(null));
440 
441         assertEquals("\"foo.bar\"",          StringEscapeUtils.unescapeCsv("\"foo.bar\""));
442     }
443 
444     @Test
445     public void testUnescapeCsvWriter() throws Exception {
446         checkCsvUnescapeWriter("foo.bar",            "foo.bar");
447         checkCsvUnescapeWriter("foo,bar",            "\"foo,bar\"");
448         checkCsvUnescapeWriter("foo\nbar",           "\"foo\nbar\"");
449         checkCsvUnescapeWriter("foo\rbar",           "\"foo\rbar\"");
450         checkCsvUnescapeWriter("foo\"bar",           "\"foo\"\"bar\"");
451         checkCsvUnescapeWriter("foo\uD84C\uDFB4bar", "foo\uD84C\uDFB4bar");
452         checkCsvUnescapeWriter("", null);
453         checkCsvUnescapeWriter("", "");
454 
455         checkCsvUnescapeWriter("\"foo.bar\"",        "\"foo.bar\"");
456     }
457 
458     private void checkCsvUnescapeWriter(final String expected, final String value) throws IOException {
459         final StringWriter writer = new StringWriter();
460         StringEscapeUtils.UNESCAPE_CSV.translate(value, writer);
461         assertEquals(expected, writer.toString());
462     }
463 
464     @Test
465     public void testUnescapeCsvIllegalStateException() {
466         final StringWriter writer = new StringWriter();
467         assertThrows(IllegalStateException.class, () -> StringEscapeUtils.UNESCAPE_CSV.translate("foo", -1, writer));
468     }
469 
470     /**
471      * Tests // https://issues.apache.org/jira/browse/LANG-480
472      */
473     @Test
474     public void testEscapeHtmlHighUnicode() {
475         // this is the utf8 representation of the character:
476         // COUNTING ROD UNIT DIGIT THREE
477         // in Unicode
478         // codepoint: U+1D362
479         final byte[] data = new byte[] { (byte) 0xF0, (byte) 0x9D, (byte) 0x8D, (byte) 0xA2 };
480 
481         final String original = new String(data, StandardCharsets.UTF_8);
482 
483         final String escaped = StringEscapeUtils.escapeHtml4( original );
484         assertEquals(original, escaped, "High Unicode should not have been escaped");
485 
486         final String unescaped = StringEscapeUtils.unescapeHtml4( escaped );
487         assertEquals(original, unescaped, "High Unicode should have been unchanged");
488 
489 // TODO: I think this should hold, needs further investigation
490 //        String unescapedFromEntity = StringEscapeUtils.unescapeHtml4( "&#119650;" );
491 //        assertEquals( "High Unicode should have been unescaped", original, unescapedFromEntity);
492     }
493 
494     /**
495      * Tests https://issues.apache.org/jira/browse/LANG-339
496      */
497     @Test
498     public void testEscapeHiragana() {
499         // Some random Japanese Unicode characters
500         final String original = "\u304B\u304C\u3068";
501         final String escaped = StringEscapeUtils.escapeHtml4(original);
502         assertEquals(original, escaped,
503                 "Hiragana character Unicode behavior should not be being escaped by escapeHtml4");
504 
505         final String unescaped = StringEscapeUtils.unescapeHtml4( escaped );
506 
507         assertEquals(escaped, unescaped, "Hiragana character Unicode behavior has changed - expected no unescaping");
508     }
509 
510     /**
511      * Tests https://issues.apache.org/jira/browse/LANG-708
512      *
513      * @throws IOException
514      *             if an I/O error occurs
515      */
516     @Test
517     public void testLang708() throws IOException {
518         final byte[] inputBytes = Files.readAllBytes(Paths.get("src/test/resources/lang-708-input.txt"));
519         final String input = new String(inputBytes, StandardCharsets.UTF_8);
520         final String escaped = StringEscapeUtils.escapeEcmaScript(input);
521         // just the end:
522         assertTrue(escaped.endsWith("}]"), escaped);
523         // a little more:
524         assertTrue(escaped.endsWith("\"valueCode\\\":\\\"\\\"}]"), escaped);
525     }
526 
527     /**
528      * Tests https://issues.apache.org/jira/browse/LANG-720
529      */
530     @Test
531     public void testLang720() {
532         final String input = "\ud842\udfb7" + "A";
533         final String escaped = StringEscapeUtils.escapeXml(input);
534         assertEquals(input, escaped);
535     }
536 
537     /**
538      * Tests https://issues.apache.org/jira/browse/LANG-911
539      */
540     @Test
541     public void testLang911() {
542         final String bellsTest = "\ud83d\udc80\ud83d\udd14";
543         final String value = StringEscapeUtils.escapeJava(bellsTest);
544         final String valueTest = StringEscapeUtils.unescapeJava(value);
545         assertEquals(bellsTest, valueTest);
546     }
547 
548     @Test
549     public void testEscapeJson() {
550         assertNull(StringEscapeUtils.escapeJson(null));
551         assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_JSON.translate(null, null));
552         assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_JSON.translate("", null));
553 
554         assertEquals("He didn't say, \\\"stop!\\\"", StringEscapeUtils.escapeJson("He didn't say, \"stop!\""));
555 
556         final String expected = "\\\"foo\\\" isn't \\\"bar\\\". specials: \\b\\r\\n\\f\\t\\\\\\/";
557         final String input ="\"foo\" isn't \"bar\". specials: \b\r\n\f\t\\/";
558 
559         assertEquals(expected, StringEscapeUtils.escapeJson(input));
560     }
561 
562     @Test
563     public void testUnescapeJson() {
564         assertNull(StringEscapeUtils.unescapeJson(null));
565         assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_JSON.translate(null, null));
566         assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_JSON.translate("", null));
567 
568         assertEquals("He didn't say, \"stop!\"", StringEscapeUtils.unescapeJson("He didn't say, \\\"stop!\\\""));
569 
570         final String expected ="\"foo\" isn't \"bar\". specials: \b\r\n\f\t\\/";
571         final String input = "\\\"foo\\\" isn't \\\"bar\\\". specials: \\b\\r\\n\\f\\t\\\\\\/";
572 
573         assertEquals(expected, StringEscapeUtils.unescapeJson(input));
574     }
575 }