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    *      https://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  
18  package org.apache.commons.lang3.text.translate;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import org.apache.commons.lang3.AbstractLangTest;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests for {@link org.apache.commons.lang3.text.translate.UnicodeEscaper}.
27   */
28  @Deprecated
29  class UnicodeUnescaperTest extends AbstractLangTest {
30  
31      @Test
32      void testLessThanFour() {
33          final UnicodeUnescaper uu = new UnicodeUnescaper();
34          // A truncated escape is not a well-formed escape: it passes through untranslated.
35          final String input = "\\0047\\u006";
36          assertEquals(input, uu.translate(input), "A truncated Unicode escape sequence must pass through untranslated");
37      }
38  
39      @Test
40      void testNonAsciiHexDigits() {
41          final UnicodeUnescaper uu = new UnicodeUnescaper();
42          // Integer.parseInt would accept Unicode decimal digits from any script and fullwidth Latin hex
43          // letters via Character.digit; those spellings are not well-formed escapes and pass through.
44          assertEquals("\\u\uFF10\uFF10\uFF12\uFF12", uu.translate("\\u\uFF10\uFF10\uFF12\uFF12"),
45                  "Fullwidth digit spellings must pass through untranslated");
46          assertEquals("\\u\u0660\u0660\u0664\u0661", uu.translate("\\u\u0660\u0660\u0664\u0661"),
47                  "Arabic-Indic digit spellings must pass through untranslated");
48      }
49  
50      @Test
51      void testSignedValue() {
52          final UnicodeUnescaper uu = new UnicodeUnescaper();
53          // Integer.parseInt accepts a leading sign, but a sign character is not an ASCII hex digit:
54          // these are not well-formed escapes and pass through untranslated.
55          assertEquals("\\u-047", uu.translate("\\u-047"), "A signed Unicode escape sequence must pass through untranslated");
56          assertEquals("\\u++0047", uu.translate("\\u++0047"), "A signed Unicode escape sequence must pass through untranslated");
57          // The documented u+ notation is still accepted.
58          assertEquals("G", uu.translate("\\u+0047"), "Failed to unescape Unicode characters with 'u+' notation");
59      }
60  
61      // Requested in LANG-507
62      @Test
63      void testUPlus() {
64          final UnicodeUnescaper uu = new UnicodeUnescaper();
65          final String input = "\\u+0047";
66          assertEquals("G", uu.translate(input), "Failed to unescape Unicode characters with 'u+' notation");
67      }
68  
69      @Test
70      void testUuuuu() {
71          final UnicodeUnescaper uu = new UnicodeUnescaper();
72          final String input = "\\uuuuuuuu0047";
73          final String result = uu.translate(input);
74          assertEquals("G", result, "Failed to unescape Unicode characters with many 'u' characters");
75      }
76  }