mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
add test
This commit is contained in:
@@ -23,10 +23,14 @@ import cn.hutool.v7.core.map.MapUtil;
|
|||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collector;
|
import java.util.stream.Collector;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CollectionStream测试方法
|
* CollectionStream测试方法
|
||||||
*/
|
*/
|
||||||
@@ -35,17 +39,17 @@ public class CollStreamUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testToIdentityMap() {
|
public void testToIdentityMap() {
|
||||||
Map<Long, Student> map = CollStreamUtil.toIdentityMap(null, Student::getStudentId);
|
Map<Long, Student> map = CollStreamUtil.toIdentityMap(null, Student::getStudentId);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
final List<Student> list = new ArrayList<>();
|
final List<Student> list = new ArrayList<>();
|
||||||
map = CollStreamUtil.toIdentityMap(list, Student::getStudentId);
|
map = CollStreamUtil.toIdentityMap(list, Student::getStudentId);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
list.add(new Student(1, 1, 1, "张三"));
|
list.add(new Student(1, 1, 1, "张三"));
|
||||||
list.add(new Student(1, 1, 2, "李四"));
|
list.add(new Student(1, 1, 2, "李四"));
|
||||||
list.add(new Student(1, 1, 3, "王五"));
|
list.add(new Student(1, 1, 3, "王五"));
|
||||||
map = CollStreamUtil.toIdentityMap(list, Student::getStudentId);
|
map = CollStreamUtil.toIdentityMap(list, Student::getStudentId);
|
||||||
Assertions.assertEquals(map.get(1L).getName(), "张三");
|
assertEquals("张三", map.get(1L).getName());
|
||||||
Assertions.assertEquals(map.get(2L).getName(), "李四");
|
assertEquals("李四", map.get(2L).getName());
|
||||||
Assertions.assertEquals(map.get(3L).getName(), "王五");
|
assertEquals("王五", map.get(3L).getName());
|
||||||
Assertions.assertNull(map.get(4L));
|
Assertions.assertNull(map.get(4L));
|
||||||
|
|
||||||
// 测试value为空时
|
// 测试value为空时
|
||||||
@@ -57,17 +61,17 @@ public class CollStreamUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testToMap() {
|
public void testToMap() {
|
||||||
Map<Long, String> map = CollStreamUtil.toMap(null, Student::getStudentId, Student::getName);
|
Map<Long, String> map = CollStreamUtil.toMap(null, Student::getStudentId, Student::getName);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
final List<Student> list = new ArrayList<>();
|
final List<Student> list = new ArrayList<>();
|
||||||
map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName);
|
map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
list.add(new Student(1, 1, 1, "张三"));
|
list.add(new Student(1, 1, 1, "张三"));
|
||||||
list.add(new Student(1, 1, 2, "李四"));
|
list.add(new Student(1, 1, 2, "李四"));
|
||||||
list.add(new Student(1, 1, 3, "王五"));
|
list.add(new Student(1, 1, 3, "王五"));
|
||||||
map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName);
|
map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName);
|
||||||
Assertions.assertEquals(map.get(1L), "张三");
|
assertEquals("张三", map.get(1L));
|
||||||
Assertions.assertEquals(map.get(2L), "李四");
|
assertEquals("李四", map.get(2L));
|
||||||
Assertions.assertEquals(map.get(3L), "王五");
|
assertEquals("王五", map.get(3L));
|
||||||
Assertions.assertNull(map.get(4L));
|
Assertions.assertNull(map.get(4L));
|
||||||
|
|
||||||
// 测试value为空时
|
// 测试value为空时
|
||||||
@@ -79,10 +83,10 @@ public class CollStreamUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testGroupByKey() {
|
public void testGroupByKey() {
|
||||||
Map<Long, List<Student>> map = CollStreamUtil.groupByKey(null, Student::getClassId);
|
Map<Long, List<Student>> map = CollStreamUtil.groupByKey(null, Student::getClassId);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
final List<Student> list = new ArrayList<>();
|
final List<Student> list = new ArrayList<>();
|
||||||
map = CollStreamUtil.groupByKey(list, Student::getClassId);
|
map = CollStreamUtil.groupByKey(list, Student::getClassId);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
list.add(new Student(1, 1, 1, "张三"));
|
list.add(new Student(1, 1, 1, "张三"));
|
||||||
list.add(new Student(1, 2, 2, "李四"));
|
list.add(new Student(1, 2, 2, "李四"));
|
||||||
list.add(new Student(2, 1, 1, "擎天柱"));
|
list.add(new Student(2, 1, 1, "擎天柱"));
|
||||||
@@ -102,16 +106,16 @@ public class CollStreamUtilTest {
|
|||||||
final List<Student> class3 = new ArrayList<>();
|
final List<Student> class3 = new ArrayList<>();
|
||||||
class3.add(new Student(2, 3, 2, "霸天虎"));
|
class3.add(new Student(2, 3, 2, "霸天虎"));
|
||||||
compare.put(3L, class3);
|
compare.put(3L, class3);
|
||||||
Assertions.assertEquals(map, compare);
|
assertEquals(map, compare);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGroupBy2Key() {
|
public void testGroupBy2Key() {
|
||||||
Map<Long, Map<Long, List<Student>>> map = CollStreamUtil.groupBy2Key(null, Student::getTermId, Student::getClassId);
|
Map<Long, Map<Long, List<Student>>> map = CollStreamUtil.groupBy2Key(null, Student::getTermId, Student::getClassId);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
final List<Student> list = new ArrayList<>();
|
final List<Student> list = new ArrayList<>();
|
||||||
map = CollStreamUtil.groupBy2Key(list, Student::getTermId, Student::getClassId);
|
map = CollStreamUtil.groupBy2Key(list, Student::getTermId, Student::getClassId);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
list.add(new Student(1, 1, 1, "张三"));
|
list.add(new Student(1, 1, 1, "张三"));
|
||||||
list.add(new Student(1, 2, 2, "李四"));
|
list.add(new Student(1, 2, 2, "李四"));
|
||||||
list.add(new Student(1, 2, 3, "王五"));
|
list.add(new Student(1, 2, 3, "王五"));
|
||||||
@@ -141,17 +145,17 @@ public class CollStreamUtilTest {
|
|||||||
list22.add(new Student(2, 2, 3, "霸天虎"));
|
list22.add(new Student(2, 2, 3, "霸天虎"));
|
||||||
map2.put(2L, list22);
|
map2.put(2L, list22);
|
||||||
compare.put(2L, map2);
|
compare.put(2L, map2);
|
||||||
Assertions.assertEquals(map, compare);
|
assertEquals(map, compare);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGroup2Map() {
|
public void testGroup2Map() {
|
||||||
Map<Long, Map<Long, Student>> map = CollStreamUtil.group2Map(null, Student::getTermId, Student::getClassId);
|
Map<Long, Map<Long, Student>> map = CollStreamUtil.group2Map(null, Student::getTermId, Student::getClassId);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
|
|
||||||
final List<Student> list = new ArrayList<>();
|
final List<Student> list = new ArrayList<>();
|
||||||
map = CollStreamUtil.group2Map(list, Student::getTermId, Student::getClassId);
|
map = CollStreamUtil.group2Map(list, Student::getTermId, Student::getClassId);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
list.add(new Student(1, 1, 1, "张三"));
|
list.add(new Student(1, 1, 1, "张三"));
|
||||||
list.add(new Student(1, 2, 1, "李四"));
|
list.add(new Student(1, 2, 1, "李四"));
|
||||||
list.add(new Student(2, 2, 1, "王五"));
|
list.add(new Student(2, 2, 1, "王五"));
|
||||||
@@ -164,27 +168,28 @@ public class CollStreamUtilTest {
|
|||||||
final Map<Long, Student> map2 = new HashMap<>();
|
final Map<Long, Student> map2 = new HashMap<>();
|
||||||
map2.put(2L, new Student(2, 2, 1, "王五"));
|
map2.put(2L, new Student(2, 2, 1, "王五"));
|
||||||
compare.put(2L, map2);
|
compare.put(2L, map2);
|
||||||
Assertions.assertEquals(compare, map);
|
assertEquals(compare, map);
|
||||||
|
|
||||||
// 对null友好
|
// 对null友好
|
||||||
final Map<Long, Map<Long, Student>> termIdClassIdStudentMap = CollStreamUtil.group2Map(Arrays.asList(null, new Student(2, 2, 1, "王五")), Student::getTermId, Student::getClassId);
|
final Map<Long, Map<Long, Student>> termIdClassIdStudentMap = CollStreamUtil.group2Map(Arrays.asList(null, new Student(2, 2, 1, "王五")), Student::getTermId, Student::getClassId);
|
||||||
final Map<Long, Map<Long, Student>> termIdClassIdStudentCompareMap = new HashMap<Long, Map<Long, Student>>() {
|
final Map<Long, Map<Long, Student>> termIdClassIdStudentCompareMap = new HashMap<Long, Map<Long, Student>>() {
|
||||||
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
{
|
{
|
||||||
put(null, MapUtil.empty());
|
put(null, MapUtil.empty());
|
||||||
put(2L, MapUtil.of(2L, new Student(2, 2, 1, "王五")));
|
put(2L, MapUtil.of(2L, new Student(2, 2, 1, "王五")));
|
||||||
}};
|
}};
|
||||||
Assertions.assertEquals(termIdClassIdStudentCompareMap, termIdClassIdStudentMap);
|
assertEquals(termIdClassIdStudentCompareMap, termIdClassIdStudentMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGroupKeyValue() {
|
public void testGroupKeyValue() {
|
||||||
Map<Long, List<Long>> map = CollStreamUtil.groupKeyValue(null, Student::getTermId, Student::getClassId);
|
Map<Long, List<Long>> map = CollStreamUtil.groupKeyValue(null, Student::getTermId, Student::getClassId);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
|
|
||||||
final List<Student> list = new ArrayList<>();
|
final List<Student> list = new ArrayList<>();
|
||||||
map = CollStreamUtil.groupKeyValue(list, Student::getTermId, Student::getClassId);
|
map = CollStreamUtil.groupKeyValue(list, Student::getTermId, Student::getClassId);
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
list.add(new Student(1, 1, 1, "张三"));
|
list.add(new Student(1, 1, 1, "张三"));
|
||||||
list.add(new Student(1, 2, 1, "李四"));
|
list.add(new Student(1, 2, 1, "李四"));
|
||||||
list.add(new Student(2, 2, 1, "王五"));
|
list.add(new Student(2, 2, 1, "王五"));
|
||||||
@@ -193,7 +198,7 @@ public class CollStreamUtilTest {
|
|||||||
final Map<Long, List<Long>> compare = new HashMap<>();
|
final Map<Long, List<Long>> compare = new HashMap<>();
|
||||||
compare.put(1L, Arrays.asList(1L, 2L));
|
compare.put(1L, Arrays.asList(1L, 2L));
|
||||||
compare.put(2L, Collections.singletonList(2L));
|
compare.put(2L, Collections.singletonList(2L));
|
||||||
Assertions.assertEquals(compare, map);
|
assertEquals(compare, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -202,12 +207,12 @@ public class CollStreamUtilTest {
|
|||||||
|
|
||||||
// 参数null测试
|
// 参数null测试
|
||||||
Map<Long, List<Student>> map = CollStreamUtil.groupBy(null, Student::getTermId, Collectors.toList());
|
Map<Long, List<Student>> map = CollStreamUtil.groupBy(null, Student::getTermId, Collectors.toList());
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
|
|
||||||
// 参数空数组测试
|
// 参数空数组测试
|
||||||
final List<Student> list = new ArrayList<>();
|
final List<Student> list = new ArrayList<>();
|
||||||
map = CollStreamUtil.groupBy(list, Student::getTermId, Collectors.toList());
|
map = CollStreamUtil.groupBy(list, Student::getTermId, Collectors.toList());
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
|
|
||||||
// 放入元素
|
// 放入元素
|
||||||
list.add(new Student(1, 1, 1, "张三"));
|
list.add(new Student(1, 1, 1, "张三"));
|
||||||
@@ -216,11 +221,11 @@ public class CollStreamUtilTest {
|
|||||||
// 先根据termId分组,再通过classId比较,找出最大值所属的那个Student,返回的Optional
|
// 先根据termId分组,再通过classId比较,找出最大值所属的那个Student,返回的Optional
|
||||||
final Map<Long, Optional<Student>> longOptionalMap = CollStreamUtil.groupBy(list, Student::getTermId, Collectors.maxBy(Comparator.comparing(Student::getClassId)));
|
final Map<Long, Optional<Student>> longOptionalMap = CollStreamUtil.groupBy(list, Student::getTermId, Collectors.maxBy(Comparator.comparing(Student::getClassId)));
|
||||||
//noinspection OptionalGetWithoutIsPresent
|
//noinspection OptionalGetWithoutIsPresent
|
||||||
Assertions.assertEquals("李四", longOptionalMap.get(1L).get().getName());
|
assertEquals("李四", longOptionalMap.get(1L).get().getName());
|
||||||
|
|
||||||
// 先根据termId分组,再转换为Map<studentId,name>
|
// 先根据termId分组,再转换为Map<studentId,name>
|
||||||
final Map<Long, HashMap<Long, String>> groupThen = CollStreamUtil.groupBy(list, Student::getTermId, Collector.of(HashMap::new, (m, v) -> m.put(v.getStudentId(), v.getName()), (l, r) -> l));
|
final Map<Long, HashMap<Long, String>> groupThen = CollStreamUtil.groupBy(list, Student::getTermId, Collector.of(HashMap::new, (m, v) -> m.put(v.getStudentId(), v.getName()), (l, r) -> l));
|
||||||
Assertions.assertEquals(
|
assertEquals(
|
||||||
MapUtil.builder()
|
MapUtil.builder()
|
||||||
.put(1L, MapUtil.builder().put(1L, "李四").build())
|
.put(1L, MapUtil.builder().put(1L, "李四").build())
|
||||||
.put(2L, MapUtil.builder().put(1L, "王五").build())
|
.put(2L, MapUtil.builder().put(1L, "王五").build())
|
||||||
@@ -235,23 +240,23 @@ public class CollStreamUtilTest {
|
|||||||
final Map<Long, List<Student>> termIdStudentsCompareMap = new HashMap<>();
|
final Map<Long, List<Student>> termIdStudentsCompareMap = new HashMap<>();
|
||||||
termIdStudentsCompareMap.put(null, Collections.emptyList());
|
termIdStudentsCompareMap.put(null, Collections.emptyList());
|
||||||
termIdStudentsCompareMap.put(1L, Arrays.asList(new Student(1L, 1, 1, "张三"), new Student(1L, 2, 1, "李四")));
|
termIdStudentsCompareMap.put(1L, Arrays.asList(new Student(1L, 1, 1, "张三"), new Student(1L, 2, 1, "李四")));
|
||||||
Assertions.assertEquals(termIdStudentsCompareMap, termIdStudentsMap);
|
assertEquals(termIdStudentsCompareMap, termIdStudentsMap);
|
||||||
|
|
||||||
final Map<Long, Long> termIdCountMap = CollStreamUtil.groupBy(students, Student::getTermId, Collectors.counting());
|
final Map<Long, Long> termIdCountMap = CollStreamUtil.groupBy(students, Student::getTermId, Collectors.counting());
|
||||||
final Map<Long, Long> termIdCountCompareMap = new HashMap<>();
|
final Map<Long, Long> termIdCountCompareMap = new HashMap<>();
|
||||||
termIdCountCompareMap.put(null, 2L);
|
termIdCountCompareMap.put(null, 2L);
|
||||||
termIdCountCompareMap.put(1L, 2L);
|
termIdCountCompareMap.put(1L, 2L);
|
||||||
Assertions.assertEquals(termIdCountCompareMap, termIdCountMap);
|
assertEquals(termIdCountCompareMap, termIdCountMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTranslate2List() {
|
public void testTranslate2List() {
|
||||||
List<String> list = CollStreamUtil.toList(null, Student::getName);
|
List<String> list = CollStreamUtil.toList(null, Student::getName);
|
||||||
Assertions.assertEquals(list, Collections.EMPTY_LIST);
|
assertEquals(Collections.EMPTY_LIST, list);
|
||||||
final List<Student> students = new ArrayList<>();
|
final List<Student> students = new ArrayList<>();
|
||||||
list = CollStreamUtil.toList(students, Student::getName);
|
list = CollStreamUtil.toList(students, Student::getName);
|
||||||
Assertions.assertEquals(list, Collections.EMPTY_LIST);
|
assertEquals(Collections.EMPTY_LIST, list);
|
||||||
students.add(new Student(1, 1, 1, "张三"));
|
students.add(new Student(1, 1, 1, "张三"));
|
||||||
students.add(new Student(1, 2, 2, "李四"));
|
students.add(new Student(1, 2, 2, "李四"));
|
||||||
students.add(new Student(2, 1, 1, "李四"));
|
students.add(new Student(2, 1, 1, "李四"));
|
||||||
@@ -264,16 +269,16 @@ public class CollStreamUtilTest {
|
|||||||
compare.add("李四");
|
compare.add("李四");
|
||||||
compare.add("李四");
|
compare.add("李四");
|
||||||
compare.add("霸天虎");
|
compare.add("霸天虎");
|
||||||
Assertions.assertEquals(list, compare);
|
assertEquals(list, compare);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTranslate2Set() {
|
public void testTranslate2Set() {
|
||||||
Set<String> set = CollStreamUtil.toSet(null, Student::getName);
|
Set<String> set = CollStreamUtil.toSet(null, Student::getName);
|
||||||
Assertions.assertEquals(set, Collections.EMPTY_SET);
|
assertEquals(Collections.EMPTY_SET, set);
|
||||||
final List<Student> students = new ArrayList<>();
|
final List<Student> students = new ArrayList<>();
|
||||||
set = CollStreamUtil.toSet(students, Student::getName);
|
set = CollStreamUtil.toSet(students, Student::getName);
|
||||||
Assertions.assertEquals(set, Collections.EMPTY_SET);
|
assertEquals(Collections.EMPTY_SET, set);
|
||||||
students.add(new Student(1, 1, 1, "张三"));
|
students.add(new Student(1, 1, 1, "张三"));
|
||||||
students.add(new Student(1, 2, 2, "李四"));
|
students.add(new Student(1, 2, 2, "李四"));
|
||||||
students.add(new Student(2, 1, 1, "李四"));
|
students.add(new Student(2, 1, 1, "李四"));
|
||||||
@@ -284,7 +289,7 @@ public class CollStreamUtilTest {
|
|||||||
compare.add("张三");
|
compare.add("张三");
|
||||||
compare.add("李四");
|
compare.add("李四");
|
||||||
compare.add("霸天虎");
|
compare.add("霸天虎");
|
||||||
Assertions.assertEquals(set, compare);
|
assertEquals(set, compare);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ConstantValue")
|
@SuppressWarnings("ConstantValue")
|
||||||
@@ -293,19 +298,19 @@ public class CollStreamUtilTest {
|
|||||||
Map<Long, Student> map1 = null;
|
Map<Long, Student> map1 = null;
|
||||||
Map<Long, Student> map2 = Collections.emptyMap();
|
Map<Long, Student> map2 = Collections.emptyMap();
|
||||||
Map<Long, String> map = CollStreamUtil.merge(map1, map2, (s1, s2) -> s1.getName() + s2.getName());
|
Map<Long, String> map = CollStreamUtil.merge(map1, map2, (s1, s2) -> s1.getName() + s2.getName());
|
||||||
Assertions.assertEquals(map, Collections.EMPTY_MAP);
|
assertEquals(Collections.EMPTY_MAP, map);
|
||||||
map1 = new HashMap<>();
|
map1 = new HashMap<>();
|
||||||
map1.put(1L, new Student(1, 1, 1, "张三"));
|
map1.put(1L, new Student(1, 1, 1, "张三"));
|
||||||
map = CollStreamUtil.merge(map1, map2, this::merge);
|
map = CollStreamUtil.merge(map1, map2, this::merge);
|
||||||
final Map<Long, String> temp = new HashMap<>();
|
final Map<Long, String> temp = new HashMap<>();
|
||||||
temp.put(1L, "张三");
|
temp.put(1L, "张三");
|
||||||
Assertions.assertEquals(map, temp);
|
assertEquals(map, temp);
|
||||||
map2 = new HashMap<>();
|
map2 = new HashMap<>();
|
||||||
map2.put(1L, new Student(2, 1, 1, "李四"));
|
map2.put(1L, new Student(2, 1, 1, "李四"));
|
||||||
map = CollStreamUtil.merge(map1, map2, this::merge);
|
map = CollStreamUtil.merge(map1, map2, this::merge);
|
||||||
final Map<Long, String> compare = new HashMap<>();
|
final Map<Long, String> compare = new HashMap<>();
|
||||||
compare.put(1L, "张三李四");
|
compare.put(1L, "张三李四");
|
||||||
Assertions.assertEquals(map, compare);
|
assertEquals(map, compare);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String merge(final Student student1, final Student student2) {
|
private String merge(final Student student1, final Student student2) {
|
||||||
@@ -327,16 +332,16 @@ public class CollStreamUtilTest {
|
|||||||
sets.add(ListUtil.of("John", "Jack"));
|
sets.add(ListUtil.of("John", "Jack"));
|
||||||
sets.add(ListUtil.of('I', 'J'));
|
sets.add(ListUtil.of('I', 'J'));
|
||||||
|
|
||||||
final List<List<Object>> collect = CollStreamUtil.cartesianProduct(sets, 0).collect(Collectors.toList());
|
final List<List<Object>> collect = CollStreamUtil.cartesianProduct(sets, 0).toList();
|
||||||
Assertions.assertEquals(8, collect.size());
|
assertEquals(8, collect.size());
|
||||||
Assertions.assertEquals("[10, John, I]", collect.get(0).toString());
|
assertEquals("[10, John, I]", collect.get(0).toString());
|
||||||
Assertions.assertEquals("[10, John, J]", collect.get(1).toString());
|
assertEquals("[10, John, J]", collect.get(1).toString());
|
||||||
Assertions.assertEquals("[10, Jack, I]", collect.get(2).toString());
|
assertEquals("[10, Jack, I]", collect.get(2).toString());
|
||||||
Assertions.assertEquals("[10, Jack, J]", collect.get(3).toString());
|
assertEquals("[10, Jack, J]", collect.get(3).toString());
|
||||||
Assertions.assertEquals("[20, John, I]", collect.get(4).toString());
|
assertEquals("[20, John, I]", collect.get(4).toString());
|
||||||
Assertions.assertEquals("[20, John, J]", collect.get(5).toString());
|
assertEquals("[20, John, J]", collect.get(5).toString());
|
||||||
Assertions.assertEquals("[20, Jack, I]", collect.get(6).toString());
|
assertEquals("[20, Jack, I]", collect.get(6).toString());
|
||||||
Assertions.assertEquals("[20, Jack, J]", collect.get(7).toString());
|
assertEquals("[20, Jack, J]", collect.get(7).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -351,4 +356,38 @@ public class CollStreamUtilTest {
|
|||||||
private long studentId;//班级id
|
private long studentId;//班级id
|
||||||
private String name;//学生名称
|
private String name;//学生名称
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToMap_KeyCollision_SilentlyOverwrite() {
|
||||||
|
final List<Student> list = new ArrayList<>();
|
||||||
|
list.add(new Student(1, 101, 1, "张三"));
|
||||||
|
list.add(new Student(1, 102, 1, "李四"));
|
||||||
|
final Map<Long, String> map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName, false);
|
||||||
|
|
||||||
|
assertEquals(1, map.size());
|
||||||
|
assertEquals("李四", map.get(1L)); // 确保后面的值覆盖前面的
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToMap_NullKeyOrValue() {
|
||||||
|
final List<Student> list = new ArrayList<>();
|
||||||
|
list.add(new Student(1, 1, 1L, "张三"));
|
||||||
|
list.add(null);
|
||||||
|
list.add(new Student(1, 2, 2L, null));
|
||||||
|
|
||||||
|
assertThrows(NullPointerException.class, () -> {
|
||||||
|
CollStreamUtil.toMap(list, Student::getStudentId, Student::getName);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToMap_LargeInputPerformance() {
|
||||||
|
final List<Student> list = new ArrayList<>();
|
||||||
|
for (long i = 0; i < 10000; i++) {
|
||||||
|
list.add(new Student(1, 1, i, "学生" + i));
|
||||||
|
}
|
||||||
|
final Map<Long, String> map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName);
|
||||||
|
|
||||||
|
assertEquals(10000, map.size());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1300,4 +1300,64 @@ public class CollUtilTest {
|
|||||||
assertTrue(subtract.contains(2));
|
assertTrue(subtract.contains(2));
|
||||||
assertTrue(subtract.contains(3));
|
assertTrue(subtract.contains(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPadLeft_NegativeMinLen_ShouldNotModifyList() {
|
||||||
|
final List<String> list = ListUtil.of("a", "b", "c");
|
||||||
|
final List<String> original = ListUtil.of("a", "b", "c");
|
||||||
|
|
||||||
|
CollUtil.padLeft(list, -5, "x");
|
||||||
|
|
||||||
|
assertEquals(original, list, "List should remain unchanged when minLen is negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPadLeft_EmptyList_MinLenZero() {
|
||||||
|
final List<String> list = ListUtil.of();
|
||||||
|
|
||||||
|
CollUtil.padLeft(list, 0, "x");
|
||||||
|
|
||||||
|
assertTrue(list.isEmpty(), "List should remain empty when minLen is 0");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSubtractWithDuplicates() {
|
||||||
|
final Collection<String> coll1 = new ArrayList<>(Arrays.asList("a", "b", "b", "c"));
|
||||||
|
final Collection<String> coll2 = Collections.singletonList("b");
|
||||||
|
final Collection<String> result = CollUtil.subtract(coll1, coll2);
|
||||||
|
|
||||||
|
final List<String> expected = Arrays.asList("a", "c");
|
||||||
|
final List<String> resultList = new ArrayList<>(result);
|
||||||
|
Collections.sort(resultList);
|
||||||
|
Collections.sort(expected);
|
||||||
|
assertEquals(expected, resultList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void lastIndexOf_NoMatchExists() {
|
||||||
|
final List<String> list = ListUtil.of("a", "b", "c");
|
||||||
|
final int idx = CollUtil.lastIndexOf(list, item -> item.equals("z"));
|
||||||
|
assertEquals(-1, idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void lastIndexOf_MatcherIsNull_MatchAll() {
|
||||||
|
final List<String> list = ListUtil.of("x", "y", "z");
|
||||||
|
final int idx = CollUtil.lastIndexOf(list, null);
|
||||||
|
assertEquals(2, idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void lastIndexOf_EmptyCollection() {
|
||||||
|
final List<String> list = ListUtil.of();
|
||||||
|
final int idx = CollUtil.lastIndexOf(list, Objects::nonNull);
|
||||||
|
assertEquals(-1, idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void lastIndexOf_SingletonCollection_Match() {
|
||||||
|
final List<String> list = ListUtil.of("foo");
|
||||||
|
final int idx = CollUtil.lastIndexOf(list, item -> item.equals("foo"));
|
||||||
|
assertEquals(0, idx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,8 +30,7 @@ import java.io.File;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link FileUtil} 单元测试类
|
* {@link FileUtil} 单元测试类
|
||||||
@@ -156,7 +155,7 @@ public class FileUtilTest {
|
|||||||
final File destFile1 = FileUtil.file("d:/hutool.jpg");
|
final File destFile1 = FileUtil.file("d:/hutool.jpg");
|
||||||
|
|
||||||
final boolean notEquals = FileUtil.equals(srcFile1, destFile1);
|
final boolean notEquals = FileUtil.equals(srcFile1, destFile1);
|
||||||
Assertions.assertFalse(notEquals);
|
assertFalse(notEquals);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -434,7 +433,7 @@ public class FileUtilTest {
|
|||||||
public void isSubTest() {
|
public void isSubTest() {
|
||||||
final File file = new File("d:/test");
|
final File file = new File("d:/test");
|
||||||
final File file2 = new File("d:/test2/aaa");
|
final File file2 = new File("d:/test2/aaa");
|
||||||
Assertions.assertFalse(FileUtil.isSub(file, file2));
|
assertFalse(FileUtil.isSub(file, file2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -507,7 +506,7 @@ public class FileUtilTest {
|
|||||||
assertTrue(FileUtil.isAbsolutePath(path));
|
assertTrue(FileUtil.isAbsolutePath(path));
|
||||||
|
|
||||||
path = "test\\aaa.txt";
|
path = "test\\aaa.txt";
|
||||||
Assertions.assertFalse(FileUtil.isAbsolutePath(path));
|
assertFalse(FileUtil.isAbsolutePath(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -536,4 +535,42 @@ public class FileUtilTest {
|
|||||||
FileUtil.checkSlip(FileUtil.file("test/a"), FileUtil.file("test/../a"));
|
FileUtil.checkSlip(FileUtil.file("test/a"), FileUtil.file("test/../a"));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isSub_SubIsAncestorOfParentTest() {
|
||||||
|
final File parent = new File("d:/home/user/docs/notes");
|
||||||
|
final File sub = new File("d:/home/user/docs");
|
||||||
|
assertFalse(FileUtil.isSub(parent, sub));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isSub_SamePathTest() {
|
||||||
|
final File parent = new File("d:/home/user/docs");
|
||||||
|
final File sub = new File("d:/home/user/docs");
|
||||||
|
assertTrue(FileUtil.isSub(parent, sub));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isSub_NonexistentPathsTest() {
|
||||||
|
final File parent = new File("d:/unlikely/to/exist/parent");
|
||||||
|
final File sub = new File("d:/unlikely/to/exist/parent/child/file.txt");
|
||||||
|
assertTrue(FileUtil.isSub(parent, sub));
|
||||||
|
|
||||||
|
final File nonchild = new File("d:/also/unlikely/path.txt");
|
||||||
|
assertFalse(FileUtil.isSub(parent, nonchild));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isSub_NullParentTest() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
FileUtil.isSub(null, new java.io.File("d:/any/path"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isSub_NullSubTest() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
FileUtil.isSub(new java.io.File("d:/any/path"), null);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user