1 /*
   2  * Copyright (c) 2016, 2017, 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 8136930
  27  * @summary Test that the VM ignores explicitly specified module internal properties.
  28  * @modules java.base/jdk.internal.misc
  29  * @library /test/lib
  30  * @run driver IgnoreModulePropertiesTest
  31  */
  32 
  33 import jdk.test.lib.process.OutputAnalyzer;
  34 import jdk.test.lib.process.ProcessTools;
  35 
  36 // Test that the VM ignores module related properties such as "jdk.module.addmods"
  37 // and jdk.module.addreads.0" that can only be set using module options.
  38 public class IgnoreModulePropertiesTest {
  39 
  40     // Test that the specified property and its value are ignored.  If the VM accepted
  41     // the property and value then an exception would be thrown because the value is
  42     // bogus for that property.  But, since the property is ignored no exception is
  43     // thrown.
  44     public static void testProperty(String prop, String value) throws Exception {
  45         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  46             "-D" + prop + "=" + value, "-version");
  47         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  48         output.shouldContain(" version ");
  49         output.shouldHaveExitValue(0);
  50 
  51         // Ensure that the property and its value aren't available.
  52         if (System.getProperty(prop) != null) {
  53             throw new RuntimeException(
  54                 "Unexpected non-null value for property " + prop);
  55         }
  56     }
  57 
  58     // For options of the form "option=value", check that an exception gets thrown for
  59     // the illegal value and then check that its corresponding property is handled
  60     // correctly.
  61     public static void testOption(String option, String value,
  62                                   String prop, String result) throws Exception {
  63         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  64             option + "=" + value, "-version");
  65         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  66         output.shouldContain(result);
  67         testProperty(prop, value);
  68     }
  69 
  70     public static void main(String[] args) throws Exception {
  71         testOption("--add-modules", "java.sqlx", "jdk.module.addmods.0", "java.lang.module.FindException");
  72         testOption("--limit-modules", "java.sqlx", "jdk.module.limitmods", "java.lang.module.FindException");
  73         testOption("--add-reads", "xyzz=yyzd", "jdk.module.addreads.0", "WARNING: Unknown module: xyzz");
  74         testOption("--add-exports", "java.base/xyzz=yyzd", "jdk.module.addexports.0",
  75                    "WARNING: package xyzz not in java.base");
  76         testOption("--patch-module", "=d", "jdk.module.patch.0", "Unable to parse --patch-module");
  77     }
  78 }