Skip to content

Commit b7d5954

Browse files
authored
Add iterator fail-fast tests for LinkedTreeMap.clear() (#2992)
* Add boundary tests for JsonTreeWriter.endArray() error handling * Add comprehensive boundary tests for LinkedTreeMap.clear() method * Update JsonTreeWriterTest.java * Remove redundant clear map tests from LinkedTreeMapTest Removed multiple test cases for clearing LinkedTreeMap, including tests for single and multiple entries, null values, and various scenarios. * Remove blank line in LinkedTreeMapTest Removed unnecessary blank line in LinkedTreeMapTest.
1 parent 36cacf3 commit b7d5954

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

gson/src/test/java/com/google/gson/internal/LinkedTreeMapTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.ObjectInputStream;
2727
import java.io.ObjectOutputStream;
2828
import java.util.Collections;
29+
import java.util.ConcurrentModificationException;
2930
import java.util.Iterator;
3031
import java.util.Map;
3132
import java.util.Map.Entry;
@@ -219,4 +220,34 @@ public void testJavaSerialization() throws IOException, ClassNotFoundException {
219220
Map<String, Integer> deserialized = (Map<String, Integer>) objIn.readObject();
220221
assertThat(deserialized).isEqualTo(Collections.singletonMap("a", 1));
221222
}
223+
224+
@Test
225+
public void testClearInvalidatesExistingIterator() {
226+
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
227+
map.put("key1", "value1");
228+
map.put("key2", "value2");
229+
map.put("key3", "value3");
230+
231+
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
232+
assertThat(iterator.hasNext()).isTrue();
233+
234+
map.clear();
235+
236+
assertThrows(ConcurrentModificationException.class, iterator::next);
237+
}
238+
239+
@Test
240+
public void testClearInvalidatesExistingKeySetIterator() {
241+
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
242+
map.put("key1", "value1");
243+
map.put("key2", "value2");
244+
map.put("key3", "value3");
245+
246+
Iterator<String> iterator = map.keySet().iterator();
247+
assertThat(iterator.hasNext()).isTrue();
248+
249+
map.clear();
250+
251+
assertThrows(ConcurrentModificationException.class, iterator::next);
252+
}
222253
}

0 commit comments

Comments
 (0)