2323
2424/*
2525 * @test
26- * @bug 8262731 8268675
26+ * @bug 8262731 8268675 8381208
2727 * @key printer
2828 * @summary Verify that "PrinterJob.print" throws the expected exception,
2929 * if "Printable.print" throws an exception.
3838import java .awt .print .Printable ;
3939import java .awt .print .PrinterException ;
4040import java .awt .print .PrinterJob ;
41+ import java .io .File ;
4142import java .lang .reflect .InvocationTargetException ;
43+
44+ import javax .print .attribute .HashPrintRequestAttributeSet ;
45+ import javax .print .attribute .PrintRequestAttributeSet ;
46+ import javax .print .attribute .standard .Destination ;
4247import javax .swing .SwingUtilities ;
4348
4449public class ExceptionFromPrintableIsIgnoredTest {
@@ -47,7 +52,14 @@ private enum TestExceptionType {PE, RE}
4752
4853 private volatile Throwable printError ;
4954
55+ private volatile PrinterException thrownPE ;
56+ private volatile RuntimeException thrownRE ;
57+
5058 public static void main (String [] args ) {
59+ if (PrinterJob .lookupPrintServices ().length == 0 ) {
60+ throw new RuntimeException ("Printer not configured or available." );
61+ }
62+
5163 if (args .length < 2 ) {
5264 throw new RuntimeException ("Two arguments are expected:"
5365 + " test thread type and test exception type." );
@@ -58,7 +70,7 @@ public static void main(String[] args) {
5870 TestExceptionType .valueOf (args [1 ]));
5971 }
6072
61- public ExceptionFromPrintableIsIgnoredTest (
73+ private ExceptionFromPrintableIsIgnoredTest (
6274 final TestThreadType threadType ,
6375 final TestExceptionType exceptionType ) {
6476 System .out .println (String .format (
@@ -87,15 +99,28 @@ public void run() {
8799 } else if (!(printError instanceof PrinterException )) {
88100 throw new RuntimeException ("Unexpected exception was thrown." );
89101 }
102+
103+ if (exceptionType == TestExceptionType .PE
104+ && thrownPE != printError ) {
105+ throw new RuntimeException (
106+ "Expected the same instance of PrinterException" );
107+ }
108+
109+ if (exceptionType == TestExceptionType .RE
110+ && thrownRE != printError .getCause ()) {
111+ throw new RuntimeException (
112+ "Expected the cause of PrinterException to be the thrown exception" );
113+ }
114+
90115 System .out .println ("Test passed." );
91116 }
92117
93118 private void runTest (final TestExceptionType exceptionType ) {
119+ PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet ();
120+ final File file = new File ("out.prn" );
121+ attrs .add (new Destination (file .toURI ()));
122+
94123 PrinterJob job = PrinterJob .getPrinterJob ();
95- if (job .getPrintService () == null ) {
96- System .out .println ("No printers are available." );
97- return ;
98- }
99124
100125 job .setPrintable (new Printable () {
101126 @ Override
@@ -105,23 +130,25 @@ public int print(Graphics graphics, PageFormat pageFormat,
105130 return NO_SUCH_PAGE ;
106131 }
107132 if (exceptionType == TestExceptionType .PE ) {
108- throw new PrinterException (
133+ throw thrownPE = new PrinterException (
109134 "Exception from 'Printable.print'." );
110135 } else if (exceptionType == TestExceptionType .RE ) {
111- throw new RuntimeException (
136+ throw thrownRE = new RuntimeException (
112137 "Exception from 'Printable.print'." );
113138 }
114139 return PAGE_EXISTS ;
115140 }
116141 });
117142
118143 try {
119- job .print ();
144+ job .print (attrs );
120145 } catch (Throwable t ) {
121146 printError = t ;
122147
123148 System .out .println ("'PrinterJob.print' threw the exception:" );
124149 t .printStackTrace (System .out );
150+ } finally {
151+ file .delete ();
125152 }
126153 }
127154}
0 commit comments