1 /*
   2  * Copyright (c) 2016, 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 8170859
  27  * @summary Ensure no incubator modules are resolved by default in the image
  28  * @library /lib/testlibrary
  29  * @modules jdk.compiler
  30  * @build CompilerUtils
  31  * @run testng DefaultImage
  32  */
  33 
  34 import java.io.ByteArrayOutputStream;
  35 import java.io.IOException;
  36 import java.io.PrintStream;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.function.Consumer;
  41 import java.util.stream.Stream;
  42 
  43 import org.testng.annotations.BeforeTest;
  44 import org.testng.annotations.DataProvider;
  45 import org.testng.annotations.Test;
  46 
  47 import static java.nio.charset.StandardCharsets.UTF_8;
  48 import static jdk.testlibrary.ProcessTools.executeCommand;
  49 import static org.testng.Assert.*;
  50 
  51 public class DefaultImage {
  52     private static final String JAVA_HOME = System.getProperty("java.home");
  53     private static final Path TEST_SRC = Paths.get(System.getProperty("test.src"));
  54     private static final Path CP_DIR = Paths.get("cp");
  55 
  56     @BeforeTest
  57     private void setup() throws Throwable {
  58         Path src = TEST_SRC.resolve("src").resolve("cp").resolve("listmods");
  59         assertTrue(CompilerUtils.compile(src, CP_DIR));
  60     }
  61 
  62     public void test() throws Throwable {
  63         java("-cp", CP_DIR.toString(),
  64              "listmods.ListModules")
  65             .assertSuccess()
  66             .resultChecker(r -> r.assertOutputContains("java.base"))
  67             .resultChecker(r -> r.assertOutputDoesNotContain("jdk.incubator"));
  68     }
  69 
  70     @DataProvider(name = "tokens")
  71     public Object[][] singleModuleValues() throws IOException {
  72         return new Object[][]{ { "ALL-DEFAULT" }, { "ALL-SYSTEM"} };
  73     }
  74 
  75     @Test(dataProvider = "tokens")
  76     public void testAddMods(String addModsToken) throws Throwable {
  77         java("--add-modules", addModsToken,
  78              "-cp", CP_DIR.toString(),
  79              "listmods.ListModules")
  80             .assertSuccess()
  81             .resultChecker(r -> r.assertOutputContains("java.base"))
  82             .resultChecker(r -> r.assertOutputDoesNotContain("jdk.incubator"));
  83     }
  84 
  85     static ToolResult java(String... opts) throws Throwable {
  86         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  87         PrintStream ps = new PrintStream(baos);
  88         String[] options = Stream.concat(Stream.of(getJava()), Stream.of(opts))
  89                 .toArray(String[]::new);
  90 
  91         ProcessBuilder pb = new ProcessBuilder(options);
  92         int exitValue = executeCommand(pb).outputTo(ps)
  93                 .errorTo(ps)
  94                 .getExitValue();
  95 
  96         return new ToolResult(exitValue, new String(baos.toByteArray(), UTF_8));
  97     }
  98 
  99     static class ToolResult {
 100         final int exitCode;
 101         final String output;
 102 
 103         ToolResult(int exitValue, String output) {
 104             this.exitCode = exitValue;
 105             this.output = output;
 106         }
 107 
 108         ToolResult assertSuccess() {
 109             assertEquals(exitCode, 0,
 110                     "Expected exit code 0, got " + exitCode
 111                             + ", with output[" + output + "]");
 112             return this;
 113         }
 114 
 115         ToolResult resultChecker(Consumer<ToolResult> r) {
 116             r.accept(this);
 117             return this;
 118         }
 119 
 120         ToolResult assertOutputContains(String subString) {
 121             assertTrue(output.contains(subString),
 122                        "Expected to find [" + subString + "], in output ["
 123                             + output + "]" + "\n");
 124             return this;
 125         }
 126 
 127         ToolResult assertOutputDoesNotContain(String subString) {
 128             assertFalse(output.contains(subString),
 129                         "Expected to NOT find [" + subString + "], in output ["
 130                             + output + "]" + "\n");
 131             return this;
 132         }
 133     }
 134 
 135     static String getJava() {
 136         Path image = Paths.get(JAVA_HOME);
 137         boolean isWindows = System.getProperty("os.name").startsWith("Windows");
 138         Path java = image.resolve("bin").resolve(isWindows ? "java.exe" : "java");
 139         if (Files.notExists(java))
 140             throw new RuntimeException(java + " not found");
 141         return java.toAbsolutePath().toString();
 142     }
 143 }