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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Unit tests.
28   * {@link StaticBucketMap}.
29   */
30  public class StaticBucketMapTest<K, V> extends AbstractIterableMapTest<K, V> {
31  
32      public StaticBucketMapTest() {
33          super(StaticBucketMapTest.class.getSimpleName());
34      }
35  
36      @Override
37      public String[] ignoredTests() {
38          final String pre = "StaticBucketMapTest.bulkTestMap";
39          final String post = ".testCollectionIteratorFailFast";
40          return new String[] {
41              pre + "EntrySet" + post,
42              pre + "KeySet" + post,
43              pre + "Values" + post
44          };
45      }
46  
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      public boolean isFailFastExpected() {
52          return false;
53      }
54  
55      @Override
56      public StaticBucketMap<K, V> makeObject() {
57          return new StaticBucketMap<>(30);
58      }
59  
60      @Test
61      @SuppressWarnings("unchecked")
62      public void test_containsKey_nullMatchesIncorrectly() {
63          final StaticBucketMap<K, V> map = new StaticBucketMap<>(17);
64          map.put(null, (V) "A");
65          assertTrue(map.containsKey(null));
66          // loop so we find a string that is in the same bucket as the null
67          for (int i = 'A'; i <= 'Z'; i++) {
68              final String str = String.valueOf((char) i);
69              assertFalse(map.containsKey(str), "String: " + str);
70          }
71      }
72  
73      @Test
74      @SuppressWarnings("unchecked")
75      public void test_containsValue_nullMatchesIncorrectly() {
76          final StaticBucketMap<K, V> map = new StaticBucketMap<>(17);
77          map.put((K) "A", null);
78          assertTrue(map.containsValue(null));
79          // loop so we find a string that is in the same bucket as the null
80          for (int i = 'A'; i <= 'Z'; i++) {
81              final String str = String.valueOf((char) i);
82              assertFalse(map.containsValue(str), "String: " + str);
83          }
84      }
85  
86      // Bugzilla 37567
87      @Test
88      @SuppressWarnings("unchecked")
89      public void test_get_nullMatchesIncorrectly() {
90          final StaticBucketMap<K, V> map = new StaticBucketMap<>(17);
91          map.put(null, (V) "A");
92          assertEquals("A", map.get(null));
93          // loop so we find a string that is in the same bucket as the null
94          for (int i = 'A'; i <= 'Z'; i++) {
95              final String str = String.valueOf((char) i);
96              assertNull(map.get(str), "String: " + str);
97          }
98      }
99  
100 }