Path: blob/master/test/jdk/java/awt/EventQueue/NonComponentSourcePost.java
41149 views
/*1* Copyright (c) 1999, 2019, 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*/2223import java.awt.AWTEvent;24import java.awt.ActiveEvent;25import java.awt.EventQueue;26import java.awt.Toolkit;27import java.awt.event.ActionEvent;28import java.util.concurrent.CountDownLatch;29import java.util.concurrent.TimeUnit;3031/**32* @test33* @bug 413779634* @summary Checks that the posting of events whose sources are not Components35* does not corrupt the EventQueue.36*/37public class NonComponentSourcePost {3839public static final int COUNT = 100;40public static CountDownLatch go = new CountDownLatch(COUNT);4142public static void main(String[] args) throws Throwable {43EventQueue q = new EventQueue();44for (int i = 0; i < COUNT; i++) {45q.postEvent(new NewActionEvent());46}47if (!go.await(30, TimeUnit.SECONDS)) {48throw new RuntimeException("Timeout");49}50AWTEvent event = q.peekEvent();51if (event != null) {52throw new Exception("Non null event: " + event);53}5455if (NewActionEvent.result != NewActionEvent.sum) {56throw new Exception("result: " + NewActionEvent.result +57" sum: " + NewActionEvent.sum);58}59// Simple way to shutdown the AWT machinery60Toolkit.getDefaultToolkit().getSystemEventQueue().push(q);61}6263static class NewActionEvent extends ActionEvent implements ActiveEvent {64static int counter = 1;65static int sum = 0;66static int result = 0;6768int myval;6970public NewActionEvent() {71super("", ACTION_PERFORMED, "" + counter);72myval = counter++;73sum += myval;74}7576public synchronized void dispatch() {77result += myval;78try {79wait(100);80} catch (InterruptedException e) {81}82go.countDown();83}84}85}868788