|
| 1 | +package com.baeldung.java.map; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import com.google.common.collect.ImmutableMap; |
| 14 | + |
| 15 | + |
| 16 | +public class ImmutableMapUnitTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void whenCollectionsUnModifiableMapMethod_thenOriginalCollectionChangesReflectInUnmodifiableMap() { |
| 20 | + |
| 21 | + Map<String, String> mutableMap = new HashMap<>(); |
| 22 | + mutableMap.put("USA", "North America"); |
| 23 | + |
| 24 | + Map<String, String> unmodifiableMap = Collections.unmodifiableMap(mutableMap); |
| 25 | + assertThrows(UnsupportedOperationException.class, () -> unmodifiableMap.put("Canada", "North America")); |
| 26 | + |
| 27 | + mutableMap.remove("USA"); |
| 28 | + assertFalse(unmodifiableMap.containsKey("USA")); |
| 29 | + |
| 30 | + mutableMap.put("Mexico", "North America"); |
| 31 | + assertTrue(unmodifiableMap.containsKey("Mexico")); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + @SuppressWarnings("deprecation") |
| 36 | + public void whenGuavaImmutableMapFromCopyOfMethod_thenOriginalCollectionChangesDoNotReflectInImmutableMap() { |
| 37 | + |
| 38 | + Map<String, String> mutableMap = new HashMap<>(); |
| 39 | + mutableMap.put("USA", "North America"); |
| 40 | + |
| 41 | + ImmutableMap<String, String> immutableMap = ImmutableMap.copyOf(mutableMap); |
| 42 | + assertTrue(immutableMap.containsKey("USA")); |
| 43 | + |
| 44 | + assertThrows(UnsupportedOperationException.class, () -> immutableMap.put("Canada", "North America")); |
| 45 | + |
| 46 | + mutableMap.remove("USA"); |
| 47 | + assertTrue(immutableMap.containsKey("USA")); |
| 48 | + |
| 49 | + mutableMap.put("Mexico", "North America"); |
| 50 | + assertFalse(immutableMap.containsKey("Mexico")); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @SuppressWarnings("deprecation") |
| 55 | + public void whenGuavaImmutableMapFromBuilderMethod_thenOriginalCollectionChangesDoNotReflectInImmutableMap() { |
| 56 | + |
| 57 | + Map<String, String> mutableMap = new HashMap<>(); |
| 58 | + mutableMap.put("USA", "North America"); |
| 59 | + |
| 60 | + ImmutableMap<String, String> immutableMap = ImmutableMap.<String, String>builder() |
| 61 | + .putAll(mutableMap) |
| 62 | + .put("Costa Rica", "North America") |
| 63 | + .build(); |
| 64 | + assertTrue(immutableMap.containsKey("USA")); |
| 65 | + assertTrue(immutableMap.containsKey("Costa Rica")); |
| 66 | + |
| 67 | + assertThrows(UnsupportedOperationException.class, () -> immutableMap.put("Canada", "North America")); |
| 68 | + |
| 69 | + mutableMap.remove("USA"); |
| 70 | + assertTrue(immutableMap.containsKey("USA")); |
| 71 | + |
| 72 | + mutableMap.put("Mexico", "North America"); |
| 73 | + assertFalse(immutableMap.containsKey("Mexico")); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + @SuppressWarnings("deprecation") |
| 78 | + public void whenGuavaImmutableMapFromOfMethod_thenOriginalCollectionChangesDoNotReflectInImmutableMap() { |
| 79 | + |
| 80 | + ImmutableMap<String, String> immutableMap = ImmutableMap.of("USA", "North America", "Costa Rica", "North America"); |
| 81 | + assertTrue(immutableMap.containsKey("USA")); |
| 82 | + assertTrue(immutableMap.containsKey("Costa Rica")); |
| 83 | + |
| 84 | + assertThrows(UnsupportedOperationException.class, () -> immutableMap.put("Canada", "North America")); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments