81 } 82 } 83 84 private static String readInputStream(InputStream is) throws IOException { 85 byte[] buffer = new byte[4096]; 86 int len = 0; 87 StringBuilder sb = new StringBuilder(); 88 try (InputStreamReader isr = new InputStreamReader(is)) { 89 while ((len = is.read(buffer)) > 0) { 90 sb.append(new String(buffer, 0, len)); 91 } 92 } 93 return sb.toString(); 94 } 95 96 private static void init() throws Exception { 97 //*** Create instructions for the user here *** 98 99 borderShift = frameBorderCounter(); 100 borderShift = Math.abs(borderShift) == 1 ? borderShift : (borderShift / 2); 101 String[] instructions = { 102 "This is an AUTOMATIC test, simply wait until it is done.", 103 "The result (passed or failed) will be shown in the", 104 "message window below." 105 }; 106 SwingUtilities.invokeAndWait(new Runnable() { 107 public void run() { 108 Sysout.createDialog(); 109 Sysout.printInstructions(instructions); 110 111 // prepare controls 112 113 frame = new JFrame(); 114 115 Panel awtPanel = new Panel(); 116 awtPanel.setBackground(Color.GREEN); 117 awtButton = new Button("AWTButton"); 118 awtPanel.add(awtButton); 119 awtButton.setForeground(awtColor); 120 awtButton.setBackground(awtColor); 121 jbutton = new JButton("SwingButton"); 122 awtPanel.add(jbutton); 123 jbutton.setForeground(jbColor); 124 jbutton.setBackground(jbColor); 125 126 JPanel jPanel = new JPanel(); 127 jbutton2 = new JButton("SwingButton2"); 128 jPanel.add(jbutton2); 129 jbutton2.setForeground(jb2Color); 130 jbutton2.setBackground(jb2Color); 256 // so that the harness gets it and deals with it. 257 if (!testGeneratedInterrupt) { 258 throw e; 259 } 260 261 //reset flag in case hit this code more than once for some reason (just safety) 262 testGeneratedInterrupt = false; 263 264 if (theTestPassed == false) { 265 throw new RuntimeException(failureMessage); 266 } 267 } 268 269 }//main 270 271 public static synchronized void setTimeoutTo(int seconds) { 272 sleepTime = seconds * 1000; 273 } 274 275 public static synchronized void pass() { 276 Sysout.println("The test passed."); 277 Sysout.println("The test is over, hit Ctl-C to stop Java VM"); 278 //first check if this is executing in main thread 279 if (mainThread == Thread.currentThread()) { 280 //Still in the main thread, so set the flag just for kicks, 281 // and throw a test passed exception which will be caught 282 // and end the test. 283 theTestPassed = true; 284 throw new TestPassedException(); 285 } 286 theTestPassed = true; 287 testGeneratedInterrupt = true; 288 mainThread.interrupt(); 289 }//pass() 290 291 public static synchronized void fail() { 292 //test writer didn't specify why test failed, so give generic 293 fail("it just plain failed! :-)"); 294 } 295 296 public static synchronized void fail(String whyFailed) { 297 Sysout.println("The test failed: " + whyFailed); 298 Sysout.println("The test is over, hit Ctl-C to stop Java VM"); 299 //check if this called from main thread 300 if (mainThread == Thread.currentThread()) { 301 //If main thread, fail now 'cause not sleeping 302 throw new RuntimeException(whyFailed); 303 } 304 theTestPassed = false; 305 testGeneratedInterrupt = true; 306 failureMessage = whyFailed; 307 mainThread.interrupt(); 308 }//fail() 309 }// class JButtonInGlassPane 310 class TestPassedException extends RuntimeException { 311 } 312 313 //*********** End Standard Test Machinery Section ********** 314 //************ Begin classes defined for the test **************** 315 // if want to make listeners, here is the recommended place for them, then instantiate 316 // them in init() 317 318 /* Example of a class which may be written as part of a test 319 class NewClass implements anInterface 320 { 321 static int newVar = 0; 322 323 public void eventDispatched(AWTEvent e) 324 { 325 //Counting events to see if we get enough 326 eventCount++; 327 328 if( eventCount == 20 ) 329 { 330 //got enough events, so pass 331 332 JButtonInGlassPane.pass(); 333 } 334 else if( tries == 20 ) 335 { 336 //tried too many times without getting enough events so fail 337 338 JButtonInGlassPane.fail(); 339 } 340 341 }// eventDispatched() 342 343 }// NewClass class 344 345 */ 346 //************** End classes defined for the test ******************* 347 /**************************************************** 348 Standard Test Machinery 349 DO NOT modify anything below -- it's a standard 350 chunk of code whose purpose is to make user 351 interaction uniform, and thereby make it simpler 352 to read and understand someone else's test. 353 ****************************************************/ 354 /** 355 This is part of the standard test machinery. 356 It creates a dialog (with the instructions), and is the interface 357 for sending text messages to the user. 358 To print the instructions, send an array of strings to Sysout.createDialog 359 WithInstructions method. Put one line of instructions per array entry. 360 To display a message for the tester to see, simply call Sysout.println 361 with the string to be displayed. 362 This mimics System.out.println but works within the test harness as well 363 as standalone. 364 */ 365 class Sysout { 366 367 private static TestDialog dialog; 368 369 public static void createDialogWithInstructions(String[] instructions) { 370 dialog = new TestDialog(new Frame(), "Instructions"); 371 dialog.printInstructions(instructions); 372 //dialog.setVisible(true); 373 println("Any messages for the tester will display here."); 374 } 375 376 public static void createDialog() { 377 dialog = new TestDialog(new Frame(), "Instructions"); 378 String[] defInstr = {"Instructions will appear here. ", ""}; 379 dialog.printInstructions(defInstr); 380 //dialog.setVisible(true); 381 println("Any messages for the tester will display here."); 382 } 383 384 public static void printInstructions(String[] instructions) { 385 dialog.printInstructions(instructions); 386 } 387 388 public static void println(String messageIn) { 389 dialog.displayMessage(messageIn); 390 System.out.println(messageIn); 391 } 392 }// Sysout class 393 394 /** 395 This is part of the standard test machinery. It provides a place for the 396 test instructions to be displayed, and a place for interactive messages 397 to the user to be displayed. 398 To have the test instructions displayed, see Sysout. 399 To have a message to the user be displayed, see Sysout. 400 Do not call anything in this dialog directly. 401 */ 402 class TestDialog extends Dialog { 403 404 TextArea instructionsText; 405 TextArea messageText; 406 int maxStringLength = 80; 407 408 //DO NOT call this directly, go through Sysout 409 public TestDialog(Frame frame, String name) { 410 super(frame, name); 411 int scrollBoth = TextArea.SCROLLBARS_BOTH; 412 instructionsText = new TextArea("", 15, maxStringLength, scrollBoth); 413 add("North", instructionsText); 414 415 messageText = new TextArea("", 5, maxStringLength, scrollBoth); 416 add("Center", messageText); 417 418 pack(); 419 420 //setVisible(true); 421 }// TestDialog() 422 423 //DO NOT call this directly, go through Sysout 424 public void printInstructions(String[] instructions) { 425 //Clear out any current instructions 426 instructionsText.setText(""); 427 428 //Go down array of instruction strings 429 430 String printStr, remainingStr; 431 for (int i = 0; i < instructions.length; i++) { 432 //chop up each into pieces maxSringLength long 433 remainingStr = instructions[i]; 434 while (remainingStr.length() > 0) { 435 //if longer than max then chop off first max chars to print 436 if (remainingStr.length() >= maxStringLength) { 437 //Try to chop on a word boundary 438 int posOfSpace = remainingStr.lastIndexOf(' ', maxStringLength - 1); 439 440 if (posOfSpace <= 0) { 441 posOfSpace = maxStringLength - 1; 442 } 443 444 printStr = remainingStr.substring(0, posOfSpace + 1); 445 remainingStr = remainingStr.substring(posOfSpace + 1); 446 } //else just print 447 else { 448 printStr = remainingStr; 449 remainingStr = ""; 450 } 451 452 instructionsText.append(printStr + "\n"); 453 454 }// while 455 456 }// for 457 458 }//printInstructions() 459 460 //DO NOT call this directly, go through Sysout 461 public void displayMessage(String messageIn) { 462 messageText.append(messageIn + "\n"); 463 System.out.println(messageIn); 464 } 465 }// TestDialog class 466 | 81 } 82 } 83 84 private static String readInputStream(InputStream is) throws IOException { 85 byte[] buffer = new byte[4096]; 86 int len = 0; 87 StringBuilder sb = new StringBuilder(); 88 try (InputStreamReader isr = new InputStreamReader(is)) { 89 while ((len = is.read(buffer)) > 0) { 90 sb.append(new String(buffer, 0, len)); 91 } 92 } 93 return sb.toString(); 94 } 95 96 private static void init() throws Exception { 97 //*** Create instructions for the user here *** 98 99 borderShift = frameBorderCounter(); 100 borderShift = Math.abs(borderShift) == 1 ? borderShift : (borderShift / 2); 101 SwingUtilities.invokeAndWait(new Runnable() { 102 public void run() { 103 // prepare controls 104 105 frame = new JFrame(); 106 107 Panel awtPanel = new Panel(); 108 awtPanel.setBackground(Color.GREEN); 109 awtButton = new Button("AWTButton"); 110 awtPanel.add(awtButton); 111 awtButton.setForeground(awtColor); 112 awtButton.setBackground(awtColor); 113 jbutton = new JButton("SwingButton"); 114 awtPanel.add(jbutton); 115 jbutton.setForeground(jbColor); 116 jbutton.setBackground(jbColor); 117 118 JPanel jPanel = new JPanel(); 119 jbutton2 = new JButton("SwingButton2"); 120 jPanel.add(jbutton2); 121 jbutton2.setForeground(jb2Color); 122 jbutton2.setBackground(jb2Color); 248 // so that the harness gets it and deals with it. 249 if (!testGeneratedInterrupt) { 250 throw e; 251 } 252 253 //reset flag in case hit this code more than once for some reason (just safety) 254 testGeneratedInterrupt = false; 255 256 if (theTestPassed == false) { 257 throw new RuntimeException(failureMessage); 258 } 259 } 260 261 }//main 262 263 public static synchronized void setTimeoutTo(int seconds) { 264 sleepTime = seconds * 1000; 265 } 266 267 public static synchronized void pass() { 268 System.out.println("The test passed."); 269 System.out.println("The test is over, hit Ctl-C to stop Java VM"); 270 //first check if this is executing in main thread 271 if (mainThread == Thread.currentThread()) { 272 //Still in the main thread, so set the flag just for kicks, 273 // and throw a test passed exception which will be caught 274 // and end the test. 275 theTestPassed = true; 276 throw new TestPassedException(); 277 } 278 theTestPassed = true; 279 testGeneratedInterrupt = true; 280 mainThread.interrupt(); 281 }//pass() 282 283 public static synchronized void fail() { 284 //test writer didn't specify why test failed, so give generic 285 fail("it just plain failed! :-)"); 286 } 287 288 public static synchronized void fail(String whyFailed) { 289 System.out.println("The test failed: " + whyFailed); 290 System.out.println("The test is over, hit Ctl-C to stop Java VM"); 291 //check if this called from main thread 292 if (mainThread == Thread.currentThread()) { 293 //If main thread, fail now 'cause not sleeping 294 throw new RuntimeException(whyFailed); 295 } 296 theTestPassed = false; 297 testGeneratedInterrupt = true; 298 failureMessage = whyFailed; 299 mainThread.interrupt(); 300 }//fail() 301 }// class JButtonInGlassPane 302 class TestPassedException extends RuntimeException { 303 } |