From 993f2baeea0bbe3348eaac6406c5106460e7ea7a Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Mon, 30 Nov 2020 07:25:51 +0530 Subject: [PATCH] Added changes based on review comments --- .../ConcurrentModificationErrorUnitTest.java | 41 +++++++++++++++++++ .../NullAllowInMapUnitTest.java | 40 ++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentModificationErrorUnitTest.java create mode 100644 core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/NullAllowInMapUnitTest.java diff --git a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentModificationErrorUnitTest.java b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentModificationErrorUnitTest.java new file mode 100644 index 0000000000..899c902fb0 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentModificationErrorUnitTest.java @@ -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 map = new HashMap<>(); + map.put(1, "baeldung"); + map.put(2, "HashMap"); + Map synchronizedMap = Collections.synchronizedMap(map); + Iterator> iterator = synchronizedMap.entrySet().iterator(); + while (iterator.hasNext()) { + synchronizedMap.put(3, "Modification"); + iterator.next(); + } + } + + public void whenRemoveAndAddOnConcurrentHashMap_thenNoError() { + Map map = new ConcurrentHashMap<>(); + map.put(1, "baeldung"); + map.put(2, "HashMap"); + Iterator> iterator = map.entrySet().iterator(); + while (iterator.hasNext()) { + map.put(3, "Modification"); + iterator.next(); + } + + Assert.assertEquals(3, map.size()); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/NullAllowInMapUnitTest.java b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/NullAllowInMapUnitTest.java new file mode 100644 index 0000000000..60e293f4b3 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/NullAllowInMapUnitTest.java @@ -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 map = Collections + .synchronizedMap(new HashMap()); + map.put(null, 1); + Assert.assertTrue(map.get(null).equals(1)); + } + + @Test(expected = NullPointerException.class) + public void allowNullKey_In_ConcurrentHasMap() { + Map map = new ConcurrentHashMap<>(); + map.put(null, 1); + } + + @Test + public void allowNullValue_In_SynchronizedMap() { + Map map = Collections.synchronizedMap(new HashMap()); + map.put("1", null); + Assert.assertNull(map.get("1")); + } + + @Test(expected = NullPointerException.class) + public void allowNullValue_In_ConcurrentHasMap() { + Map map = new ConcurrentHashMap<>(); + map.put("1", null); + } + +} \ No newline at end of file