|
1 | 1 | /* |
2 | | - * Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2007, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -import java.awt.Button; |
25 | | -import java.awt.Dimension; |
26 | | -import java.awt.Frame; |
27 | 24 | import java.awt.Graphics; |
28 | 25 | import java.awt.Graphics2D; |
29 | | -import java.awt.Panel; |
30 | 26 | import java.awt.print.PageFormat; |
31 | 27 | import java.awt.print.Printable; |
32 | 28 | import java.awt.print.PrinterException; |
33 | 29 | import java.awt.print.PrinterJob; |
| 30 | +import java.io.File; |
34 | 31 | import java.text.AttributedCharacterIterator; |
35 | 32 | import java.text.AttributedString; |
36 | 33 |
|
37 | | -import javax.swing.JOptionPane; |
| 34 | +import javax.print.attribute.HashPrintRequestAttributeSet; |
| 35 | +import javax.print.attribute.PrintRequestAttributeSet; |
| 36 | +import javax.print.attribute.standard.Destination; |
38 | 37 |
|
39 | 38 | /* |
40 | 39 | * @test |
41 | 40 | * @bug 4223328 |
42 | | - * @summary Printer graphics must behave the same as screen graphics |
| 41 | + * @summary Printer graphics must throw expected exceptions |
43 | 42 | * @key printer |
44 | | - * @library /java/awt/regtesthelpers |
45 | | - * @build PassFailJFrame |
46 | | - * @run main/manual PrintNullString |
| 43 | + * @run main PrintNullString |
47 | 44 | */ |
48 | | -public class PrintNullString extends Frame { |
49 | | - private static final String INSTRUCTIONS = |
50 | | - "This test should print a page which contains the same\n" + |
51 | | - "text messages as in the test window on the screen.\n" + |
52 | | - "\n" + |
53 | | - "The messages should contain only 'OK' and 'expected' messages.\n" + |
54 | | - "Press Pass if it's the case; otherwise press Fail.\n" + |
55 | | - "\n" + |
56 | | - "If the page fails to print, but there were no exceptions\n" + |
57 | | - "then the problem is likely elsewhere (i.e. your printer)"; |
| 45 | +public class PrintNullString implements Printable { |
| 46 | + private final String nullStr = null; |
| 47 | + private final String emptyStr = ""; |
| 48 | + private final AttributedString emptyAttStr = new AttributedString(emptyStr); |
| 49 | + private final AttributedCharacterIterator nullIterator = null; |
| 50 | + private final AttributedCharacterIterator emptyIterator = emptyAttStr.getIterator(); |
58 | 51 |
|
59 | 52 | public static void main(String[] args) throws Exception { |
60 | 53 | if (PrinterJob.lookupPrintServices().length == 0) { |
61 | 54 | throw new RuntimeException("Printer not configured or available."); |
62 | 55 | } |
63 | 56 |
|
64 | | - PassFailJFrame.builder() |
65 | | - .instructions(INSTRUCTIONS) |
66 | | - .testUI(PrintNullString::new) |
67 | | - .rows((int) INSTRUCTIONS.lines().count() + 1) |
68 | | - .columns(45) |
69 | | - .build() |
70 | | - .awaitAndCheck(); |
| 57 | + new PrintNullString(); |
71 | 58 | } |
72 | 59 |
|
73 | | - public PrintNullString() { |
74 | | - super("PrintNullString"); |
75 | | - |
76 | | - TextCanvas c = new TextCanvas(); |
77 | | - add("Center", c); |
78 | | - |
79 | | - Button b = new Button("Print"); |
80 | | - add("South", b); |
81 | | - b.addActionListener(e -> { |
82 | | - PrinterJob pj = PrinterJob.getPrinterJob(); |
83 | | - if (pj.printDialog()) { |
84 | | - pj.setPrintable(c); |
85 | | - try { |
86 | | - pj.print(); |
87 | | - } catch (PrinterException ex) { |
88 | | - ex.printStackTrace(); |
89 | | - String msg = "PrinterException: " + ex.getMessage(); |
90 | | - JOptionPane.showMessageDialog(b, msg, "Error occurred", |
91 | | - JOptionPane.ERROR_MESSAGE); |
92 | | - PassFailJFrame.forceFail(msg); |
93 | | - } |
94 | | - } |
95 | | - }); |
96 | | - pack(); |
| 60 | + public PrintNullString() throws PrinterException { |
| 61 | + PrinterJob pj = PrinterJob.getPrinterJob(); |
| 62 | + pj.setPrintable(this, new PageFormat()); |
| 63 | + PrintRequestAttributeSet pSet = new HashPrintRequestAttributeSet(); |
| 64 | + File file = new File("out.prn"); |
| 65 | + file.deleteOnExit(); |
| 66 | + pSet.add(new Destination(file.toURI())); |
| 67 | + pj.print(pSet); |
97 | 68 | } |
98 | 69 |
|
99 | | - private static class TextCanvas extends Panel implements Printable { |
100 | | - private final String nullStr = null; |
101 | | - private final String emptyStr = ""; |
102 | | - private final AttributedString emptyAttStr = new AttributedString(emptyStr); |
103 | | - private final AttributedCharacterIterator nullIterator = null; |
104 | | - private final AttributedCharacterIterator emptyIterator = emptyAttStr.getIterator(); |
105 | | - |
106 | | - @Override |
107 | | - public void paint(Graphics g) { |
108 | | - Graphics2D g2d = (Graphics2D) g; |
109 | | - paint(g2d); |
| 70 | + @Override |
| 71 | + public int print(Graphics g, PageFormat pgFmt, int pgIndex) { |
| 72 | + if (pgIndex > 0) { |
| 73 | + return NO_SUCH_PAGE; |
110 | 74 | } |
111 | 75 |
|
112 | | - @Override |
113 | | - public int print(Graphics g, PageFormat pgFmt, int pgIndex) { |
114 | | - if (pgIndex > 0) { |
115 | | - return NO_SUCH_PAGE; |
116 | | - } |
| 76 | + Graphics2D g2d = (Graphics2D) g; |
| 77 | + g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY()); |
117 | 78 |
|
118 | | - Graphics2D g2d = (Graphics2D) g; |
119 | | - g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY()); |
120 | | - paint(g2d); |
| 79 | + // API 1: null & empty drawString(String, int, int); |
| 80 | + try { |
| 81 | + g2d.drawString(nullStr, 20, 40); |
| 82 | + throw new RuntimeException("FAILURE: No NPE for null String, int"); |
| 83 | + } catch (NullPointerException e) { |
| 84 | + g2d.drawString("caught expected NPE for null String, int", 20, 40); |
| 85 | + } |
| 86 | + |
| 87 | + g2d.drawString(emptyStr, 20, 60); |
| 88 | + g2d.drawString("OK for empty String, int", 20, 60); |
| 89 | + |
| 90 | + // API 2: null & empty drawString(String, float, float); |
| 91 | + try { |
| 92 | + g2d.drawString(nullStr, 20.0f, 80.0f); |
| 93 | + throw new RuntimeException("FAILURE: No NPE for null String, float"); |
| 94 | + } catch (NullPointerException e) { |
| 95 | + g2d.drawString("caught expected NPE for null String, float", 20, 80); |
| 96 | + } |
121 | 97 |
|
122 | | - return PAGE_EXISTS; |
| 98 | + g2d.drawString(emptyStr, 20.0f, 100.0f); |
| 99 | + g2d.drawString("OK for empty String, float", 20.0f, 100.f); |
| 100 | + |
| 101 | + // API 3: null & empty drawString(Iterator, int, int); |
| 102 | + try { |
| 103 | + g2d.drawString(nullIterator, 20, 120); |
| 104 | + throw new RuntimeException("FAILURE: No NPE for null iterator, int"); |
| 105 | + } catch (NullPointerException e) { |
| 106 | + g2d.drawString("caught expected NPE for null iterator, int", 20, 120); |
| 107 | + } |
| 108 | + |
| 109 | + try { |
| 110 | + g2d.drawString(emptyIterator, 20, 140); |
| 111 | + throw new RuntimeException("FAILURE: No IAE for empty iterator, int"); |
| 112 | + } catch (IllegalArgumentException e) { |
| 113 | + g2d.drawString("caught expected IAE for empty iterator, int", 20, 140); |
123 | 114 | } |
124 | 115 |
|
125 | | - private void paint(Graphics2D g2d) { |
126 | | - // API 1: null & empty drawString(String, int, int); |
127 | | - try { |
128 | | - g2d.drawString(nullStr, 20, 40); |
129 | | - g2d.drawString("FAILURE: No NPE for null String, int", 20, 40); |
130 | | - } catch (NullPointerException e) { |
131 | | - g2d.drawString("caught expected NPE for null String, int", 20, 40); |
132 | | - } |
133 | | - |
134 | | - g2d.drawString(emptyStr, 20, 60); |
135 | | - g2d.drawString("OK for empty String, int", 20, 60); |
136 | | - |
137 | | - // API 2: null & empty drawString(String, float, float); |
138 | | - try { |
139 | | - g2d.drawString(nullStr, 20.0f, 80.0f); |
140 | | - g2d.drawString("FAILURE: No NPE for null String, float", 20, 80); |
141 | | - } catch (NullPointerException e) { |
142 | | - g2d.drawString("caught expected NPE for null String, float", 20, 80); |
143 | | - } |
144 | | - |
145 | | - g2d.drawString(emptyStr, 20.0f, 100.0f); |
146 | | - g2d.drawString("OK for empty String, float", 20.0f, 100.f); |
147 | | - |
148 | | - // API 3: null & empty drawString(Iterator, int, int); |
149 | | - try { |
150 | | - g2d.drawString(nullIterator, 20, 120); |
151 | | - g2d.drawString("FAILURE: No NPE for null iterator, int", 20, 120); |
152 | | - } catch (NullPointerException e) { |
153 | | - g2d.drawString("caught expected NPE for null iterator, int", 20, 120); |
154 | | - } |
155 | | - |
156 | | - try { |
157 | | - g2d.drawString(emptyIterator, 20, 140); |
158 | | - g2d.drawString("FAILURE: No IAE for empty iterator, int", 20, 140); |
159 | | - } catch (IllegalArgumentException e) { |
160 | | - g2d.drawString("caught expected IAE for empty iterator, int", 20, 140); |
161 | | - } |
162 | | - |
163 | | - // API 4: null & empty drawString(Iterator, float, int); |
164 | | - try { |
165 | | - g2d.drawString(nullIterator, 20.0f, 160.0f); |
166 | | - g2d.drawString("FAILURE: No NPE for null iterator, float", 20, 160); |
167 | | - } catch (NullPointerException e) { |
168 | | - g2d.drawString("caught expected NPE for null iterator, float", 20, 160); |
169 | | - } |
170 | | - |
171 | | - try { |
172 | | - g2d.drawString(emptyIterator, 20.0f, 180.0f); |
173 | | - g2d.drawString("FAILURE: No IAE for empty iterator, float", 20, 180); |
174 | | - } catch (IllegalArgumentException e) { |
175 | | - g2d.drawString("caught expected IAE for empty iterator, float", 20, 180); |
176 | | - } |
| 116 | + // API 4: null & empty drawString(Iterator, float, int); |
| 117 | + try { |
| 118 | + g2d.drawString(nullIterator, 20.0f, 160.0f); |
| 119 | + throw new RuntimeException("FAILURE: No NPE for null iterator, float"); |
| 120 | + } catch (NullPointerException e) { |
| 121 | + g2d.drawString("caught expected NPE for null iterator, float", 20, 160); |
177 | 122 | } |
178 | 123 |
|
179 | | - @Override |
180 | | - public Dimension getPreferredSize() { |
181 | | - return new Dimension(450, 250); |
| 124 | + try { |
| 125 | + g2d.drawString(emptyIterator, 20.0f, 180.0f); |
| 126 | + throw new RuntimeException("FAILURE: No IAE for empty iterator, float"); |
| 127 | + } catch (IllegalArgumentException e) { |
| 128 | + g2d.drawString("caught expected IAE for empty iterator, float", 20, 180); |
182 | 129 | } |
| 130 | + |
| 131 | + return PAGE_EXISTS; |
183 | 132 | } |
184 | 133 | } |
0 commit comments