Path: blob/master/test/jdk/java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.java
41153 views
/*1* Copyright (c) 1998, 2013, 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 402328326* @key headful27* @summary Checks that an Error which propogates up to the EventDispatch28* loop does not crash AWT.29* @author Andrei Dmitriev: area=awt.event30* @library ../../regtesthelpers31* @modules java.desktop/sun.awt32* @build Util33* @run main LoopRobustness34*/3536import java.awt.*;37import java.awt.event.*;3839import sun.awt.SunToolkit;4041import test.java.awt.regtesthelpers.Util;4243public class LoopRobustness {4445final static long TIMEOUT = 5000;46final static Object LOCK = new Object();4748public static int clicks = 0;49public static volatile boolean notifyOccured = false;50public static volatile boolean otherExceptionsCaught = false;5152public static void main(String [] args) throws Exception {53SunToolkit.createNewAppContext();5455ThreadGroup mainThreadGroup = Thread.currentThread().getThreadGroup();5657long at;58//wait for a TIMEOUT giving a chance to a new Thread above to accomplish its stuff.59synchronized (LoopRobustness.LOCK) {60new Thread(new TestThreadGroup(mainThreadGroup, "TestGroup"), new Impl()).start();61at = System.currentTimeMillis();62try {63while (!notifyOccured && (System.currentTimeMillis() - at < TIMEOUT)) {64LoopRobustness.LOCK.wait(1000);65}66} catch (InterruptedException e) {67throw new RuntimeException("Test interrupted.", e);68}69}7071if (!notifyOccured) {72//notify doesn't occur after a reasonable time.73throw new RuntimeException("Test FAILED: second thread hasn't notified MainThread");74}7576//now wait for two clicks77at = System.currentTimeMillis();78while(System.currentTimeMillis() - at < TIMEOUT && clicks < 2) {79try {80Thread.sleep(100);81} catch(InterruptedException e) {82throw new RuntimeException("Test interrupted.", e);83}84}85if (clicks != 2) {86throw new RuntimeException("Test FAILED: robot should press button twice");87}88if (otherExceptionsCaught) {89throw new RuntimeException("Test FAILED: unexpected exceptions caught");90}91}92}9394class Impl implements Runnable{95static Robot robot;96public void run() {97SunToolkit.createNewAppContext();9899Button b = new Button("Press me to test the AWT-Event Queue thread");100Frame lr = new Frame("ROBUST FRAME");101lr.setBounds(100, 100, 300, 100);102b.addActionListener(new ActionListener() {103public void actionPerformed(ActionEvent e) {104LoopRobustness.clicks++;105//throwing an exception in Static Initializer106System.out.println(HostileCrasher.aStaticMethod());107}108});109lr.add(b);110lr.setVisible(true);111112try {113robot = new Robot();114} catch (AWTException e) {115throw new RuntimeException("Test interrupted.", e);116}117Util.waitForIdle(robot);118119synchronized (LoopRobustness.LOCK){120LoopRobustness.LOCK.notify();121LoopRobustness.notifyOccured = true;122}123124int i = 0;125while (i < 2) {126robot.mouseMove(b.getLocationOnScreen().x + b.getWidth()/2,127b.getLocationOnScreen().y + b.getHeight()/2);128Util.waitForIdle(robot);129robot.mousePress(InputEvent.BUTTON1_MASK);130Util.waitForIdle(robot);131robot.mouseRelease(InputEvent.BUTTON1_MASK);132Util.waitForIdle(robot);133i++;134}135}136}137138class TestThreadGroup extends ThreadGroup {139TestThreadGroup(ThreadGroup threadGroup, String name) {140super(threadGroup, name);141}142143public void uncaughtException(Thread thread, Throwable e) {144System.out.println("Exception caught: " + e);145e.printStackTrace(System.out);146System.out.flush();147if ((e instanceof ExceptionInInitializerError) ||148(e instanceof NoClassDefFoundError))149{150// These two are expected151return;152}153LoopRobustness.otherExceptionsCaught = true;154}155}156157class HostileCrasher {158static {159if (Math.random() >= 0.0) {160throw new RuntimeException("Die, AWT-Event Queue thread!");161}162}163public static String aStaticMethod() {164return "hello, world";165}166}167168169