1 /*
   2  * Copyright (c) 2015, 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        8081485
  27   @summary    tests that a program terminates automatically after EventQueue.push()
  28 */
  29 
  30 import sun.awt.AWTAutoShutdown;
  31 
  32 import java.awt.*;
  33 import java.util.EmptyStackException;
  34 
  35 public class EventQueuePushAutoshutdown implements Runnable {
  36     private int status = 2;
  37 
  38     public EventQueuePushAutoshutdown() throws Exception {
  39         Runtime.getRuntime().addShutdownHook(new Thread(this));
  40         Thread thread = new Thread() {
  41             @Override
  42             public void run() {
  43                 status = 0;
  44                 try {
  45                     Thread.sleep(10000);
  46                 } catch (InterruptedException e) {
  47                     e.printStackTrace();
  48                 } finally {
  49                     status = 1;
  50                     System.exit(status);
  51                 }
  52             }
  53         };
  54         thread.setDaemon(true);
  55         thread.start();
  56 
  57         System.setProperty("java.awt.headless", "true");
  58         final EventQueue systemQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
  59         systemQueue.push(new EventQueue());
  60         EventQueue.invokeAndWait(new Runnable() {
  61             @Override
  62             public void run() {
  63                 System.out.println("Activated EDT");
  64             }
  65         });
  66         System.out.println("After EDT activation");
  67     }
  68 
  69     public static void main(String[] args) throws Exception  {
  70         new EventQueuePushAutoshutdown();
  71     }
  72 
  73     @Override
  74     public void run() {
  75         Runtime.getRuntime().halt(status);
  76     }
  77 }