test/jdk/jdk/nio/zipfs/jarfs/JFSTester.java
changeset 54630 04b17e84c87d
parent 47216 71c04702a3d5
child 59216 47c879f478d2
equal deleted inserted replaced
54629:9ebb614d293d 54630:04b17e84c87d
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8164389
    26  * @bug 8164389 8222440
    27  * @summary walk entries in a jdk.nio.zipfs.JarFileSystem
    27  * @summary walk entries in a jdk.nio.zipfs.JarFileSystem
    28  * @modules jdk.jartool/sun.tools.jar
    28  * @library /lib/testlibrary/java/util/jar
       
    29  * @modules jdk.jartool
    29  *          jdk.zipfs
    30  *          jdk.zipfs
       
    31  * @build Compiler JarBuilder
    30  * @run testng JFSTester
    32  * @run testng JFSTester
    31  */
    33  */
    32 
    34 
    33 import org.testng.Assert;
    35 import org.testng.Assert;
    34 import org.testng.annotations.AfterClass;
       
    35 import org.testng.annotations.BeforeClass;
    36 import org.testng.annotations.BeforeClass;
    36 import org.testng.annotations.Test;
    37 import org.testng.annotations.Test;
    37 
    38 
    38 import java.io.IOException;
    39 import java.io.IOException;
    39 import java.io.UncheckedIOException;
    40 import java.io.UncheckedIOException;
    48 import java.util.Set;
    49 import java.util.Set;
    49 import java.util.stream.Collectors;
    50 import java.util.stream.Collectors;
    50 
    51 
    51 public class JFSTester {
    52 public class JFSTester {
    52     private URI jarURI;
    53     private URI jarURI;
    53     private Path jarfile;
    54 
       
    55     final private String root_dir1_leaf1_txt = "This is leaf 1." + System.lineSeparator();
       
    56     final private String root_dir1_leaf2_txt = "This is leaf 2." + System.lineSeparator();
       
    57     final private String root_dir2_leaf3_txt = "This is leaf 3." + System.lineSeparator();
       
    58     final private String root_dir2_leaf4_txt = "This is leaf 4." + System.lineSeparator();
       
    59     final private String v9_root_dir2_leaf3_txt = "This is version 9 leaf 3." + System.lineSeparator();
       
    60     final private String v9_root_dir2_leaf4_txt = "This is version 9 leaf 4." + System.lineSeparator();
       
    61     final private String v9_root_dir3_leaf5_txt = "This is version 9 leaf 5." + System.lineSeparator();
       
    62     final private String v9_root_dir3_leaf6_txt = "This is version 9 leaf 6." + System.lineSeparator();
       
    63     final private String v10_root_dir3_leaf5_txt = "This is version 10 leaf 5." + System.lineSeparator();
       
    64     final private String v10_root_dir3_leaf6_txt = "This is version 10 leaf 6." + System.lineSeparator();
    54 
    65 
    55     @BeforeClass
    66     @BeforeClass
    56     public void initialize() throws Exception {
    67     public void initialize() throws Exception {
    57         String userdir = System.getProperty("user.dir",".");
    68         Path jarfile = Paths.get("test.jar");
    58         jarfile = Paths.get(userdir, "test.jar");
    69         JarBuilder jb = new JarBuilder(jarfile.toString());
    59         String srcdir = System.getProperty("test.src");
    70         jb.addAttribute("Multi-Release", "true");
    60         String[] args = (
    71         jb.addEntry("root/dir1/leaf1.txt", root_dir1_leaf1_txt.getBytes());
    61                         "-cf "
    72         jb.addEntry("root/dir1/leaf2.txt", root_dir1_leaf2_txt.getBytes());
    62                         + jarfile.toString()
    73         jb.addEntry("root/dir2/leaf3.txt", root_dir2_leaf3_txt.getBytes());
    63                         + " -C "
    74         jb.addEntry("root/dir2/leaf4.txt", root_dir2_leaf4_txt.getBytes());
    64                         + srcdir
    75         jb.addEntry("META-INF/versions/9/root/dir2/leaf3.txt", v9_root_dir2_leaf3_txt.getBytes());
    65                         + " root --release 9 -C "
    76         jb.addEntry("META-INF/versions/9/root/dir2/leaf4.txt", v9_root_dir2_leaf4_txt.getBytes());
    66                         + srcdir
    77         jb.addEntry("META-INF/versions/9/root/dir3/leaf5.txt", v9_root_dir3_leaf5_txt.getBytes());
    67                         + System.getProperty("file.separator")
    78         jb.addEntry("META-INF/versions/9/root/dir3/leaf6.txt", v9_root_dir3_leaf6_txt.getBytes());
    68                         + "v9 root"
    79         jb.addEntry("META-INF/versions/10/root/dir3/leaf5.txt", v10_root_dir3_leaf5_txt.getBytes());
    69         ).split(" +");
    80         jb.addEntry("META-INF/versions/10/root/dir3/leaf6.txt", v10_root_dir3_leaf6_txt.getBytes());
    70         new sun.tools.jar.Main(System.out, System.err, "jar").run(args);
    81         jb.build();
    71         String ssp = jarfile.toUri().toString();
    82         System.out.println("Created " + jarfile + ": " + Files.exists(jarfile));
    72         jarURI = new URI("jar", ssp, null);
    83         jarURI = new URI("jar", jarfile.toUri().toString(), null);
    73     }
       
    74 
       
    75     @AfterClass
       
    76     public void close() throws IOException {
       
    77         Files.deleteIfExists(jarfile);
       
    78     }
    84     }
    79 
    85 
    80     @Test
    86     @Test
    81     public void testWalk() throws IOException {
    87     public void testWalk() throws IOException {
       
    88         // treat multi-release jar as unversioned
       
    89         Map<String, String> env = new HashMap<>();
       
    90         Set<String> contents = doTest(env);
       
    91         Set<String> expectedContents = Set.of(
       
    92             root_dir1_leaf1_txt,
       
    93             root_dir1_leaf2_txt,
       
    94             root_dir2_leaf3_txt,
       
    95             root_dir2_leaf4_txt
       
    96         );
       
    97         Assert.assertEquals(contents, expectedContents);
    82 
    98 
    83         // no configuration, treat multi-release jar as unversioned
    99         // open file as multi-release for version 9
    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");
   100         env.put("multi-release", "9");
    96         contents = doTest(env);
   101         contents = doTest(env);
    97         Set<String> versionedContents = Set.of(
   102         expectedContents = Set.of(
    98                 "This is versioned leaf 1.\n",
   103             root_dir1_leaf1_txt,
    99                 "This is versioned leaf 2.\n",
   104             root_dir1_leaf2_txt,
   100                 "This is versioned leaf 3.\n",
   105             v9_root_dir2_leaf3_txt,
   101                 "This is versioned leaf 4.\n"
   106             v9_root_dir2_leaf4_txt,
       
   107             v9_root_dir3_leaf5_txt,
       
   108             v9_root_dir3_leaf6_txt
   102         );
   109         );
   103         Assert.assertEquals(contents, versionedContents);
   110         Assert.assertEquals(contents, expectedContents);
       
   111 
       
   112         // open file as multi-release for version 10
       
   113         env.put("multi-release", "10");
       
   114         contents = doTest(env);
       
   115         expectedContents = Set.of(
       
   116             root_dir1_leaf1_txt,
       
   117             root_dir1_leaf2_txt,
       
   118             v9_root_dir2_leaf3_txt,
       
   119             v9_root_dir2_leaf4_txt,
       
   120             v10_root_dir3_leaf5_txt,
       
   121             v10_root_dir3_leaf6_txt
       
   122         );
       
   123         Assert.assertEquals(contents, expectedContents);
   104     }
   124     }
   105 
   125 
   106     private Set<String> doTest(Map<String,String> env) throws IOException {
   126     private Set<String> doTest(Map<String,String> env) throws IOException {
   107         Set<String> contents;
   127         Set<String> contents;
   108         try (FileSystem fs = FileSystems.newFileSystem(jarURI, env)) {
   128         try (FileSystem fs = FileSystems.newFileSystem(jarURI, env)) {
   109             Path root = fs.getPath("root");
   129             Path root = fs.getPath("root");
   110             contents = Files.walk(root)
   130             contents = Files.walk(root)
   111                     .filter(p -> !Files.isDirectory(p))
   131                 .filter(p -> !Files.isDirectory(p))
   112                     .map(this::pathToContents)
   132                 .map(this::pathToContents)
   113                     .collect(Collectors.toSet());
   133                 .sorted()
       
   134                 .collect(Collectors.toSet());
   114         }
   135         }
   115         return contents;
   136         return contents;
   116     }
   137     }
   117 
   138 
   118     private String pathToContents(Path path) {
   139     private String pathToContents(Path path) {