|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 8334433 |
| 27 | + * @summary Verify that when running JShell on platforms that support FFMTerminalProvider, |
| 28 | + * no new processes are spawned. |
| 29 | + * @requires os.family == "windows" | os.family == "mac" | os.family == "linux" |
| 30 | + * @library /tools/lib |
| 31 | + * @modules jdk.compiler/com.sun.tools.javac.api |
| 32 | + * jdk.compiler/com.sun.tools.javac.main |
| 33 | + * jdk.compiler/com.sun.tools.javac.util |
| 34 | + * @build toolbox.ToolBox toolbox.JavaTask TerminalNoExecTest |
| 35 | + * @run main TerminalNoExecTest |
| 36 | + */ |
| 37 | + |
| 38 | +import java.io.Writer; |
| 39 | +import java.nio.file.Files; |
| 40 | +import java.nio.file.Path; |
| 41 | +import java.nio.file.Paths; |
| 42 | +import java.util.concurrent.TimeUnit; |
| 43 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 44 | +import jdk.jfr.consumer.RecordingStream; |
| 45 | +import jdk.jshell.tool.JavaShellToolBuilder; |
| 46 | + |
| 47 | +import toolbox.ToolBox; |
| 48 | + |
| 49 | +public class TerminalNoExecTest { |
| 50 | + |
| 51 | + public static void main(String... args) throws Exception { |
| 52 | + if (args.length > 0) { |
| 53 | + AtomicBoolean spawnedNewProcess = new AtomicBoolean(); |
| 54 | + try (var rs = new RecordingStream()) { |
| 55 | + rs.enable("jdk.ProcessStart").withoutThreshold(); |
| 56 | + rs.onEvent(evt -> { |
| 57 | + System.err.println("evt: " + evt); |
| 58 | + spawnedNewProcess.set(true); |
| 59 | + }); |
| 60 | + rs.startAsync(); |
| 61 | + JavaShellToolBuilder.builder().run("--execution=local", "--no-startup"); |
| 62 | + rs.stop(); |
| 63 | + } |
| 64 | + if (spawnedNewProcess.get()) { |
| 65 | + System.err.println("Spawned a new process!"); |
| 66 | + System.exit(1); |
| 67 | + } |
| 68 | + System.exit(0); |
| 69 | + } else { |
| 70 | + Path testScript = Paths.get("do-exit"); |
| 71 | + try (Writer w = Files.newBufferedWriter(testScript)) { |
| 72 | + w.append("/exit\n"); |
| 73 | + } |
| 74 | + |
| 75 | + ToolBox tb = new ToolBox(); |
| 76 | + Process target = |
| 77 | + new ProcessBuilder(tb.getJDKTool("java").toString(), |
| 78 | + "-classpath", System.getProperty("java.class.path"), |
| 79 | + TerminalNoExecTest.class.getName(), |
| 80 | + "run-test") |
| 81 | + .redirectError(ProcessBuilder.Redirect.INHERIT) |
| 82 | + .redirectOutput(ProcessBuilder.Redirect.INHERIT) |
| 83 | + .redirectInput(testScript.toFile()) |
| 84 | + .start(); |
| 85 | + |
| 86 | + target.waitFor(); |
| 87 | + |
| 88 | + int exitCode = target.exitValue(); |
| 89 | + |
| 90 | + if (exitCode != 0) { |
| 91 | + throw new AssertionError("Incorrect exit value, expected 0, got: " + exitCode); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments