From 3e330f1c647281f2518ef155c9cfb3909a6e82e9 Mon Sep 17 00:00:00 2001 From: qibinhang Date: Fri, 8 Aug 2025 13:18:32 +0800 Subject: [PATCH 1/3] Add tests for covering additional, important scenarios of `CollStreamUtil`. --- .../core/collection/CollStreamUtilTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollStreamUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollStreamUtilTest.java index 1bfb2fb66..2699c32a7 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollStreamUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollStreamUtilTest.java @@ -60,6 +60,40 @@ public class CollStreamUtilTest { assertNull(map.get(4L)); } + @Test + public void testToMap_KeyCollision_SilentlyOverwrite() { + List list = new ArrayList<>(); + list.add(new Student(1, 101, 1, "张三")); + list.add(new Student(1, 102, 1, "李四")); + Map map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName, false); + + assertEquals(1, map.size()); + assertEquals("李四", map.get(1L)); // 确保后面的值覆盖前面的 + } + + @Test + public void testToMap_NullKeyOrValue() { + List 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() { + List list = new ArrayList<>(); + for (long i = 0; i < 10000; i++) { + list.add(new Student(1, 1, i, "学生" + i)); + } + Map map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName); + + assertEquals(10000, map.size()); + } + @Test public void testGroupByKey() { Map> map = CollStreamUtil.groupByKey(null, Student::getClassId); From 8bfefbb9ba5355b6c90938b854b24c9b47166d25 Mon Sep 17 00:00:00 2001 From: qibinhang Date: Fri, 8 Aug 2025 13:19:52 +0800 Subject: [PATCH 2/3] Add tests for covering additional, important scenarios of `CollUtil`. --- .../hutool/core/collection/CollUtilTest.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index 5615a97aa..fea503b8a 100755 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -62,6 +62,25 @@ public class CollUtilTest { assertEquals(srcList, answerList); } + @Test + public void testPadLeft_NegativeMinLen_ShouldNotModifyList() { + List list = CollUtil.newArrayList("a", "b", "c"); + List original = CollUtil.newArrayList("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() { + List list = CollUtil.newArrayList(); + + CollUtil.padLeft(list, 0, "x"); + + assertTrue(list.isEmpty(), "List should remain empty when minLen is 0"); + } + @Test public void testPadRight() { final List srcList = CollUtil.newArrayList("a"); @@ -211,6 +230,19 @@ public class CollUtilTest { final List r2 = CollUtil.subtractToList(map1.keySet(), map2.keySet()); assertEquals("[1]", r2.toString()); } + + @Test + public void testSubtractWithDuplicates() { + Collection coll1 = new ArrayList<>(Arrays.asList("a", "b", "b", "c")); + Collection coll2 = Collections.singletonList("b"); + Collection result = CollUtil.subtract(coll1, coll2); + + List expected = Arrays.asList("a", "c"); + List resultList = new ArrayList<>(result); + Collections.sort(resultList); + Collections.sort(expected); + assertEquals(expected, resultList); + } @Test public void toMapListAndToListMapTest() { @@ -825,6 +857,34 @@ public class CollUtilTest { assertEquals(2, i); } + @Test + public void lastIndexOf_NoMatchExists() { + List list = CollUtil.newArrayList("a", "b", "c"); + int idx = CollUtil.lastIndexOf(list, item -> item.equals("z")); + assertEquals(-1, idx); + } + + @Test + public void lastIndexOf_MatcherIsNull_MatchAll() { + List list = CollUtil.newArrayList("x", "y", "z"); + int idx = CollUtil.lastIndexOf(list, null); + assertEquals(2, idx); + } + + @Test + public void lastIndexOf_EmptyCollection() { + List list = CollUtil.newArrayList(); + int idx = CollUtil.lastIndexOf(list, item -> item != null); + assertEquals(-1, idx); + } + + @Test + public void lastIndexOf_SingletonCollection_Match() { + List list = CollUtil.newArrayList("foo"); + int idx = CollUtil.lastIndexOf(list, item -> item.equals("foo")); + assertEquals(0, idx); + } + @Test public void pageTest() { final List objects = CollUtil.newArrayList(); From f3d95e908bdf9b5c7a2280db422a518e73fae4c8 Mon Sep 17 00:00:00 2001 From: qibinhang Date: Fri, 8 Aug 2025 13:20:08 +0800 Subject: [PATCH 3/3] Add tests for covering additional, important scenarios of `FileUtil`. --- .../java/cn/hutool/core/io/FileUtilTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/hutool-core/src/test/java/cn/hutool/core/io/FileUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/io/FileUtilTest.java index 056a209b3..8405af6ca 100644 --- a/hutool-core/src/test/java/cn/hutool/core/io/FileUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/io/FileUtilTest.java @@ -528,6 +528,44 @@ public class FileUtilTest { assertTrue(FileUtil.isSub(file, file2)); } + @Test + public void isSub_SubIsAncestorOfParentTest() { + File parent = new File("d:/home/user/docs/notes"); + File sub = new File("d:/home/user/docs"); + assertFalse(FileUtil.isSub(parent, sub)); + } + + @Test + public void isSub_SamePathTest() { + File parent = new File("d:/home/user/docs"); + File sub = new File("d:/home/user/docs"); + assertTrue(FileUtil.isSub(parent, sub)); + } + + @Test + public void isSub_NonexistentPathsTest() { + File parent = new File("d:/unlikely/to/exist/parent"); + File sub = new File("d:/unlikely/to/exist/parent/child/file.txt"); + assertTrue(FileUtil.isSub(parent, sub)); + + 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); + }); + } + @Test @Disabled public void appendLinesTest(){