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