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 18 package tests.api.java.io; 19 20 import java.io.ByteArrayInputStream; 21 import java.io.ByteArrayOutputStream; 22 import java.io.InputStreamReader; 23 import java.io.DataInputStream; 24 import java.io.File; 25 import java.io.FileNotFoundException; 26 import java.io.IOException; 27 import java.io.OutputStream; 28 import java.io.PrintStream; 29 import java.io.UnsupportedEncodingException; 30 import java.util.Locale; 31 32 public class PrintStreamTest extends junit.framework.TestCase { 33 34 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 35 36 byte[] ibuf = new byte[4096]; 37 38 private File testFile = null; 39 40 private String testFilePath = null; 41 42 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"; 43 44 private static class MockPrintStream extends PrintStream { 45 46 public MockPrintStream(String fileName) throws FileNotFoundException { 47 super(fileName); 48 } 49 50 public MockPrintStream(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException { 51 super(fileName, csn); 52 } 53 54 public MockPrintStream(OutputStream os) { 55 super(os); 56 } 57 58 @Override 59 public void clearError() { 60 super.clearError(); 61 } 62 63 @Override 64 public void setError() { 65 super.setError(); 66 } 67 } 68 69 /** 70 * @tests {@link java.io.PrintStream#PrintStream(String)} 71 */ 72 public void test_Constructor_Ljava_lang_String() throws IOException { 73 MockPrintStream os = new MockPrintStream(testFilePath); 74 assertNotNull(os); 75 os.close(); 76 } 77 78 /** 79 * @tests {@link java.io.PrintStream#PrintStream(String, String)} 80 */ 81 public void test_Constructor_Ljava_lang_String_Ljava_lang_String() throws Exception { 82 MockPrintStream os = new MockPrintStream(testFilePath, "utf-8"); 83 assertNotNull(os); 84 os.close(); 85 } 86 87 /** 88 * @tests java.io.PrintStream#PrintStream(java.io.OutputStream) 89 */ 90 public void test_ConstructorLjava_io_OutputStream() throws Exception { 91 // Test for method java.io.PrintStream(java.io.OutputStream) 92 PrintStream os = new PrintStream(bos); 93 os.print(2345.76834720202); 94 os.close(); 95 96 // regression for HARMONY-1195 97 try { 98 os = new PrintStream(bos, true, null); 99 fail("Should throw NPE"); 100 } catch (NullPointerException e) {} 101 } 102 103 /** 104 * @tests java.io.PrintStream#PrintStream(java.io.OutputStream, boolean) 105 */ 106 public void test_ConstructorLjava_io_OutputStreamZ() { 107 // Test for method java.io.PrintStream(java.io.OutputStream, boolean) 108 PrintStream os = new PrintStream(bos); 109 os.println(2345.76834720202); 110 os.flush(); 111 assertTrue("Bytes not written", bos.size() > 0); 112 os.close(); 113 } 114 115 /** 116 * @tests java.io.PrintStream#PrintStream(java.io.OutputStream, boolean, String) 117 */ 118 public void test_ConstructorLjava_io_OutputStreamZLjava_lang_String() { 119 try { 120 new PrintStream(new ByteArrayOutputStream(), false, 121 "%Illegal_name!"); 122 fail("Expected UnsupportedEncodingException"); 123 } catch (UnsupportedEncodingException e) { 124 // expected 125 } 126 } 127 128 /** 129 * @tests java.io.PrintStream#checkError() 130 */ 131 public void test_checkError() throws Exception { 132 // Test for method boolean java.io.PrintStream.checkError() 133 PrintStream os = new PrintStream(new OutputStream() { 134 135 public void write(int b) throws IOException { 136 throw new IOException(); 137 } 138 139 public void write(byte[] b, int o, int l) throws IOException { 140 throw new IOException(); 141 } 142 }); 143 os.print(fileString.substring(0, 501)); 144 145 assertTrue("Checkerror should return true", os.checkError()); 146 } 147 148 /** 149 * @tests {@link java.io.PrintStream#clearError()} 150 */ 151 public void test_clearError() throws FileNotFoundException { 152 MockPrintStream os = new MockPrintStream(testFilePath); 153 assertFalse(os.checkError()); 154 os.setError(); 155 assertTrue(os.checkError()); 156 os.clearError(); 157 assertFalse(os.checkError()); 158 os.close(); 159 } 160 161 /** 162 * @tests java.io.PrintStream#close() 163 */ 164 public void test_close() throws Exception { 165 // Test for method void java.io.PrintStream.close() 166 PrintStream os = new PrintStream(bos); 167 os.close(); 168 bos.close(); 169 } 170 171 /** 172 * @tests java.io.PrintStream#flush() 173 */ 174 public void test_flush() throws Exception { 175 // Test for method void java.io.PrintStream.flush() 176 PrintStream os = new PrintStream(bos); 177 os.print(fileString.substring(0, 501)); 178 os.flush(); 179 assertEquals("Bytes not written after flush", 501, bos.size()); 180 bos.close(); 181 os.close(); 182 } 183 184 /** 185 * @tests java.io.PrintStream#print(char[]) 186 */ 187 public void test_print$C() { 188 // Test for method void java.io.PrintStream.print(char []) 189 PrintStream os = new PrintStream(bos, true); 190 try { 191 os.print((char[]) null); 192 fail("NPE expected"); 193 } catch (NullPointerException ok) {} 194 195 os = new PrintStream(bos, true); 196 char[] sc = new char[4000]; 197 fileString.getChars(0, fileString.length(), sc, 0); 198 os.print(sc); 199 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 200 os.close(); 201 202 byte[] rbytes = new byte[4000]; 203 bis.read(rbytes, 0, fileString.length()); 204 assertEquals("Incorrect char[] written", fileString, new String(rbytes, 205 0, fileString.length())); 206 } 207 208 /** 209 * @tests java.io.PrintStream#print(char) 210 */ 211 public void test_printC() throws Exception { 212 // Test for method void java.io.PrintStream.print(char) 213 PrintStream os = new PrintStream(bos, true); 214 os.print('t'); 215 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 216 InputStreamReader isr = new InputStreamReader(bis); 217 assertEquals("Incorrect char written", 't', isr.read()); 218 } 219 220 /** 221 * @tests java.io.PrintStream#print(double) 222 */ 223 public void test_printD() { 224 // Test for method void java.io.PrintStream.print(double) 225 byte[] rbuf = new byte[100]; 226 PrintStream os = new PrintStream(bos, true); 227 os.print(2345.76834720202); 228 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 229 bis.read(rbuf, 0, 16); 230 assertEquals("Incorrect double written", "2345.76834720202", 231 new String(rbuf, 0, 16)); 232 } 233 234 /** 235 * @tests java.io.PrintStream#print(float) 236 */ 237 public void test_printF() { 238 // Test for method void java.io.PrintStream.print(float) 239 PrintStream os = new PrintStream(bos, true); 240 byte rbuf[] = new byte[10]; 241 os.print(29.08764f); 242 os.flush(); 243 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 244 bis.read(rbuf, 0, 8); 245 assertEquals("Incorrect float written", "29.08764", new String(rbuf, 0, 246 8)); 247 248 } 249 250 /** 251 * @tests java.io.PrintStream#print(int) 252 */ 253 public void test_printI() { 254 // Test for method void java.io.PrintStream.print(int) 255 PrintStream os = new PrintStream(bos, true); 256 os.print(768347202); 257 byte[] rbuf = new byte[18]; 258 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 259 bis.read(rbuf, 0, 9); 260 assertEquals("Incorrect int written", "768347202", new String(rbuf, 0, 261 9)); 262 } 263 264 /** 265 * @tests java.io.PrintStream#print(long) 266 */ 267 public void test_printJ() { 268 // Test for method void java.io.PrintStream.print(long) 269 byte[] rbuf = new byte[100]; 270 PrintStream os = new PrintStream(bos, true); 271 os.print(9875645283333L); 272 os.close(); 273 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 274 bis.read(rbuf, 0, 13); 275 assertEquals("Incorrect long written", "9875645283333", new String( 276 rbuf, 0, 13)); 277 } 278 279 /** 280 * @tests java.io.PrintStream#print(java.lang.Object) 281 */ 282 public void test_printLjava_lang_Object() throws Exception { 283 // Test for method void java.io.PrintStream.print(java.lang.Object) 284 PrintStream os = new PrintStream(bos, true); 285 os.print((Object) null); 286 os.flush(); 287 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 288 byte[] nullbytes = new byte[4]; 289 bis.read(nullbytes, 0, 4); 290 assertEquals("null should be written", "null", new String(nullbytes, 0, 291 4)); 292 293 bis.close(); 294 bos.close(); 295 os.close(); 296 297 ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); 298 os = new PrintStream(bos1, true); 299 os.print(new java.util.Vector()); 300 bis = new ByteArrayInputStream(bos1.toByteArray()); 301 byte[] rbytes = new byte[2]; 302 bis.read(rbytes, 0, 2); 303 assertEquals("Incorrect Object written", "[]", new String(rbytes, 0, 2)); 304 } 305 306 /** 307 * @tests java.io.PrintStream#print(java.lang.String) 308 */ 309 public void test_printLjava_lang_String() throws Exception { 310 // Test for method void java.io.PrintStream.print(java.lang.String) 311 PrintStream os = new PrintStream(bos, true); 312 os.print((String) null); 313 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 314 byte[] nullbytes = new byte[4]; 315 bis.read(nullbytes, 0, 4); 316 assertEquals("null should be written", "null", new String(nullbytes, 0, 317 4)); 318 319 bis.close(); 320 bos.close(); 321 os.close(); 322 323 ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); 324 os = new PrintStream(bos1, true); 325 os.print("Hello World"); 326 bis = new ByteArrayInputStream(bos1.toByteArray()); 327 byte rbytes[] = new byte[100]; 328 bis.read(rbytes, 0, 11); 329 assertEquals("Incorrect string written", "Hello World", new String( 330 rbytes, 0, 11)); 331 } 332 333 /** 334 * @tests java.io.PrintStream#print(boolean) 335 */ 336 public void test_printZ() throws Exception { 337 // Test for method void java.io.PrintStream.print(boolean) 338 PrintStream os = new PrintStream(bos, true); 339 os.print(true); 340 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bos 341 .toByteArray())); 342 343 assertTrue("Incorrect boolean written", dis.readBoolean()); 344 } 345 346 /** 347 * @tests java.io.PrintStream#println() 348 */ 349 public void test_println() throws Exception { 350 // Test for method void java.io.PrintStream.println() 351 char c; 352 PrintStream os = new PrintStream(bos, true); 353 os.println(""); 354 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 355 InputStreamReader isr = new InputStreamReader(bis); 356 assertTrue("Newline not written", (c = (char) isr.read()) == '\r' 357 || c == '\n'); 358 } 359 360 /** 361 * @tests java.io.PrintStream#println(char[]) 362 */ 363 public void test_println$C() throws Exception { 364 // Test for method void java.io.PrintStream.println(char []) 365 PrintStream os = new PrintStream(bos, true); 366 char[] sc = new char[4000]; 367 fileString.getChars(0, fileString.length(), sc, 0); 368 os.println(sc); 369 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 370 InputStreamReader isr = new InputStreamReader(bis); 371 byte[] rbytes = new byte[4000]; 372 bis.read(rbytes, 0, fileString.length()); 373 assertEquals("Incorrect char[] written", fileString, new String(rbytes, 374 0, fileString.length())); 375 376 // In this particular test method, the end of data is not immediately 377 // followed by newLine separator in the reading buffer, instead its 378 // followed by zeros. The newline is written as the last entry 379 // in the inputStream buffer. Therefore, we must keep reading until we 380 // hit a new line. 381 int r; 382 boolean newline = false; 383 while ((r = isr.read()) != -1) { 384 if (r == '\r' || r == '\n') 385 newline = true; 386 } 387 assertTrue("Newline not written", newline); 388 } 389 390 /** 391 * @tests java.io.PrintStream#println(char) 392 */ 393 public void test_printlnC() throws Exception { 394 // Test for method void java.io.PrintStream.println(char) 395 int c; 396 PrintStream os = new PrintStream(bos, true); 397 os.println('t'); 398 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 399 InputStreamReader isr = new InputStreamReader(bis); 400 assertEquals("Incorrect char written", 't', isr.read()); 401 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n'); 402 } 403 404 /** 405 * @tests java.io.PrintStream#println(double) 406 */ 407 public void test_printlnD() throws Exception { 408 // Test for method void java.io.PrintStream.println(double) 409 int c; 410 PrintStream os = new PrintStream(bos, true); 411 os.println(2345.76834720202); 412 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 413 InputStreamReader isr = new InputStreamReader(bis); 414 byte[] rbuf = new byte[100]; 415 bis.read(rbuf, 0, 16); 416 assertEquals("Incorrect double written", "2345.76834720202", 417 new String(rbuf, 0, 16)); 418 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n'); 419 } 420 421 /** 422 * @tests java.io.PrintStream#println(float) 423 */ 424 public void test_printlnF() throws Exception { 425 // Test for method void java.io.PrintStream.println(float) 426 int c; 427 byte[] rbuf = new byte[100]; 428 PrintStream os = new PrintStream(bos, true); 429 os.println(29.08764f); 430 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 431 InputStreamReader isr = new InputStreamReader(bis); 432 bis.read(rbuf, 0, 8); 433 assertEquals("Incorrect float written", "29.08764", new String(rbuf, 0, 434 8)); 435 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n'); 436 } 437 438 /** 439 * @tests java.io.PrintStream#println(int) 440 */ 441 public void test_printlnI() throws Exception { 442 // Test for method void java.io.PrintStream.println(int) 443 int c; 444 PrintStream os = new PrintStream(bos, true); 445 os.println(768347202); 446 byte[] rbuf = new byte[100]; 447 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 448 InputStreamReader isr = new InputStreamReader(bis); 449 bis.read(rbuf, 0, 9); 450 assertEquals("Incorrect int written", "768347202", new String(rbuf, 0, 451 9)); 452 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n'); 453 } 454 455 /** 456 * @tests java.io.PrintStream#println(long) 457 */ 458 public void test_printlnJ() throws Exception { 459 // Test for method void java.io.PrintStream.println(long) 460 int c; 461 PrintStream os = new PrintStream(bos, true); 462 os.println(9875645283333L); 463 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 464 InputStreamReader isr = new InputStreamReader(bis); 465 byte[] rbuf = new byte[100]; 466 bis.read(rbuf, 0, 13); 467 assertEquals("Incorrect long written", "9875645283333", new String( 468 rbuf, 0, 13)); 469 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n'); 470 } 471 472 /** 473 * @tests java.io.PrintStream#println(java.lang.Object) 474 */ 475 public void test_printlnLjava_lang_Object() throws Exception { 476 // Test for method void java.io.PrintStream.println(java.lang.Object) 477 char c; 478 PrintStream os = new PrintStream(bos, true); 479 os.println(new java.util.Vector()); 480 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 481 InputStreamReader isr = new InputStreamReader(bis); 482 byte[] rbytes = new byte[2]; 483 bis.read(rbytes, 0, 2); 484 assertEquals("Incorrect Vector written", "[]", new String(rbytes, 0, 2)); 485 assertTrue("Newline not written", (c = (char) isr.read()) == '\r' 486 || c == '\n'); 487 } 488 489 /** 490 * @tests java.io.PrintStream#println(java.lang.String) 491 */ 492 public void test_printlnLjava_lang_String() throws Exception { 493 // Test for method void java.io.PrintStream.println(java.lang.String) 494 char c; 495 PrintStream os = new PrintStream(bos, true); 496 os.println("Hello World"); 497 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 498 InputStreamReader isr = new InputStreamReader(bis); 499 byte rbytes[] = new byte[100]; 500 bis.read(rbytes, 0, 11); 501 assertEquals("Incorrect string written", "Hello World", new String( 502 rbytes, 0, 11)); 503 assertTrue("Newline not written", (c = (char) isr.read()) == '\r' 504 || c == '\n'); 505 } 506 507 /** 508 * @tests java.io.PrintStream#println(boolean) 509 */ 510 public void test_printlnZ() throws Exception { 511 // Test for method void java.io.PrintStream.println(boolean) 512 int c; 513 PrintStream os = new PrintStream(bos, true); 514 os.println(true); 515 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 516 InputStreamReader isr = new InputStreamReader(bis); 517 byte[] rbuf = new byte[100]; 518 bis.read(rbuf, 0, 4); 519 assertEquals("Incorrect boolean written", "true", 520 new String(rbuf, 0, 4)); 521 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n'); 522 } 523 524 /** 525 * @tests java.io.PrintStream#write(byte[], int, int) 526 */ 527 public void test_write$BII() { 528 // Test for method void java.io.PrintStream.write(byte [], int, int) 529 PrintStream os = new PrintStream(bos, true); 530 os.write(fileString.getBytes(), 0, fileString.length()); 531 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 532 byte rbytes[] = new byte[4000]; 533 bis.read(rbytes, 0, fileString.length()); 534 assertTrue("Incorrect bytes written", new String(rbytes, 0, fileString 535 .length()).equals(fileString)); 536 } 537 538 /** 539 * @tests java.io.PrintStream#write(int) 540 */ 541 public void test_writeI() { 542 // Test for method void java.io.PrintStream.write(int) 543 PrintStream os = new PrintStream(bos, true); 544 os.write('t'); 545 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 546 assertEquals("Incorrect char written", 't', bis.read()); 547 } 548 549 /** 550 * @tests java.io.PrintStream#append(char) 551 */ 552 public void test_appendChar() throws IOException { 553 char testChar = ' '; 554 ByteArrayOutputStream out = new ByteArrayOutputStream(); 555 PrintStream printStream = new PrintStream(out); 556 printStream.append(testChar); 557 printStream.flush(); 558 assertEquals(String.valueOf(testChar), out.toString()); 559 printStream.close(); 560 } 561 562 /** 563 * @tests java.io.PrintStream#append(CharSequence) 564 */ 565 public void test_appendCharSequence() { 566 String testString = "My Test String"; 567 ByteArrayOutputStream out = new ByteArrayOutputStream(); 568 PrintStream printStream = new PrintStream(out); 569 printStream.append(testString); 570 printStream.flush(); 571 assertEquals(testString, out.toString()); 572 printStream.close(); 573 } 574 575 /** 576 * @tests java.io.PrintStream#append(CharSequence, int, int) 577 */ 578 public void test_appendCharSequenceIntInt() { 579 String testString = "My Test String"; 580 ByteArrayOutputStream out = new ByteArrayOutputStream(); 581 PrintStream printStream = new PrintStream(out); 582 printStream.append(testString, 1, 3); 583 printStream.flush(); 584 assertEquals(testString.substring(1, 3), out.toString()); 585 printStream.close(); 586 } 587 588 /** 589 * @tests java.io.PrintStream#format(java.lang.String, java.lang.Object...) 590 */ 591 public void test_formatLjava_lang_String$Ljava_lang_Object() { 592 PrintStream os = new PrintStream(bos, false); 593 os.format("%s %s", "Hello", "World"); 594 os.flush(); 595 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 596 byte[] rbytes = new byte[11]; 597 bis.read(rbytes, 0, rbytes.length); 598 assertEquals("Wrote incorrect string", "Hello World", 599 new String(rbytes)); 600 601 } 602 603 /** 604 * @tests java.io.PrintStream#format(java.util.Locale, java.lang.String, 605 * java.lang.Object...) 606 */ 607 public void test_formatLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() { 608 PrintStream os = new PrintStream(bos, false); 609 os.format(Locale.US, "%s %s", "Hello", "World"); 610 os.flush(); 611 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 612 byte[] rbytes = new byte[11]; 613 bis.read(rbytes, 0, rbytes.length); 614 assertEquals("Wrote incorrect string", "Hello World", 615 new String(rbytes)); 616 } 617 618 /** 619 * @tests java.io.PrintStream#printf(java.lang.String, java.lang.Object...) 620 */ 621 public void test_printfLjava_lang_String$Ljava_lang_Object() { 622 PrintStream os = new PrintStream(bos, false); 623 os.printf("%s %s", "Hello", "World"); 624 os.flush(); 625 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 626 byte[] rbytes = new byte[11]; 627 bis.read(rbytes, 0, rbytes.length); 628 assertEquals("Wrote incorrect string", "Hello World", 629 new String(rbytes)); 630 } 631 632 /** 633 * @tests java.io.PrintStream#printf(java.util.Locale, java.lang.String, 634 * java.lang.Object...) 635 */ 636 public void test_printfLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() { 637 PrintStream os = new PrintStream(bos, false); 638 os.printf(Locale.US, "%s %s", "Hello", "World"); 639 os.flush(); 640 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 641 byte[] rbytes = new byte[11]; 642 bis.read(rbytes, 0, rbytes.length); 643 assertEquals("Wrote incorrect string", "Hello World", 644 new String(rbytes)); 645 } 646 647 @Override 648 protected void setUp() throws Exception { 649 super.setUp(); 650 testFile = File.createTempFile("test", null); 651 testFilePath = testFile.getAbsolutePath(); 652 } 653 654 @Override 655 protected void tearDown() throws Exception { 656 testFile.delete(); 657 testFile = null; 658 testFilePath = null; 659 super.tearDown(); 660 } 661 662 663 } 664