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