1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4227137 27 * @summary Basic functional test for virtual-machine shutdown hooks 28 * @modules java.desktop 29 * @library /test/lib 30 * @build jdk.test.lib.process.* 31 * @run testng Basic 32 */ 33 34 import java.lang.reflect.Method; 35 import java.lang.reflect.InvocationTargetException; 36 import java.io.PrintStream; 37 import java.io.FileOutputStream; 38 import java.awt.Frame; 39 import java.awt.TextArea; 40 import java.awt.event.WindowEvent; 41 import java.awt.event.WindowAdapter; 42 43 import org.testng.annotations.DataProvider; 44 import org.testng.annotations.Test; 45 import jdk.test.lib.process.ProcessTools; 46 47 public class Basic { 48 49 static Runtime rt = Runtime.getRuntime(); 50 static PrintStream out = System.out; 51 52 // Expect that no finalizer is invoked at exit 53 @DataProvider(name = "testcase") 54 public Object[][] getTestCase() { 55 return new Object[][] { 56 { "fallThrough", 0, "h1", "" }, 57 { "exit0", 0, "h1", "" }, 58 { "exit0NoHook", 0, "", "" }, 59 { "exit1", 1, "h1", "" }, 60 { "exit1NoHook", 1, "", "" }, 61 { "halt", 0, "", "" }, 62 { "haltNoHook", 0, "", "" }, 63 { "haltInHook", 0, "h1", "" }, 64 { "addLate", 0, "h1", 65 "Caught as expected: java.lang.IllegalStateException: Shutdown in progress" }, 66 { "removeLate", 0, "h2", 67 "Caught as expected: java.lang.IllegalStateException: Shutdown in progress" } 68 }; 69 } 70 71 @Test(dataProvider = "testcase") 72 public void test(String testcase, int exitValue, String hook, String finalizer) 73 throws Exception { 74 System.out.println("Test " + testcase); 75 ProcessTools.executeTestJava("Basic", testcase) 76 .shouldHaveExitValue(exitValue) 77 .stdoutShouldMatch( 78 hook + (hook.isEmpty() ? "" : System.lineSeparator()) + finalizer); 79 System.out.println("Passed"); 80 } 81 82 public static class Fin { 83 String name; 84 85 public Fin(String name) { 86 this.name = name; 87 } 88 89 public void go() { } 90 91 protected void finalize() { 92 out.println(name); 93 go(); 94 } 95 } 96 97 98 public static class Hook extends Thread { 99 String name; 100 101 public Hook(String name) { 102 this.name = name; 103 } 104 105 public void go() { } 106 107 public void run() { 108 out.println(name); 109 go(); 110 } 111 } 112 113 public static void fallThrough() throws Exception { 114 rt.addShutdownHook(new Hook("h1")); 115 Fin f = new Fin("f1"); 116 } 117 118 public static void exit0() throws Exception { 119 rt.addShutdownHook(new Hook("h1")); 120 Fin f = new Fin("f1"); 121 rt.exit(0); 122 } 123 124 public static void exit0NoHook() throws Exception { 125 Fin f = new Fin("f1"); 126 rt.exit(0); 127 } 128 129 /* Finalizer should not run */ 130 public static void exit1() throws Exception { 131 rt.addShutdownHook(new Hook("h1")); 132 Fin f = new Fin("f1"); 133 rt.exit(1); 134 } 135 136 public static void exit1NoHook() throws Exception { 137 Fin f = new Fin("f1"); 138 rt.exit(1); 139 } 140 141 public static void halt() throws Exception { 142 rt.addShutdownHook(new Hook("h1") { 143 public void go() { rt.halt(1); }}); 144 Fin f = new Fin("f1") { public void go() { rt.halt(1); }}; 145 rt.halt(0); 146 } 147 148 public static void haltNoHook() throws Exception { 149 Fin f = new Fin("f1") { public void go() { rt.halt(1); }}; 150 rt.halt(0); 151 } 152 153 public static void haltInHook() throws Exception { 154 rt.addShutdownHook(new Hook("h1") { 155 public void go() { rt.halt(0); }}); 156 Fin f = new Fin("f1"); 157 rt.exit(1); 158 } 159 160 public static void addLate() throws Exception { 161 rt.addShutdownHook(new Hook("h1") { 162 public void go() { 163 try { 164 rt.addShutdownHook(new Hook("h2")); 165 } catch (IllegalStateException x) { 166 out.println("Caught as expected: " + x); 167 rt.halt(0); 168 } 169 }}); 170 rt.exit(1); 171 } 172 173 public static void removeLate() throws Exception { 174 final Hook h1 = new Hook("h1"); 175 rt.addShutdownHook(new Hook("h2") { 176 public void go() { 177 try { 178 rt.removeShutdownHook(h1); 179 } catch (IllegalStateException x) { 180 out.println("Caught as expected: " + x); 181 rt.halt(0); 182 } 183 }}); 184 rt.exit(1); 185 } 186 187 /* For INT, HUP, TERM */ 188 public static void stall() throws Exception { 189 Fin f = new Fin("f1"); 190 rt.addShutdownHook(new Hook("h1")); 191 out.print("Type ^C now: "); 192 out.flush(); 193 Thread.sleep(100000); 194 } 195 196 197 public static void main(String[] args) throws Throwable { 198 Method m = Basic.class.getMethod(args[0], new Class[] { }); 199 String log = null; 200 try { 201 log = System.getProperty("log"); 202 } catch (SecurityException x) { } 203 if (log != null) { 204 out = new PrintStream(new FileOutputStream(log), true); 205 } 206 try { 207 m.invoke(null, new Object[] { }); 208 } catch (InvocationTargetException x) { 209 throw x.getTargetException(); 210 } 211 } 212 213 }