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.text.translate;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests {@link NumericEntityUnescaper}.
27   */
28  class NumericEntityUnescaperTest {
29  
30      @Test
31      void testCreatesNumericEntityUnescaperOne() {
32          final NumericEntityUnescaper.OPTION[] numericEntityUnescaperOPTIONArray = {};
33          final NumericEntityUnescaper numericEntityUnescaper = new NumericEntityUnescaper(numericEntityUnescaperOPTIONArray);
34          assertEquals("2|y|O7y`&#uVWj", numericEntityUnescaper.translate("2|y|O7y`&#uVWj"));
35      }
36  
37      @Test
38      void testCreatesNumericEntityUnescaperTwo() {
39          final NumericEntityUnescaper.OPTION[] numericEntityUnescaperOPTIONArray = {};
40          final NumericEntityUnescaper numericEntityUnescaper = new NumericEntityUnescaper(numericEntityUnescaperOPTIONArray);
41          assertEquals("Ws2v8|O=7NR&#cB", numericEntityUnescaper.translate("Ws2v8|O=7NR&#cB"));
42      }
43  
44      @Test
45      void testOutOfBounds() {
46          final NumericEntityUnescaper neu = new NumericEntityUnescaper();
47          assertEquals("Test &", neu.translate("Test &"), "Failed to ignore when last character is &");
48          assertEquals("Test &#", neu.translate("Test &#"), "Failed to ignore when last character is &");
49          assertEquals("Test &#x", neu.translate("Test &#x"), "Failed to ignore when last character is &");
50          assertEquals("Test &#X", neu.translate("Test &#X"), "Failed to ignore when last character is &");
51      }
52  
53      @Test
54      void testOutOfRangeCodePoint() {
55          final NumericEntityUnescaper neu = new NumericEntityUnescaper();
56          assertEquals("�", neu.translate("�"), "Failed to ignore code point above 0x10FFFF");
57          assertEquals("�", neu.translate("�"), "Failed to ignore code point above 0x10FFFF");
58          assertEquals("�", neu.translate("�"), "Failed to ignore code point above 0x10FFFF");
59      }
60  
61      @Test
62      void testSupplementaryUnescaping() {
63          final NumericEntityUnescaper neu = new NumericEntityUnescaper();
64          final String input = "𐰢";
65          final String expected = "\uD803\uDC22";
66          final String result = neu.translate(input);
67          assertEquals(expected, result, "Failed to unescape numeric entities supplementary characters");
68      }
69  
70      @Test
71      void testUnfinishedEntity() {
72          // parse it
73          NumericEntityUnescaper neu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.semiColonOptional);
74          final String input = "Test &#x30 not test";
75          String expected = "Test \u0030 not test";
76          String result = neu.translate(input);
77          assertEquals(expected, result, "Failed to support unfinished entities (i.e. missing semicolon)");
78          // ignore it
79          neu = new NumericEntityUnescaper();
80          expected = input;
81          result = neu.translate(input);
82          assertEquals(expected, result, "Failed to ignore unfinished entities (i.e. missing semicolon)");
83          // fail it
84          assertThrows(IllegalArgumentException.class,
85                  () -> new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.errorIfNoSemiColon).translate("Test &#x30 not test"));
86      }
87  }