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 java.io.IOException;
22  import java.io.StringWriter;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests {@link AggregateTranslator}.
30   */
31  class AggregateTranslatorTest {
32  
33      @Test
34      void testNonNull() throws IOException {
35          final Map<CharSequence, CharSequence> oneTwoMap = new HashMap<>();
36          oneTwoMap.put("one", "two");
37          final Map<CharSequence, CharSequence> threeFourMap = new HashMap<>();
38          threeFourMap.put("three", "four");
39          final CharSequenceTranslator translator1 = new LookupTranslator(oneTwoMap);
40          final CharSequenceTranslator translator2 = new LookupTranslator(threeFourMap);
41          final AggregateTranslator subject = new AggregateTranslator(translator1, translator2);
42          final StringWriter out1 = new StringWriter();
43          final int result1 = subject.translate(new StringBuffer("one"), 0, out1);
44          assertEquals(3, result1, "Incorrect code point consumption");
45          assertEquals("two", out1.toString());
46          final StringWriter out2 = new StringWriter();
47          final int result2 = subject.translate(new StringBuffer("three"), 0, out2);
48          assertEquals(5, result2, "Incorrect code point consumption");
49          assertEquals("four", out2.toString(), "Incorrect value");
50      }
51  
52      @Test
53      void testNullConstructor() {
54          final String testString = "foo";
55          final AggregateTranslator subject = new AggregateTranslator((CharSequenceTranslator[]) null);
56          assertEquals(testString, subject.translate(testString));
57      }
58  
59      @Test
60      void testNullVarargConstructor() {
61          final String testString = "foo";
62          final AggregateTranslator subject = new AggregateTranslator((CharSequenceTranslator) null);
63          assertEquals(testString, subject.translate(testString));
64      }
65  
66  }