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  public class UnmodifiableOrderedMapIteratorTest<K, V> extends AbstractOrderedMapIteratorTest<K, V> {
38  
39      public UnmodifiableOrderedMapIteratorTest() {
40          super(UnmodifiableOrderedMapIteratorTest.class.getSimpleName());
41      }
42  
43      @Override
44      @SuppressWarnings("unchecked")
45      public Map<K, V> getConfirmedMap() {
46          final Map<K, V> testMap = new TreeMap<>();
47          testMap.put((K) "A", (V) "a");
48          testMap.put((K) "B", (V) "b");
49          testMap.put((K) "C", (V) "c");
50          return testMap;
51      }
52  
53      @Override
54      @SuppressWarnings("unchecked")
55      public OrderedMap<K, V> getMap() {
56          final OrderedMap<K, V> testMap = ListOrderedMap.listOrderedMap(new HashMap<K, V>());
57          testMap.put((K) "A", (V) "a");
58          testMap.put((K) "B", (V) "b");
59          testMap.put((K) "C", (V) "c");
60          return testMap;
61      }
62  
63      @Override
64      public OrderedMapIterator<K, V> makeEmptyIterator() {
65          return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(
66                  ListOrderedMap.listOrderedMap(new HashMap<K, V>()).mapIterator());
67      }
68  
69      @Override
70      public OrderedMapIterator<K, V> makeObject() {
71          return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(getMap().mapIterator());
72      }
73  
74      @Override
75      public boolean supportsRemove() {
76          return false;
77      }
78  
79      @Override
80      public boolean supportsSetValue() {
81          return false;
82      }
83  
84      @Test
85      public void testDecorateFactory() {
86          OrderedMapIterator<K, V> it = makeObject();
87          assertSame(it, UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it));
88  
89          it = getMap().mapIterator();
90          assertNotSame(it, UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it));
91  
92          assertThrows(NullPointerException.class, () -> UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(null));
93      }
94  
95      @Test
96      public void testOrderedMapIterator() {
97          assertTrue(makeEmptyIterator() instanceof Unmodifiable);
98      }
99  
100 }