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