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