Skip to content

Commit

Permalink
Use Map.of wherever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsloan committed May 21, 2024
1 parent f488266 commit 5bb63d6
Showing 1 changed file with 41 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ class MapUtilsTest {

@Test
void testCastMap_ValidStringMap_ReturnsTypedMap() {
Map<Object, Object> rawMap = new HashMap<>();
rawMap.put("key1", "value1");
rawMap.put("key2", "value2");
Map<Object, Object> rawMap =
Map.of(
"key1", "value1",
"key2", "value2"
);

Map<String, String> typedMap = MapUtils.castMap(rawMap, String.class, String.class);

Expand All @@ -38,20 +40,22 @@ void testCastMap_ValidStringMap_ReturnsTypedMap() {

@Test
void testCastMap_NonStringKeyOrValue_ThrowsException() {
Map<Object, Object> rawMap = new HashMap<>();
rawMap.put("key1", "value1");
rawMap.put(123, "value2"); // Non-String key
Map<Object, Object> rawMap =
Map.of(
"key1", "value1",
123, "value2" // Non-String key
);

assertThrows(IllegalArgumentException.class, () -> {
MapUtils.castMap(rawMap, String.class, String.class);
});
assertThrows(IllegalArgumentException.class, () -> MapUtils.castMap(rawMap, String.class, String.class));
}

@Test
void testCastMap_ValidIntegerMap_ReturnsTypedMap() {
Map<Object, Object> rawMap = new HashMap<>();
rawMap.put(10, 99);
rawMap.put(20, 145);
Map<Object, Object> rawMap =
Map.of(
10, 99,
20, 145
);

Map<Integer, Integer> typedMap = MapUtils.castMap(rawMap, Integer.class, Integer.class);

Expand All @@ -61,9 +65,11 @@ void testCastMap_ValidIntegerMap_ReturnsTypedMap() {

@Test
void testCastMap_NonIntegerKeyOrValue_ThrowsException() {
Map<Object, Object> rawMap = new HashMap<>();
rawMap.put(10, 99);
rawMap.put(20, "pigeons"); // Non-Integer value
Map<Object, Object> rawMap =
Map.of(
10, 99,
20, "pigeons" // Non-Integer value
);

assertThrows(IllegalArgumentException.class, () -> {
MapUtils.castMap(rawMap, Integer.class, Integer.class);
Expand All @@ -84,9 +90,11 @@ void testCastMap_WithNullValues_ShouldHandleGracefully() {

@Test
void testCastMap_SuperclassTypeCompatibility_ShouldPass() {
Map<Object, Object> rawMap = new HashMap<>();
rawMap.put("key1", "value1");
rawMap.put("key2", "value2");
Map<Object, Object> rawMap =
Map.of(
"key1", "value1",
"key2", "value2"
);

// Cast to Map<CharSequence, CharSequence> since String implements CharSequence
Map<CharSequence, CharSequence> typedMap = MapUtils.castMap(rawMap, CharSequence.class, CharSequence.class);
Expand All @@ -97,9 +105,11 @@ void testCastMap_SuperclassTypeCompatibility_ShouldPass() {

@Test
void testCastMap_InterfaceTypeCompatibility_ShouldPass() {
Map<Object, Object> rawMap = new HashMap<>();
rawMap.put("key1", "value1");
rawMap.put("key2", "value2");
Map<Object, Object> rawMap =
Map.of(
"key1", "value1",
"key2", "value2"
);

// Cast to Map<Object, Object> since String is an Object
Map<Object, Object> typedMap = MapUtils.castMap(rawMap, Object.class, Object.class);
Expand All @@ -110,9 +120,11 @@ void testCastMap_InterfaceTypeCompatibility_ShouldPass() {

@Test
void testCastMap_StringToObject_ShouldPass() {
Map<Object, Object> rawMap = new HashMap<>();
rawMap.put("key1", "value1");
rawMap.put("key2", "value2");
Map<Object, Object> rawMap =
Map.of(
"key1", "value1",
"key2", "value2"
);

// Cast to Map<Object, Object> since String is an Object
Map<Object, Object> typedMap = MapUtils.castMap(rawMap, Object.class, Object.class);
Expand All @@ -123,9 +135,11 @@ void testCastMap_StringToObject_ShouldPass() {

@Test
void testCastMap_StringToCharSequence_ShouldPass() {
Map<Object, Object> rawMap = new HashMap<>();
rawMap.put("key1", "value1");
rawMap.put("key2", "value2");
Map<Object, Object> rawMap =
Map.of(
"key1", "value1",
"key2", "value2"
);

// Cast to Map<CharSequence, CharSequence> since String implements CharSequence
Map<CharSequence, CharSequence> typedMap = MapUtils.castMap(rawMap, CharSequence.class, CharSequence.class);
Expand Down

0 comments on commit 5bb63d6

Please sign in to comment.