author | prr |
Wed, 29 Nov 2017 09:47:02 -0800 (2017-11-29) | |
changeset 47982 | 44371ff1ee9a |
parent 47216 | 71c04702a3d5 |
child 54231 | e4813eded7cb |
permissions | -rw-r--r-- |
2 | 1 |
/* |
19175
478a81cf568d
8022247: java/awt/EventDispatchThread/LoopRobustness/LoopRobustness throws NPE
leonidr
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* |
|
26 |
* @bug 4023283 |
|
40128
e635645d2a8a
8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents:
19175
diff
changeset
|
27 |
* @key headful |
e635645d2a8a
8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents:
19175
diff
changeset
|
28 |
* @summary Checks that an Error which propogates up to the EventDispatch |
2 | 29 |
* loop does not crash AWT. |
2953
d8c81312ac1c
6847584: closed/java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.html fails
dcherepanov
parents:
2
diff
changeset
|
30 |
* @author Andrei Dmitriev: area=awt.event |
d8c81312ac1c
6847584: closed/java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.html fails
dcherepanov
parents:
2
diff
changeset
|
31 |
* @library ../../regtesthelpers |
d8c81312ac1c
6847584: closed/java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.html fails
dcherepanov
parents:
2
diff
changeset
|
32 |
* @build Util |
d8c81312ac1c
6847584: closed/java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.html fails
dcherepanov
parents:
2
diff
changeset
|
33 |
* @run main LoopRobustness |
2 | 34 |
*/ |
35 |
||
36 |
import java.awt.*; |
|
37 |
import java.awt.event.*; |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
38 |
|
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
39 |
import sun.awt.SunToolkit; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
40 |
|
2 | 41 |
import test.java.awt.regtesthelpers.Util; |
42 |
||
43 |
public class LoopRobustness { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
44 |
|
2 | 45 |
final static long TIMEOUT = 5000; |
46 |
final static Object LOCK = new Object(); |
|
47 |
||
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
48 |
public static int clicks = 0; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
49 |
public static volatile boolean notifyOccured = false; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
50 |
public static volatile boolean otherExceptionsCaught = false; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
51 |
|
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
52 |
public static void main(String [] args) throws Exception { |
19175
478a81cf568d
8022247: java/awt/EventDispatchThread/LoopRobustness/LoopRobustness throws NPE
leonidr
parents:
7668
diff
changeset
|
53 |
SunToolkit.createNewAppContext(); |
478a81cf568d
8022247: java/awt/EventDispatchThread/LoopRobustness/LoopRobustness throws NPE
leonidr
parents:
7668
diff
changeset
|
54 |
|
2 | 55 |
ThreadGroup mainThreadGroup = Thread.currentThread().getThreadGroup(); |
56 |
||
57 |
long at; |
|
58 |
//wait for a TIMEOUT giving a chance to a new Thread above to accomplish its stuff. |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
59 |
synchronized (LoopRobustness.LOCK) { |
2 | 60 |
new Thread(new TestThreadGroup(mainThreadGroup, "TestGroup"), new Impl()).start(); |
61 |
at = System.currentTimeMillis(); |
|
62 |
try { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
63 |
while (!notifyOccured && (System.currentTimeMillis() - at < TIMEOUT)) { |
2 | 64 |
LoopRobustness.LOCK.wait(1000); |
65 |
} |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
66 |
} catch (InterruptedException e) { |
2 | 67 |
throw new RuntimeException("Test interrupted.", e); |
68 |
} |
|
69 |
} |
|
70 |
||
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
71 |
if (!notifyOccured) { |
2 | 72 |
//notify doesn't occur after a reasonable time. |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
73 |
throw new RuntimeException("Test FAILED: second thread hasn't notified MainThread"); |
2 | 74 |
} |
75 |
||
76 |
//now wait for two clicks |
|
77 |
at = System.currentTimeMillis(); |
|
78 |
while(System.currentTimeMillis() - at < TIMEOUT && clicks < 2) { |
|
79 |
try { |
|
80 |
Thread.sleep(100); |
|
81 |
} catch(InterruptedException e) { |
|
82 |
throw new RuntimeException("Test interrupted.", e); |
|
83 |
} |
|
84 |
} |
|
85 |
if (clicks != 2) { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
86 |
throw new RuntimeException("Test FAILED: robot should press button twice"); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
87 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
88 |
if (otherExceptionsCaught) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
89 |
throw new RuntimeException("Test FAILED: unexpected exceptions caught"); |
2 | 90 |
} |
91 |
} |
|
92 |
} |
|
93 |
||
94 |
class Impl implements Runnable{ |
|
95 |
static Robot robot; |
|
96 |
public void run() { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
97 |
SunToolkit.createNewAppContext(); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
98 |
|
2 | 99 |
Button b = new Button("Press me to test the AWT-Event Queue thread"); |
100 |
Frame lr = new Frame("ROBUST FRAME"); |
|
101 |
lr.setBounds(100, 100, 300, 100); |
|
102 |
b.addActionListener(new ActionListener() { |
|
103 |
public void actionPerformed(ActionEvent e) { |
|
104 |
LoopRobustness.clicks++; |
|
105 |
//throwing an exception in Static Initializer |
|
106 |
System.out.println(HostileCrasher.aStaticMethod()); |
|
107 |
} |
|
108 |
}); |
|
109 |
lr.add(b); |
|
110 |
lr.setVisible(true); |
|
111 |
||
112 |
try { |
|
113 |
robot = new Robot(); |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
114 |
} catch (AWTException e) { |
2 | 115 |
throw new RuntimeException("Test interrupted.", e); |
116 |
} |
|
117 |
Util.waitForIdle(robot); |
|
118 |
||
119 |
synchronized (LoopRobustness.LOCK){ |
|
120 |
LoopRobustness.LOCK.notify(); |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
121 |
LoopRobustness.notifyOccured = true; |
2 | 122 |
} |
123 |
||
124 |
int i = 0; |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
125 |
while (i < 2) { |
2 | 126 |
robot.mouseMove(b.getLocationOnScreen().x + b.getWidth()/2, |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
127 |
b.getLocationOnScreen().y + b.getHeight()/2); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
128 |
Util.waitForIdle(robot); |
2 | 129 |
robot.mousePress(InputEvent.BUTTON1_MASK); |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
130 |
Util.waitForIdle(robot); |
2 | 131 |
robot.mouseRelease(InputEvent.BUTTON1_MASK); |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
132 |
Util.waitForIdle(robot); |
2 | 133 |
i++; |
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
class TestThreadGroup extends ThreadGroup { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
139 |
TestThreadGroup(ThreadGroup threadGroup, String name) { |
2 | 140 |
super(threadGroup, name); |
141 |
} |
|
142 |
||
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
143 |
public void uncaughtException(Thread thread, Throwable e) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
144 |
System.out.println("Exception caught: " + e); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
145 |
e.printStackTrace(System.out); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
146 |
System.out.flush(); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
147 |
if ((e instanceof ExceptionInInitializerError) || |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
148 |
(e instanceof NoClassDefFoundError)) |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
149 |
{ |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
150 |
// These two are expected |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
151 |
return; |
2 | 152 |
} |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
153 |
LoopRobustness.otherExceptionsCaught = true; |
2 | 154 |
} |
155 |
} |
|
156 |
||
157 |
class HostileCrasher { |
|
158 |
static { |
|
159 |
if (Math.random() >= 0.0) { |
|
160 |
throw new RuntimeException("Die, AWT-Event Queue thread!"); |
|
161 |
} |
|
162 |
} |
|
163 |
public static String aStaticMethod() { |
|
164 |
return "hello, world"; |
|
165 |
} |
|
166 |
} |