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  
22  import java.io.BufferedReader;
23  import java.io.FileReader;
24  import java.util.Map;
25  
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests {@link EntityArrays}.
30   */
31  class EntityArraysTest {
32  
33      @Test
34      void testAposMap() {
35          testEscapeVsUnescapeMaps(EntityArrays.APOS_ESCAPE, EntityArrays.APOS_UNESCAPE);
36      }
37  
38      @Test
39      void testBasicMap() {
40          testEscapeVsUnescapeMaps(EntityArrays.BASIC_ESCAPE, EntityArrays.BASIC_UNESCAPE);
41      }
42  
43      @Test
44      void testConstructorExists() {
45          new EntityArrays();
46      }
47  
48      private void testEscapeVsUnescapeMaps(final Map<CharSequence, CharSequence> escapeMap, final Map<CharSequence, CharSequence> unescapeMap) {
49          for (final CharSequence escapeKey : escapeMap.keySet()) {
50              for (final CharSequence unescapeKey : unescapeMap.keySet()) {
51                  if (escapeKey == unescapeMap.get(unescapeKey)) {
52                      assertEquals(escapeMap.get(escapeKey), unescapeKey);
53                  }
54              }
55          }
56      }
57  
58      // LANG-659, LANG-658 - avoid duplicate entries
59      @Test
60      void testForDuplicatedDeclaredMapKeys() throws Exception {
61          final String packageDirectory = EntityArraysTest.class.getPackage().getName().replace(".", "/");
62          try (BufferedReader br = new BufferedReader(new FileReader("src/main/java/" + packageDirectory + "/EntityArrays.java"))) {
63              String line;
64              int mapDeclarationCounter = 0;
65              while ((line = br.readLine()) != null) {
66                  // Start with map declaration and count put lines
67                  if (line.contains("new HashMap<>();")) {
68                      mapDeclarationCounter = 0;
69                  } else if (line.contains(".put(")) {
70                      mapDeclarationCounter++;
71                  } else if (line.contains("Collections.unmodifiableMap(initialMap);")) {
72                      final String mapVariableName = line.split("=")[0].trim();
73                      @SuppressWarnings("unchecked") // This is test code
74                      final Map<String, String> mapValue = (Map<String, String>) EntityArrays.class.getDeclaredField(mapVariableName).get(EntityArrays.class);
75                      // Validate that we are not inserting into the same key twice in the map declaration. If this,
76                      // indeed was the case the keySet().size() would be smaller than the number of put() statements
77                      assertEquals(mapDeclarationCounter, mapValue.size());
78                  }
79              }
80          }
81      }
82  
83      @Test
84      void testForDuplicateDeclaredMapValuesAposMap() {
85          assertEquals(EntityArrays.APOS_UNESCAPE.size(), EntityArrays.APOS_ESCAPE.size());
86      }
87  
88      @Test
89      void testForDuplicateDeclaredMapValuesBasicMap() {
90          assertEquals(EntityArrays.BASIC_ESCAPE.size(), EntityArrays.BASIC_UNESCAPE.size());
91      }
92  
93      @Test
94      void testForDuplicateDeclaredMapValuesHtml40ExtendedMap() {
95          assertEquals(EntityArrays.HTML40_EXTENDED_ESCAPE.size(), EntityArrays.HTML40_EXTENDED_UNESCAPE.size());
96      }
97  
98      @Test
99      void testForDuplicateDeclaredMapValuesISO8859Map() {
100         assertEquals(EntityArrays.ISO8859_1_ESCAPE.size(), EntityArrays.ISO8859_1_UNESCAPE.size());
101     }
102 
103     @Test
104     void testForDuplicateDeclaredMapValuesJavaCtrlCharsMap() {
105         assertEquals(EntityArrays.JAVA_CTRL_CHARS_ESCAPE.size(), EntityArrays.JAVA_CTRL_CHARS_UNESCAPE.size());
106     }
107 
108     @Test
109     void testHtml40ExtendedMap() {
110         testEscapeVsUnescapeMaps(EntityArrays.HTML40_EXTENDED_ESCAPE, EntityArrays.HTML40_EXTENDED_UNESCAPE);
111     }
112 
113     @Test
114     void testISO8859Map() {
115         testEscapeVsUnescapeMaps(EntityArrays.ISO8859_1_ESCAPE, EntityArrays.ISO8859_1_UNESCAPE);
116     }
117 
118     @Test
119     void testJavaCtrlCharsMap() {
120         testEscapeVsUnescapeMaps(EntityArrays.JAVA_CTRL_CHARS_ESCAPE, EntityArrays.JAVA_CTRL_CHARS_UNESCAPE);
121     }
122 
123 }