1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
49 return AbstractIterableMapTest.this.getConfirmed();
50 }
51
52 @Override
53 public Map<K, V> getMap() {
54
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
94
95
96
97 public AbstractIterableMapTest(final String testName) {
98 super(testName);
99 }
100
101 public BulkTest bulkTestMapIterator() {
102 return new InnerTestMapIterator();
103 }
104
105
106
107
108 @Override
109 public IterableMap<K, V> getMap() {
110 return (IterableMap<K, V>) super.getMap();
111 }
112
113
114
115
116 @Override
117 public IterableMap<K, V> makeFullMap() {
118 return (IterableMap<K, V>) super.makeFullMap();
119 }
120
121
122
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
174
175
176
177
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 }