mirror of
https://github.com/google/gson.git
synced 2026-03-13 08:01:59 +08:00
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.
This commit is contained in:
@@ -26,6 +26,7 @@ import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
@@ -219,4 +220,34 @@ public final class LinkedTreeMapTest {
|
||||
Map<String, Integer> deserialized = (Map<String, Integer>) objIn.readObject();
|
||||
assertThat(deserialized).isEqualTo(Collections.singletonMap("a", 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClearInvalidatesExistingIterator() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
map.put("key3", "value3");
|
||||
|
||||
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
|
||||
assertThat(iterator.hasNext()).isTrue();
|
||||
|
||||
map.clear();
|
||||
|
||||
assertThrows(ConcurrentModificationException.class, iterator::next);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClearInvalidatesExistingKeySetIterator() {
|
||||
LinkedTreeMap<String, String> map = new LinkedTreeMap<>();
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
map.put("key3", "value3");
|
||||
|
||||
Iterator<String> iterator = map.keySet().iterator();
|
||||
assertThat(iterator.hasNext()).isTrue();
|
||||
|
||||
map.clear();
|
||||
|
||||
assertThrows(ConcurrentModificationException.class, iterator::next);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user