1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.tests.java.io; 19 20 import java.io.BufferedReader; 21 import java.io.ByteArrayInputStream; 22 import java.io.ByteArrayOutputStream; 23 import java.io.File; 24 import java.io.IOException; 25 import java.io.InputStreamReader; 26 import java.io.OutputStream; 27 import java.io.PrintWriter; 28 import java.nio.charset.Charset; 29 import java.util.Locale; 30 31 import tests.support.Support_StringReader; 32 import tests.support.Support_StringWriter; 33 34 public class PrintWriterTest extends junit.framework.TestCase { 35 36 static class Bogus { 37 public String toString() { 38 return "Bogus"; 39 } 40 } 41 42 /** 43 * @since 1.6 44 */ 45 static class MockPrintWriter extends PrintWriter { 46 47 public MockPrintWriter(OutputStream out, boolean autoflush) { 48 super(out, autoflush); 49 } 50 51 @Override 52 public void clearError() { 53 super.clearError(); 54 } 55 56 } 57 58 PrintWriter pw; 59 60 ByteArrayOutputStream bao; 61 62 ByteArrayInputStream bai; 63 64 BufferedReader br; 65 66 /** 67 * java.io.PrintWriter#PrintWriter(java.io.OutputStream) 68 */ 69 public void test_ConstructorLjava_io_OutputStream() { 70 // Test for method java.io.PrintWriter(java.io.OutputStream) 71 String s; 72 pw.println("Random Chars"); 73 pw.write("Hello World"); 74 pw.flush(); 75 try { 76 br = new BufferedReader(new Support_StringReader(bao.toString())); 77 s = br.readLine(); 78 assertTrue("Incorrect string written/read: " + s, s 79 .equals("Random Chars")); 80 s = br.readLine(); 81 assertTrue("Incorrect string written/read: " + s, s 82 .equals("Hello World")); 83 } catch (IOException e) { 84 fail("IOException during test : " + e.getMessage()); 85 } 86 } 87 88 /** 89 * java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean) 90 */ 91 public void test_ConstructorLjava_io_OutputStreamZ() { 92 // Test for method java.io.PrintWriter(java.io.OutputStream, boolean) 93 String s; 94 pw = new PrintWriter(bao, true); 95 pw.println("Random Chars"); 96 pw.write("Hello World"); 97 try { 98 br = new BufferedReader(new Support_StringReader(bao.toString())); 99 s = br.readLine(); 100 assertTrue("Incorrect string written/read: " + s, s 101 .equals("Random Chars")); 102 pw.flush(); 103 br = new BufferedReader(new Support_StringReader(bao.toString())); 104 s = br.readLine(); 105 assertTrue("Incorrect string written/read: " + s, s 106 .equals("Random Chars")); 107 s = br.readLine(); 108 assertTrue("Incorrect string written/read: " + s, s 109 .equals("Hello World")); 110 } catch (IOException e) { 111 fail("IOException during test : " + e.getMessage()); 112 } 113 } 114 115 /** 116 * java.io.PrintWriter#PrintWriter(java.io.Writer) 117 */ 118 public void test_ConstructorLjava_io_Writer() { 119 // Test for method java.io.PrintWriter(java.io.Writer) 120 Support_StringWriter sw; 121 pw = new PrintWriter(sw = new Support_StringWriter()); 122 pw.print("Hello"); 123 pw.flush(); 124 assertEquals("Failed to construct proper writer", 125 "Hello", sw.toString()); 126 } 127 128 /** 129 * java.io.PrintWriter#PrintWriter(java.io.Writer, boolean) 130 */ 131 public void test_ConstructorLjava_io_WriterZ() { 132 // Test for method java.io.PrintWriter(java.io.Writer, boolean) 133 Support_StringWriter sw; 134 pw = new PrintWriter(sw = new Support_StringWriter(), true); 135 pw.print("Hello"); 136 // Auto-flush should have happened 137 assertEquals("Failed to construct proper writer", 138 "Hello", sw.toString()); 139 } 140 141 /** 142 * java.io.PrintWriter#PrintWriter(java.io.File) 143 */ 144 public void test_ConstructorLjava_io_File() throws Exception { 145 File file = File.createTempFile(getClass().getName(), null); 146 try { 147 PrintWriter writer = new PrintWriter(file); 148 writer.close(); 149 } finally { 150 file.delete(); 151 } 152 } 153 154 /** 155 * java.io.PrintWriter#PrintWriter(java.io.File, java.lang.String) 156 */ 157 public void test_ConstructorLjava_io_File_Ljava_lang_String() throws Exception { 158 File file = File.createTempFile(getClass().getName(), null); 159 try { 160 PrintWriter writer = new PrintWriter(file, 161 Charset.defaultCharset().name()); 162 writer.close(); 163 } finally { 164 file.delete(); 165 } 166 } 167 168 /** 169 * java.io.PrintWriter#PrintWriter(java.lang.String) 170 */ 171 public void test_ConstructorLjava_lang_String() throws Exception { 172 File file = File.createTempFile(getClass().getName(), null); 173 try { 174 PrintWriter writer = new PrintWriter(file.getPath()); 175 writer.close(); 176 } finally { 177 file.delete(); 178 } 179 } 180 181 /** 182 * java.io.PrintWriter#PrintWriter(java.lang.String, java.lang.String) 183 */ 184 public void test_ConstructorLjava_lang_String_Ljava_lang_String() throws Exception { 185 File file = File.createTempFile(getClass().getName(), null); 186 try { 187 PrintWriter writer = new PrintWriter(file.getPath(), 188 Charset.defaultCharset().name()); 189 writer.close(); 190 } finally { 191 file.delete(); 192 } 193 } 194 195 /** 196 * java.io.PrintWriter#checkError() 197 */ 198 public void test_checkError() { 199 // Test for method boolean java.io.PrintWriter.checkError() 200 pw.close(); 201 pw.print(490000000000.08765); 202 assertTrue("Failed to return error", pw.checkError()); 203 } 204 205 /** 206 * java.io.PrintWriter#clearError() 207 * @since 1.6 208 */ 209 public void test_clearError() { 210 // Test for method boolean java.io.PrintWriter.clearError() 211 MockPrintWriter mpw = new MockPrintWriter(new ByteArrayOutputStream(), false); 212 mpw.close(); 213 mpw.print(490000000000.08765); 214 assertTrue("Failed to return error", mpw.checkError()); 215 mpw.clearError(); 216 assertFalse("Internal error state has not be cleared", mpw.checkError()); 217 } 218 219 /** 220 * java.io.PrintWriter#close() 221 */ 222 public void test_close() { 223 // Test for method void java.io.PrintWriter.close() 224 pw.close(); 225 pw.println("l"); 226 assertTrue("Write on closed stream failed to generate error", pw 227 .checkError()); 228 } 229 230 /** 231 * java.io.PrintWriter#flush() 232 */ 233 public void test_flush() { 234 // Test for method void java.io.PrintWriter.flush() 235 final double dub = 490000000000.08765; 236 pw.print(dub); 237 pw.flush(); 238 assertTrue("Failed to flush", new String(bao.toByteArray()) 239 .equals(String.valueOf(dub))); 240 } 241 242 /** 243 * java.io.PrintWriter#print(char[]) 244 */ 245 public void test_print$C() { 246 // Test for method void java.io.PrintWriter.print(char []) 247 String s = null; 248 char[] schars = new char[11]; 249 "Hello World".getChars(0, 11, schars, 0); 250 pw.print(schars); 251 pw.flush(); 252 try { 253 br = new BufferedReader(new Support_StringReader(bao.toString())); 254 s = br.readLine(); 255 } catch (IOException e) { 256 fail("IOException during test : " + e.getMessage()); 257 } 258 assertTrue("Wrote incorrect char[] string: " + s, s 259 .equals("Hello World")); 260 int r = 0; 261 try { 262 pw.print((char[]) null); 263 } catch (NullPointerException e) { 264 r = 1; 265 } 266 assertEquals("null pointer exception for printing null char[] is not caught", 267 1, r); 268 } 269 270 /** 271 * java.io.PrintWriter#print(char) 272 */ 273 public void test_printC() { 274 // Test for method void java.io.PrintWriter.print(char) 275 pw.print('c'); 276 pw.flush(); 277 assertEquals("Wrote incorrect char string", "c", new String(bao.toByteArray()) 278 ); 279 } 280 281 /** 282 * java.io.PrintWriter#print(double) 283 */ 284 public void test_printD() { 285 // Test for method void java.io.PrintWriter.print(double) 286 final double dub = 490000000000.08765; 287 pw.print(dub); 288 pw.flush(); 289 assertTrue("Wrote incorrect double string", new String(bao 290 .toByteArray()).equals(String.valueOf(dub))); 291 } 292 293 /** 294 * java.io.PrintWriter#print(float) 295 */ 296 public void test_printF() { 297 // Test for method void java.io.PrintWriter.print(float) 298 final float flo = 49.08765f; 299 pw.print(flo); 300 pw.flush(); 301 assertTrue("Wrote incorrect float string", 302 new String(bao.toByteArray()).equals(String.valueOf(flo))); 303 } 304 305 /** 306 * java.io.PrintWriter#print(int) 307 */ 308 public void test_printI() { 309 // Test for method void java.io.PrintWriter.print(int) 310 pw.print(4908765); 311 pw.flush(); 312 assertEquals("Wrote incorrect int string", "4908765", new String(bao.toByteArray()) 313 ); 314 } 315 316 /** 317 * java.io.PrintWriter#print(long) 318 */ 319 public void test_printJ() { 320 // Test for method void java.io.PrintWriter.print(long) 321 pw.print(49087650000L); 322 pw.flush(); 323 assertEquals("Wrote incorrect long string", "49087650000", new String(bao.toByteArray()) 324 ); 325 } 326 327 /** 328 * java.io.PrintWriter#print(java.lang.Object) 329 */ 330 public void test_printLjava_lang_Object() { 331 // Test for method void java.io.PrintWriter.print(java.lang.Object) 332 pw.print((Object) null); 333 pw.flush(); 334 assertEquals("Did not write null", "null", new String(bao.toByteArray()) 335 ); 336 bao.reset(); 337 338 pw.print(new Bogus()); 339 pw.flush(); 340 assertEquals("Wrote in incorrect Object string", "Bogus", new String(bao 341 .toByteArray())); 342 } 343 344 /** 345 * java.io.PrintWriter#print(java.lang.String) 346 */ 347 public void test_printLjava_lang_String() { 348 // Test for method void java.io.PrintWriter.print(java.lang.String) 349 pw.print((String) null); 350 pw.flush(); 351 assertEquals("did not write null", "null", new String(bao.toByteArray()) 352 ); 353 bao.reset(); 354 355 pw.print("Hello World"); 356 pw.flush(); 357 assertEquals("Wrote incorrect string", "Hello World", new String(bao.toByteArray()) 358 ); 359 } 360 361 /** 362 * java.io.PrintWriter#print(boolean) 363 */ 364 public void test_printZ() { 365 // Test for method void java.io.PrintWriter.print(boolean) 366 pw.print(true); 367 pw.flush(); 368 assertEquals("Wrote in incorrect boolean string", "true", new String(bao 369 .toByteArray())); 370 } 371 372 /** 373 * java.io.PrintWriter#println() 374 */ 375 public void test_println() { 376 // Test for method void java.io.PrintWriter.println() 377 String s; 378 pw.println("Blarg"); 379 pw.println(); 380 pw.println("Bleep"); 381 pw.flush(); 382 try { 383 br = new BufferedReader(new Support_StringReader(bao.toString())); 384 s = br.readLine(); 385 assertTrue("Wrote incorrect line: " + s, s.equals("Blarg")); 386 s = br.readLine(); 387 assertTrue("Wrote incorrect line: " + s, s.equals("")); 388 s = br.readLine(); 389 assertTrue("Wrote incorrect line: " + s, s.equals("Bleep")); 390 } catch (IOException e) { 391 fail("IOException during test : " + e.getMessage()); 392 } 393 } 394 395 /** 396 * java.io.PrintWriter#println(char[]) 397 */ 398 public void test_println$C() { 399 // Test for method void java.io.PrintWriter.println(char []) 400 String s = null; 401 char[] schars = new char[11]; 402 "Hello World".getChars(0, 11, schars, 0); 403 pw.println("Random Chars"); 404 pw.println(schars); 405 pw.flush(); 406 try { 407 br = new BufferedReader(new Support_StringReader(bao.toString())); 408 s = br.readLine(); 409 s = br.readLine(); 410 } catch (IOException e) { 411 fail("IOException during test : " + e.getMessage()); 412 } 413 assertTrue("Wrote incorrect char[] string: " + s, s 414 .equals("Hello World")); 415 } 416 417 /** 418 * java.io.PrintWriter#println(char) 419 */ 420 public void test_printlnC() { 421 // Test for method void java.io.PrintWriter.println(char) 422 String s = null; 423 pw.println("Random Chars"); 424 pw.println('c'); 425 pw.flush(); 426 try { 427 br = new BufferedReader(new Support_StringReader(bao.toString())); 428 s = br.readLine(); 429 s = br.readLine(); 430 } catch (IOException e) { 431 fail("IOException during test : " + e.getMessage()); 432 } 433 assertTrue("Wrote incorrect char string: " + s, s.equals("c")); 434 } 435 436 /** 437 * java.io.PrintWriter#println(double) 438 */ 439 public void test_printlnD() { 440 // Test for method void java.io.PrintWriter.println(double) 441 String s = null; 442 final double dub = 4000000000000000.657483; 443 pw.println("Random Chars"); 444 pw.println(dub); 445 pw.flush(); 446 try { 447 br = new BufferedReader(new Support_StringReader(bao.toString())); 448 br.readLine(); 449 s = br.readLine(); 450 } catch (IOException e) { 451 fail("IOException during test : " + e.getMessage()); 452 } 453 assertTrue("Wrote incorrect double string: " + s, s.equals(String 454 .valueOf(dub))); 455 } 456 457 /** 458 * java.io.PrintWriter#println(float) 459 */ 460 public void test_printlnF() { 461 // Test for method void java.io.PrintWriter.println(float) 462 String s; 463 final float flo = 40.4646464f; 464 pw.println("Random Chars"); 465 pw.println(flo); 466 pw.flush(); 467 try { 468 br = new BufferedReader(new Support_StringReader(bao.toString())); 469 br.readLine(); 470 s = br.readLine(); 471 assertTrue("Wrote incorrect float string: " + s + " wanted: " 472 + String.valueOf(flo), s.equals(String.valueOf(flo))); 473 } catch (IOException e) { 474 fail("IOException during test : " + e.getMessage()); 475 } 476 477 } 478 479 /** 480 * java.io.PrintWriter#println(int) 481 */ 482 public void test_printlnI() { 483 // Test for method void java.io.PrintWriter.println(int) 484 String s = null; 485 pw.println("Random Chars"); 486 pw.println(400000); 487 pw.flush(); 488 try { 489 br = new BufferedReader(new Support_StringReader(bao.toString())); 490 br.readLine(); 491 s = br.readLine(); 492 } catch (IOException e) { 493 fail("IOException during test : " + e.getMessage()); 494 } 495 assertTrue("Wrote incorrect int string: " + s, s.equals("400000")); 496 } 497 498 /** 499 * java.io.PrintWriter#println(long) 500 */ 501 public void test_printlnJ() { 502 // Test for method void java.io.PrintWriter.println(long) 503 String s = null; 504 pw.println("Random Chars"); 505 pw.println(4000000000000L); 506 pw.flush(); 507 try { 508 br = new BufferedReader(new Support_StringReader(bao.toString())); 509 br.readLine(); 510 s = br.readLine(); 511 } catch (IOException e) { 512 fail("IOException during test : " + e.getMessage()); 513 } 514 assertTrue("Wrote incorrect long string: " + s, s 515 .equals("4000000000000")); 516 } 517 518 /** 519 * java.io.PrintWriter#println(java.lang.Object) 520 */ 521 public void test_printlnLjava_lang_Object() { 522 // Test for method void java.io.PrintWriter.println(java.lang.Object) 523 String s = null; 524 pw.println("Random Chars"); 525 pw.println(new Bogus()); 526 pw.flush(); 527 try { 528 br = new BufferedReader(new Support_StringReader(bao.toString())); 529 br.readLine(); 530 s = br.readLine(); 531 } catch (IOException e) { 532 fail("IOException during test : " + e.getMessage()); 533 } 534 assertTrue("Wrote incorrect Object string: " + s, s.equals("Bogus")); 535 } 536 537 /** 538 * java.io.PrintWriter#println(java.lang.String) 539 */ 540 public void test_printlnLjava_lang_String() { 541 // Test for method void java.io.PrintWriter.println(java.lang.String) 542 String s = null; 543 pw.println("Random Chars"); 544 pw.println("Hello World"); 545 pw.flush(); 546 try { 547 br = new BufferedReader(new Support_StringReader(bao.toString())); 548 br.readLine(); 549 s = br.readLine(); 550 } catch (IOException e) { 551 fail("IOException during test : " + e.getMessage()); 552 } 553 assertTrue("Wrote incorrect string: " + s, s.equals("Hello World")); 554 } 555 556 /** 557 * java.io.PrintWriter#println(boolean) 558 */ 559 public void test_printlnZ() { 560 // Test for method void java.io.PrintWriter.println(boolean) 561 String s = null; 562 pw.println("Random Chars"); 563 pw.println(false); 564 pw.flush(); 565 try { 566 br = new BufferedReader(new Support_StringReader(bao.toString())); 567 br.readLine(); 568 s = br.readLine(); 569 } catch (IOException e) { 570 fail("IOException during test : " + e.getMessage()); 571 } 572 assertTrue("Wrote incorrect boolean string: " + s, s.equals("false")); 573 } 574 575 /** 576 * java.io.PrintWriter#write(char[]) 577 */ 578 public void test_write$C() { 579 // Test for method void java.io.PrintWriter.write(char []) 580 String s = null; 581 char[] schars = new char[11]; 582 "Hello World".getChars(0, 11, schars, 0); 583 pw.println("Random Chars"); 584 pw.write(schars); 585 pw.flush(); 586 try { 587 br = new BufferedReader(new Support_StringReader(bao.toString())); 588 br.readLine(); 589 s = br.readLine(); 590 } catch (IOException e) { 591 fail("IOException during test: " + e.getMessage()); 592 } 593 assertTrue("Wrote incorrect char[] string: " + s, s 594 .equals("Hello World")); 595 } 596 597 /** 598 * java.io.PrintWriter#write(char[], int, int) 599 */ 600 public void test_write$CII() { 601 // Test for method void java.io.PrintWriter.write(char [], int, int) 602 String s = null; 603 char[] schars = new char[11]; 604 "Hello World".getChars(0, 11, schars, 0); 605 pw.println("Random Chars"); 606 pw.write(schars, 6, 5); 607 pw.flush(); 608 try { 609 br = new BufferedReader(new Support_StringReader(bao.toString())); 610 br.readLine(); 611 s = br.readLine(); 612 } catch (IOException e) { 613 fail("IOException during test : " + e.getMessage()); 614 } 615 assertTrue("Wrote incorrect char[] string: " + s, s.equals("World")); 616 } 617 618 /** 619 * java.io.PrintWriter#write(int) 620 */ 621 public void test_writeI() throws IOException { 622 // Test for method void java.io.PrintWriter.write(int) 623 char[] cab = new char[3]; 624 pw.write('a'); 625 pw.write('b'); 626 pw.write('c'); 627 pw.flush(); 628 InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(bao.toByteArray())); 629 cab[0] = (char) isr.read(); 630 cab[1] = (char) isr.read(); 631 cab[2] = (char) isr.read(); 632 assertTrue("Wrote incorrect ints", cab[0] == 'a' && cab[1] == 'b' 633 && cab[2] == 'c'); 634 635 } 636 637 /** 638 * java.io.PrintWriter#write(java.lang.String) 639 */ 640 public void test_writeLjava_lang_String() { 641 // Test for method void java.io.PrintWriter.write(java.lang.String) 642 String s = null; 643 pw.println("Random Chars"); 644 pw.write("Hello World"); 645 pw.flush(); 646 try { 647 br = new BufferedReader(new Support_StringReader(bao.toString())); 648 br.readLine(); 649 s = br.readLine(); 650 } catch (IOException e) { 651 fail("IOException during test : " + e.getMessage()); 652 } 653 assertTrue("Wrote incorrect char[] string: " + s, s 654 .equals("Hello World")); 655 } 656 657 /** 658 * java.io.PrintWriter#write(java.lang.String, int, int) 659 */ 660 public void test_writeLjava_lang_StringII() { 661 // Test for method void java.io.PrintWriter.write(java.lang.String, int, 662 // int) 663 String s = null; 664 pw.println("Random Chars"); 665 pw.write("Hello World", 6, 5); 666 pw.flush(); 667 try { 668 br = new BufferedReader(new Support_StringReader(bao.toString())); 669 br.readLine(); 670 s = br.readLine(); 671 } catch (IOException e) { 672 fail("IOException during test : " + e.getMessage()); 673 } 674 assertTrue("Wrote incorrect char[] string: " + s, s.equals("World")); 675 } 676 677 /** 678 * java.io.PrintWriter#append(char) 679 */ 680 public void test_appendChar() { 681 char testChar = ' '; 682 ByteArrayOutputStream out = new ByteArrayOutputStream(); 683 PrintWriter printWriter = new PrintWriter(out); 684 printWriter.append(testChar); 685 printWriter.flush(); 686 assertEquals(String.valueOf(testChar), out.toString()); 687 printWriter.close(); 688 } 689 690 /** 691 * java.io.PrintWriter#append(CharSequence) 692 */ 693 public void test_appendCharSequence() { 694 695 String testString = "My Test String"; 696 ByteArrayOutputStream out = new ByteArrayOutputStream(); 697 PrintWriter printWriter = new PrintWriter(out); 698 printWriter.append(testString); 699 printWriter.flush(); 700 assertEquals(testString, out.toString()); 701 printWriter.close(); 702 703 } 704 705 /** 706 * java.io.PrintWriter#append(CharSequence, int, int) 707 */ 708 public void test_appendCharSequenceIntInt() { 709 String testString = "My Test String"; 710 ByteArrayOutputStream out = new ByteArrayOutputStream(); 711 PrintWriter printWriter = new PrintWriter(out); 712 printWriter.append(testString, 1, 3); 713 printWriter.flush(); 714 assertEquals(testString.substring(1, 3), out.toString()); 715 printWriter.close(); 716 717 } 718 719 /** 720 * java.io.PrintWriter#format(java.lang.String, java.lang.Object...) 721 */ 722 public void test_formatLjava_lang_String$Ljava_lang_Object() { 723 pw.format("%s %s", "Hello", "World"); 724 pw.flush(); 725 assertEquals("Wrote incorrect string", "Hello World", 726 new String(bao.toByteArray())); 727 } 728 729 /** 730 * java.io.PrintWriter#format(java.util.Locale, java.lang.String, java.lang.Object...) 731 */ 732 public void test_formatLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() { 733 pw.format(Locale.US, "%s %s", "Hello", "World"); 734 pw.flush(); 735 assertEquals("Wrote incorrect string", "Hello World", 736 new String(bao.toByteArray())); 737 } 738 739 /** 740 * java.io.PrintWriter#printf(java.lang.String, java.lang.Object...) 741 */ 742 public void test_printfLjava_lang_String$Ljava_lang_Object() { 743 pw.printf("%s %s", "Hello", "World"); 744 pw.flush(); 745 assertEquals("Wrote incorrect string", "Hello World", 746 new String(bao.toByteArray())); 747 } 748 749 /** 750 * java.io.PrintWriter#printf(java.util.Locale, java.lang.String, java.lang.Object...) 751 */ 752 public void test_printfLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() { 753 pw.printf(Locale.US, "%s %s", "Hello", "World"); 754 pw.flush(); 755 assertEquals("Wrote incorrect string", "Hello World", 756 new String(bao.toByteArray())); 757 } 758 759 /** 760 * Sets up the fixture, for example, open a network connection. This method 761 * is called before a test is executed. 762 */ 763 protected void setUp() { 764 bao = new ByteArrayOutputStream(); 765 pw = new PrintWriter(bao, false); 766 767 } 768 769 /** 770 * Tears down the fixture, for example, close a network connection. This 771 * method is called after a test is executed. 772 */ 773 protected void tearDown() { 774 try { 775 pw.close(); 776 } catch (Exception e) { 777 } 778 } 779 } 780