From 4d330a1b8514003d81f7f5df402aaf8341aa0bf7 Mon Sep 17 00:00:00 2001 From: Kilian Schneider <48420258+Basler182@users.noreply.github.com> Date: Tue, 29 Nov 2022 03:33:35 +0100 Subject: [PATCH] BAEL-5785 Map.of() vs Map.ofEntries() test (#13064) * BAEL-5785 Map.of() vs Map.ofEntries() test * Update pom.xml * Update pom.xml * Add immutable test case * Update map naming --- .../core-java-collections-maps-5/pom.xml | 12 +++++ .../MapOfEntriesVsMapOfUnitTest.java | 52 +++++++++++++++++++ core-java-modules/pom.xml | 1 - pom.xml | 1 + 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-collections-maps-5/src/test/java/com/baeldung/map/mapofvsmapofentries/MapOfEntriesVsMapOfUnitTest.java diff --git a/core-java-modules/core-java-collections-maps-5/pom.xml b/core-java-modules/core-java-collections-maps-5/pom.xml index ba7083e42e..516a0de4be 100644 --- a/core-java-modules/core-java-collections-maps-5/pom.xml +++ b/core-java-modules/core-java-collections-maps-5/pom.xml @@ -5,6 +5,18 @@ 4.0.0 core-java-collections-maps-5 0.1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 9 + 9 + + + + core-java-collections-maps-5 jar diff --git a/core-java-modules/core-java-collections-maps-5/src/test/java/com/baeldung/map/mapofvsmapofentries/MapOfEntriesVsMapOfUnitTest.java b/core-java-modules/core-java-collections-maps-5/src/test/java/com/baeldung/map/mapofvsmapofentries/MapOfEntriesVsMapOfUnitTest.java new file mode 100644 index 0000000000..f8a8e33139 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-5/src/test/java/com/baeldung/map/mapofvsmapofentries/MapOfEntriesVsMapOfUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.map.mapofvsmapofentries; + +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class MapOfEntriesVsMapOfUnitTest { + + @Test + void mapOf() { + // Use Map.of() to create an empty immutable map + Map map = Map.of(); + assertNotNull(map); + + // Use Map.of() to create an immutable map with one entry + Map mapWithEntry = Map.of(1L, "value1"); + assertNotNull(mapWithEntry); + assertThat(mapWithEntry.size()).isEqualTo(1); + assertThat(mapWithEntry.get(1L)).isEqualTo("value1"); + + // Test if map is immutable + try { + mapWithEntry.put(2L, "value2"); + } catch (UnsupportedOperationException e) { + assertThat(e).isInstanceOf(UnsupportedOperationException.class); + } + } + + @Test + void mapOfEntries() { + // Use Map.ofEntries() to create an empty immutable map + Map map = Map.ofEntries(); + assertNotNull(map); + + // Use Map.ofEntries() to create an immutable map with two entries. + Map longUserMap = Map.ofEntries(Map.entry(1L, "User A"), Map.entry(2L, "User B")); + assertNotNull(longUserMap); + assertThat(longUserMap.size()).isEqualTo(2); + assertThat(longUserMap.get(1L)).isEqualTo("User A"); + assertThat(longUserMap.get(2L)).isEqualTo("User B"); + + // Test if map is immutable + try { + longUserMap.put(3L, "User C"); + } catch (UnsupportedOperationException e) { + assertThat(e).isInstanceOf(UnsupportedOperationException.class); + } + } +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 74bf4c662c..53ad13a3c0 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -41,7 +41,6 @@ core-java-collections-maps core-java-collections-maps-2 core-java-collections-maps-3 - core-java-collections-maps-5 core-java-concurrency-2 core-java-concurrency-advanced core-java-concurrency-advanced-2 diff --git a/pom.xml b/pom.xml index adbccc6612..f51cdb4c73 100644 --- a/pom.xml +++ b/pom.xml @@ -1129,6 +1129,7 @@ core-java-modules/core-java-collections-set core-java-modules/core-java-collections-list-4 core-java-modules/core-java-collections-maps-4 + core-java-modules/core-java-collections-maps-5 core-java-modules/core-java-concurrency-simple core-java-modules/core-java-date-operations-1 core-java-modules/core-java-datetime-conversion