BAEL-4686: Added some more test cases for TreeMap and LinkedHashMap

This commit is contained in:
Amitabh Tiwari 2020-12-09 17:45:25 +05:30
parent 993f2baeea
commit 2f42035fb5
2 changed files with 64 additions and 2 deletions

View File

@ -4,8 +4,10 @@ import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.Assert;
@ -25,7 +27,34 @@ public class ConcurrentModificationErrorUnitTest {
iterator.next();
}
}
@Test(expected = ConcurrentModificationException.class)
public void whenRemoveAndAddOnTreeMap_thenConcurrentModificationError() {
Map<Integer, String> map = new TreeMap<>();
map.put(1, "baeldung");
map.put(2, "HashMap");
Map<Integer, String> synchronizedMap = Collections.synchronizedMap(map);
Iterator<Entry<Integer, String>> iterator = synchronizedMap.entrySet().iterator();
while (iterator.hasNext()) {
synchronizedMap.put(3, "Modification");
iterator.next();
}
}
@Test(expected = ConcurrentModificationException.class)
public void whenRemoveAndAddOnLinkedHashMap_thenConcurrentModificationError() {
Map<Integer, String> map = new LinkedHashMap<>();
map.put(1, "baeldung");
map.put(2, "HashMap");
Map<Integer, String> synchronizedMap = Collections.synchronizedMap(map);
Iterator<Entry<Integer, String>> iterator = synchronizedMap.entrySet().iterator();
while (iterator.hasNext()) {
synchronizedMap.put(3, "Modification");
iterator.next();
}
}
@Test
public void whenRemoveAndAddOnConcurrentHashMap_thenNoError() {
Map<Integer, String> map = new ConcurrentHashMap<>();
map.put(1, "baeldung");

View File

@ -2,7 +2,9 @@ package com.baeldung.map.concurrenthashmap;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.Assert;
@ -11,13 +13,29 @@ import org.junit.Test;
public class NullAllowInMapUnitTest {
@Test
public void allowNullKey_In_SynchronizedMap() {
public void allowNullKey_In_HashMapBackedSynchronizedMap() {
Map<String, Integer> map = Collections
.synchronizedMap(new HashMap<String, Integer>());
map.put(null, 1);
Assert.assertTrue(map.get(null).equals(1));
}
@Test(expected = NullPointerException.class)
public void allowNullKey_In_TreeMapBackedSynchronizedMap() {
Map<String, Integer> map = Collections.synchronizedMap(new TreeMap<String, Integer>());
map.put(null, 1);
Assert.assertTrue(map.get(null).equals(1));
}
@Test
public void allowNullKey_In_LinkedHashMapBackedSynchronizedMap() {
Map<String, Integer> map = Collections
.synchronizedMap(new LinkedHashMap<String, Integer>());
map.put(null, 1);
Assert.assertTrue(map.get(null).equals(1));
}
@Test(expected = NullPointerException.class)
public void allowNullKey_In_ConcurrentHasMap() {
Map<String, Integer> map = new ConcurrentHashMap<>();
@ -25,12 +43,27 @@ public class NullAllowInMapUnitTest {
}
@Test
public void allowNullValue_In_SynchronizedMap() {
public void allowNullValue_In_HashMapBackedSynchronizedMap() {
Map<String, Integer> map = Collections.synchronizedMap(new HashMap<String, Integer>());
map.put("1", null);
Assert.assertNull(map.get("1"));
}
@Test
public void allowNullValue_In_TreeMapBackedSynchronizedMap() {
Map<String, Integer> map = Collections.synchronizedMap(new TreeMap<String, Integer>());
map.put("1", null);
Assert.assertNull(map.get("1"));
}
@Test
public void allowNullValue_In_LinkedHashSynchronizedMap() {
Map<String, Integer> map = Collections
.synchronizedMap(new LinkedHashMap<String, Integer>());
map.put("1", null);
Assert.assertNull(map.get("1"));
}
@Test(expected = NullPointerException.class)
public void allowNullValue_In_ConcurrentHasMap() {
Map<String, Integer> map = new ConcurrentHashMap<>();