diff --git a/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileCRUD.java b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileCRUD.java index 165768f1..141d32a4 100644 --- a/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileCRUD.java +++ b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileCRUD.java @@ -54,7 +54,7 @@ void testCRUD() throws IOException, InterruptedException { Thread.sleep(2000); assertTrue(Files.exists(pathN)); assertTrue(Files.exists(pathO)); - + String name = Files.readString(pathN); String content = Files.readString(pathO); assertEquals(fileName, name); @@ -84,6 +84,8 @@ void testCRUD() throws IOException, InterruptedException { assertTrue(isDeleted); boolean isDeleted2 = file.delete(); assertTrue(isDeleted2); + + tempFile.deleteOnExit(); } /** diff --git a/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileDownload.java b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileDownload.java index 3500bfb5..8d4a797c 100644 --- a/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileDownload.java +++ b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileDownload.java @@ -22,11 +22,11 @@ public class TestFileDownload { @Inject FileServerConfig config; - + String fileName; String fileContent; File tempFile; - + @BeforeEach public void setup() throws IOException { fileName = "ZZZZZZ.txt"; @@ -48,6 +48,10 @@ void testFileDownload() throws InterruptedException { File file2 = service.get(id); String filePath2 = file2.getPath(); System.out.println(filePath2); + + service.delete(id); + file.deleteOnExit(); + file2.deleteOnExit(); } - + } diff --git a/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileServer.java b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileServer.java index a9f426be..09ee3472 100644 --- a/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileServer.java +++ b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileServer.java @@ -95,7 +95,7 @@ public void tearDown() { .get(deleteUrl) .then() .statusCode(200); - tempFile.delete(); + tempFile.deleteOnExit(); } /** diff --git a/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFolderExists.java b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFolderExists.java new file mode 100644 index 00000000..0c34a1d9 --- /dev/null +++ b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFolderExists.java @@ -0,0 +1,49 @@ +package net.ximatai.muyun.test.fileserver; + +import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import net.ximatai.muyun.fileserver.FileServerConfig; +import net.ximatai.muyun.test.testcontainers.PostgresTestResource; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +@QuarkusTest +@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true) +public class TestFolderExists { + + final Logger logger = LoggerFactory.getLogger(getClass()); + + @Inject + FileServerConfig config; + + private String getUploadPath() { + String uploadPath = config.uploadPath(); + if (!uploadPath.endsWith("/") && !uploadPath.endsWith("\\")) { + uploadPath = uploadPath + "/"; + } + return uploadPath; + } + + @Test + void testFolder() { + String folderPath = getUploadPath(); + Path path = Paths.get(folderPath); + + try { + if (Files.notExists(path)) { + Files.createDirectories(path); + System.out.println("文件夹已创建: " + folderPath); + } else { + System.out.println("文件夹已存在: " + folderPath); + } + } catch (Exception e) { + logger.error("创建文件夹失败", e); + } + } +} diff --git a/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestTempFile.java b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestTempFile.java index ac6edb58..41e898a2 100644 --- a/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestTempFile.java +++ b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestTempFile.java @@ -11,10 +11,11 @@ @QuarkusTest @QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true) public class TestTempFile { - + @Test public void test() throws IOException { String fileName = "MaNing.txt"; File tempFile = File.createTempFile(fileName.split("\\.")[0], "." + fileName.split("\\.")[1]); + tempFile.deleteOnExit(); } }