From 9ff3b1dd9bca962b40889d2c2dbd400b064ebcad Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Tue, 6 Jun 2023 03:43:28 +0200 Subject: [PATCH] [map-str-obj-to-str-str] converting a Map to a Map. (#14175) --- .../ConvertMapWithTypeParamUnitTest.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/map/generictypeconversion/ConvertMapWithTypeParamUnitTest.java diff --git a/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/map/generictypeconversion/ConvertMapWithTypeParamUnitTest.java b/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/map/generictypeconversion/ConvertMapWithTypeParamUnitTest.java new file mode 100644 index 0000000000..9548abd622 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/map/generictypeconversion/ConvertMapWithTypeParamUnitTest.java @@ -0,0 +1,92 @@ +package com.baeldung.map.generictypeconversion; + +import static java.util.stream.Collectors.toMap; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.math.BigDecimal; +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import com.google.common.collect.Maps; + +public class ConvertMapWithTypeParamUnitTest { + private static final Map MAP1 = Maps.newHashMap(); + + static { + MAP1.put("K01", "GNU Linux"); + MAP1.put("K02", "Mac OS"); + MAP1.put("K03", "MS Windows"); + } + + private static final Map EXPECTED_MAP1 = Maps.newHashMap(); + + static { + EXPECTED_MAP1.put("K01", "GNU Linux"); + EXPECTED_MAP1.put("K02", "Mac OS"); + EXPECTED_MAP1.put("K03", "MS Windows"); + } + + private static final Map MAP2 = Maps.newHashMap(); + + static { + MAP2.put("K01", "GNU Linux"); + MAP2.put("K02", "Mac OS"); + MAP2.put("K03", BigDecimal.ONE); + } + + private static final Map EXPECTED_MAP2_STRING_VALUES = Maps.newHashMap(); + + static { + EXPECTED_MAP2_STRING_VALUES.put("K01", "GNU Linux"); + EXPECTED_MAP2_STRING_VALUES.put("K02", "Mac OS"); + EXPECTED_MAP2_STRING_VALUES.put("K03", "1"); + } + + @Test + void whenCastingToMap_shouldGetExpectedResult() { + Map result = (Map) MAP1; + assertEquals(EXPECTED_MAP1, result); + + Map result2 = (Map) MAP2; + assertFalse(result2.get("K03") instanceof String); + } + + Map checkAndTransform(Map inputMap) { + Map result = new HashMap<>(); + for (Map.Entry entry : inputMap.entrySet()) { + try { + result.put(entry.getKey(), (String) entry.getValue()); + } catch (ClassCastException e) { + throw e; + } + } + return result; + } + + @Test + void whenCheckAndTransform_shouldGetExpectedResult() { + Map result = checkAndTransform(MAP1); + assertEquals(EXPECTED_MAP1, result); + + assertThrows(ClassCastException.class, () -> checkAndTransform(MAP2)); + } + + @Test + void whenUsingStringValueOf_shouldGetExpectedResult() { + Map result = MAP1.entrySet() + .stream() + .collect(toMap(Map.Entry::getKey, e -> String.valueOf(e.getValue()))); + + assertEquals(EXPECTED_MAP1, result); + + Map result2 = MAP2.entrySet() + .stream() + .collect(toMap(Map.Entry::getKey, e -> String.valueOf(e.getValue()))); + + assertEquals(EXPECTED_MAP2_STRING_VALUES, result2); + } +} \ No newline at end of file