Skip to content

Commit

Permalink
8325525: Create jtreg test case for JDK-8325203
Browse files Browse the repository at this point in the history
Reviewed-by: asemenyuk, almatvee
  • Loading branch information
Vanitha-bp authored and Alexey Semenyuk committed Jul 11, 2024
1 parent b3ef2a6 commit 81a0d1b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/jdk/tools/jpackage/apps/ChildProcessAppLauncher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

public class ChildProcessAppLauncher {

public static void main(String[] args) throws IOException {
String calcPath = Path.of(System.getenv("SystemRoot"), "system32", "calc.exe").toString();
ProcessBuilder processBuilder = new ProcessBuilder(calcPath);
Process process = processBuilder.start();
System.out.println("Calc id=" + process.pid());
System.exit(0);
}
}
88 changes: 88 additions & 0 deletions test/jdk/tools/jpackage/windows/WinChildProcessTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/* @test
* @bug 8325203
* @summary Test that Jpackage windows executable application kills the launched 3rd party application
* when System.exit(0) is invoked along with terminating java program.
* @library ../helpers
* @library /test/lib
* @requires os.family == "windows"
* @build WinChildProcessTest
* @build jdk.jpackage.test.*
* @build WinChildProcessTest
* @modules jdk.jpackage/jdk.jpackage.internal
* @run main/othervm -Xmx512m jdk.jpackage.test.Main
* --jpt-run=WinChildProcessTest
*
*/

import java.util.List;
import java.util.Optional;

import java.nio.file.Path;

import jdk.jpackage.test.JPackageCommand;
import jdk.jpackage.test.Annotations.Test;
import jdk.jpackage.test.Executor;
import jdk.jpackage.test.TKit;

public class WinChildProcessTest {
private static final Path TEST_APP_JAVA = TKit.TEST_SRC_ROOT
.resolve("apps/ChildProcessAppLauncher.java");

@Test
public static void test() throws Throwable {
long calcPid = 0;
try {
JPackageCommand cmd = JPackageCommand
.helloAppImage(TEST_APP_JAVA + "*Hello");

// Create the image of the third party application launcher
cmd.executeAndAssertImageCreated();

// Start the third party application launcher and dump and save the
// output of the application
List<String> output = new Executor().saveOutput().dumpOutput()
.setExecutable(cmd.appLauncherPath().toAbsolutePath())
.execute(0).getOutput();
String pidStr = output.get(0);

// parse calculator PID
calcPid = Long.parseLong(pidStr.split("=", 2)[1]);

// Check whether the termination of third party application launcher
// also terminating the launched third party application
// If third party application is not terminated the test is
// successful else failure
Optional<ProcessHandle> processHandle = ProcessHandle.of(calcPid);
boolean isAlive = processHandle.isPresent()
&& processHandle.get().isAlive();
System.out.println("Is Alive " + isAlive);
TKit.assertTrue(isAlive, "Check is calculator process is alive");
} finally {
// Kill only a specific calculator instance
Runtime.getRuntime().exec("taskkill /F /PID " + calcPid);
}
}
}

0 comments on commit 81a0d1b

Please sign in to comment.