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