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