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.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.io.StringWriter;
23  
24  import org.apache.commons.lang3.AbstractLangTest;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Tests for {@link org.apache.commons.lang3.text.translate.NumericEntityEscaper}.
29   */
30  @Deprecated
31  class NumericEntityEscaperTest extends AbstractLangTest {
32  
33      @Test
34      void testAbove() {
35          final NumericEntityEscaper nee = NumericEntityEscaper.above('F');
36          final String input = "ADFGZ";
37          final String result = nee.translate(input);
38          assertEquals("ADFGZ", result, "Failed to escape numeric entities via the above method");
39      }
40  
41      @Test
42      void testBelow() {
43          final NumericEntityEscaper nee = NumericEntityEscaper.below('F');
44          final String input = "ADFGZ";
45          final String result = nee.translate(input);
46          assertEquals("ADFGZ", result, "Failed to escape numeric entities via the below method");
47      }
48  
49      @Test
50      void testBetween() {
51          final NumericEntityEscaper nee = NumericEntityEscaper.between('F', 'L');
52          final String input = "ADFGZ";
53          final String result = nee.translate(input);
54          assertEquals("ADFGZ", result, "Failed to escape numeric entities via the between method");
55      }
56  
57      @Test
58      void testNumericEntityOverflow() throws Exception {
59          // cp = 1234567890 > Character.MAX_CODE_POINT (0x10FFFF = 1114111).
60          // Pre-patch: IAE escapes from Character.toChars.
61          // Post-patch: return 0, no write, no exception.
62          final NumericEntityUnescaper u = new NumericEntityUnescaper();
63          final StringWriter sw = new StringWriter();
64          int consumed = u.translate("�", 0, sw);
65          assertEquals(0, consumed);
66          assertEquals("", sw.toString());
67          consumed = u.translate("---�---", 0, sw);
68          assertEquals(0, consumed);
69          assertEquals("", sw.toString());
70      }
71  
72      // See LANG-617
73      @Test
74      void testSupplementary() {
75          final NumericEntityEscaper nee = new NumericEntityEscaper();
76          final String input = "\uD803\uDC22";
77          final String expected = "𐰢";
78          final String result = nee.translate(input);
79          assertEquals(expected, result, "Failed to escape numeric entities supplementary characters");
80      }
81  
82      @Test
83      void testValidCodePoint() throws Exception {
84          // Negative control: 'A' = 'A' must translate successfully.
85          final NumericEntityUnescaper u = new NumericEntityUnescaper();
86          final StringWriter sw = new StringWriter();
87          final int consumed = u.translate("A", 0, sw);
88          assertEquals("A", sw.toString());
89          assertEquals(5, consumed);
90      }
91  }