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  * @summary Basic test of VM::getRuntimeArguments
  27  * @library /lib/testlibrary
  28  * @modules java.base/jdk.internal.misc
  29  * @run testng RuntimeArguments
  30  */
  31 
  32 import java.util.Arrays;
  33 import java.util.List;
  34 import java.util.stream.Stream;
  35 import jdk.internal.misc.VM;
  36 import jdk.testlibrary.ProcessTools;
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Test;
  39 import static org.testng.Assert.*;
  40 
  41 public class RuntimeArguments {
  42     static final String TEST_CLASSES = System.getProperty("test.classes");
  43 
  44     @DataProvider(name = "options")
  45     public Object[][] options() {
  46         return new Object[][] {
  47             { // CLI options
  48               List.of("-XaddExports:java.base/jdk.internal.misc=ALL-UNNAMED",
  49                       "-XaddExports:java.base/jdk.internal.perf=ALL-UNNAMED",
  50                       "-addmods", "jdk.zipfs"),
  51               // expected runtime arguments
  52               List.of("-Djdk.launcher.addexports.0=java.base/jdk.internal.misc=ALL-UNNAMED",
  53                       "-Djdk.launcher.addexports.1=java.base/jdk.internal.perf=ALL-UNNAMED",
  54                       "-Djdk.launcher.addmods=jdk.zipfs")
  55             },
  56             { // CLI options
  57               List.of("-XaddExports:java.base/jdk.internal.misc=ALL-UNNAMED",
  58                       "-addmods", "jdk.zipfs",
  59                       "-limitmods", "java.compact3"),
  60               // expected runtime arguments
  61               List.of("-Djdk.launcher.addexports.0=java.base/jdk.internal.misc=ALL-UNNAMED",
  62                       "-Djdk.launcher.addmods=jdk.zipfs", "-Djdk.launcher.limitmods=java.compact3")
  63             },
  64         };
  65     };
  66 
  67     public static void main(String... expected) {
  68         String[] vmArgs = VM.getRuntimeArguments();
  69         if (!Arrays.equals(vmArgs, expected)) {
  70             throw new RuntimeException(Arrays.toString(vmArgs) +
  71                 " != " + Arrays.toString(expected));
  72         }
  73     }
  74 
  75     @Test(dataProvider = "options")
  76     public void test(List<String> args, List<String> expected) throws Exception {
  77         // launch a test program
  78         // $ java <runtime-arguments> -classpath <cpath> RuntimeArguments <expected>
  79 
  80         Stream<String> options = Stream.concat(args.stream(),
  81             Stream.of("-classpath", TEST_CLASSES, "RuntimeArguments"));
  82 
  83         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  84             Stream.concat(options, expected.stream())
  85                   .toArray(String[]::new)
  86         );
  87         ProcessTools.executeProcess(pb).shouldHaveExitValue(0);
  88     }
  89 }