Added changes based on review comments
This commit is contained in:
parent
074d016b64
commit
993f2baeea
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.map.concurrenthashmap;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConcurrentModificationErrorUnitTest {
|
||||
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void whenRemoveAndAddOnHashMap_thenConcurrentModificationError() {
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
public void whenRemoveAndAddOnConcurrentHashMap_thenNoError() {
|
||||
Map<Integer, String> map = new ConcurrentHashMap<>();
|
||||
map.put(1, "baeldung");
|
||||
map.put(2, "HashMap");
|
||||
Iterator<Entry<Integer, String>> iterator = map.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
map.put(3, "Modification");
|
||||
iterator.next();
|
||||
}
|
||||
|
||||
Assert.assertEquals(3, map.size());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.map.concurrenthashmap;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NullAllowInMapUnitTest {
|
||||
|
||||
@Test
|
||||
public void allowNullKey_In_SynchronizedMap() {
|
||||
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_ConcurrentHasMap() {
|
||||
Map<String, Integer> map = new ConcurrentHashMap<>();
|
||||
map.put(null, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allowNullValue_In_SynchronizedMap() {
|
||||
Map<String, Integer> map = Collections.synchronizedMap(new HashMap<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<>();
|
||||
map.put("1", null);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue