Path: blob/master/test/jdk/tools/jlink/JLinkReproducibleTest.java
41144 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.nio.file.*;25import java.util.*;2627import static jdk.test.lib.Asserts.*;28import jdk.test.lib.JDKToolFinder;29import jdk.test.lib.process.ProcessTools;3031/*32* @test33* @bug 821423034* @summary Test that jlinks generates reproducible modules files35* @library /test/lib36* @run driver JLinkReproducibleTest37*/38public class JLinkReproducibleTest {39private static void run(List<String> cmd) throws Exception {40var pb = new ProcessBuilder(cmd.toArray(new String[0]));41var res = ProcessTools.executeProcess(pb);42res.shouldHaveExitValue(0);43}4445private static void jlink(Path image, boolean with_default_trace_file) throws Exception {46var cmd = new ArrayList<String>();47cmd.add(JDKToolFinder.getJDKTool("jlink"));48cmd.addAll(List.of(49"--module-path", JMODS_DIR.toString() + File.pathSeparator + CLASS_DIR.toString(),50"--add-modules", "main",51"--compress=2",52"--output", image.toString()53));54if (!with_default_trace_file) {55cmd.add("--generate-jli-classes=@file-not-exists");56}57run(cmd);58}5960private static void javac(String... args) throws Exception {61var cmd = new ArrayList<String>();62cmd.add(JDKToolFinder.getJDKTool("javac"));63cmd.addAll(Arrays.asList(args));64run(cmd);65}6667private static final List<String> MODULE_INFO = List.of(68"module main {",69" exports org.test.main;",70"}"71);7273private static final List<String> MAIN_CLASS = List.of(74"package org.test.main;",75"public class Main {",76" public static void main(String[] args) {",77" System.out.println(\"Hello, world\");",78" }",79"}"80);8182private static final Path CLASS_DIR = Path.of("classes");83private static final Path JMODS_DIR = Path.of(System.getProperty("java.home"), "jmods");8485public static void main(String[] args) throws Exception {86// Write the source code87var srcDir = Path.of("main", "org", "test", "main");88Files.createDirectories(srcDir.toAbsolutePath());8990var srcFile = srcDir.resolve("Main.java");91Files.write(srcFile, MAIN_CLASS);9293var moduleFile = Path.of("main").resolve("module-info.java");94Files.write(moduleFile, MODULE_INFO);9596// Compile the source code to class files97javac("--module-source-path", ".",98"--module", "main",99"-d", CLASS_DIR.toString());100101// Link the first image102var firstImage = Path.of("image-first");103jlink(firstImage, true);104var firstModulesFile = firstImage.resolve("lib")105.resolve("modules");106107// Link the second image108var secondImage = Path.of("image-second");109jlink(secondImage, true);110var secondModulesFile = secondImage.resolve("lib")111.resolve("modules");112113// Ensure module files are identical114assertEquals(-1L, Files.mismatch(firstModulesFile, secondModulesFile));115116// Link the third image117var thirdImage = Path.of("image-third");118jlink(thirdImage, false);119var thirdModulesFile = thirdImage.resolve("lib")120.resolve("modules");121// Link the fourth image122var fourthImage = Path.of("image-fourth");123jlink(fourthImage, false);124var fourthModulesFile = fourthImage.resolve("lib")125.resolve("modules");126127// Ensure module files are identical128assertEquals(-1L, Files.mismatch(thirdModulesFile, fourthModulesFile));129}130}131132133