|
1 /* |
|
2 * Copyright (c) 2016, 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 6921664 |
|
26 * @summary Verifies number of copies and the job name are passed to a |
|
27 * 3rd party PrintService. |
|
28 * @run main DummyPrintTest |
|
29 */ |
|
30 import java.awt.Graphics; |
|
31 import java.awt.print.PageFormat; |
|
32 import java.awt.print.Printable; |
|
33 import java.awt.print.PrinterException; |
|
34 import java.awt.print.PrinterJob; |
|
35 import java.util.HashSet; |
|
36 import java.util.Set; |
|
37 import javax.print.Doc; |
|
38 import javax.print.DocFlavor; |
|
39 import javax.print.DocPrintJob; |
|
40 import javax.print.PrintException; |
|
41 import javax.print.PrintService; |
|
42 import javax.print.PrintServiceLookup; |
|
43 import javax.print.ServiceUIFactory; |
|
44 import javax.print.attribute.Attribute; |
|
45 import javax.print.attribute.AttributeSet; |
|
46 import javax.print.attribute.HashPrintJobAttributeSet; |
|
47 import javax.print.attribute.HashPrintServiceAttributeSet; |
|
48 import javax.print.attribute.PrintJobAttributeSet; |
|
49 import javax.print.attribute.PrintRequestAttributeSet; |
|
50 import javax.print.attribute.PrintServiceAttribute; |
|
51 import javax.print.attribute.PrintServiceAttributeSet; |
|
52 import javax.print.attribute.standard.Copies; |
|
53 import javax.print.attribute.standard.JobName; |
|
54 import javax.print.attribute.standard.PrinterName; |
|
55 import javax.print.attribute.standard.PrinterState; |
|
56 import javax.print.event.PrintJobAttributeListener; |
|
57 import javax.print.event.PrintJobListener; |
|
58 import javax.print.event.PrintServiceAttributeListener; |
|
59 |
|
60 |
|
61 public class DummyPrintTest { |
|
62 |
|
63 public static void main(String[] args) throws Exception { |
|
64 // register custom print service implementation |
|
65 String printerName = "myDummyPrintService"; |
|
66 PrintServiceLookup.registerService(new DummyPrintService(printerName)); |
|
67 // calling third party print logic |
|
68 thirdPartyPrintLogic(printerName); |
|
69 } |
|
70 |
|
71 static void thirdPartyPrintLogic(String printerName) throws Exception { |
|
72 PrinterJob printerjob = PrinterJob.getPrinterJob(); |
|
73 printerjob.setCopies(2); |
|
74 printerjob.setJobName("myJobName"); |
|
75 printerjob.setPrintable(new DummyPrintable()); |
|
76 for (PrintService printService : PrinterJob.lookupPrintServices()) { |
|
77 System.out.println("check printer name of service " + printService); |
|
78 if (printerName.equals(printService.getName())) { |
|
79 System.out.println("correct printer service do print..."); |
|
80 printerjob.setPrintService(printService); |
|
81 printerjob.print(); |
|
82 break; |
|
83 } |
|
84 } |
|
85 } |
|
86 } |
|
87 |
|
88 class DummyPrintService implements PrintService { |
|
89 private final String _name; |
|
90 private final Set<DocFlavor> _supportedFlavors; |
|
91 private final PrintServiceAttributeSet _printServiceAttributeSet; |
|
92 |
|
93 public DummyPrintService(String name) { |
|
94 _name = name; |
|
95 _supportedFlavors = new HashSet<DocFlavor>(); |
|
96 _supportedFlavors.add(DocFlavor.SERVICE_FORMATTED.PAGEABLE); |
|
97 _supportedFlavors.add(DocFlavor.SERVICE_FORMATTED.PRINTABLE); |
|
98 _printServiceAttributeSet = new HashPrintServiceAttributeSet(); |
|
99 _printServiceAttributeSet.add(new PrinterName(name, null)); |
|
100 _printServiceAttributeSet.add(PrinterState.IDLE); |
|
101 } |
|
102 |
|
103 @Override |
|
104 public String toString() { |
|
105 return "Dummy Printer : " + getName(); |
|
106 } |
|
107 |
|
108 @Override |
|
109 public String getName() { |
|
110 return _name; |
|
111 } |
|
112 |
|
113 @Override |
|
114 public DocPrintJob createPrintJob() { |
|
115 return new DummyDocPrintJob(this); |
|
116 } |
|
117 |
|
118 @Override |
|
119 public boolean isDocFlavorSupported(DocFlavor flavor) { |
|
120 return _supportedFlavors.contains(flavor); |
|
121 } |
|
122 |
|
123 @Override |
|
124 public <T extends PrintServiceAttribute> T getAttribute(Class<T> category) { |
|
125 return category.cast(_printServiceAttributeSet.get(category)); |
|
126 } |
|
127 |
|
128 @Override |
|
129 public PrintServiceAttributeSet getAttributes() { |
|
130 return _printServiceAttributeSet; |
|
131 } |
|
132 |
|
133 @Override |
|
134 public DocFlavor[] getSupportedDocFlavors() { |
|
135 return _supportedFlavors.toArray(new DocFlavor[_supportedFlavors.size()]); |
|
136 } |
|
137 |
|
138 @Override |
|
139 public Object getDefaultAttributeValue(Class<? extends Attribute> category) { |
|
140 return null; |
|
141 } |
|
142 |
|
143 @Override |
|
144 public ServiceUIFactory getServiceUIFactory() { |
|
145 return null; |
|
146 } |
|
147 |
|
148 @Override |
|
149 public Class<?>[] getSupportedAttributeCategories() { |
|
150 return null; |
|
151 } |
|
152 |
|
153 @Override |
|
154 public Object getSupportedAttributeValues(Class<? extends Attribute> category, |
|
155 DocFlavor flavor, AttributeSet attributes) { |
|
156 return null; |
|
157 } |
|
158 |
|
159 @Override |
|
160 public AttributeSet getUnsupportedAttributes(DocFlavor flavor, |
|
161 AttributeSet attributes) { |
|
162 return null; |
|
163 } |
|
164 |
|
165 @Override |
|
166 public boolean isAttributeCategorySupported(Class<? extends Attribute> category) { |
|
167 return false; |
|
168 } |
|
169 |
|
170 @Override |
|
171 public boolean isAttributeValueSupported(Attribute attrval, |
|
172 DocFlavor flavor, |
|
173 AttributeSet attributes) { |
|
174 return false; |
|
175 } |
|
176 |
|
177 @Override |
|
178 public void addPrintServiceAttributeListener(PrintServiceAttributeListener listener) { |
|
179 } |
|
180 |
|
181 @Override |
|
182 public void removePrintServiceAttributeListener(PrintServiceAttributeListener listener) { |
|
183 } |
|
184 } |
|
185 |
|
186 class DummyDocPrintJob implements DocPrintJob { |
|
187 private static int _counter; |
|
188 private final PrintService _printService; |
|
189 private final PrintJobAttributeSet _printJobAttributeSet; |
|
190 |
|
191 public DummyDocPrintJob(PrintService printService) { |
|
192 _counter++; |
|
193 _printService = printService; |
|
194 _printJobAttributeSet = new HashPrintJobAttributeSet(); |
|
195 } |
|
196 |
|
197 @Override |
|
198 public PrintService getPrintService() { |
|
199 return _printService; |
|
200 } |
|
201 |
|
202 @Override |
|
203 public PrintJobAttributeSet getAttributes() { |
|
204 return _printJobAttributeSet; |
|
205 } |
|
206 |
|
207 @Override |
|
208 public void addPrintJobAttributeListener(PrintJobAttributeListener listener, |
|
209 PrintJobAttributeSet printJobAttributeSet) { |
|
210 } |
|
211 |
|
212 @Override |
|
213 public void removePrintJobAttributeListener(PrintJobAttributeListener listener) { |
|
214 } |
|
215 |
|
216 @Override |
|
217 public void addPrintJobListener(PrintJobListener listener) { |
|
218 } |
|
219 |
|
220 @Override |
|
221 public void removePrintJobListener(PrintJobListener listener) { |
|
222 } |
|
223 |
|
224 @Override |
|
225 public void print(Doc doc, |
|
226 PrintRequestAttributeSet printRequestAttributeSet) |
|
227 throws PrintException { |
|
228 System.out.println("job name: " + printRequestAttributeSet.get(JobName.class)); |
|
229 System.out.println("copies: " + printRequestAttributeSet.get(Copies.class)); |
|
230 if(printRequestAttributeSet.get(JobName.class) == null || |
|
231 printRequestAttributeSet.get(Copies.class) == null) { |
|
232 throw new RuntimeException("Copies and JobName is not passed correctly"); |
|
233 } |
|
234 } |
|
235 } |
|
236 |
|
237 class DummyPrintable implements Printable { |
|
238 @Override |
|
239 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) |
|
240 throws PrinterException { |
|
241 if (pageIndex == 0) { |
|
242 return Printable.PAGE_EXISTS; |
|
243 } else { |
|
244 return Printable.NO_SUCH_PAGE; |
|
245 } |
|
246 } |
|
247 } |