Skip to content

Commit

Permalink
BAEL-4686: Added some more test cases for TreeMap and LinkedHashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ati083 committed Dec 9, 2020
1 parent cd79c3f commit da0d850
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
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;
Expand All @@ -25,7 +27,34 @@ public void whenRemoveAndAddOnHashMap_thenConcurrentModificationError() {
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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;
Expand All @@ -11,26 +13,57 @@
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<>();
map.put(null, 1);
}

@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<>();
Expand Down

0 comments on commit da0d850

Please sign in to comment.