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.collections4.map;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertSame;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.HashMap;
24  import java.util.Locale;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.commons.lang3.StringUtils;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests for the {@link CaseInsensitiveMap} implementation.
33   */
34  public class CaseInsensitiveMapTest<K, V> extends AbstractIterableMapTest<K, V> {
35  
36      public CaseInsensitiveMapTest() {
37          super(CaseInsensitiveMapTest.class.getSimpleName());
38      }
39  
40      @Override
41      public String getCompatibilityVersion() {
42          return "4";
43      }
44  
45      @Override
46      public CaseInsensitiveMap<K, V> makeObject() {
47          return new CaseInsensitiveMap<>();
48      }
49  
50      @Test
51      @SuppressWarnings("unchecked")
52      public void testCaseInsensitive() {
53          final Map<K, V> map = makeObject();
54          map.put((K) "One", (V) "One");
55          map.put((K) "Two", (V) "Two");
56          assertEquals("One", map.get("one"));
57          assertEquals("One", map.get("oNe"));
58          map.put((K) "two", (V) "Three");
59          assertEquals("Three", map.get("Two"));
60      }
61  
62      @Test
63      @SuppressWarnings("unchecked")
64      public void testClone() {
65          final CaseInsensitiveMap<K, V> map = new CaseInsensitiveMap<>(10);
66          map.put((K) "1", (V) "1");
67          final CaseInsensitiveMap<K, V> cloned = map.clone();
68          assertEquals(map.size(), cloned.size());
69          assertSame(map.get("1"), cloned.get("1"));
70      }
71  
72      /**
73       * Test for <a href="https://issues.apache.org/jira/browse/COLLECTIONS-323">COLLECTIONS-323</a>.
74       */
75      @Test
76      public void testInitialCapacityZero() {
77          final CaseInsensitiveMap<String, String> map = new CaseInsensitiveMap<>(0);
78          assertEquals(1, map.data.length);
79      }
80  
81      // COLLECTIONS-294
82      @Test
83      public void testLocaleIndependence() {
84          final Locale orig = Locale.getDefault();
85  
86          final Locale[] locales = { Locale.ENGLISH, new Locale("tr", StringUtils.EMPTY, StringUtils.EMPTY), Locale.getDefault() };
87  
88          final String[][] data = {
89              { "i", "I" },
90              { "\u03C2", "\u03C3" },
91              { "\u03A3", "\u03C2" },
92              { "\u03A3", "\u03C3" },
93          };
94  
95          try {
96              for (final Locale locale : locales) {
97                  Locale.setDefault(locale);
98                  for (int j = 0; j < data.length; j++) {
99                      assertTrue(data[j][0].equalsIgnoreCase(data[j][1]), "Test data corrupt: " + j);
100                     final CaseInsensitiveMap<String, String> map = new CaseInsensitiveMap<>();
101                     map.put(data[j][0], "value");
102                     assertEquals("value", map.get(data[j][1]), Locale.getDefault() + ": " + j);
103                 }
104             }
105         } finally {
106             Locale.setDefault(orig);
107         }
108     }
109 
110 //    public void testCreate() throws Exception {
111 //        resetEmpty();
112 //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/CaseInsensitiveMap.emptyCollection.version4.obj");
113 //        resetFull();
114 //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/CaseInsensitiveMap.fullCollection.version4.obj");
115 //    }
116 
117     @Test
118     @SuppressWarnings("unchecked")
119     public void testNullHandling() {
120         final Map<K, V> map = makeObject();
121         map.put((K) "One", (V) "One");
122         map.put((K) "Two", (V) "Two");
123         map.put(null, (V) "Three");
124         assertEquals("Three", map.get(null));
125         map.put(null, (V) "Four");
126         assertEquals("Four", map.get(null));
127         final Set<K> keys = map.keySet();
128         assertTrue(keys.contains("one"));
129         assertTrue(keys.contains("two"));
130         assertTrue(keys.contains(null));
131         assertEquals(3, keys.size());
132     }
133 
134     @Test
135     public void testPutAll() {
136         final Map<Object, String> map = new HashMap<>();
137         map.put("One", "One");
138         map.put("Two", "Two");
139         map.put("one", "Three");
140         map.put(null, "Four");
141         map.put(Integer.valueOf(20), "Five");
142         final Map<Object, String> caseInsensitiveMap = new CaseInsensitiveMap<>(map);
143         assertEquals(4, caseInsensitiveMap.size()); // ones collapsed
144         final Set<Object> keys = caseInsensitiveMap.keySet();
145         assertTrue(keys.contains("one"));
146         assertTrue(keys.contains("two"));
147         assertTrue(keys.contains(null));
148         assertTrue(keys.contains(Integer.toString(20)));
149         assertEquals(4, keys.size());
150         assertTrue(!caseInsensitiveMap.containsValue("One")
151             || !caseInsensitiveMap.containsValue("Three")); // ones collapsed
152         assertEquals("Four", caseInsensitiveMap.get(null));
153     }
154 }