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  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.TreeMap;
29  
30  import org.apache.commons.collections4.BulkTest;
31  import org.apache.commons.collections4.MapIterator;
32  import org.apache.commons.collections4.list.AbstractListTest;
33  import org.apache.commons.lang3.StringUtils;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Extension of {@link AbstractOrderedMapTest} for exercising the {@link ListOrderedMap} implementation.
38   *
39   * @param <K> the key type.
40   * @param <V> the value type.
41   */
42  public class ListOrderedMapTest<K, V> extends AbstractOrderedMapTest<K, V> {
43  
44      public class TestKeyListView extends AbstractListTest<K> {
45  
46          @Override
47          public K[] getFullElements() {
48              return getSampleKeys();
49          }
50  
51          @Override
52          public boolean isAddSupported() {
53              return false;
54          }
55  
56          @Override
57          public boolean isNullSupported() {
58              return isAllowNullKey();
59          }
60  
61          @Override
62          public boolean isRemoveSupported() {
63              return false;
64          }
65  
66          @Override
67          public boolean isSetSupported() {
68              return false;
69          }
70  
71          @Override
72          public boolean isTestSerialization() {
73              return false;
74          }
75  
76          @Override
77          public List<K> makeFullCollection() {
78              return ListOrderedMapTest.this.makeFullMap().keyList();
79          }
80  
81          @Override
82          public List<K> makeObject() {
83              return ListOrderedMapTest.this.makeObject().keyList();
84          }
85  
86      }
87  
88      public class TestValueListView extends AbstractListTest<V> {
89  
90          @Override
91          public V[] getFullElements() {
92              return getSampleValues();
93          }
94  
95          @Override
96          public boolean isAddSupported() {
97              return false;
98          }
99  
100         @Override
101         public boolean isNullSupported() {
102             return isAllowNullKey();
103         }
104 
105         @Override
106         public boolean isRemoveSupported() {
107             return true;
108         }
109 
110         @Override
111         public boolean isSetSupported() {
112             return true;
113         }
114 
115         @Override
116         public boolean isTestSerialization() {
117             return false;
118         }
119 
120         @Override
121         public List<V> makeFullCollection() {
122             return ListOrderedMapTest.this.makeFullMap().valueList();
123         }
124 
125         @Override
126         public List<V> makeObject() {
127             return ListOrderedMapTest.this.makeObject().valueList();
128         }
129 
130     }
131 
132     public BulkTest bulkTestKeyListView() {
133         return new TestKeyListView();
134     }
135 
136     public BulkTest bulkTestValueListView() {
137         return new TestValueListView();
138     }
139 
140     @Override
141     public String getCompatibilityVersion() {
142         return "4";
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public ListOrderedMap<K, V> getMap() {
150         return (ListOrderedMap<K, V>) super.getMap();
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public ListOrderedMap<K, V> makeFullMap() {
158         return (ListOrderedMap<K, V>) super.makeFullMap();
159     }
160 
161     @Override
162     public ListOrderedMap<K, V> makeObject() {
163         return ListOrderedMap.listOrderedMap(new HashMap<>());
164     }
165 
166     @Test
167     public void testCOLLECTIONS_474_nonNullValues() {
168         final Object key1 = new Object();
169         final Object key2 = new Object();
170         final HashMap<Object, Object> hmap = new HashMap<>();
171         hmap.put(key1, "1");
172         hmap.put(key2, "2");
173         assertEquals(2, hmap.size(), "Should have two elements");
174         final ListOrderedMap<Object, Object> listMap = new ListOrderedMap<>();
175         listMap.put(key1, "3");
176         listMap.put(key2, "4");
177         assertEquals(2, listMap.size(), "Should have two elements");
178         listMap.putAll(2, hmap);
179     }
180 
181     @Test
182     public void testCOLLECTIONS_474_nullValues() {
183         final Object key1 = new Object();
184         final Object key2 = new Object();
185         final HashMap<Object, Object> hmap = new HashMap<>();
186         hmap.put(key1, null);
187         hmap.put(key2, null);
188         assertEquals(2, hmap.size(), "Should have two elements");
189         final ListOrderedMap<Object, Object> listMap = new ListOrderedMap<>();
190         listMap.put(key1, null);
191         listMap.put(key2, null);
192         assertEquals(2, listMap.size(), "Should have two elements");
193         listMap.putAll(2, hmap);
194     }
195 
196     @Test
197     public void testGetByIndex() {
198         resetEmpty();
199         assertThrows(IndexOutOfBoundsException.class, () -> getMap().get(0));
200         assertThrows(IndexOutOfBoundsException.class, () -> getMap().get(-1));
201         resetFull();
202         final ListOrderedMap<K, V> lom = getMap();
203         assertThrows(IndexOutOfBoundsException.class, () -> getMap().get(-1));
204         assertThrows(IndexOutOfBoundsException.class, () -> lom.get(lom.size()));
205         int i = 0;
206         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext(); i++) {
207             assertSame(it.next(), lom.get(i));
208         }
209     }
210 
211     @Test
212     public void testGetValueByIndex() {
213         resetEmpty();
214         assertThrows(IndexOutOfBoundsException.class, () -> getMap().getValue(0));
215         assertThrows(IndexOutOfBoundsException.class, () -> getMap().getValue(-1));
216         resetFull();
217         final ListOrderedMap<K, V> lom = getMap();
218         assertThrows(IndexOutOfBoundsException.class, () -> getMap().getValue(-1));
219         assertThrows(IndexOutOfBoundsException.class, () -> lom.getValue(lom.size()));
220         int i = 0;
221         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext(); i++) {
222             it.next();
223             assertSame(it.getValue(), lom.getValue(i));
224         }
225     }
226 
227     @Test
228     public void testIndexOf() {
229         resetEmpty();
230         ListOrderedMap<K, V> lom = getMap();
231         assertEquals(-1, lom.indexOf(getOtherKeys()));
232         resetFull();
233         lom = getMap();
234         final List<K> list = new ArrayList<>();
235         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext();) {
236             list.add(it.next());
237         }
238         for (int i = 0; i < list.size(); i++) {
239             assertEquals(i, lom.indexOf(list.get(i)));
240         }
241     }
242 
243     @Test
244     @SuppressWarnings("unchecked")
245     public void testPut_intObjectObject() {
246         resetEmpty();
247         ListOrderedMap<K, V> lom = getMap();
248 
249         final ListOrderedMap<K, V> finalLom = lom;
250 
251         assertThrows(IndexOutOfBoundsException.class, () -> finalLom.put(1, (K) "testInsert1", (V) "testInsert1v"),
252                 "should not be able to insert at pos 1 in empty Map");
253         assertThrows(IndexOutOfBoundsException.class, () -> finalLom.put(-1, (K) "testInsert-1", (V) "testInsert-1v"),
254                 "should not be able to insert at pos -1 in empty Map");
255         // put where key doesn't exist
256         lom.put(0, (K) "testInsert1", (V) "testInsert1v");
257         assertEquals("testInsert1v", lom.getValue(0));
258 
259         lom.put((K) "testInsertPut", (V) "testInsertPutv");
260         assertEquals("testInsert1v", lom.getValue(0));
261         assertEquals("testInsertPutv", lom.getValue(1));
262 
263         lom.put(0, (K) "testInsert0", (V) "testInsert0v");
264         assertEquals("testInsert0v", lom.getValue(0));
265         assertEquals("testInsert1v", lom.getValue(1));
266         assertEquals("testInsertPutv", lom.getValue(2));
267 
268         lom.put(3, (K) "testInsert3", (V) "testInsert3v");
269         assertEquals("testInsert0v", lom.getValue(0));
270         assertEquals("testInsert1v", lom.getValue(1));
271         assertEquals("testInsertPutv", lom.getValue(2));
272         assertEquals("testInsert3v", lom.getValue(3));
273 
274         // put in a full map
275         resetFull();
276         lom = getMap();
277         final ListOrderedMap<K, V> lom2 = new ListOrderedMap<>();
278         lom2.putAll(lom);
279 
280         lom2.put(0, (K) "testInsert0", (V) "testInsert0v");
281         assertEquals("testInsert0v", lom2.getValue(0));
282         for (int i = 0; i < lom.size(); i++) {
283             assertEquals(lom2.getValue(i + 1), lom.getValue(i));
284         }
285 
286         // put where key does exist
287         final Integer i1 = Integer.valueOf(1);
288         final Integer i1b = Integer.valueOf(1);
289         final Integer i2 = Integer.valueOf(2);
290         final Integer i3 = Integer.valueOf(3);
291 
292         resetEmpty();
293         lom = getMap();
294         lom.put((K) i1, (V) "1");
295         lom.put((K) i2, (V) "2");
296         lom.put((K) i3, (V) "3");
297         lom.put(0, (K) i1, (V) "One");
298         assertEquals(3, lom.size());
299         assertEquals(3, lom.map.size());
300         assertEquals(3, lom.keyList().size());
301         assertEquals("One", lom.getValue(0));
302         assertSame(i1, lom.get(0));
303 
304         resetEmpty();
305         lom = getMap();
306         lom.put((K) i1, (V) "1");
307         lom.put((K) i2, (V) "2");
308         lom.put((K) i3, (V) "3");
309         lom.put(0, (K) i1b, (V) "One");
310         assertEquals(3, lom.size());
311         assertEquals(3, lom.map.size());
312         assertEquals(3, lom.keyList().size());
313         assertEquals("One", lom.getValue(0));
314         assertEquals("2", lom.getValue(1));
315         assertEquals("3", lom.getValue(2));
316         assertSame(i1b, lom.get(0));
317 
318         resetEmpty();
319         lom = getMap();
320         lom.put((K) i1, (V) "1");
321         lom.put((K) i2, (V) "2");
322         lom.put((K) i3, (V) "3");
323         lom.put(1, (K) i1b, (V) "One");
324         assertEquals(3, lom.size());
325         assertEquals(3, lom.map.size());
326         assertEquals(3, lom.keyList().size());
327         assertEquals("One", lom.getValue(0));
328         assertEquals("2", lom.getValue(1));
329         assertEquals("3", lom.getValue(2));
330 
331         resetEmpty();
332         lom = getMap();
333         lom.put((K) i1, (V) "1");
334         lom.put((K) i2, (V) "2");
335         lom.put((K) i3, (V) "3");
336         lom.put(2, (K) i1b, (V) "One");
337         assertEquals(3, lom.size());
338         assertEquals(3, lom.map.size());
339         assertEquals(3, lom.keyList().size());
340         assertEquals("2", lom.getValue(0));
341         assertEquals("One", lom.getValue(1));
342         assertEquals("3", lom.getValue(2));
343 
344         resetEmpty();
345         lom = getMap();
346         lom.put((K) i1, (V) "1");
347         lom.put((K) i2, (V) "2");
348         lom.put((K) i3, (V) "3");
349         lom.put(3, (K) i1b, (V) "One");
350         assertEquals(3, lom.size());
351         assertEquals(3, lom.map.size());
352         assertEquals(3, lom.keyList().size());
353         assertEquals("2", lom.getValue(0));
354         assertEquals("3", lom.getValue(1));
355         assertEquals("One", lom.getValue(2));
356     }
357 
358     @Test
359     public void testPutAllWithIndex() {
360         resetEmpty();
361         @SuppressWarnings("unchecked")
362         final ListOrderedMap<String, String> lom = (ListOrderedMap<String, String>) map;
363 
364         // Create Initial Data
365         lom.put("testInsert0", "testInsert0v");
366         lom.put("testInsert1", "testInsert1v");
367         lom.put("testInsert2", "testInsert2v");
368         assertEquals("testInsert0v", lom.getValue(0));
369         assertEquals("testInsert1v", lom.getValue(1));
370         assertEquals("testInsert2v", lom.getValue(2));
371 
372         // Create New Test Map and Add using putAll(int, Object, Object)
373         final Map<String, String> values = new ListOrderedMap<>();
374         values.put("NewInsert0", "NewInsert0v");
375         values.put("NewInsert1", "NewInsert1v");
376         lom.putAll(1, values);
377 
378         // Perform Asserts
379         assertEquals("testInsert0v", lom.getValue(0));
380         assertEquals("NewInsert0v", lom.getValue(1));
381         assertEquals("NewInsert1v", lom.getValue(2));
382         assertEquals("testInsert1v", lom.getValue(3));
383         assertEquals("testInsert2v", lom.getValue(4));
384     }
385 
386     @Test
387     @SuppressWarnings("unchecked")
388     public void testPutAllWithIndexBug441() {
389         // see COLLECTIONS-441
390         resetEmpty();
391         final ListOrderedMap<K, V> lom = getMap();
392 
393         final int size = 5;
394         for (int i = 0; i < size; i++) {
395             lom.put((K) Integer.valueOf(i), (V) Boolean.TRUE);
396         }
397 
398         final Map<K, V> map = new TreeMap<>();
399         for (int i = 0; i < size; i++) {
400             map.put((K) Integer.valueOf(i), (V) Boolean.FALSE);
401         }
402 
403         lom.putAll(3, map);
404 
405         final List<K> orderedList = lom.asList();
406         for (int i = 0; i < size; i++) {
407             assertEquals(Integer.valueOf(i), orderedList.get(i));
408         }
409     }
410 
411     @Test
412     public void testRemoveByIndex() {
413         resetEmpty();
414         assertThrows(IndexOutOfBoundsException.class, () -> getMap().remove(0));
415         assertThrows(IndexOutOfBoundsException.class, () -> getMap().remove(-1));
416         resetFull();
417         final ListOrderedMap<K, V> lom = getMap();
418         assertThrows(IndexOutOfBoundsException.class, () -> getMap().remove(-1));
419         assertThrows(IndexOutOfBoundsException.class, () -> lom.remove(lom.size()));
420         final List<K> list = new ArrayList<>();
421         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext();) {
422             list.add(it.next());
423         }
424         for (int i = 0; i < list.size(); i++) {
425             final Object key = list.get(i);
426             final Object value = lom.get(key);
427             assertEquals(value, lom.remove(i));
428             list.remove(i);
429             assertFalse(lom.containsKey(key));
430         }
431     }
432 
433     @Test
434     @SuppressWarnings("unchecked")
435     public void testSetValueByIndex() {
436         resetEmpty();
437         assertThrows(IndexOutOfBoundsException.class, () -> getMap().setValue(0, (V) StringUtils.EMPTY));
438         assertThrows(IndexOutOfBoundsException.class, () -> getMap().setValue(-1, (V) StringUtils.EMPTY));
439         resetFull();
440         final ListOrderedMap<K, V> lom = getMap();
441         assertThrows(IndexOutOfBoundsException.class, () -> lom.setValue(-1, (V) StringUtils.EMPTY));
442         assertThrows(IndexOutOfBoundsException.class, () -> lom.setValue(lom.size(), (V) StringUtils.EMPTY));
443         for (int i = 0; i < lom.size(); i++) {
444             final V value = lom.getValue(i);
445             final Object input = Integer.valueOf(i);
446             assertEquals(value, lom.setValue(i, (V) input));
447             assertEquals(input, lom.getValue(i));
448         }
449     }
450 
451     @Test
452     public void testValueList_getByIndex() {
453         resetFull();
454         final ListOrderedMap<K, V> lom = getMap();
455         for (int i = 0; i < lom.size(); i++) {
456             final V expected = lom.getValue(i);
457             assertEquals(expected, lom.valueList().get(i));
458         }
459     }
460 
461     @Test
462     public void testValueList_removeByIndex() {
463         resetFull();
464         final ListOrderedMap<K, V> lom = getMap();
465         while (lom.size() > 1) {
466             final V expected = lom.getValue(1);
467             assertEquals(expected, lom.valueList().remove(1));
468         }
469     }
470 
471 //    public void testCreate() throws Exception {
472 //        resetEmpty();
473 //        writeExternalFormToDisk(
474 //            (java.io.Serializable) map,
475 //            "src/test/resources/data/test/ListOrderedMap.emptyCollection.version4.obj");
476 //        resetFull();
477 //        writeExternalFormToDisk(
478 //            (java.io.Serializable) map,
479 //            "src/test/resources/data/test/ListOrderedMap.fullCollection.version4.obj");
480 //    }
481 
482     @Test
483     @SuppressWarnings("unchecked")
484     public void testValueList_setByIndex() {
485         resetFull();
486         final ListOrderedMap<K, V> lom = getMap();
487         for (int i = 0; i < lom.size(); i++) {
488             final Object input = Integer.valueOf(i);
489             final V expected = lom.getValue(i);
490             assertEquals(expected, lom.valueList().set(i, (V) input));
491             assertEquals(input, lom.getValue(i));
492             assertEquals(input, lom.valueList().get(i));
493         }
494     }
495 
496 }