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.List;
25  
26  import org.apache.commons.collections4.BulkTest;
27  import org.apache.commons.collections4.MapIterator;
28  import org.apache.commons.collections4.list.AbstractListTest;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Extension of {@link AbstractOrderedMapTest} for exercising the {@link ListOrderedMap}
33   * implementation.
34   */
35  public class ListOrderedMap2Test<K, V> extends AbstractOrderedMapTest<K, V> {
36  
37      public class TestListView extends AbstractListTest<K> {
38  
39          TestListView() {
40              super("TestListView");
41          }
42  
43          @Override
44          public K[] getFullElements() {
45              return ListOrderedMap2Test.this.getSampleKeys();
46          }
47  
48          @Override
49          public boolean isAddSupported() {
50              return false;
51          }
52  
53          @Override
54          public boolean isNullSupported() {
55              return ListOrderedMap2Test.this.isAllowNullKey();
56          }
57          @Override
58          public boolean isRemoveSupported() {
59              return false;
60          }
61          @Override
62          public boolean isSetSupported() {
63              return false;
64          }
65          @Override
66          public boolean isTestSerialization() {
67              return false;
68          }
69          @Override
70          public List<K> makeFullCollection() {
71              return ListOrderedMap2Test.this.makeFullMap().asList();
72          }
73          @Override
74          public List<K> makeObject() {
75              return ListOrderedMap2Test.this.makeObject().asList();
76          }
77      }
78  
79      public ListOrderedMap2Test() {
80          super(ListOrderedMap2Test.class.getSimpleName());
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         ListOrderedMap<K, V> lom = getMap();
117         try {
118             lom.get(0);
119         } catch (final IndexOutOfBoundsException ex) {}
120         try {
121             lom.get(-1);
122         } catch (final IndexOutOfBoundsException ex) {}
123 
124         resetFull();
125         lom = getMap();
126         try {
127             lom.get(-1);
128         } catch (final IndexOutOfBoundsException ex) {}
129         try {
130             lom.get(lom.size());
131         } catch (final IndexOutOfBoundsException ex) {}
132 
133         int i = 0;
134         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext(); i++) {
135             assertSame(it.next(), lom.get(i));
136         }
137     }
138 
139     @Test
140     public void testGetValueByIndex() {
141         resetEmpty();
142         ListOrderedMap<K, V> lom = getMap();
143         try {
144             lom.getValue(0);
145         } catch (final IndexOutOfBoundsException ex) {}
146         try {
147             lom.getValue(-1);
148         } catch (final IndexOutOfBoundsException ex) {}
149 
150         resetFull();
151         lom = getMap();
152         try {
153             lom.getValue(-1);
154         } catch (final IndexOutOfBoundsException ex) {}
155         try {
156             lom.getValue(lom.size());
157         } catch (final IndexOutOfBoundsException ex) {}
158 
159         int i = 0;
160         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext(); i++) {
161             it.next();
162             assertSame(it.getValue(), lom.getValue(i));
163         }
164     }
165 
166     @Test
167     public void testIndexOf() {
168         resetEmpty();
169         ListOrderedMap<K, V> lom = getMap();
170         assertEquals(-1, lom.indexOf(getOtherKeys()));
171 
172         resetFull();
173         lom = getMap();
174         final List<K> list = new ArrayList<>();
175         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext();) {
176             list.add(it.next());
177         }
178         for (int i = 0; i < list.size(); i++) {
179             assertEquals(i, lom.indexOf(list.get(i)));
180         }
181     }
182 
183 //    public void testCreate() throws Exception {
184 //        resetEmpty();
185 //        writeExternalFormToDisk(
186 //            (java.io.Serializable) map,
187 //            "D:/dev/collections/data/test/ListOrderedMap.emptyCollection.version3.1.obj");
188 //        resetFull();
189 //        writeExternalFormToDisk(
190 //            (java.io.Serializable) map,
191 //            "D:/dev/collections/data/test/ListOrderedMap.fullCollection.version3.1.obj");
192 //    }
193 
194     @Test
195     public void testRemoveByIndex() {
196         resetEmpty();
197         ListOrderedMap<K, V> lom = getMap();
198         try {
199             lom.remove(0);
200         } catch (final IndexOutOfBoundsException ex) {}
201         try {
202             lom.remove(-1);
203         } catch (final IndexOutOfBoundsException ex) {}
204 
205         resetFull();
206         lom = getMap();
207         try {
208             lom.remove(-1);
209         } catch (final IndexOutOfBoundsException ex) {}
210         try {
211             lom.remove(lom.size());
212         } catch (final IndexOutOfBoundsException ex) {}
213 
214         final List<K> list = new ArrayList<>();
215         for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext();) {
216             list.add(it.next());
217         }
218         for (int i = 0; i < list.size(); i++) {
219             final Object key = list.get(i);
220             final Object value = lom.get(key);
221             assertEquals(value, lom.remove(i));
222             list.remove(i);
223             assertFalse(lom.containsKey(key));
224         }
225     }
226 }