Path: blob/master/test/jdk/java/awt/EventQueue/InvocationEventTest/InvocationEventTest.java
41153 views
/*1* Copyright (c) 2014, 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*/222324import java.awt.*;25import java.awt.event.*;2627/*28* @test29* @summary To Test the following assertions in InvovationEvent.30* 1.InvocationEvent when dispatched, should invoke the31* run() method of the Runnable Interface.32* 2.If catchExceptions is false, Exception should be33* propagated up to the EventDispatchThread's dispatch loop.34* 3.If catchExceptions is true, InvocationEvent.getExceptions()35* should return the exception thrown inside thr run() method.36* 4.When InvocationEvent object is posted on to the EventQueue,37* InvocationEvent.dispatch() method should be invoked by the38* EventQueue.39* 5.If the notifier object is not null, notifyAll() of the40* notifier object should be invoked when the run() method returns.41* 6.To test whether the threads are invoked in the right order42* When InvocationEvents are nested.43* 7.The getWhen method should return timestamp which is less than44* current System time and greater than the time before it has45* actually happened46* @author Dmitriy Ermashov ([email protected])47* @run main InvocationEventTest48*/4950public class InvocationEventTest {51EventQueue eventQ1 = new EventQueue();5253Object lock = new Object();5455static final int delay = 5000;5657public volatile boolean notifierStatus = false;58public Object notifierLock = new Object();5960public volatile boolean threadStatus = false;61public volatile boolean childInvoked = false;6263public synchronized void doTest() throws Exception {64// Testing assertions 1, 2 and 7:65// 1.InvocationEvent when dispatched, should invoke the66// run() method of the Runnable Interface.67// 2.If catchExceptions is false, Exception should be68// propagated up to the EventDispatchThread's dispatch loop.69// 7.The getWhen method should return timestamp which is less than70// current System time and greater than the time before it has71// actually happened7273long timeBeforeInvoking = System.currentTimeMillis();7475Thread.sleep(10);7677InvocationEvent invoc = new InvocationEvent(this, () -> { threadStatus = true; }, lock, false);78invoc.dispatch();7980Thread.sleep(10);8182if (!threadStatus) {83synchronized (lock) {84lock.wait(delay);85}86}8788// testing getException() when no exception is thrown89if (invoc.getWhen() <= timeBeforeInvoking ||90invoc.getWhen() >= System.currentTimeMillis()) {91throw new RuntimeException("getWhen method is not getting the time at which event occured");92}9394if (invoc.getException() != null) {95throw new RuntimeException("InvocationEvent.getException() does not return null " +96"when catchException is false");97}9899// testing the normal behaviour of InvocationEvent100if (!threadStatus) {101throw new RuntimeException("InvocationEvent when dispatched, did not" +102" invoke the run() of the Runnable interface ");103}104threadStatus = false;105106// Testing assertion 3:107// 3.If catchExceptions is true, InvocationEvent.getExceptions()108// should return the exception thrown inside the run() method.109RuntimeException sampleExn = new RuntimeException(" test exception");110111invoc = new InvocationEvent(this, () -> { threadStatus = true; throw sampleExn; }, lock, true);112invoc.dispatch();113if (!threadStatus) {114synchronized (lock) {115lock.wait(delay);116}117}118// testing getException() when exception is thrown119// Should return the same exception thrown inside the run() method120if (!invoc.getException().equals(sampleExn)) {121throw new RuntimeException("getException() does not return " +122"the same Exception thrown inside the run() method ");123}124threadStatus = false;125126// Testing assertions 4 and 5:127// 4.When InvocationEvent object is posted on to the EventQueue,128// InvocationEvent.dispatch() method should be invoked by the129// EventQueue.130// 5.If the notifier object is not null, notifyAll() of the131// notifier object should be invoked when the run() method returns.132133Thread notify = new Thread(){134public void run() {135synchronized (this) {136try { wait(); } catch (InterruptedException e) { throw new RuntimeException(e); }137}138notifierStatus = true;139synchronized (notifierLock) {140notifierLock.notifyAll();141}142}143};144notify.start();145146while (notify.getState() != Thread.State.WAITING)147Thread.sleep(delay/5);148149InvocationEvent invocation = new InvocationEvent(this, () -> { }, (Object) notify, false);150eventQ1.postEvent(invocation);151152while(!invocation.isDispatched())153synchronized (notifierLock) {154notifierLock.wait(delay);155}156157while (notify.getState() != Thread.State.TERMINATED)158Thread.sleep(delay/5);159160if (!notifierStatus) {161throw new RuntimeException("Notifier object did not get notified" +162" When the run method of the Runnable returns ");163}164165// Testing assertion 6:166// 6.To test whether the threads are invoked in the right order167// When InvocationEvents are nested.168Thread thread = new Thread(){169public void run() {170InvocationEvent evt = new InvocationEvent(this, () -> { childInvoked = true; }, (Object) this, false);171new EventQueue().postEvent(evt);172synchronized (this) {173try {174wait(delay);175} catch (InterruptedException e) {176throw new RuntimeException(e);177}178}179threadStatus = true;180}181};182183invocation = new InvocationEvent(this, thread, lock, false);184185eventQ1.postEvent(invocation);186187while (!invocation.isDispatched())188synchronized (lock) {189lock.wait(delay);190}191192if (!threadStatus || !childInvoked) {193throw new RuntimeException("Nesting of InvocationEvents when dispatched," +194" did not invoke the run() of the Runnables properly ");195}196}197198public static void main(String[] args) throws Exception {199new InvocationEventTest().doTest();200}201}202203204205