|
1 | 1 | package org.baeldung.java; |
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.apache.commons.collections4.ListUtils; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.google.common.collect.ImmutableList; |
| 12 | + |
3 | 13 | public class CoreJavaUnitTest { |
4 | 14 |
|
5 | 15 | // tests - |
6 | 16 |
|
| 17 | + @Test |
| 18 | + public final void givenUsingTheJdk_whenArrayListIsSynchronized_thenCorrect() { |
| 19 | + final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three")); |
| 20 | + final List<String> synchronizedList = Collections.synchronizedList(list); |
| 21 | + System.out.println("Synchronized List is: " + synchronizedList); |
| 22 | + } |
| 23 | + |
| 24 | + @Test(expected = UnsupportedOperationException.class) |
| 25 | + public final void givenUsingTheJdk_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() { |
| 26 | + final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three")); |
| 27 | + final List<String> unmodifiableList = Collections.unmodifiableList(list); |
| 28 | + unmodifiableList.add("four"); |
| 29 | + } |
| 30 | + |
| 31 | + @Test(expected = UnsupportedOperationException.class) |
| 32 | + public final void givenUsingGuava_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() { |
| 33 | + final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three")); |
| 34 | + final List<String> unmodifiableList = ImmutableList.copyOf(list); |
| 35 | + unmodifiableList.add("four"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test(expected = UnsupportedOperationException.class) |
| 39 | + public final void givenUsingCommonsCollections_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() { |
| 40 | + final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three")); |
| 41 | + final List<String> unmodifiableList = ListUtils.unmodifiableList(list); |
| 42 | + unmodifiableList.add("four"); |
| 43 | + } |
| 44 | + |
7 | 45 | } |
0 commit comments