test/jdk/tools/jlink/JLinkReproducibleTest.java
changeset 52814 abccada595dd
child 58843 63994dedec49
equal deleted inserted replaced
52813:767678b5e61b 52814:abccada595dd
       
     1 /*
       
     2  * Copyright (c) 2018, 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 import java.io.File;
       
    25 import java.nio.file.*;
       
    26 import java.util.*;
       
    27 
       
    28 import static jdk.test.lib.Asserts.*;
       
    29 import jdk.test.lib.JDKToolFinder;
       
    30 import jdk.test.lib.process.ProcessTools;
       
    31 
       
    32 /*
       
    33  * @test
       
    34  * @bug 8214230
       
    35  * @summary Test that jlinks generates reproducible modules files
       
    36  * @library /test/lib
       
    37  * @run driver JLinkReproducibleTest
       
    38  */
       
    39 public class JLinkReproducibleTest {
       
    40     private static void run(List<String> cmd) throws Exception {
       
    41         var pb = new ProcessBuilder(cmd.toArray(new String[0]));
       
    42         var res = ProcessTools.executeProcess(pb);
       
    43         res.shouldHaveExitValue(0);
       
    44     }
       
    45 
       
    46     private static void jlink(Path image) throws Exception {
       
    47         var cmd = new ArrayList<String>();
       
    48         cmd.add(JDKToolFinder.getJDKTool("jlink"));
       
    49         cmd.addAll(List.of(
       
    50             "--module-path", JMODS_DIR.toString() + File.pathSeparator + CLASS_DIR.toString(),
       
    51             "--add-modules", "main",
       
    52             "--compress=2",
       
    53             "--output", image.toString()
       
    54         ));
       
    55         run(cmd);
       
    56     }
       
    57 
       
    58     private static void javac(String... args) throws Exception {
       
    59         var cmd = new ArrayList<String>();
       
    60         cmd.add(JDKToolFinder.getJDKTool("javac"));
       
    61         cmd.addAll(Arrays.asList(args));
       
    62         run(cmd);
       
    63     }
       
    64 
       
    65     private static final List<String> MODULE_INFO = List.of(
       
    66         "module main {",
       
    67         "    exports org.test.main;",
       
    68         "}"
       
    69     );
       
    70 
       
    71     private static final List<String> MAIN_CLASS = List.of(
       
    72         "package org.test.main;",
       
    73         "public class Main {",
       
    74         "    public static void main(String[] args) {",
       
    75         "        System.out.println(\"Hello, world\");",
       
    76         "    }",
       
    77         "}"
       
    78     );
       
    79 
       
    80     private static final Path CLASS_DIR = Path.of("classes");
       
    81     private static final Path JMODS_DIR = Path.of(System.getProperty("java.home"), "jmods");
       
    82 
       
    83     public static void main(String[] args) throws Exception {
       
    84         // Write the source code
       
    85         var srcDir = Path.of("main", "org", "test", "main");
       
    86         Files.createDirectories(srcDir.toAbsolutePath());
       
    87 
       
    88         var srcFile = srcDir.resolve("Main.java");
       
    89         Files.write(srcFile, MAIN_CLASS);
       
    90 
       
    91         var moduleFile = Path.of("main").resolve("module-info.java");
       
    92         Files.write(moduleFile, MODULE_INFO);
       
    93 
       
    94         // Compile the source code to class files
       
    95         javac("--module-source-path", ".",
       
    96               "--module", "main",
       
    97               "-d", CLASS_DIR.toString());
       
    98 
       
    99         // Link the first image
       
   100         var firstImage = Path.of("image-first");
       
   101         jlink(firstImage);
       
   102         var firstModulesFile = firstImage.resolve("lib")
       
   103                                          .resolve("modules");
       
   104 
       
   105         // Link the second image
       
   106         var secondImage = Path.of("image-second");
       
   107         jlink(secondImage);
       
   108         var secondModulesFile = secondImage.resolve("lib")
       
   109                                            .resolve("modules");
       
   110 
       
   111         // Ensure module files are identical
       
   112         assertEquals(-1L, Files.mismatch(firstModulesFile, secondModulesFile));
       
   113     }
       
   114 }