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.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.commons.collections4.Factory;
28  import org.apache.commons.collections4.FactoryUtils;
29  import org.apache.commons.collections4.IterableMap;
30  import org.apache.commons.collections4.Transformer;
31  import org.apache.commons.collections4.TransformerUtils;
32  import org.apache.commons.collections4.functors.ConstantFactory;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Extension of {@link AbstractMapTest} for exercising the
37   * {@link DefaultedMap} implementation.
38   *
39   * @param <K> the key type.
40   * @param <V> the value type.
41   */
42  public class DefaultedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
43  
44      protected final Factory<V> nullFactory = FactoryUtils.<V>nullFactory();
45      protected final Transformer<K, V> nullTransformer = TransformerUtils.<K, V>nullTransformer();
46  
47      @Override
48      public String getCompatibilityVersion() {
49          return "4";
50      }
51  
52      @Override
53      public IterableMap<K, V> makeObject() {
54          return DefaultedMap.defaultedMap(new HashMap<K, V>(), nullFactory);
55      }
56  
57      @Test
58      public void testFactoryMethods() {
59          final HashMap<K, V> base = new HashMap<>();
60          assertThrows(NullPointerException.class, () -> DefaultedMap.defaultedMap(null, (V) "DEFAULT_VALUE"));
61          assertThrows(NullPointerException.class, () -> DefaultedMap.defaultedMap((Map<K, V>) null, nullFactory));
62          assertThrows(NullPointerException.class, () -> DefaultedMap.defaultedMap(base, (Factory<V>) null));
63          assertThrows(NullPointerException.class, () -> DefaultedMap.defaultedMap((Map<K, V>) null, nullTransformer));
64          assertThrows(NullPointerException.class, () -> DefaultedMap.defaultedMap(base, (Transformer<K, V>) null));
65      }
66  
67      @Test
68      @Override
69      @SuppressWarnings("unchecked")
70      public void testMapGet() {
71          final Map<K, V> map = new DefaultedMap<>((V) "NULL");
72  
73          assertEquals(0, map.size());
74          assertFalse(map.containsKey("NotInMap"));
75          assertEquals("NULL", map.get("NotInMap"));
76  
77          map.put((K) "Key", (V) "Value");
78          assertEquals(1, map.size());
79          assertTrue(map.containsKey("Key"));
80          assertEquals("Value", map.get("Key"));
81          assertFalse(map.containsKey("NotInMap"));
82          assertEquals("NULL", map.get("NotInMap"));
83      }
84  
85      @Test
86      @SuppressWarnings("unchecked")
87      public void testMapGet2() {
88          final HashMap<K, V> base = new HashMap<>();
89          final Map<K, V> map = DefaultedMap.defaultedMap(base, (V) "NULL");
90  
91          assertEquals(0, map.size());
92          assertEquals(0, base.size());
93          assertFalse(map.containsKey("NotInMap"));
94          assertEquals("NULL", map.get("NotInMap"));
95  
96          map.put((K) "Key", (V) "Value");
97          assertEquals(1, map.size());
98          assertEquals(1, base.size());
99          assertTrue(map.containsKey("Key"));
100         assertEquals("Value", map.get("Key"));
101         assertFalse(map.containsKey("NotInMap"));
102         assertEquals("NULL", map.get("NotInMap"));
103     }
104 
105     @Test
106     @SuppressWarnings("unchecked")
107     public void testMapGet3() {
108         final HashMap<K, V> base = new HashMap<>();
109         final Map<K, V> map = DefaultedMap.defaultedMap(base, ConstantFactory.constantFactory((V) "NULL"));
110 
111         assertEquals(0, map.size());
112         assertEquals(0, base.size());
113         assertFalse(map.containsKey("NotInMap"));
114         assertEquals("NULL", map.get("NotInMap"));
115 
116         map.put((K) "Key", (V) "Value");
117         assertEquals(1, map.size());
118         assertEquals(1, base.size());
119         assertTrue(map.containsKey("Key"));
120         assertEquals("Value", map.get("Key"));
121         assertFalse(map.containsKey("NotInMap"));
122         assertEquals("NULL", map.get("NotInMap"));
123     }
124 
125     @Test
126     @SuppressWarnings("unchecked")
127     public void testMapGet4() {
128         final HashMap<K, V> base = new HashMap<>();
129         final Map<K, V> map = DefaultedMap.defaultedMap(base, (Transformer<K, V>) input -> {
130             if (input instanceof String) {
131                 return (V) "NULL";
132             }
133             return (V) "NULL_OBJECT";
134         });
135 
136         assertEquals(0, map.size());
137         assertEquals(0, base.size());
138         assertFalse(map.containsKey("NotInMap"));
139         assertEquals("NULL", map.get("NotInMap"));
140         assertEquals("NULL_OBJECT", map.get(Integer.valueOf(0)));
141 
142         map.put((K) "Key", (V) "Value");
143         assertEquals(1, map.size());
144         assertEquals(1, base.size());
145         assertTrue(map.containsKey("Key"));
146         assertEquals("Value", map.get("Key"));
147         assertFalse(map.containsKey("NotInMap"));
148         assertEquals("NULL", map.get("NotInMap"));
149         assertEquals("NULL_OBJECT", map.get(Integer.valueOf(0)));
150     }
151 
152 //    public void testCreate() throws Exception {
153 //        resetEmpty();
154 //        writeExternalFormToDisk(
155 //            (java.io.Serializable) map,
156 //            "src/test/resources/data/test/DefaultedMap.emptyCollection.version4.obj");
157 //        resetFull();
158 //        writeExternalFormToDisk(
159 //            (java.io.Serializable) map,
160 //            "src/test/resources/data/test/DefaultedMap.fullCollection.version4.obj");
161 //    }
162 
163 }