Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.java
38828 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*25* @bug 402328326* @summary Checks that an Error which propogate up to the EventDispatch27* loop does not crash AWT.28* @author Andrei Dmitriev: area=awt.event29* @library ../../regtesthelpers30* @build Util31* @run main LoopRobustness32*/3334import java.awt.*;35import java.awt.event.*;3637import sun.awt.SunToolkit;3839import test.java.awt.regtesthelpers.Util;4041public class LoopRobustness {4243final static long TIMEOUT = 5000;44final static Object LOCK = new Object();4546public static int clicks = 0;47public static volatile boolean notifyOccured = false;48public static volatile boolean otherExceptionsCaught = false;4950public static void main(String [] args) throws Exception {51SunToolkit.createNewAppContext();5253ThreadGroup mainThreadGroup = Thread.currentThread().getThreadGroup();5455long at;56//wait for a TIMEOUT giving a chance to a new Thread above to accomplish its stuff.57synchronized (LoopRobustness.LOCK) {58new Thread(new TestThreadGroup(mainThreadGroup, "TestGroup"), new Impl()).start();59at = System.currentTimeMillis();60try {61while (!notifyOccured && (System.currentTimeMillis() - at < TIMEOUT)) {62LoopRobustness.LOCK.wait(1000);63}64} catch (InterruptedException e) {65throw new RuntimeException("Test interrupted.", e);66}67}6869if (!notifyOccured) {70//notify doesn't occur after a reasonable time.71throw new RuntimeException("Test FAILED: second thread hasn't notified MainThread");72}7374//now wait for two clicks75at = System.currentTimeMillis();76while(System.currentTimeMillis() - at < TIMEOUT && clicks < 2) {77try {78Thread.sleep(100);79} catch(InterruptedException e) {80throw new RuntimeException("Test interrupted.", e);81}82}83if (clicks != 2) {84throw new RuntimeException("Test FAILED: robot should press button twice");85}86if (otherExceptionsCaught) {87throw new RuntimeException("Test FAILED: unexpected exceptions caught");88}89}90}9192class Impl implements Runnable{93static Robot robot;94public void run() {95SunToolkit.createNewAppContext();9697Button b = new Button("Press me to test the AWT-Event Queue thread");98Frame lr = new Frame("ROBUST FRAME");99lr.setBounds(100, 100, 300, 100);100b.addActionListener(new ActionListener() {101public void actionPerformed(ActionEvent e) {102LoopRobustness.clicks++;103//throwing an exception in Static Initializer104System.out.println(HostileCrasher.aStaticMethod());105}106});107lr.add(b);108lr.setVisible(true);109110try {111robot = new Robot();112} catch (AWTException e) {113throw new RuntimeException("Test interrupted.", e);114}115Util.waitForIdle(robot);116117synchronized (LoopRobustness.LOCK){118LoopRobustness.LOCK.notify();119LoopRobustness.notifyOccured = true;120}121122int i = 0;123while (i < 2) {124robot.mouseMove(b.getLocationOnScreen().x + b.getWidth()/2,125b.getLocationOnScreen().y + b.getHeight()/2);126Util.waitForIdle(robot);127robot.mousePress(InputEvent.BUTTON1_MASK);128Util.waitForIdle(robot);129robot.mouseRelease(InputEvent.BUTTON1_MASK);130Util.waitForIdle(robot);131i++;132}133}134}135136class TestThreadGroup extends ThreadGroup {137TestThreadGroup(ThreadGroup threadGroup, String name) {138super(threadGroup, name);139}140141public void uncaughtException(Thread thread, Throwable e) {142System.out.println("Exception caught: " + e);143e.printStackTrace(System.out);144System.out.flush();145if ((e instanceof ExceptionInInitializerError) ||146(e instanceof NoClassDefFoundError))147{148// These two are expected149return;150}151LoopRobustness.otherExceptionsCaught = true;152}153}154155class HostileCrasher {156static {157if (Math.random() >= 0.0) {158throw new RuntimeException("Die, AWT-Event Queue thread!");159}160}161public static String aStaticMethod() {162return "hello, world";163}164}165166167