Skip to content

Commit

Permalink
test-boot-增加临时文件命名冲突的单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Ning19230223 committed Oct 24, 2024
1 parent ea234ab commit 4ced332
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
}
}

0 comments on commit 4ced332

Please sign in to comment.