< prev index next >

jdk/test/tools/jlink/basic/BasicTest.java

Print this page




  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 /*
  25  * @test
  26  * @summary Basic test of jlink to create jmods and images
  27  * @author Andrei Eremeev
  28  * @library /lib/testlibrary
  29  * @modules java.base/jdk.internal.module
  30  *          jdk.jlink/jdk.tools.jlink.internal
  31  *          jdk.jlink/jdk.tools.jmod
  32  *          jdk.compiler
  33  * @build jdk.testlibrary.ProcessTools
  34  *        jdk.testlibrary.OutputAnalyzer
  35  *        JarUtils CompilerUtils
  36  * @run main BasicTest
  37  */
  38 
  39 import java.io.File;
  40 import java.io.PrintWriter;
  41 import java.nio.file.Files;
  42 import java.nio.file.Path;
  43 import java.nio.file.Paths;
  44 import java.util.ArrayList;
  45 import java.util.Collections;
  46 import java.util.List;

  47 
  48 import jdk.testlibrary.OutputAnalyzer;
  49 import jdk.testlibrary.ProcessTools;
  50 
  51 public class BasicTest {









  52 
  53     private final Path jdkHome = Paths.get(System.getProperty("test.jdk"));
  54     private final Path jdkMods = jdkHome.resolve("jmods");
  55     private final Path testSrc = Paths.get(System.getProperty("test.src"));
  56     private final Path src = testSrc.resolve("src");
  57     private final Path classes = Paths.get("classes");
  58     private final Path jmods = Paths.get("jmods");
  59     private final Path jars = Paths.get("jars");
  60 
  61     public static void main(String[] args) throws Throwable {
  62         new BasicTest().run();
  63     }
  64 
  65     public void run() throws Throwable {
  66         if (Files.notExists(jdkMods)) {
  67             return;
  68         }
  69 
  70         if (!CompilerUtils.compile(src, classes)) {
  71             throw new AssertionError("Compilation failure. See log.");


  93     private void execute(Path image, String moduleName) throws Throwable {
  94         String cmd = image.resolve("bin").resolve(moduleName).toString();
  95         OutputAnalyzer analyzer;
  96         if (System.getProperty("os.name").startsWith("Windows")) {
  97             analyzer = ProcessTools.executeProcess("sh.exe", cmd, "1", "2", "3");
  98         } else {
  99             analyzer = ProcessTools.executeProcess(cmd, "1", "2", "3");
 100         }
 101         if (analyzer.getExitValue() != 0) {
 102             throw new AssertionError("Image invocation failed: rc=" + analyzer.getExitValue());
 103         }
 104     }
 105 
 106     private void runJlink(Path image, String modName, String... options) {
 107         List<String> args = new ArrayList<>();
 108         Collections.addAll(args,
 109                 "--module-path", jdkMods + File.pathSeparator + jmods,
 110                 "--add-modules", modName,
 111                 "--output", image.toString());
 112         Collections.addAll(args, options);
 113         int rc = jdk.tools.jlink.internal.Main.run(args.toArray(new String[args.size()]), new PrintWriter(System.out));


 114         if (rc != 0) {
 115             throw new AssertionError("Jlink failed: rc = " + rc);
 116         }
 117     }
 118 
 119     private void runJmod(String cp, String modName) {
 120         int rc = jdk.tools.jmod.Main.run(new String[] {
 121                 "create",
 122                 "--class-path", cp,
 123                 "--module-version", "1.0",
 124                 "--main-class", "jdk.test.Test",
 125                 jmods.resolve(modName + ".jmod").toString(),
 126         }, System.out);
 127         if (rc != 0) {
 128             throw new AssertionError("Jmod failed: rc = " + rc);
 129         }
 130     }
 131 }


  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 /*
  25  * @test
  26  * @summary Basic test of jlink to create jmods and images
  27  * @author Andrei Eremeev
  28  * @library /lib/testlibrary
  29  * @modules java.base/jdk.internal.module
  30  *          jdk.jlink

  31  *          jdk.compiler
  32  * @build jdk.testlibrary.ProcessTools
  33  *        jdk.testlibrary.OutputAnalyzer
  34  *        JarUtils CompilerUtils
  35  * @run main BasicTest
  36  */
  37 
  38 import java.io.File;
  39 import java.io.PrintWriter;
  40 import java.nio.file.Files;
  41 import java.nio.file.Path;
  42 import java.nio.file.Paths;
  43 import java.util.ArrayList;
  44 import java.util.Collections;
  45 import java.util.List;
  46 import java.util.spi.ToolProvider;
  47 
  48 import jdk.testlibrary.OutputAnalyzer;
  49 import jdk.testlibrary.ProcessTools;
  50 
  51 public class BasicTest {
  52     static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
  53         .orElseThrow(() ->
  54             new RuntimeException("jmod tool not found")
  55         );
  56 
  57     static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
  58         .orElseThrow(() ->
  59             new RuntimeException("jlink tool not found")
  60         );
  61 
  62     private final Path jdkHome = Paths.get(System.getProperty("test.jdk"));
  63     private final Path jdkMods = jdkHome.resolve("jmods");
  64     private final Path testSrc = Paths.get(System.getProperty("test.src"));
  65     private final Path src = testSrc.resolve("src");
  66     private final Path classes = Paths.get("classes");
  67     private final Path jmods = Paths.get("jmods");
  68     private final Path jars = Paths.get("jars");
  69 
  70     public static void main(String[] args) throws Throwable {
  71         new BasicTest().run();
  72     }
  73 
  74     public void run() throws Throwable {
  75         if (Files.notExists(jdkMods)) {
  76             return;
  77         }
  78 
  79         if (!CompilerUtils.compile(src, classes)) {
  80             throw new AssertionError("Compilation failure. See log.");


 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 }
< prev index next >