1 /*
2 * Copyright (c) 2014, 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 */
231 if( ! testGeneratedInterrupt ) throw e;
232
233 //reset flag in case hit this code more than once for some reason (just safety)
234 testGeneratedInterrupt = false;
235
236 if ( theTestPassed == false )
237 {
238 throw new RuntimeException( failureMessage );
239 }
240 }
241
242 }//main
243
244 public static synchronized void setTimeoutTo( int seconds )
245 {
246 sleepTime = seconds * 1000;
247 }
248
249 public static synchronized void pass()
250 {
251 Sysout.println( "The test passed." );
252 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
253 //first check if this is executing in main thread
254 if ( mainThread == Thread.currentThread() )
255 {
256 //Still in the main thread, so set the flag just for kicks,
257 // and throw a test passed exception which will be caught
258 // and end the test.
259 theTestPassed = true;
260 throw new TestPassedException();
261 }
262 theTestPassed = true;
263 testGeneratedInterrupt = true;
264 mainThread.interrupt();
265 }//pass()
266
267 public static synchronized void fail()
268 {
269 //test writer didn't specify why test failed, so give generic
270 fail( "it just plain failed! :-)" );
271 }
272
273 public static synchronized void fail( String whyFailed )
274 {
275 Sysout.println( "The test failed: " + whyFailed );
276 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
277 //check if this called from main thread
278 if ( mainThread == Thread.currentThread() )
279 {
280 //If main thread, fail now 'cause not sleeping
281 throw new RuntimeException( whyFailed );
282 }
283 theTestPassed = false;
284 testGeneratedInterrupt = true;
285 failureMessage = whyFailed;
286 mainThread.interrupt();
287 }//fail()
288
289 }// class TestAlwaysOnTopBeforeShow
290
291 //This exception is used to exit from any level of call nesting
292 // when it's determined that the test has passed, and immediately
293 // end the test.
294 class TestPassedException extends RuntimeException
295 {
296 }
297
298 //*********** End Standard Test Machinery Section **********
299
300
301 //************ Begin classes defined for the test ****************
302
303 // if want to make listeners, here is the recommended place for them, then instantiate
304 // them in init()
305
306 /* Example of a class which may be written as part of a test
307 class NewClass implements anInterface
308 {
309 static int newVar = 0;
310
311 public void eventDispatched(AWTEvent e)
312 {
313 //Counting events to see if we get enough
314 eventCount++;
315
316 if( eventCount == 20 )
317 {
318 //got enough events, so pass
319
320 TestAlwaysOnTopBeforeShow.pass();
321 }
322 else if( tries == 20 )
323 {
324 //tried too many times without getting enough events so fail
325
326 TestAlwaysOnTopBeforeShow.fail();
327 }
328
329 }// eventDispatched()
330
331 }// NewClass class
332
333 */
334
335
336 //************** End classes defined for the test *******************
337
338
339
340
341 /****************************************************
342 Standard Test Machinery
343 DO NOT modify anything below -- it's a standard
344 chunk of code whose purpose is to make user
345 interaction uniform, and thereby make it simpler
346 to read and understand someone else's test.
347 ****************************************************/
348
349 /**
350 This is part of the standard test machinery.
351 It creates a dialog (with the instructions), and is the interface
352 for sending text messages to the user.
353 To print the instructions, send an array of strings to Sysout.createDialog
354 WithInstructions method. Put one line of instructions per array entry.
355 To display a message for the tester to see, simply call Sysout.println
356 with the string to be displayed.
357 This mimics System.out.println but works within the test harness as well
358 as standalone.
359 */
360
361 class Sysout
362 {
363 private static TestDialog dialog;
364
365 public static void createDialogWithInstructions( String[] instructions )
366 {
367 dialog = new TestDialog( new Frame(), "Instructions" );
368 dialog.printInstructions( instructions );
369 dialog.setVisible(true);
370 println( "Any messages for the tester will display here." );
371 }
372
373 public static void createDialog( )
374 {
375 dialog = new TestDialog( new Frame(), "Instructions" );
376 String[] defInstr = { "Instructions will appear here. ", "" } ;
377 dialog.printInstructions( defInstr );
378 dialog.setVisible(true);
379 println( "Any messages for the tester will display here." );
380 }
381
382
383 public static void printInstructions( String[] instructions )
384 {
385 dialog.printInstructions( instructions );
386 }
387
388
389 public static void println( String messageIn )
390 {
391 System.out.println(messageIn);
392 }
393
394 }// Sysout class
395
396 /**
397 This is part of the standard test machinery. It provides a place for the
398 test instructions to be displayed, and a place for interactive messages
399 to the user to be displayed.
400 To have the test instructions displayed, see Sysout.
401 To have a message to the user be displayed, see Sysout.
402 Do not call anything in this dialog directly.
403 */
404 class TestDialog extends Dialog
405 {
406
407 TextArea instructionsText;
408 TextArea messageText;
409 int maxStringLength = 80;
410
411 //DO NOT call this directly, go through Sysout
412 public TestDialog( Frame frame, String name )
413 {
414 super( frame, name );
415 int scrollBoth = TextArea.SCROLLBARS_BOTH;
416 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
417 add( "North", instructionsText );
418
419 messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
420 add("Center", messageText);
421
422 pack();
423
424 setVisible(true);
425 }// TestDialog()
426
427 //DO NOT call this directly, go through Sysout
428 public void printInstructions( String[] instructions )
429 {
430 //Clear out any current instructions
431 instructionsText.setText( "" );
432
433 //Go down array of instruction strings
434
435 String printStr, remainingStr;
436 for( int i=0; i < instructions.length; i++ )
437 {
438 //chop up each into pieces maxSringLength long
439 remainingStr = instructions[ i ];
440 while( remainingStr.length() > 0 )
441 {
442 //if longer than max then chop off first max chars to print
443 if( remainingStr.length() >= maxStringLength )
444 {
445 //Try to chop on a word boundary
446 int posOfSpace = remainingStr.
447 lastIndexOf( ' ', maxStringLength - 1 );
448
449 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
450
451 printStr = remainingStr.substring( 0, posOfSpace + 1 );
452 remainingStr = remainingStr.substring( posOfSpace + 1 );
453 }
454 //else just print
455 else
456 {
457 printStr = remainingStr;
458 remainingStr = "";
459 }
460
461 instructionsText.append( printStr + "\n" );
462
463 }// while
464
465 }// for
466
467 }//printInstructions()
468
469 //DO NOT call this directly, go through Sysout
470 public void displayMessage( String messageIn )
471 {
472 messageText.append( messageIn + "\n" );
473 System.out.println(messageIn);
474 }
475
476 }// TestDialog class
|
1 /*
2 * Copyright (c) 2014, 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 */
231 if( ! testGeneratedInterrupt ) throw e;
232
233 //reset flag in case hit this code more than once for some reason (just safety)
234 testGeneratedInterrupt = false;
235
236 if ( theTestPassed == false )
237 {
238 throw new RuntimeException( failureMessage );
239 }
240 }
241
242 }//main
243
244 public static synchronized void setTimeoutTo( int seconds )
245 {
246 sleepTime = seconds * 1000;
247 }
248
249 public static synchronized void pass()
250 {
251 System.out.println( "The test passed." );
252 System.out.println( "The test is over, hit Ctl-C to stop Java VM" );
253 //first check if this is executing in main thread
254 if ( mainThread == Thread.currentThread() )
255 {
256 //Still in the main thread, so set the flag just for kicks,
257 // and throw a test passed exception which will be caught
258 // and end the test.
259 theTestPassed = true;
260 throw new TestPassedException();
261 }
262 theTestPassed = true;
263 testGeneratedInterrupt = true;
264 mainThread.interrupt();
265 }//pass()
266
267 public static synchronized void fail()
268 {
269 //test writer didn't specify why test failed, so give generic
270 fail( "it just plain failed! :-)" );
271 }
272
273 public static synchronized void fail( String whyFailed )
274 {
275 System.out.println( "The test failed: " + whyFailed );
276 System.out.println( "The test is over, hit Ctl-C to stop Java VM" );
277 //check if this called from main thread
278 if ( mainThread == Thread.currentThread() )
279 {
280 //If main thread, fail now 'cause not sleeping
281 throw new RuntimeException( whyFailed );
282 }
283 theTestPassed = false;
284 testGeneratedInterrupt = true;
285 failureMessage = whyFailed;
286 mainThread.interrupt();
287 }//fail()
288
289 }// class TestAlwaysOnTopBeforeShow
290
291 //This exception is used to exit from any level of call nesting
292 // when it's determined that the test has passed, and immediately
293 // end the test.
294 class TestPassedException extends RuntimeException
295 {
296 }
|