Improve ZipUtils tests. (#12946)
Fix ZipUtilsWriteTest for Windows, slashes get converted inside ZipUtils. Add test for unzipping. This was not explicitely tested. Signed-off-by: Gerben Oolbekkink <g.j.w.oolbekkink@gmail.com>7.20.x
parent
00c16c875c
commit
c5498c2ac5
|
|
@ -21,6 +21,9 @@ import java.io.FileOutputStream;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Random;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
|
|
@ -33,19 +36,21 @@ import org.testng.annotations.Test;
|
|||
|
||||
public class ZipUtilsTest {
|
||||
|
||||
public static final String FILE_NAME = "test";
|
||||
private File zipFile;
|
||||
private byte[] testData;
|
||||
|
||||
@BeforeMethod
|
||||
public void setUp() throws IOException {
|
||||
zipFile = File.createTempFile("test", "zip");
|
||||
zipFile.deleteOnExit();
|
||||
|
||||
byte[] testData = new byte[2048];
|
||||
testData = new byte[2048];
|
||||
Random random = new Random();
|
||||
random.nextBytes(testData);
|
||||
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
|
||||
ZipEntry entry = new ZipEntry("test");
|
||||
ZipEntry entry = new ZipEntry(FILE_NAME);
|
||||
entry.setSize(testData.length);
|
||||
zos.putNextEntry(entry);
|
||||
zos.write(testData);
|
||||
|
|
@ -70,4 +75,13 @@ public class ZipUtilsTest {
|
|||
|
||||
verify(consumer, times(2)).accept(any(InputStream.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnzipFile() throws Exception {
|
||||
Path targetDir = Paths.get(zipFile.getParent());
|
||||
ZipUtils.unzip(zipFile, targetDir.toFile());
|
||||
Path unzippedFile = Paths.get(targetDir.toString(), FILE_NAME);
|
||||
Assert.assertEquals(testData, Files.readAllBytes(unzippedFile));
|
||||
Files.delete(unzippedFile);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import java.io.IOException;
|
|||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Collection;
|
||||
|
|
@ -88,7 +89,7 @@ public class ZipUtilsWriteTest {
|
|||
}
|
||||
Collection<String> entries = listEntries(zipFile.toFile());
|
||||
assertTrue(entries.contains("foo.bar"));
|
||||
assertTrue(entries.contains("inner/temp.bla"));
|
||||
assertTrue(entries.contains(Paths.get("inner/temp.bla").toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -98,7 +99,7 @@ public class ZipUtilsWriteTest {
|
|||
}
|
||||
Collection<String> entries = listEntries(zipFile.toFile());
|
||||
String tempFileName = tempDir.getFileName().toString();
|
||||
assertTrue(entries.contains(tempFileName + "/foo.bar"));
|
||||
assertTrue(entries.contains(tempFileName + "/inner/temp.bla"));
|
||||
assertTrue(entries.contains(Paths.get(tempFileName + "/foo.bar").toString()));
|
||||
assertTrue(entries.contains(Paths.get(tempFileName + "/inner/temp.bla").toString()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue