1 /*
2  * Copyright (c) 1999, 2019, 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 import java.awt.AWTEvent;
25 import java.awt.ActiveEvent;
26 import java.awt.EventQueue;
27 import java.awt.Toolkit;
28 import java.awt.event.ActionEvent;
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.TimeUnit;
31 
32 /**
33  * @test
34  * @bug 4137796
35  * @summary Checks that the posting of events whose sources are not Components
36  *          does not corrupt the EventQueue.
37  */
38 public class NonComponentSourcePost {
39 
40     public static final int COUNT = 100;
41     public static CountDownLatch go = new CountDownLatch(COUNT);
42 
main(String[] args)43     public static void main(String[] args) throws Throwable {
44         EventQueue q = new EventQueue();
45         for (int i = 0; i < COUNT; i++) {
46             q.postEvent(new NewActionEvent());
47         }
48         if (!go.await(30, TimeUnit.SECONDS)) {
49             throw new RuntimeException("Timeout");
50         }
51         AWTEvent event = q.peekEvent();
52         if (event != null) {
53             throw new Exception("Non null event: " + event);
54         }
55 
56         if (NewActionEvent.result != NewActionEvent.sum) {
57             throw new Exception("result: " + NewActionEvent.result +
58                                 "  sum: " + NewActionEvent.sum);
59         }
60         // Simple way to shutdown the AWT machinery
61         Toolkit.getDefaultToolkit().getSystemEventQueue().push(q);
62     }
63 
64     static class NewActionEvent extends ActionEvent implements ActiveEvent {
65         static int counter = 1;
66         static int sum = 0;
67         static int result = 0;
68 
69         int myval;
70 
NewActionEvent()71         public NewActionEvent() {
72             super("", ACTION_PERFORMED, "" + counter);
73             myval = counter++;
74             sum += myval;
75         }
76 
dispatch()77         public synchronized void dispatch() {
78             result += myval;
79             try {
80                 wait(100);
81             } catch (InterruptedException e) {
82             }
83             go.countDown();
84         }
85     }
86 }
87