Path: blob/master/test/jdk/java/lang/RuntimeTests/shutdown/Basic.java
41152 views
/*1* Copyright (c) 2018, 2019, 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 422713726* @summary Basic functional test for virtual-machine shutdown hooks27* @modules java.desktop28* @library /test/lib29* @build jdk.test.lib.process.*30* @run testng Basic31*/3233import java.lang.reflect.Method;34import java.lang.reflect.InvocationTargetException;35import java.io.PrintStream;36import java.io.FileOutputStream;37import java.awt.Frame;38import java.awt.TextArea;39import java.awt.event.WindowEvent;40import java.awt.event.WindowAdapter;4142import org.testng.annotations.DataProvider;43import org.testng.annotations.Test;44import jdk.test.lib.process.ProcessTools;4546public class Basic {4748static Runtime rt = Runtime.getRuntime();49static PrintStream out = System.out;5051// Expect that no finalizer is invoked at exit52@DataProvider(name = "testcase")53public Object[][] getTestCase() {54return new Object[][] {55{ "fallThrough", 0, "h1", "" },56{ "exit0", 0, "h1", "" },57{ "exit0NoHook", 0, "", "" },58{ "exit1", 1, "h1", "" },59{ "exit1NoHook", 1, "", "" },60{ "halt", 0, "", "" },61{ "haltNoHook", 0, "", "" },62{ "haltInHook", 0, "h1", "" },63{ "addLate", 0, "h1",64"Caught as expected: java.lang.IllegalStateException: Shutdown in progress" },65{ "removeLate", 0, "h2",66"Caught as expected: java.lang.IllegalStateException: Shutdown in progress" }67};68}6970@Test(dataProvider = "testcase")71public void test(String testcase, int exitValue, String hook, String finalizer)72throws Exception {73System.out.println("Test " + testcase);74ProcessTools.executeTestJava("Basic", testcase)75.shouldHaveExitValue(exitValue)76.stdoutShouldMatch(77hook + (hook.isEmpty() ? "" : System.lineSeparator()) + finalizer);78System.out.println("Passed");79}8081public static class Fin {82String name;8384public Fin(String name) {85this.name = name;86}8788public void go() { }8990protected void finalize() {91out.println(name);92go();93}94}959697public static class Hook extends Thread {98String name;99100public Hook(String name) {101this.name = name;102}103104public void go() { }105106public void run() {107out.println(name);108go();109}110}111112public static void fallThrough() throws Exception {113rt.addShutdownHook(new Hook("h1"));114Fin f = new Fin("f1");115}116117public static void exit0() throws Exception {118rt.addShutdownHook(new Hook("h1"));119Fin f = new Fin("f1");120rt.exit(0);121}122123public static void exit0NoHook() throws Exception {124Fin f = new Fin("f1");125rt.exit(0);126}127128/* Finalizer should not run */129public static void exit1() throws Exception {130rt.addShutdownHook(new Hook("h1"));131Fin f = new Fin("f1");132rt.exit(1);133}134135public static void exit1NoHook() throws Exception {136Fin f = new Fin("f1");137rt.exit(1);138}139140public static void halt() throws Exception {141rt.addShutdownHook(new Hook("h1") {142public void go() { rt.halt(1); }});143Fin f = new Fin("f1") { public void go() { rt.halt(1); }};144rt.halt(0);145}146147public static void haltNoHook() throws Exception {148Fin f = new Fin("f1") { public void go() { rt.halt(1); }};149rt.halt(0);150}151152public static void haltInHook() throws Exception {153rt.addShutdownHook(new Hook("h1") {154public void go() { rt.halt(0); }});155Fin f = new Fin("f1");156rt.exit(1);157}158159public static void addLate() throws Exception {160rt.addShutdownHook(new Hook("h1") {161public void go() {162try {163rt.addShutdownHook(new Hook("h2"));164} catch (IllegalStateException x) {165out.println("Caught as expected: " + x);166rt.halt(0);167}168}});169rt.exit(1);170}171172public static void removeLate() throws Exception {173final Hook h1 = new Hook("h1");174rt.addShutdownHook(new Hook("h2") {175public void go() {176try {177rt.removeShutdownHook(h1);178} catch (IllegalStateException x) {179out.println("Caught as expected: " + x);180rt.halt(0);181}182}});183rt.exit(1);184}185186/* For INT, HUP, TERM */187public static void stall() throws Exception {188Fin f = new Fin("f1");189rt.addShutdownHook(new Hook("h1"));190out.print("Type ^C now: ");191out.flush();192Thread.sleep(100000);193}194195196public static void main(String[] args) throws Throwable {197Method m = Basic.class.getMethod(args[0], new Class[] { });198String log = null;199try {200log = System.getProperty("log");201} catch (SecurityException x) { }202if (log != null) {203out = new PrintStream(new FileOutputStream(log), true);204}205try {206m.invoke(null, new Object[] { });207} catch (InvocationTargetException x) {208throw x.getTargetException();209}210}211212}213214215