From c40d6962dd5c630b7079297e7ba8ae90a55073e6 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sat, 4 Feb 2017 13:45:39 +0000 Subject: [PATCH 1/3] Added ConcurrentModificationException test --- .../ConcurrentModificationUnitTest.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java new file mode 100644 index 0000000000..9db460abbd --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java @@ -0,0 +1,54 @@ +package com.baeldung.java.concurrentmodificationexception; + +import org.assertj.core.api.Assertions; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.ConcurrentModificationException; +import java.util.Iterator; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.util.Lists.newArrayList; + +public class ConcurrentModificationUnitTest { + @Test(expected = ConcurrentModificationException.class) + public void whilstRemovingDuringIteration_shouldThrowException() throws InterruptedException { + + ArrayList integers = newArrayList(1, 2, 3); + + for (Integer integer : integers) { + integers.remove(1); + } + } + + @Test + public void whilstRemovingDuringIteration_shouldNotThrowException() throws InterruptedException { + + ArrayList integers = newArrayList(1, 2, 3); + + for (Iterator iterator = integers.iterator(); iterator.hasNext();) { + Integer integer = iterator.next(); + if(integer == 2) { + iterator.remove(); + } + } + + assertThat(integers).containsExactly(1, 3); + } + + @Test + public void whilstRemovingDuringForEach_shouldNotThrowException() throws InterruptedException { + + ArrayList integers = newArrayList(1, 2, 3); + ArrayList toRemove = newArrayList(); + + for (Integer integer : integers) { + if(integer == 2) { + toRemove.add(integer); + } + } + integers.removeAll(toRemove); + + assertThat(integers).containsExactly(1, 3); + } +} From 0a444d32cf1d9bb9225a0e28d54e2576fe718775 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 6 Feb 2017 21:55:24 +0000 Subject: [PATCH 2/3] Renamed unit tests for ConcurrentModificationUnit --- .../ConcurrentModificationUnitTest.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java index 9db460abbd..11c6012ecd 100644 --- a/core-java/src/test/java/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java +++ b/core-java/src/test/java/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java @@ -12,7 +12,7 @@ import static org.assertj.core.util.Lists.newArrayList; public class ConcurrentModificationUnitTest { @Test(expected = ConcurrentModificationException.class) - public void whilstRemovingDuringIteration_shouldThrowException() throws InterruptedException { + public void givenIterating_whenRemoving_thenThrowException() throws InterruptedException { ArrayList integers = newArrayList(1, 2, 3); @@ -22,7 +22,7 @@ public class ConcurrentModificationUnitTest { } @Test - public void whilstRemovingDuringIteration_shouldNotThrowException() throws InterruptedException { + public void givenIterating_whenUsingIteratorRemove_thenDontError() throws InterruptedException { ArrayList integers = newArrayList(1, 2, 3); @@ -37,7 +37,7 @@ public class ConcurrentModificationUnitTest { } @Test - public void whilstRemovingDuringForEach_shouldNotThrowException() throws InterruptedException { + public void givenIterating_whenUsingRemovalList_thenDontError() throws InterruptedException { ArrayList integers = newArrayList(1, 2, 3); ArrayList toRemove = newArrayList(); @@ -51,4 +51,14 @@ public class ConcurrentModificationUnitTest { assertThat(integers).containsExactly(1, 3); } + + @Test + public void whenUsingRemoveIf_thenRemoveElements() throws InterruptedException { + + ArrayList integers = newArrayList(1, 2, 3); + + integers.removeIf((i) -> i == 2); + + assertThat(integers).containsExactly(1, 3); + } } From 3559789573f612be3911eb983bf51abed125c5c0 Mon Sep 17 00:00:00 2001 From: Pedja Date: Tue, 7 Feb 2017 11:54:48 +0100 Subject: [PATCH 3/3] BAEL-677 Small refactoring --- .../ConcurrentModificationUnitTest.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) rename core-java/src/test/java/com/baeldung/java/{concurrentmodificationexception => concurrentmodification}/ConcurrentModificationUnitTest.java (81%) diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java similarity index 81% rename from core-java/src/test/java/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java rename to core-java/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java index 11c6012ecd..d16a1fcdf4 100644 --- a/core-java/src/test/java/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java +++ b/core-java/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java @@ -1,6 +1,5 @@ -package com.baeldung.java.concurrentmodificationexception; +package com.baeldung.java.concurrentmodification; -import org.assertj.core.api.Assertions; import org.junit.Test; import java.util.ArrayList; @@ -22,7 +21,7 @@ public class ConcurrentModificationUnitTest { } @Test - public void givenIterating_whenUsingIteratorRemove_thenDontError() throws InterruptedException { + public void givenIterating_whenUsingIteratorRemove_thenNoError() throws InterruptedException { ArrayList integers = newArrayList(1, 2, 3); @@ -37,7 +36,7 @@ public class ConcurrentModificationUnitTest { } @Test - public void givenIterating_whenUsingRemovalList_thenDontError() throws InterruptedException { + public void givenIterating_whenUsingRemovalList_thenNoError() throws InterruptedException { ArrayList integers = newArrayList(1, 2, 3); ArrayList toRemove = newArrayList(); @@ -57,7 +56,7 @@ public class ConcurrentModificationUnitTest { ArrayList integers = newArrayList(1, 2, 3); - integers.removeIf((i) -> i == 2); + integers.removeIf(i -> i == 2); assertThat(integers).containsExactly(1, 3); }