1/* 2 * Copyright (c) 2007, 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.*; 25 import java.awt.print.*; 26 27 public class SmallPaperPrinting 28 { 29 public static void main(String args[]) 30 { 31 System.out.println("----------------- Instructions --------------------"); 32 System.out.println("Arguments: (none) - paper width=1, height=.0001"); 33 System.out.println(" 1 - paper width=.0001, height=1"); 34 System.out.println(" 2 - paper width=-1, height=1"); 35 System.out.println("A passing test should catch a PrinterException"); 36 System.out.println("and should display \"Print error: (exception msg)\"."); 37 System.out.println("---------------------------------------------------\n"); 38 PrinterJob job = PrinterJob.getPrinterJob(); 39 PageFormat format = job.defaultPage(); 40 Paper paper = format.getPaper(); 41 42 double w = 1, h = .0001; // Generates ArithmeticException: / by zero. 43 if(args.length > 0 && args[0].equals("1")) { 44 w = .0001; h = 1; } // Generates IllegalArgumentException. 45 else if(args.length > 0 && args[0].equals("2")) { 46 w = -1; h = 1; } // Generates NegativeArraySizeException. 47 paper.setSize(w, h); 48 paper.setImageableArea(0, 0, w, h); 49 format.setPaper(paper); 50 job.setPrintable( 51 new Printable() { 52 public int print(Graphics g, PageFormat page_format, int page) { 53 return NO_SUCH_PAGE; 54 } 55 }, format); 56 57 try { 58 job.print(); } 59 catch(PrinterException e) { 60 System.err.println("Print error:\n" + e.getMessage()); // Passing test! 61 } 62 } 63 } 64