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