1 /*
   2  * Copyright (c) 2018, 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 /**
  25  * @test
  26  * @bug 8197532
  27  * @modules jdk.compiler
  28  *          jdk.jlink
  29  *          jdk.zipfs
  30  * @library src /test/lib
  31  * @build java.json/*
  32  * @run main DefaultModules
  33  * @summary Test that all modules that export an API are in the set of modules
  34  *          resolved when compiling or running code on the class path
  35  */
  36 
  37 import java.io.PrintStream;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.util.spi.ToolProvider;
  41 
  42 import jdk.test.lib.process.ProcessTools;
  43 
  44 /**
  45  * This test compiles and runs the following tests on the class path:
  46  *
  47  *   TestRootModules.java.java - tests that every module that exports an API
  48  *       is resolved. Also tests that java.se is not resolved.
  49  *
  50  *   TestJson.java - exercises APIs exported by the java.json module. The
  51  *       java.json module is not a Java SE module.
  52  */
  53 
  54 public class DefaultModules {
  55     private static final PrintStream out = System.out;
  56 
  57     public static void main(String[] args) throws Exception {
  58         String javaHome = System.getProperty("java.home");
  59         String testSrc = System.getProperty("test.src");
  60 
  61         // $JDK_HOME/bin/java TestModules.java
  62         String source = Path.of(testSrc, "src", "TestRootModules.java").toString();
  63         ProcessTools.executeTestJava(source)
  64                 .outputTo(System.out)
  65                 .errorTo(System.err)
  66                 .shouldHaveExitValue(0);
  67 
  68         /**
  69          * Create a run-time image containing java.se, java.json and the javac
  70          * compiler. Use the run-time image to compile and run both
  71          * TestModules.java and JsonTest.java
  72          */
  73         if (Files.exists(Path.of(javaHome, "jmods", "java.se.jmod"))) {
  74             // jlink --add-modules java.se,java.json,jdk.compiler,jdk.zipfs
  75             Path here = Path.of(".");
  76             Path image = Files.createTempDirectory(here, "images").resolve("myimage");
  77             ToolProvider jlink = ToolProvider.findFirst("jlink")
  78                     .orElseThrow(() -> new RuntimeException("jlink not found"));
  79             int exitCode = jlink.run(System.out, System.err,
  80                     "--module-path", System.getProperty("test.module.path"),
  81                     "--add-modules", "java.se,java.json,jdk.compiler,jdk.zipfs",
  82                     "--output", image.toString());
  83             if (exitCode != 0)
  84                 throw new RuntimeException("jlink failed");
  85 
  86             // path to java launcher in run-time image
  87             String javaLauncher = image.resolve("bin").resolve("java").toString();
  88             if (System.getProperty("os.name").startsWith("Windows"))
  89                 javaLauncher += ".exe";
  90 
  91             // $CUSTOM_JDK/bin/java TestRootModules.java
  92             source = Path.of(testSrc, "src", "TestRootModules.java").toString();
  93             out.format("Command line: [%s %s]%n", javaLauncher, source);
  94             ProcessTools.executeProcess(new ProcessBuilder(javaLauncher, source))
  95                     .outputTo(System.out)
  96                     .errorTo(System.err)
  97                     .shouldHaveExitValue(0);
  98 
  99             // $CUSTOM_JDK/bin/java TestJson.java
 100             source = Path.of(testSrc, "src", "TestJson.java").toString();
 101             out.format("Command line: [%s %s]%n", javaLauncher, source);
 102             ProcessTools.executeProcess(new ProcessBuilder(javaLauncher, source))
 103                     .outputTo(System.out)
 104                     .errorTo(System.err)
 105                     .shouldHaveExitValue(0);
 106         }
 107     }
 108 }