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  public class UnmodifiableMapIteratorTest<K, V> extends AbstractMapIteratorTest<K, V> {
37  
38      public UnmodifiableMapIteratorTest() {
39          super(UnmodifiableMapIteratorTest.class.getSimpleName());
40      }
41  
42      @Override
43      @SuppressWarnings("unchecked")
44      public Map<K, V> getConfirmedMap() {
45          final Map<K, V> testMap = new HashMap<>();
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 IterableMap<K, V> getMap() {
55          final IterableMap<K, V> testMap = new DualHashBidiMap<>();
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 MapIterator<K, V> makeEmptyIterator() {
64          return UnmodifiableMapIterator.unmodifiableMapIterator(new DualHashBidiMap<K, V>().mapIterator());
65      }
66  
67      @Override
68      public MapIterator<K, V> makeObject() {
69          return UnmodifiableMapIterator.unmodifiableMapIterator(getMap().mapIterator());
70      }
71  
72      @Override
73      public boolean supportsRemove() {
74          return false;
75      }
76  
77      @Override
78      public boolean supportsSetValue() {
79          return false;
80      }
81  
82      @Test
83      public void testDecorateFactory() {
84          MapIterator<K, V> it = makeObject();
85          assertSame(it, UnmodifiableMapIterator.unmodifiableMapIterator(it));
86  
87          it = getMap().mapIterator();
88          assertNotSame(it, UnmodifiableMapIterator.unmodifiableMapIterator(it));
89  
90          assertThrows(NullPointerException.class, () -> UnmodifiableMapIterator.unmodifiableMapIterator(null));
91      }
92  
93      @Test
94      public void testMapIterator() {
95          assertTrue(makeEmptyIterator() instanceof Unmodifiable);
96      }
97  
98  }