|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 8234808 |
| 27 | + * |
| 28 | + * @library /test/lib |
| 29 | + * @run main/othervm JdbOptions |
| 30 | + */ |
| 31 | + |
| 32 | +import jdk.test.lib.Platform; |
| 33 | +import lib.jdb.Jdb; |
| 34 | +import lib.jdb.JdbCommand; |
| 35 | +import jdk.test.lib.process.OutputAnalyzer; |
| 36 | + |
| 37 | +import java.lang.management.ManagementFactory; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +class JbdOptionsTarg { |
| 42 | + static final String OK_MSG = "JbdOptionsTarg: OK"; |
| 43 | + |
| 44 | + static String argString(String s) { |
| 45 | + return "arg >" + s + "<"; |
| 46 | + } |
| 47 | + |
| 48 | + static String propString(String name, String value) { |
| 49 | + return "prop[" + name + "] = >" + value + "<"; |
| 50 | + } |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + System.out.println(OK_MSG); |
| 54 | + // print all args |
| 55 | + List<String> vmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments(); |
| 56 | + for (String s: vmArgs) { |
| 57 | + System.out.println(argString(s)); |
| 58 | + } |
| 59 | + // print requested sys.props |
| 60 | + for (String p: args) { |
| 61 | + System.out.println(propString(p, System.getProperty(p))); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +public class JdbOptions { |
| 67 | + private static final String targ = JbdOptionsTarg.class.getName(); |
| 68 | + |
| 69 | + public static void main(String[] args) throws Exception { |
| 70 | + // the simplest case |
| 71 | + test("-connect", |
| 72 | + "com.sun.jdi.CommandLineLaunch:vmexec=java,options=-client -XX:+PrintVMOptions,main=" + targ) |
| 73 | + .expectedArg("-XX:+PrintVMOptions"); |
| 74 | + |
| 75 | + // pass property through 'options' |
| 76 | + test("-connect", |
| 77 | + "com.sun.jdi.CommandLineLaunch:vmexec=java,options='-Dboo=foo',main=" + targ + " boo") |
| 78 | + .expectedProp("boo", "foo"); |
| 79 | + |
| 80 | + // property with spaces |
| 81 | + test("-connect", |
| 82 | + "com.sun.jdi.CommandLineLaunch:vmexec=java,options=\"-Dboo=foo 2\",main=" + targ + " boo") |
| 83 | + .expectedProp("boo", "foo 2"); |
| 84 | + |
| 85 | + // property with spaces (with single quotes) |
| 86 | + test("-connect", |
| 87 | + "com.sun.jdi.CommandLineLaunch:vmexec=java,options='-Dboo=foo 2',main=" + targ + " boo") |
| 88 | + .expectedProp("boo", "foo 2"); |
| 89 | + |
| 90 | + // properties with spaces (with single quotes) |
| 91 | + test("-connect", |
| 92 | + "com.sun.jdi.CommandLineLaunch:vmexec=java,options=-Dboo=foo '-Dboo2=foo 2',main=" + targ + " boo boo2") |
| 93 | + .expectedProp("boo", "foo") |
| 94 | + .expectedProp("boo2", "foo 2"); |
| 95 | + |
| 96 | + // 'options' contains commas - values are quoted (double quotes) |
| 97 | + test("-connect", |
| 98 | + "com.sun.jdi.CommandLineLaunch:vmexec=java,options=\"-client\" \"-XX:+PrintVMOptions\"" |
| 99 | + + " \"-XX:StartFlightRecording=dumponexit=true,maxsize=500M\" \"-XX:FlightRecorderOptions=repository=jfrrep\"" |
| 100 | + + ",main=" + targ) |
| 101 | + .expectedArg("-XX:StartFlightRecording=dumponexit=true,maxsize=500M") |
| 102 | + .expectedArg("-XX:FlightRecorderOptions=repository=jfrrep"); |
| 103 | + |
| 104 | + // 'options' contains commas - values are quoted (single quotes) |
| 105 | + test("-connect", |
| 106 | + "com.sun.jdi.CommandLineLaunch:vmexec=java,options='-client' '-XX:+PrintVMOptions'" |
| 107 | + + " '-XX:StartFlightRecording=dumponexit=true,maxsize=500M' '-XX:FlightRecorderOptions=repository=jfrrep'" |
| 108 | + + ",main=" + targ) |
| 109 | + .expectedArg("-XX:StartFlightRecording=dumponexit=true,maxsize=500M") |
| 110 | + .expectedArg("-XX:FlightRecorderOptions=repository=jfrrep"); |
| 111 | + |
| 112 | + // java options are specified in 2 ways, with and without spaces |
| 113 | + // options are quoted by using single and double quotes. |
| 114 | + test("-Dprop1=val1", |
| 115 | + "-Dprop2=val 2", |
| 116 | + "-connect", |
| 117 | + "com.sun.jdi.CommandLineLaunch:vmexec=java,options=-Dprop3=val3 '-Dprop4=val 4'" |
| 118 | + + " \"-XX:StartFlightRecording=dumponexit=true,maxsize=500M\"" |
| 119 | + + " '-XX:FlightRecorderOptions=repository=jfrrep'" |
| 120 | + + ",main=" + targ + " prop1 prop2 prop3 prop4") |
| 121 | + .expectedProp("prop1", "val1") |
| 122 | + .expectedProp("prop2", "val 2") |
| 123 | + .expectedProp("prop3", "val3") |
| 124 | + .expectedProp("prop4", "val 4") |
| 125 | + .expectedArg("-XX:StartFlightRecording=dumponexit=true,maxsize=500M") |
| 126 | + .expectedArg("-XX:FlightRecorderOptions=repository=jfrrep"); |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + private static class TestResult { |
| 131 | + OutputAnalyzer out; |
| 132 | + TestResult(OutputAnalyzer output) { |
| 133 | + out = output; |
| 134 | + } |
| 135 | + TestResult expectedArg(String s) { |
| 136 | + out.shouldContain(JbdOptionsTarg.argString(s)); |
| 137 | + return this; |
| 138 | + } |
| 139 | + TestResult expectedProp(String name, String value) { |
| 140 | + out.shouldContain(JbdOptionsTarg.propString(name, value)); |
| 141 | + return this; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static TestResult test(String... args) throws Exception { |
| 146 | + System.out.println(); |
| 147 | + System.out.println("...testcase..."); |
| 148 | + if (Platform.isWindows()) { |
| 149 | + // on Windows we need to escape quotes |
| 150 | + args = Arrays.stream(args) |
| 151 | + .map(s -> s.replace("\"", "\\\"")) |
| 152 | + .toArray(String[]::new); |
| 153 | + } |
| 154 | + try (Jdb jdb = new Jdb(args)) { |
| 155 | + jdb.waitForSimplePrompt(1024, true); // 1024 lines should be enough |
| 156 | + jdb.command(JdbCommand.run().allowExit()); |
| 157 | + OutputAnalyzer out = new OutputAnalyzer(jdb.getJdbOutput()); |
| 158 | + out.shouldContain(JbdOptionsTarg.OK_MSG); |
| 159 | + return new TestResult(out); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments