test/jdk/java/lang/Runtime/shutdown/Basic.java
changeset 48742 364944ba4e2f
child 48942 a6c4b85163c1
equal deleted inserted replaced
48741:d682d3da7923 48742:364944ba4e2f
       
     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     @DataProvider(name = "testcase")
       
    53     public Object[][] getTestCase() {
       
    54         return new Object[][] {
       
    55             { "fallThrough", 0, "h1", "f1" },
       
    56             { "exit0",       0, "h1", "f1" },
       
    57             { "exit0NoHook", 0, "",   "f1" },
       
    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     }
       
    69 
       
    70     @Test(dataProvider = "testcase")
       
    71     public void test(String testcase, int exitValue, String hook, String finalizer)
       
    72             throws Exception {
       
    73         System.out.println("Test " + testcase);
       
    74         ProcessTools.executeTestJava("Basic", testcase)
       
    75                 .shouldHaveExitValue(exitValue)
       
    76                 .stdoutShouldMatch(
       
    77                     hook + (hook.isEmpty() ? "" : System.lineSeparator()) + finalizer);
       
    78         System.out.println("Passed");
       
    79     }
       
    80 
       
    81     public static class Fin {
       
    82         String name;
       
    83 
       
    84         public Fin(String name) {
       
    85             this.name = name;
       
    86         }
       
    87 
       
    88         public void go() { }
       
    89 
       
    90         protected void finalize() {
       
    91             out.println(name);
       
    92             go();
       
    93         }
       
    94     }
       
    95 
       
    96 
       
    97     public static class Hook extends Thread {
       
    98         String name;
       
    99 
       
   100         public Hook(String name) {
       
   101             this.name = name;
       
   102         }
       
   103 
       
   104         public void go() { }
       
   105 
       
   106         public void run() {
       
   107             out.println(name);
       
   108             go();
       
   109         }
       
   110     }
       
   111 
       
   112     public static void fallThrough() throws Exception {
       
   113         rt.addShutdownHook(new Hook("h1"));
       
   114         Fin f = new Fin("f1");
       
   115         rt.runFinalizersOnExit(true);
       
   116     }
       
   117 
       
   118     public static void exit0() throws Exception {
       
   119         rt.addShutdownHook(new Hook("h1"));
       
   120         Fin f = new Fin("f1");
       
   121         rt.runFinalizersOnExit(true);
       
   122         rt.exit(0);
       
   123     }
       
   124 
       
   125     public static void exit0NoHook() throws Exception {
       
   126         Fin f = new Fin("f1");
       
   127         rt.runFinalizersOnExit(true);
       
   128         rt.exit(0);
       
   129     }
       
   130 
       
   131     /* Finalizer should not run */
       
   132     public static void exit1() throws Exception {
       
   133         rt.addShutdownHook(new Hook("h1"));
       
   134         Fin f = new Fin("f1");
       
   135         rt.runFinalizersOnExit(true);
       
   136         rt.exit(1);
       
   137     }
       
   138 
       
   139     public static void exit1NoHook() throws Exception {
       
   140         Fin f = new Fin("f1");
       
   141         rt.runFinalizersOnExit(true);
       
   142         rt.exit(1);
       
   143     }
       
   144 
       
   145     public static void halt() throws Exception {
       
   146         rt.addShutdownHook(new Hook("h1") {
       
   147             public void go() { rt.halt(1); }});
       
   148         Fin f = new Fin("f1") { public void go() { rt.halt(1); }};
       
   149         rt.runFinalizersOnExit(true);
       
   150         rt.halt(0);
       
   151     }
       
   152 
       
   153     public static void haltNoHook() throws Exception {
       
   154         Fin f = new Fin("f1") { public void go() { rt.halt(1); }};
       
   155         rt.runFinalizersOnExit(true);
       
   156         rt.halt(0);
       
   157     }
       
   158 
       
   159     public static void haltInHook() throws Exception {
       
   160         rt.addShutdownHook(new Hook("h1") {
       
   161             public void go() { rt.halt(0); }});
       
   162         Fin f = new Fin("f1");
       
   163         rt.runFinalizersOnExit(true);
       
   164         rt.exit(1);
       
   165     }
       
   166 
       
   167     public static void addLate() throws Exception {
       
   168         rt.addShutdownHook(new Hook("h1") {
       
   169             public void go() {
       
   170                 try {
       
   171                     rt.addShutdownHook(new Hook("h2"));
       
   172                 } catch (IllegalStateException x) {
       
   173                     out.println("Caught as expected: " + x);
       
   174                     rt.halt(0);
       
   175                 }
       
   176             }});
       
   177         rt.exit(1);
       
   178     }
       
   179 
       
   180     public static void removeLate() throws Exception {
       
   181         final Hook h1 = new Hook("h1");
       
   182         rt.addShutdownHook(new Hook("h2") {
       
   183             public void go() {
       
   184                 try {
       
   185                     rt.removeShutdownHook(h1);
       
   186                 } catch (IllegalStateException x) {
       
   187                     out.println("Caught as expected: " + x);
       
   188                     rt.halt(0);
       
   189                 }
       
   190             }});
       
   191         rt.exit(1);
       
   192     }
       
   193 
       
   194 
       
   195     /* The following two methods are provided for manual testing only.
       
   196      * Neither test is suitable for being run from within the harness.
       
   197      */
       
   198 
       
   199     public static void awt() throws Exception {
       
   200         final Frame f = new Frame();
       
   201         final TextArea ta = new TextArea();
       
   202         Fin fx = new Fin("f1");
       
   203         rt.runFinalizersOnExit(true);
       
   204         rt.addShutdownHook(new Hook("h1") {
       
   205             public void go() {
       
   206                 ta.setText("Hooked!");
       
   207                 out.println("Hooked!");
       
   208                 try {
       
   209                     Thread.sleep(1000);
       
   210                 } catch (InterruptedException x) { }
       
   211                 ta.append("\nSleep 1");
       
   212                 out.println("Sleep 1");
       
   213                 try {
       
   214                     Thread.sleep(1000);
       
   215                 } catch (InterruptedException x) { }
       
   216                 ta.append("\nSleep 2");
       
   217                 out.println("Sleep 2");
       
   218             }});
       
   219         f.addWindowListener(new WindowAdapter() {
       
   220             public void windowClosing(WindowEvent e) {
       
   221                 out.println("Closing...");
       
   222                 ta.setText("Closing...");
       
   223                 try {
       
   224                     Thread.sleep(1000);
       
   225                 } catch (InterruptedException x) { }
       
   226                 f.dispose();
       
   227                 rt.exit(42);
       
   228             }});
       
   229         ta.setText("Terminate me!");
       
   230         f.add(ta);
       
   231         f.pack();
       
   232         f.show();
       
   233         Thread.sleep(10000);
       
   234     }
       
   235 
       
   236     /* For INT, HUP, TERM */
       
   237     public static void stall() throws Exception {
       
   238         Fin f = new Fin("f1");
       
   239         rt.runFinalizersOnExit(true);
       
   240         rt.addShutdownHook(new Hook("h1"));
       
   241         out.print("Type ^C now: ");
       
   242         out.flush();
       
   243         Thread.sleep(100000);
       
   244     }
       
   245 
       
   246 
       
   247     public static void main(String[] args) throws Throwable {
       
   248         Method m = Basic.class.getMethod(args[0], new Class[] { });
       
   249         String log = null;
       
   250         try {
       
   251             log = System.getProperty("log");
       
   252         } catch (SecurityException x) { }
       
   253         if (log != null) {
       
   254             out = new PrintStream(new FileOutputStream(log), true);
       
   255         }
       
   256         try {
       
   257             m.invoke(null, new Object[] { });
       
   258         } catch (InvocationTargetException x) {
       
   259             throw x.getTargetException();
       
   260         }
       
   261     }
       
   262 
       
   263 }