gpt4 book ai didi

java - JUnit 测试文件夹压缩

转载 作者:行者123 更新时间:2023-12-02 11:55:13 25 4
gpt4 key购买 nike

我已经实现了一种压缩文件的方法。此方法能够压缩子文件夹、覆盖现有的 zip 文件等等。现在我想为此方法创建单元测试。我从包含一些内容的默认 zip 文件开始,这样我就可以测试覆盖等。

我设置源文件的方法:

@Before
public void setUp() throws IOException, URISyntaxException {

util = new ZipFileUtil();

parent = folder.newFolder("Parent");
File subfolder = folder.newFolder("Parent", "Subfolder");
File sub2 = folder.newFolder("Parent", "Subfolder", "Sub2");

File f1 = File.createTempFile("file1", ".txt", parent);
File f2 = File.createTempFile("file2", ".txt", parent);
File f3 = File.createTempFile("file3", ".txt", parent);

File f4 = File.createTempFile("file4", ".txt", subfolder);
File f5 = File.createTempFile("file5", ".txt", subfolder);

File f6 = File.createTempFile("file6", ".txt", sub2);
File f7 = File.createTempFile("file7", ".txt", sub2);

File f8 = File.createTempFile("test", ".txt", sub2);

File f9 = File.createTempFile("test1", ".txt", parent);

files[0] = f1;
files[1] = f2;
files[2] = f3;
files[3] = f4;
files[4] = f5;
files[5] = f6;
files[6] = f7;
files[7] = f8;
files[8] = f9;

File file = new File(getClass().getClassLoader().getResource("test.zip").toURI());

Files.copy(file.toPath(), Paths.get(folder.getRoot().toString(), "test.zip"),
StandardCopyOption.REPLACE_EXISTING);
}

我的测试:

@Test
public void testOverwriteFilesInExistingZip() throws IOException {
util.setZipNameGenerator(new ZipNamingStrategy() {

@Override
public String getName() {
// TODO Auto-generated method stub
return "test";
}

});
util.setOverwriteFilesInExistingZip(true);
util.setSourceFolder(parent);
util.setDestinationFolder(folder.getRoot());
util.generateZip(new FilenameFilter() {

@Override
public boolean accept(File dir, String name) {
return true;

}
});
FileSystem fs = FileSystems.newFileSystem(Paths.get(folder.getRoot().getAbsolutePath(), "test.zip"), null);

assertEquals("/" + files[0].getName(),
fs.getPath("/", parent.toPath().relativize(files[0].toPath()).toString()).toString());
assertEquals("/" + files[1].getName(),
fs.getPath("/", parent.toPath().relativize(files[1].toPath()).toString()).toString());
assertEquals("/" + files[2].getName(),
fs.getPath("/", parent.toPath().relativize(files[2].toPath()).toString()).toString());
assertEquals(true,
new File(fs.getPath("/", parent.toPath().relativize(files[8].toPath()).toString()).toString()).exists());

assertEquals("/file.txt", fs.getPath("/", "file.txt").toString());
assertEquals("/New Text Document.txt", fs.getPath("/", "New Text Document.txt").toString());
assertEquals("/test.txt", fs.getPath("/", "test.txt").toString());
assertEquals("/test1.txt", fs.getPath("/", "test1.txt").toString());

assertEquals("/Subfolder/file.txt", fs.getPath("/", "test1.txt").toString());
assertEquals("/Subfolder/desktop.ini", fs.getPath("/", "test1.txt").toString());
assertEquals("/Subfolder/test.txt", fs.getPath("/", "test1.txt").toString());
assertEquals("/Subfolder/New Text Document.txt", fs.getPath("/", "test1.txt").toString());

assertEquals(15, util.getProcessedFiles());

}

我有点不知道如何正确检查 zip 文件是否具有正确的结构(文件必须存在于文件中)。目前我只是检查路径,但这是错误的,我知道。谁能告诉我如何正确测试这种方法?

最佳答案

如果您想检查 zip 文件中是否存在这些文件,可以使用 Files.exist(path) .

如果您想进一步检查实际内容,您可以解压到另一个临时目录,或者要求 Java read directly from the zip file .

关于java - JUnit 测试文件夹压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47655783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com