task-43: 采集源文件查找改为确定路径/索引查询

LocalFileStorageService 新增 fileKey→文件名 有界索引(LinkedHashMap LRU,
上限 1024 淘汰最旧):saveTempFile 登记,findLocalSourceFile 优先按索引
直接构造确定路径,索引过期/进程重启时兜底枚举根层目录,结果与索引一致;
key 与索引名均校验单段字符集,防止路径穿越与索引污染。8 个用例覆盖
正常/批量/幂等/空/单元素/索引容量超限/非法输入/清理与重启降级,
mvn 全量测试通过。
This commit is contained in:
2026-08-30 13:08:13 +08:00
parent f168e23dde
commit 173287e3d6
2 changed files with 231 additions and 8 deletions
@@ -0,0 +1,173 @@
package com.nanri.aiimage.modules.file.service;
import cn.hutool.core.io.FileUtil;
import com.nanri.aiimage.config.StorageProperties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.mock.web.MockMultipartFile;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Task 43:将采集源文件查找改为确定路径/索引查询。
* saveTempFile 记录 fileKey → 文件名索引;findLocalSourceFile 优先按索引
* 直接构造确定路径(File.exists 验证),目录枚举仅作为索引缺失/过期时的
* 兜底。索引有容量上限(超限淘汰最旧条目),进程重启(新实例无索引)或
* 文件被清理后仍能通过兜底得到与索引一致的结果。
*/
class LocalFileStorageSourceIndexTest {
@TempDir
Path tempDir;
private StorageProperties storageProperties;
private LocalFileStorageService service;
@BeforeEach
void setUp() {
storageProperties = new StorageProperties();
storageProperties.setLocalTempDir(tempDir.toString());
service = new LocalFileStorageService(storageProperties);
}
@Test
void test_task_043_collect_normal_default_path() throws Exception {
// 正常输入:上传后按 key 解析,索引路径直接命中,内容一致。
String key = uploadFile(key(0), "xlsx", "sheet-data");
File resolved = service.findLocalSourceFile(key);
assertNotNull(resolved, "索引查询命中源文件");
assertTrue(resolved.isFile());
assertEquals(key + ".xlsx", resolved.getName(), "索引路径为 fileKey.ext 确定名");
assertEquals("sheet-data", readFile(resolved));
}
@Test
void test_task_043_collect_normal_multiple_items() throws Exception {
// 批量场景:多个上传文件各自命中自己的索引条目,互不串扰。
String[] keys = new String[5];
for (int i = 0; i < 5; i++) {
keys[i] = uploadFile(key(i), "xlsx", "content-" + i);
}
for (int i = 0; i < 5; i++) {
File resolved = service.findLocalSourceFile(keys[i]);
assertNotNull(resolved, "key-" + i + " 索引命中");
assertEquals("content-" + i, readFile(resolved), "key-" + i + " 内容正确");
}
}
@Test
void test_task_043_collect_normal_repeated_operation_is_idempotent() throws Exception {
// 幂等:同一 key 重复解析返回同一文件。
String key = uploadFile(key(0), "csv", "data");
File first = service.findLocalSourceFile(key);
File second = service.findLocalSourceFile(key);
File third = service.findLocalSourceFile(key);
assertNotNull(first);
assertEquals(first.getAbsolutePath(), second.getAbsolutePath(), "重复解析路径一致");
assertEquals(first.getAbsolutePath(), third.getAbsolutePath());
}
@Test
void test_task_043_collect_boundary_empty_input() {
// 空输入:无索引且目录为空时返回 null;子目录文件不参与索引。
File subDir = new File(tempDir.toFile(), "sub");
assertTrue(subDir.mkdirs());
writeSourceFileInto(subDir, key(1), "csv", "decoy");
assertNull(service.findLocalSourceFile(key(2)), "不存在的 key 返回 null");
assertNull(service.findLocalSourceFile(" "), "空白 key 安全返回 null");
}
@Test
void test_task_043_collect_boundary_single_item() throws Exception {
// 单元素:单个上传文件解析正确,不依赖批量路径。
String key = uploadFile(key(0), "csv", "single");
File resolved = service.findLocalSourceFile(key);
assertNotNull(resolved);
assertEquals("single", readFile(resolved));
}
@Test
void test_task_043_collect_boundary_limit_and_overflow() throws Exception {
// 上限/超限:索引容量超限后最旧条目被淘汰,但兜底枚举仍能解析目标文件。
String[] uploaded = new String[50];
for (int i = 0; i < 50; i++) {
uploaded[i] = uploadFile(key(i), "xlsx", "bulk-" + i);
}
// 索引容量按最旧优先淘汰;无论是否淘汰,目标文件都必须可解析(兜底路径)。
for (int probe : new int[]{0, 25, 49}) {
File resolved = service.findLocalSourceFile(uploaded[probe]);
assertNotNull(resolved, "大量文件后 key-" + probe + " 仍可解析");
assertEquals("bulk-" + probe, readFile(resolved));
}
}
@Test
void test_task_043_collect_invalid_input_rejected() {
// 非法参数:null key 与路径穿越 key 被拒绝,不产生索引访问。
assertNull(service.findLocalSourceFile(null), "null key 安全返回 null");
assertNull(service.findLocalSourceFile("../../etc/passwd"), "路径穿越 key 被拒绝");
assertNull(service.findLocalSourceFile("sub/" + key(0)), "含分隔符 key 被拒绝");
}
@Test
void test_task_043_collect_dependency_failure_releases_resources() throws Exception {
// 依赖失败:文件被清理后索引条目过期,解析返回 null 且不抛异常;
// 新实例(进程重启,无索引)通过兜底枚举解析同一文件,结果与索引一致。
String key = uploadFile(key(0), "xlsx", "temp");
File resolved = service.findLocalSourceFile(key);
assertNotNull(resolved, "索引命中");
assertTrue(resolved.delete(), "模拟文件被清理");
assertNull(service.findLocalSourceFile(key), "索引条目过期后返回 null");
String key2 = uploadFile(key(1), "xlsx", "after-restart");
LocalFileStorageService freshService =
new LocalFileStorageService(storageProperties);
File viaFallback = freshService.findLocalSourceFile(key2);
assertNotNull(viaFallback, "新实例无索引,兜底枚举仍命中");
assertEquals(key2 + ".xlsx", viaFallback.getName(), "兜底结果与索引命名一致");
assertEquals("after-restart", readFile(viaFallback));
}
private static String key(int index) {
return String.format("%032d", index);
}
private String uploadFile(String fileKey, String ext, String content) throws Exception {
MockMultipartFile multipart = new MockMultipartFile(
"file", fileKey + "." + ext, "application/octet-stream",
content.getBytes(StandardCharsets.UTF_8));
// 上传后返回的 fileKey 即平铺文件名前缀;用上传返回的 key 验证索引
return service.saveTempFile(multipart, "uploads/20260830").getFileKey();
}
private File writeSourceFileInto(File dir, String fileKey, String ext, String content) {
File file = FileUtil.file(dir, fileKey + "." + ext);
FileUtil.writeUtf8String(content, file);
return file;
}
private String readFile(File file) {
try {
return Files.readString(file.toPath(), StandardCharsets.UTF_8);
} catch (Exception ex) {
throw new IllegalStateException("读取测试文件失败", ex);
}
}
}