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.Collection;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Extension of {@link AbstractMapTest} for exercising the {@link CompositeMap} implementation.
33   *
34   * @param <K> the key type.
35   * @param <V> the value type.
36   */
37  public class CompositeMapTest<K, V> extends AbstractIterableMapTest<K, V> {
38  
39      /** Used as a flag in MapMutator tests */
40      private boolean pass;
41  
42      @SuppressWarnings("unchecked")
43      private Map<K, V> buildOne() {
44          final HashMap<K, V> map = new HashMap<>();
45          map.put((K) "1", (V) "one");
46          map.put((K) "2", (V) "two");
47          return map;
48      }
49  
50      @SuppressWarnings("unchecked")
51      public Map<K, V> buildTwo() {
52          final HashMap<K, V> map = new HashMap<>();
53          map.put((K) "3", (V) "three");
54          map.put((K) "4", (V) "four");
55          return map;
56      }
57  
58      @Override
59      public String getCompatibilityVersion() {
60          return "4";
61      }
62  
63      @Override
64      public CompositeMap<K, V> makeObject() {
65          final CompositeMap<K, V> map = new CompositeMap<>();
66          map.addComposited(new HashMap<>());
67          map.setMutator(new EmptyMapMutator<>());
68          return map;
69      }
70  
71      @BeforeEach
72      public void setUp() throws Exception {
73          pass = false;
74      }
75  
76      @Test
77      @SuppressWarnings("unchecked")
78      public void testAddComposited() {
79          final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo());
80          final HashMap<K, V> three = new HashMap<>();
81          three.put((K) "5", (V) "five");
82          map.addComposited(null);
83          map.addComposited(three);
84          assertTrue(map.containsKey("5"));
85  
86          assertThrows(IllegalArgumentException.class, () -> map.addComposited(three));
87      }
88  
89      @Test
90      public void testGet() {
91          final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo());
92          assertEquals("one", map.get("1"));
93          assertEquals("four", map.get("4"));
94      }
95  
96      @Test
97      @SuppressWarnings("unchecked")
98      public void testPut() {
99          final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo(), new CompositeMap.MapMutator<K, V>() {
100             private static final long serialVersionUID = 1L;
101 
102             @Override
103             public V put(final CompositeMap<K, V> map, final Map<K, V>[] composited, final K key, final V value) {
104                 pass = true;
105                 return (V) "foo";
106             }
107 
108             @Override
109             public void putAll(final CompositeMap<K, V> map, final Map<K, V>[] composited, final Map<? extends K, ? extends V> t) {
110                 throw new UnsupportedOperationException();
111             }
112 
113             @Override
114             public void resolveCollision(final CompositeMap<K, V> composite, final Map<K, V> existing, final Map<K, V> added, final Collection<K> intersect) {
115                 throw new UnsupportedOperationException();
116             }
117         });
118 
119         map.put((K) "willy", (V) "wonka");
120         assertTrue(pass);
121     }
122 
123     @Test
124     public void testPutAll() {
125         final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo(), new CompositeMap.MapMutator<K, V>() {
126             private static final long serialVersionUID = 1L;
127 
128             @Override
129             public V put(final CompositeMap<K, V> map, final Map<K, V>[] composited, final K key, final V value) {
130                 throw new UnsupportedOperationException();
131             }
132 
133             @Override
134             public void putAll(final CompositeMap<K, V> map, final Map<K, V>[] composited, final Map<? extends K, ? extends V> t) {
135                 pass = true;
136             }
137 
138             @Override
139             public void resolveCollision(final CompositeMap<K, V> composite, final Map<K, V> existing, final Map<K, V> added, final Collection<K> intersect) {
140                 throw new UnsupportedOperationException();
141             }
142         });
143 
144         map.putAll(null);
145         assertTrue(pass);
146     }
147 
148     @Test
149     @SuppressWarnings("unchecked")
150     public void testRemoveComposited() {
151         final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo());
152         final HashMap<K, V> three = new HashMap<>();
153         three.put((K) "5", (V) "five");
154         map.addComposited(null);
155         map.addComposited(three);
156         assertTrue(map.containsKey("5"));
157 
158         map.removeComposited(three);
159         assertFalse(map.containsKey("5"));
160 
161         map.removeComposited(buildOne());
162         assertFalse(map.containsKey("2"));
163 
164     }
165 
166     @Test
167     @SuppressWarnings("unchecked")
168     public void testRemoveFromComposited() {
169         final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo());
170         final HashMap<K, V> three = new HashMap<>();
171         three.put((K) "5", (V) "five");
172         map.addComposited(null);
173         map.addComposited(three);
174         assertTrue(map.containsKey("5"));
175 
176         // Now remove "5"
177         map.remove("5");
178         assertFalse(three.containsKey("5"));
179     }
180 
181     @Test
182     @SuppressWarnings("unchecked")
183     public void testRemoveFromUnderlying() {
184         final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo());
185         final HashMap<K, V> three = new HashMap<>();
186         three.put((K) "5", (V) "five");
187         map.addComposited(null);
188         map.addComposited(three);
189         assertTrue(map.containsKey("5"));
190 
191         // Now remove "5"
192         three.remove("5");
193         assertFalse(map.containsKey("5"));
194     }
195 
196     @Test
197     public void testResolveCollision() {
198         final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo(), new CompositeMap.MapMutator<K, V>() {
199             private static final long serialVersionUID = 1L;
200 
201             @Override
202             public V put(final CompositeMap<K, V> map, final Map<K, V>[] composited, final K key, final V value) {
203                 throw new UnsupportedOperationException();
204             }
205 
206             @Override
207             public void putAll(final CompositeMap<K, V> map, final Map<K, V>[] composited, final Map<? extends K, ? extends V> t) {
208                 throw new UnsupportedOperationException();
209             }
210 
211             @Override
212             public void resolveCollision(final CompositeMap<K, V> composite, final Map<K, V> existing, final Map<K, V> added, final Collection<K> intersect) {
213                 pass = true;
214             }
215         });
216 
217         map.addComposited(buildOne());
218         assertTrue(pass);
219     }
220 
221 //    public void testCreate() throws Exception {
222 //        resetEmpty();
223 //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/CompositeMap.emptyCollection.version4.obj");
224 //        resetFull();
225 //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/CompositeMap.fullCollection.version4.obj");
226 //    }
227 
228 }