Reputation: 6435
I have implemented a method to zip files. This method is capable of zipping subfolder, overwriting existing zip files and some more. Now I want to create unit tests for this method. I started with a default zip-file with some content so I could test the overwriting etc.
My method to set up the source files:
@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);
}
And my test:
@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());
}
I am kinda lost on how to properly check if the zip file has the right structure (files have to exist in the file). Currently I am just checking the paths, but this is so wrong and I know it. Can anyone enlighten me on how to properly test this kind of method?
Upvotes: 0
Views: 6913
Reputation: 272517
If you want to check whether these files exist within the zip file, you can use Files.exist(path)
.
If you want to go further and check the actual content, you can either unzip into a different temporary directory, or ask Java to read directly from the zip file.
Upvotes: 2