OutputStreamWriterTest.java revision 3819a76e7c1f49253f0e077bd497f149340c02b8
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 dalvik.annotation.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import java.io.ByteArrayInputStream; 26import java.io.ByteArrayOutputStream; 27import java.io.IOException; 28import java.io.InputStreamReader; 29import java.io.OutputStreamWriter; 30import java.io.UnsupportedEncodingException; 31import java.nio.charset.Charset; 32import java.nio.charset.CharsetEncoder; 33import java.util.Arrays; 34 35import tests.support.Support_OutputStream; 36 37import junit.framework.TestCase; 38 39/** 40 * 41 */ 42@TestTargetClass(OutputStreamWriter.class) 43public class OutputStreamWriterTest extends TestCase { 44 45 private static final int UPPER = 0xd800; 46 47 private static final int BUFFER_SIZE = 10000; 48 49 static private final String[] MINIMAL_CHARSETS = new String[] { "US-ASCII", 50 "ISO-8859-1", "UTF-16BE", "UTF-16LE", "UTF-16", "UTF-8" }; 51 52 OutputStreamWriter osw; 53 54 InputStreamReader isr; 55 56 private Support_OutputStream fos; 57 58 public String testString = "This is a test message with Unicode characters. \u4e2d\u56fd is China's name in Chinese"; 59 60 /* 61 * @see TestCase#setUp() 62 */ 63 protected void setUp() throws Exception { 64 super.setUp(); 65 fos = new Support_OutputStream(500); 66 osw = new OutputStreamWriter(fos, "UTF-8"); 67 } 68 69 /* 70 * @see TestCase#tearDown() 71 */ 72 protected void tearDown() throws Exception { 73 try { 74 if (isr != null) isr.close(); 75 osw.close(); 76 } catch (Exception e) { 77 } 78 79 super.tearDown(); 80 } 81 82 /** 83 * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream) 84 */ 85 @TestTargetNew( 86 level = TestLevel.COMPLETE, 87 notes = "", 88 method = "OutputStreamWriter", 89 args = {java.io.OutputStream.class} 90 ) 91 public void test_ConstructorLjava_io_OutputStream() throws IOException { 92 OutputStreamWriter writer = null; 93 94 try { 95 writer = new OutputStreamWriter(null); 96 fail("Test 1: NullPointerException expected."); 97 } catch (NullPointerException e) { 98 // Expected 99 } 100 101 try { 102 writer = new OutputStreamWriter(new Support_OutputStream()); 103 } catch (Exception e) { 104 fail("Test 2: Unexpected exception: " + e.getMessage()); 105 } 106 107 // Test that the default encoding has been used. 108 assertEquals("Test 3: Incorrect default encoding used.", 109 Charset.defaultCharset(), 110 Charset.forName(writer.getEncoding())); 111 112 if (writer != null) writer.close(); 113 } 114 115 /** 116 * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream, 117 * java.lang.String) 118 */ 119 @TestTargetNew( 120 level = TestLevel.COMPLETE, 121 notes = "", 122 method = "OutputStreamWriter", 123 args = {java.io.OutputStream.class, java.lang.String.class} 124 ) 125 public void test_ConstructorLjava_io_OutputStreamLjava_lang_String() 126 throws UnsupportedEncodingException { 127 128 try { 129 osw = new OutputStreamWriter(null, "utf-8"); 130 fail("Test 1: NullPointerException expected."); 131 } catch (NullPointerException e) { 132 // Expected 133 } 134 135 try { 136 osw = new OutputStreamWriter(fos, (String) null); 137 fail("Test 2: NullPointerException expected."); 138 } catch (NullPointerException e) { 139 // Expected 140 } 141 142 try { 143 osw = new OutputStreamWriter(fos, ""); 144 fail("Test 3: UnsupportedEncodingException expected."); 145 } catch (UnsupportedEncodingException e) { 146 // Expected 147 } 148 149 try { 150 osw = new OutputStreamWriter(fos, "Bogus"); 151 fail("Test 4: UnsupportedEncodingException expected."); 152 } catch (UnsupportedEncodingException e) { 153 // Expected 154 } 155 156 try { 157 osw = new OutputStreamWriter(fos, "8859_1"); 158 } catch (UnsupportedEncodingException e) { 159 fail("Test 5: Unexpected UnsupportedEncodingException."); 160 } 161 162 assertEquals("Test 6: Encoding not set correctly. ", 163 Charset.forName("8859_1"), 164 Charset.forName(osw.getEncoding())); 165 } 166 167 /** 168 * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream, 169 * java.nio.charset.Charset) 170 */ 171 @TestTargetNew( 172 level = TestLevel.COMPLETE, 173 notes = "", 174 method = "OutputStreamWriter", 175 args = {java.io.OutputStream.class, java.nio.charset.Charset.class} 176 ) 177 public void test_ConstructorLjava_io_OutputStreamLjava_nio_charset_Charset() 178 throws IOException { 179 OutputStreamWriter writer; 180 Support_OutputStream out = new Support_OutputStream(); 181 Charset cs = Charset.forName("ascii"); 182 183 try { 184 writer = new OutputStreamWriter(null, cs); 185 fail("Test 1: NullPointerException expected."); 186 } catch (NullPointerException e) { 187 // Expected 188 } 189 190 try { 191 writer = new OutputStreamWriter(out, (Charset) null); 192 fail("Test 2: NullPointerException expected."); 193 } catch (NullPointerException e) { 194 // Expected 195 } 196 197 writer = new OutputStreamWriter(out, cs); 198 assertEquals("Test 3: Encoding not set correctly. ", 199 cs, Charset.forName(writer.getEncoding())); 200 writer.close(); 201 } 202 203 /** 204 * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream, 205 * java.nio.charset.CharsetEncoder) 206 */ 207 @TestTargetNew( 208 level = TestLevel.COMPLETE, 209 notes = "", 210 method = "OutputStreamWriter", 211 args = {java.io.OutputStream.class, java.nio.charset.CharsetEncoder.class} 212 ) 213 public void test_ConstructorLjava_io_OutputStreamLjava_nio_charset_CharsetEncoder() 214 throws IOException { 215 OutputStreamWriter writer; 216 Support_OutputStream out = new Support_OutputStream(); 217 Charset cs = Charset.forName("ascii"); 218 CharsetEncoder enc = cs.newEncoder(); 219 220 try { 221 writer = new OutputStreamWriter(null, enc); 222 fail("Test 1: NullPointerException expected."); 223 } catch (NullPointerException e) { 224 // Expected 225 } 226 227 try { 228 writer = new OutputStreamWriter(out, (CharsetEncoder) null); 229 fail("Test 2: NullPointerException expected."); 230 } catch (NullPointerException e) { 231 // Expected 232 } 233 234 writer = new OutputStreamWriter(out, cs); 235 assertEquals("Test 3: CharacterEncoder not set correctly. ", 236 cs, Charset.forName(writer.getEncoding())); 237 writer.close(); 238 } 239 240 /** 241 * @tests java.io.OutputStreamWriter#close() 242 */ 243 @TestTargetNew( 244 level = TestLevel.COMPLETE, 245 notes = "An issue in the API code has been identified (ticket #87). This test must be updated when the ticket is closed.", 246 method = "close", 247 args = {} 248 ) 249 public void test_close() { 250 251 fos.setThrowsException(true); 252 try { 253 osw.close(); 254 fail("Test 1: IOException expected."); 255 } catch (IOException e) { 256 // Expected. 257 } 258 259/* Test 2 does not work and has therefore been disabled (see Ticket #87). 260 // Test 2: Write should not fail since the closing 261 // in test 1 has not been successful. 262 try { 263 osw.write("Lorem ipsum..."); 264 } catch (IOException e) { 265 fail("Test 2: Unexpected IOException."); 266 } 267 268 // Test 3: Close should succeed. 269 fos.setThrowsException(false); 270 try { 271 osw.close(); 272 } catch (IOException e) { 273 fail("Test 3: Unexpected IOException."); 274 } 275*/ 276 277 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 278 try { 279 OutputStreamWriter writer = new OutputStreamWriter(bout, 280 "ISO2022JP"); 281 writer.write(new char[] { 'a' }); 282 writer.close(); 283 // The default is ASCII, there should not be any mode changes. 284 String converted = new String(bout.toByteArray(), "ISO8859_1"); 285 assertTrue("Test 4: Invalid conversion: " + converted, 286 converted.equals("a")); 287 288 bout.reset(); 289 writer = new OutputStreamWriter(bout, "ISO2022JP"); 290 writer.write(new char[] { '\u3048' }); 291 writer.flush(); 292 // The byte sequence should not switch to ASCII mode until the 293 // stream is closed. 294 converted = new String(bout.toByteArray(), "ISO8859_1"); 295 assertTrue("Test 5: Invalid conversion: " + converted, 296 converted.equals("\u001b$B$(")); 297 writer.close(); 298 converted = new String(bout.toByteArray(), "ISO8859_1"); 299 assertTrue("Test 6: Invalid conversion: " + converted, 300 converted.equals("\u001b$B$(\u001b(B")); 301 302 bout.reset(); 303 writer = new OutputStreamWriter(bout, "ISO2022JP"); 304 writer.write(new char[] { '\u3048' }); 305 writer.write(new char[] { '\u3048' }); 306 writer.close(); 307 // There should not be a mode switch between writes. 308 assertEquals("Test 7: Invalid conversion. ", 309 "\u001b$B$($(\u001b(B", 310 new String(bout.toByteArray(), "ISO8859_1")); 311 } catch (UnsupportedEncodingException e) { 312 // Can't test missing converter. 313 System.out.println(e); 314 } catch (IOException e) { 315 fail("Unexpected: " + e); 316 } 317 } 318 319 /** 320 * @tests java.io.OutputStreamWriter#flush() 321 */ 322 @TestTargetNew( 323 level = TestLevel.COMPLETE, 324 notes = "", 325 method = "flush", 326 args = {} 327 ) 328 public void test_flush() { 329 // Test for method void java.io.OutputStreamWriter.flush() 330 try { 331 char[] buf = new char[testString.length()]; 332 osw.write(testString, 0, testString.length()); 333 osw.flush(); 334 openInputStream(); 335 isr.read(buf, 0, buf.length); 336 assertTrue("Test 1: Characters have not been flushed.", 337 new String(buf, 0, buf.length).equals(testString)); 338 } catch (Exception e) { 339 fail("Test 1: Unexpected exception: " + e.getMessage()); 340 } 341 342 fos.setThrowsException(true); 343 try { 344 osw.flush(); 345 fail("Test 2: IOException expected."); 346 } catch (IOException e) { 347 // Expected 348 } 349 fos.setThrowsException(false); 350 } 351 352 @TestTargets({ 353 @TestTargetNew( 354 level = TestLevel.PARTIAL_COMPLETE, 355 notes = "", 356 method = "write", 357 args = {int.class} 358 ), 359 @TestTargetNew( 360 level = TestLevel.PARTIAL_COMPLETE, 361 notes = "", 362 method = "read", 363 args = {}, 364 clazz = InputStreamReader.class 365 ) 366 }) 367 public void test_singleCharIO() throws Exception { 368 int upper; 369 OutputStreamWriter writer = null; 370 ByteArrayOutputStream out; 371 InputStreamReader isr = null; 372 373 for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) { 374 try { 375 out = new ByteArrayOutputStream(); 376 writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]); 377 378 switch (i) { 379 case 0: 380 upper = 128; 381 break; 382 case 1: 383 upper = 256; 384 break; 385 default: 386 upper = UPPER; 387 } 388 389 for (int c = 0; c < upper; ++c) { 390 writer.write(c); 391 } 392 writer.flush(); 393 byte[] result = out.toByteArray(); 394 395 isr = new InputStreamReader(new ByteArrayInputStream(result), 396 MINIMAL_CHARSETS[i]); 397 for (int expected = 0; expected < upper; ++expected) { 398 assertEquals("Error when reading bytes in " 399 + MINIMAL_CHARSETS[i], expected, isr.read()); 400 } 401 } finally { 402 try { 403 if (isr != null) isr.close(); 404 } catch (Exception e) { 405 } 406 try { 407 if (writer != null) writer.close(); 408 } catch (Exception e) { 409 } 410 } 411 } 412 } 413 414 @TestTargets({ 415 @TestTargetNew( 416 level = TestLevel.PARTIAL_COMPLETE, 417 method = "write", 418 args = {char[].class} 419 ), 420 @TestTargetNew( 421 level = TestLevel.PARTIAL_COMPLETE, 422 notes = "", 423 method = "read", 424 args = {}, 425 clazz = InputStreamReader.class 426 ) 427 }) 428 public void test_write$C() throws Exception { 429 int upper; 430 InputStreamReader isr = null; 431 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 432 OutputStreamWriter writer = null; 433 434 char[] largeBuffer = new char[BUFFER_SIZE]; 435 for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) { 436 try { 437 baos = new ByteArrayOutputStream(); 438 writer = new OutputStreamWriter(baos, MINIMAL_CHARSETS[i]); 439 440 switch (i) { 441 case 0: 442 upper = 128; 443 break; 444 case 1: 445 upper = 256; 446 break; 447 default: 448 upper = UPPER; 449 } 450 451 int m = 0; 452 for (int c = 0; c < upper; ++c) { 453 largeBuffer[m++] = (char) c; 454 if (m == BUFFER_SIZE) { 455 writer.write(largeBuffer); 456 m = 0; 457 } 458 } 459 writer.write(largeBuffer, 0, m); 460 writer.flush(); 461 byte[] result = baos.toByteArray(); 462 463 isr = new InputStreamReader(new ByteArrayInputStream(result), 464 MINIMAL_CHARSETS[i]); 465 int expected = 0, read = 0, j = 0; 466 while (expected < upper) { 467 if (j == read) { 468 read = isr.read(largeBuffer); 469 j = 0; 470 } 471 assertEquals("Error when reading bytes in " 472 + MINIMAL_CHARSETS[i], expected++, largeBuffer[j++]); 473 } 474 } finally { 475 try { 476 if (isr != null) isr.close(); 477 } catch (Exception e) { 478 } 479 try { 480 if (writer != null) writer.close(); 481 } catch (Exception e) { 482 } 483 } 484 } 485 } 486 487 @TestTargetNew( 488 level = TestLevel.PARTIAL_COMPLETE, 489 notes = "", 490 method = "write", 491 args = {char[].class} 492 ) 493 public void test_write_US_ASCII() throws Exception { 494 testEncodeCharset("US-ASCII", 128); 495 } 496 497 @TestTargetNew( 498 level = TestLevel.PARTIAL_COMPLETE, 499 notes = "", 500 method = "write", 501 args = {char[].class} 502 ) 503 public void test_write_ISO_8859_1() throws Exception { 504 testEncodeCharset("ISO-8859-1", 256); 505 } 506 507 @TestTargetNew( 508 level = TestLevel.PARTIAL_COMPLETE, 509 notes = "", 510 method = "write", 511 args = {char[].class} 512 ) 513 public void test_write_UTF_16BE() throws Exception { 514 testEncodeCharset("UTF-16BE", 0xd800); 515 } 516 517 @TestTargetNew( 518 level = TestLevel.PARTIAL_COMPLETE, 519 notes = "", 520 method = "write", 521 args = {char[].class} 522 ) 523 public void test_write_UTF_16LE() throws Exception { 524 testEncodeCharset("UTF-16LE", 0xd800); 525 } 526 527 @TestTargetNew( 528 level = TestLevel.PARTIAL_COMPLETE, 529 notes = "", 530 method = "write", 531 args = {char[].class} 532 ) 533 public void test_write_UTF_16() throws Exception { 534 testEncodeCharset("UTF-16", 0xd800); 535 } 536 537 @TestTargetNew( 538 level = TestLevel.PARTIAL_COMPLETE, 539 notes = "", 540 method = "write", 541 args = {char[].class} 542 ) 543 public void test_write_UTF_8() throws Exception { 544 testEncodeCharset("UTF-8", 0xd800); 545 } 546 547 private void testEncodeCharset(String charset, int maxChar) throws Exception { 548 char[] chars = new char[maxChar]; 549 for (int i = 0; i < maxChar; i++) { 550 chars[i] = (char) i; 551 } 552 553 // to byte array 554 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 555 OutputStreamWriter charsOut = new OutputStreamWriter(bytesOut, charset); 556 charsOut.write(chars); 557 charsOut.flush(); 558 559 // decode from byte array, one character at a time 560 ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytesOut.toByteArray()); 561 InputStreamReader charsIn = new InputStreamReader(bytesIn, charset); 562 for (int i = 0; i < maxChar; i++) { 563 assertEquals(i, charsIn.read()); 564 } 565 assertEquals(-1, charsIn.read()); 566 567 // decode from byte array, using byte buffers 568 bytesIn = new ByteArrayInputStream(bytesOut.toByteArray()); 569 charsIn = new InputStreamReader(bytesIn, charset); 570 char[] decoded = new char[maxChar]; 571 for (int r = 0; r < maxChar; ) { 572 r += charsIn.read(decoded, r, maxChar - r); 573 } 574 assertEquals(-1, charsIn.read()); 575 for (int i = 0; i < maxChar; i++) { 576 assertEquals(i, decoded[i]); 577 } 578 } 579 580 /** 581 * @tests java.io.OutputStreamWriter#getEncoding() 582 */ 583 @TestTargetNew( 584 level = TestLevel.COMPLETE, 585 notes = "", 586 method = "getEncoding", 587 args = {} 588 ) 589 public void test_getEncoding() throws IOException { 590 OutputStreamWriter writer; 591 writer = new OutputStreamWriter(new Support_OutputStream(), "utf-8"); 592 assertEquals("Test 1: Incorrect encoding returned.", 593 Charset.forName("utf-8"), 594 Charset.forName(writer.getEncoding())); 595 596 writer.close(); 597 assertNull("Test 2: getEncoding() did not return null for a closed writer.", 598 writer.getEncoding()); 599 } 600 601 /** 602 * @tests java.io.OutputStreamWriter#write(char[], int, int) 603 */ 604 @TestTargetNew( 605 level = TestLevel.PARTIAL_COMPLETE, 606 notes = "", 607 method = "write", 608 args = {char[].class, int.class, int.class} 609 ) 610 public void test_write$CII() throws IOException { 611 char[] chars = testString.toCharArray(); 612 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 613 Support_OutputStream out = new Support_OutputStream(500); 614 OutputStreamWriter writer; 615 616 writer = new OutputStreamWriter(out, "utf-8"); 617 618 try { 619 writer.write(chars, -1, 1); 620 fail("Test 1: IndexOutOfBoundsException expected."); 621 } catch (IndexOutOfBoundsException e) { 622 // Expected 623 } 624 625 try { 626 writer.write(chars, 0, -1); 627 fail("Test 2: IndexOutOfBoundsException expected."); 628 } catch (IndexOutOfBoundsException e) { 629 // Expected 630 } 631 632 try { 633 writer.write(new char[0], 0, 1); 634 fail("Test 3: IndexOutOfBoundsException expected."); 635 } catch (IndexOutOfBoundsException e) { 636 // Expected 637 } 638 639 try { 640 writer.write((char[]) null, 0, 1); 641 fail("Test 4: NullPointerException expected."); 642 } catch (NullPointerException e) { 643 // Expected 644 } 645 646 try { 647 writer.write(chars, 1, chars.length); 648 fail("Test 5a: IndexOutOfBoundsException expected."); 649 } catch (IndexOutOfBoundsException e) { 650 // Expected 651 } 652 try { 653 writer.write(chars, 0, chars.length + 1); 654 fail("Test 5b: IndexOutOfBoundsException expected."); 655 } catch (IndexOutOfBoundsException e) { 656 // Expected 657 } 658 try { 659 writer.write(chars, chars.length, 1); 660 fail("Test 5c: IndexOutOfBoundsException expected."); 661 } catch (IndexOutOfBoundsException e) { 662 // Expected 663 } 664 try { 665 writer.write(chars, chars.length + 1, 0); 666 fail("Test 5d: IndexOutOfBoundsException expected."); 667 } catch (IndexOutOfBoundsException e) { 668 // Expected 669 } 670 671 out.setThrowsException(true); 672 try { 673 for (int i = 0; i < 200; i++) { 674 writer.write(chars, 0, chars.length); 675 } 676 fail("Test 6: IOException expected."); 677 } catch (IOException e) { 678 // Expected 679 } 680 out.setThrowsException(false); 681 682 writer.close(); 683 writer = new OutputStreamWriter(baos, "utf-8"); 684 writer.write(chars, 1, 2); 685 writer.flush(); 686 assertEquals("Test 7: write(char[], int, int) has not produced the " + 687 "expected content in the output stream.", 688 "hi", baos.toString("utf-8")); 689 690 writer.write(chars, 0, chars.length); 691 writer.flush(); 692 assertEquals("Test 8: write(char[], int, int) has not produced the " + 693 "expected content in the output stream.", 694 "hi" + testString, baos.toString("utf-8")); 695 696 writer.close(); 697 try { 698 writer.write((char[]) null, -1, -1); 699 fail("Test 9: IOException expected."); 700 } catch (IOException e) { 701 // Expected 702 } 703 } 704 705 /** 706 * @tests java.io.OutputStreamWriter#write(int) 707 */ 708 @TestTargetNew( 709 level = TestLevel.PARTIAL_COMPLETE, 710 notes = "", 711 method = "write", 712 args = {int.class} 713 ) 714 public void test_writeI() throws IOException { 715 Support_OutputStream out = new Support_OutputStream(500); 716 OutputStreamWriter writer; 717 718 out.setThrowsException(true); 719 writer = new OutputStreamWriter(out, "utf-8"); 720 try { 721 // Since there is an internal buffer in the encoder, more than 722 // one character needs to be written. 723 for (int i = 0; i < 200; i++) { 724 for (int j = 0; j < testString.length(); j++) { 725 writer.write(testString.charAt(j)); 726 } 727 } 728 fail("Test 1: IOException expected."); 729 } catch (IOException e) { 730 // Expected 731 } 732 out.setThrowsException(false); 733 writer.close(); 734 735 writer = new OutputStreamWriter(out, "utf-8"); 736 writer.write(1); 737 writer.flush(); 738 String str = new String(out.toByteArray(), "utf-8"); 739 assertEquals("Test 2: ", "\u0001", str); 740 741 writer.write(2); 742 writer.flush(); 743 str = new String(out.toByteArray(), "utf-8"); 744 assertEquals("Test 3: ", "\u0001\u0002", str); 745 746 writer.write(-1); 747 writer.flush(); 748 str = new String(out.toByteArray(), "utf-8"); 749 assertEquals("Test 4: ", "\u0001\u0002\uffff", str); 750 751 writer.write(0xfedcb); 752 writer.flush(); 753 str = new String(out.toByteArray(), "utf-8"); 754 assertEquals("Test 5: ", "\u0001\u0002\uffff\uedcb", str); 755 756 writer.close(); 757 try { 758 writer.write(1); 759 fail("Test 6: IOException expected."); 760 } catch (IOException e) { 761 // Expected 762 } 763 } 764 765 /** 766 * @tests java.io.OutputStreamWriter#write(java.lang.String, int, int) 767 */ 768 @TestTargetNew( 769 level = TestLevel.COMPLETE, 770 notes = "", 771 method = "write", 772 args = {java.lang.String.class, int.class, int.class} 773 ) 774 public void test_writeLjava_lang_StringII() throws IOException { 775 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 776 Support_OutputStream out = new Support_OutputStream(500); 777 OutputStreamWriter writer; 778 779 writer = new OutputStreamWriter(out, "utf-8"); 780 781 try { 782 writer.write("Lorem", -1, 0); 783 fail("Test 1: IndexOutOfBoundsException expected."); 784 } catch (IndexOutOfBoundsException e) { 785 // Expected 786 } 787 788 try { 789 writer.write("Lorem", 0, -1); 790 fail("Test 2: IndexOutOfBoundsException expected."); 791 } catch (IndexOutOfBoundsException e) { 792 // Expected 793 } 794 795 try { 796 writer.write("", 0, 1); 797 fail("Test 3: IndexOutOfBoundsException expected."); 798 } catch (IndexOutOfBoundsException e) { 799 // Expected 800 } 801 802 try { 803 writer.write(testString, 1, testString.length()); 804 fail("Test 4a: IndexOutOfBoundsException expected."); 805 } catch (IndexOutOfBoundsException e) { 806 // Expected 807 } 808 809 try { 810 writer.write(testString, 0, testString.length() + 1); 811 fail("Test 4b: IndexOutOfBoundsException expected."); 812 } catch (IndexOutOfBoundsException e) { 813 // Expected 814 } 815 816 try { 817 writer.write(testString, testString.length(), 1); 818 fail("Test 4c: IndexOutOfBoundsException expected."); 819 } catch (IndexOutOfBoundsException e) { 820 // Expected 821 } 822 823 try { 824 writer.write(testString, testString.length() + 1, 0); 825 fail("Test 4d: IndexOutOfBoundsException expected."); 826 } catch (IndexOutOfBoundsException e) { 827 // Expected 828 } 829 830 try { 831 writer.write((String) null, 0, 1); 832 fail("Test 5: NullPointerException expected."); 833 } catch (NullPointerException e) { 834 // Expected 835 } 836 837 out.setThrowsException(true); 838 try { 839 for (int i = 0; i < 200; i++) { 840 writer.write(testString, 0, testString.length()); 841 } 842 fail("Test 6: IOException expected."); 843 } catch (IOException e) { 844 // Expected 845 } 846 out.setThrowsException(false); 847 848 writer.close(); 849 writer = new OutputStreamWriter(baos, "utf-8"); 850 851 writer.write("abc", 1, 2); 852 writer.flush(); 853 assertEquals("Test 7: write(String, int, int) has not produced the " + 854 "expected content in the output stream.", 855 "bc", baos.toString("utf-8")); 856 857 writer.write(testString, 0, testString.length()); 858 writer.flush(); 859 assertEquals("Test 7: write(String, int, int) has not produced the " + 860 "expected content in the output stream.", 861 "bc" + testString, baos.toString("utf-8")); 862 863 writer.close(); 864 try { 865 writer.write("abc", 0, 1); 866 fail("Test 8: IOException expected."); 867 } catch (IOException e) { 868 // Expected 869 } 870 } 871 872 private void openInputStream() { 873 try { 874 isr = new InputStreamReader(new ByteArrayInputStream(fos.toByteArray()), "UTF-8"); 875 } catch (UnsupportedEncodingException e) { 876 fail("UTF-8 not supported"); 877 } 878 } 879 880} 881