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