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 static org.junit.jupiter.api.Assertions.assertThrows;
20  
21  import java.util.ConcurrentModificationException;
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  import org.apache.commons.collections4.BulkTest;
26  import org.apache.commons.collections4.IterableMap;
27  import org.apache.commons.collections4.MapIterator;
28  import org.apache.commons.collections4.iterators.AbstractMapIteratorTest;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests {@link IterableMap}.
33   *
34   * @param <K> the key type.
35   * @param <V> the value type.
36   */
37  public abstract class AbstractIterableMapTest<K, V> extends AbstractMapTest<IterableMap<K, V>, K, V> {
38  
39      public class InnerTestMapIterator extends AbstractMapIteratorTest<K, V> {
40  
41          @Override
42          public V[] addSetValues() {
43              return getNewSampleValues();
44          }
45  
46          @Override
47          public Map<K, V> getConfirmedMap() {
48              // assumes makeFullMapIterator() called first
49              return getConfirmed();
50          }
51  
52          @Override
53          public Map<K, V> getMap() {
54              // assumes makeFullMapIterator() called first
55              return AbstractIterableMapTest.this.getMap();
56          }
57  
58          @Override
59          public boolean isGetStructuralModify() {
60              return AbstractIterableMapTest.this.isGetStructuralModify();
61          }
62  
63          @Override
64          public MapIterator<K, V> makeEmptyIterator() {
65              resetEmpty();
66              return AbstractIterableMapTest.this.getMap().mapIterator();
67          }
68  
69          @Override
70          public MapIterator<K, V> makeObject() {
71              resetFull();
72              return AbstractIterableMapTest.this.getMap().mapIterator();
73          }
74  
75          @Override
76          public boolean supportsRemove() {
77              return isRemoveSupported();
78          }
79  
80          @Override
81          public boolean supportsSetValue() {
82              return isSetValueSupported();
83          }
84  
85          @Override
86          public void verify() {
87              super.verify();
88              AbstractIterableMapTest.this.verify();
89          }
90      }
91  
92      public BulkTest bulkTestMapIterator() {
93          return new InnerTestMapIterator();
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public IterableMap<K, V> getMap() {
101         return super.getMap();
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public IterableMap<K, V> makeFullMap() {
109         return super.makeFullMap();
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public abstract IterableMap<K, V> makeObject();
117 
118     @Test
119     public void testFailFastEntrySet() {
120         if (!isRemoveSupported()) {
121             return;
122         }
123         if (!isFailFastExpected()) {
124             return;
125         }
126         resetFull();
127         Iterator<Map.Entry<K, V>> it = getMap().entrySet().iterator();
128         final Map.Entry<K, V> val = it.next();
129         getMap().remove(val.getKey());
130         final Iterator<Map.Entry<K, V>> finalIt0 = it;
131         assertThrows(ConcurrentModificationException.class, () -> finalIt0.next());
132 
133         resetFull();
134         it = getMap().entrySet().iterator();
135         it.next();
136         getMap().clear();
137         final Iterator<Map.Entry<K, V>> finalIt1 = it;
138         assertThrows(ConcurrentModificationException.class, () -> finalIt1.next());
139     }
140 
141     @Test
142     public void testFailFastKeySet() {
143         if (!isRemoveSupported()) {
144             return;
145         }
146         if (!isFailFastExpected()) {
147             return;
148         }
149         resetFull();
150         Iterator<K> it = getMap().keySet().iterator();
151         final K val = it.next();
152         getMap().remove(val);
153         final Iterator<K> finalIt0 = it;
154         assertThrows(ConcurrentModificationException.class, () -> finalIt0.next());
155 
156         resetFull();
157         it = getMap().keySet().iterator();
158         it.next();
159         getMap().clear();
160         final Iterator<K> finalIt1 = it;
161         assertThrows(ConcurrentModificationException.class, () -> finalIt1.next());
162     }
163 
164 //  public void testCreate() throws Exception {
165 //      resetEmpty();
166 //      writeExternalFormToDisk((Serializable) map, "D:/dev/collections/data/test/HashedMap.emptyCollection.version3.obj");
167 //      resetFull();
168 //      writeExternalFormToDisk((Serializable) map, "D:/dev/collections/data/test/HashedMap.fullCollection.version3.obj");
169 //  }
170 
171     @Test
172     public void testFailFastValues() {
173         if (!isRemoveSupported()) {
174             return;
175         }
176         if (!isFailFastExpected()) {
177             return;
178         }
179         resetFull();
180         Iterator<V> it = getMap().values().iterator();
181         it.next();
182         getMap().remove(getMap().keySet().iterator().next());
183         final Iterator<V> finalIt0 = it;
184         assertThrows(ConcurrentModificationException.class, () -> finalIt0.next());
185 
186         resetFull();
187         it = getMap().values().iterator();
188         it.next();
189         getMap().clear();
190         final Iterator<V> finalIt1 = it;
191         assertThrows(ConcurrentModificationException.class, () -> finalIt1.next());
192     }
193 
194 }