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.List;
26  
27  import org.apache.commons.collections4.BulkTest;
28  import org.apache.commons.collections4.MapIterator;
29  import org.apache.commons.collections4.list.AbstractListTest;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Extension of {@link AbstractOrderedMapTest} for exercising the {@link ListOrderedMap} implementation.
34   *
35   * @param <K> the key type.
36   * @param <V> the value type.
37   */
38  public class ListOrderedMap2Test<K, V> extends AbstractOrderedMapTest<K, V> {
39  
40      public class TestListView extends AbstractListTest<K> {
41  
42          @Override
43          public K[] getFullElements() {
44              return getSampleKeys();
45          }
46  
47          @Override
48          public boolean isAddSupported() {
49              return false;
50          }
51  
52          @Override
53          public boolean isNullSupported() {
54              return isAllowNullKey();
55          }
56  
57          @Override
58          public boolean isRemoveSupported() {
59              return false;
60          }
61  
62          @Override
63          public boolean isSetSupported() {
64              return false;
65          }
66  
67          @Override
68          public boolean isTestSerialization() {
69              return false;
70          }
71  
72          @Override
73          public List<K> makeFullCollection() {
74              return ListOrderedMap2Test.this.makeFullMap().asList();
75          }
76  
77          @Override
78          public List<K> makeObject() {
79              return ListOrderedMap2Test.this.makeObject().asList();
80          }
81      }
82  
83      public BulkTest bulkTestListView() {
84          return new TestListView();
85      }
86  
87      @Override
88      public String getCompatibilityVersion() {
89          return "4";
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public ListOrderedMap<K, V> getMap() {
97          return (ListOrderedMap<K, V>) super.getMap();
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public ListOrderedMap<K, V> makeFullMap() {
105         return (ListOrderedMap<K, V>) super.makeFullMap();
106     }
107 
108     @Override
109     public ListOrderedMap<K, V> makeObject() {
110         return new ListOrderedMap<>();
111     }
112 
113     @Test
114     public void testGetByIndex() {
115         resetEmpty();
116         assertThrows(IndexOutOfBoundsException.class, () -> getMap().get(0));
117         assertThrows(IndexOutOfBoundsException.class, () -> getMap().get(-1));
118         resetFull();
119         final ListOrderedMap<K, V> lom = getMap();
120         assertThrows(IndexOutOfBoundsException.class, () -> lom.get(-1));
121         assertThrows(IndexOutOfBoundsException.class, () -> lom.get(lom.size()));
122         int i = 0;
123         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext(); i++) {
124             assertSame(it.next(), lom.get(i));
125         }
126     }
127 
128     @Test
129     public void testGetValueByIndex() {
130         resetEmpty();
131         assertThrows(IndexOutOfBoundsException.class, () -> getMap().getValue(0));
132         assertThrows(IndexOutOfBoundsException.class, () -> getMap().getValue(-1));
133         resetFull();
134         final ListOrderedMap<K, V> lom = getMap();
135         assertThrows(IndexOutOfBoundsException.class, () -> lom.getValue(-1));
136         assertThrows(IndexOutOfBoundsException.class, () -> lom.getValue(lom.size()));
137         int i = 0;
138         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext(); i++) {
139             it.next();
140             assertSame(it.getValue(), lom.getValue(i));
141         }
142     }
143 
144     @Test
145     public void testIndexOf() {
146         resetEmpty();
147         ListOrderedMap<K, V> lom = getMap();
148         assertEquals(-1, lom.indexOf(getOtherKeys()));
149 
150         resetFull();
151         lom = getMap();
152         final List<K> list = new ArrayList<>();
153         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext();) {
154             list.add(it.next());
155         }
156         for (int i = 0; i < list.size(); i++) {
157             assertEquals(i, lom.indexOf(list.get(i)));
158         }
159     }
160 
161 //    public void testCreate() throws Exception {
162 //        resetEmpty();
163 //        writeExternalFormToDisk(
164 //            (java.io.Serializable) map,
165 //            "D:/dev/collections/data/test/ListOrderedMap.emptyCollection.version3.1.obj");
166 //        resetFull();
167 //        writeExternalFormToDisk(
168 //            (java.io.Serializable) map,
169 //            "D:/dev/collections/data/test/ListOrderedMap.fullCollection.version3.1.obj");
170 //    }
171 
172     @Test
173     public void testRemoveByIndex() {
174         resetEmpty();
175         assertThrows(IndexOutOfBoundsException.class, () -> getMap().remove(0));
176         assertThrows(IndexOutOfBoundsException.class, () -> getMap().remove(-1));
177         resetFull();
178         final ListOrderedMap<K, V> lom = getMap();
179         assertThrows(IndexOutOfBoundsException.class, () -> lom.remove(-1));
180         assertThrows(IndexOutOfBoundsException.class, () -> lom.remove(lom.size()));
181         final List<K> list = new ArrayList<>();
182         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext();) {
183             list.add(it.next());
184         }
185         for (int i = 0; i < list.size(); i++) {
186             final Object key = list.get(i);
187             final Object value = lom.get(key);
188             assertEquals(value, lom.remove(i));
189             list.remove(i);
190             assertFalse(lom.containsKey(key));
191         }
192     }
193 }