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