From a41309f4c65af08cf0bd609388d2bb59810808f1 Mon Sep 17 00:00:00 2001 From: Mateusz Szablak 'Saber-k Date: Tue, 19 Jan 2021 21:40:10 +0100 Subject: [PATCH] [BAEL-4214] Converting java.util.Properties to HashMap --- .../core-java-collections-maps-3/pom.xml | 4 + .../PropertiesToHashMapConverter.java | 39 ++++ .../PropertiesToHashMapConverterUnitTest.java | 192 ++++++++++++++++++ .../src/test/resources/toHashMap.properties | 3 + 4 files changed, 238 insertions(+) create mode 100644 core-java-modules/core-java-collections-maps-3/src/main/java/com/baeldung/map/propertieshashmap/PropertiesToHashMapConverter.java create mode 100644 core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/propertieshashmap/PropertiesToHashMapConverterUnitTest.java create mode 100644 core-java-modules/core-java-collections-maps-3/src/test/resources/toHashMap.properties diff --git a/core-java-modules/core-java-collections-maps-3/pom.xml b/core-java-modules/core-java-collections-maps-3/pom.xml index 577ad58255..2561f891f1 100644 --- a/core-java-modules/core-java-collections-maps-3/pom.xml +++ b/core-java-modules/core-java-collections-maps-3/pom.xml @@ -20,6 +20,10 @@ jmh-core ${jmh-core.version} + + com.google.guava + guava + diff --git a/core-java-modules/core-java-collections-maps-3/src/main/java/com/baeldung/map/propertieshashmap/PropertiesToHashMapConverter.java b/core-java-modules/core-java-collections-maps-3/src/main/java/com/baeldung/map/propertieshashmap/PropertiesToHashMapConverter.java new file mode 100644 index 0000000000..2f333638a9 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-3/src/main/java/com/baeldung/map/propertieshashmap/PropertiesToHashMapConverter.java @@ -0,0 +1,39 @@ +package com.baeldung.map.propertieshashmap; + +import com.google.common.collect.Maps; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.stream.Collectors; + +public class PropertiesToHashMapConverter { + + @SuppressWarnings({"rawtypes", "unchecked"}) + public static HashMap typeCastConvert(Properties prop) { + Map step1 = prop; + Map step2 = (Map) step1; + return new HashMap<>(step2); + } + + public static HashMap loopConvert(Properties prop) { + HashMap retMap = new HashMap<>(); + for (Map.Entry entry : prop.entrySet()) { + retMap.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue())); + } + return retMap; + } + + public static HashMap streamConvert(Properties prop) { + return prop.entrySet().stream().collect( + Collectors.toMap( + e -> String.valueOf(e.getKey()), + e -> String.valueOf(e.getValue()), + (prev, next) -> next, HashMap::new + )); + } + + public static HashMap guavaConvert(Properties prop) { + return Maps.newHashMap(Maps.fromProperties(prop)); + } +} diff --git a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/propertieshashmap/PropertiesToHashMapConverterUnitTest.java b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/propertieshashmap/PropertiesToHashMapConverterUnitTest.java new file mode 100644 index 0000000000..b1370dcfbf --- /dev/null +++ b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/propertieshashmap/PropertiesToHashMapConverterUnitTest.java @@ -0,0 +1,192 @@ +package com.baeldung.map.propertieshashmap; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.*; + +class PropertiesToHashMapConverterUnitTest { + + private Properties properties; + + private final static String propertyFileName = "toHashMap.properties"; + + @BeforeEach + public void setup() throws IOException { + properties = new Properties(); + try (InputStream is = getClass().getClassLoader().getResourceAsStream(propertyFileName)) { + if (is != null) { + properties.load(is); + } + } + } + + @Test + public void havingPropertiesLoaded_whenCheck_thenEquals() { + assertEquals(3, properties.size()); + assertEquals("str_value", properties.get("property1")); + assertEquals("123", properties.get("property2")); + assertEquals("", properties.get("property3")); + } + + @Test + public void whenPropertiesModified_thenTypeSafeIssues() { + compromiseProperties(properties); + + assertEquals(5, properties.size()); + + assertNull(properties.getProperty("property4")); + assertNotEquals(String.class, properties.get("property4").getClass()); + assertEquals(456, properties.get("property4")); + + + assertNull(properties.getProperty("5")); + assertNotEquals(String.class, properties.get(5).getClass()); + assertEquals(10.11, properties.get(5)); + } + + @Test + public void havingNonModifiedProperties_whenTypeCastConvert_thenNoTypeSafeIssues() { + HashMap hMap = PropertiesToHashMapConverter.typeCastConvert(properties); + + assertEquals(3, hMap.size()); + assertEquals(String.class, hMap.get("property1").getClass()); + assertEquals(properties.get("property1"), hMap.get("property1")); + assertEquals(String.class, hMap.get("property2").getClass()); + assertEquals(properties.get("property2"), hMap.get("property2")); + assertEquals(String.class, hMap.get("property3").getClass()); + assertEquals(properties.get("property3"), hMap.get("property3")); + } + + @Test + public void havingModifiedProperties_whenTypeCastConvert_thenClassCastException() { + compromiseProperties(properties); + HashMap hMap = PropertiesToHashMapConverter.typeCastConvert(properties); + assertEquals(5, hMap.size()); + + assertThrows(ClassCastException.class, () -> { + String s = hMap.get("property4"); + }); + assertEquals(Integer.class, ((Object) hMap.get("property4")).getClass()); + + assertNull(hMap.get("5")); + assertNotNull(hMap.get(5)); + assertThrows(ClassCastException.class, () -> { + String s = hMap.get(5); + }); + assertEquals(Double.class, ((Object) hMap.get(5)).getClass()); + } + + @Test + public void havingNonModifiedProperties_whenLoopConvert_thenNoTypeSafeIssues() { + HashMap hMap = PropertiesToHashMapConverter.loopConvert(properties); + + assertEquals(3, hMap.size()); + assertEquals(String.class, hMap.get("property1").getClass()); + assertEquals(properties.get("property1"), hMap.get("property1")); + assertEquals(String.class, hMap.get("property2").getClass()); + assertEquals(properties.get("property2"), hMap.get("property2")); + assertEquals(String.class, hMap.get("property3").getClass()); + assertEquals(properties.get("property3"), hMap.get("property3")); + } + + @Test + public void havingModifiedProperties_whenLoopConvert_thenNoClassCastException() { + compromiseProperties(properties); + HashMap hMap = PropertiesToHashMapConverter.loopConvert(properties); + assertEquals(5, hMap.size()); + + assertDoesNotThrow(() -> { + String s = hMap.get("property4"); + }); + assertEquals(String.class, hMap.get("property4").getClass()); + assertEquals("456", hMap.get("property4")); + + assertDoesNotThrow(() -> { + String s = hMap.get("5"); + }); + assertEquals("10.11", hMap.get("5")); + } + + @Test + public void havingNonModifiedProperties_whenStreamConvert_thenNoTypeSafeIssues() { + HashMap hMap = PropertiesToHashMapConverter.streamConvert(properties); + + assertEquals(3, hMap.size()); + assertEquals(String.class, hMap.get("property1").getClass()); + assertEquals(properties.get("property1"), hMap.get("property1")); + assertEquals(String.class, hMap.get("property2").getClass()); + assertEquals(properties.get("property2"), hMap.get("property2")); + assertEquals(String.class, hMap.get("property3").getClass()); + assertEquals(properties.get("property3"), hMap.get("property3")); + } + + @Test + public void havingModifiedProperties_whenStreamConvert_thenNoClassCastException() { + compromiseProperties(properties); + HashMap hMap = PropertiesToHashMapConverter.streamConvert(properties); + assertEquals(5, hMap.size()); + + assertDoesNotThrow(() -> { + String s = hMap.get("property4"); + }); + assertEquals(String.class, hMap.get("property4").getClass()); + assertEquals("456", hMap.get("property4")); + + assertDoesNotThrow(() -> { + String s = hMap.get("5"); + }); + assertEquals("10.11", hMap.get("5")); + } + + @Test + public void havingModifiedProperties_whenLoopConvertAndStreamConvert_thenHashMapsSame() { + compromiseProperties(properties); + HashMap hMap1 = PropertiesToHashMapConverter.loopConvert(properties); + HashMap hMap2 = PropertiesToHashMapConverter.streamConvert(properties); + + assertEquals(hMap2, hMap1); + } + + @Test + public void havingNonModifiedProperties_whenGuavaConvert_thenNoTypeSafeIssues() { + HashMap hMap = PropertiesToHashMapConverter.guavaConvert(properties); + + assertEquals(3, hMap.size()); + assertEquals(String.class, hMap.get("property1").getClass()); + assertEquals(properties.get("property1"), hMap.get("property1")); + assertEquals(String.class, hMap.get("property2").getClass()); + assertEquals(properties.get("property2"), hMap.get("property2")); + assertEquals(String.class, hMap.get("property3").getClass()); + assertEquals(properties.get("property3"), hMap.get("property3")); + } + + @Test + public void havingModifiedProperties_whenGuavaConvert_thenUnableToConvertAndThrowException() { + compromiseProperties(properties); + assertThrows(Exception.class, () -> PropertiesToHashMapConverter.guavaConvert(properties)); + } + + @Test + public void havingModifiedPropertiesWithNoIntegerValue_whenGuavaConvert_thenNullPointerException() { + properties.put("property4", 456); + assertThrows(NullPointerException.class, () -> PropertiesToHashMapConverter.guavaConvert(properties)); + } + + @Test + public void havingModifiedPropertiesWithNoIntegerKey_whenGuavaConvert_thenClassCastException() { + properties.put(5, 10.11); + assertThrows(ClassCastException.class, () -> PropertiesToHashMapConverter.guavaConvert(properties)); + } + + + private void compromiseProperties(Properties prop) { + prop.put("property4", 456); + prop.put(5, 10.11); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-maps-3/src/test/resources/toHashMap.properties b/core-java-modules/core-java-collections-maps-3/src/test/resources/toHashMap.properties new file mode 100644 index 0000000000..b731aa94bb --- /dev/null +++ b/core-java-modules/core-java-collections-maps-3/src/test/resources/toHashMap.properties @@ -0,0 +1,3 @@ +property1=str_value +property2=123 +property3= \ No newline at end of file