mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
add test
This commit is contained in:
@@ -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.*;
|
||||||
@@ -194,7 +193,7 @@ public class MapUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sortJoinTest(){
|
public void sortJoinTest() {
|
||||||
final Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
|
final Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
|
||||||
.put("key1", "value1")
|
.put("key1", "value1")
|
||||||
.put("key3", "value3")
|
.put("key3", "value3")
|
||||||
@@ -211,7 +210,7 @@ public class MapUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void ofEntriesTest(){
|
public void ofEntriesTest() {
|
||||||
final Map<String, Integer> map = MapUtil.ofEntries(MapUtil.entry("a", 1), MapUtil.entry("b", 2));
|
final Map<String, Integer> map = MapUtil.ofEntries(MapUtil.entry("a", 1), MapUtil.entry("b", 2));
|
||||||
assertEquals(2, map.size());
|
assertEquals(2, map.size());
|
||||||
|
|
||||||
@@ -220,10 +219,10 @@ public class MapUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void ofEntriesSimpleEntryTest(){
|
public void ofEntriesSimpleEntryTest() {
|
||||||
final Map<String, Integer> map = MapUtil.ofEntries(
|
final Map<String, Integer> map = MapUtil.ofEntries(
|
||||||
MapUtil.entry("a", 1,false),
|
MapUtil.entry("a", 1, false),
|
||||||
MapUtil.entry("b", 2,false)
|
MapUtil.entry("b", 2, false)
|
||||||
);
|
);
|
||||||
assertEquals(2, map.size());
|
assertEquals(2, map.size());
|
||||||
|
|
||||||
@@ -232,7 +231,7 @@ public class MapUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getIntTest(){
|
public void getIntTest() {
|
||||||
assertThrows(NumberFormatException.class, () -> {
|
assertThrows(NumberFormatException.class, () -> {
|
||||||
final HashMap<String, String> map = MapUtil.of("age", "d");
|
final HashMap<String, String> map = MapUtil.of("age", "d");
|
||||||
final Integer age = MapUtil.getInt(map, "age");
|
final Integer age = MapUtil.getInt(map, "age");
|
||||||
@@ -256,13 +255,14 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void renameKeyMapEmptyNoChange() {
|
public void renameKeyMapEmptyNoChange() {
|
||||||
Map<String,String> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
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<>();
|
||||||
map.put("anotherKey", "value");
|
map.put("anotherKey", "value");
|
||||||
Map<String, String> result = MapUtil.renameKey(map, "oldKey", "newKey");
|
Map<String, String> result = MapUtil.renameKey(map, "oldKey", "newKey");
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
@@ -271,7 +271,7 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void renameKeyOldKeyPresentNewKeyNotPresentKeyRenamed() {
|
public void renameKeyOldKeyPresentNewKeyNotPresentKeyRenamed() {
|
||||||
Map<String,String> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("oldKey", "value");
|
map.put("oldKey", "value");
|
||||||
Map<String, String> result = MapUtil.renameKey(map, "oldKey", "newKey");
|
Map<String, String> result = MapUtil.renameKey(map, "oldKey", "newKey");
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
@@ -280,7 +280,7 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void renameKeyNewKeyPresentThrowsException() {
|
public void renameKeyNewKeyPresentThrowsException() {
|
||||||
Map<String,String> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("oldKey", "value");
|
map.put("oldKey", "value");
|
||||||
map.put("newKey", "existingValue");
|
map.put("newKey", "existingValue");
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
@@ -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"));
|
||||||
@@ -498,66 +500,89 @@ public class MapUtilTest {
|
|||||||
//----------valuesOfKeys
|
//----------valuesOfKeys
|
||||||
@Test
|
@Test
|
||||||
public void valuesOfKeysEmptyIteratorReturnsEmptyList() {
|
public void valuesOfKeysEmptyIteratorReturnsEmptyList() {
|
||||||
Map<String, String> map= new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void valuesOfKeysNonEmptyIteratorReturnsValuesList() {
|
public void valuesOfKeysNonEmptyIteratorReturnsValuesList() {
|
||||||
Map<String, String> map= new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
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
|
||||||
public void valuesOfKeysKeysNotInMapReturnsNulls() {
|
public void valuesOfKeysKeysNotInMapReturnsNulls() {
|
||||||
Map<String, String> map= new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
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
|
||||||
public void valuesOfKeysMixedKeysReturnsMixedValues() {
|
public void valuesOfKeysMixedKeysReturnsMixedValues() {
|
||||||
Map<String, String> map= new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
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
|
||||||
@@ -569,14 +594,14 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clearEmptyMapNoChange() {
|
public void clearEmptyMapNoChange() {
|
||||||
Map<String, String> map= new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
MapUtil.clear(map);
|
MapUtil.clear(map);
|
||||||
assertTrue(map.isEmpty());
|
assertTrue(map.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clearNonEmptyMapClearsMap() {
|
public void clearNonEmptyMapClearsMap() {
|
||||||
Map<String, String> map= new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("key", "value");
|
map.put("key", "value");
|
||||||
MapUtil.clear(map);
|
MapUtil.clear(map);
|
||||||
assertTrue(map.isEmpty());
|
assertTrue(map.isEmpty());
|
||||||
@@ -601,7 +626,7 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clearMixedMapsClearsNonEmptyMaps() {
|
public void clearMixedMapsClearsNonEmptyMaps() {
|
||||||
Map<String, String> map= new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("key", "value");
|
map.put("key", "value");
|
||||||
|
|
||||||
Map<String, String> emptyMap = new HashMap<>();
|
Map<String, String> emptyMap = new HashMap<>();
|
||||||
@@ -664,14 +689,14 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void removeNullValueEmptyMapReturnsEmptyMap() {
|
public void removeNullValueEmptyMapReturnsEmptyMap() {
|
||||||
Map<String, String> map= new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
Map<String, String> result = MapUtil.removeNullValue(map);
|
Map<String, String> result = MapUtil.removeNullValue(map);
|
||||||
assertEquals(0, result.size());
|
assertEquals(0, result.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void removeNullValueNoNullValuesReturnsSameMap() {
|
public void removeNullValueNoNullValuesReturnsSameMap() {
|
||||||
Map<String, String> map= new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("key1", "value1");
|
map.put("key1", "value1");
|
||||||
map.put("key2", "value2");
|
map.put("key2", "value2");
|
||||||
|
|
||||||
@@ -684,7 +709,7 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void removeNullValueWithNullValuesRemovesNullEntries() {
|
public void removeNullValueWithNullValuesRemovesNullEntries() {
|
||||||
Map<String, String> map= new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("key1", "value1");
|
map.put("key1", "value1");
|
||||||
map.put("key2", null);
|
map.put("key2", null);
|
||||||
map.put("key3", "value3");
|
map.put("key3", "value3");
|
||||||
@@ -699,7 +724,7 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void removeNullValueAllNullValuesReturnsEmptyMap() {
|
public void removeNullValueAllNullValuesReturnsEmptyMap() {
|
||||||
Map<String, String> map= new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("key1", null);
|
map.put("key1", null);
|
||||||
map.put("key2", null);
|
map.put("key2", null);
|
||||||
|
|
||||||
@@ -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);
|
||||||
@@ -720,47 +746,52 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getQuietlyKeyExistsReturnsConvertedValue() {
|
public void getQuietlyKeyExistsReturnsConvertedValue() {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getQuietlyKeyDoesNotExistReturnsDefaultValue() {
|
public void getQuietlyKeyDoesNotExistReturnsDefaultValue() {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getQuietlyConversionFailsReturnsDefaultValue() {
|
public void getQuietlyConversionFailsReturnsDefaultValue() {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getQuietlyKeyExistsWithCorrectTypeReturnsValue() {
|
public void getQuietlyKeyExistsWithCorrectTypeReturnsValue() {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getQuietlyKeyExistsWithNullValueReturnsDefaultValue() {
|
public void getQuietlyKeyExistsWithNullValueReturnsDefaultValue() {
|
||||||
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);
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -771,7 +802,7 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getKeyExistsReturnsConvertedValue() {
|
public void getKeyExistsReturnsConvertedValue() {
|
||||||
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", String.class));
|
assertEquals("18", MapUtil.get(map, "age", String.class));
|
||||||
@@ -779,7 +810,7 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getKeyDoesNotExistReturnsDefaultValue() {
|
public void getKeyDoesNotExistReturnsDefaultValue() {
|
||||||
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("default", MapUtil.get(map, "nonexistent", String.class, "default"));
|
assertEquals("default", MapUtil.get(map, "nonexistent", String.class, "default"));
|
||||||
@@ -787,7 +818,7 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getTypeConversionFailsReturnsDefaultValue() {
|
public void getTypeConversionFailsReturnsDefaultValue() {
|
||||||
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", Integer.class, 0));
|
assertEquals(18, MapUtil.get(map, "age", Integer.class, 0));
|
||||||
@@ -795,7 +826,7 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getQuietlyTypeConversionFailsReturnsDefaultValue() {
|
public void getQuietlyTypeConversionFailsReturnsDefaultValue() {
|
||||||
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(0, MapUtil.getQuietly(map, "name", Integer.class, 0));
|
assertEquals(0, MapUtil.getQuietly(map, "name", Integer.class, 0));
|
||||||
@@ -803,28 +834,32 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getTypeReferenceReturnsConvertedValue() {
|
public void getTypeReferenceReturnsConvertedValue() {
|
||||||
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
|
||||||
public void getTypeReferenceWithDefaultValueReturnsConvertedValue() {
|
public void getTypeReferenceWithDefaultValueReturnsConvertedValue() {
|
||||||
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
|
||||||
public void getTypeReferenceWithDefaultValueTypeConversionFailsReturnsDefaultValue() {
|
public void getTypeReferenceWithDefaultValueTypeConversionFailsReturnsDefaultValue() {
|
||||||
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user