< prev index next >

test/tools/jlink/basic/BasicTest.java

Print this page




  70 
  71     public static void main(String[] args) throws Throwable {
  72         new BasicTest().run();
  73     }
  74 
  75     public void run() throws Throwable {
  76         if (Files.notExists(jdkMods)) {
  77             return;
  78         }
  79 
  80         if (!CompilerUtils.compile(src, classes)) {
  81             throw new AssertionError("Compilation failure. See log.");
  82         }
  83 
  84         Files.createDirectories(jmods);
  85         Files.createDirectories(jars);
  86         Path jarfile = jars.resolve("test.jar");
  87         JarUtils.createJarFile(jarfile, classes);
  88 
  89         Path image = Paths.get("mysmallimage");
  90         runJmod(jarfile.toString(), TEST_MODULE);
  91         runJlink(image, TEST_MODULE, "--compress", "2");
  92         execute(image, TEST_MODULE);
  93 
  94         Files.delete(jmods.resolve(TEST_MODULE + ".jmod"));
  95 
  96         image = Paths.get("myimage");
  97         runJmod(classes.toString(), TEST_MODULE);
  98         runJlink(image, TEST_MODULE);
  99         execute(image, TEST_MODULE);









 100     }
 101 
 102     private void execute(Path image, String moduleName) throws Throwable {
 103         String cmd = image.resolve("bin").resolve(moduleName).toString();
 104         OutputAnalyzer analyzer;
 105         if (System.getProperty("os.name").startsWith("Windows")) {
 106             analyzer = ProcessTools.executeProcess("sh.exe", cmd, "1", "2", "3");
 107         } else {
 108             analyzer = ProcessTools.executeProcess(cmd, "1", "2", "3");
 109         }
 110         if (analyzer.getExitValue() != 0) {
 111             throw new AssertionError("Image invocation failed: rc=" + analyzer.getExitValue());
 112         }
 113     }
 114 
 115     private void runJlink(Path image, String modName, String... options) {
 116         List<String> args = new ArrayList<>();
 117         Collections.addAll(args,
 118                 "--module-path", jdkMods + File.pathSeparator + jmods,
 119                 "--add-modules", modName,
 120                 "--output", image.toString());
 121         Collections.addAll(args, options);
 122 
 123         PrintWriter pw = new PrintWriter(System.out);
 124         int rc = JLINK_TOOL.run(pw, pw, args.toArray(new String[args.size()]));
 125         if (rc != 0) {
 126             throw new AssertionError("Jlink failed: rc = " + rc);
 127         }
 128     }
 129 
 130     private void runJmod(String cp, String modName) {
 131         int rc = JMOD_TOOL.run(System.out, System.out, new String[] {


 132                 "create",
 133                 "--class-path", cp,
 134                 "--module-version", "1.0",
 135                 "--main-class", "jdk.test.Test",







 136                 jmods.resolve(modName + ".jmod").toString(),
 137         });


 138         if (rc != 0) {
 139             throw new AssertionError("Jmod failed: rc = " + rc);
 140         }
 141     }
 142 }


  70 
  71     public static void main(String[] args) throws Throwable {
  72         new BasicTest().run();
  73     }
  74 
  75     public void run() throws Throwable {
  76         if (Files.notExists(jdkMods)) {
  77             return;
  78         }
  79 
  80         if (!CompilerUtils.compile(src, classes)) {
  81             throw new AssertionError("Compilation failure. See log.");
  82         }
  83 
  84         Files.createDirectories(jmods);
  85         Files.createDirectories(jars);
  86         Path jarfile = jars.resolve("test.jar");
  87         JarUtils.createJarFile(jarfile, classes);
  88 
  89         Path image = Paths.get("mysmallimage");
  90         runJmod(jarfile.toString(), TEST_MODULE, true);
  91         runJlink(image, TEST_MODULE, "--compress", "2", "--launcher", "foo=" + TEST_MODULE);
  92         execute(image, "foo");
  93 
  94         Files.delete(jmods.resolve(TEST_MODULE + ".jmod"));
  95 
  96         image = Paths.get("myimage");
  97         runJmod(classes.toString(), TEST_MODULE, true);
  98         runJlink(image, TEST_MODULE, "--launcher", "bar=" + TEST_MODULE);
  99         execute(image, "bar");
 100 
 101         Files.delete(jmods.resolve(TEST_MODULE + ".jmod"));
 102 
 103         image = Paths.get("myimage2");
 104         runJmod(classes.toString(), TEST_MODULE, false /* no ModuleMainClass! */);
 105         // specify main class in --launcher command line
 106         runJlink(image, TEST_MODULE, "--launcher", "bar2=" + TEST_MODULE + "/jdk.test.Test");
 107         execute(image, "bar2");
 108 
 109     }
 110 
 111     private void execute(Path image, String scriptName) throws Throwable {
 112         String cmd = image.resolve("bin").resolve(scriptName).toString();
 113         OutputAnalyzer analyzer;
 114         if (System.getProperty("os.name").startsWith("Windows")) {
 115             analyzer = ProcessTools.executeProcess("sh.exe", cmd, "1", "2", "3");
 116         } else {
 117             analyzer = ProcessTools.executeProcess(cmd, "1", "2", "3");
 118         }
 119         if (analyzer.getExitValue() != 0) {
 120             throw new AssertionError("Image invocation failed: rc=" + analyzer.getExitValue());
 121         }
 122     }
 123 
 124     private void runJlink(Path image, String modName, String... options) {
 125         List<String> args = new ArrayList<>();
 126         Collections.addAll(args,
 127                 "--module-path", jdkMods + File.pathSeparator + jmods,
 128                 "--add-modules", modName,
 129                 "--output", image.toString());
 130         Collections.addAll(args, options);
 131 
 132         PrintWriter pw = new PrintWriter(System.out);
 133         int rc = JLINK_TOOL.run(pw, pw, args.toArray(new String[args.size()]));
 134         if (rc != 0) {
 135             throw new AssertionError("Jlink failed: rc = " + rc);
 136         }
 137     }
 138 
 139     private void runJmod(String cp, String modName, boolean main) {
 140         int rc;
 141         if (main) {
 142             rc = JMOD_TOOL.run(System.out, System.out, new String[] {
 143                 "create",
 144                 "--class-path", cp,
 145                 "--module-version", "1.0",
 146                 "--main-class", "jdk.test.Test",
 147                 jmods.resolve(modName + ".jmod").toString()
 148             });
 149         } else {
 150             rc = JMOD_TOOL.run(System.out, System.out, new String[] {
 151                 "create",
 152                 "--class-path", cp,
 153                 "--module-version", "1.0",
 154                 jmods.resolve(modName + ".jmod").toString(),
 155             });
 156         }
 157 
 158         if (rc != 0) {
 159             throw new AssertionError("Jmod failed: rc = " + rc);
 160         }
 161     }
 162 }
< prev index next >