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 * jdk.zipfs 30 * @run testng JFSTester 31 */ 32 33import org.testng.Assert; 34import org.testng.annotations.AfterClass; 35import org.testng.annotations.BeforeClass; 36import org.testng.annotations.Test; 37 38import java.io.IOException; 39import java.io.UncheckedIOException; 40import java.net.URI; 41import java.nio.file.FileSystem; 42import java.nio.file.FileSystems; 43import java.nio.file.Files; 44import java.nio.file.Path; 45import java.nio.file.Paths; 46import java.util.HashMap; 47import java.util.Map; 48import java.util.Set; 49import java.util.stream.Collectors; 50 51public class JFSTester { 52 private URI jarURI; 53 private Path jarfile; 54 55 @BeforeClass 56 public void initialize() throws Exception { 57 String userdir = System.getProperty("user.dir","."); 58 jarfile = Paths.get(userdir, "test.jar"); 59 String srcdir = System.getProperty("test.src"); 60 String[] args = ( 61 "-cf " 62 + jarfile.toString() 63 + " -C " 64 + srcdir 65 + " root --release 9 -C " 66 + srcdir 67 + System.getProperty("file.separator") 68 + "v9 root" 69 ).split(" +"); 70 new sun.tools.jar.Main(System.out, System.err, "jar").run(args); 71 String ssp = jarfile.toUri().toString(); 72 jarURI = new URI("jar", ssp, null); 73 } 74 75 @AfterClass 76 public void close() throws IOException { 77 Files.deleteIfExists(jarfile); 78 } 79 80 @Test 81 public void testWalk() throws IOException { 82 83 // no configuration, treat multi-release jar as unversioned 84 Map<String,String> env = new HashMap<>(); 85 Set<String> contents = doTest(env); 86 Set<String> baseContents = Set.of( 87 "This is leaf 1.\n", 88 "This is leaf 2.\n", 89 "This is leaf 3.\n", 90 "This is leaf 4.\n" 91 ); 92 Assert.assertEquals(contents, baseContents); 93 94 // a configuration and jar file is multi-release 95 env.put("multi-release", "9"); 96 contents = doTest(env); 97 Set<String> versionedContents = Set.of( 98 "This is versioned leaf 1.\n", 99 "This is versioned leaf 2.\n", 100 "This is versioned leaf 3.\n", 101 "This is versioned leaf 4.\n" 102 ); 103 Assert.assertEquals(contents, versionedContents); 104 } 105 106 private Set<String> doTest(Map<String,String> env) throws IOException { 107 Set<String> contents; 108 try (FileSystem fs = FileSystems.newFileSystem(jarURI, env)) { 109 Path root = fs.getPath("root"); 110 contents = Files.walk(root) 111 .filter(p -> !Files.isDirectory(p)) 112 .map(this::pathToContents) 113 .collect(Collectors.toSet()); 114 } 115 return contents; 116 } 117 118 private String pathToContents(Path path) { 119 try { 120 return new String(Files.readAllBytes(path)); 121 } catch (IOException x) { 122 throw new UncheckedIOException(x); 123 } 124 } 125} 126