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
+ /* @test
25
+ * @bug 8325203
26
+ * @summary Test that Jpackage windows executable application kills the launched 3rd party application
27
+ * when System.exit(0) is invoked along with terminating java program.
28
+ * @library ../helpers
29
+ * @library /test/lib
30
+ * @requires os.family == "windows"
31
+ * @build WinChildProcessTest
32
+ * @build jdk.jpackage.test.*
33
+ * @build WinChildProcessTest
34
+ * @modules jdk.jpackage/jdk.jpackage.internal
35
+ * @run main/othervm -Xmx512m jdk.jpackage.test.Main
36
+ * --jpt-run=WinChildProcessTest
37
+ *
38
+ */
39
+
40
+ import java .util .List ;
41
+ import java .util .Optional ;
42
+
43
+ import java .nio .file .Path ;
44
+
45
+ import jdk .jpackage .test .JPackageCommand ;
46
+ import jdk .jpackage .test .Annotations .Test ;
47
+ import jdk .jpackage .test .Executor ;
48
+ import jdk .jpackage .test .TKit ;
49
+
50
+ public class WinChildProcessTest {
51
+ private static final Path TEST_APP_JAVA = TKit .TEST_SRC_ROOT
52
+ .resolve ("apps/ChildProcessAppLauncher.java" );
53
+
54
+ @ Test
55
+ public static void test () throws Throwable {
56
+ long calcPid = 0 ;
57
+ try {
58
+ JPackageCommand cmd = JPackageCommand
59
+ .helloAppImage (TEST_APP_JAVA + "*Hello" );
60
+
61
+ // Create the image of the third party application launcher
62
+ cmd .executeAndAssertImageCreated ();
63
+
64
+ // Start the third party application launcher and dump and save the
65
+ // output of the application
66
+ List <String > output = new Executor ().saveOutput ().dumpOutput ()
67
+ .setExecutable (cmd .appLauncherPath ().toAbsolutePath ())
68
+ .execute (0 ).getOutput ();
69
+ String pidStr = output .get (0 );
70
+
71
+ // parse calculator PID
72
+ calcPid = Long .parseLong (pidStr .split ("=" , 2 )[1 ]);
73
+
74
+ // Check whether the termination of third party application launcher
75
+ // also terminating the launched third party application
76
+ // If third party application is not terminated the test is
77
+ // successful else failure
78
+ Optional <ProcessHandle > processHandle = ProcessHandle .of (calcPid );
79
+ boolean isAlive = processHandle .isPresent ()
80
+ && processHandle .get ().isAlive ();
81
+ System .out .println ("Is Alive " + isAlive );
82
+ TKit .assertTrue (isAlive , "Check is calculator process is alive" );
83
+ } finally {
84
+ // Kill only a specific calculator instance
85
+ Runtime .getRuntime ().exec ("taskkill /F /PID " + calcPid );
86
+ }
87
+ }
88
+ }
0 commit comments