1 /*
   2  * Copyright (c) 2007, 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  */
  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 
  49 import java.awt.*;
  50 import java.awt.event.*;
  51 import java.awt.geom.*;
  52 import java.awt.print.*;
  53 
  54 // This test is a "main" test as applets would need Runtime permission
  55 // "queuePrintJob".
  56 
  57 public class NullPaper {
  58 
  59    private static void init()
  60     {
  61     boolean settingNullWorked = false;
  62 
  63     try {
  64         /* Setting the paper to null should throw an exception.
  65          * The bug was the exception was not being thrown.
  66          */
  67         new PageFormat().setPaper(null);
  68         settingNullWorked = true;
  69 
  70     /* If the test succeeds we'll end up here, so write
  71      * to standard out.
  72      */
  73     } catch (NullPointerException e) {
  74         pass();
  75 
  76     /* The test failed if we end up here because an exception
  77      * other than the one we were expecting was thrown.
  78      */
  79     } catch (Exception e) {
  80         fail("Instead of the expected NullPointerException, '" + e + "' was thrown.");
  81     }
  82 
  83     if (settingNullWorked) {
  84         fail("The expected NullPointerException was not thrown");
  85     }
  86 
  87     }//End  init()
  88 
  89 
  90    /*****************************************************
  91      Standard Test Machinery Section
  92       DO NOT modify anything in this section -- it's a
  93       standard chunk of code which has all of the
  94       synchronisation necessary for the test harness.
  95       By keeping it the same in all tests, it is easier
  96       to read and understand someone else's test, as
  97       well as insuring that all tests behave correctly
  98       with the test harness.
  99      There is a section following this for test-defined
 100       classes
 101    ******************************************************/
 102    private static boolean theTestPassed = false;
 103    private static boolean testGeneratedInterrupt = false;
 104    private static String failureMessage = "";
 105 
 106    private static Thread mainThread = null;
 107 
 108    private static int sleepTime = 300000;
 109 
 110    public static void main( String args[] ) throws InterruptedException
 111     {
 112       mainThread = Thread.currentThread();
 113       try
 114        {
 115          init();
 116        }
 117       catch( TestPassedException e )
 118        {
 119          //The test passed, so just return from main and harness will
 120          // interepret this return as a pass
 121          return;
 122        }
 123       //At this point, neither test passed nor test failed has been
 124       // called -- either would have thrown an exception and ended the
 125       // test, so we know we have multiple threads.
 126 
 127       //Test involves other threads, so sleep and wait for them to
 128       // called pass() or fail()
 129       try
 130        {
 131          Thread.sleep( sleepTime );
 132          //Timed out, so fail the test
 133          throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 134        }
 135       catch (InterruptedException e)
 136        {
 137          if( ! testGeneratedInterrupt ) throw e;
 138 
 139          //reset flag in case hit this code more than once for some reason (just safety)
 140          testGeneratedInterrupt = false;
 141          if ( theTestPassed == false )
 142           {
 143             throw new RuntimeException( failureMessage );
 144           }
 145        }
 146 
 147     }//main
 148 
 149    public static synchronized void setTimeoutTo( int seconds )
 150     {
 151       sleepTime = seconds * 1000;
 152     }
 153 
 154    public static synchronized void pass()
 155     {
 156       System.out.println( "The test passed." );
 157       System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 158       //first check if this is executing in main thread
 159       if ( mainThread == Thread.currentThread() )
 160        {
 161          //Still in the main thread, so set the flag just for kicks,
 162          // and throw a test passed exception which will be caught
 163          // and end the test.
 164          theTestPassed = true;
 165          throw new TestPassedException();
 166        }
 167       //pass was called from a different thread, so set the flag and interrupt
 168       // the main thead.
 169       theTestPassed = true;
 170       testGeneratedInterrupt = true;
 171       mainThread.interrupt();
 172     }//pass()
 173 
 174    public static synchronized void fail()
 175     {
 176       //test writer didn't specify why test failed, so give generic
 177       fail( "it just plain failed! :-)" );
 178     }
 179 
 180    public static synchronized void fail( String whyFailed )
 181     {
 182       System.out.println( "The test failed: " + whyFailed );
 183       System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 184       //check if this called from main thread
 185       if ( mainThread == Thread.currentThread() )
 186        {
 187          //If main thread, fail now 'cause not sleeping
 188          throw new RuntimeException( whyFailed );
 189        }
 190       theTestPassed = false;
 191       testGeneratedInterrupt = true;
 192       failureMessage = whyFailed;
 193       mainThread.interrupt();
 194     }//fail()
 195 
 196  }// class NullPaper
 197 
 198 //This exception is used to exit from any level of call nesting
 199 // when it's determined that the test has passed, and immediately
 200 // end the test.
 201 class TestPassedException extends RuntimeException
 202  {
 203  }