Skip to content

Commit

Permalink
test-fileServer-增加检查文件夹测试,清理由单元测试产生的临时文件
Browse files Browse the repository at this point in the history
  • Loading branch information
Ning19230223 committed Oct 25, 2024
1 parent de34acd commit c5e725a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -84,6 +84,8 @@ void testCRUD() throws IOException, InterruptedException {
assertTrue(isDeleted);
boolean isDeleted2 = file.delete();
assertTrue(isDeleted2);

tempFile.deleteOnExit();
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void tearDown() {
.get(deleteUrl)
.then()
.statusCode(200);
tempFile.delete();
tempFile.deleteOnExit();
}

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

0 comments on commit c5e725a

Please sign in to comment.