mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-19 02:24:40 +02:00
8183374: Refactor java/lang/Runtime shell tests to java
Reviewed-by: coffeys
This commit is contained in:
parent
343a4a76f2
commit
ec383abc1d
4 changed files with 111 additions and 165 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1999, 2021, 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
|
||||
|
@ -28,52 +28,66 @@
|
|||
* Runtime.exec(String[] command, String[] env, File path) and
|
||||
* Runtime.exec(String command, String[] env, File path).
|
||||
*
|
||||
* @build SetCwd
|
||||
* @run shell setcwd.sh
|
||||
* @library /test/lib
|
||||
* @run testng/othervm SetCwd
|
||||
*/
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static jdk.test.lib.Asserts.assertTrue;
|
||||
|
||||
public class SetCwd {
|
||||
public static void testExec(String cmd, String[] cmdarray, boolean flag)
|
||||
throws Exception {
|
||||
File dir = new File(".");
|
||||
File[] files = dir.listFiles();
|
||||
String curDir = dir.getCanonicalPath();
|
||||
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
File f = files[i];
|
||||
if (f.isDirectory() && (new File(f, "SetCwd.class")).exists()) {
|
||||
String newDir = f.getCanonicalPath();
|
||||
// exec a new SetCwd in the sub directory
|
||||
Process p = null;
|
||||
if (flag) {
|
||||
p = Runtime.getRuntime().exec(cmd, null, f);
|
||||
} else {
|
||||
p = Runtime.getRuntime().exec(cmdarray, null, f);
|
||||
}
|
||||
private static final String TEST_CLASSES = System.getProperty(
|
||||
"test.classes", ".");
|
||||
|
||||
BufferedReader in = new BufferedReader
|
||||
(new InputStreamReader(p.getInputStream()));
|
||||
// Read back output from child
|
||||
String s = in.readLine();
|
||||
if (!s.startsWith(newDir)) {
|
||||
throw new Exception("inconsistent directory after exec");
|
||||
}
|
||||
// Join on the child
|
||||
p.waitFor();
|
||||
private static final String[] CMD_ARRAY = new String[2];
|
||||
|
||||
@BeforeTest
|
||||
public static void setUp() throws Exception {
|
||||
CMD_ARRAY[0] = System.getProperty("java.home") + File.separator +
|
||||
"bin" + File.separator + "java";
|
||||
CMD_ARRAY[1] = SimpleProcess.class.getName();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuntimeExecWithArray() throws Exception {
|
||||
Process process = Runtime.getRuntime().exec(CMD_ARRAY, null,
|
||||
new File(TEST_CLASSES));
|
||||
verifyProcessOutput(process);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuntimeExecWithString() throws Exception {
|
||||
String cmd = String.join(" ", CMD_ARRAY);
|
||||
Process process = Runtime.getRuntime().exec(cmd, null,
|
||||
new File(TEST_CLASSES));
|
||||
verifyProcessOutput(process);
|
||||
}
|
||||
|
||||
// Verify the process has executed by comparing its output with the expected
|
||||
private void verifyProcessOutput(Process process) throws Exception {
|
||||
process.waitFor();
|
||||
assertTrue(process.exitValue() == 0);
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(process.getInputStream()))) {
|
||||
String line = reader.readLine();
|
||||
if (!line.startsWith(TEST_CLASSES)) {
|
||||
String error = String.format("Expected process output first line: " +
|
||||
"'%s' Actual: '%s'", TEST_CLASSES, line);
|
||||
throw new Exception(error);
|
||||
}
|
||||
}
|
||||
System.out.println(curDir);
|
||||
}
|
||||
|
||||
public static void main (String args[]) throws Exception {
|
||||
String cmdarray[] = new String[2];
|
||||
cmdarray[0] = System.getProperty("java.home") + File.separator +
|
||||
"bin" + File.separator + "java";
|
||||
cmdarray[1] = "SetCwd";
|
||||
String cmd = cmdarray[0] + " " + cmdarray[1];
|
||||
// test the two new methods
|
||||
testExec(cmd, null, true);
|
||||
testExec(null, cmdarray, false);
|
||||
// This class main will be the entry point for test subprocesses
|
||||
static class SimpleProcess {
|
||||
public static void main (String[] args) throws Exception {
|
||||
File dir = new File(".");
|
||||
System.out.println(dir.getCanonicalPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
#! /bin/sh
|
||||
|
||||
#
|
||||
# Copyright (c) 1999, 2019, 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.
|
||||
#
|
||||
|
||||
#
|
||||
|
||||
if [ x"$TESTJAVA" = x ]; then
|
||||
TESTJAVA=$1
|
||||
shift
|
||||
fi
|
||||
if [ x"$TESTCLASSES" = x ]; then TESTCLASSES=.; fi
|
||||
|
||||
# copy the class to our working directory
|
||||
mkdir foo
|
||||
cp ${TESTCLASSES}/SetCwd.class .
|
||||
cp ${TESTCLASSES}/SetCwd.class foo
|
||||
|
||||
# now start the test
|
||||
${TESTJAVA}/bin/java ${TESTVMOPTS} SetCwd
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2009, 2021, 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
|
||||
|
@ -22,46 +22,76 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* @bug 6829503
|
||||
* @test
|
||||
* @bug 6829503
|
||||
* @summary 1) Test Console and DeleteOnExitHook can be initialized
|
||||
* while shutdown is in progress
|
||||
* 2) Test if files that are added by the application shutdown
|
||||
* hook are deleted on exit during shutdown
|
||||
* @library /test/lib
|
||||
* @run testng/othervm ShutdownHooks
|
||||
*/
|
||||
|
||||
import jdk.test.lib.process.ProcessTools;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import static jdk.test.lib.Asserts.assertFalse;
|
||||
|
||||
public class ShutdownHooks {
|
||||
private static File file;
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length != 2) {
|
||||
throw new IllegalArgumentException("Usage: ShutdownHooks <dir> <filename>");
|
||||
}
|
||||
|
||||
// Add a shutdown hook
|
||||
Runtime.getRuntime().addShutdownHook(new Cleaner());
|
||||
private static final String TEST_FILE_NAME = "fileToBeDeleted";
|
||||
|
||||
File dir = new File(args[0]);
|
||||
file = new File(dir, args[1]);
|
||||
// write to file
|
||||
System.out.println("writing to "+ file);
|
||||
try (PrintWriter pw = new PrintWriter(file)) {
|
||||
pw.println("Shutdown begins");
|
||||
}
|
||||
private static final File TEST_FILE = new File(TEST_FILE_NAME);
|
||||
|
||||
private static final String TEST_CLASSES = System.getProperty(
|
||||
"test.classes", ".");
|
||||
|
||||
@BeforeTest
|
||||
public static void setUp() throws Exception {
|
||||
// Make sure file does not exist before test
|
||||
Files.deleteIfExists(TEST_FILE.toPath());
|
||||
}
|
||||
|
||||
public static class Cleaner extends Thread {
|
||||
public void run() {
|
||||
// register the Console's shutdown hook while the application
|
||||
// shutdown hook is running
|
||||
Console cons = System.console();
|
||||
// register the DeleteOnExitHook while the application
|
||||
// shutdown hook is running
|
||||
file.deleteOnExit();
|
||||
try (PrintWriter pw = new PrintWriter(file)) {
|
||||
pw.println("file is being deleted");
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
@Test
|
||||
public void testShutdownHooks() throws Exception {
|
||||
// Run in a new process in order to evaluate shutdown hook results
|
||||
String[] testCommand = new String[] {"-classpath", TEST_CLASSES,
|
||||
ShutdownHooksProcess.class.getName()};
|
||||
ProcessTools.executeTestJvm(testCommand).shouldHaveExitValue(0);
|
||||
|
||||
String errorMsg = "File exists despite shutdown hook has been run";
|
||||
assertFalse(Files.exists(TEST_FILE.toPath()), errorMsg);
|
||||
}
|
||||
|
||||
// This class main will be the entry point for test subprocesses
|
||||
static class ShutdownHooksProcess {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Runtime.getRuntime().addShutdownHook(new Cleaner());
|
||||
|
||||
System.out.println("Writing to "+ TEST_FILE);
|
||||
try (PrintWriter pw = new PrintWriter(TEST_FILE)) {
|
||||
pw.println("Shutdown begins");
|
||||
}
|
||||
}
|
||||
|
||||
static class Cleaner extends Thread {
|
||||
public void run() {
|
||||
// register the Console's shutdown hook while the application
|
||||
// shutdown hook is running
|
||||
Console cons = System.console();
|
||||
// register the DeleteOnExitHook while the application
|
||||
// shutdown hook is running
|
||||
TEST_FILE.deleteOnExit();
|
||||
try (PrintWriter pw = new PrintWriter(TEST_FILE)) {
|
||||
pw.println("File is being deleted");
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
#
|
||||
# Copyright (c) 2009, 2019, 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 6829503
|
||||
# @summary 1) Test Console and DeleteOnExitHook can be initialized
|
||||
# while shutdown is in progress
|
||||
# 2) Test if files that are added by the application shutdown
|
||||
# hook are deleted on exit during shutdown
|
||||
#
|
||||
# @build ShutdownHooks
|
||||
# @run shell ShutdownHooks.sh
|
||||
|
||||
if [ "${TESTJAVA}" = "" ]
|
||||
then
|
||||
echo "TESTJAVA not set. Test cannot execute. Failed."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
FILENAME=fileToBeDeleted
|
||||
rm -f ${TESTCLASSES}/${FILENAME}
|
||||
|
||||
# create the file to be deleted on exit
|
||||
echo "testing shutdown" > ${TESTCLASSES}/${FILENAME}
|
||||
|
||||
${TESTJAVA}/bin/java ${TESTVMOPTS} -classpath ${TESTCLASSES} ShutdownHooks ${TESTCLASSES} $FILENAME
|
||||
if [ $? != 0 ] ; then
|
||||
echo "Test Failed"; exit 1
|
||||
fi
|
||||
|
||||
if [ -f ${TESTCLASSES}/${FILENAME} ]; then
|
||||
echo "Test Failed: ${TESTCLASSES}/${FILENAME} not deleted"; exit 2
|
||||
fi
|
||||
echo "ShutdownHooks test passed.";
|
Loading…
Add table
Add a link
Reference in a new issue