我已经实现了一个压缩文件的方法。此方法能够压缩子文件夹,覆盖现有的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文件是否具有正确的结构(文件必须存在于文件中)。目前我只是检查路径,但这是错误的,我知道。任何人都可以告诉我如何正确测试这种方法吗?
答案 0 :(得分:1)
如果您想检查zip文件中的这些文件是否存在,可以使用Files.exist(path)
。
如果您想进一步检查实际内容,可以解压缩到另一个临时目录,或者向Java read directly from the zip file询问。