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  package org.apache.commons.text.translate;
18  
19  import static org.assertj.core.api.Assertions.assertThat;
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  public class AggregateTranslatorTest {
32  
33      @Test
34      public 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          assertThat(result1).as("Incorrect code point consumption").isEqualTo(3);
45          assertThat(out1.toString()).as("Incorrect value").isEqualTo("two");
46          final StringWriter out2 = new StringWriter();
47          final int result2 = subject.translate(new StringBuffer("three"), 0, out2);
48          assertThat(result2).as("Incorrect code point consumption").isEqualTo(5);
49          assertThat(out2.toString()).as("Incorrect value").isEqualTo("four");
50      }
51  
52      @Test
53      public void testNullConstructor() {
54          final String testString = "foo";
55          final AggregateTranslator subject = new AggregateTranslator((CharSequenceTranslator[]) null);
56          assertThat(subject.translate(testString)).isEqualTo(testString);
57      }
58  
59      @Test
60      public void testNullVarargConstructor() {
61          final String testString = "foo";
62          final AggregateTranslator subject = new AggregateTranslator((CharSequenceTranslator) null);
63          assertThat(subject.translate(testString)).isEqualTo(testString);
64      }
65  
66  }