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