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