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.fail;
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 =
34                  new NumericEntityUnescaper(numericEntityUnescaperOPTIONArray);
35          assertEquals("2|y|O7y`&#uVWj", numericEntityUnescaper.translate("2|y|O7y`&#uVWj"));
36      }
37  
38      @Test
39      void testCreatesNumericEntityUnescaperTwo() {
40          final NumericEntityUnescaper.OPTION[] numericEntityUnescaperOPTIONArray = {};
41          final NumericEntityUnescaper numericEntityUnescaper =
42                  new NumericEntityUnescaper(numericEntityUnescaperOPTIONArray);
43          assertEquals("Ws2v8|O=7NR&#cB", numericEntityUnescaper.translate("Ws2v8|O=7NR&#cB"));
44      }
45  
46      @Test
47      void testOutOfBounds() {
48          final NumericEntityUnescaper neu = new NumericEntityUnescaper();
49  
50          assertEquals("Test &", neu.translate("Test &"), "Failed to ignore when last character is &");
51          assertEquals("Test &#", neu.translate("Test &#"), "Failed to ignore when last character is &");
52          assertEquals("Test &#x", neu.translate("Test &#x"), "Failed to ignore when last character is &");
53          assertEquals("Test &#X", neu.translate("Test &#X"), "Failed to ignore when last character is &");
54      }
55  
56      @Test
57      void testSupplementaryUnescaping() {
58          final NumericEntityUnescaper neu = new NumericEntityUnescaper();
59          final String input = "𐰢";
60          final String expected = "\uD803\uDC22";
61  
62          final String result = neu.translate(input);
63          assertEquals(expected, result, "Failed to unescape numeric entities supplementary characters");
64      }
65  
66      @Test
67      void testUnfinishedEntity() {
68          // parse it
69          NumericEntityUnescaper neu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.semiColonOptional);
70          String input = "Test &#x30 not test";
71          String expected = "Test \u0030 not test";
72  
73          String result = neu.translate(input);
74          assertEquals(expected, result, "Failed to support unfinished entities (i.e. missing semicolon)");
75  
76          // ignore it
77          neu = new NumericEntityUnescaper();
78          input = "Test &#x30 not test";
79          expected = input;
80  
81          result = neu.translate(input);
82          assertEquals(expected, result, "Failed to ignore unfinished entities (i.e. missing semicolon)");
83  
84          // fail it
85          neu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.errorIfNoSemiColon);
86          input = "Test &#x30 not test";
87  
88          try {
89              result = neu.translate(input);
90              fail("IllegalArgumentException expected");
91          } catch (final IllegalArgumentException iae) {
92              // expected
93          }
94      }
95  
96  }