Skip to content

Commit 0df4bd4

Browse files
committed
8381208: Init cause with the caught runtime exception
Reviewed-by: dmarkov, serb, azvegint
1 parent 3384c67 commit 0df4bd4

2 files changed

Lines changed: 43 additions & 16 deletions

File tree

src/java.desktop/share/classes/sun/print/RasterPrinterJob.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,23 @@
3333
import java.awt.KeyboardFocusManager;
3434
import java.awt.Rectangle;
3535
import java.awt.Shape;
36+
import java.awt.Window;
3637
import java.awt.geom.AffineTransform;
3738
import java.awt.geom.Point2D;
3839
import java.awt.geom.Rectangle2D;
3940
import java.awt.image.BufferedImage;
4041
import java.awt.print.Book;
41-
import java.awt.print.Pageable;
4242
import java.awt.print.PageFormat;
43+
import java.awt.print.Pageable;
4344
import java.awt.print.Paper;
4445
import java.awt.print.Printable;
4546
import java.awt.print.PrinterAbortException;
4647
import java.awt.print.PrinterException;
4748
import java.awt.print.PrinterJob;
48-
import java.awt.Window;
4949
import java.io.File;
5050
import java.io.IOException;
5151
import java.util.ArrayList;
5252
import java.util.Locale;
53-
import sun.awt.image.ByteInterleavedRaster;
5453

5554
import javax.print.Doc;
5655
import javax.print.DocFlavor;
@@ -69,8 +68,8 @@
6968
import javax.print.attribute.Size2DSyntax;
7069
import javax.print.attribute.standard.Copies;
7170
import javax.print.attribute.standard.Destination;
72-
import javax.print.attribute.standard.DialogTypeSelection;
7371
import javax.print.attribute.standard.DialogOwner;
72+
import javax.print.attribute.standard.DialogTypeSelection;
7473
import javax.print.attribute.standard.Fidelity;
7574
import javax.print.attribute.standard.JobName;
7675
import javax.print.attribute.standard.JobSheets;
@@ -81,15 +80,17 @@
8180
import javax.print.attribute.standard.OrientationRequested;
8281
import javax.print.attribute.standard.OutputBin;
8382
import javax.print.attribute.standard.PageRanges;
83+
import javax.print.attribute.standard.PrinterIsAcceptingJobs;
8484
import javax.print.attribute.standard.PrinterResolution;
8585
import javax.print.attribute.standard.PrinterState;
8686
import javax.print.attribute.standard.PrinterStateReason;
8787
import javax.print.attribute.standard.PrinterStateReasons;
88-
import javax.print.attribute.standard.PrinterIsAcceptingJobs;
8988
import javax.print.attribute.standard.RequestingUserName;
9089
import javax.print.attribute.standard.SheetCollate;
9190
import javax.print.attribute.standard.Sides;
9291

92+
import sun.awt.image.ByteInterleavedRaster;
93+
9394
import static sun.font.FontUtilities.isIgnorableWhitespace;
9495

9596
/**
@@ -1613,8 +1614,7 @@ public void print(PrintRequestAttributeSet attributes)
16131614
} catch (PrinterException pe) {
16141615
throw pe;
16151616
} catch (Throwable printError) {
1616-
throw (PrinterException)
1617-
new PrinterException().initCause(printError.getCause());
1617+
throw (PrinterException) new PrinterException().initCause(printError);
16181618
} finally {
16191619
// reset previousPaper in case this job is invoked again.
16201620
previousPaper = null;

test/jdk/java/awt/print/PrinterJob/ExceptionFromPrintableIsIgnoredTest.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
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.
@@ -38,7 +38,12 @@
3838
import java.awt.print.Printable;
3939
import java.awt.print.PrinterException;
4040
import java.awt.print.PrinterJob;
41+
import java.io.File;
4142
import java.lang.reflect.InvocationTargetException;
43+
44+
import javax.print.attribute.HashPrintRequestAttributeSet;
45+
import javax.print.attribute.PrintRequestAttributeSet;
46+
import javax.print.attribute.standard.Destination;
4247
import javax.swing.SwingUtilities;
4348

4449
public 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

Comments
 (0)