Path: blob/master/test/jdk/tools/jlink/JLinkReproducible3Test.java
41144 views
/*1* Copyright (c) 2020, 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 jdk.test.lib.process.ProcessTools;2425import java.io.File;26import java.io.IOException;27import java.io.UncheckedIOException;28import java.nio.file.*;2930import java.nio.file.attribute.BasicFileAttributes;31import java.util.Optional;3233/*34* @test35* @summary Make sure that jimages are consistent when created by jlink. Copies test jdk and runs against original.36* @bug 825273037* @modules jdk.jlink38* jdk.management39* jdk.unsupported40* jdk.charsets41* @library /test/lib42* @run main JLinkReproducible3Test43*/44public class JLinkReproducible3Test {4546public static void main(String[] args) throws Exception {47Path image1 = Paths.get("./image1");48Path image2 = Paths.get("./image2");4950Path copyJdk1Dir = Path.of("./copy-jdk1-tmpdir");51Files.createDirectory(copyJdk1Dir);5253Path copyJdk2Dir = Path.of("./copy-jdk2-tmpdir");54Files.createDirectory(copyJdk2Dir);5556Path jdkTestDir = Path.of(57Optional.of(58System.getProperty("test.jdk"))59.orElseThrow(() -> new RuntimeException("Couldn't load JDK Test Dir"))60);6162copyJDK(jdkTestDir, copyJdk1Dir);63copyJDK(jdkTestDir, copyJdk2Dir);6465Path copiedJlink1 = Optional.of(66Paths.get(copyJdk1Dir.toString(), "bin", "jlink"))67.orElseThrow(() -> new RuntimeException("Unable to load copied jlink")68);6970Path copiedJlink2 = Optional.of(71Paths.get(copyJdk2Dir.toString(), "bin", "jlink"))72.orElseThrow(() -> new RuntimeException("Unable to load copied jlink")73);7475runCopiedJlink(copiedJlink1.toString(), "--add-modules", "java.base,jdk.management,jdk.unsupported,jdk.charsets", "--output", image1.toString());76runCopiedJlink(copiedJlink2.toString(), "--add-modules", "java.base,jdk.management,jdk.unsupported,jdk.charsets", "--output", image2.toString());7778long mismatch = Files.mismatch(image1.resolve("lib").resolve("modules"), image2.resolve("lib").resolve("modules"));79if (mismatch != -1L) {80throw new RuntimeException("jlink producing inconsistent result in modules. Mismatch in modules file occurred at byte position " + mismatch);81}82}8384private static void runCopiedJlink(String... args) throws Exception {85var process = new ProcessBuilder(args);86var res = ProcessTools.executeProcess(process);87res.shouldHaveExitValue(0);88}8990private static void copyJDK(Path src, Path dst) throws Exception {91Files.walk(src).skip(1).forEach(file -> {92try {93Files.copy(file, dst.resolve(src.relativize(file)), StandardCopyOption.COPY_ATTRIBUTES);94} catch (IOException ioe) {95throw new UncheckedIOException(ioe);96}97});98}99}100101102103