test/jdk/java/lang/Runtime/shutdown/Basic.java
changeset 48942 a6c4b85163c1
parent 48742 364944ba4e2f
equal deleted inserted replaced
48941:4f11514fe783 48942:a6c4b85163c1
    47 public class Basic {
    47 public class Basic {
    48 
    48 
    49     static Runtime rt = Runtime.getRuntime();
    49     static Runtime rt = Runtime.getRuntime();
    50     static PrintStream out = System.out;
    50     static PrintStream out = System.out;
    51 
    51 
       
    52     // Expect that no finalizer is invoked at exit
    52     @DataProvider(name = "testcase")
    53     @DataProvider(name = "testcase")
    53     public Object[][] getTestCase() {
    54     public Object[][] getTestCase() {
    54         return new Object[][] {
    55         return new Object[][] {
    55             { "fallThrough", 0, "h1", "f1" },
    56             { "fallThrough", 0, "h1", "" },
    56             { "exit0",       0, "h1", "f1" },
    57             { "exit0",       0, "h1", "" },
    57             { "exit0NoHook", 0, "",   "f1" },
    58             { "exit0NoHook", 0, "",   "" },
    58             { "exit1",       1, "h1", "" },
    59             { "exit1",       1, "h1", "" },
    59             { "exit1NoHook", 1, "",   "" },
    60             { "exit1NoHook", 1, "",   "" },
    60             { "halt",        0, "",   "" },
    61             { "halt",        0, "",   "" },
    61             { "haltNoHook",  0, "",   "" },
    62             { "haltNoHook",  0, "",   "" },
    62             { "haltInHook",  0, "h1", "" },
    63             { "haltInHook",  0, "h1", "" },
   110     }
   111     }
   111 
   112 
   112     public static void fallThrough() throws Exception {
   113     public static void fallThrough() throws Exception {
   113         rt.addShutdownHook(new Hook("h1"));
   114         rt.addShutdownHook(new Hook("h1"));
   114         Fin f = new Fin("f1");
   115         Fin f = new Fin("f1");
   115         rt.runFinalizersOnExit(true);
       
   116     }
   116     }
   117 
   117 
   118     public static void exit0() throws Exception {
   118     public static void exit0() throws Exception {
   119         rt.addShutdownHook(new Hook("h1"));
   119         rt.addShutdownHook(new Hook("h1"));
   120         Fin f = new Fin("f1");
   120         Fin f = new Fin("f1");
   121         rt.runFinalizersOnExit(true);
       
   122         rt.exit(0);
   121         rt.exit(0);
   123     }
   122     }
   124 
   123 
   125     public static void exit0NoHook() throws Exception {
   124     public static void exit0NoHook() throws Exception {
   126         Fin f = new Fin("f1");
   125         Fin f = new Fin("f1");
   127         rt.runFinalizersOnExit(true);
       
   128         rt.exit(0);
   126         rt.exit(0);
   129     }
   127     }
   130 
   128 
   131     /* Finalizer should not run */
   129     /* Finalizer should not run */
   132     public static void exit1() throws Exception {
   130     public static void exit1() throws Exception {
   133         rt.addShutdownHook(new Hook("h1"));
   131         rt.addShutdownHook(new Hook("h1"));
   134         Fin f = new Fin("f1");
   132         Fin f = new Fin("f1");
   135         rt.runFinalizersOnExit(true);
       
   136         rt.exit(1);
   133         rt.exit(1);
   137     }
   134     }
   138 
   135 
   139     public static void exit1NoHook() throws Exception {
   136     public static void exit1NoHook() throws Exception {
   140         Fin f = new Fin("f1");
   137         Fin f = new Fin("f1");
   141         rt.runFinalizersOnExit(true);
       
   142         rt.exit(1);
   138         rt.exit(1);
   143     }
   139     }
   144 
   140 
   145     public static void halt() throws Exception {
   141     public static void halt() throws Exception {
   146         rt.addShutdownHook(new Hook("h1") {
   142         rt.addShutdownHook(new Hook("h1") {
   147             public void go() { rt.halt(1); }});
   143             public void go() { rt.halt(1); }});
   148         Fin f = new Fin("f1") { public void go() { rt.halt(1); }};
   144         Fin f = new Fin("f1") { public void go() { rt.halt(1); }};
   149         rt.runFinalizersOnExit(true);
       
   150         rt.halt(0);
   145         rt.halt(0);
   151     }
   146     }
   152 
   147 
   153     public static void haltNoHook() throws Exception {
   148     public static void haltNoHook() throws Exception {
   154         Fin f = new Fin("f1") { public void go() { rt.halt(1); }};
   149         Fin f = new Fin("f1") { public void go() { rt.halt(1); }};
   155         rt.runFinalizersOnExit(true);
       
   156         rt.halt(0);
   150         rt.halt(0);
   157     }
   151     }
   158 
   152 
   159     public static void haltInHook() throws Exception {
   153     public static void haltInHook() throws Exception {
   160         rt.addShutdownHook(new Hook("h1") {
   154         rt.addShutdownHook(new Hook("h1") {
   161             public void go() { rt.halt(0); }});
   155             public void go() { rt.halt(0); }});
   162         Fin f = new Fin("f1");
   156         Fin f = new Fin("f1");
   163         rt.runFinalizersOnExit(true);
       
   164         rt.exit(1);
   157         rt.exit(1);
   165     }
   158     }
   166 
   159 
   167     public static void addLate() throws Exception {
   160     public static void addLate() throws Exception {
   168         rt.addShutdownHook(new Hook("h1") {
   161         rt.addShutdownHook(new Hook("h1") {
   189                 }
   182                 }
   190             }});
   183             }});
   191         rt.exit(1);
   184         rt.exit(1);
   192     }
   185     }
   193 
   186 
   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 */
   187     /* For INT, HUP, TERM */
   237     public static void stall() throws Exception {
   188     public static void stall() throws Exception {
   238         Fin f = new Fin("f1");
   189         Fin f = new Fin("f1");
   239         rt.runFinalizersOnExit(true);
       
   240         rt.addShutdownHook(new Hook("h1"));
   190         rt.addShutdownHook(new Hook("h1"));
   241         out.print("Type ^C now: ");
   191         out.print("Type ^C now: ");
   242         out.flush();
   192         out.flush();
   243         Thread.sleep(100000);
   193         Thread.sleep(100000);
   244     }
   194     }