PrintStreamTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.io; 19 20import java.io.ByteArrayInputStream; 21import java.io.ByteArrayOutputStream; 22import java.io.DataInputStream; 23import java.io.File; 24import java.io.FileNotFoundException; 25import java.io.IOException; 26import java.io.OutputStream; 27import java.io.PrintStream; 28import java.io.UnsupportedEncodingException; 29import java.util.IllegalFormatException; 30import java.util.Locale; 31 32import dalvik.annotation.TestLevel; 33import dalvik.annotation.TestTargetClass; 34import dalvik.annotation.TestTargetNew; 35 36@TestTargetClass(PrintStream.class) 37public class PrintStreamTest extends junit.framework.TestCase { 38 39 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 40 41 byte[] ibuf = new byte[4096]; 42 43 private File testFile = null; 44 private String testFilePath = null; 45 46 public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n"; 47 48 private static class MockPrintStream extends PrintStream { 49 50 public MockPrintStream(String fileName) throws FileNotFoundException { 51 super(fileName); 52 } 53 54 public MockPrintStream(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException { 55 super(fileName, csn); 56 } 57 58 public MockPrintStream(OutputStream os) { 59 super(os); 60 } 61 62 @Override 63 public void setError() { 64 super.setError(); 65 } 66 } 67 68 @TestTargetNew( 69 level = TestLevel.COMPLETE, 70 notes = "", 71 method = "PrintStream", 72 args = {java.io.File.class} 73 ) 74 public void test_Constructor_Ljava_io_File() throws IOException { 75 PrintStream tobj; 76 77 tobj = new PrintStream(testFile); 78 assertNotNull(tobj); 79 tobj.write(1); 80 tobj.close(); 81 assertEquals("output file has wrong length", 1, testFile.length()); 82 tobj = new PrintStream(testFile); 83 assertNotNull(tobj); 84 tobj.close(); 85 assertEquals("output file should be empty", 0, testFile.length()); 86 87 File file = new File("/invalidDirectory/Dummy"); 88 try { 89 tobj = new PrintStream(file); 90 fail("FileNotFoundException not thrown."); 91 } catch (FileNotFoundException e) { 92 // expected 93 } 94 } 95 96 @TestTargetNew( 97 level = TestLevel.COMPLETE, 98 notes = "", 99 method = "PrintStream", 100 args = {java.io.File.class, java.lang.String.class} 101 ) 102 public void test_Constructor_Ljava_io_File_Ljava_lang_String() throws Exception { 103 PrintStream tobj; 104 105 tobj = new PrintStream(testFile, "utf-8"); 106 assertNotNull(tobj); 107 tobj.write(1); 108 tobj.close(); 109 assertEquals("output file has wrong length", 1, testFile.length()); 110 tobj = new PrintStream(testFile, "utf-8"); 111 assertNotNull(tobj); 112 tobj.close(); 113 assertEquals("output file should be empty", 0, testFile.length()); 114 115 File file = new File("/invalidDirectory/Dummy"); 116 try { 117 tobj = new PrintStream(file, "utf-8"); 118 fail("FileNotFoundException not thrown."); 119 } catch (FileNotFoundException e) { 120 // expected 121 } 122 123 try { 124 tobj = new PrintStream(testFile, "invalidEncoding"); 125 fail("UnsupportedEncodingException not thrown."); 126 } catch (UnsupportedEncodingException e) { 127 // expected 128 } 129 } 130 131 /** 132 * @tests {@link java.io.PrintStream#PrintStream(String)} 133 */ 134 @TestTargetNew( 135 level = TestLevel.COMPLETE, 136 notes = "", 137 method = "PrintStream", 138 args = {java.lang.String.class} 139 ) 140 public void test_Constructor_Ljava_lang_String() throws IOException { 141 PrintStream tobj; 142 143 tobj = new PrintStream(testFilePath); 144 assertNotNull(tobj); 145 tobj.write(1); 146 tobj.close(); 147 assertEquals("output file has wrong length", 1, testFile.length()); 148 tobj = new PrintStream(testFilePath); 149 assertNotNull(tobj); 150 tobj.close(); 151 assertEquals("output file should be empty", 0, testFile.length()); 152 153 try { 154 tobj = new PrintStream("/invalidDirectory/Dummy"); 155 fail("FileNotFoundException not thrown."); 156 } catch (FileNotFoundException e) { 157 // expected 158 } 159 } 160 161 /** 162 * @tests {@link java.io.PrintStream#PrintStream(String, String)} 163 */ 164 @TestTargetNew( 165 level = TestLevel.COMPLETE, 166 notes = "", 167 method = "PrintStream", 168 args = {java.lang.String.class, java.lang.String.class} 169 ) 170 public void test_Constructor_Ljava_lang_String_Ljava_lang_String() throws Exception { 171 PrintStream tobj; 172 173 tobj = new PrintStream(testFilePath, "utf-8"); 174 assertNotNull(tobj); 175 tobj.write(1); 176 tobj.close(); 177 assertEquals("output file has wrong length", 1, testFile.length()); 178 tobj = new PrintStream(testFilePath, "utf-8"); 179 assertNotNull(tobj); 180 tobj.close(); 181 assertEquals("output file should be empty", 0, testFile.length()); 182 183 try { 184 tobj = new PrintStream("/invalidDirectory/", "utf-8"); 185 fail("FileNotFoundException not thrown."); 186 } catch (FileNotFoundException e) { 187 // expected 188 } 189 190 try { 191 tobj = new PrintStream(testFilePath, "invalidEncoding"); 192 fail("UnsupportedEncodingException not thrown."); 193 } catch (UnsupportedEncodingException e) { 194 // expected 195 } 196 } 197 198 /** 199 * @tests java.io.PrintStream#PrintStream(java.io.OutputStream) 200 */ 201 @TestTargetNew( 202 level = TestLevel.COMPLETE, 203 notes = "", 204 method = "PrintStream", 205 args = {java.io.OutputStream.class} 206 ) 207 public void test_ConstructorLjava_io_OutputStream() throws Exception { 208 // Test for method java.io.PrintStream(java.io.OutputStream) 209 PrintStream os = new PrintStream(baos); 210 os.print(2345.76834720202); 211 os.close(); 212 213 // regression for HARMONY-1195 214 try { 215 os = new PrintStream(baos, true, null); 216 fail("Should throw NPE"); 217 } catch (NullPointerException e) {} 218 } 219 220 /** 221 * @throws FileNotFoundException 222 * @tests java.io.PrintStream#PrintStream(java.io.OutputStream, boolean) 223 */ 224 @TestTargetNew( 225 level = TestLevel.SUFFICIENT, 226 notes = "Passing FALSE for autoFlush not tested.", 227 method = "PrintStream", 228 args = {java.io.OutputStream.class, boolean.class} 229 ) 230 public void test_ConstructorLjava_io_OutputStreamZ() throws FileNotFoundException { 231 PrintStream tobj; 232 233 tobj = new PrintStream(baos, true); 234 tobj.println(2345.76834720202); 235 assertTrue("Bytes not written", baos.size() > 0); 236 tobj.close(); 237 238// tobj = new PrintStream(bos, false); 239// tobj.println(2345.76834720202); 240// assertEquals("Bytes should not be written, yet", 0, bos.size()); 241// tobj.flush(); 242// assertTrue("Bytes not written", bos.size() > 0); 243// tobj.close(); 244 245// FileOutputStream fos = new FileOutputStream(testFile); 246// 247// tobj = new PrintStream(fos, true); 248// tobj.println(2345.76834720202); 249// assertTrue("Bytes not written", testFile.length() > 0); 250// tobj.close(); 251// 252// tobj = new PrintStream(fos, false); 253// tobj.println(2345.76834720202); 254// assertTrue("Bytes not written", testFile.length() > 0); 255// tobj.close(); 256// 257 } 258 259 @TestTargetNew( 260 level = TestLevel.COMPLETE, 261 notes = "", 262 method = "PrintStream", 263 args = {java.io.OutputStream.class, boolean.class, java.lang.String.class} 264 ) 265 public void test_ConstructorLjava_io_OutputStream_Z_Ljava_lang_String() { 266 PrintStream tobj; 267 268 tobj = new PrintStream(baos, true); 269 tobj.println(2345.76834720202); 270 assertTrue("Bytes not written", baos.size() > 0); 271 tobj.close(); 272 273 try { 274 tobj = new PrintStream(baos, true, "invalidEncoding"); 275 fail("UnsupportedEncodingException not thrown."); 276 } catch (UnsupportedEncodingException e) { 277 // expected 278 } 279 } 280 281 /** 282 * @tests java.io.PrintStream#checkError() 283 */ 284 @TestTargetNew( 285 level = TestLevel.COMPLETE, 286 notes = "", 287 method = "checkError", 288 args = {} 289 ) 290 public void test_checkError() throws Exception { 291 // Test for method boolean java.io.PrintStream.checkError() 292 PrintStream os = new PrintStream(new OutputStream() { 293 294 public void write(int b) throws IOException { 295 throw new IOException(); 296 } 297 298 public void write(byte[] b, int o, int l) throws IOException { 299 throw new IOException(); 300 } 301 }); 302 os.print(fileString.substring(0, 501)); 303 304 assertTrue("Checkerror should return true", os.checkError()); 305 } 306 307 /** 308 * @tests java.io.PrintStream#setError() 309 */ 310 @TestTargetNew( 311 level = TestLevel.COMPLETE, 312 method = "setError", 313 args = {} 314 ) 315 public void test_setError() throws Exception { 316 MockPrintStream os = new MockPrintStream(new ByteArrayOutputStream()); 317 assertFalse("Test 1: Error flag should not be set.", os.checkError()); 318 os.setError(); 319 assertTrue("Test 2: Error flag should be set.", os.checkError()); 320 } 321 322 /** 323 * @tests java.io.PrintStream#close() 324 */ 325 @TestTargetNew( 326 level = TestLevel.COMPLETE, 327 notes = "", 328 method = "close", 329 args = {} 330 ) 331 public void test_close() throws Exception { 332 // Test for method void java.io.PrintStream.close() 333 PrintStream os = new PrintStream(baos); 334 os.close(); 335 baos.close(); 336 } 337 338 /** 339 * @tests java.io.PrintStream#flush() 340 */ 341 @TestTargetNew( 342 level = TestLevel.COMPLETE, 343 notes = "", 344 method = "flush", 345 args = {} 346 ) 347 public void test_flush() throws Exception { 348 // Test for method void java.io.PrintStream.flush() 349 PrintStream os = new PrintStream(baos); 350 os.print(fileString.substring(0, 501)); 351 os.flush(); 352 assertEquals("Bytes not written after flush", 501, baos.size()); 353 baos.close(); 354 os.close(); 355 } 356 357 /** 358 * @tests java.io.PrintStream#print(char[]) 359 */ 360 @TestTargetNew( 361 level = TestLevel.COMPLETE, 362 notes = "", 363 method = "print", 364 args = {char[].class} 365 ) 366 public void test_print$C() { 367 // Test for method void java.io.PrintStream.print(char []) 368 PrintStream os = new PrintStream(baos, true); 369 try { 370 os.print((char[]) null); 371 fail("NPE expected"); 372 } catch (NullPointerException ok) {} 373 374 os = new PrintStream(baos, true); 375 char[] sc = new char[4000]; 376 fileString.getChars(0, fileString.length(), sc, 0); 377 os.print(sc); 378 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 379 os.close(); 380 381 byte[] rbytes = new byte[4000]; 382 bis.read(rbytes, 0, fileString.length()); 383 assertEquals("Incorrect char[] written", fileString, new String(rbytes, 384 0, fileString.length())); 385 } 386 387 /** 388 * @tests java.io.PrintStream#print(char) 389 */ 390 @TestTargetNew( 391 level = TestLevel.COMPLETE, 392 notes = "", 393 method = "print", 394 args = {char.class} 395 ) 396 public void test_printC() { 397 // Test for method void java.io.PrintStream.print(char) 398 PrintStream os = new PrintStream(baos, true); 399 os.print('t'); 400 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 401 assertEquals("Incorrect char written", 't', bis.read()); 402 } 403 404 /** 405 * @tests java.io.PrintStream#print(double) 406 */ 407 @TestTargetNew( 408 level = TestLevel.COMPLETE, 409 notes = "", 410 method = "print", 411 args = {double.class} 412 ) 413 public void test_printD() { 414 // Test for method void java.io.PrintStream.print(double) 415 byte[] rbuf = new byte[100]; 416 PrintStream os = new PrintStream(baos, true); 417 os.print(2345.76834720202); 418 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 419 bis.read(rbuf, 0, 16); 420 assertEquals("Incorrect double written", "2345.76834720202", 421 new String(rbuf, 0, 16)); 422 } 423 424 /** 425 * @tests java.io.PrintStream#print(float) 426 */ 427 @TestTargetNew( 428 level = TestLevel.COMPLETE, 429 notes = "", 430 method = "print", 431 args = {float.class} 432 ) 433 public void test_printF() { 434 // Test for method void java.io.PrintStream.print(float) 435 PrintStream os = new PrintStream(baos, true); 436 byte rbuf[] = new byte[10]; 437 os.print(29.08764f); 438 os.flush(); 439 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 440 bis.read(rbuf, 0, 8); 441 assertEquals("Incorrect float written", "29.08764", new String(rbuf, 0, 442 8)); 443 444 } 445 446 /** 447 * @tests java.io.PrintStream#print(int) 448 */ 449 @TestTargetNew( 450 level = TestLevel.COMPLETE, 451 notes = "", 452 method = "print", 453 args = {int.class} 454 ) 455 public void test_printI() { 456 // Test for method void java.io.PrintStream.print(int) 457 PrintStream os = new PrintStream(baos, true); 458 os.print(768347202); 459 byte[] rbuf = new byte[18]; 460 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 461 bis.read(rbuf, 0, 9); 462 assertEquals("Incorrect int written", "768347202", new String(rbuf, 0, 463 9)); 464 } 465 466 /** 467 * @tests java.io.PrintStream#print(long) 468 */ 469 @TestTargetNew( 470 level = TestLevel.COMPLETE, 471 notes = "", 472 method = "print", 473 args = {long.class} 474 ) 475 public void test_printJ() { 476 // Test for method void java.io.PrintStream.print(long) 477 byte[] rbuf = new byte[100]; 478 PrintStream os = new PrintStream(baos, true); 479 os.print(9875645283333L); 480 os.close(); 481 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 482 bis.read(rbuf, 0, 13); 483 assertEquals("Incorrect long written", "9875645283333", new String( 484 rbuf, 0, 13)); 485 } 486 487 /** 488 * @tests java.io.PrintStream#print(java.lang.Object) 489 */ 490 @TestTargetNew( 491 level = TestLevel.COMPLETE, 492 notes = "", 493 method = "print", 494 args = {java.lang.Object.class} 495 ) 496 public void test_printLjava_lang_Object() throws Exception { 497 // Test for method void java.io.PrintStream.print(java.lang.Object) 498 PrintStream os = new PrintStream(baos, true); 499 os.print((Object) null); 500 os.flush(); 501 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 502 byte[] nullbytes = new byte[4]; 503 bis.read(nullbytes, 0, 4); 504 assertEquals("null should be written", "null", new String(nullbytes, 0, 505 4)); 506 507 bis.close(); 508 baos.close(); 509 os.close(); 510 511 ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); 512 os = new PrintStream(bos1, true); 513 os.print(new java.util.Vector()); 514 bis = new ByteArrayInputStream(bos1.toByteArray()); 515 byte[] rbytes = new byte[2]; 516 bis.read(rbytes, 0, 2); 517 assertEquals("Incorrect Object written", "[]", new String(rbytes, 0, 2)); 518 } 519 520 /** 521 * @tests java.io.PrintStream#print(java.lang.String) 522 */ 523 @TestTargetNew( 524 level = TestLevel.COMPLETE, 525 notes = "", 526 method = "print", 527 args = {java.lang.String.class} 528 ) 529 public void test_printLjava_lang_String() throws Exception { 530 // Test for method void java.io.PrintStream.print(java.lang.String) 531 PrintStream os = new PrintStream(baos, true); 532 os.print((String) null); 533 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 534 byte[] nullbytes = new byte[4]; 535 bis.read(nullbytes, 0, 4); 536 assertEquals("null should be written", "null", new String(nullbytes, 0, 537 4)); 538 539 bis.close(); 540 baos.close(); 541 os.close(); 542 543 ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); 544 os = new PrintStream(bos1, true); 545 os.print("Hello World"); 546 bis = new ByteArrayInputStream(bos1.toByteArray()); 547 byte rbytes[] = new byte[100]; 548 bis.read(rbytes, 0, 11); 549 assertEquals("Incorrect string written", "Hello World", new String( 550 rbytes, 0, 11)); 551 } 552 553 /** 554 * @tests java.io.PrintStream#print(boolean) 555 */ 556 @TestTargetNew( 557 level = TestLevel.COMPLETE, 558 notes = "", 559 method = "print", 560 args = {boolean.class} 561 ) 562 public void test_printZ() throws Exception { 563 // Test for method void java.io.PrintStream.print(boolean) 564 PrintStream os = new PrintStream(baos, true); 565 os.print(true); 566 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(baos 567 .toByteArray())); 568 569 assertTrue("Incorrect boolean written", dis.readBoolean()); 570 } 571 572 /** 573 * @tests java.io.PrintStream#println() 574 */ 575 @TestTargetNew( 576 level = TestLevel.COMPLETE, 577 notes = "", 578 method = "println", 579 args = {} 580 ) 581 public void test_println() { 582 // Test for method void java.io.PrintStream.println() 583 char c; 584 PrintStream os = new PrintStream(baos, true); 585 os.println(""); 586 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 587 assertTrue("Newline not written", (c = (char) bis.read()) == '\r' 588 || c == '\n'); 589 } 590 591 /** 592 * @tests java.io.PrintStream#println(char[]) 593 */ 594 @TestTargetNew( 595 level = TestLevel.COMPLETE, 596 notes = "", 597 method = "println", 598 args = {char[].class} 599 ) 600 public void test_println$C() { 601 // Test for method void java.io.PrintStream.println(char []) 602 PrintStream os = new PrintStream(baos, true); 603 char[] sc = new char[4000]; 604 fileString.getChars(0, fileString.length(), sc, 0); 605 os.println(sc); 606 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 607 byte[] rbytes = new byte[4000]; 608 bis.read(rbytes, 0, fileString.length()); 609 assertEquals("Incorrect char[] written", fileString, new String(rbytes, 610 0, fileString.length())); 611 612 // In this particular test method, the end of data is not immediately 613 // followed by newLine separator in the reading buffer, instead its 614 // followed by zeros. The newline is written as the last entry 615 // in the inputStream buffer. Therefore, we must keep reading until we 616 // hit a new line. 617 int r; 618 boolean newline = false; 619 while ((r = bis.read()) != -1) { 620 if (r == '\r' || r == '\n') 621 newline = true; 622 } 623 assertTrue("Newline not written", newline); 624 } 625 626 /** 627 * @tests java.io.PrintStream#println(char) 628 */ 629 @TestTargetNew( 630 level = TestLevel.COMPLETE, 631 notes = "", 632 method = "println", 633 args = {char.class} 634 ) 635 public void test_printlnC() { 636 // Test for method void java.io.PrintStream.println(char) 637 int c; 638 PrintStream os = new PrintStream(baos, true); 639 os.println('t'); 640 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 641 assertEquals("Incorrect char written", 't', bis.read()); 642 assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n'); 643 } 644 645 /** 646 * @tests java.io.PrintStream#println(double) 647 */ 648 @TestTargetNew( 649 level = TestLevel.COMPLETE, 650 notes = "", 651 method = "println", 652 args = {double.class} 653 ) 654 public void test_printlnD() { 655 // Test for method void java.io.PrintStream.println(double) 656 int c; 657 PrintStream os = new PrintStream(baos, true); 658 os.println(2345.76834720202); 659 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 660 byte[] rbuf = new byte[100]; 661 bis.read(rbuf, 0, 16); 662 assertEquals("Incorrect double written", "2345.76834720202", 663 new String(rbuf, 0, 16)); 664 assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n'); 665 } 666 667 /** 668 * @tests java.io.PrintStream#println(float) 669 */ 670 @TestTargetNew( 671 level = TestLevel.COMPLETE, 672 notes = "", 673 method = "println", 674 args = {float.class} 675 ) 676 public void test_printlnF() { 677 // Test for method void java.io.PrintStream.println(float) 678 int c; 679 byte[] rbuf = new byte[100]; 680 PrintStream os = new PrintStream(baos, true); 681 os.println(29.08764f); 682 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 683 bis.read(rbuf, 0, 8); 684 assertEquals("Incorrect float written", "29.08764", new String(rbuf, 0, 685 8)); 686 assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n'); 687 } 688 689 /** 690 * @tests java.io.PrintStream#println(int) 691 */ 692 @TestTargetNew( 693 level = TestLevel.COMPLETE, 694 notes = "", 695 method = "println", 696 args = {int.class} 697 ) 698 public void test_printlnI() { 699 // Test for method void java.io.PrintStream.println(int) 700 int c; 701 PrintStream os = new PrintStream(baos, true); 702 os.println(768347202); 703 byte[] rbuf = new byte[100]; 704 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 705 bis.read(rbuf, 0, 9); 706 assertEquals("Incorrect int written", "768347202", new String(rbuf, 0, 707 9)); 708 assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n'); 709 } 710 711 /** 712 * @tests java.io.PrintStream#println(long) 713 */ 714 @TestTargetNew( 715 level = TestLevel.COMPLETE, 716 notes = "", 717 method = "println", 718 args = {long.class} 719 ) 720 public void test_printlnJ() { 721 // Test for method void java.io.PrintStream.println(long) 722 int c; 723 PrintStream os = new PrintStream(baos, true); 724 os.println(9875645283333L); 725 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 726 byte[] rbuf = new byte[100]; 727 bis.read(rbuf, 0, 13); 728 assertEquals("Incorrect long written", "9875645283333", new String( 729 rbuf, 0, 13)); 730 assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n'); 731 } 732 733 /** 734 * @tests java.io.PrintStream#println(java.lang.Object) 735 */ 736 @TestTargetNew( 737 level = TestLevel.COMPLETE, 738 notes = "", 739 method = "println", 740 args = {java.lang.Object.class} 741 ) 742 public void test_printlnLjava_lang_Object() { 743 // Test for method void java.io.PrintStream.println(java.lang.Object) 744 char c; 745 PrintStream os = new PrintStream(baos, true); 746 os.println(new java.util.Vector()); 747 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 748 byte[] rbytes = new byte[2]; 749 bis.read(rbytes, 0, 2); 750 assertEquals("Incorrect Vector written", "[]", new String(rbytes, 0, 2)); 751 assertTrue("Newline not written", (c = (char) bis.read()) == '\r' 752 || c == '\n'); 753 } 754 755 /** 756 * @tests java.io.PrintStream#println(java.lang.String) 757 */ 758 @TestTargetNew( 759 level = TestLevel.COMPLETE, 760 notes = "", 761 method = "println", 762 args = {java.lang.String.class} 763 ) 764 public void test_printlnLjava_lang_String() { 765 // Test for method void java.io.PrintStream.println(java.lang.String) 766 char c; 767 PrintStream os = new PrintStream(baos, true); 768 os.println("Hello World"); 769 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 770 byte rbytes[] = new byte[100]; 771 bis.read(rbytes, 0, 11); 772 assertEquals("Incorrect string written", "Hello World", new String( 773 rbytes, 0, 11)); 774 assertTrue("Newline not written", (c = (char) bis.read()) == '\r' 775 || c == '\n'); 776 } 777 778 /** 779 * @tests java.io.PrintStream#println(boolean) 780 */ 781 @TestTargetNew( 782 level = TestLevel.COMPLETE, 783 notes = "", 784 method = "println", 785 args = {boolean.class} 786 ) 787 public void test_printlnZ() { 788 // Test for method void java.io.PrintStream.println(boolean) 789 int c; 790 PrintStream os = new PrintStream(baos, true); 791 os.println(true); 792 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 793 byte[] rbuf = new byte[100]; 794 bis.read(rbuf, 0, 4); 795 assertEquals("Incorrect boolean written", "true", 796 new String(rbuf, 0, 4)); 797 assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n'); 798 } 799 800 /** 801 * @tests java.io.PrintStream#write(byte[], int, int) 802 */ 803 @TestTargetNew( 804 level = TestLevel.COMPLETE, 805 notes = "", 806 method = "write", 807 args = {byte[].class, int.class, int.class} 808 ) 809 public void test_write$BII() { 810 // Test for method void java.io.PrintStream.write(byte [], int, int) 811 PrintStream os = new PrintStream(baos, true); 812 os.write(fileString.getBytes(), 0, fileString.length()); 813 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 814 byte rbytes[] = new byte[4000]; 815 bis.read(rbytes, 0, fileString.length()); 816 assertTrue("Incorrect bytes written", new String(rbytes, 0, fileString 817 .length()).equals(fileString)); 818 } 819 820 /** 821 * @tests java.io.PrintStream#write(int) 822 */ 823 @TestTargetNew( 824 level = TestLevel.COMPLETE, 825 notes = "", 826 method = "write", 827 args = {int.class} 828 ) 829 public void test_writeI() { 830 // Test for method void java.io.PrintStream.write(int) 831 PrintStream os = new PrintStream(baos, true); 832 os.write('t'); 833 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 834 assertEquals("Incorrect char written", 't', bis.read()); 835 } 836 837 /** 838 * @tests java.io.PrintStream#append(char) 839 */ 840 @TestTargetNew( 841 level = TestLevel.COMPLETE, 842 notes = "", 843 method = "append", 844 args = {char.class} 845 ) 846 public void test_appendChar() throws IOException { 847 char testChar = ' '; 848 ByteArrayOutputStream out = new ByteArrayOutputStream(); 849 PrintStream printStream = new PrintStream(out); 850 printStream.append(testChar); 851 printStream.flush(); 852 assertEquals(String.valueOf(testChar), out.toString()); 853 printStream.close(); 854 } 855 856 /** 857 * @tests java.io.PrintStream#append(CharSequence) 858 */ 859 @TestTargetNew( 860 level = TestLevel.COMPLETE, 861 notes = "", 862 method = "append", 863 args = {java.lang.CharSequence.class} 864 ) 865 public void test_appendCharSequence() { 866 String testString = "My Test String"; 867 ByteArrayOutputStream out = new ByteArrayOutputStream(); 868 PrintStream printStream = new PrintStream(out); 869 printStream.append(testString); 870 printStream.flush(); 871 assertEquals(testString, out.toString()); 872 printStream.close(); 873 } 874 875 /** 876 * @tests java.io.PrintStream#append(CharSequence, int, int) 877 */ 878 @TestTargetNew( 879 level = TestLevel.COMPLETE, 880 notes = "", 881 method = "append", 882 args = {java.lang.CharSequence.class, int.class, int.class} 883 ) 884 public void test_appendCharSequenceIntInt() { 885 String testString = "My Test String"; 886 ByteArrayOutputStream out = new ByteArrayOutputStream(); 887 PrintStream printStream = new PrintStream(out); 888 printStream.append(testString, 1, 3); 889 printStream.flush(); 890 assertEquals(testString.substring(1, 3), out.toString()); 891 try { 892 printStream.append(testString, 4, 100); 893 fail("IndexOutOfBoundsException not thrown"); 894 } catch (IndexOutOfBoundsException e) { 895 // expected 896 } 897 try { 898 printStream.append(testString, 100, 1); 899 fail("IndexOutOfBoundsException not thrown"); 900 } catch (IndexOutOfBoundsException e) { 901 // expected 902 } 903 printStream.close(); 904 } 905 906 /** 907 * @tests java.io.PrintStream#format(java.lang.String, java.lang.Object...) 908 */ 909 @TestTargetNew( 910 level = TestLevel.COMPLETE, 911 notes = "", 912 method = "format", 913 args = {java.lang.String.class, java.lang.Object[].class} 914 ) 915 public void test_formatLjava_lang_String$Ljava_lang_Object() { 916 PrintStream tobj; 917 918 tobj = new PrintStream(baos, false); 919 tobj.format("%s %s", "Hello", "World"); 920 tobj.flush(); 921 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 922 byte[] rbytes = new byte[11]; 923 bis.read(rbytes, 0, rbytes.length); 924 assertEquals("Wrote incorrect string", "Hello World", 925 new String(rbytes)); 926 927 baos.reset(); 928 tobj = new PrintStream(baos); 929 tobj.format("%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456); 930 tobj.flush(); 931 assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray())); 932 tobj.close(); 933 934 baos.reset(); 935 tobj = new PrintStream(baos); 936 try { 937 tobj.format("%1$.3G, %1$x", 12345.678); 938 fail("IllegalFormatException not thrown"); 939 } catch (IllegalFormatException e) { 940 // expected 941 } 942 943 try { 944 tobj.format("%s %q", "Hello", "World"); 945 fail("IllegalFormatException not thrown"); 946 } catch (IllegalFormatException e) { 947 // expected 948 } 949 950 try { 951 tobj.format("%s %s", "Hello"); 952 fail("IllegalFormatException not thrown"); 953 } catch (IllegalFormatException e) { 954 // expected 955 } 956 } 957 958 /** 959 * @tests java.io.PrintStream#format(java.util.Locale, java.lang.String, 960 * java.lang.Object...) 961 */ 962 @TestTargetNew( 963 level = TestLevel.COMPLETE, 964 notes = "", 965 method = "format", 966 args = {java.util.Locale.class, java.lang.String.class, java.lang.Object[].class} 967 ) 968 public void test_formatLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() { 969 PrintStream tobj; 970 971 tobj = new PrintStream(baos, false); 972 tobj.format(Locale.US, "%s %s", "Hello", "World"); 973 tobj.flush(); 974 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 975 byte[] rbytes = new byte[11]; 976 bis.read(rbytes, 0, rbytes.length); 977 assertEquals("Wrote incorrect string", "Hello World", 978 new String(rbytes)); 979 980 baos.reset(); 981 tobj = new PrintStream(baos); 982 tobj.format(Locale.GERMANY, "%1$.3G; %1$.5f; 0%2$xx", 12345.678, 123456); 983 tobj.flush(); 984 assertEquals("Wrong output!", "1,23E+04; 12345,67800; 01e240x", new String(baos.toByteArray())); 985 tobj.close(); 986 987 baos.reset(); 988 tobj = new PrintStream(baos); 989 tobj.format(Locale.US, "%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456); 990 tobj.flush(); 991 assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray())); 992 tobj.close(); 993 994 baos.reset(); 995 tobj = new PrintStream(baos); 996 try { 997 tobj.format(Locale.US, "%1$.3G, %1$x", 12345.678); 998 fail("IllegalFormatException not thrown"); 999 } catch (IllegalFormatException e) { 1000 // expected 1001 } 1002 1003 try { 1004 tobj.format(Locale.US, "%s %q", "Hello", "World"); 1005 fail("IllegalFormatException not thrown"); 1006 } catch (IllegalFormatException e) { 1007 // expected 1008 } 1009 1010 try { 1011 tobj.format(Locale.US, "%s %s", "Hello"); 1012 fail("IllegalFormatException not thrown"); 1013 } catch (IllegalFormatException e) { 1014 // expected 1015 } 1016 } 1017 1018 /** 1019 * @tests java.io.PrintStream#printf(java.lang.String, java.lang.Object...) 1020 */ 1021 @TestTargetNew( 1022 level = TestLevel.COMPLETE, 1023 notes = "", 1024 method = "printf", 1025 args = {java.lang.String.class, java.lang.Object[].class} 1026 ) 1027 public void test_printfLjava_lang_String$Ljava_lang_Object() { 1028 PrintStream tobj; 1029 1030 tobj = new PrintStream(baos, false); 1031 tobj.printf("%s %s", "Hello", "World"); 1032 tobj.flush(); 1033 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 1034 byte[] rbytes = new byte[11]; 1035 bis.read(rbytes, 0, rbytes.length); 1036 assertEquals("Wrote incorrect string", "Hello World", 1037 new String(rbytes)); 1038 1039 baos.reset(); 1040 tobj = new PrintStream(baos); 1041 tobj.printf("%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456); 1042 tobj.flush(); 1043 assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray())); 1044 tobj.close(); 1045 1046 baos.reset(); 1047 tobj = new PrintStream(baos); 1048 try { 1049 tobj.printf("%1$.3G, %1$x", 12345.678); 1050 fail("IllegalFormatException not thrown"); 1051 } catch (IllegalFormatException e) { 1052 // expected 1053 } 1054 1055 try { 1056 tobj.printf("%s %q", "Hello", "World"); 1057 fail("IllegalFormatException not thrown"); 1058 } catch (IllegalFormatException e) { 1059 // expected 1060 } 1061 1062 try { 1063 tobj.printf("%s %s", "Hello"); 1064 fail("IllegalFormatException not thrown"); 1065 } catch (IllegalFormatException e) { 1066 // expected 1067 } 1068 } 1069 1070 /** 1071 * @tests java.io.PrintStream#printf(java.util.Locale, java.lang.String, 1072 * java.lang.Object...) 1073 */ 1074 @TestTargetNew( 1075 level = TestLevel.COMPLETE, 1076 notes = "", 1077 method = "printf", 1078 args = {java.util.Locale.class, java.lang.String.class, java.lang.Object[].class} 1079 ) 1080 public void test_printfLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() { 1081 PrintStream tobj; 1082 1083 tobj = new PrintStream(baos, false); 1084 tobj.printf(Locale.US, "%s %s", "Hello", "World"); 1085 tobj.flush(); 1086 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 1087 byte[] rbytes = new byte[11]; 1088 bis.read(rbytes, 0, rbytes.length); 1089 assertEquals("Wrote incorrect string", "Hello World", 1090 new String(rbytes)); 1091 1092 baos.reset(); 1093 tobj = new PrintStream(baos); 1094 tobj.printf(Locale.GERMANY, "%1$.3G; %1$.5f; 0%2$xx", 12345.678, 123456); 1095 tobj.flush(); 1096 assertEquals("Wrong output!", "1,23E+04; 12345,67800; 01e240x", new String(baos.toByteArray())); 1097 tobj.close(); 1098 1099 baos.reset(); 1100 tobj = new PrintStream(baos); 1101 tobj.printf(Locale.US, "%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456); 1102 tobj.flush(); 1103 assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray())); 1104 tobj.close(); 1105 1106 baos.reset(); 1107 tobj = new PrintStream(baos); 1108 try { 1109 tobj.printf(Locale.US, "%1$.3G, %1$x", 12345.678); 1110 fail("IllegalFormatException not thrown"); 1111 } catch (IllegalFormatException e) { 1112 // expected 1113 } 1114 1115 try { 1116 tobj.printf(Locale.US, "%s %q", "Hello", "World"); 1117 fail("IllegalFormatException not thrown"); 1118 } catch (IllegalFormatException e) { 1119 // expected 1120 } 1121 1122 try { 1123 tobj.printf(Locale.US, "%s %s", "Hello"); 1124 fail("IllegalFormatException not thrown"); 1125 } catch (IllegalFormatException e) { 1126 // expected 1127 } 1128 } 1129 1130 @Override 1131 protected void setUp() throws Exception { 1132 super.setUp(); 1133 testFile = File.createTempFile("test", null); 1134 testFilePath = testFile.getAbsolutePath(); 1135 } 1136 1137 @Override 1138 protected void tearDown() throws Exception { 1139 testFile.delete(); 1140 testFile = null; 1141 testFilePath = null; 1142 super.tearDown(); 1143 } 1144 1145 1146} 1147