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  
18  package org.apache.commons.lang3.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.apache.commons.lang3.AbstractLangTest;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Unit tests for {@link org.apache.commons.lang3.text.translate.NumericEntityUnescaper}.
28   */
29  @Deprecated
30  public class NumericEntityUnescaperTest extends AbstractLangTest {
31  
32      @Test
33      public void testOutOfBounds() {
34          final NumericEntityUnescaper neu = new NumericEntityUnescaper();
35  
36          assertEquals("Test &", neu.translate("Test &"), "Failed to ignore when last character is &");
37          assertEquals("Test &#", neu.translate("Test &#"), "Failed to ignore when last character is &");
38          assertEquals("Test &#x", neu.translate("Test &#x"), "Failed to ignore when last character is &");
39          assertEquals("Test &#X", neu.translate("Test &#X"), "Failed to ignore when last character is &");
40      }
41  
42      @Test
43      public void testSupplementaryUnescaping() {
44          final NumericEntityUnescaper neu = new NumericEntityUnescaper();
45          final String input = "𐰢";
46          final String expected = "\uD803\uDC22";
47  
48          final String result = neu.translate(input);
49          assertEquals(expected, result, "Failed to unescape numeric entities supplementary characters");
50      }
51  
52      @Test
53      public void testUnfinishedEntity() {
54          // parse it
55          NumericEntityUnescaper neu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.semiColonOptional);
56          String input = "Test &#x30 not test";
57          String expected = "Test \u0030 not test";
58  
59          String result = neu.translate(input);
60          assertEquals(expected, result, "Failed to support unfinished entities (i.e. missing semicolon)");
61  
62          // ignore it
63          neu = new NumericEntityUnescaper();
64          input = "Test &#x30 not test";
65          expected = input;
66  
67          result = neu.translate(input);
68          assertEquals(expected, result, "Failed to ignore unfinished entities (i.e. missing semicolon)");
69  
70          // fail it
71          final NumericEntityUnescaper failingNeu =
72                  new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.errorIfNoSemiColon);
73          final String failingInput = "Test &#x30 not test";
74          assertThrows(IllegalArgumentException.class, () -> failingNeu.translate(failingInput));
75      }
76  
77  }