< prev index next >

test/tools/jlink/JLink2Test.java

Print this page




  49 import java.util.jar.JarOutputStream;
  50 import jdk.tools.jlink.internal.PluginRepository;
  51 import jdk.tools.jlink.plugin.Plugin;
  52 
  53 import tests.Helper;
  54 import tests.JImageGenerator;
  55 import tests.JImageValidator;
  56 
  57 public class JLink2Test {
  58 
  59     public static void main(String[] args) throws Exception {
  60         Helper helper = Helper.newHelper();
  61         if (helper == null) {
  62             System.err.println("Test not run");
  63             return;
  64         }
  65         helper.generateDefaultModules();
  66 
  67         // This test case must be first one, the JlinkTask is clean
  68         // and reveals possible bug related to plugin options in defaults
  69         // e. g.: --genbom
  70         testBomFile(helper);
  71         testSameNames(helper);
  72         testModulePath(helper);
  73         testOptions();
  74     }
  75 
  76     private static void testModulePath(Helper helper) throws IOException {
  77         Path doesNotExist = helper.createNewImageDir("doesnotexist");
  78         Path jar = helper.getJarDir().resolve("bad.jar");
  79         JImageGenerator.getJLinkTask()
  80                 .pluginModulePath(doesNotExist)
  81                 .option("--help")
  82                 .call().assertSuccess();
  83         Files.createFile(jar);
  84         JImageGenerator.getJLinkTask()
  85                 .pluginModulePath(jar)
  86                 .option("--help")
  87                 .call().assertFailure("(\n|\r|.)*Error: Invalid modules in the plugins path: (\n|\r|.)*");
  88         JImageGenerator.getJLinkTask()
  89                 .pluginModulePath(jar.getParent())
  90                 .option("--help")


 117                 .output(helper.createNewImageDir("bad"))
 118                 .addJmods(jar.getParent())
 119                 .addJars(helper.getStdJmodsDir())
 120                 .addMods("bad")
 121                 .call().assertFailure("(\n|\r|.)*Error: jdk.tools.jlink.plugin.PluginException: module-info.class not found for bad module(\n|\r|.)*");
 122     }
 123 
 124     private static void testSameNames(Helper helper) throws Exception {
 125         // Multiple modules with the same name in modulepath, take the first one in the path.
 126         // First jmods then jars. So jmods are found, jars are hidden.
 127         String[] jarClasses = {"amodule.jar.Main"};
 128         String[] jmodsClasses = {"amodule.jmods.Main"};
 129         helper.generateDefaultJarModule("amodule", Arrays.asList(jarClasses));
 130         helper.generateDefaultJModule("amodule", Arrays.asList(jmodsClasses));
 131         List<String> okLocations = new ArrayList<>();
 132         okLocations.addAll(Helper.toLocation("amodule", Arrays.asList(jmodsClasses)));
 133         Path image = helper.generateDefaultImage(new String[0], "amodule").assertSuccess();
 134         JImageValidator validator = new JImageValidator("amodule", okLocations,
 135                 image.toFile(), Collections.emptyList(), Collections.emptyList());
 136         validator.validate();
 137     }
 138 
 139     private static void testBomFile(Helper helper) throws Exception {
 140         String[] userOptions = {
 141             "--compress",
 142             "2",
 143             "--addmods",
 144             "bomzip",
 145             "--strip-debug",
 146             "--genbom",
 147             "--exclude-resources",
 148             "*.jcov,*/META-INF/*"};
 149         String moduleName = "bomzip";
 150         helper.generateDefaultJModule(moduleName, "composite2");
 151         Path imgDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
 152         helper.checkImage(imgDir, moduleName, userOptions, null, null);
 153         File bom = new File(imgDir.toFile(), "bom");
 154         if (!bom.exists()) {
 155             throw new RuntimeException(bom.getAbsolutePath() + " not generated");
 156         }
 157         String bomcontent = new String(Files.readAllBytes(bom.toPath()));
 158         if (!bomcontent.contains("--strip-debug")
 159                 || !bomcontent.contains("--compress")
 160                 || !bomcontent.contains("--genbom")
 161                 || !bomcontent.contains("--exclude-resources *.jcov,"
 162                         + "*/META-INF/*")
 163                 || !bomcontent.contains("--addmods bomzip")) {
 164             throw new Exception("Not expected content in " + bom);
 165         }
 166     }
 167 
 168     private static void testOptions() throws Exception {
 169         List<Plugin> builtInPlugins = new ArrayList<>();
 170         builtInPlugins.addAll(PluginRepository.getPlugins(Layer.boot()));
 171         if(builtInPlugins.isEmpty()) {
 172             throw new Exception("No builtin plugins");
 173         }
 174         List<String> options = new ArrayList<>();
 175         for (Plugin p : builtInPlugins) {
 176             if (p.getOption() == null) {
 177                 throw new Exception("Null option for " + p.getName());
 178             }
 179             if (options.contains(p.getName())) {
 180                 throw new Exception("Option " + p.getOption() + " used more than once");
 181             }
 182             options.add(p.getName());
 183         }
 184     }
 185 }


  49 import java.util.jar.JarOutputStream;
  50 import jdk.tools.jlink.internal.PluginRepository;
  51 import jdk.tools.jlink.plugin.Plugin;
  52 
  53 import tests.Helper;
  54 import tests.JImageGenerator;
  55 import tests.JImageValidator;
  56 
  57 public class JLink2Test {
  58 
  59     public static void main(String[] args) throws Exception {
  60         Helper helper = Helper.newHelper();
  61         if (helper == null) {
  62             System.err.println("Test not run");
  63             return;
  64         }
  65         helper.generateDefaultModules();
  66 
  67         // This test case must be first one, the JlinkTask is clean
  68         // and reveals possible bug related to plugin options in defaults


  69         testSameNames(helper);
  70         testModulePath(helper);
  71         testOptions();
  72     }
  73 
  74     private static void testModulePath(Helper helper) throws IOException {
  75         Path doesNotExist = helper.createNewImageDir("doesnotexist");
  76         Path jar = helper.getJarDir().resolve("bad.jar");
  77         JImageGenerator.getJLinkTask()
  78                 .pluginModulePath(doesNotExist)
  79                 .option("--help")
  80                 .call().assertSuccess();
  81         Files.createFile(jar);
  82         JImageGenerator.getJLinkTask()
  83                 .pluginModulePath(jar)
  84                 .option("--help")
  85                 .call().assertFailure("(\n|\r|.)*Error: Invalid modules in the plugins path: (\n|\r|.)*");
  86         JImageGenerator.getJLinkTask()
  87                 .pluginModulePath(jar.getParent())
  88                 .option("--help")


 115                 .output(helper.createNewImageDir("bad"))
 116                 .addJmods(jar.getParent())
 117                 .addJars(helper.getStdJmodsDir())
 118                 .addMods("bad")
 119                 .call().assertFailure("(\n|\r|.)*Error: jdk.tools.jlink.plugin.PluginException: module-info.class not found for bad module(\n|\r|.)*");
 120     }
 121 
 122     private static void testSameNames(Helper helper) throws Exception {
 123         // Multiple modules with the same name in modulepath, take the first one in the path.
 124         // First jmods then jars. So jmods are found, jars are hidden.
 125         String[] jarClasses = {"amodule.jar.Main"};
 126         String[] jmodsClasses = {"amodule.jmods.Main"};
 127         helper.generateDefaultJarModule("amodule", Arrays.asList(jarClasses));
 128         helper.generateDefaultJModule("amodule", Arrays.asList(jmodsClasses));
 129         List<String> okLocations = new ArrayList<>();
 130         okLocations.addAll(Helper.toLocation("amodule", Arrays.asList(jmodsClasses)));
 131         Path image = helper.generateDefaultImage(new String[0], "amodule").assertSuccess();
 132         JImageValidator validator = new JImageValidator("amodule", okLocations,
 133                 image.toFile(), Collections.emptyList(), Collections.emptyList());
 134         validator.validate();





























 135     }
 136 
 137     private static void testOptions() throws Exception {
 138         List<Plugin> builtInPlugins = new ArrayList<>();
 139         builtInPlugins.addAll(PluginRepository.getPlugins(Layer.boot()));
 140         if(builtInPlugins.isEmpty()) {
 141             throw new Exception("No builtin plugins");
 142         }
 143         List<String> options = new ArrayList<>();
 144         for (Plugin p : builtInPlugins) {
 145             if (p.getOption() == null) {
 146                 throw new Exception("Null option for " + p.getName());
 147             }
 148             if (options.contains(p.getName())) {
 149                 throw new Exception("Option " + p.getOption() + " used more than once");
 150             }
 151             options.add(p.getName());
 152         }
 153     }
 154 }
< prev index next >