|
| 1 | +package com.baeldung.java9.language.collections; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import static org.junit.Assert.assertEquals; |
| 9 | +import static org.junit.Assert.assertFalse; |
| 10 | + |
| 11 | +public class MapFactoryMethodsTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + public void whenMapCreated_thenSuccess() { |
| 15 | + Map<String, String> traditionlMap = new HashMap<String, String>(); |
| 16 | + traditionlMap.put("foo", "a"); |
| 17 | + traditionlMap.put("bar", "b"); |
| 18 | + traditionlMap.put("baz", "c"); |
| 19 | + Map<String, String> factoryCreatedMap = Map.of("foo", "a", "bar", "b", "baz", "c"); |
| 20 | + assertEquals(traditionlMap, factoryCreatedMap); |
| 21 | + } |
| 22 | + |
| 23 | + @Test(expected = UnsupportedOperationException.class) |
| 24 | + public void onElemAdd_ifUnSupportedOpExpnThrown_thenSuccess() { |
| 25 | + Map<String, String> map = Map.of("foo", "a", "bar", "b"); |
| 26 | + map.put("baz", "c"); |
| 27 | + } |
| 28 | + |
| 29 | + @Test(expected = UnsupportedOperationException.class) |
| 30 | + public void onElemModify_ifUnSupportedOpExpnThrown_thenSuccess() { |
| 31 | + Map<String, String> map = Map.of("foo", "a", "bar", "b"); |
| 32 | + map.put("foo", "c"); |
| 33 | + } |
| 34 | + |
| 35 | + @Test(expected = UnsupportedOperationException.class) |
| 36 | + public void onElemRemove_ifUnSupportedOpExpnThrown_thenSuccess() { |
| 37 | + Map<String, String> map = Map.of("foo", "a", "bar", "b"); |
| 38 | + map.remove("foo"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test(expected = IllegalArgumentException.class) |
| 42 | + public void givenDuplicateKeys_ifIllegalArgExp_thenSuccess() { |
| 43 | + Map.of("foo", "a", "foo", "b"); |
| 44 | + } |
| 45 | + |
| 46 | + @Test(expected = NullPointerException.class) |
| 47 | + public void onNullKey_ifNullPtrExp_thenSuccess() { |
| 48 | + Map.of("foo", "a", null, "b"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test(expected = NullPointerException.class) |
| 52 | + public void onNullValue_ifNullPtrExp_thenSuccess() { |
| 53 | + Map.of("foo", "a", "bar", null); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void ifNotHashMap_thenSuccess() { |
| 58 | + Map<String, String> map = Map.of("foo", "a", "bar", "b"); |
| 59 | + assertFalse(map instanceof HashMap); |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments