|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package com.baeldung.java.map; |
| 5 | + |
| 6 | +import static org.junit.Assert.assertEquals; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | +import org.apache.commons.collections4.BidiMap; |
| 15 | +import org.apache.commons.collections4.bidimap.DualHashBidiMap; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import com.google.common.collect.HashBiMap; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author swpraman |
| 22 | + * |
| 23 | + */ |
| 24 | +public class MapUtilUnitTest { |
| 25 | + |
| 26 | + |
| 27 | + @Test |
| 28 | + public void whenUsingImperativeWayForSingleKey_shouldReturnSingleKey() { |
| 29 | + Map<String, String> capitalCountryMap = new HashMap<>(); |
| 30 | + capitalCountryMap.put("Tokyo", "Japan"); |
| 31 | + capitalCountryMap.put("New Delhi", "India"); |
| 32 | + assertEquals("New Delhi", MapUtil.getKey(capitalCountryMap, "India")); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void whenUsingImperativeWayForAllKeys_shouldReturnAllKeys() { |
| 37 | + Map<String, String> capitalCountryMap = new HashMap<>(); |
| 38 | + capitalCountryMap.put("Tokyo", "Japan"); |
| 39 | + capitalCountryMap.put("Berlin", "Germany"); |
| 40 | + capitalCountryMap.put("Cape Town", "South Africa"); |
| 41 | + capitalCountryMap.put("Pretoria", "South Africa"); |
| 42 | + capitalCountryMap.put("Bloemfontein", "South Africa"); |
| 43 | + |
| 44 | + assertEquals(new HashSet<String>(Arrays.asList( |
| 45 | + new String[] {"Cape Town", "Pretoria", "Bloemfontein"})), |
| 46 | + MapUtil.getKeys(capitalCountryMap, "South Africa")); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void whenUsingFunctionalWayForSingleKey_shouldReturnSingleKey() { |
| 51 | + Map<String, String> capitalCountryMap = new HashMap<>(); |
| 52 | + capitalCountryMap.put("Tokyo", "Japan"); |
| 53 | + capitalCountryMap.put("Berlin", "Germany"); |
| 54 | + assertEquals("Berlin", MapUtil.keys(capitalCountryMap, "Germany").findFirst().get()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void whenUsingFunctionalWayForAllKeys_shouldReturnAllKeys() { |
| 59 | + Map<String, String> capitalCountryMap = new HashMap<>(); |
| 60 | + capitalCountryMap.put("Tokyo", "Japan"); |
| 61 | + capitalCountryMap.put("Berlin", "Germany"); |
| 62 | + capitalCountryMap.put("Cape Town", "South Africa"); |
| 63 | + capitalCountryMap.put("Pretoria", "South Africa"); |
| 64 | + capitalCountryMap.put("Bloemfontein", "South Africa"); |
| 65 | + assertEquals(new HashSet<String>(Arrays.asList( |
| 66 | + new String[] {"Cape Town", "Pretoria", "Bloemfontein"})), |
| 67 | + MapUtil.keys(capitalCountryMap, "South Africa").collect(Collectors.toSet())); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void whenUsingBidiMap_shouldReturnKey() { |
| 72 | + BidiMap<String, String> capitalCountryMap = new DualHashBidiMap<String, String>(); |
| 73 | + capitalCountryMap.put("Berlin", "Germany"); |
| 74 | + capitalCountryMap.put("Cape Town", "South Africa"); |
| 75 | + assertEquals("Berlin", capitalCountryMap.getKey("Germany")); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void whenUsingBidiMapAddDuplicateValue_shouldRemoveOldEntry() { |
| 80 | + BidiMap<String, String> capitalCountryMap = new DualHashBidiMap<String, String>(); |
| 81 | + capitalCountryMap.put("Berlin", "Germany"); |
| 82 | + capitalCountryMap.put("Cape Town", "South Africa"); |
| 83 | + capitalCountryMap.put("Pretoria", "South Africa"); |
| 84 | + assertEquals("Pretoria", capitalCountryMap.getKey("South Africa")); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void whenUsingBiMap_shouldReturnKey() { |
| 89 | + HashBiMap<String, String> capitalCountryMap = HashBiMap.create(); |
| 90 | + capitalCountryMap.put("Berlin", "Germany"); |
| 91 | + capitalCountryMap.put("Cape Town", "South Africa"); |
| 92 | + assertEquals("Berlin", capitalCountryMap.inverse().get("Germany")); |
| 93 | + } |
| 94 | + |
| 95 | + @Test(expected=IllegalArgumentException.class) |
| 96 | + public void whenUsingBiMapAddDuplicateValue_shouldThrowException() { |
| 97 | + HashBiMap<String, String> capitalCountryMap = HashBiMap.create(); |
| 98 | + capitalCountryMap.put("Berlin", "Germany"); |
| 99 | + capitalCountryMap.put("Cape Town", "South Africa"); |
| 100 | + capitalCountryMap.put("Pretoria", "South Africa"); |
| 101 | + assertEquals("Berlin", capitalCountryMap.inverse().get("Germany")); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments