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.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import org.apache.commons.lang3.AbstractLangTest;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests for {@link org.apache.commons.lang3.text.translate.NumericEntityUnescaper}.
28   */
29  @Deprecated
30  class NumericEntityUnescaperTest extends AbstractLangTest {
31  
32      @Test
33      void testDecimalEntityFollowedByHexLetter() {
34          // A decimal entity ends at the first non-decimal character, so a following a-f letter is text, not part of the number.
35          final NumericEntityUnescaper neu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.semiColonOptional);
36          assertEquals("0abc", neu.translate("&#48abc"), "Failed to stop a decimal entity at a trailing hex letter");
37          assertEquals("Test 0 not test", neu.translate("Test &#48 not test"), "Failed on a decimal entity terminated by a space");
38          assertEquals("0xyz", neu.translate("&#48xyz"), "Failed on a decimal entity terminated by a non-hex letter");
39      }
40  
41      @Test
42      void testOutOfBounds() {
43          final NumericEntityUnescaper neu = new NumericEntityUnescaper();
44          assertEquals("Test &", neu.translate("Test &"), "Failed to ignore when last character is &");
45          assertEquals("Test &#", neu.translate("Test &#"), "Failed to ignore when last character is &");
46          assertEquals("Test &#x", neu.translate("Test &#x"), "Failed to ignore when last character is &");
47          assertEquals("Test &#X", neu.translate("Test &#X"), "Failed to ignore when last character is &");
48      }
49  
50      @Test
51      void testOutOfRangeCodePoint() {
52          final NumericEntityUnescaper neu = new NumericEntityUnescaper();
53          assertEquals("�", neu.translate("�"), "Failed to ignore code point above 0x10FFFF");
54          assertEquals("�", neu.translate("�"), "Failed to ignore code point above 0x10FFFF");
55          assertEquals("�", neu.translate("�"), "Failed to ignore code point above 0x10FFFF");
56      }
57  
58      @Test
59      void testSupplementaryUnescaping() {
60          final NumericEntityUnescaper neu = new NumericEntityUnescaper();
61          final String input = "𐰢";
62          final String expected = "\uD803\uDC22";
63          final String result = neu.translate(input);
64          assertEquals(expected, result, "Failed to unescape numeric entities supplementary characters");
65      }
66  
67      @Test
68      void testUnfinishedEntity() {
69          // parse it
70          NumericEntityUnescaper neu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.semiColonOptional);
71          String input = "Test &#x30 not test";
72          String expected = "Test \u0030 not test";
73          String result = neu.translate(input);
74          assertEquals(expected, result, "Failed to support unfinished entities (i.e. missing semicolon)");
75          // ignore it
76          neu = new NumericEntityUnescaper();
77          input = "Test &#x30 not test";
78          expected = input;
79          result = neu.translate(input);
80          assertEquals(expected, result, "Failed to ignore unfinished entities (i.e. missing semicolon)");
81          // fail it
82          final NumericEntityUnescaper failingNeu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.errorIfNoSemiColon);
83          final String failingInput = "Test &#x30 not test";
84          assertIllegalArgumentException(() -> failingNeu.translate(failingInput));
85      }
86  
87  }