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
This commit is contained in:
parent
28c0b9569e
commit
4d330a1b85
|
@ -5,6 +5,18 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-collections-maps-5</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>9</source>
|
||||
<target>9</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<name>core-java-collections-maps-5</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
|
|
@ -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<Long, String> map = Map.of();
|
||||
assertNotNull(map);
|
||||
|
||||
// Use Map.of() to create an immutable map with one entry
|
||||
Map<Long, String> 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<Long, String> map = Map.ofEntries();
|
||||
assertNotNull(map);
|
||||
|
||||
// Use Map.ofEntries() to create an immutable map with two entries.
|
||||
Map<Long, String> 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -41,7 +41,6 @@
|
|||
<module>core-java-collections-maps</module>
|
||||
<module>core-java-collections-maps-2</module>
|
||||
<module>core-java-collections-maps-3</module>
|
||||
<module>core-java-collections-maps-5</module>
|
||||
<module>core-java-concurrency-2</module>
|
||||
<module>core-java-concurrency-advanced</module>
|
||||
<module>core-java-concurrency-advanced-2</module>
|
||||
|
|
1
pom.xml
1
pom.xml
|
@ -1129,6 +1129,7 @@
|
|||
<module>core-java-modules/core-java-collections-set</module>
|
||||
<module>core-java-modules/core-java-collections-list-4</module>
|
||||
<module>core-java-modules/core-java-collections-maps-4</module>
|
||||
<module>core-java-modules/core-java-collections-maps-5</module>
|
||||
<module>core-java-modules/core-java-concurrency-simple</module>
|
||||
<module>core-java-modules/core-java-date-operations-1</module>
|
||||
<module>core-java-modules/core-java-datetime-conversion</module>
|
||||
|
|
Loading…
Reference in New Issue