1 /* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package tests.api.java.nio.charset; 17 18 import dalvik.annotation.TestTargetClass; 19 import dalvik.annotation.TestTargets; 20 import dalvik.annotation.TestTargetNew; 21 import dalvik.annotation.TestLevel; 22 23 import java.io.UnsupportedEncodingException; 24 import java.nio.ByteBuffer; 25 import java.nio.CharBuffer; 26 import java.nio.charset.CharacterCodingException; 27 import java.nio.charset.Charset; 28 import java.nio.charset.CharsetDecoder; 29 import java.nio.charset.CoderResult; 30 import java.nio.charset.CodingErrorAction; 31 import java.nio.charset.MalformedInputException; 32 import java.nio.charset.UnmappableCharacterException; 33 34 import junit.framework.TestCase; 35 36 @TestTargetClass(CharsetDecoder.class) 37 38 /** 39 * Super class for concrete charset test suites. 40 */ 41 public class AbstractCharsetDecoderTestCase extends TestCase { 42 43 Charset cs; 44 45 // Target decoder (tobj): 46 protected static CharsetDecoder decoder; 47 48 static final String unistr = " buffer";// \u8000\u8001\u00a5\u3000\r\n"; 49 50 byte[] unibytes; 51 52 String bom = ""; 53 54 55 protected void setUp() throws Exception { 56 super.setUp(); 57 decoder = cs.newDecoder(); 58 } 59 60 protected void tearDown() throws Exception { 61 super.tearDown(); 62 } 63 64 65 @TestTargets({ 66 @TestTargetNew( 67 level = TestLevel.PARTIAL_COMPLETE, 68 method = "charset", 69 args = {} 70 ), 71 @TestTargetNew( 72 level = TestLevel.PARTIAL_COMPLETE, 73 method = "detectedCharset", 74 args = {} 75 ), 76 @TestTargetNew( 77 level = TestLevel.PARTIAL_COMPLETE, 78 method = "isCharsetDetected", 79 args = {} 80 ), 81 @TestTargetNew( 82 level = TestLevel.PARTIAL_COMPLETE, 83 method = "isAutoDetecting", 84 args = {} 85 ), 86 @TestTargetNew( 87 level = TestLevel.PARTIAL_COMPLETE, 88 method = "malformedInputAction", 89 args = {} 90 ), 91 @TestTargetNew( 92 level = TestLevel.PARTIAL_COMPLETE, 93 method = "unmappableCharacterAction", 94 args = {} 95 ), 96 @TestTargetNew( 97 level = TestLevel.PARTIAL_COMPLETE, 98 method = "replacement", 99 args = {} 100 ) 101 }) 102 public void testDefaultValues() { 103 assertSame(cs, decoder.charset()); 104 try { 105 decoder.detectedCharset(); 106 fail("should unsupported"); 107 } catch (UnsupportedOperationException e) { 108 } 109 try { 110 assertTrue(decoder.isCharsetDetected()); 111 fail("should unsupported"); 112 } catch (UnsupportedOperationException e) { 113 } 114 assertFalse(decoder.isAutoDetecting()); 115 assertSame(CodingErrorAction.REPORT, decoder.malformedInputAction()); 116 assertSame(CodingErrorAction.REPORT, decoder 117 .unmappableCharacterAction()); 118 assertEquals(decoder.replacement(), "\ufffd"); 119 } 120 121 122 123 /* 124 * test onMalformedInput 125 */ 126 @TestTargets({ 127 @TestTargetNew( 128 level = TestLevel.PARTIAL_COMPLETE, 129 method = "malformedInputAction", 130 args = {} 131 ), 132 @TestTargetNew( 133 level = TestLevel.PARTIAL_COMPLETE, 134 method = "onMalformedInput", 135 args = {java.nio.charset.CodingErrorAction.class} 136 ) 137 }) 138 public void testOnMalformedInput() { 139 assertSame(CodingErrorAction.REPORT, decoder.malformedInputAction()); 140 try { 141 decoder.onMalformedInput(null); 142 fail("should throw null pointer exception"); 143 } catch (IllegalArgumentException e) { 144 } 145 decoder.onMalformedInput(CodingErrorAction.IGNORE); 146 assertSame(CodingErrorAction.IGNORE, decoder.malformedInputAction()); 147 } 148 149 /* 150 * test unmappableCharacter 151 */ 152 @TestTargets({ 153 @TestTargetNew( 154 level = TestLevel.PARTIAL_COMPLETE, 155 method = "unmappableCharacterAction", 156 args = {} 157 ), 158 @TestTargetNew( 159 level = TestLevel.PARTIAL_COMPLETE, 160 method = "onUnmappableCharacter", 161 args = {java.nio.charset.CodingErrorAction.class} 162 ) 163 }) 164 public void testOnUnmappableCharacter() { 165 assertSame(CodingErrorAction.REPORT, decoder 166 .unmappableCharacterAction()); 167 try { 168 decoder.onUnmappableCharacter(null); 169 fail("should throw null pointer exception"); 170 } catch (IllegalArgumentException e) { 171 } 172 decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); 173 assertSame(CodingErrorAction.IGNORE, decoder 174 .unmappableCharacterAction()); 175 } 176 177 /* 178 * test replaceWith 179 */ 180 @TestTargets({ 181 @TestTargetNew( 182 level = TestLevel.PARTIAL_COMPLETE, 183 method = "replaceWith", 184 args = {java.lang.String.class} 185 ), 186 @TestTargetNew( 187 level = TestLevel.PARTIAL_COMPLETE, 188 method = "replacement", 189 args = {} 190 ) 191 }) 192 public void testReplaceWith() { 193 try { 194 decoder.replaceWith(null); 195 fail("should throw null pointer exception"); 196 } catch (IllegalArgumentException e) { 197 } 198 try { 199 decoder.replaceWith(""); 200 fail("should throw null pointer exception"); 201 } catch (IllegalArgumentException e) { 202 } 203 try { 204 decoder.replaceWith("testReplaceWith"); 205 fail("should throw illegal argument exception"); 206 } catch (IllegalArgumentException e) { 207 } 208 209 decoder.replaceWith("a"); 210 assertSame("a", decoder.replacement()); 211 } 212 213 /* 214 * Class under test for CharBuffer decode(ByteBuffer) 215 */ 216 @TestTargets({ 217 @TestTargetNew( 218 level = TestLevel.PARTIAL_COMPLETE, 219 method = "decode", 220 args = {java.nio.ByteBuffer.class} 221 ) 222 }) 223 public void testDecodeByteBuffer() throws CharacterCodingException { 224 implTestDecodeByteBuffer(); 225 } 226 227 void implTestDecodeByteBuffer() throws CharacterCodingException { 228 // Null pointer 229 try { 230 decoder.decode(null); 231 fail("should throw null pointer exception"); 232 } catch (NullPointerException e) { 233 } 234 235 // empty input buffer 236 CharBuffer out = decoder.decode(ByteBuffer.allocate(0)); 237 assertCharBufferValue(out, ""); 238 239 // normal case 240 ByteBuffer in = ByteBuffer.wrap(getUnibytes()); 241 out = decoder.decode(in); 242 assertEquals(out.position(), 0); 243 assertEquals(out.limit(), unistr.length()); 244 assertEquals(out.remaining(), unistr.length()); 245 assertEquals(new String(out.array(), 0, out.limit()), unistr); 246 } 247 248 @TestTargetNew( 249 level = TestLevel.PARTIAL_COMPLETE, 250 method = "decode", 251 args = {java.nio.ByteBuffer.class} 252 ) 253 public void testDecodeByteBufferException() 254 throws CharacterCodingException, UnsupportedEncodingException { 255 CharBuffer out; 256 ByteBuffer in; 257 String replaceStr = decoder.replacement() + " buffer"; 258 259 // MalformedException: 260 decoder.onMalformedInput(CodingErrorAction.REPORT); 261 decoder.onUnmappableCharacter(CodingErrorAction.REPORT); 262 in = getMalformByteBuffer(); 263 if (in != null) { 264 try { 265 CharBuffer buffer = decoder.decode(in); 266 assertTrue(buffer.remaining() > 0); 267 fail("should throw MalformedInputException"); 268 } catch (MalformedInputException e) { 269 } 270 271 decoder.reset(); 272 in.rewind(); 273 decoder.onMalformedInput(CodingErrorAction.IGNORE); 274 out = decoder.decode(in); 275 assertCharBufferValue(out, " buffer"); 276 277 decoder.reset(); 278 in.rewind(); 279 decoder.onMalformedInput(CodingErrorAction.REPLACE); 280 out = decoder.decode(in); 281 assertCharBufferValue(out, replaceStr); 282 } 283 284 // Unmapped Exception: 285 decoder.onMalformedInput(CodingErrorAction.REPORT); 286 decoder.onUnmappableCharacter(CodingErrorAction.REPORT); 287 in = getUnmappedByteBuffer(); 288 if (in != null) { 289 try { 290 decoder.decode(in); 291 fail("should throw UnmappableCharacterException"); 292 } catch (UnmappableCharacterException e) { 293 } 294 295 decoder.reset(); 296 in.rewind(); 297 decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); 298 out = decoder.decode(in); 299 assertCharBufferValue(out, " buffer"); 300 301 decoder.reset(); 302 in.rewind(); 303 decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); 304 out = decoder.decode(in); 305 assertCharBufferValue(out, replaceStr); 306 } 307 308 // RuntimeException 309 try { 310 decoder.decode(getExceptionByteArray()); 311 fail("should throw runtime exception"); 312 } catch (RuntimeException e) { 313 } 314 } 315 316 /* 317 * Class under test for CoderResult decode(ByteBuffer, CharBuffer, boolean) 318 */ 319 320 @TestTargetNew( 321 level = TestLevel.PARTIAL_COMPLETE, 322 method = "decode", 323 args = {java.nio.ByteBuffer.class, java.nio.CharBuffer.class, boolean.class} 324 ) 325 public void testDecodeByteBufferCharBufferboolean() { 326 implTestDecodeByteBufferCharBufferboolean(); 327 } 328 329 void implTestDecodeByteBufferCharBufferboolean() { 330 byte[] gb = getUnibytes(); 331 ByteBuffer in = ByteBuffer.wrap(gb); 332 CharBuffer out = CharBuffer.allocate(100); 333 334 // Null pointer 335 try { 336 decoder.decode(null, out, true); 337 fail("should throw null pointer exception"); 338 } catch (NullPointerException e) { 339 } 340 try { 341 decoder.decode(in, null, true); 342 fail("should throw null pointer exception"); 343 } catch (NullPointerException e) { 344 } 345 346 // normal case, one complete operation 347 decoder.reset(); 348 in.rewind(); 349 out.rewind(); 350 assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true)); 351 assertEquals(out.limit(), 100); 352 assertEquals(out.position(), unistr.length()); 353 assertEquals(out.remaining(), 100 - unistr.length()); 354 assertEquals(out.capacity(), 100); 355 assertCharBufferValue(out, unistr); 356 decoder.flush(out); 357 358 // normal case, one complete operation, but call twice, first time set 359 // endOfInput to false 360 decoder.reset(); 361 in.rewind(); 362 out.clear(); 363 assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false)); 364 assertEquals(out.limit(), 100); 365 assertEquals(out.position(), unistr.length()); 366 assertEquals(out.remaining(), 100 - unistr.length()); 367 assertEquals(out.capacity(), 100); 368 assertCharBufferValue(out, unistr); 369 370 decoder.reset(); 371 in.rewind(); 372 out.clear(); 373 assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false)); 374 in = ByteBuffer.wrap(unibytes); 375 assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false)); 376 in.rewind(); 377 assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true)); 378 assertEquals(out.limit(), 100); 379 assertTrue(out.position() > 0); 380 assertEquals(out.remaining(), out.capacity() - out.position()); 381 assertEquals(out.capacity(), 100); 382 assertCharBufferValue(out, unistr + unistr + unistr); 383 384 // overflow 385 out = CharBuffer.allocate(4); 386 decoder.reset(); 387 in = ByteBuffer.wrap(getUnibytes()); 388 out.rewind(); 389 assertSame(CoderResult.OVERFLOW, decoder.decode(in, out, false)); 390 391 assertEquals(new String(out.array()), unistr.substring(0, 4)); 392 out = CharBuffer.allocate(100); 393 assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false)); 394 assertCharBufferValue(out, unistr.substring(4)); 395 in.rewind(); 396 out = CharBuffer.allocate(100); 397 assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true)); 398 assertCharBufferValue(out, bom + unistr); 399 } 400 401 @TestTargetNew( 402 level = TestLevel.PARTIAL_COMPLETE, 403 method = "decode", 404 args = {java.nio.ByteBuffer.class, java.nio.CharBuffer.class, boolean.class} 405 ) 406 public void testDecodeCharBufferByteBufferbooleanExceptionTrue() 407 throws CharacterCodingException, UnsupportedEncodingException { 408 implTestDecodeCharBufferByteBufferbooleanException(true); 409 } 410 411 @TestTargetNew( 412 level = TestLevel.PARTIAL_COMPLETE, 413 method = "decode", 414 args = {java.nio.ByteBuffer.class, java.nio.CharBuffer.class, boolean.class} 415 ) 416 public void testDecodeCharBufferByteBufferbooleanExceptionFalse() 417 throws CharacterCodingException, UnsupportedEncodingException { 418 implTestDecodeCharBufferByteBufferbooleanException(false); 419 } 420 421 void implTestDecodeCharBufferByteBufferbooleanException(boolean endOfInput) 422 throws CharacterCodingException, UnsupportedEncodingException { 423 CharBuffer out; 424 ByteBuffer in; 425 426 // Unmapped Exception: 427 in = getUnmappedByteBuffer(); 428 out = CharBuffer.allocate(50); 429 decoder.onMalformedInput(CodingErrorAction.REPORT); 430 if (null != in) { 431 decoder.reset(); 432 decoder.onUnmappableCharacter(CodingErrorAction.REPORT); 433 CoderResult result = decoder.decode(in, out, endOfInput); 434 assertTrue(result.isUnmappable()); 435 436 decoder.reset(); 437 out.clear(); 438 in.rewind(); 439 decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); 440 assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, 441 endOfInput)); 442 assertCharBufferValue(out, " buffer"); 443 444 decoder.reset(); 445 out.clear(); 446 in.rewind(); 447 decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); 448 assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, 449 endOfInput)); 450 assertCharBufferValue(out, decoder.replacement() + " buffer"); 451 } else if (endOfInput) { 452 // System.err.println("Cannot find unmappable byte array for " 453 // + cs.name()); 454 } 455 456 // MalformedException: 457 in = getMalformByteBuffer(); 458 out = CharBuffer.allocate(50); 459 decoder.onUnmappableCharacter(CodingErrorAction.REPORT); 460 if (null != in) { 461 decoder.onMalformedInput(CodingErrorAction.REPORT); 462 CoderResult result = decoder.decode(in, out, endOfInput); 463 assertTrue(result.isMalformed()); 464 465 decoder.reset(); 466 out.clear(); 467 in.rewind(); 468 decoder.onMalformedInput(CodingErrorAction.IGNORE); 469 assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, 470 endOfInput)); 471 assertCharBufferValue(out, " buffer"); 472 473 decoder.reset(); 474 out.clear(); 475 in.rewind(); 476 decoder.onMalformedInput(CodingErrorAction.REPLACE); 477 assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, 478 endOfInput)); 479 assertCharBufferValue(out, decoder.replacement() + " buffer"); 480 } else if (endOfInput) { 481 // System.err.println("Cannot find malform byte array for " 482 // + cs.name()); 483 } 484 485 // RuntimeException 486 in = getExceptionByteArray(); 487 try { 488 decoder.decode(in, out, endOfInput); 489 fail("should throw runtime exception"); 490 } catch (RuntimeException e) { 491 } 492 } 493 494 ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException { 495 // "runtime" 496 return ByteBuffer 497 .wrap(new byte[] { 114, 117, 110, 116, 105, 109, 101 }); 498 } 499 500 ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException { 501 // "unmap buffer" 502 byte[] ba = new byte[] { 117, 110, 109, 97, 112, 32, 98, 117, 102, 102, 503 101, 114 }; 504 return ByteBuffer.wrap(ba); 505 } 506 507 ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException { 508 // "malform buffer" 509 byte[] ba = new byte[] { 109, 97, 108, 102, 111, 114, 109, 32, 98, 117, 510 102, 102, 101, 114 }; 511 return ByteBuffer.wrap(ba); 512 } 513 514 void assertCharBufferValue(CharBuffer out, String expected) { 515 if (out.position() != 0) { 516 out.flip(); 517 } 518 assertEquals(new String(out.array(), 0, out.limit()), expected); 519 } 520 521 /* 522 * test flush 523 */ 524 @TestTargetNew( 525 level = TestLevel.PARTIAL_COMPLETE, 526 method = "flush", 527 args = {java.nio.CharBuffer.class} 528 ) 529 public void testFlush() throws CharacterCodingException { 530 CharBuffer out = CharBuffer.allocate(10); 531 ByteBuffer in = ByteBuffer.wrap(new byte[] { 12, 12 }); 532 decoder.decode(in, out, true); 533 assertSame(CoderResult.UNDERFLOW, decoder.flush(out)); 534 535 decoder.reset(); 536 decoder.decode((ByteBuffer) in.rewind(), (CharBuffer) out.rewind(), 537 true); 538 assertSame(CoderResult.UNDERFLOW, decoder 539 .flush(CharBuffer.allocate(10))); 540 } 541 542 543 /* 544 * ---------------------------------- methods to test illegal state 545 * ----------------------------------- 546 */ 547 548 // Normal case: just after reset, and it also means reset can be done 549 // anywhere 550 @TestTargetNew( 551 level = TestLevel.PARTIAL_COMPLETE, 552 method = "reset", 553 args = {} 554 ) 555 public void testResetIllegalState() throws CharacterCodingException { 556 byte[] gb = getUnibytes(); 557 decoder.reset(); 558 decoder.decode(ByteBuffer.wrap(gb)); 559 decoder.reset(); 560 decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(3), false); 561 decoder.reset(); 562 decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(3), true); 563 decoder.reset(); 564 } 565 566 @TestTargets({ 567 @TestTargetNew( 568 level = TestLevel.PARTIAL_COMPLETE, 569 method = "flush", 570 args = {java.nio.CharBuffer.class} 571 ), 572 @TestTargetNew( 573 level = TestLevel.PARTIAL_COMPLETE, 574 method = "reset", 575 args = {} 576 ) 577 }) 578 public void testFlushIllegalState() throws CharacterCodingException { 579 ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 }); 580 CharBuffer out = CharBuffer.allocate(5); 581 // Normal case: after decode with endOfInput is true 582 decoder.reset(); 583 decoder.decode(in, out, true); 584 out.rewind(); 585 CoderResult result = decoder.flush(out); 586 assertSame(result, CoderResult.UNDERFLOW); 587 588 // Illegal state: flush twice 589 try { 590 decoder.flush(out); 591 fail("should throw IllegalStateException"); 592 } catch (IllegalStateException e) { 593 } 594 595 // Illegal state: flush after decode with endOfInput is false 596 decoder.reset(); 597 decoder.decode(in, out, false); 598 try { 599 decoder.flush(out); 600 fail("should throw IllegalStateException"); 601 } catch (IllegalStateException e) { 602 } 603 } 604 605 byte[] getUnibytes() { 606 return unibytes; 607 } 608 609 // test illegal states for decode facade 610 @TestTargets({ 611 @TestTargetNew( 612 level = TestLevel.PARTIAL_COMPLETE, 613 method = "decode", 614 args = {java.nio.ByteBuffer.class} 615 ), 616 @TestTargetNew( 617 level = TestLevel.PARTIAL_COMPLETE, 618 method = "decode", 619 args = {java.nio.ByteBuffer.class, java.nio.CharBuffer.class, boolean.class} 620 ) 621 }) 622 public void testDecodeFacadeIllegalState() throws CharacterCodingException { 623 // decode facade can be execute in anywhere 624 byte[] gb = getUnibytes(); 625 ByteBuffer in = ByteBuffer.wrap(gb); 626 // Normal case: just created 627 decoder.decode(in); 628 in.rewind(); 629 630 // Normal case: just after decode facade 631 decoder.decode(in); 632 in.rewind(); 633 634 // Normal case: just after decode with that endOfInput is true 635 decoder.reset(); 636 decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(30), true); 637 decoder.decode(in); 638 in.rewind(); 639 640 // Normal case:just after decode with that endOfInput is false 641 decoder.reset(); 642 decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(30), false); 643 decoder.decode(in); 644 in.rewind(); 645 646 // Normal case: just after flush 647 decoder.reset(); 648 decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(30), true); 649 decoder.flush(CharBuffer.allocate(10)); 650 decoder.decode(in); 651 in.rewind(); 652 } 653 654 // test illegal states for two decode method with endOfInput is true 655 @TestTargetNew( 656 level = TestLevel.PARTIAL_COMPLETE, 657 method = "decode", 658 args = {java.nio.ByteBuffer.class, java.nio.CharBuffer.class, boolean.class} 659 ) 660 public void testDecodeTrueIllegalState() throws CharacterCodingException { 661 ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 }); 662 CharBuffer out = CharBuffer.allocate(100); 663 // Normal case: just created 664 decoder.decode(in, out, true); 665 in.rewind(); 666 out.rewind(); 667 668 // Normal case: just after decode with that endOfInput is true 669 decoder.reset(); 670 decoder.decode(in, CharBuffer.allocate(30), true); 671 in.rewind(); 672 decoder.decode(in, out, true); 673 in.rewind(); 674 out.rewind(); 675 676 // Normal case:just after decode with that endOfInput is false 677 decoder.reset(); 678 decoder.decode(in, CharBuffer.allocate(30), false); 679 in.rewind(); 680 decoder.decode(in, out, true); 681 in.rewind(); 682 out.rewind(); 683 684 // Illegal state: just after flush 685 decoder.reset(); 686 decoder.decode(in, CharBuffer.allocate(30), true); 687 decoder.flush(CharBuffer.allocate(10)); 688 in.rewind(); 689 try { 690 decoder.decode(in, out, true); 691 fail("should illegal state"); 692 } catch (IllegalStateException e) { 693 } 694 in.rewind(); 695 out.rewind(); 696 697 } 698 699 // test illegal states for two decode method with endOfInput is false 700 @TestTargetNew( 701 level = TestLevel.PARTIAL_COMPLETE, 702 method = "decode", 703 args = {java.nio.ByteBuffer.class, java.nio.CharBuffer.class, boolean.class} 704 ) 705 public void testDecodeFalseIllegalState() throws CharacterCodingException { 706 ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 }); 707 CharBuffer out = CharBuffer.allocate(5); 708 // Normal case: just created 709 decoder.decode(in, out, false); 710 in.rewind(); 711 out.rewind(); 712 713 // Illegal state: just after decode facade 714 decoder.reset(); 715 decoder.decode(in); 716 in.rewind(); 717 try { 718 decoder.decode(in, out, false); 719 fail("should illegal state"); 720 } catch (IllegalStateException e) { 721 } 722 in.rewind(); 723 out.rewind(); 724 725 // Illegal state: just after decode with that endOfInput is true 726 decoder.reset(); 727 decoder.decode(in, CharBuffer.allocate(30), true); 728 in.rewind(); 729 try { 730 decoder.decode(in, out, false); 731 fail("should illegal state"); 732 } catch (IllegalStateException e) { 733 } 734 in.rewind(); 735 out.rewind(); 736 737 // Normal case:just after decode with that endOfInput is false 738 decoder.reset(); 739 decoder.decode(in, CharBuffer.allocate(30), false); 740 in.rewind(); 741 decoder.decode(in, out, false); 742 in.rewind(); 743 out.rewind(); 744 745 // Illegal state: just after flush 746 decoder.reset(); 747 decoder.decode(in, CharBuffer.allocate(30), true); 748 in.rewind(); 749 decoder.flush(CharBuffer.allocate(10)); 750 try { 751 decoder.decode(in, out, false); 752 fail("should illegal state"); 753 } catch (IllegalStateException e) { 754 } 755 } 756 757 /* 758 * --------------------------------- illegal state test end 759 * --------------------------------- 760 */ 761 762 } 763