1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8164389
  27  * @summary walk entries in a jdk.nio.zipfs.JarFileSystem
  28  * @modules jdk.jartool/sun.tools.jar
  29  * @run testng JFSTester
  30  */
  31 
  32 import org.testng.Assert;
  33 import org.testng.annotations.AfterClass;
  34 import org.testng.annotations.BeforeClass;
  35 import org.testng.annotations.Test;
  36 
  37 import java.io.IOException;
  38 import java.io.UncheckedIOException;
  39 import java.net.URI;
  40 import java.nio.file.FileSystem;
  41 import java.nio.file.FileSystems;
  42 import java.nio.file.Files;
  43 import java.nio.file.Path;
  44 import java.nio.file.Paths;
  45 import java.util.HashMap;
  46 import java.util.Map;
  47 import java.util.Set;
  48 import java.util.stream.Collectors;
  49 
  50 public class JFSTester {
  51     private URI jarURI;
  52     private Path jarfile;
  53 
  54     @BeforeClass
  55     public void initialize() throws Exception {
  56         String userdir = System.getProperty("user.dir",".");
  57         jarfile = Paths.get(userdir, "test.jar");
  58         String srcdir = System.getProperty("test.src");
  59         String[] args = (
  60                         "-cf "
  61                         + jarfile.toString()
  62                         + " -C "
  63                         + srcdir
  64                         + " root --release 9 -C "
  65                         + srcdir
  66                         + System.getProperty("file.separator")
  67                         + "v9 root"
  68         ).split(" +");
  69         new sun.tools.jar.Main(System.out, System.err, "jar").run(args);
  70         String ssp = jarfile.toUri().toString();
  71         jarURI = new URI("jar", ssp, null);
  72     }
  73 
  74     @AfterClass
  75     public void close() throws IOException {
  76         Files.deleteIfExists(jarfile);
  77     }
  78 
  79     @Test
  80     public void testWalk() throws IOException {
  81 
  82         // no configuration, treat multi-release jar as unversioned
  83         Map<String,String> env = new HashMap<>();
  84         Set<String> contents = doTest(env);
  85         Set<String> baseContents = Set.of(
  86                 "This is leaf 1.\n",
  87                 "This is leaf 2.\n",
  88                 "This is leaf 3.\n",
  89                 "This is leaf 4.\n"
  90         );
  91         Assert.assertEquals(contents, baseContents);
  92 
  93         // a configuration and jar file is multi-release
  94         env.put("multi-release", "9");
  95         contents = doTest(env);
  96         Set<String> versionedContents = Set.of(
  97                 "This is versioned leaf 1.\n",
  98                 "This is versioned leaf 2.\n",
  99                 "This is versioned leaf 3.\n",
 100                 "This is versioned leaf 4.\n"
 101         );
 102         Assert.assertEquals(contents, versionedContents);
 103     }
 104 
 105     private Set<String> doTest(Map<String,String> env) throws IOException {
 106         Set<String> contents;
 107         try (FileSystem fs = FileSystems.newFileSystem(jarURI, env)) {
 108             Path root = fs.getPath("root");
 109             contents = Files.walk(root)
 110                     .filter(p -> !Files.isDirectory(p))
 111                     .map(this::pathToContents)
 112                     .collect(Collectors.toSet());
 113         }
 114         return contents;
 115     }
 116 
 117     private String pathToContents(Path path) {
 118         try {
 119             return new String(Files.readAllBytes(path));
 120         } catch (IOException x) {
 121             throw new UncheckedIOException(x);
 122         }
 123     }
 124 }