1 /* 2 * Copyright (c) 2013, 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 4973278 8015586 26 @run main PrintToDir 27 @summary Must throw exception when printing to an invalid filename - a dir. 28 */ 29 import java.io.*; 30 import java.net.*; 31 import java.awt.*; 32 import java.awt.geom.*; 33 import java.awt.print.*; 34 import javax.print.PrintService; 35 import javax.print.attribute.*; 36 import javax.print.attribute.standard.*; 37 import java.util.PropertyPermission; 38 39 public class PrintToDir extends Frame implements Printable { 40 41 boolean firstTime = true; 42 double sx, sy; 43 Shape clip, firstClip; 44 45 TextField tf = new TextField(); 46 Label tfLabel = new Label ("File Name"); 47 Panel p = new Panel (new GridLayout(2,2)); 48 Button b = new Button("Print"); 49 50 PrintToDir() { 51 add("South", p); 52 p.add(tfLabel); 53 p.add(tf); 54 p.add(b); 55 setSize(300, 300); 56 setVisible(true); 57 } 58 59 public int print(Graphics g, PageFormat pf, int pageIndex) { 60 Graphics2D g2 = (Graphics2D)g; 61 if (pageIndex>=1) { 62 return Printable.NO_SUCH_PAGE; 63 } 64 g2.drawString("hello world", 100, 100); 65 return Printable.PAGE_EXISTS; 66 } 67 68 void doPrintJob(String fileStr) { 69 PageAttributes pa = new PageAttributes(); 70 JobAttributes ja = new JobAttributes(); 71 ja.setDialog(JobAttributes.DialogType.NONE); 72 ja.setDestination(JobAttributes.DestinationType.FILE); 73 ja.setFileName(fileStr); 74 try { 75 PrintJob pjob = Toolkit.getDefaultToolkit().getPrintJob(this, 76 "PrintDialog Testing", ja, pa); 77 if (pjob != null) { 78 System.out.println("Printjob successfully created: " + pjob); 79 Graphics g = pjob.getGraphics(); 80 this.printAll(g); 81 g.dispose(); 82 pjob.end(); 83 } 84 System.out.println("Printing completed"); 85 } catch (IllegalArgumentException e) { 86 System.out.println("PrintJob passed."); 87 return; 88 } 89 throw new RuntimeException("PrintJob::IllegalArgumentException expected but not thrown. \nTEST FAILED"); 90 } 91 92 public static void doPrinterJob(String fileStr, OrientationRequested o) { 93 PrinterJob pj = PrinterJob.getPrinterJob(); 94 PrintService ps = pj.getPrintService(); 95 if (ps == null) { 96 System.out.println("No print service found."); 97 return; 98 } 99 pj.setPrintable(new PrintToDir()); 100 PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 101 aset.add(o); 102 File f = new File(fileStr); 103 // f.deleteOnExit(); 104 URI dest = f.toURI(); 105 Destination d = new Destination(dest); 106 if (ps.isAttributeValueSupported(d, null, null)) { 107 aset.add(d); 108 try { 109 pj.print(aset); 110 } catch (PrinterException e) { 111 System.out.println("PrinterJob passed."); 112 return; 113 } 114 throw new RuntimeException("PrinterJob:PrinterException expected but not thrown. \nTEST FAILED"); 115 } else { 116 System.out.println("Destination attribute is not a supported value. PrinterJob passed."); 117 } 118 } 119 120 121 public static void main(String arg[]) { 122 SecurityManager security = System.getSecurityManager(); 123 if (security != null) { 124 System.out.println("Security manager detected"); 125 try { 126 security.checkPermission(new FilePermission("<<ALL FILES>>", "read,write")); 127 security.checkPermission(new PropertyPermission("user.dir", "read")); 128 } catch (SecurityException se) { 129 System.out.println("Security requirement not obtained. TEST PASSED"); 130 return; 131 } 132 } 133 String[] testStr = {".", ""}; 134 for (int i=0; i<testStr.length; i++) { 135 System.out.println("Testing file name = \""+testStr[i]+"\""); 136 doPrinterJob(testStr[i], OrientationRequested.PORTRAIT); 137 PrintToDir ptd = new PrintToDir(); 138 ptd.doPrintJob(testStr[i]); 139 ptd.dispose(); 140 } 141 System.out.println("TEST PASSED"); 142 } 143 144 }