8183374: Refactor java/lang/Runtime shell tests to java

Reviewed-by: coffeys
This commit is contained in:
Fernando Guallini 2021-04-28 14:00:23 +00:00 committed by Sean Coffey
parent 343a4a76f2
commit ec383abc1d
4 changed files with 111 additions and 165 deletions

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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) and
* Runtime.exec(String command, String[] env, File path). * Runtime.exec(String command, String[] env, File path).
* *
* @build SetCwd * @library /test/lib
* @run shell setcwd.sh * @run testng/othervm SetCwd
*/ */
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.*; import java.io.*;
import static jdk.test.lib.Asserts.assertTrue;
public class SetCwd { 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++) { private static final String TEST_CLASSES = System.getProperty(
File f = files[i]; "test.classes", ".");
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);
}
BufferedReader in = new BufferedReader private static final String[] CMD_ARRAY = new String[2];
(new InputStreamReader(p.getInputStream()));
// Read back output from child @BeforeTest
String s = in.readLine(); public static void setUp() throws Exception {
if (!s.startsWith(newDir)) { CMD_ARRAY[0] = System.getProperty("java.home") + File.separator +
throw new Exception("inconsistent directory after exec"); "bin" + File.separator + "java";
} CMD_ARRAY[1] = SimpleProcess.class.getName();
// Join on the child }
p.waitFor();
@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 { // This class main will be the entry point for test subprocesses
String cmdarray[] = new String[2]; static class SimpleProcess {
cmdarray[0] = System.getProperty("java.home") + File.separator + public static void main (String[] args) throws Exception {
"bin" + File.separator + "java"; File dir = new File(".");
cmdarray[1] = "SetCwd"; System.out.println(dir.getCanonicalPath());
String cmd = cmdarray[0] + " " + cmdarray[1]; }
// test the two new methods
testExec(cmd, null, true);
testExec(null, cmdarray, false);
} }
} }

View file

@ -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

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 * @summary 1) Test Console and DeleteOnExitHook can be initialized
* while shutdown is in progress * while shutdown is in progress
* 2) Test if files that are added by the application shutdown * 2) Test if files that are added by the application shutdown
* hook are deleted on exit during 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.io.*;
import java.nio.file.Files;
import static jdk.test.lib.Asserts.assertFalse;
public class ShutdownHooks { 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 private static final String TEST_FILE_NAME = "fileToBeDeleted";
Runtime.getRuntime().addShutdownHook(new Cleaner());
File dir = new File(args[0]); private static final File TEST_FILE = new File(TEST_FILE_NAME);
file = new File(dir, args[1]);
// write to file private static final String TEST_CLASSES = System.getProperty(
System.out.println("writing to "+ file); "test.classes", ".");
try (PrintWriter pw = new PrintWriter(file)) {
pw.println("Shutdown begins"); @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 { @Test
public void run() { public void testShutdownHooks() throws Exception {
// register the Console's shutdown hook while the application // Run in a new process in order to evaluate shutdown hook results
// shutdown hook is running String[] testCommand = new String[] {"-classpath", TEST_CLASSES,
Console cons = System.console(); ShutdownHooksProcess.class.getName()};
// register the DeleteOnExitHook while the application ProcessTools.executeTestJvm(testCommand).shouldHaveExitValue(0);
// shutdown hook is running
file.deleteOnExit(); String errorMsg = "File exists despite shutdown hook has been run";
try (PrintWriter pw = new PrintWriter(file)) { assertFalse(Files.exists(TEST_FILE.toPath()), errorMsg);
pw.println("file is being deleted"); }
} catch (FileNotFoundException e) {
throw new RuntimeException(e); // 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);
}
} }
} }
} }
} }

View file

@ -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.";