|
| 1 | +/* |
| 2 | + * Copyright (c) 2007, 2026, 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 | +import java.awt.Font; |
| 25 | +import java.awt.Graphics; |
| 26 | +import java.awt.print.PageFormat; |
| 27 | +import java.awt.print.Pageable; |
| 28 | +import java.awt.print.Printable; |
| 29 | +import java.awt.print.PrinterException; |
| 30 | +import java.awt.print.PrinterJob; |
| 31 | +import java.io.File; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.BitSet; |
| 35 | +import java.util.List; |
| 36 | +import java.util.stream.IntStream; |
| 37 | + |
| 38 | +import javax.print.attribute.HashPrintRequestAttributeSet; |
| 39 | +import javax.print.attribute.PrintRequestAttributeSet; |
| 40 | +import javax.print.attribute.standard.Destination; |
| 41 | +import javax.print.attribute.standard.PageRanges; |
| 42 | + |
| 43 | +/* |
| 44 | + * @test |
| 45 | + * @bug 6575331 8297191 |
| 46 | + * @key printer |
| 47 | + * @summary Automatically verifies all the pages in a range are printed |
| 48 | + * @run main PageRangesAuto |
| 49 | + */ |
| 50 | +public class PageRangesAuto implements Pageable, Printable { |
| 51 | + |
| 52 | + private static final Font font = new Font(Font.SERIF, Font.PLAIN, 50); |
| 53 | + |
| 54 | + private static final int MAX_PAGE = 10; |
| 55 | + |
| 56 | + private static final int[][] ranges = { |
| 57 | + {1, 1}, |
| 58 | + {1, MAX_PAGE}, |
| 59 | + {2, 3}, |
| 60 | + {3, 6}, |
| 61 | + {4, 7}, |
| 62 | + {7, 7}, |
| 63 | + {9, MAX_PAGE}, |
| 64 | + {MAX_PAGE, MAX_PAGE}, |
| 65 | + }; |
| 66 | + |
| 67 | + private enum Type { |
| 68 | + PRINTABLE, |
| 69 | + PAGEABLE |
| 70 | + } |
| 71 | + |
| 72 | + private final BitSet printedPages = new BitSet(); |
| 73 | + |
| 74 | + /** |
| 75 | + * Configures a printer job and prints it. |
| 76 | + * @param type the type of the interface tested |
| 77 | + * ({@code Printable} or {@code Pageable}) |
| 78 | + * @param pageRange the range of pages to print; |
| 79 | + * if {@code null}, print all pages |
| 80 | + * @return a bit set of printed page numbers |
| 81 | + */ |
| 82 | + private static BitSet printJob(final Type type, |
| 83 | + final PageRanges pageRange) |
| 84 | + throws PrinterException { |
| 85 | + final PageRangesAuto test = new PageRangesAuto(); |
| 86 | + |
| 87 | + final PrinterJob job = PrinterJob.getPrinterJob(); |
| 88 | + final String baseName = type.name().toLowerCase(); |
| 89 | + |
| 90 | + switch (type) { |
| 91 | + case PRINTABLE -> job.setPrintable(test); |
| 92 | + case PAGEABLE -> job.setPageable(test); |
| 93 | + } |
| 94 | + |
| 95 | + String fileName = pageRange == null |
| 96 | + ? baseName + "-all.pdf" |
| 97 | + : String.format("%s-%d-%d.pdf", |
| 98 | + baseName, |
| 99 | + pageRange.getMembers()[0][0], |
| 100 | + pageRange.getMembers()[0][1]); |
| 101 | + |
| 102 | + PrintRequestAttributeSet set = new HashPrintRequestAttributeSet(); |
| 103 | + set.add(new Destination(new File(fileName) |
| 104 | + .toURI())); |
| 105 | + if (pageRange != null) { |
| 106 | + set.add(pageRange); |
| 107 | + } |
| 108 | + |
| 109 | + job.print(set); |
| 110 | + |
| 111 | + return test.printedPages; |
| 112 | + } |
| 113 | + |
| 114 | + public static void main(String[] args) throws Exception { |
| 115 | + final List<Error> errors = new ArrayList<>(); |
| 116 | + |
| 117 | + for (Type type : Type.values()) { |
| 118 | + BitSet pages; // Printed pages |
| 119 | + |
| 120 | + // Print all pages |
| 121 | + System.out.println(type + " - all pages"); |
| 122 | + pages = printJob(type, null); |
| 123 | + if (!IntStream.range(0, MAX_PAGE) |
| 124 | + .allMatch(pages::get)) { |
| 125 | + errors.add(new Error("Not all pages printed in " + type + ": " |
| 126 | + + pages)); |
| 127 | + } |
| 128 | + |
| 129 | + // Print page range |
| 130 | + for (int[] range : ranges) { |
| 131 | + System.out.println(type + " - " + Arrays.toString(range)); |
| 132 | + pages = printJob(type, new PageRanges(range[0], range[1])); |
| 133 | + if (!IntStream.range(range[0] - 1, range[1]) |
| 134 | + .allMatch(pages::get)) { |
| 135 | + errors.add(new Error("Not all pages printed in " + type |
| 136 | + + " within the range " |
| 137 | + + Arrays.toString(range) |
| 138 | + + ": " + pages)); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if (!errors.isEmpty()) { |
| 144 | + errors.forEach(System.err::println); |
| 145 | + throw new RuntimeException("Errors detected: " + errors.size() |
| 146 | + + ". - " + errors.get(0)); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public int print(Graphics g, PageFormat format, int pageIndex) |
| 152 | + throws PrinterException { |
| 153 | + printedPages.set(pageIndex); |
| 154 | + |
| 155 | + final int pageNo = pageIndex + 1; |
| 156 | + System.out.println(" test.printPage " + pageNo); |
| 157 | + if (pageIndex >= MAX_PAGE) { |
| 158 | + return NO_SUCH_PAGE; |
| 159 | + } |
| 160 | + |
| 161 | + g.setFont(font); |
| 162 | + g.drawString("Page: " + pageNo, |
| 163 | + 100, 150); |
| 164 | + |
| 165 | + return PAGE_EXISTS; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public int getNumberOfPages() { |
| 170 | + System.out.println(" test.getNumberOfPages = " + MAX_PAGE); |
| 171 | + return MAX_PAGE; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public PageFormat getPageFormat(int pageIndex) |
| 176 | + throws IndexOutOfBoundsException { |
| 177 | + checkPageIndex(pageIndex); |
| 178 | + return new PageFormat(); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public Printable getPrintable(int pageIndex) |
| 183 | + throws IndexOutOfBoundsException { |
| 184 | + checkPageIndex(pageIndex); |
| 185 | + System.out.println(" test.getPrintable(" + (pageIndex + 1) + ")"); |
| 186 | + return this; |
| 187 | + } |
| 188 | + |
| 189 | + private static void checkPageIndex(int pageIndex) |
| 190 | + throws IndexOutOfBoundsException { |
| 191 | + if (pageIndex < 0) { |
| 192 | + throw new IndexOutOfBoundsException("pageIndex < 0"); |
| 193 | + } |
| 194 | + |
| 195 | + if (pageIndex >= MAX_PAGE) { |
| 196 | + throw new IndexOutOfBoundsException("pageIndex >= " + MAX_PAGE); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments