This commit is contained in:
Looly
2024-12-26 18:24:50 +08:00
parent 629f7d4915
commit 46fa4a1295

View File

@@ -7,7 +7,6 @@ import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.*; import java.util.*;
@@ -260,6 +259,7 @@ public class MapUtilTest {
Map<String, String> result = MapUtil.renameKey(map, "oldKey", "newKey"); Map<String, String> result = MapUtil.renameKey(map, "oldKey", "newKey");
assertTrue(result.isEmpty()); assertTrue(result.isEmpty());
} }
@Test @Test
public void renameKeyOldKeyNotPresentNoChange() { public void renameKeyOldKeyNotPresentNoChange() {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
@@ -292,11 +292,13 @@ public class MapUtilTest {
public void issue3162Test() { public void issue3162Test() {
final Map<String, Object> map = new HashMap<String, Object>() { final Map<String, Object> map = new HashMap<String, Object>() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
{ {
put("a", "1"); put("a", "1");
put("b", "2"); put("b", "2");
put("c", "3"); put("c", "3");
}}; }
};
final Map<String, Object> filtered = MapUtil.filter(map, "a", "b"); final Map<String, Object> filtered = MapUtil.filter(map, "a", "b");
assertEquals(2, filtered.size()); assertEquals(2, filtered.size());
assertEquals("1", filtered.get("a")); assertEquals("1", filtered.get("a"));
@@ -502,7 +504,7 @@ public class MapUtilTest {
map.put("a", "1"); map.put("a", "1");
map.put("b", "2"); map.put("b", "2");
map.put("c", "3"); map.put("c", "3");
Iterator<String> emptyIterator = new ArrayList<String>().iterator(); Iterator<String> emptyIterator = Collections.emptyIterator();
ArrayList<String> result = MapUtil.valuesOfKeys(map, emptyIterator); ArrayList<String> result = MapUtil.valuesOfKeys(map, emptyIterator);
assertEquals(new ArrayList<String>(), result); assertEquals(new ArrayList<String>(), result);
} }
@@ -513,15 +515,23 @@ public class MapUtilTest {
map.put("a", "1"); map.put("a", "1");
map.put("b", "2"); map.put("b", "2");
map.put("c", "3"); map.put("c", "3");
Iterator<String> iterator = new ArrayList<String>() {{ Iterator<String> iterator = new ArrayList<String>() {
private static final long serialVersionUID = -4593258366224032110L;
{
add("a"); add("a");
add("b"); add("b");
}}.iterator(); }
}.iterator();
ArrayList<String> result = MapUtil.valuesOfKeys(map, iterator); ArrayList<String> result = MapUtil.valuesOfKeys(map, iterator);
assertEquals(new ArrayList<String>() {{ assertEquals(new ArrayList<String>() {
private static final long serialVersionUID = 7218152799308667271L;
{
add("1"); add("1");
add("2"); add("2");
}}, result); }
}, result);
} }
@Test @Test
@@ -530,15 +540,23 @@ public class MapUtilTest {
map.put("a", "1"); map.put("a", "1");
map.put("b", "2"); map.put("b", "2");
map.put("c", "3"); map.put("c", "3");
Iterator<String> iterator = new ArrayList<String>() {{ Iterator<String> iterator = new ArrayList<String>() {
private static final long serialVersionUID = -5479427021989481058L;
{
add("d"); add("d");
add("e"); add("e");
}}.iterator(); }
}.iterator();
ArrayList<String> result = MapUtil.valuesOfKeys(map, iterator); ArrayList<String> result = MapUtil.valuesOfKeys(map, iterator);
assertEquals(new ArrayList<String>() {{ assertEquals(new ArrayList<String>() {
private static final long serialVersionUID = 4390715387901549136L;
{
add(null); add(null);
add(null); add(null);
}}, result); }
}, result);
} }
@Test @Test
@@ -547,17 +565,24 @@ public class MapUtilTest {
map.put("a", "1"); map.put("a", "1");
map.put("b", "2"); map.put("b", "2");
map.put("c", "3"); map.put("c", "3");
Iterator<String> iterator = new ArrayList<String>() {{ Iterator<String> iterator = new ArrayList<String>() {
private static final long serialVersionUID = 8510595063492828968L;
{
add("a"); add("a");
add("d"); add("d");
add("b"); add("b");
}}.iterator(); }
}.iterator();
ArrayList<String> result = MapUtil.valuesOfKeys(map, iterator); ArrayList<String> result = MapUtil.valuesOfKeys(map, iterator);
assertEquals(new ArrayList<String>() {{ assertEquals(new ArrayList<String>() {
private static final long serialVersionUID = 6383576410597048337L;
{
add("1"); add("1");
add(null); add(null);
add("2"); add("2");
}}, result); }
}, result);
} }
//--------clear //--------clear
@@ -712,7 +737,8 @@ public class MapUtilTest {
//------getQuietly //------getQuietly
@Test @Test
public void getQuietlyMapIsNullReturnsDefaultValue() { public void getQuietlyMapIsNullReturnsDefaultValue() {
String result = MapUtil.getQuietly(null, "key1", new TypeReference<String>() {}, "default"); String result = MapUtil.getQuietly(null, "key1", new TypeReference<String>() {
}, "default");
assertEquals("default", result); assertEquals("default", result);
result = MapUtil.getQuietly(null, "key1", String.class, "default"); result = MapUtil.getQuietly(null, "key1", String.class, "default");
assertEquals("default", result); assertEquals("default", result);
@@ -723,7 +749,8 @@ public class MapUtilTest {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("key1", "value1"); map.put("key1", "value1");
map.put("key2", 123); map.put("key2", 123);
String result = MapUtil.getQuietly(map, "key1", new TypeReference<String>() {}, "default"); String result = MapUtil.getQuietly(map, "key1", new TypeReference<String>() {
}, "default");
assertEquals("value1", result); assertEquals("value1", result);
} }
@@ -732,7 +759,8 @@ public class MapUtilTest {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("key1", "value1"); map.put("key1", "value1");
map.put("key2", 123); map.put("key2", 123);
String result = MapUtil.getQuietly(map, "key3", new TypeReference<String>() {}, "default"); String result = MapUtil.getQuietly(map, "key3", new TypeReference<String>() {
}, "default");
assertEquals("default", result); assertEquals("default", result);
} }
@@ -741,7 +769,8 @@ public class MapUtilTest {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("key1", "value1"); map.put("key1", "value1");
map.put("key2", 123); map.put("key2", 123);
Integer result = MapUtil.getQuietly(map, "key1", new TypeReference<Integer>() {}, 0); Integer result = MapUtil.getQuietly(map, "key1", new TypeReference<Integer>() {
}, 0);
assertEquals(0, result); assertEquals(0, result);
} }
@@ -750,7 +779,8 @@ public class MapUtilTest {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("key1", "value1"); map.put("key1", "value1");
map.put("key2", 123); map.put("key2", 123);
Integer result = MapUtil.getQuietly(map, "key2", new TypeReference<Integer>() {}, 0); Integer result = MapUtil.getQuietly(map, "key2", new TypeReference<Integer>() {
}, 0);
assertEquals(123, result); assertEquals(123, result);
} }
@@ -760,7 +790,8 @@ public class MapUtilTest {
map.put("key1", "value1"); map.put("key1", "value1");
map.put("key2", 123); map.put("key2", 123);
map.put("key3", null); map.put("key3", null);
String result = MapUtil.getQuietly(map, "key3", new TypeReference<String>() {}, "default"); String result = MapUtil.getQuietly(map, "key3", new TypeReference<String>() {
}, "default");
assertEquals("default", result); assertEquals("default", result);
} }
@@ -806,7 +837,8 @@ public class MapUtilTest {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("age", "18"); map.put("age", "18");
map.put("name", "Hutool"); map.put("name", "Hutool");
assertEquals("18", MapUtil.get(map, "age", new TypeReference<String>() {})); assertEquals("18", MapUtil.get(map, "age", new TypeReference<String>() {
}));
} }
@Test @Test
@@ -814,7 +846,8 @@ public class MapUtilTest {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("age", "18"); map.put("age", "18");
map.put("name", "Hutool"); map.put("name", "Hutool");
assertEquals("18", MapUtil.get(map, "age", new TypeReference<String>() {}, "default")); assertEquals("18", MapUtil.get(map, "age", new TypeReference<String>() {
}, "default"));
} }
@Test @Test
@@ -822,9 +855,11 @@ public class MapUtilTest {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("age", "18"); map.put("age", "18");
map.put("name", "Hutool"); map.put("name", "Hutool");
assertEquals(18, MapUtil.get(map, "age", new TypeReference<Integer>() {}, 0)); assertEquals(18, MapUtil.get(map, "age", new TypeReference<Integer>() {
}, 0));
map = null; map = null;
assertEquals(0, MapUtil.get(map, "age", new TypeReference<Integer>() {}, 0)); assertEquals(0, MapUtil.get(map, "age", new TypeReference<Integer>() {
}, 0));
} }
} }