changeset 3993:ddf1f385bf4b

7023011: Toolkit.getPrintJob(Frame,String,Properties) throws HE instead of specified NPE Reviewed-by: dcherepanov, art
author dav
date Mon, 28 Mar 2011 15:25:24 +0400
parents 9ed3ec97315c
children 44c31bdf4d72
files src/share/classes/java/awt/Toolkit.java src/share/classes/sun/awt/HeadlessToolkit.java src/solaris/classes/sun/awt/X11/XToolkit.java src/windows/classes/sun/awt/windows/WToolkit.java test/java/awt/Toolkit/Headless/GetPrintJob/GetPrintJob.java test/java/awt/Toolkit/Headless/GetPrintJob/GetPrintJobHeadless.java
diffstat 6 files changed, 112 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/awt/Toolkit.java	Fri Mar 18 15:44:45 2011 -0700
+++ b/src/share/classes/java/awt/Toolkit.java	Mon Mar 28 15:25:24 2011 +0400
@@ -1157,12 +1157,9 @@
      *          takes JobAttributes and PageAttributes objects. This object
      *          may be updated to reflect the user's job choices on exit. May
      *          be null.
-     *
      * @return  a <code>PrintJob</code> object, or <code>null</code> if the
      *          user cancelled the print job.
-     * @throws  NullPointerException if frame is null.  This exception is
-     *          always thrown when GraphicsEnvironment.isHeadless() returns
-     *          true.
+     * @throws  NullPointerException if frame is null
      * @throws  SecurityException if this thread is not allowed to initiate a
      *          print job request
      * @see     java.awt.GraphicsEnvironment#isHeadless
@@ -1201,12 +1198,9 @@
      *          job. The attributes will be updated to reflect the user's
      *          choices as outlined in the PageAttributes documentation. May be
      *          null.
-     *
      * @return  a <code>PrintJob</code> object, or <code>null</code> if the
      *          user cancelled the print job.
-     * @throws  NullPointerException if frame is null and either jobAttributes
-     *          is null or jobAttributes.getDialog() returns
-     *          JobAttributes.DialogType.NATIVE.
+     * @throws  NullPointerException if frame is null
      * @throws  IllegalArgumentException if pageAttributes specifies differing
      *          cross feed and feed resolutions. Also if this thread has
      *          access to the file system and jobAttributes specifies
@@ -1218,9 +1212,6 @@
      *          opportunity to select a file and proceed with printing.
      *          The dialog will ensure that the selected output file
      *          is valid before returning from this method.
-     *          <p>
-     *          This exception is always thrown when GraphicsEnvironment.isHeadless()
-     *          returns true.
      * @throws  SecurityException if this thread is not allowed to initiate a
      *          print job request, or if jobAttributes specifies print to file,
      *          and this thread is not allowed to access the file system
@@ -1236,10 +1227,6 @@
                                 PageAttributes pageAttributes) {
         // Override to add printing support with new job/page control classes
 
-        if (GraphicsEnvironment.isHeadless()) {
-            throw new IllegalArgumentException();
-        }
-
         if (this != Toolkit.getDefaultToolkit()) {
             return Toolkit.getDefaultToolkit().getPrintJob(frame, jobtitle,
                                                            jobAttributes,
--- a/src/share/classes/sun/awt/HeadlessToolkit.java	Fri Mar 18 15:44:45 2011 -0700
+++ b/src/share/classes/sun/awt/HeadlessToolkit.java	Mon Mar 28 15:25:24 2011 +0400
@@ -320,8 +320,7 @@
             // Should never happen
             throw new HeadlessException();
         }
-        throw new IllegalArgumentException(
-                "PrintJob not supported in a headless environment");
+        throw new NullPointerException("frame must not be null");
     }
 
     public PrintJob getPrintJob(Frame frame, String doctitle, Properties props)
@@ -330,8 +329,7 @@
             // Should never happen
             throw new HeadlessException();
         }
-        throw new IllegalArgumentException(
-                "PrintJob not supported in a headless environment");
+        throw new NullPointerException("frame must not be null");
     }
 
     /*
--- a/src/solaris/classes/sun/awt/X11/XToolkit.java	Fri Mar 18 15:44:45 2011 -0700
+++ b/src/solaris/classes/sun/awt/X11/XToolkit.java	Mon Mar 28 15:25:24 2011 +0400
@@ -1222,8 +1222,8 @@
     public PrintJob getPrintJob(final Frame frame, final String doctitle,
                                 final Properties props) {
 
-        if (GraphicsEnvironment.isHeadless()) {
-            throw new IllegalArgumentException();
+        if (frame == null) {
+            throw new NullPointerException("frame must not be null");
         }
 
         PrintJob2D printJob = new PrintJob2D(frame, doctitle, props);
@@ -1236,11 +1236,10 @@
 
     public PrintJob getPrintJob(final Frame frame, final String doctitle,
                 final JobAttributes jobAttributes,
-                final PageAttributes pageAttributes) {
-
-
-        if (GraphicsEnvironment.isHeadless()) {
-            throw new IllegalArgumentException();
+                final PageAttributes pageAttributes)
+    {
+        if (frame == null) {
+            throw new NullPointerException("frame must not be null");
         }
 
         PrintJob2D printJob = new PrintJob2D(frame, doctitle,
--- a/src/windows/classes/sun/awt/windows/WToolkit.java	Fri Mar 18 15:44:45 2011 -0700
+++ b/src/windows/classes/sun/awt/windows/WToolkit.java	Mon Mar 28 15:25:24 2011 +0400
@@ -630,10 +630,10 @@
 
     public PrintJob getPrintJob(Frame frame, String doctitle,
                                 JobAttributes jobAttributes,
-                                PageAttributes pageAttributes) {
-
-        if (GraphicsEnvironment.isHeadless()) {
-            throw new IllegalArgumentException();
+                                PageAttributes pageAttributes)
+    {
+        if (frame == null) {
+            throw new NullPointerException("frame must not be null");
         }
 
         PrintJob2D printJob = new PrintJob2D(frame, doctitle,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/awt/Toolkit/Headless/GetPrintJob/GetPrintJob.java	Mon Mar 28 15:25:24 2011 +0400
@@ -0,0 +1,48 @@
+/*
+@test
+@bug 7023011
+@library ../../../regtesthelpers
+@build Sysout
+@summary Toolkit.getPrintJob() throws wrong exceptions
+@author andrei dmitriev: area=awt.headless
+@run main GetPrintJob
+ */
+
+import java.awt.*;
+import java.util.Properties;
+import test.java.awt.regtesthelpers.Sysout;
+/*
+ * In headfull mode we should always getting NPE on the getPrintJob() call if frame == null.
+ */
+
+public class GetPrintJob {
+
+    public static void main(String[] s) {
+        boolean stage1Passed = false;
+        boolean stage2Passed = false;
+
+        try {
+            Toolkit.getDefaultToolkit().getPrintJob(
+                    (Frame) null, "title", new Properties());
+        } catch (NullPointerException e) {
+            stage1Passed = true;
+            Sysout.println("Stage 1 passed. getPrintJob(null, String, property) has thrown NPE.");
+        }
+        if (!stage1Passed) {
+            throw new RuntimeException("getPrintJob() should have thrown NPE but didn't.");
+        }
+
+        try {
+            Toolkit.getDefaultToolkit().getPrintJob(
+                    (Frame) null, "title", new JobAttributes(), new PageAttributes());
+        } catch (NullPointerException e) {
+            stage2Passed = true;
+            Sysout.println("Stage 2 passed. getPrintJob(null, String, jobAttrs, pageAttr) has thrown NPE.");
+        }
+        if (!stage2Passed) {
+            throw new RuntimeException("getPrintJob() should have thrown NPE but didn't.");
+        }
+
+        Sysout.println("Test PASSED");
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/awt/Toolkit/Headless/GetPrintJob/GetPrintJobHeadless.java	Mon Mar 28 15:25:24 2011 +0400
@@ -0,0 +1,50 @@
+/*
+@test
+@bug 7023011
+@library ../../../regtesthelpers
+@build Sysout
+@summary Toolkit.getPrintJob() throws wrong exceptions
+@author andrei dmitriev: area=awt.headless
+@run main/othervm -Djava.awt.headless=true GetPrintJobHeadless
+ */
+
+/*
+ * In headless mode we should always getting NPE on the getPrintJob() call
+ */
+import java.awt.*;
+import java.util.Properties;
+import test.java.awt.regtesthelpers.Sysout;
+
+public class GetPrintJobHeadless {
+
+    public static void main(String[] s) {
+        boolean stage1Passed = false;
+        boolean stage2Passed = false;
+
+        try {
+            Toolkit.getDefaultToolkit().getPrintJob(
+                    (Frame) null, "title", new Properties());
+        } catch (NullPointerException e) {
+            stage1Passed = true;
+            e.printStackTrace();
+            Sysout.println("Stage 1 passed. getPrintJob(null, String, property) has thrown NPE.");
+        }
+        if (!stage1Passed) {
+            throw new RuntimeException("getPrintJob() should have thrown NPE but didn't.");
+        }
+
+        try {
+            Toolkit.getDefaultToolkit().getPrintJob(
+                    (Frame) null, "title", new JobAttributes(), new PageAttributes());
+        } catch (NullPointerException e) {
+            stage2Passed = true;
+            e.printStackTrace();
+            Sysout.println("Stage 2 passed. getPrintJob(null, String, jobAttrs, pageAttr) has thrown  NPE.");
+        }
+        if (!stage2Passed) {
+            throw new RuntimeException("getPrintJob() should have thrown NPE but didn't.");
+        }
+
+        Sysout.println("Test PASSED");
+    }
+}