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 /** 25 * @test 26 * @bug 4223328 27 * @summary Printer graphics must behave the same as screen graphics 28 * @author prr 29 * @run main/manual PrintNullString 30 */ 31 32 33 import java.awt.*; 34 import java.awt.event.*; 35 import java.awt.print.*; 36 import java.text.*; 37 38 public class PrintNullString extends Frame implements ActionListener { 39 40 private TextCanvas c; 41 main(String args[])42 public static void main(String args[]) { 43 44 String[] instructions = 45 { 46 "You must have a printer available to perform this test", 47 "This test should print a page which contains the same", 48 "text messages as in the test window on the screen", 49 "The messages should contain only 'OK' and 'expected' messages", 50 "There should be no FAILURE messages.", 51 "You should also monitor the command line to see if any exceptions", 52 "were thrown", 53 "If the page fails to print, but there were no exceptions", 54 "then the problem is likely elsewhere (ie your printer)" 55 }; 56 Sysout.createDialog( ); 57 Sysout.printInstructions( instructions ); 58 59 PrintNullString f = new PrintNullString(); 60 f.show(); 61 } 62 PrintNullString()63 public PrintNullString() { 64 super("JDK 1.2 drawString Printing"); 65 66 c = new TextCanvas(); 67 add("Center", c); 68 69 Button printButton = new Button("Print"); 70 printButton.addActionListener(this); 71 add("South", printButton); 72 73 addWindowListener(new WindowAdapter() { 74 public void windowClosing(WindowEvent e) { 75 System.exit(0); 76 } 77 }); 78 79 pack(); 80 } 81 actionPerformed(ActionEvent e)82 public void actionPerformed(ActionEvent e) { 83 84 PrinterJob pj = PrinterJob.getPrinterJob(); 85 86 if (pj != null && pj.printDialog()) { 87 88 pj.setPrintable(c); 89 try { 90 pj.print(); 91 } catch (PrinterException pe) { 92 } finally { 93 System.err.println("PRINT RETURNED"); 94 } 95 } 96 } 97 98 class TextCanvas extends Panel implements Printable { 99 100 String nullStr = null; 101 String emptyStr = new String(); 102 AttributedString nullAttStr = null; 103 AttributedString emptyAttStr = new AttributedString(emptyStr); 104 AttributedCharacterIterator nullIterator = null; 105 AttributedCharacterIterator emptyIterator = emptyAttStr.getIterator(); 106 print(Graphics g, PageFormat pgFmt, int pgIndex)107 public int print(Graphics g, PageFormat pgFmt, int pgIndex) { 108 109 if (pgIndex > 0) 110 return Printable.NO_SUCH_PAGE; 111 112 Graphics2D g2d = (Graphics2D)g; 113 g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY()); 114 115 paint(g); 116 117 return Printable.PAGE_EXISTS; 118 } 119 paint(Graphics g1)120 public void paint(Graphics g1) { 121 Graphics2D g = (Graphics2D)g1; 122 123 // API 1: null & empty drawString(String, int, int); 124 try { 125 g.drawString(nullStr, 20, 40); 126 g.drawString("FAILURE: No NPE for null String, int", 20, 40); 127 } catch (NullPointerException e) { 128 g.drawString("caught expected NPE for null String, int", 20, 40); 129 }/* catch (Exception e) { 130 g.drawString("FAILURE: unexpected exception for null String, int", 131 20, 40); 132 }*/ 133 134 //try { 135 g.drawString(emptyStr, 20, 60); 136 g.drawString("OK for empty String, int", 20, 60); 137 /*} catch (Exception e) { 138 g.drawString("FAILURE: unexpected exception for empty String, int", 139 20, 60); 140 }*/ 141 142 143 // API 2: null & empty drawString(String, float, float); 144 try { 145 g.drawString(nullStr, 20.0f, 80.0f); 146 g.drawString("FAILURE: No NPE for null String, float", 20, 80); 147 } catch (NullPointerException e) { 148 g.drawString("caught expected NPE for null String, float", 20, 80); 149 } /*catch (Exception e) { 150 g.drawString("FAILURE: unexpected exception for null String, float", 151 20, 80); 152 }*/ 153 //try { 154 g.drawString(emptyStr, 20.0f, 100.0f); 155 g.drawString("OK for empty String, float", 20.0f, 100.f); 156 /* } catch (Exception e) { 157 g.drawString("FAILURE: unexpected exception for empty String, float", 158 20, 100); 159 }*/ 160 161 // API 3: null & empty drawString(Iterator, int, int); 162 try { 163 g.drawString(nullIterator, 20, 120); 164 g.drawString("FAILURE: No NPE for null iterator, float", 20, 120); 165 } catch (NullPointerException e) { 166 g.drawString("caught expected NPE for null iterator, int", 20, 120); 167 } /*catch (Exception e) { 168 g.drawString("FAILURE: unexpected exception for null iterator, int", 169 20, 120); 170 } */ 171 try { 172 g.drawString(emptyIterator, 20, 140); 173 g.drawString("FAILURE: No IAE for empty iterator, int", 174 20, 140); 175 } catch (IllegalArgumentException e) { 176 g.drawString("caught expected IAE for empty iterator, int", 177 20, 140); 178 } /*catch (Exception e) { 179 g.drawString("FAILURE: unexpected exception for empty iterator, int", 180 20, 140); 181 } */ 182 183 184 // API 4: null & empty drawString(Iterator, float, int); 185 try { 186 g.drawString(nullIterator, 20.0f, 160.0f); 187 g.drawString("FAILURE: No NPE for null iterator, float", 20, 160); 188 } catch (NullPointerException e) { 189 g.drawString("caught expected NPE for null iterator, float", 20, 160); 190 } /*catch (Exception e) { 191 g.drawString("FAILURE: unexpected exception for null iterator, float", 192 20, 160); 193 } */ 194 195 try { 196 g.drawString(emptyIterator, 20, 180); 197 g.drawString("FAILURE: No IAE for empty iterator, float", 198 20, 180); 199 } catch (IllegalArgumentException e) { 200 g.drawString("caught expected IAE for empty iterator, float", 201 20, 180); 202 } /*catch (Exception e) { 203 g.drawString("FAILURE: unexpected exception for empty iterator, float", 204 20, 180); 205 } */ 206 } 207 getPreferredSize()208 public Dimension getPreferredSize() { 209 return new Dimension(450, 250); 210 } 211 } 212 213 } 214 215 class Sysout 216 { 217 private static TestDialog dialog; 218 createDialogWithInstructions( String[] instructions )219 public static void createDialogWithInstructions( String[] instructions ) 220 { 221 dialog = new TestDialog( new Frame(), "Instructions" ); 222 dialog.printInstructions( instructions ); 223 dialog.show(); 224 println( "Any messages for the tester will display here." ); 225 } 226 createDialog( )227 public static void createDialog( ) 228 { 229 dialog = new TestDialog( new Frame(), "Instructions" ); 230 String[] defInstr = { "Instructions will appear here. ", "" } ; 231 dialog.printInstructions( defInstr ); 232 dialog.show(); 233 println( "Any messages for the tester will display here." ); 234 } 235 236 printInstructions( String[] instructions )237 public static void printInstructions( String[] instructions ) 238 { 239 dialog.printInstructions( instructions ); 240 } 241 242 println( String messageIn )243 public static void println( String messageIn ) 244 { 245 dialog.displayMessage( messageIn ); 246 } 247 248 }// Sysout class 249 250 /** 251 This is part of the standard test machinery. It provides a place for the 252 test instructions to be displayed, and a place for interactive messages 253 to the user to be displayed. 254 To have the test instructions displayed, see Sysout. 255 To have a message to the user be displayed, see Sysout. 256 Do not call anything in this dialog directly. 257 */ 258 class TestDialog extends Dialog { 259 260 TextArea instructionsText; 261 TextArea messageText; 262 int maxStringLength = 80; 263 264 //DO NOT call this directly, go through Sysout TestDialog( Frame frame, String name )265 public TestDialog( Frame frame, String name ) 266 { 267 super( frame, name ); 268 int scrollBoth = TextArea.SCROLLBARS_BOTH; 269 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 270 add( "North", instructionsText ); 271 272 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 273 add("Center", messageText); 274 275 pack(); 276 277 show(); 278 }// TestDialog() 279 280 //DO NOT call this directly, go through Sysout printInstructions( String[] instructions )281 public void printInstructions( String[] instructions ) 282 { 283 //Clear out any current instructions 284 instructionsText.setText( "" ); 285 286 //Go down array of instruction strings 287 288 String printStr, remainingStr; 289 for( int i=0; i < instructions.length; i++ ) 290 { 291 //chop up each into pieces maxSringLength long 292 remainingStr = instructions[ i ]; 293 while( remainingStr.length() > 0 ) 294 { 295 //if longer than max then chop off first max chars to print 296 if( remainingStr.length() >= maxStringLength ) 297 { 298 //Try to chop on a word boundary 299 int posOfSpace = remainingStr. 300 lastIndexOf( ' ', maxStringLength - 1 ); 301 302 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 303 304 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 305 remainingStr = remainingStr.substring( posOfSpace + 1 ); 306 } 307 //else just print 308 else 309 { 310 printStr = remainingStr; 311 remainingStr = ""; 312 } 313 314 instructionsText.append( printStr + "\n" ); 315 316 }// while 317 318 }// for 319 320 }//printInstructions() 321 322 //DO NOT call this directly, go through Sysout displayMessage( String messageIn )323 public void displayMessage( String messageIn ) 324 { 325 messageText.append( messageIn + "\n" ); 326 } 327 328 }// TestDialog class 329