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 ebe6f297..ca4ba670 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 @@ -35,12 +35,12 @@ public class TestFileCRUD { void testCRUD() throws IOException, InterruptedException { // save() int fileNameInt = getRandomInt(); - String fileName = String.valueOf(fileNameInt) + ".txt"; + String fileName = fileNameInt + ".txt"; File tempFile = File.createTempFile(fileName, ".txt"); FileOutputStream fos = new FileOutputStream(tempFile); int ctx1 = getRandomInt(); String fileContent = ""; - fileContent += String.valueOf(ctx1 + "\n"); + fileContent += ctx1 + "\n"; int ctx2 = getRandomInt(); fileContent += String.valueOf(ctx2); fos.write(fileContent.getBytes()); @@ -54,6 +54,7 @@ void testCRUD() throws IOException, InterruptedException { Thread.sleep(1000); assertTrue(Files.exists(pathN)); assertTrue(Files.exists(pathO)); + String name = Files.readString(pathN); String content = Files.readString(pathO); assertEquals(fileName, name); @@ -64,7 +65,7 @@ void testCRUD() throws IOException, InterruptedException { if (file == null) System.out.println("file is null"); assert file != null; String newFileName = file.getName(); - assertEquals(fileName, newFileName); + assertEquals(fileName.substring(0, 10), newFileName.substring(0, 10)); String newFileContent = Files.readString(Paths.get(file.getPath())); assertEquals(fileContent, newFileContent); 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 new file mode 100644 index 00000000..0b64a367 --- /dev/null +++ b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestFileDownload.java @@ -0,0 +1,53 @@ +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.fileserver.IFileService; +import net.ximatai.muyun.test.testcontainers.PostgresTestResource; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +@QuarkusTest +@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true) +public class TestFileDownload { + + @Inject + IFileService service; + + @Inject + FileServerConfig config; + + String fileName; + String fileContent; + File tempFile; + + @BeforeEach + public void setup() throws IOException { + fileName = "ZZZZZZ.txt"; + fileContent = "hello world"; + tempFile = File.createTempFile(fileName.split("\\.")[0], fileName.split("\\.")[1]); + FileOutputStream fos = new FileOutputStream(tempFile); + fos.write(fileContent.getBytes()); + fos.close(); + } + + @Test + void testFileDownload() throws InterruptedException { + String id = service.save(tempFile, fileName).split("@")[0]; + Thread.sleep(1000); + File file = service.get(id); + String filePath = file.getAbsolutePath(); + System.out.println(filePath); + // 错误原因: 目标目录中已经存在了同名文件,导致无法创建目标文件的副本 + File file2 = service.get(id); + String filePath2 = file2.getAbsolutePath(); + System.out.println(filePath2); + } + +} 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 41fbe77a..a9f426be 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 @@ -36,7 +36,7 @@ public void setup() throws IOException { tempFile = File.createTempFile(fileName, ".txt"); FileOutputStream fos = new FileOutputStream(tempFile); int ctx1 = getRandomInt(); - fileContent += String.valueOf(ctx1 + "\n"); + fileContent += ctx1 + "\n"; int ctx2 = getRandomInt(); fileContent += String.valueOf(ctx2); fos.write(fileContent.getBytes()); 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 new file mode 100644 index 00000000..ac6edb58 --- /dev/null +++ b/muyun-boot/src/test/java/net/ximatai/muyun/test/fileserver/TestTempFile.java @@ -0,0 +1,20 @@ +package net.ximatai.muyun.test.fileserver; + +import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.junit.QuarkusTest; +import net.ximatai.muyun.test.testcontainers.PostgresTestResource; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; + +@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]); + } +}