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