1 /*
   2  * Copyright (c) 2016, 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.nio.file.Path;
  25 import java.util.List;
  26 import java.util.stream.Collectors;
  27 
  28 import jdk.tools.jlink.internal.plugins.GenerateJLIClassesPlugin;
  29 
  30 import tests.Helper;
  31 import tests.JImageGenerator;
  32 import tests.JImageValidator;
  33 import tests.Result;
  34 
  35  /*
  36  * @test
  37  * @library ../../lib
  38  * @summary Test --generate-jli-classes plugin
  39  * @modules java.base/jdk.internal.jimage
  40  *          jdk.jdeps/com.sun.tools.classfile
  41  *          jdk.jlink/jdk.tools.jlink.internal
  42  *          jdk.jlink/jdk.tools.jlink.internal.plugins
  43  *          jdk.jlink/jdk.tools.jmod
  44  *          jdk.jlink/jdk.tools.jimage
  45  * @build tests.*
  46  * @run main/othervm GenerateJLIClassesPluginTest
  47  */
  48 public class GenerateJLIClassesPluginTest {
  49 
  50     private static Helper helper;
  51 
  52     public static void main(String[] args) throws Exception {
  53         helper = Helper.newHelper();
  54         if (helper == null) {
  55             System.err.println("Test not run");
  56             return;
  57         }
  58 
  59         helper.generateDefaultModules();
  60 
  61 
  62         // Test that generate-jli is enabled by default
  63         Result result = JImageGenerator.getJLinkTask()
  64                 .modulePath(helper.defaultModulePath())
  65                 .output(helper.createNewImageDir("generate-jli"))
  66                 .addMods("java.base")
  67                 .call();
  68 
  69         Path image = result.assertSuccess();
  70 
  71         JImageValidator.validate(
  72             image.resolve("lib").resolve("modules"),
  73                     classFilesForSpecies(GenerateJLIClassesPlugin.defaultSpecies()),
  74                     List.of());
  75 
  76 
  77         // Test a valid set of options
  78         result = JImageGenerator.getJLinkTask()
  79                 .modulePath(helper.defaultModulePath())
  80                 .output(helper.createNewImageDir("generate-jli"))
  81                 .option("--generate-jli-classes=bmh:bmh-species=LL,L3")
  82                 .addMods("java.base")
  83                 .call();
  84 
  85         image = result.assertSuccess();
  86 
  87         JImageValidator.validate(
  88                 image.resolve("lib").resolve("modules"),
  89                 classFilesForSpecies(List.of("LL", "L3")),
  90                 classFilesForSpecies(List.of("L4")));
  91 
  92 
  93         // Test disabling BMH species generation
  94         result = JImageGenerator.getJLinkTask()
  95                 .modulePath(helper.defaultModulePath())
  96                 .output(helper.createNewImageDir("generate-jli"))
  97                 .option("--generate-jli-classes=not-bmh:bmh-species=LL,L3")
  98                 .addMods("java.base")
  99                 .call();
 100 
 101         image = result.assertSuccess();
 102         JImageValidator.validate(
 103             image.resolve("lib").resolve("modules"),
 104             List.of(),
 105             classFilesForSpecies(List.of("LL", "L3", "L4")));
 106 
 107 
 108         // Test an invalid set of options
 109         result = JImageGenerator.getJLinkTask()
 110                 .modulePath(helper.defaultModulePath())
 111                 .output(helper.createNewImageDir("generate-jli"))
 112                 .option("--generate-jli-classes=bmh:bmh-species=LL,L7V")
 113                 .addMods("java.base")
 114                 .call();
 115 
 116         result.assertFailure();
 117     }
 118 
 119     private static List<String> classFilesForSpecies(List<String> species) {
 120         return species.stream()
 121                 .map(s -> "/java.base/java/lang/invoke/BoundMethodHandle$Species_" + s + ".class")
 122                 .collect(Collectors.toList());
 123     }
 124 }