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.assertSame;
22  
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.commons.collections4.BulkTest;
29  import org.apache.commons.collections4.MapIterator;
30  import org.apache.commons.collections4.OrderedMap;
31  import org.apache.commons.collections4.ResettableIterator;
32  import org.apache.commons.collections4.list.AbstractListTest;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * JUnit tests.
37   */
38  public class LinkedMapTest<K, V> extends AbstractOrderedMapTest<K, V> {
39  
40      public class TestListView extends AbstractListTest<K> {
41  
42          TestListView() {
43              super("TestListView");
44          }
45  
46          @Override
47          public K[] getFullElements() {
48              return LinkedMapTest.this.getSampleKeys();
49          }
50  
51          @Override
52          public boolean isAddSupported() {
53              return false;
54          }
55  
56          @Override
57          public boolean isNullSupported() {
58              return LinkedMapTest.this.isAllowNullKey();
59          }
60          @Override
61          public boolean isRemoveSupported() {
62              return false;
63          }
64          @Override
65          public boolean isSetSupported() {
66              return false;
67          }
68          @Override
69          public boolean isTestSerialization() {
70              return false;
71          }
72          @Override
73          public List<K> makeFullCollection() {
74              return LinkedMapTest.this.makeFullMap().asList();
75          }
76          @Override
77          public List<K> makeObject() {
78              return LinkedMapTest.this.makeObject().asList();
79          }
80      }
81  
82      public LinkedMapTest() {
83          super(LinkedMapTest.class.getSimpleName());
84      }
85  
86      public BulkTest bulkTestListView() {
87          return new TestListView();
88      }
89  
90      @Override
91      public String getCompatibilityVersion() {
92          return "4";
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public LinkedMap<K, V> getMap() {
100         return (LinkedMap<K, V>) super.getMap();
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public LinkedMap<K, V> makeFullMap() {
108         return (LinkedMap<K, V>) super.makeFullMap();
109     }
110 
111     @Override
112     public LinkedMap<K, V> makeObject() {
113         return new LinkedMap<>();
114     }
115 
116     @Test
117     @SuppressWarnings("unchecked")
118     public void testClone() {
119         final LinkedMap<K, V> map = new LinkedMap<>(10);
120         map.put((K) "1", (V) "1");
121         final Map<K, V> cloned = map.clone();
122         assertEquals(map.size(), cloned.size());
123         assertSame(map.get("1"), cloned.get("1"));
124     }
125 
126     @Test
127     public void testGetByIndex() {
128         resetEmpty();
129         LinkedMap<K, V> lm = getMap();
130         try {
131             lm.get(0);
132         } catch (final IndexOutOfBoundsException ex) {}
133         try {
134             lm.get(-1);
135         } catch (final IndexOutOfBoundsException ex) {}
136 
137         resetFull();
138         lm = getMap();
139         try {
140             lm.get(-1);
141         } catch (final IndexOutOfBoundsException ex) {}
142         try {
143             lm.get(lm.size());
144         } catch (final IndexOutOfBoundsException ex) {}
145 
146         int i = 0;
147         for (final MapIterator<K, V> it = lm.mapIterator(); it.hasNext(); i++) {
148             assertSame(it.next(), lm.get(i));
149         }
150     }
151 
152     @Test
153     public void testGetValueByIndex() {
154         resetEmpty();
155         LinkedMap<K, V> lm = getMap();
156         try {
157             lm.getValue(0);
158         } catch (final IndexOutOfBoundsException ex) {}
159         try {
160             lm.getValue(-1);
161         } catch (final IndexOutOfBoundsException ex) {}
162 
163         resetFull();
164         lm = getMap();
165         try {
166             lm.getValue(-1);
167         } catch (final IndexOutOfBoundsException ex) {}
168         try {
169             lm.getValue(lm.size());
170         } catch (final IndexOutOfBoundsException ex) {}
171 
172         int i = 0;
173         for (final MapIterator<K, V> it = lm.mapIterator(); it.hasNext(); i++) {
174             it.next();
175             assertSame(it.getValue(), lm.getValue(i));
176         }
177     }
178 
179     @Test
180     public void testIndexOf() {
181         resetEmpty();
182         LinkedMap<K, V> lm = getMap();
183         assertEquals(-1, lm.indexOf(getOtherKeys()));
184 
185         resetFull();
186         lm = getMap();
187         final List<K> list = new ArrayList<>();
188         for (final MapIterator<K, V> it = lm.mapIterator(); it.hasNext();) {
189             list.add(it.next());
190         }
191         for (int i = 0; i < list.size(); i++) {
192             assertEquals(i, lm.indexOf(list.get(i)));
193         }
194     }
195 
196     /**
197      * Test for <a href="https://issues.apache.org/jira/browse/COLLECTIONS-323">COLLECTIONS-323</a>.
198      */
199     @Test
200     public void testInitialCapacityZero() {
201         final LinkedMap<String, String> map = new LinkedMap<>(0);
202         assertEquals(1, map.data.length);
203     }
204 
205     @Test
206     public void testInsertionOrder() {
207         if (!isPutAddSupported() || !isPutChangeSupported()) {
208             return;
209         }
210         final K[] keys = getSampleKeys();
211         final V[] values = getSampleValues();
212         Iterator<K> keyIter;
213         Iterator<V> valueIter;
214 
215         resetEmpty();
216         map.put(keys[0], values[0]);
217         map.put(keys[1], values[1]);
218         keyIter = map.keySet().iterator();
219         assertSame(keys[0], keyIter.next());
220         assertSame(keys[1], keyIter.next());
221         valueIter = map.values().iterator();
222         assertSame(values[0], valueIter.next());
223         assertSame(values[1], valueIter.next());
224 
225         // no change to order
226         map.put(keys[1], values[1]);
227         keyIter = map.keySet().iterator();
228         assertSame(keys[0], keyIter.next());
229         assertSame(keys[1], keyIter.next());
230         valueIter = map.values().iterator();
231         assertSame(values[0], valueIter.next());
232         assertSame(values[1], valueIter.next());
233 
234         // no change to order
235         map.put(keys[1], values[2]);
236         keyIter = map.keySet().iterator();
237         assertSame(keys[0], keyIter.next());
238         assertSame(keys[1], keyIter.next());
239         valueIter = map.values().iterator();
240         assertSame(values[0], valueIter.next());
241         assertSame(values[2], valueIter.next());
242 
243         // no change to order
244         map.put(keys[0], values[3]);
245         keyIter = map.keySet().iterator();
246         assertSame(keys[0], keyIter.next());
247         assertSame(keys[1], keyIter.next());
248         valueIter = map.values().iterator();
249         assertSame(values[3], valueIter.next());
250         assertSame(values[2], valueIter.next());
251     }
252 
253 //    public void testCreate() throws Exception {
254 //        resetEmpty();
255 //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/LinkedMap.emptyCollection.version4.obj");
256 //        resetFull();
257 //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/LinkedMap.fullCollection.version4.obj");
258 //    }
259 
260     @Test
261     public void testRemoveByIndex() {
262         resetEmpty();
263         LinkedMap<K, V> lm = getMap();
264         try {
265             lm.remove(0);
266         } catch (final IndexOutOfBoundsException ex) {}
267         try {
268             lm.remove(-1);
269         } catch (final IndexOutOfBoundsException ex) {}
270 
271         resetFull();
272         lm = getMap();
273         try {
274             lm.remove(-1);
275         } catch (final IndexOutOfBoundsException ex) {}
276         try {
277             lm.remove(lm.size());
278         } catch (final IndexOutOfBoundsException ex) {}
279 
280         final List<K> list = new ArrayList<>();
281         for (final MapIterator<K, V> it = lm.mapIterator(); it.hasNext();) {
282             list.add(it.next());
283         }
284         for (int i = 0; i < list.size(); i++) {
285             final Object key = list.get(i);
286             final Object value = lm.get(key);
287             assertEquals(value, lm.remove(i));
288             list.remove(i);
289             assertFalse(lm.containsKey(key));
290         }
291     }
292 
293     @Test
294     @SuppressWarnings("unchecked")
295     public void testReset() {
296         resetEmpty();
297         OrderedMap<K, V> ordered = getMap();
298         ((ResettableIterator<K>) ordered.mapIterator()).reset();
299 
300         resetFull();
301         ordered = getMap();
302         final List<K> list = new ArrayList<>(ordered.keySet());
303         final ResettableIterator<K> it = (ResettableIterator<K>) ordered.mapIterator();
304         assertSame(list.get(0), it.next());
305         assertSame(list.get(1), it.next());
306         it.reset();
307         assertSame(list.get(0), it.next());
308     }
309 }