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   * @param <K> the key type.
31   * @param <V> the value type.
32   */
33  public class StaticBucketMapTest<K, V> extends AbstractIterableMapTest<K, V> {
34  
35      /**
36       * {@inheritDoc}
37       */
38      @Override
39      public boolean isFailFastExpected() {
40          return false;
41      }
42  
43      @Override
44      public StaticBucketMap<K, V> makeObject() {
45          return new StaticBucketMap<>(30);
46      }
47  
48      @Test
49      @SuppressWarnings("unchecked")
50      public void test_containsKey_nullMatchesIncorrectly() {
51          final StaticBucketMap<K, V> map = new StaticBucketMap<>(17);
52          map.put(null, (V) "A");
53          assertTrue(map.containsKey(null));
54          // loop so we find a string that is in the same bucket as the null
55          for (int i = 'A'; i <= 'Z'; i++) {
56              final String str = String.valueOf((char) i);
57              assertFalse(map.containsKey(str), "String: " + str);
58          }
59      }
60  
61      @Test
62      @SuppressWarnings("unchecked")
63      public void test_containsValue_nullMatchesIncorrectly() {
64          final StaticBucketMap<K, V> map = new StaticBucketMap<>(17);
65          map.put((K) "A", null);
66          assertTrue(map.containsValue(null));
67          // loop so we find a string that is in the same bucket as the null
68          for (int i = 'A'; i <= 'Z'; i++) {
69              final String str = String.valueOf((char) i);
70              assertFalse(map.containsValue(str), "String: " + str);
71          }
72      }
73  
74      // Bugzilla 37567
75      @Test
76      @SuppressWarnings("unchecked")
77      public void test_get_nullMatchesIncorrectly() {
78          final StaticBucketMap<K, V> map = new StaticBucketMap<>(17);
79          map.put(null, (V) "A");
80          assertEquals("A", map.get(null));
81          // loop so we find a string that is in the same bucket as the null
82          for (int i = 'A'; i <= 'Z'; i++) {
83              final String str = String.valueOf((char) i);
84              assertNull(map.get(str), "String: " + str);
85          }
86      }
87  
88  }