Path: blob/master/test/jdk/tools/jlink/plugins/GenerateJLIClassesPluginTest.java
41149 views
/*1* Copyright (c) 2016, 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 java.io.IOException;24import java.nio.charset.Charset;25import java.nio.file.Files;26import java.nio.file.Path;27import java.util.Collection;28import java.util.List;29import java.util.stream.Collectors;3031import tests.Helper;32import tests.JImageGenerator;33import tests.JImageValidator;34import tests.Result;3536import org.testng.annotations.BeforeTest;37import org.testng.annotations.Test;3839/*40* @test41* @bug 825291942* @library ../../lib43* @summary Test --generate-jli-classes plugin44* @modules java.base/jdk.internal.jimage45* jdk.jdeps/com.sun.tools.classfile46* jdk.jlink/jdk.tools.jlink.internal47* jdk.jlink/jdk.tools.jlink.internal.plugins48* jdk.jlink/jdk.tools.jmod49* jdk.jlink/jdk.tools.jimage50* @build tests.*51* @run testng/othervm GenerateJLIClassesPluginTest52*/53public class GenerateJLIClassesPluginTest {5455private static Helper helper;5657@BeforeTest58public static void setup() throws Exception {59helper = Helper.newHelper();60if (helper == null) {61System.err.println("Test not run");62return;63}64helper.generateDefaultModules();65}6667@Test68public static void testSpecies() throws IOException {69// Check that --generate-jli-classes=@file works as intended70Path baseFile = Files.createTempFile("base", "trace");71String species = "LLLLLLLLLLLLLLLLLLL";72String fileString = "[SPECIES_RESOLVE] java.lang.invoke.BoundMethodHandle$Species_" + species + " (salvaged)\n";73Files.write(baseFile, fileString.getBytes(Charset.defaultCharset()));74Result result = JImageGenerator.getJLinkTask()75.modulePath(helper.defaultModulePath())76.output(helper.createNewImageDir("generate-jli-file"))77.option("--generate-jli-classes=@" + baseFile.toString())78.addMods("java.base")79.call();8081Path image = result.assertSuccess();82validateHolderClasses(image);83JImageValidator.validate(image.resolve("lib").resolve("modules"),84classFilesForSpecies(List.of(species)), // species should be in the image85classFilesForSpecies(List.of(species.substring(1)))); // but not it's immediate parent86}8788@Test89public static void testInvalidSignatures() throws IOException {90// Check that --generate-jli-classes=@file fails as intended on shapes that can't be generated91String[] args = new String[] {92"[LF_RESOLVE] java.lang.invoke.DirectMethodHandle$Holder invokeVirtual L_L (success)\n",93"[LF_RESOLVE] java.lang.invoke.DirectMethodHandle$Holder invokeInterface L_L (success)\n",94"[LF_RESOLVE] java.lang.invoke.DirectMethodHandle$Holder invokeStatic I_L (success)\n"95};96for (String fileString : args) {97Path failFile = Files.createTempFile("fail", "trace");98fileString = "[LF_RESOLVE] java.lang.invoke.DirectMethodHandle$Holder invokeVirtual L_L (success)\n";99Files.write(failFile, fileString.getBytes(Charset.defaultCharset()));100Result result = JImageGenerator.getJLinkTask()101.modulePath(helper.defaultModulePath())102.output(helper.createNewImageDir("invalid-signature"))103.option("--generate-jli-classes=@" + failFile.toString())104.addMods("java.base")105.call();106107result.assertFailure();108}109}110111@Test112public static void nonExistentTraceFile() throws IOException {113Result result = JImageGenerator.getJLinkTask()114.modulePath(helper.defaultModulePath())115.output(helper.createNewImageDir("non-existent-tracefile"))116.option("--generate-jli-classes=@NON_EXISTENT_FILE")117.addMods("java.base")118.call();119120Path image = result.assertSuccess();121validateHolderClasses(image);122}123124private static void validateHolderClasses(Path image) throws IOException {125JImageValidator.validate(image.resolve("lib").resolve("modules"),126List.of("/java.base/java/lang/invoke/DirectMethodHandle$Holder.class",127"/java.base/java/lang/invoke/DelegatingMethodHandle$Holder.class",128"/java.base/java/lang/invoke/LambdaForm$Holder.class",129"/java.base/java/lang/invoke/Invokers$Holder.class"),130List.of());131}132133private static List<String> classFilesForSpecies(Collection<String> species) {134return species.stream()135.map(s -> "/java.base/java/lang/invoke/BoundMethodHandle$Species_" + s + ".class")136.collect(Collectors.toList());137}138}139140141