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.text.translate;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
22  
23  import java.io.IOException;
24  import java.io.StringWriter;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link LookupTranslator}.
32   */
33  class LookupTranslatorTest  {
34  
35      @Test
36      void testBasicLookup() throws IOException {
37          final Map<CharSequence, CharSequence> translatorMap = new HashMap<>();
38          translatorMap.put("one", "two");
39          final LookupTranslator lt = new LookupTranslator(translatorMap);
40          final StringWriter out = new StringWriter();
41          final int result = lt.translate("one", 0, out);
42          assertEquals(3, result, "Incorrect code point consumption");
43          assertEquals("two", out.toString(), "Incorrect value");
44      }
45  
46      @Test
47      void testFailsToCreateLookupTranslatorThrowsInvalidParameterException() {
48          assertThrowsExactly(NullPointerException.class, () -> new LookupTranslator(null));
49      }
50  
51      // Tests: https://issues.apache.org/jira/browse/LANG-882
52      @Test
53      void testLang882() throws IOException {
54          final Map<CharSequence, CharSequence> translatorMap = new HashMap<>();
55          translatorMap.put(new StringBuffer("one"), new StringBuffer("two"));
56          final LookupTranslator lt = new LookupTranslator(translatorMap);
57          final StringWriter out = new StringWriter();
58          final int result = lt.translate(new StringBuffer("one"), 0, out);
59          assertEquals(3, result, "Incorrect code point consumption");
60          assertEquals("two", out.toString(), "Incorrect value");
61      }
62  
63      @Test
64      void testTranslateSupplementaryCharacter() {
65          /* Key: string with Mathematical double-struck capital A (U+1D538) */
66          final String symbol = new StringBuilder().appendCodePoint(0x1D538).toString();
67          /* Map U+1D538 to "A" */
68          final Map<CharSequence, CharSequence> map = new HashMap<>();
69          map.put(symbol, "A");
70          final LookupTranslator translator = new LookupTranslator(map);
71          final String translated = translator.translate(symbol + "=A");
72          /* we should get "A=A". */
73          assertEquals("A=A", translated, "Incorrect value");
74      }
75  
76  }