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 java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.NoSuchElementException;
26  import java.util.TreeMap;
27  
28  import org.apache.commons.collections4.BulkTest;
29  import org.apache.commons.collections4.OrderedMap;
30  import org.apache.commons.collections4.OrderedMapIterator;
31  import org.apache.commons.collections4.comparators.NullComparator;
32  import org.apache.commons.collections4.iterators.AbstractOrderedMapIteratorTest;
33  
34  /**
35   * Abstract test class for {@link OrderedMap} methods and contracts.
36   *
37   */
38  public abstract class AbstractOrderedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
39  
40      /**
41       * JUnit constructor.
42       *
43       * @param testName  the test name
44       */
45      public AbstractOrderedMapTest(final String testName) {
46          super(testName);
47      }
48  
49      /**
50       * {@inheritDoc}
51       */
52      @Override
53      public abstract OrderedMap<K, V> makeObject();
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public OrderedMap<K, V> makeFullMap() {
60          return (OrderedMap<K, V>) super.makeFullMap();
61      }
62  
63      //-----------------------------------------------------------------------
64      /**
65       * OrderedMap uses TreeMap as its known comparison.
66       *
67       * @return a map that is known to be valid
68       */
69      @Override
70      public Map<K, V> makeConfirmedMap() {
71          return new TreeMap<>(new NullComparator<K>());
72      }
73  
74      /**
75       * The only confirmed collection we have that is ordered is the sorted one.
76       * Thus, sort the keys.
77       */
78      @Override
79      @SuppressWarnings("unchecked")
80      public K[] getSampleKeys() {
81          final List<K> list = new ArrayList<>(Arrays.asList(super.getSampleKeys()));
82          Collections.sort(list, new NullComparator<K>());
83          return (K[]) list.toArray();
84      }
85  
86      //-----------------------------------------------------------------------
87      public void testFirstKey() {
88          resetEmpty();
89          OrderedMap<K, V> ordered = getMap();
90          try {
91              ordered.firstKey();
92              fail();
93          } catch (final NoSuchElementException ex) {}
94  
95          resetFull();
96          ordered = getMap();
97          final K confirmedFirst = confirmed.keySet().iterator().next();
98          assertEquals(confirmedFirst, ordered.firstKey());
99      }
100 
101     public void testLastKey() {
102         resetEmpty();
103         OrderedMap<K, V> ordered = getMap();
104         try {
105             ordered.lastKey();
106             fail();
107         } catch (final NoSuchElementException ex) {}
108 
109         resetFull();
110         ordered = getMap();
111         K confirmedLast = null;
112         for (final Iterator<K> it = confirmed.keySet().iterator(); it.hasNext();) {
113             confirmedLast = it.next();
114         }
115         assertEquals(confirmedLast, ordered.lastKey());
116     }
117 
118     //-----------------------------------------------------------------------
119     public void testNextKey() {
120         resetEmpty();
121         OrderedMap<K, V> ordered = getMap();
122         assertEquals(null, ordered.nextKey(getOtherKeys()[0]));
123         if (!isAllowNullKey()) {
124             try {
125                 assertEquals(null, ordered.nextKey(null)); // this is allowed too
126             } catch (final NullPointerException ex) {}
127         } else {
128             assertEquals(null, ordered.nextKey(null));
129         }
130 
131         resetFull();
132         ordered = getMap();
133         final Iterator<K> it = confirmed.keySet().iterator();
134         K confirmedLast = it.next();
135         while (it.hasNext()) {
136             final K confirmedObject = it.next();
137             assertEquals(confirmedObject, ordered.nextKey(confirmedLast));
138             confirmedLast = confirmedObject;
139         }
140         assertEquals(null, ordered.nextKey(confirmedLast));
141 
142         if (!isAllowNullKey()) {
143             try {
144                 ordered.nextKey(null);
145                 fail();
146             } catch (final NullPointerException ex) {}
147         } else {
148             assertEquals(null, ordered.nextKey(null));
149         }
150     }
151 
152     public void testPreviousKey() {
153         resetEmpty();
154         OrderedMap<K, V> ordered = getMap();
155         assertEquals(null, ordered.previousKey(getOtherKeys()[0]));
156         if (!isAllowNullKey()) {
157             try {
158                 assertEquals(null, ordered.previousKey(null)); // this is allowed too
159             } catch (final NullPointerException ex) {}
160         } else {
161             assertEquals(null, ordered.previousKey(null));
162         }
163 
164         resetFull();
165         ordered = getMap();
166         final List<K> list = new ArrayList<>(confirmed.keySet());
167         Collections.reverse(list);
168         final Iterator<K> it = list.iterator();
169         K confirmedLast = it.next();
170         while (it.hasNext()) {
171             final K confirmedObject = it.next();
172             assertEquals(confirmedObject, ordered.previousKey(confirmedLast));
173             confirmedLast = confirmedObject;
174         }
175         assertEquals(null, ordered.previousKey(confirmedLast));
176 
177         if (!isAllowNullKey()) {
178             try {
179                 ordered.previousKey(null);
180                 fail();
181             } catch (final NullPointerException ex) {}
182         } else {
183             if (!isAllowNullKey()) {
184                 assertEquals(null, ordered.previousKey(null));
185             }
186         }
187     }
188 
189     //-----------------------------------------------------------------------
190     public BulkTest bulkTestOrderedMapIterator() {
191         return new InnerTestOrderedMapIterator();
192     }
193 
194     public class InnerTestOrderedMapIterator extends AbstractOrderedMapIteratorTest<K, V> {
195         public InnerTestOrderedMapIterator() {
196             super("InnerTestOrderedMapIterator");
197         }
198 
199         @Override
200         public boolean supportsRemove() {
201             return AbstractOrderedMapTest.this.isRemoveSupported();
202         }
203 
204         @Override
205         public boolean isGetStructuralModify() {
206             return AbstractOrderedMapTest.this.isGetStructuralModify();
207         }
208 
209         @Override
210         public boolean supportsSetValue() {
211             return AbstractOrderedMapTest.this.isSetValueSupported();
212         }
213 
214         @Override
215         public OrderedMapIterator<K, V> makeEmptyIterator() {
216             resetEmpty();
217             return AbstractOrderedMapTest.this.getMap().mapIterator();
218         }
219 
220         @Override
221         public OrderedMapIterator<K, V> makeObject() {
222             resetFull();
223             return AbstractOrderedMapTest.this.getMap().mapIterator();
224         }
225 
226         @Override
227         public OrderedMap<K, V> getMap() {
228             // assumes makeFullMapIterator() called first
229             return AbstractOrderedMapTest.this.getMap();
230         }
231 
232         @Override
233         public Map<K, V> getConfirmedMap() {
234             // assumes makeFullMapIterator() called first
235             return AbstractOrderedMapTest.this.getConfirmed();
236         }
237 
238         @Override
239         public void verify() {
240             super.verify();
241             AbstractOrderedMapTest.this.verify();
242         }
243     }
244 
245     /**
246      * {@inheritDoc}
247      */
248     @Override
249     public OrderedMap<K, V> getMap() {
250         return (OrderedMap<K, V>) super.getMap();
251     }
252 }