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  amount of copies and the printer job name is not correctly
  27             passed to implementation
  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   /**
  72    *
  73    * @param printerName
  74    * @throws Exception
  75    */
  76   static void thirdPartyPrintLogic(String printerName) throws Exception {
  77     PrinterJob printerjob = PrinterJob.getPrinterJob();
  78     printerjob.setCopies(2);
  79     printerjob.setJobName("myJobName");
  80     printerjob.setPrintable(new DummyPrintable());
  81     for (PrintService printService : PrinterJob.lookupPrintServices()) {
  82       System.out.println("check printer name of service " + printService);
  83       if (printerName.equals(printService.getName())) {
  84         System.out.println("correct printer service do print...");
  85         printerjob.setPrintService(printService);
  86         printerjob.print();
  87         break;
  88       }
  89     }
  90   }
  91 }
  92 
  93 class DummyPrintService implements PrintService {
  94     private final String _name;
  95     private final Set<DocFlavor> _supportedFlavors;
  96     private final PrintServiceAttributeSet _printServiceAttributeSet;
  97 
  98     public DummyPrintService(String name) {
  99         _name = name;
 100         _supportedFlavors = new HashSet<DocFlavor>();
 101         _supportedFlavors.add(DocFlavor.SERVICE_FORMATTED.PAGEABLE);
 102         _supportedFlavors.add(DocFlavor.SERVICE_FORMATTED.PRINTABLE);
 103         _printServiceAttributeSet = new HashPrintServiceAttributeSet();
 104         _printServiceAttributeSet.add(new PrinterName(name, null));
 105         _printServiceAttributeSet.add(PrinterState.IDLE);
 106     }
 107 
 108     @Override
 109     public String toString() {
 110         return "Dummy Printer : " + getName();
 111     }
 112 
 113     public String getName() {
 114         return _name;
 115     }
 116 
 117     public DocPrintJob createPrintJob() {
 118         return new DummyDocPrintJob(this);
 119     }
 120 
 121     public boolean isDocFlavorSupported(DocFlavor flavor) {
 122         return _supportedFlavors.contains(flavor);
 123     }
 124 
 125     public <T extends PrintServiceAttribute> T getAttribute(Class<T> category) {
 126         return category.cast(_printServiceAttributeSet.get(category));
 127     }
 128 
 129     public PrintServiceAttributeSet getAttributes() {
 130         return _printServiceAttributeSet;
 131     }
 132 
 133     public DocFlavor[] getSupportedDocFlavors() {
 134         return _supportedFlavors.toArray(new DocFlavor[_supportedFlavors.size()]);
 135     }
 136 
 137     public Object getDefaultAttributeValue(Class<? extends Attribute> category) {
 138         return null;
 139     }
 140 
 141     public ServiceUIFactory getServiceUIFactory() {
 142         return null;
 143     }
 144 
 145     public Class<?>[] getSupportedAttributeCategories() {
 146         return null;
 147     }
 148 
 149     public Object getSupportedAttributeValues(Class<? extends Attribute> category, DocFlavor flavor, AttributeSet attributes) {
 150         return null;
 151     }
 152 
 153     public AttributeSet getUnsupportedAttributes(DocFlavor flavor, AttributeSet attributes) {
 154         return null;
 155     }
 156 
 157     public boolean isAttributeCategorySupported(Class<? extends Attribute> category) {
 158         return false;
 159     }
 160 
 161     public boolean isAttributeValueSupported(Attribute attrval, DocFlavor flavor, AttributeSet attributes) {
 162         return false;
 163     }
 164 
 165     public void addPrintServiceAttributeListener(PrintServiceAttributeListener listener) {
 166     }
 167 
 168     public void removePrintServiceAttributeListener(PrintServiceAttributeListener listener) {
 169     }
 170 }
 171 
 172 class DummyDocPrintJob implements DocPrintJob {
 173     private static int _counter;
 174     private final PrintService _printService;
 175     private final PrintJobAttributeSet _printJobAttributeSet;
 176 
 177     public DummyDocPrintJob(PrintService printService) {
 178         _counter++;
 179         _printService = printService;
 180         _printJobAttributeSet = new HashPrintJobAttributeSet();
 181     }
 182 
 183     public PrintService getPrintService() {
 184         return _printService;
 185     }
 186 
 187     public PrintJobAttributeSet getAttributes() {
 188         return _printJobAttributeSet;
 189     }
 190 
 191     public void addPrintJobAttributeListener(PrintJobAttributeListener listener, 
 192                                     PrintJobAttributeSet printJobAttributeSet) {
 193     }
 194 
 195     public void removePrintJobAttributeListener(PrintJobAttributeListener listener) {
 196     }
 197 
 198     public void addPrintJobListener(PrintJobListener listener) {
 199     }
 200 
 201     public void removePrintJobListener(PrintJobListener listener) {
 202     }
 203 
 204     public void print(Doc doc, PrintRequestAttributeSet printRequestAttributeSet) 
 205           throws PrintException {
 206         System.out.println("job name: " + printRequestAttributeSet.get(JobName.class));
 207         System.out.println("copies: " + printRequestAttributeSet.get(Copies.class));
 208         if(printRequestAttributeSet.get(JobName.class) == null || 
 209             printRequestAttributeSet.get(Copies.class) == null) {
 210             throw new RuntimeException("Copies and JobName is not passed correctly");
 211         }
 212     }
 213 }
 214 
 215 class DummyPrintable implements Printable {
 216     public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
 217         if (pageIndex == 0) {
 218             return Printable.PAGE_EXISTS;
 219         } else {
 220             return Printable.NO_SUCH_PAGE;
 221         }
 222     }
 223 }