< prev index next >

test/demo/zipfs/ZFSTests.java

Print this page

        

*** 20,43 **** * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test ! @bug 7156873 @summary ZipFileSystem regression tests */ ! import java.net.URI; import java.nio.file.*; import java.util.Map; import java.util.HashMap; public class ZFSTests { public static void main(String[] args) throws Throwable { test7156873(); } static void test7156873() throws Throwable { String DIRWITHSPACE = "testdir with spaces"; Path dir = Paths.get(DIRWITHSPACE); --- 20,53 ---- * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test ! @bug 7156873,8199776 @summary ZipFileSystem regression tests */ ! import java.io.IOException; ! import java.io.OutputStream; import java.net.URI; import java.nio.file.*; + import java.nio.file.attribute.BasicFileAttributes; + import java.util.Arrays; + import java.util.Collections; + import java.util.HashSet; import java.util.Map; import java.util.HashMap; + import java.util.Set; + import java.util.concurrent.atomic.AtomicInteger; + import java.util.zip.ZipEntry; + import java.util.zip.ZipOutputStream; public class ZFSTests { public static void main(String[] args) throws Throwable { test7156873(); + test8199776(); } static void test7156873() throws Throwable { String DIRWITHSPACE = "testdir with spaces"; Path dir = Paths.get(DIRWITHSPACE);
*** 51,56 **** --- 61,172 ---- } finally { Files.deleteIfExists(path); Files.deleteIfExists(dir); } } + + static void test8199776() throws Throwable { + + // root entry "/" + Path path = Paths.get("rootdir.zip"); + URI uri = URI.create("jar:" + path.toUri()); + + Set<String> dirs = new HashSet<>(); + Set<String> files = new HashSet<>(); + try (OutputStream os = Files.newOutputStream(path); + ZipOutputStream zos = new ZipOutputStream(os)) { + zos.putNextEntry(new ZipEntry("/")); + dirs.add("/"); + zos.putNextEntry(new ZipEntry("foo")); + files.add("/foo"); + zos.write("/foo".getBytes()); + zos.putNextEntry(new ZipEntry("bar")); + files.add("/bar"); + zos.write("/bar".getBytes()); + } + AtomicInteger cnt = new AtomicInteger(); + int max = 3; + try (FileSystem fs = FileSystems.newFileSystem(uri, + Collections.emptyMap())) { + Files.walkFileTree(fs.getRootDirectories().iterator().next(), + new SimpleFileVisitor<Path>() { + + @Override + public FileVisitResult visitFile(Path file, + BasicFileAttributes attrs) throws IOException { + if (cnt.incrementAndGet() > max) + throw new RuntimeException( + "visited too many files/dirs"); + files.remove(file.toString()); + if (!Arrays.equals(Files.readAllBytes(file), file + .toString().getBytes())) + throw new RuntimeException( + "visited files has wrong content: " + + file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, + BasicFileAttributes attrs) throws IOException { + if (cnt.incrementAndGet() > max) + throw new RuntimeException( + "visited too many files/dirs"); + dirs.remove(dir.toString()); + return FileVisitResult.CONTINUE; + } + }); + if (cnt.get() != max || dirs.size() != 0 || files.size() != 0) + throw new RuntimeException("walk files/dirs failed"); + + } finally { + Files.deleteIfExists(path); + } + + // absolute file/dir, no need to test cnt again.. + dirs.clear(); + files.clear(); + try (OutputStream os = Files.newOutputStream(path); + ZipOutputStream zos = new ZipOutputStream(os)) { + zos.putNextEntry(new ZipEntry("/")); + dirs.add("/"); + zos.putNextEntry(new ZipEntry("/fooo/")); + dirs.add("/fooo/"); + zos.putNextEntry(new ZipEntry("/foo")); + files.add("/foo"); + zos.write("/foo".getBytes()); + zos.putNextEntry(new ZipEntry("/bar")); + files.add("/bar"); + zos.write("/bar".getBytes()); + zos.putNextEntry(new ZipEntry("/fooo/bar")); + files.add("/fooo/bar"); + zos.write("/fooo/bar".getBytes()); + } + + try (FileSystem fs = FileSystems.newFileSystem(uri, + Collections.emptyMap())) { + Files.walkFileTree(fs.getRootDirectories().iterator().next(), + new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult visitFile(Path file, + BasicFileAttributes attrs) throws IOException { + files.remove(file.toString()); + if (!Arrays.equals(Files.readAllBytes(file), file + .toString().getBytes())) + throw new RuntimeException( + "visited files has wrong content: " + + file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, + BasicFileAttributes attrs) throws IOException { + dirs.remove(dir.toString()); + return FileVisitResult.CONTINUE; + } + }); + if (dirs.size() != 0 || files.size() != 0) + throw new RuntimeException("walk files/dirs failed"); + } + } }
< prev index next >