|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 | +/* @test |
| 25 | + @bug 8262731 |
| 26 | + @key headful printer |
| 27 | + @summary Verify that "PrinterJob.print" throws the expected exception, |
| 28 | + if "Printable.print" throws an exception. |
| 29 | + @run main ExceptionFromPrintableIsIgnoredTest MAIN PE |
| 30 | + @run main ExceptionFromPrintableIsIgnoredTest MAIN RE |
| 31 | + @run main ExceptionFromPrintableIsIgnoredTest EDT PE |
| 32 | + @run main ExceptionFromPrintableIsIgnoredTest EDT RE |
| 33 | + */ |
| 34 | + |
| 35 | +import java.awt.Graphics; |
| 36 | +import java.awt.print.PageFormat; |
| 37 | +import java.awt.print.Printable; |
| 38 | +import java.awt.print.PrinterException; |
| 39 | +import java.awt.print.PrinterJob; |
| 40 | +import java.lang.reflect.InvocationTargetException; |
| 41 | +import javax.swing.SwingUtilities; |
| 42 | + |
| 43 | +public class ExceptionFromPrintableIsIgnoredTest { |
| 44 | + private enum TestThreadType {MAIN, EDT} |
| 45 | + private enum TestExceptionType {PE, RE} |
| 46 | + |
| 47 | + private volatile Throwable printError; |
| 48 | + |
| 49 | + public static void main(String[] args) { |
| 50 | + if (args.length < 2) { |
| 51 | + throw new RuntimeException("Two arguments are expected:" |
| 52 | + + " test thread type and test exception type."); |
| 53 | + } |
| 54 | + |
| 55 | + new ExceptionFromPrintableIsIgnoredTest( |
| 56 | + TestThreadType.valueOf(args[0]), |
| 57 | + TestExceptionType.valueOf(args[1])); |
| 58 | + } |
| 59 | + |
| 60 | + public ExceptionFromPrintableIsIgnoredTest( |
| 61 | + final TestThreadType threadType, |
| 62 | + final TestExceptionType exceptionType) { |
| 63 | + System.out.println(String.format( |
| 64 | + "Test started. threadType='%s', exceptionType='%s'", |
| 65 | + threadType, exceptionType)); |
| 66 | + |
| 67 | + String osName = System.getProperty("os.name"); |
| 68 | + boolean isOSX = osName.toLowerCase().startsWith("mac"); |
| 69 | + if ((exceptionType == TestExceptionType.RE) && !isOSX) { |
| 70 | + System.out.println( |
| 71 | + "Currently this test scenario can be verified only on macOS."); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + printError = null; |
| 76 | + |
| 77 | + if (threadType == TestThreadType.MAIN) { |
| 78 | + runTest(exceptionType); |
| 79 | + } else if (threadType == TestThreadType.EDT) { |
| 80 | + try { |
| 81 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 82 | + @Override |
| 83 | + public void run() { |
| 84 | + runTest(exceptionType); |
| 85 | + } |
| 86 | + }); |
| 87 | + } catch (InterruptedException | InvocationTargetException e) { |
| 88 | + throw new RuntimeException(e); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (printError == null) { |
| 93 | + throw new RuntimeException("No exception was thrown."); |
| 94 | + } else if (!(printError instanceof PrinterException)) { |
| 95 | + throw new RuntimeException("Unexpected exception was thrown."); |
| 96 | + } |
| 97 | + System.out.println("Test passed."); |
| 98 | + } |
| 99 | + |
| 100 | + private void runTest(final TestExceptionType exceptionType) { |
| 101 | + PrinterJob job = PrinterJob.getPrinterJob(); |
| 102 | + if (job.getPrintService() == null) { |
| 103 | + System.out.println("No printers are available."); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + job.setPrintable(new Printable() { |
| 108 | + @Override |
| 109 | + public int print(Graphics graphics, PageFormat pageFormat, |
| 110 | + int pageIndex) throws PrinterException { |
| 111 | + if (pageIndex > 1) { |
| 112 | + return NO_SUCH_PAGE; |
| 113 | + } |
| 114 | + if (exceptionType == TestExceptionType.PE) { |
| 115 | + throw new PrinterException( |
| 116 | + "Exception from 'Printable.print'."); |
| 117 | + } else if (exceptionType == TestExceptionType.RE) { |
| 118 | + throw new RuntimeException( |
| 119 | + "Exception from 'Printable.print'."); |
| 120 | + } |
| 121 | + return PAGE_EXISTS; |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + try { |
| 126 | + job.print(); |
| 127 | + } catch (Throwable t) { |
| 128 | + printError = t; |
| 129 | + |
| 130 | + System.out.println("'PrinterJob.print' threw the exception:"); |
| 131 | + t.printStackTrace(System.out); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments