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