파일 압축 방법을 구현했습니다. 이 방법은 하위 폴더를 압축하여 기존 zip 파일을 덮어 쓸 수 있습니다. 이제이 메서드에 대한 단위 테스트를 만들고 싶습니다. 그래서 내가 덮어 쓰기를 테스트 할 수있는 몇 가지 내용으로 기본 압축 파일로 시작 등폴더의 JUnit 테스트 압축
소스 파일 설정하는 나의 방법 :
@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 파일이 올바른 구조 (파일이 파일에 존재해야 함)가 있는지 제대로 확인하는 방법에 대해서는 다소 분실했습니다. 현재 나는 경로를 확인하고 있지만 이것은 너무 잘못되어서 알고 있습니다. 누구든지 이런 종류의 방법을 적절하게 테스트하는 방법에 대해 나에게 계몽 수 있습니까?
이것은 약간 분명하지 않습니다. 압축을 풀고 내용을 확인할 수 있습니까? –
@OliverCharlesworth 분명히 할 수 있겠지만, 아마도 경로가 엉망이 될 것이고 나는 ** ** ** zip 파일 안에 있는지 확인하고 싶을 것입니다. – XtremeBaumer
'ZipFileUtil'이 당신이 작성한 클래스가 아니라는 것이 맞습니까? –