Skip to content

Commit fe49903

Browse files
authored
BAEL-5785 Map.of() vs Map.ofEntries() test (eugenp#13064)
* BAEL-5785 Map.of() vs Map.ofEntries() test * Update pom.xml * Update pom.xml * Add immutable test case * Update map naming
1 parent 9a6015b commit fe49903

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

core-java-modules/core-java-collections-maps-5/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>core-java-collections-maps-5</artifactId>
77
<version>0.1.0-SNAPSHOT</version>
8+
<build>
9+
<plugins>
10+
<plugin>
11+
<groupId>org.apache.maven.plugins</groupId>
12+
<artifactId>maven-compiler-plugin</artifactId>
13+
<configuration>
14+
<source>9</source>
15+
<target>9</target>
16+
</configuration>
17+
</plugin>
18+
</plugins>
19+
</build>
820
<name>core-java-collections-maps-5</name>
921
<packaging>jar</packaging>
1022

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.map.mapofvsmapofentries;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Map;
6+
7+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
9+
10+
public class MapOfEntriesVsMapOfUnitTest {
11+
12+
@Test
13+
void mapOf() {
14+
// Use Map.of() to create an empty immutable map
15+
Map<Long, String> map = Map.of();
16+
assertNotNull(map);
17+
18+
// Use Map.of() to create an immutable map with one entry
19+
Map<Long, String> mapWithEntry = Map.of(1L, "value1");
20+
assertNotNull(mapWithEntry);
21+
assertThat(mapWithEntry.size()).isEqualTo(1);
22+
assertThat(mapWithEntry.get(1L)).isEqualTo("value1");
23+
24+
// Test if map is immutable
25+
try {
26+
mapWithEntry.put(2L, "value2");
27+
} catch (UnsupportedOperationException e) {
28+
assertThat(e).isInstanceOf(UnsupportedOperationException.class);
29+
}
30+
}
31+
32+
@Test
33+
void mapOfEntries() {
34+
// Use Map.ofEntries() to create an empty immutable map
35+
Map<Long, String> map = Map.ofEntries();
36+
assertNotNull(map);
37+
38+
// Use Map.ofEntries() to create an immutable map with two entries.
39+
Map<Long, String> longUserMap = Map.ofEntries(Map.entry(1L, "User A"), Map.entry(2L, "User B"));
40+
assertNotNull(longUserMap);
41+
assertThat(longUserMap.size()).isEqualTo(2);
42+
assertThat(longUserMap.get(1L)).isEqualTo("User A");
43+
assertThat(longUserMap.get(2L)).isEqualTo("User B");
44+
45+
// Test if map is immutable
46+
try {
47+
longUserMap.put(3L, "User C");
48+
} catch (UnsupportedOperationException e) {
49+
assertThat(e).isInstanceOf(UnsupportedOperationException.class);
50+
}
51+
}
52+
}

core-java-modules/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<module>core-java-collections-maps</module>
4242
<module>core-java-collections-maps-2</module>
4343
<module>core-java-collections-maps-3</module>
44-
<module>core-java-collections-maps-5</module>
4544
<module>core-java-concurrency-2</module>
4645
<module>core-java-concurrency-advanced</module>
4746
<module>core-java-concurrency-advanced-2</module>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,7 @@
11291129
<module>core-java-modules/core-java-collections-set</module>
11301130
<module>core-java-modules/core-java-collections-list-4</module>
11311131
<module>core-java-modules/core-java-collections-maps-4</module>
1132+
<module>core-java-modules/core-java-collections-maps-5</module>
11321133
<module>core-java-modules/core-java-concurrency-simple</module>
11331134
<module>core-java-modules/core-java-date-operations-1</module>
11341135
<module>core-java-modules/core-java-datetime-conversion</module>

0 commit comments

Comments
 (0)