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   * Abstract test class for {@link IterableMap} methods and contracts.
33   */
34  public abstract class AbstractIterableMapTest<K, V> extends AbstractMapTest<K, V> {
35  
36      public class InnerTestMapIterator extends AbstractMapIteratorTest<K, V> {
37          public InnerTestMapIterator() {
38              super("InnerTestMapIterator");
39          }
40  
41          @Override
42          public V[] addSetValues() {
43              return AbstractIterableMapTest.this.getNewSampleValues();
44          }
45  
46          @Override
47          public Map<K, V> getConfirmedMap() {
48              // assumes makeFullMapIterator() called first
49              return AbstractIterableMapTest.this.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 AbstractIterableMapTest.this.isRemoveSupported();
78          }
79  
80          @Override
81          public boolean supportsSetValue() {
82              return AbstractIterableMapTest.this.isSetValueSupported();
83          }
84  
85          @Override
86          public void verify() {
87              super.verify();
88              AbstractIterableMapTest.this.verify();
89          }
90      }
91  
92      /**
93       * JUnit constructor.
94       *
95       * @param testName  the test name
96       */
97      public AbstractIterableMapTest(final String testName) {
98          super(testName);
99      }
100 
101     public BulkTest bulkTestMapIterator() {
102         return new InnerTestMapIterator();
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public IterableMap<K, V> getMap() {
110         return (IterableMap<K, V>) super.getMap();
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     public IterableMap<K, V> makeFullMap() {
118         return (IterableMap<K, V>) super.makeFullMap();
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public abstract IterableMap<K, V> makeObject();
126 
127     @Test
128     public void testFailFastEntrySet() {
129         if (!isRemoveSupported()) {
130             return;
131         }
132         if (!isFailFastExpected()) {
133             return;
134         }
135         resetFull();
136         Iterator<Map.Entry<K, V>> it = getMap().entrySet().iterator();
137         final Map.Entry<K, V> val = it.next();
138         getMap().remove(val.getKey());
139         final Iterator<Map.Entry<K, V>> finalIt0 = it;
140         assertThrows(ConcurrentModificationException.class, () -> finalIt0.next());
141 
142         resetFull();
143         it = getMap().entrySet().iterator();
144         it.next();
145         getMap().clear();
146         final Iterator<Map.Entry<K, V>> finalIt1 = it;
147         assertThrows(ConcurrentModificationException.class, () -> finalIt1.next());
148     }
149 
150     @Test
151     public void testFailFastKeySet() {
152         if (!isRemoveSupported()) {
153             return;
154         }
155         if (!isFailFastExpected()) {
156             return;
157         }
158         resetFull();
159         Iterator<K> it = getMap().keySet().iterator();
160         final K val = it.next();
161         getMap().remove(val);
162         final Iterator<K> finalIt0 = it;
163         assertThrows(ConcurrentModificationException.class, () -> finalIt0.next());
164 
165         resetFull();
166         it = getMap().keySet().iterator();
167         it.next();
168         getMap().clear();
169         final Iterator<K> finalIt1 = it;
170         assertThrows(ConcurrentModificationException.class, () -> finalIt1.next());
171     }
172 
173 //  public void testCreate() throws Exception {
174 //      resetEmpty();
175 //      writeExternalFormToDisk((Serializable) map, "D:/dev/collections/data/test/HashedMap.emptyCollection.version3.obj");
176 //      resetFull();
177 //      writeExternalFormToDisk((Serializable) map, "D:/dev/collections/data/test/HashedMap.fullCollection.version3.obj");
178 //  }
179 
180     @Test
181     public void testFailFastValues() {
182         if (!isRemoveSupported()) {
183             return;
184         }
185         if (!isFailFastExpected()) {
186             return;
187         }
188         resetFull();
189         Iterator<V> it = getMap().values().iterator();
190         it.next();
191         getMap().remove(getMap().keySet().iterator().next());
192         final Iterator<V> finalIt0 = it;
193         assertThrows(ConcurrentModificationException.class, () -> finalIt0.next());
194 
195         resetFull();
196         it = getMap().values().iterator();
197         it.next();
198         getMap().clear();
199         final Iterator<V> finalIt1 = it;
200         assertThrows(ConcurrentModificationException.class, () -> finalIt1.next());
201     }
202 
203 }