RealSyncOnEDT.java revision 14851:980da45565c8
1/* 2 * Copyright (c) 2007, 2016, 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 41import java.awt.*; 42import java.awt.event.*; 43 44 45public class RealSyncOnEDT 46{ 47 static Exception exceptionCaught = null; 48 49 private static void init() 50 { 51 String[] instructions = 52 { 53 "This is an AUTOMATIC test, simply wait until it is done.", 54 "The result (passed or failed) will be shown in the", 55 "message window below." 56 }; 57 Sysout.createDialog( ); 58 Sysout.printInstructions( instructions ); 59 60 61 // Try to invoke the realSync() on the EDT 62 try { 63 EventQueue.invokeAndWait(new Runnable() { 64 public void run() { 65 Frame frame = new Frame("Test frame"); 66 67 frame.setSize(100, 100); 68 frame.setVisible(true); 69 70 try { 71 ((sun.awt.SunToolkit)java.awt.Toolkit.getDefaultToolkit()).realSync(); 72 } catch (Exception e) { 73 exceptionCaught = e; 74 } 75 } 76 }); 77 } catch (InterruptedException e) { 78 e.printStackTrace(); 79 fail("Unexpected exception caught: " + e); 80 return; 81 } catch (java.lang.reflect.InvocationTargetException e) { 82 e.printStackTrace(); 83 fail("Unexpected exception caught: " + e); 84 return; 85 } 86 87 // We expect the IllegalThreadException to be thrown. 88 if (exceptionCaught == null) { 89 fail("No exception was thrown by the realSync() method."); 90 return; 91 } else if (!exceptionCaught.getClass().getName().equals("sun.awt.SunToolkit$IllegalThreadException")) { 92 exceptionCaught.printStackTrace(); 93 fail("Unexpected exception caught while invoking the realSync(): " + exceptionCaught); 94 return; 95 } 96 pass(); 97 98 }//End init() 99 100 101 102 /***************************************************** 103 * Standard Test Machinery Section 104 * DO NOT modify anything in this section -- it's a 105 * standard chunk of code which has all of the 106 * synchronisation necessary for the test harness. 107 * By keeping it the same in all tests, it is easier 108 * to read and understand someone else's test, as 109 * well as insuring that all tests behave correctly 110 * with the test harness. 111 * There is a section following this for test- 112 * classes 113 ******************************************************/ 114 private static boolean theTestPassed = false; 115 private static boolean testGeneratedInterrupt = false; 116 private static String failureMessage = ""; 117 118 private static Thread mainThread = null; 119 120 private static int sleepTime = 300000; 121 122 // Not sure about what happens if multiple of this test are 123 // instantiated in the same VM. Being static (and using 124 // static vars), it aint gonna work. Not worrying about 125 // it for now. 126 public static void main( String args[] ) throws InterruptedException 127 { 128 mainThread = Thread.currentThread(); 129 try 130 { 131 init(); 132 } 133 catch( TestPassedException e ) 134 { 135 //The test passed, so just return from main and harness will 136 // interepret this return as a pass 137 return; 138 } 139 //At this point, neither test pass nor test fail has been 140 // called -- either would have thrown an exception and ended the 141 // test, so we know we have multiple threads. 142 143 //Test involves other threads, so sleep and wait for them to 144 // called pass() or fail() 145 try 146 { 147 Thread.sleep( sleepTime ); 148 //Timed out, so fail the test 149 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); 150 } 151 catch (InterruptedException e) 152 { 153 //The test harness may have interrupted the test. If so, rethrow the exception 154 // so that the harness gets it and deals with it. 155 if( ! testGeneratedInterrupt ) throw e; 156 157 //reset flag in case hit this code more than once for some reason (just safety) 158 testGeneratedInterrupt = false; 159 160 if ( theTestPassed == false ) 161 { 162 throw new RuntimeException( failureMessage ); 163 } 164 } 165 166 }//main 167 168 public static synchronized void setTimeoutTo( int seconds ) 169 { 170 sleepTime = seconds * 1000; 171 } 172 173 public static synchronized void pass() 174 { 175 Sysout.println( "The test passed." ); 176 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 177 //first check if this is executing in main thread 178 if ( mainThread == Thread.currentThread() ) 179 { 180 //Still in the main thread, so set the flag just for kicks, 181 // and throw a test passed exception which will be caught 182 // and end the test. 183 theTestPassed = true; 184 throw new TestPassedException(); 185 } 186 theTestPassed = true; 187 testGeneratedInterrupt = true; 188 mainThread.interrupt(); 189 }//pass() 190 191 public static synchronized void fail() 192 { 193 //test writer didn't specify why test failed, so give generic 194 fail( "it just plain failed! :-)" ); 195 } 196 197 public static synchronized void fail( String whyFailed ) 198 { 199 Sysout.println( "The test failed: " + whyFailed ); 200 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 201 //check if this called from main thread 202 if ( mainThread == Thread.currentThread() ) 203 { 204 //If main thread, fail now 'cause not sleeping 205 throw new RuntimeException( whyFailed ); 206 } 207 theTestPassed = false; 208 testGeneratedInterrupt = true; 209 failureMessage = whyFailed; 210 mainThread.interrupt(); 211 }//fail() 212 213}// class RealSyncOnEDT 214 215//This exception is used to exit from any level of call nesting 216// when it's determined that the test has passed, and immediately 217// end the test. 218class TestPassedException extends RuntimeException 219{ 220} 221 222//*********** End Standard Test Machinery Section ********** 223 224 225//************ Begin classes defined for the test **************** 226 227// if want to make listeners, here is the recommended place for them, then instantiate 228// them in init() 229 230/* Example of a class which may be written as part of a test 231class NewClass implements anInterface 232 { 233 static int newVar = 0; 234 235 public void eventDispatched(AWTEvent e) 236 { 237 //Counting events to see if we get enough 238 eventCount++; 239 240 if( eventCount == 20 ) 241 { 242 //got enough events, so pass 243 244 RealSyncOnEDT.pass(); 245 } 246 else if( tries == 20 ) 247 { 248 //tried too many times without getting enough events so fail 249 250 RealSyncOnEDT.fail(); 251 } 252 253 }// eventDispatched() 254 255 }// NewClass class 256 257*/ 258 259 260//************** End classes defined for the test ******************* 261 262 263 264 265/**************************************************** 266 Standard Test Machinery 267 DO NOT modify anything below -- it's a standard 268 chunk of code whose purpose is to make user 269 interaction uniform, and thereby make it simpler 270 to read and understand someone else's test. 271 ****************************************************/ 272 273/** 274 This is part of the standard test machinery. 275 It creates a dialog (with the instructions), and is the interface 276 for sending text messages to the user. 277 To print the instructions, send an array of strings to Sysout.createDialog 278 WithInstructions method. Put one line of instructions per array entry. 279 To display a message for the tester to see, simply call Sysout.println 280 with the string to be displayed. 281 This mimics System.out.println but works within the test harness as well 282 as standalone. 283 */ 284 285class Sysout 286{ 287 private static TestDialog dialog; 288 289 public static void createDialogWithInstructions( String[] instructions ) 290 { 291 dialog = new TestDialog( new Frame(), "Instructions" ); 292 dialog.printInstructions( instructions ); 293 dialog.setVisible(true); 294 println( "Any messages for the tester will display here." ); 295 } 296 297 public static void createDialog( ) 298 { 299 dialog = new TestDialog( new Frame(), "Instructions" ); 300 String[] defInstr = { "Instructions will appear here. ", "" } ; 301 dialog.printInstructions( defInstr ); 302 dialog.setVisible(true); 303 println( "Any messages for the tester will display here." ); 304 } 305 306 307 public static void printInstructions( String[] instructions ) 308 { 309 dialog.printInstructions( instructions ); 310 } 311 312 313 public static void println( String messageIn ) 314 { 315 dialog.displayMessage( messageIn ); 316 System.out.println(messageIn); 317 } 318 319}// Sysout class 320 321/** 322 This is part of the standard test machinery. It provides a place for the 323 test instructions to be displayed, and a place for interactive messages 324 to the user to be displayed. 325 To have the test instructions displayed, see Sysout. 326 To have a message to the user be displayed, see Sysout. 327 Do not call anything in this dialog directly. 328 */ 329class TestDialog extends Dialog 330{ 331 332 TextArea instructionsText; 333 TextArea messageText; 334 int maxStringLength = 80; 335 336 //DO NOT call this directly, go through Sysout 337 public TestDialog( Frame frame, String name ) 338 { 339 super( frame, name ); 340 int scrollBoth = TextArea.SCROLLBARS_BOTH; 341 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 342 add( "North", instructionsText ); 343 344 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 345 add("Center", messageText); 346 347 pack(); 348 349 setVisible(true); 350 }// TestDialog() 351 352 //DO NOT call this directly, go through Sysout 353 public void printInstructions( String[] instructions ) 354 { 355 //Clear out any current instructions 356 instructionsText.setText( "" ); 357 358 //Go down array of instruction strings 359 360 String printStr, remainingStr; 361 for( int i=0; i < instructions.length; i++ ) 362 { 363 //chop up each into pieces maxSringLength long 364 remainingStr = instructions[ i ]; 365 while( remainingStr.length() > 0 ) 366 { 367 //if longer than max then chop off first max chars to print 368 if( remainingStr.length() >= maxStringLength ) 369 { 370 //Try to chop on a word boundary 371 int posOfSpace = remainingStr. 372 lastIndexOf( ' ', maxStringLength - 1 ); 373 374 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 375 376 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 377 remainingStr = remainingStr.substring( posOfSpace + 1 ); 378 } 379 //else just print 380 else 381 { 382 printStr = remainingStr; 383 remainingStr = ""; 384 } 385 386 instructionsText.append( printStr + "\n" ); 387 388 }// while 389 390 }// for 391 392 }//printInstructions() 393 394 //DO NOT call this directly, go through Sysout 395 public void displayMessage( String messageIn ) 396 { 397 messageText.append( messageIn + "\n" ); 398 System.out.println(messageIn); 399 } 400 401}// TestDialog class 402