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 4987884
  26  * @summary  Verify if PrinterJob carries over some values between calls to print
  27  * @run main TestCarryOverValue
  28  */
  29 import java.awt.Graphics;
  30 import java.awt.print.Book;
  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 javax.print.DocFlavor;
  36 import javax.print.PrintService;
  37 import javax.print.attribute.AttributeSet;
  38 import javax.print.attribute.HashPrintRequestAttributeSet;
  39 import javax.print.attribute.PrintRequestAttributeSet;
  40 import javax.print.attribute.standard.PageRanges;
  41 
  42 public class TestCarryOverValue implements Printable {
  43 
  44     static int pageCount = 0;
  45 
  46     public static void main(String args[]) {
  47         PrinterJob pjob = PrinterJob.getPrinterJob();
  48         PageFormat pf = pjob.defaultPage();
  49         Book book = new Book();
  50 
  51         book.append(new TestCarryOverValue(), pf);
  52         book.append(new TestCarryOverValue(), pf);
  53         pjob.setPageable(book);
  54         PrintRequestAttributeSet aset =
  55                 new HashPrintRequestAttributeSet();
  56         aset.add(new PageRanges(1,1));
  57         PrintService service = pjob.getPrintService();
  58         AttributeSet unsupportedSet =
  59                 service.getUnsupportedAttributes(
  60                         DocFlavor.SERVICE_FORMATTED.PAGEABLE, aset);
  61         if (unsupportedSet != null) {
  62             System.out.println("job unsupported - exiting");
  63             return;
  64         }
  65         try {
  66             pjob.print(aset);
  67         } catch (PrinterException e) {
  68             throw new RuntimeException(e.getMessage());
  69         }
  70 
  71         Book book2 = new Book();
  72         book2.append(new TestCarryOverValue(), pf);
  73         book2.append(new TestCarryOverValue(), pf);
  74         pjob.setPageable(book2);
  75 
  76         pageCount = 0;
  77         try {
  78             pjob.print();
  79         } catch (PrinterException e) {
  80             throw new RuntimeException(e.getMessage());
  81         }
  82         if (pageCount < 4) {
  83             throw new RuntimeException(" Wrong number of pages printed. "
  84                     + " Value carried forward between print calls");
  85         }
  86     }
  87 
  88     public int print(Graphics g, PageFormat pf, int pageIndex) {
  89         pageCount++;
  90         System.out.println("print " + pageIndex);
  91         return PAGE_EXISTS;
  92     }
  93 }
  94 
  95