diff --git a/hutool-core/src/test/java/cn/hutool/core/map/MapUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/map/MapUtilTest.java index 746175321..3082e0979 100644 --- a/hutool-core/src/test/java/cn/hutool/core/map/MapUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/map/MapUtilTest.java @@ -7,7 +7,6 @@ import cn.hutool.core.lang.TypeReference; import cn.hutool.core.util.StrUtil; import lombok.Builder; import lombok.Data; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.*; @@ -65,10 +64,10 @@ public class MapUtilTest { public void mapTest() { // Add test like a foreigner final Map adjectivesMap = MapUtil.builder() - .put(0, "lovely") - .put(1, "friendly") - .put(2, "happily") - .build(); + .put(0, "lovely") + .put(1, "friendly") + .put(2, "happily") + .build(); final Map resultMap = MapUtil.map(adjectivesMap, (k, v) -> v + " " + PeopleEnum.values()[k].name().toLowerCase()); @@ -84,7 +83,7 @@ public class MapUtilTest { final Map idUserMap = Stream.iterate(0L, i -> ++i).limit(4).map(i -> User.builder().id(i).name(customers.poll()).build()).collect(Collectors.toMap(User::getId, Function.identity())); // 如你所见,它是一个map,key由分组id,value由用户ids组成,典型的多对多关系 final Map> groupIdUserIdsMap = groups.stream().flatMap(group -> idUserMap.keySet().stream().map(userId -> UserGroup.builder().groupId(group.getId()).userId(userId).build())) - .collect(Collectors.groupingBy(UserGroup::getGroupId, Collectors.mapping(UserGroup::getUserId, Collectors.toList()))); + .collect(Collectors.groupingBy(UserGroup::getGroupId, Collectors.mapping(UserGroup::getUserId, Collectors.toList()))); // 神奇的魔法发生了, 分组id和用户ids组成的map,竟然变成了订单编号和用户实体集合组成的map final Map> groupIdUserMap = MapUtil.map(groupIdUserIdsMap, (groupId, userIds) -> userIds.stream().map(idUserMap::get).collect(Collectors.toList())); @@ -194,11 +193,11 @@ public class MapUtilTest { } @Test - public void sortJoinTest(){ + public void sortJoinTest() { final Map build = MapUtil.builder(new HashMap()) - .put("key1", "value1") - .put("key3", "value3") - .put("key2", "value2").build(); + .put("key1", "value1") + .put("key3", "value3") + .put("key2", "value2").build(); final String join1 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false); assertEquals("key1value1key2value2key3value3", join1); @@ -211,7 +210,7 @@ public class MapUtilTest { } @Test - public void ofEntriesTest(){ + public void ofEntriesTest() { final Map map = MapUtil.ofEntries(MapUtil.entry("a", 1), MapUtil.entry("b", 2)); assertEquals(2, map.size()); @@ -220,10 +219,10 @@ public class MapUtilTest { } @Test - public void ofEntriesSimpleEntryTest(){ + public void ofEntriesSimpleEntryTest() { final Map map = MapUtil.ofEntries( - MapUtil.entry("a", 1,false), - MapUtil.entry("b", 2,false) + MapUtil.entry("a", 1, false), + MapUtil.entry("b", 2, false) ); assertEquals(2, map.size()); @@ -232,7 +231,7 @@ public class MapUtilTest { } @Test - public void getIntTest(){ + public void getIntTest() { assertThrows(NumberFormatException.class, () -> { final HashMap map = MapUtil.of("age", "d"); final Integer age = MapUtil.getInt(map, "age"); @@ -256,13 +255,14 @@ public class MapUtilTest { @Test public void renameKeyMapEmptyNoChange() { - Map map = new HashMap<>(); + Map map = new HashMap<>(); Map result = MapUtil.renameKey(map, "oldKey", "newKey"); assertTrue(result.isEmpty()); } + @Test public void renameKeyOldKeyNotPresentNoChange() { - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("anotherKey", "value"); Map result = MapUtil.renameKey(map, "oldKey", "newKey"); assertEquals(1, result.size()); @@ -271,7 +271,7 @@ public class MapUtilTest { @Test public void renameKeyOldKeyPresentNewKeyNotPresentKeyRenamed() { - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("oldKey", "value"); Map result = MapUtil.renameKey(map, "oldKey", "newKey"); assertEquals(1, result.size()); @@ -280,7 +280,7 @@ public class MapUtilTest { @Test public void renameKeyNewKeyPresentThrowsException() { - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("oldKey", "value"); map.put("newKey", "existingValue"); assertThrows(IllegalArgumentException.class, () -> { @@ -292,11 +292,13 @@ public class MapUtilTest { public void issue3162Test() { final Map map = new HashMap() { private static final long serialVersionUID = 1L; + { - put("a", "1"); - put("b", "2"); - put("c", "3"); - }}; + put("a", "1"); + put("b", "2"); + put("c", "3"); + } + }; final Map filtered = MapUtil.filter(map, "a", "b"); assertEquals(2, filtered.size()); assertEquals("1", filtered.get("a")); @@ -498,66 +500,89 @@ public class MapUtilTest { //----------valuesOfKeys @Test public void valuesOfKeysEmptyIteratorReturnsEmptyList() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); - Iterator emptyIterator = new ArrayList().iterator(); + Iterator emptyIterator = Collections.emptyIterator(); ArrayList result = MapUtil.valuesOfKeys(map, emptyIterator); assertEquals(new ArrayList(), result); } @Test public void valuesOfKeysNonEmptyIteratorReturnsValuesList() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); - Iterator iterator = new ArrayList() {{ - add("a"); - add("b"); - }}.iterator(); + Iterator iterator = new ArrayList() { + private static final long serialVersionUID = -4593258366224032110L; + + { + add("a"); + add("b"); + } + }.iterator(); ArrayList result = MapUtil.valuesOfKeys(map, iterator); - assertEquals(new ArrayList() {{ - add("1"); - add("2"); - }}, result); + assertEquals(new ArrayList() { + private static final long serialVersionUID = 7218152799308667271L; + + { + add("1"); + add("2"); + } + }, result); } @Test public void valuesOfKeysKeysNotInMapReturnsNulls() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); - Iterator iterator = new ArrayList() {{ - add("d"); - add("e"); - }}.iterator(); + Iterator iterator = new ArrayList() { + private static final long serialVersionUID = -5479427021989481058L; + + { + add("d"); + add("e"); + } + }.iterator(); ArrayList result = MapUtil.valuesOfKeys(map, iterator); - assertEquals(new ArrayList() {{ - add(null); - add(null); - }}, result); + assertEquals(new ArrayList() { + private static final long serialVersionUID = 4390715387901549136L; + + { + add(null); + add(null); + } + }, result); } @Test public void valuesOfKeysMixedKeysReturnsMixedValues() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); - Iterator iterator = new ArrayList() {{ - add("a"); - add("d"); - add("b"); - }}.iterator(); + Iterator iterator = new ArrayList() { + private static final long serialVersionUID = 8510595063492828968L; + + { + add("a"); + add("d"); + add("b"); + } + }.iterator(); ArrayList result = MapUtil.valuesOfKeys(map, iterator); - assertEquals(new ArrayList() {{ - add("1"); - add(null); - add("2"); - }}, result); + assertEquals(new ArrayList() { + private static final long serialVersionUID = 6383576410597048337L; + { + add("1"); + add(null); + add("2"); + } + }, result); } //--------clear @@ -569,14 +594,14 @@ public class MapUtilTest { @Test public void clearEmptyMapNoChange() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); MapUtil.clear(map); assertTrue(map.isEmpty()); } @Test public void clearNonEmptyMapClearsMap() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("key", "value"); MapUtil.clear(map); assertTrue(map.isEmpty()); @@ -601,7 +626,7 @@ public class MapUtilTest { @Test public void clearMixedMapsClearsNonEmptyMaps() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("key", "value"); Map emptyMap = new HashMap<>(); @@ -664,14 +689,14 @@ public class MapUtilTest { @Test public void removeNullValueEmptyMapReturnsEmptyMap() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); Map result = MapUtil.removeNullValue(map); assertEquals(0, result.size()); } @Test public void removeNullValueNoNullValuesReturnsSameMap() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); @@ -684,7 +709,7 @@ public class MapUtilTest { @Test public void removeNullValueWithNullValuesRemovesNullEntries() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", null); map.put("key3", "value3"); @@ -699,7 +724,7 @@ public class MapUtilTest { @Test public void removeNullValueAllNullValuesReturnsEmptyMap() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("key1", null); map.put("key2", null); @@ -712,7 +737,8 @@ public class MapUtilTest { //------getQuietly @Test public void getQuietlyMapIsNullReturnsDefaultValue() { - String result = MapUtil.getQuietly(null, "key1", new TypeReference() {}, "default"); + String result = MapUtil.getQuietly(null, "key1", new TypeReference() { + }, "default"); assertEquals("default", result); result = MapUtil.getQuietly(null, "key1", String.class, "default"); assertEquals("default", result); @@ -720,47 +746,52 @@ public class MapUtilTest { @Test public void getQuietlyKeyExistsReturnsConvertedValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", 123); - String result = MapUtil.getQuietly(map, "key1", new TypeReference() {}, "default"); + String result = MapUtil.getQuietly(map, "key1", new TypeReference() { + }, "default"); assertEquals("value1", result); } @Test public void getQuietlyKeyDoesNotExistReturnsDefaultValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", 123); - String result = MapUtil.getQuietly(map, "key3", new TypeReference() {}, "default"); + String result = MapUtil.getQuietly(map, "key3", new TypeReference() { + }, "default"); assertEquals("default", result); } @Test public void getQuietlyConversionFailsReturnsDefaultValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", 123); - Integer result = MapUtil.getQuietly(map, "key1", new TypeReference() {}, 0); + Integer result = MapUtil.getQuietly(map, "key1", new TypeReference() { + }, 0); assertEquals(0, result); } @Test public void getQuietlyKeyExistsWithCorrectTypeReturnsValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", 123); - Integer result = MapUtil.getQuietly(map, "key2", new TypeReference() {}, 0); + Integer result = MapUtil.getQuietly(map, "key2", new TypeReference() { + }, 0); assertEquals(123, result); } @Test public void getQuietlyKeyExistsWithNullValueReturnsDefaultValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", 123); map.put("key3", null); - String result = MapUtil.getQuietly(map, "key3", new TypeReference() {}, "default"); + String result = MapUtil.getQuietly(map, "key3", new TypeReference() { + }, "default"); assertEquals("default", result); } @@ -771,7 +802,7 @@ public class MapUtilTest { @Test public void getKeyExistsReturnsConvertedValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("age", "18"); map.put("name", "Hutool"); assertEquals("18", MapUtil.get(map, "age", String.class)); @@ -779,7 +810,7 @@ public class MapUtilTest { @Test public void getKeyDoesNotExistReturnsDefaultValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("age", "18"); map.put("name", "Hutool"); assertEquals("default", MapUtil.get(map, "nonexistent", String.class, "default")); @@ -787,7 +818,7 @@ public class MapUtilTest { @Test public void getTypeConversionFailsReturnsDefaultValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("age", "18"); map.put("name", "Hutool"); assertEquals(18, MapUtil.get(map, "age", Integer.class, 0)); @@ -795,7 +826,7 @@ public class MapUtilTest { @Test public void getQuietlyTypeConversionFailsReturnsDefaultValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("age", "18"); map.put("name", "Hutool"); assertEquals(0, MapUtil.getQuietly(map, "name", Integer.class, 0)); @@ -803,28 +834,32 @@ public class MapUtilTest { @Test public void getTypeReferenceReturnsConvertedValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("age", "18"); map.put("name", "Hutool"); - assertEquals("18", MapUtil.get(map, "age", new TypeReference() {})); + assertEquals("18", MapUtil.get(map, "age", new TypeReference() { + })); } @Test public void getTypeReferenceWithDefaultValueReturnsConvertedValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("age", "18"); map.put("name", "Hutool"); - assertEquals("18", MapUtil.get(map, "age", new TypeReference() {}, "default")); + assertEquals("18", MapUtil.get(map, "age", new TypeReference() { + }, "default")); } @Test public void getTypeReferenceWithDefaultValueTypeConversionFailsReturnsDefaultValue() { - Map map= new HashMap<>(); + Map map = new HashMap<>(); map.put("age", "18"); map.put("name", "Hutool"); - assertEquals(18, MapUtil.get(map, "age", new TypeReference() {}, 0)); + assertEquals(18, MapUtil.get(map, "age", new TypeReference() { + }, 0)); map = null; - assertEquals(0, MapUtil.get(map, "age", new TypeReference() {}, 0)); + assertEquals(0, MapUtil.get(map, "age", new TypeReference() { + }, 0)); } }