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