8164389: jdk.nio.zipfs.JarFileSystem does not completely traverse the versioned entries in a multi-release jar file
authorsdrach
Thu, 18 Aug 2016 17:18:21 -0700
changeset 40528 c2d4a13ab15a
parent 40527 542d72803808
child 40529 7bf112d991eb
8164389: jdk.nio.zipfs.JarFileSystem does not completely traverse the versioned entries in a multi-release jar file Reviewed-by: psandoz Contributed-by: steve.drach@oracle.com
jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/JarFileSystem.java
jdk/test/jdk/nio/zipfs/jarfs/JFSTester.java
jdk/test/jdk/nio/zipfs/jarfs/root/dir1/leaf1.txt
jdk/test/jdk/nio/zipfs/jarfs/root/dir1/leaf2.txt
jdk/test/jdk/nio/zipfs/jarfs/root/dir2/leaf3.txt
jdk/test/jdk/nio/zipfs/jarfs/root/dir2/leaf4.txt
jdk/test/jdk/nio/zipfs/jarfs/v9/root/dir1/leaf1.txt
jdk/test/jdk/nio/zipfs/jarfs/v9/root/dir1/leaf2.txt
jdk/test/jdk/nio/zipfs/jarfs/v9/root/dir2/leaf3.txt
jdk/test/jdk/nio/zipfs/jarfs/v9/root/dir2/leaf4.txt
--- a/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/JarFileSystem.java	Mon Aug 22 22:16:54 2016 +0300
+++ b/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/JarFileSystem.java	Thu Aug 18 17:18:21 2016 -0700
@@ -165,8 +165,8 @@
             walk(inode.child, process);
         } else {
             process.accept(inode);
-            walk(inode.sibling, process);
         }
+        walk(inode.sibling, process);
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/nio/zipfs/jarfs/JFSTester.java	Thu Aug 18 17:18:21 2016 -0700
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8164389
+ * @summary walk entries in a jdk.nio.zipfs.JarFileSystem
+ * @modules jdk.jartool/sun.tools.jar
+ * @run testng JFSTester
+ */
+
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.URI;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class JFSTester {
+    private URI jarURI;
+    private Path jarfile;
+
+    @BeforeClass
+    public void initialize() throws Exception {
+        String userdir = System.getProperty("user.dir",".");
+        jarfile = Paths.get(userdir, "test.jar");
+        String srcdir = System.getProperty("test.src");
+        String[] args = (
+                        "-cf "
+                        + jarfile.toString()
+                        + " -C "
+                        + srcdir
+                        + " root --release 9 -C "
+                        + srcdir
+                        + System.getProperty("file.separator")
+                        + "v9 root"
+        ).split(" +");
+        new sun.tools.jar.Main(System.out, System.err, "jar").run(args);
+        String ssp = jarfile.toUri().toString();
+        jarURI = new URI("jar", ssp, null);
+    }
+
+    @AfterClass
+    public void close() throws IOException {
+        Files.deleteIfExists(jarfile);
+    }
+
+    @Test
+    public void testWalk() throws IOException {
+
+        // no configuration, treat multi-release jar as unversioned
+        Map<String,String> env = new HashMap<>();
+        Set<String> contents = doTest(env);
+        Set<String> baseContents = Set.of(
+                "This is leaf 1.\n",
+                "This is leaf 2.\n",
+                "This is leaf 3.\n",
+                "This is leaf 4.\n"
+        );
+        Assert.assertEquals(contents, baseContents);
+
+        // a configuration and jar file is multi-release
+        env.put("multi-release", "9");
+        contents = doTest(env);
+        Set<String> versionedContents = Set.of(
+                "This is versioned leaf 1.\n",
+                "This is versioned leaf 2.\n",
+                "This is versioned leaf 3.\n",
+                "This is versioned leaf 4.\n"
+        );
+        Assert.assertEquals(contents, versionedContents);
+    }
+
+    private Set<String> doTest(Map<String,String> env) throws IOException {
+        Set<String> contents;
+        try (FileSystem fs = FileSystems.newFileSystem(jarURI, env)) {
+            Path root = fs.getPath("root");
+            contents = Files.walk(root)
+                    .filter(p -> !Files.isDirectory(p))
+                    .map(this::pathToContents)
+                    .collect(Collectors.toSet());
+        }
+        return contents;
+    }
+
+    private String pathToContents(Path path) {
+        try {
+            return new String(Files.readAllBytes(path));
+        } catch (IOException x) {
+            throw new UncheckedIOException(x);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/nio/zipfs/jarfs/root/dir1/leaf1.txt	Thu Aug 18 17:18:21 2016 -0700
@@ -0,0 +1,1 @@
+This is leaf 1.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/nio/zipfs/jarfs/root/dir1/leaf2.txt	Thu Aug 18 17:18:21 2016 -0700
@@ -0,0 +1,1 @@
+This is leaf 2.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/nio/zipfs/jarfs/root/dir2/leaf3.txt	Thu Aug 18 17:18:21 2016 -0700
@@ -0,0 +1,1 @@
+This is leaf 3.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/nio/zipfs/jarfs/root/dir2/leaf4.txt	Thu Aug 18 17:18:21 2016 -0700
@@ -0,0 +1,1 @@
+This is leaf 4.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/nio/zipfs/jarfs/v9/root/dir1/leaf1.txt	Thu Aug 18 17:18:21 2016 -0700
@@ -0,0 +1,1 @@
+This is versioned leaf 1.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/nio/zipfs/jarfs/v9/root/dir1/leaf2.txt	Thu Aug 18 17:18:21 2016 -0700
@@ -0,0 +1,1 @@
+This is versioned leaf 2.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/nio/zipfs/jarfs/v9/root/dir2/leaf3.txt	Thu Aug 18 17:18:21 2016 -0700
@@ -0,0 +1,1 @@
+This is versioned leaf 3.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/nio/zipfs/jarfs/v9/root/dir2/leaf4.txt	Thu Aug 18 17:18:21 2016 -0700
@@ -0,0 +1,1 @@
+This is versioned leaf 4.