Merge pull request #4007 from qibinhang/v5-dev

新增测试用例以测试更多重要场景
This commit is contained in:
Golden Looly
2025-08-11 10:59:23 +08:00
committed by GitHub
3 changed files with 132 additions and 0 deletions

View File

@@ -60,6 +60,40 @@ public class CollStreamUtilTest {
assertNull(map.get(4L));
}
@Test
public void testToMap_KeyCollision_SilentlyOverwrite() {
List<Student> list = new ArrayList<>();
list.add(new Student(1, 101, 1, "张三"));
list.add(new Student(1, 102, 1, "李四"));
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() {
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() {
List<Student> list = new ArrayList<>();
for (long i = 0; i < 10000; i++) {
list.add(new Student(1, 1, i, "学生" + i));
}
Map<Long, String> map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName);
assertEquals(10000, map.size());
}
@Test
public void testGroupByKey() {
Map<Long, List<Student>> map = CollStreamUtil.groupByKey(null, Student::getClassId);

View File

@@ -62,6 +62,25 @@ public class CollUtilTest {
assertEquals(srcList, answerList);
}
@Test
public void testPadLeft_NegativeMinLen_ShouldNotModifyList() {
List<String> list = CollUtil.newArrayList("a", "b", "c");
List<String> 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<String> 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<String> srcList = CollUtil.newArrayList("a");
@@ -211,6 +230,19 @@ public class CollUtilTest {
final List<String> r2 = CollUtil.subtractToList(map1.keySet(), map2.keySet());
assertEquals("[1]", r2.toString());
}
@Test
public void testSubtractWithDuplicates() {
Collection<String> coll1 = new ArrayList<>(Arrays.asList("a", "b", "b", "c"));
Collection<String> coll2 = Collections.singletonList("b");
Collection<String> result = CollUtil.subtract(coll1, coll2);
List<String> expected = Arrays.asList("a", "c");
List<String> 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<String> 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<String> list = CollUtil.newArrayList("x", "y", "z");
int idx = CollUtil.lastIndexOf(list, null);
assertEquals(2, idx);
}
@Test
public void lastIndexOf_EmptyCollection() {
List<String> list = CollUtil.newArrayList();
int idx = CollUtil.lastIndexOf(list, item -> item != null);
assertEquals(-1, idx);
}
@Test
public void lastIndexOf_SingletonCollection_Match() {
List<String> list = CollUtil.newArrayList("foo");
int idx = CollUtil.lastIndexOf(list, item -> item.equals("foo"));
assertEquals(0, idx);
}
@Test
public void pageTest() {
final List<Dict> objects = CollUtil.newArrayList();

View File

@@ -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(){