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