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.iterators;
18  
19  import static org.junit.jupiter.api.Assertions.assertNotSame;
20  import static org.junit.jupiter.api.Assertions.assertSame;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.TreeMap;
27  
28  import org.apache.commons.collections4.OrderedMap;
29  import org.apache.commons.collections4.OrderedMapIterator;
30  import org.apache.commons.collections4.Unmodifiable;
31  import org.apache.commons.collections4.map.ListOrderedMap;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests the UnmodifiableOrderedMapIterator.
36   *
37   * @param <K> the type of the keys in the maps tested.
38   * @param <V> the type of the values in the maps tested.
39   */
40  public class UnmodifiableOrderedMapIteratorTest<K, V> extends AbstractOrderedMapIteratorTest<K, V> {
41  
42      @Override
43      @SuppressWarnings("unchecked")
44      public Map<K, V> getConfirmedMap() {
45          final Map<K, V> testMap = new TreeMap<>();
46          testMap.put((K) "A", (V) "a");
47          testMap.put((K) "B", (V) "b");
48          testMap.put((K) "C", (V) "c");
49          return testMap;
50      }
51  
52      @Override
53      @SuppressWarnings("unchecked")
54      public OrderedMap<K, V> getMap() {
55          final OrderedMap<K, V> testMap = ListOrderedMap.listOrderedMap(new HashMap<K, V>());
56          testMap.put((K) "A", (V) "a");
57          testMap.put((K) "B", (V) "b");
58          testMap.put((K) "C", (V) "c");
59          return testMap;
60      }
61  
62      @Override
63      public OrderedMapIterator<K, V> makeEmptyIterator() {
64          return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(
65                  ListOrderedMap.listOrderedMap(new HashMap<K, V>()).mapIterator());
66      }
67  
68      @Override
69      public OrderedMapIterator<K, V> makeObject() {
70          return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(getMap().mapIterator());
71      }
72  
73      @Override
74      public boolean supportsRemove() {
75          return false;
76      }
77  
78      @Override
79      public boolean supportsSetValue() {
80          return false;
81      }
82  
83      @Test
84      public void testDecorateFactory() {
85          OrderedMapIterator<K, V> it = makeObject();
86          assertSame(it, UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it));
87  
88          it = getMap().mapIterator();
89          assertNotSame(it, UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it));
90  
91          assertThrows(NullPointerException.class, () -> UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(null));
92      }
93  
94      @Test
95      public void testOrderedMapIterator() {
96          assertTrue(makeEmptyIterator() instanceof Unmodifiable);
97      }
98  
99  }