Path: blob/master/test/jdk/java/lang/RuntimeTests/shutdown/ShutdownHooks.java
41152 views
/*1* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 682950326* @summary 1) Test Console and DeleteOnExitHook can be initialized27* while shutdown is in progress28* 2) Test if files that are added by the application shutdown29* hook are deleted on exit during shutdown30* @library /test/lib31* @run testng/othervm ShutdownHooks32*/3334import jdk.test.lib.process.ProcessTools;35import org.testng.annotations.BeforeTest;36import org.testng.annotations.Test;3738import java.io.*;39import java.nio.file.Files;4041import static jdk.test.lib.Asserts.assertFalse;4243public class ShutdownHooks {4445private static final String TEST_FILE_NAME = "fileToBeDeleted";4647private static final File TEST_FILE = new File(TEST_FILE_NAME);4849private static final String TEST_CLASSES = System.getProperty(50"test.classes", ".");5152@BeforeTest53public static void setUp() throws Exception {54// Make sure file does not exist before test55Files.deleteIfExists(TEST_FILE.toPath());56}5758@Test59public void testShutdownHooks() throws Exception {60// Run in a new process in order to evaluate shutdown hook results61String[] testCommand = new String[] {"-classpath", TEST_CLASSES,62ShutdownHooksProcess.class.getName()};63ProcessTools.executeTestJvm(testCommand).shouldHaveExitValue(0);6465String errorMsg = "File exists despite shutdown hook has been run";66assertFalse(Files.exists(TEST_FILE.toPath()), errorMsg);67}6869// This class main will be the entry point for test subprocesses70static class ShutdownHooksProcess {71public static void main(String[] args) throws Exception {72Runtime.getRuntime().addShutdownHook(new Cleaner());7374System.out.println("Writing to "+ TEST_FILE);75try (PrintWriter pw = new PrintWriter(TEST_FILE)) {76pw.println("Shutdown begins");77}78}7980static class Cleaner extends Thread {81public void run() {82// register the Console's shutdown hook while the application83// shutdown hook is running84Console cons = System.console();85// register the DeleteOnExitHook while the application86// shutdown hook is running87TEST_FILE.deleteOnExit();88try (PrintWriter pw = new PrintWriter(TEST_FILE)) {89pw.println("File is being deleted");90} catch (FileNotFoundException e) {91throw new RuntimeException(e);92}93}94}95}96}979899