1 /*
   2  * Copyright (c) 2007, 2018, 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 /*
  25   @test
  26   @key headful
  27   @bug 6541903
  28   @summary Tests if the realSync() throws the IllegalThreadException while invoked on the EDT
  29   @author anthony.petrov: area=awt.toolkit
  30   @modules java.desktop/sun.awt
  31   @run main/timeout=10 RealSyncOnEDT
  32 */
  33 
  34 
  35 /**
  36  * RealSyncOnEDT.java
  37  *
  38  * summary: Tests if the realSync() throws the IllegalThreadException while invoked on the EDT
  39  */
  40 
  41 import java.awt.*;
  42 import java.awt.event.*;
  43 
  44 
  45 public class RealSyncOnEDT
  46 {
  47     static Exception exceptionCaught = null;
  48 
  49     private static void init()
  50     {
  51         // Try to invoke the realSync() on the EDT
  52         try {
  53             EventQueue.invokeAndWait(new Runnable() {
  54                 public void run() {
  55                     Frame frame = new Frame("Test frame");
  56 
  57                     frame.setSize(100, 100);
  58                     frame.setVisible(true);
  59 
  60                     try {
  61                         ((sun.awt.SunToolkit)java.awt.Toolkit.getDefaultToolkit()).realSync();
  62                     } catch (Exception e) {
  63                         exceptionCaught = e;
  64                     }
  65                 }
  66             });
  67         } catch (InterruptedException e) {
  68             e.printStackTrace();
  69             fail("Unexpected exception caught: " + e);
  70             return;
  71         } catch (java.lang.reflect.InvocationTargetException e) {
  72             e.printStackTrace();
  73             fail("Unexpected exception caught: " + e);
  74             return;
  75         }
  76 
  77         // We expect the IllegalThreadException to be thrown.
  78         if (exceptionCaught == null) {
  79             fail("No exception was thrown by the realSync() method.");
  80             return;
  81         } else if (!exceptionCaught.getClass().getName().equals("sun.awt.SunToolkit$IllegalThreadException")) {
  82             exceptionCaught.printStackTrace();
  83             fail("Unexpected exception caught while invoking the realSync(): " + exceptionCaught);
  84             return;
  85         }
  86         pass();
  87 
  88     }//End  init()
  89 
  90 
  91 
  92     /*****************************************************
  93      * Standard Test Machinery Section
  94      * DO NOT modify anything in this section -- it's a
  95      * standard chunk of code which has all of the
  96      * synchronisation necessary for the test harness.
  97      * By keeping it the same in all tests, it is easier
  98      * to read and understand someone else's test, as
  99      * well as insuring that all tests behave correctly
 100      * with the test harness.
 101      * There is a section following this for test-
 102      * classes
 103      ******************************************************/
 104     private static boolean theTestPassed = false;
 105     private static boolean testGeneratedInterrupt = false;
 106     private static String failureMessage = "";
 107 
 108     private static Thread mainThread = null;
 109 
 110     private static int sleepTime = 300000;
 111 
 112     // Not sure about what happens if multiple of this test are
 113     //  instantiated in the same VM.  Being static (and using
 114     //  static vars), it aint gonna work.  Not worrying about
 115     //  it for now.
 116     public static void main( String args[] ) throws InterruptedException
 117     {
 118         mainThread = Thread.currentThread();
 119         try
 120         {
 121             init();
 122         }
 123         catch( TestPassedException e )
 124         {
 125             //The test passed, so just return from main and harness will
 126             // interepret this return as a pass
 127             return;
 128         }
 129         //At this point, neither test pass nor test fail has been
 130         // called -- either would have thrown an exception and ended the
 131         // test, so we know we have multiple threads.
 132 
 133         //Test involves other threads, so sleep and wait for them to
 134         // called pass() or fail()
 135         try
 136         {
 137             Thread.sleep( sleepTime );
 138             //Timed out, so fail the test
 139             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 140         }
 141         catch (InterruptedException e)
 142         {
 143             //The test harness may have interrupted the test.  If so, rethrow the exception
 144             // so that the harness gets it and deals with it.
 145             if( ! testGeneratedInterrupt ) throw e;
 146 
 147             //reset flag in case hit this code more than once for some reason (just safety)
 148             testGeneratedInterrupt = false;
 149 
 150             if ( theTestPassed == false )
 151             {
 152                 throw new RuntimeException( failureMessage );
 153             }
 154         }
 155 
 156     }//main
 157 
 158     public static synchronized void setTimeoutTo( int seconds )
 159     {
 160         sleepTime = seconds * 1000;
 161     }
 162 
 163     public static synchronized void pass()
 164     {
 165         System.out.println( "The test passed." );
 166         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 167         //first check if this is executing in main thread
 168         if ( mainThread == Thread.currentThread() )
 169         {
 170             //Still in the main thread, so set the flag just for kicks,
 171             // and throw a test passed exception which will be caught
 172             // and end the test.
 173             theTestPassed = true;
 174             throw new TestPassedException();
 175         }
 176         theTestPassed = true;
 177         testGeneratedInterrupt = true;
 178         mainThread.interrupt();
 179     }//pass()
 180 
 181     public static synchronized void fail()
 182     {
 183         //test writer didn't specify why test failed, so give generic
 184         fail( "it just plain failed! :-)" );
 185     }
 186 
 187     public static synchronized void fail( String whyFailed )
 188     {
 189         System.out.println( "The test failed: " + whyFailed );
 190         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 191         //check if this called from main thread
 192         if ( mainThread == Thread.currentThread() )
 193         {
 194             //If main thread, fail now 'cause not sleeping
 195             throw new RuntimeException( whyFailed );
 196         }
 197         theTestPassed = false;
 198         testGeneratedInterrupt = true;
 199         failureMessage = whyFailed;
 200         mainThread.interrupt();
 201     }//fail()
 202 
 203 }// class RealSyncOnEDT
 204 
 205 //This exception is used to exit from any level of call nesting
 206 // when it's determined that the test has passed, and immediately
 207 // end the test.
 208 class TestPassedException extends RuntimeException
 209 {
 210 }