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  package org.apache.commons.text.translate;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests {@link JavaUnicodeEscaper}.
25   */
26  class JavaUnicodeEscaperTest {
27  
28      @Test
29      void testAbove() {
30          final JavaUnicodeEscaper jue = JavaUnicodeEscaper.above('F');
31          final String input = "ADFGZ";
32          final String result = jue.translate(input);
33          assertEquals("ADF\\u0047\\u005A", result, "Failed to escape Unicode characters via the above method");
34      }
35  
36      @Test
37      void testBelow() {
38          final JavaUnicodeEscaper jue = JavaUnicodeEscaper.below('F');
39          final String input = "ADFGZ";
40          final String result = jue.translate(input);
41          assertEquals("\\u0041\\u0044FGZ", result, "Failed to escape Unicode characters via the below method");
42      }
43  
44      @Test
45      void testBetween() {
46          final JavaUnicodeEscaper jue = JavaUnicodeEscaper.between('F', 'L');
47          final String input = "ADFGZ";
48          final String result = jue.translate(input);
49          assertEquals("AD\\u0046\\u0047Z", result, "Failed to escape Unicode characters via the between method");
50      }
51  
52      @Test
53      void testToUtf16Escape() {
54          final JavaUnicodeEscaper jue = JavaUnicodeEscaper.below('F');
55          // According to https://en.wikipedia.org/wiki/UTF-16#Code_points_U.2B10000..U.2B10FFFF,
56          // Character ?, U+24B62, Binary Code Point 0010 0100 1011 0110 0010,
57          // Binary UTF-167 1101 1000 0101 0010 1101 1111 0110 0010, UTF-16 Hex Code Units D852 DF62
58          final String encoding = jue.toUtf16Escape(Integer.parseInt("024B62", 16));
59          assertEquals("\\uD852\\uDF62", encoding);
60      }
61  }