forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'foo' of https://github.com/mogronalol/tutorials into mo…
…gronalol-foo
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...ava/com/baeldung/java/concurrentmodificationexception/ConcurrentModificationUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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 givenIterating_whenRemoving_thenThrowException() throws InterruptedException { | ||
|
||
ArrayList<Integer> integers = newArrayList(1, 2, 3); | ||
|
||
for (Integer integer : integers) { | ||
integers.remove(1); | ||
} | ||
} | ||
|
||
@Test | ||
public void givenIterating_whenUsingIteratorRemove_thenDontError() throws InterruptedException { | ||
|
||
ArrayList<Integer> integers = newArrayList(1, 2, 3); | ||
|
||
for (Iterator<Integer> iterator = integers.iterator(); iterator.hasNext();) { | ||
Integer integer = iterator.next(); | ||
if(integer == 2) { | ||
iterator.remove(); | ||
} | ||
} | ||
|
||
assertThat(integers).containsExactly(1, 3); | ||
} | ||
|
||
@Test | ||
public void givenIterating_whenUsingRemovalList_thenDontError() throws InterruptedException { | ||
|
||
ArrayList<Integer> integers = newArrayList(1, 2, 3); | ||
ArrayList<Integer> toRemove = newArrayList(); | ||
|
||
for (Integer integer : integers) { | ||
if(integer == 2) { | ||
toRemove.add(integer); | ||
} | ||
} | ||
integers.removeAll(toRemove); | ||
|
||
assertThat(integers).containsExactly(1, 3); | ||
} | ||
|
||
@Test | ||
public void whenUsingRemoveIf_thenRemoveElements() throws InterruptedException { | ||
|
||
ArrayList<Integer> integers = newArrayList(1, 2, 3); | ||
|
||
integers.removeIf((i) -> i == 2); | ||
|
||
assertThat(integers).containsExactly(1, 3); | ||
} | ||
} |