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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package tests.api.java.nio.charset;
18 import dalvik.annotation.TestTargetClass;
19 import dalvik.annotation.TestTargets;
20 import dalvik.annotation.TestTargetNew;
21 import dalvik.annotation.TestLevel;
23 import java.nio.ByteBuffer;
24 import java.nio.CharBuffer;
25 import java.nio.charset.CharacterCodingException;
26 import java.nio.charset.Charset;
27 import java.nio.charset.CharsetDecoder;
28 import java.nio.charset.CharsetEncoder;
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 import java.nio.charset.UnsupportedCharsetException;
34 import java.util.Arrays;
36 import junit.framework.TestCase;
38 @TestTargetClass(CharsetEncoder.class)
41 * Super class for concrete charset test suites.
43 public class AbstractCharsetEncoderTestCase extends TestCase {
47 // Target encoder (tobj):
48 CharsetEncoder encoder;
50 static final String unistr = " buffer";// \u8000\u8001\u00a5\u3000\r\n";
52 byte[] unibytes = new byte[] { 32, 98, 117, 102, 102, 101, 114 };
55 // default for Charset abstract class
56 byte[] defaultReplacement = new byte[] { 63 };
58 // specific for Charset implementation subclass
59 byte[] specifiedReplacement = new byte[] { 26 };
61 byte[] unibytesWithRep = null;
63 byte[] surrogate = new byte[0];
66 protected void setUp() throws Exception {
68 encoder = cs.newEncoder();
69 if (null == unibytesWithRep) {
70 byte[] replacement = encoder.replacement();
71 unibytesWithRep = new byte[replacement.length + unibytes.length];
72 System.arraycopy(replacement, 0, unibytesWithRep, 0,
74 System.arraycopy(unibytes, 0, unibytesWithRep, replacement.length,
79 protected void tearDown() throws Exception {
85 * Class under test for boolean canEncode(char)
88 level = TestLevel.PARTIAL_COMPLETE,
92 public void testCanEncodechar() throws CharacterCodingException {
93 // for non-mapped char
94 assertTrue(encoder.canEncode('\uc2c0'));
95 // surrogate char for unicode
96 // 1st byte: d800-dbff
97 // 2nd byte: dc00-dfff
98 assertTrue(encoder.canEncode('\ud800'));
99 // valid surrogate pair
100 assertTrue(encoder.canEncode('\udc00'));
103 /*-----------------------------------------
104 * Class under test for illegal state case
105 * methods which can change internal states are two encode, flush, two canEncode, reset
106 * -----------------------------------------
109 // Normal case: just after reset, and it also means reset can be done
113 level = TestLevel.PARTIAL_COMPLETE,
114 method = "canEncode",
118 level = TestLevel.PARTIAL_COMPLETE,
119 method = "canEncode",
120 args = {java.lang.CharSequence.class}
123 level = TestLevel.PARTIAL_COMPLETE,
125 args = {java.nio.CharBuffer.class}
128 level = TestLevel.PARTIAL_COMPLETE,
130 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
133 level = TestLevel.PARTIAL_COMPLETE,
138 public void testResetIllegalState() throws CharacterCodingException {
139 assertSame(encoder, encoder.reset());
140 encoder.canEncode('\ud901');
141 assertSame(encoder, encoder.reset());
142 encoder.canEncode("\ud901\udc00");
143 assertSame(encoder, encoder.reset());
144 encoder.encode(CharBuffer.wrap("aaa"));
145 assertSame(encoder, encoder.reset());
146 encoder.encode(CharBuffer.wrap("aaa"), ByteBuffer.allocate(3), false);
147 assertSame(encoder, encoder.reset());
148 encoder.encode(CharBuffer.wrap("aaa"), ByteBuffer.allocate(3), true);
149 assertSame(encoder, encoder.reset());
154 level = TestLevel.PARTIAL_COMPLETE,
159 level = TestLevel.PARTIAL_COMPLETE,
161 args = {java.nio.ByteBuffer.class}
164 public void testFlushIllegalState() throws CharacterCodingException {
165 CharBuffer in = CharBuffer.wrap("aaa");
166 ByteBuffer out = ByteBuffer.allocate(5);
168 // Normal case: after encode with endOfInput is true
169 assertSame(encoder, encoder.reset());
170 encoder.encode(in, out, true);
172 CoderResult result = encoder.flush(out);
174 // Illegal state: flush twice
177 fail("should throw IllegalStateException");
178 } catch (IllegalStateException e) {
181 // Illegal state: flush after encode with endOfInput is false
182 assertSame(encoder, encoder.reset());
183 encoder.encode(in, out, false);
186 fail("should throw IllegalStateException");
187 } catch (IllegalStateException e) {
191 // test illegal states for encode facade
194 level = TestLevel.PARTIAL_COMPLETE,
196 args = {java.nio.CharBuffer.class}
199 level = TestLevel.PARTIAL_COMPLETE,
200 method = "canEncode",
204 level = TestLevel.PARTIAL_COMPLETE,
205 method = "canEncode",
206 args = {java.lang.CharSequence.class}
209 level = TestLevel.PARTIAL_COMPLETE,
211 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
214 level = TestLevel.PARTIAL_COMPLETE,
216 args = {java.nio.ByteBuffer.class}
219 public void testEncodeFacadeIllegalState() throws CharacterCodingException {
220 // encode facade can be execute in anywhere
221 CharBuffer in = CharBuffer.wrap("aaa");
222 // Normal case: just created
226 // Normal case: just after encode facade
230 // Normal case: just after canEncode
231 assertSame(encoder, encoder.reset());
232 encoder.canEncode("\ud902\udc00");
235 assertSame(encoder, encoder.reset());
236 encoder.canEncode('\ud902');
240 // Normal case: just after encode with that endOfInput is true
241 assertSame(encoder, encoder.reset());
242 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState2"),
243 ByteBuffer.allocate(30), true);
247 // Normal case:just after encode with that endOfInput is false
248 assertSame(encoder, encoder.reset());
249 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState3"),
250 ByteBuffer.allocate(30), false);
254 // Normal case: just after flush
255 assertSame(encoder, encoder.reset());
256 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState4"),
257 ByteBuffer.allocate(30), true);
258 encoder.flush(ByteBuffer.allocate(10));
263 // test illegal states for two encode method with endOfInput is true
266 level = TestLevel.PARTIAL_COMPLETE,
268 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
271 level = TestLevel.PARTIAL_COMPLETE,
272 method = "canEncode",
276 level = TestLevel.PARTIAL_COMPLETE,
277 method = "canEncode",
278 args = {java.lang.CharSequence.class}
281 level = TestLevel.PARTIAL_COMPLETE,
286 level = TestLevel.PARTIAL_COMPLETE,
288 args = {java.nio.ByteBuffer.class}
291 public void testEncodeTrueIllegalState() throws CharacterCodingException {
292 CharBuffer in = CharBuffer.wrap("aaa");
293 ByteBuffer out = ByteBuffer.allocate(5);
294 // Normal case: just created
295 encoder.encode(in, out, true);
302 // Normal case: just after encode with that endOfInput is true
303 assertSame(encoder, encoder.reset());
304 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState2"),
305 ByteBuffer.allocate(30), true);
306 encoder.encode(in, out, true);
310 // Normal case:just after encode with that endOfInput is false
311 assertSame(encoder, encoder.reset());
312 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState3"),
313 ByteBuffer.allocate(30), false);
314 encoder.encode(in, out, true);
318 // Illegal state: just after flush
319 assertSame(encoder, encoder.reset());
320 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState4"),
321 ByteBuffer.allocate(30), true);
322 encoder.flush(ByteBuffer.allocate(10));
324 encoder.encode(in, out, true);
325 fail("should illegal state");
326 } catch (IllegalStateException e) {
329 // Normal case: after canEncode
330 assertSame(encoder, encoder.reset());
331 encoder.canEncode("\ud906\udc00");
332 encoder.encode(in, out, true);
335 assertSame(encoder, encoder.reset());
336 encoder.canEncode('\ud905');
337 encoder.encode(in, out, true);
340 // test illegal states for two encode method with endOfInput is false
343 level = TestLevel.PARTIAL_COMPLETE,
345 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
348 level = TestLevel.PARTIAL_COMPLETE,
350 args = {java.nio.CharBuffer.class}
353 level = TestLevel.PARTIAL_COMPLETE,
354 method = "canEncode",
358 level = TestLevel.PARTIAL_COMPLETE,
359 method = "canEncode",
360 args = {java.lang.CharSequence.class}
363 level = TestLevel.PARTIAL_COMPLETE,
368 level = TestLevel.PARTIAL_COMPLETE,
370 args = {java.nio.ByteBuffer.class}
373 public void testEncodeFalseIllegalState() throws CharacterCodingException {
374 CharBuffer in = CharBuffer.wrap("aaa");
375 ByteBuffer out = ByteBuffer.allocate(5);
376 // Normal case: just created
377 encoder.encode(in, out, false);
381 // Illegal state: just after encode facade
382 assertSame(encoder, encoder.reset());
383 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState1"));
385 encoder.encode(in, out, false);
386 fail("should illegal state");
387 } catch (IllegalStateException e) {
390 // Illegal state: just after encode with that endOfInput is true
391 assertSame(encoder, encoder.reset());
392 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState2"),
393 ByteBuffer.allocate(30), true);
395 encoder.encode(in, out, false);
396 fail("should illegal state");
397 } catch (IllegalStateException e) {
400 // Normal case:just after encode with that endOfInput is false
401 assertSame(encoder, encoder.reset());
402 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState3"),
403 ByteBuffer.allocate(30), false);
404 encoder.encode(in, out, false);
408 // Illegal state: just after flush
409 assertSame(encoder, encoder.reset());
410 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState4"),
411 ByteBuffer.allocate(30), true);
412 encoder.flush(ByteBuffer.allocate(10));
414 encoder.encode(in, out, false);
415 fail("should illegal state");
416 } catch (IllegalStateException e) {
419 // Normal case: after canEncode
420 assertSame(encoder, encoder.reset());
421 encoder.canEncode("\ud906\udc00");
422 encoder.encode(in, out, false);
425 assertSame(encoder, encoder.reset());
426 encoder.canEncode('\ud905');
427 encoder.encode(in, out, false);
430 // test illegal states for two canEncode methods
433 level = TestLevel.PARTIAL_COMPLETE,
434 method = "canEncode",
438 level = TestLevel.PARTIAL_COMPLETE,
439 method = "canEncode",
440 args = {java.lang.CharSequence.class}
443 level = TestLevel.PARTIAL_COMPLETE,
448 level = TestLevel.PARTIAL_COMPLETE,
450 args = {java.nio.ByteBuffer.class}
453 public void testCanEncodeIllegalState() throws CharacterCodingException {
454 // Normal case: just created
455 encoder.canEncode("\ud900\udc00");
456 encoder.canEncode('\ud900');
458 // Illegal state: just after encode with that endOfInput is true
459 assertSame(encoder, encoder.reset());
460 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState2"),
461 ByteBuffer.allocate(30), true);
463 encoder.canEncode("\ud903\udc00");
464 fail("should throw illegal state exception");
465 } catch (IllegalStateException e) {
468 // Illegal state:just after encode with that endOfInput is false
469 assertSame(encoder, encoder.reset());
470 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState3"),
471 ByteBuffer.allocate(30), false);
473 encoder.canEncode("\ud904\udc00");
474 fail("should throw illegal state exception");
475 } catch (IllegalStateException e) {
478 // Normal case: just after flush
479 encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState4"),
480 ByteBuffer.allocate(30), true);
481 encoder.flush(ByteBuffer.allocate(10));
482 encoder.canEncode("\ud905\udc00");
483 encoder.canEncode('\ud906');
485 // Normal case: after reset again
486 assertSame(encoder, encoder.reset());
487 encoder.canEncode("\ud906\udc00");
488 encoder.canEncode('\ud905');
492 * --------------------------------- illegal state test end
493 * ---------------------------------
497 * Class under test for boolean canEncode(CharSequence)
500 level = TestLevel.PARTIAL_COMPLETE,
501 method = "canEncode",
502 args = {java.lang.CharSequence.class}
504 public void testCanEncodeCharSequence() {
505 // for non-mapped char
506 assertTrue(encoder.canEncode("\uc2c0"));
507 // surrogate char for unicode
508 // 1st byte: d800-dbff
509 // 2nd byte: dc00-dfff
510 assertTrue(encoder.canEncode("\ud800"));
511 // valid surrogate pair
512 assertTrue(encoder.canEncode("\ud800\udc00"));
513 // invalid surrogate pair
514 assertTrue(encoder.canEncode("\ud800\udb00"));
520 * Class under test for ByteBuffer encode(CharBuffer)
523 level = TestLevel.PARTIAL_COMPLETE,
525 args = {java.nio.CharBuffer.class}
527 public void testEncodeCharBuffer() throws CharacterCodingException {
530 encoder.encode(null);
531 fail("should throw null pointer exception");
532 } catch (NullPointerException e) {
535 // empty input buffer
536 ByteBuffer out = encoder.encode(CharBuffer.wrap(""));
537 assertEquals(out.position(), 0);
538 assertByteArray(out, new byte[0]);
539 // assertByteArray(out, surrogate);
542 out = encoder.encode(CharBuffer.wrap(unistr));
543 assertEquals(out.position(), 0);
544 assertByteArray(out, addSurrogate(unibytes));
546 // Regression test for harmony-3378
547 Charset cs = Charset.forName("UTF-8");
548 CharsetEncoder encoder = cs.newEncoder();
549 encoder.onMalformedInput(CodingErrorAction.REPLACE);
550 encoder = encoder.replaceWith(new byte[] { (byte) 0xef, (byte) 0xbf,
552 CharBuffer in = CharBuffer.wrap("\ud800");
553 out = encoder.encode(in);
557 private byte[] addSurrogate(byte[] expected) {
558 if (surrogate.length > 0) {
559 byte[] temp = new byte[surrogate.length + expected.length];
560 System.arraycopy(surrogate, 0, temp, 0, surrogate.length);
561 System.arraycopy(expected, 0, temp, surrogate.length,
571 protected byte[] getEmptyByteArray() {
575 CharBuffer getMalformedCharBuffer() {
576 return CharBuffer.wrap("malform buffer");
579 CharBuffer getUnmapCharBuffer() {
580 return CharBuffer.wrap("unmap buffer");
583 CharBuffer getExceptionCharBuffer() {
584 return CharBuffer.wrap("runtime buffer");
588 level = TestLevel.PARTIAL_COMPLETE,
590 args = {java.nio.CharBuffer.class}
592 public void testEncodeCharBufferException() throws CharacterCodingException {
595 // MalformedException:
596 in = getMalformedCharBuffer();
597 encoder.onMalformedInput(CodingErrorAction.REPORT);
598 encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
601 // regression test for Harmony-1379
603 fail("should throw MalformedInputException");
604 } catch (MalformedInputException e) {
609 encoder.onMalformedInput(CodingErrorAction.IGNORE);
610 out = encoder.encode(in);
611 assertByteArray(out, addSurrogate(unibytes));
615 encoder.onMalformedInput(CodingErrorAction.REPLACE);
616 out = encoder.encode(in);
617 assertByteArray(out, addSurrogate(unibytesWithRep));
620 // Unmapped Exception:
621 in = getUnmapCharBuffer();
622 encoder.onMalformedInput(CodingErrorAction.REPORT);
623 encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
628 fail("should throw UnmappableCharacterException");
629 } catch (UnmappableCharacterException e) {
634 encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
635 out = encoder.encode(in);
636 assertByteArray(out, unibytes);
640 encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
641 out = encoder.encode(in);
642 assertByteArray(out, unibytesWithRep);
647 encoder.encode(getExceptionCharBuffer());
648 fail("should throw runtime exception");
649 } catch (RuntimeException e) {
654 * utility method, extract given bytebuffer to a string and compare with
657 void assertByteArray(ByteBuffer out, byte[] expected) {
658 out = out.duplicate();
659 if (out.position() != 0) {
662 byte[] ba = new byte[out.limit() - out.position()];
664 // byte[] ba = out.array();
665 assertTrue(Arrays.equals(ba, expected));
669 * Class under test for CoderResult encode(CharBuffer, ByteBuffer, boolean)
672 level = TestLevel.PARTIAL_COMPLETE,
674 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
676 public void testEncodeCharBufferByteBufferboolean()
677 throws CharacterCodingException {
678 ByteBuffer out = ByteBuffer.allocate(200);
679 CharBuffer in = CharBuffer.wrap(unistr);
682 encoder.encode(null, out, true);
683 fail("should throw null pointer exception");
684 } catch (NullPointerException e) {
687 encoder.encode(in, null, true);
688 fail("should throw null pointer exception");
689 } catch (NullPointerException e) {
692 // normal case, one complete operation
693 assertSame(encoder, encoder.reset());
696 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, true));
697 assertEquals(out.limit(), 200);
698 assertTrue(out.position() > 0);
699 assertTrue(out.remaining() > 0);
700 assertEquals(out.capacity(), 200);
701 assertByteArray(out, addSurrogate(unibytes));
706 // normal case, one complete operation, but call twice, first time set
707 // endOfInput to false
708 assertSame(encoder, encoder.reset());
710 out = ByteBuffer.allocate(200);
711 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, false));
712 assertEquals(out.limit(), 200);
713 assertTrue(out.position() > 0);
714 assertTrue(out.remaining() > 0);
715 assertEquals(out.capacity(), 200);
716 assertByteArray(out, addSurrogate(unibytes));
719 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, false));
721 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, true));
722 assertEquals(out.limit(), 200);
723 assertTrue(out.position() > 0);
724 assertTrue(out.remaining() > 0);
725 assertEquals(out.capacity(), 200);
727 assertByteArray(out, addSurrogate(duplicateByteArray(unibytes, 3)));
730 out = ByteBuffer.allocate(4);
731 assertSame(encoder, encoder.reset());
734 assertSame(CoderResult.OVERFLOW, encoder.encode(in, out, true));
735 assertEquals(out.limit(), 4);
736 assertEquals(out.position(), 4);
737 assertEquals(out.remaining(), 0);
738 assertEquals(out.capacity(), 4);
739 ByteBuffer temp = ByteBuffer.allocate(200);
743 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, true));
744 assertEquals(out.limit(), 200);
745 assertTrue(out.position() > 0);
746 assertTrue(out.remaining() > 0);
747 assertEquals(out.capacity(), 200);
748 assertByteArray(out, addSurrogate(unibytes));
750 assertSame(encoder, encoder.reset());
752 out = ByteBuffer.allocate(4);
753 assertSame(CoderResult.OVERFLOW, encoder.encode(in, out, false));
754 assertEquals(out.limit(), 4);
755 assertEquals(out.position(), 4);
756 assertEquals(out.remaining(), 0);
757 assertEquals(out.capacity(), 4);
758 temp = ByteBuffer.allocate(200);
762 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, false));
763 assertEquals(out.limit(), 200);
764 assertTrue(out.position() > 0);
765 assertTrue(out.remaining() > 0);
766 assertEquals(out.capacity(), 200);
767 assertByteArray(out, addSurrogate(unibytes));
770 void printByteBuffer(ByteBuffer buffer) {
771 System.out.println("print buffer");
772 if (buffer.position() != 0) {
775 byte[] ba = buffer.array();
776 for (int i = 0; i < ba.length; i++) {
777 System.out.println(Integer.toHexString(ba[i]));
782 level = TestLevel.PARTIAL_COMPLETE,
784 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
786 public void testEncodeCharBufferByteBufferbooleanExceptionFalse()
787 throws CharacterCodingException {
788 implTestEncodeCharBufferByteBufferbooleanException(false);
792 level = TestLevel.PARTIAL_COMPLETE,
794 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
796 public void testEncodeCharBufferByteBufferbooleanExceptionTrue()
797 throws CharacterCodingException {
798 implTestEncodeCharBufferByteBufferbooleanException(true);
801 private byte[] duplicateByteArray(byte[] ba, int times) {
802 byte[] result = new byte[ba.length * times];
803 for (int i = 0; i < times; i++) {
804 System.arraycopy(ba, 0, result, i * ba.length, ba.length);
809 protected void implTestEncodeCharBufferByteBufferbooleanException(
810 boolean endOfInput) throws CharacterCodingException {
811 ByteBuffer out = ByteBuffer.allocate(100);
813 // MalformedException:
814 CharBuffer in = getMalformedCharBuffer();
815 encoder.onMalformedInput(CodingErrorAction.REPORT);
816 encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
819 CoderResult r = encoder.encode(in, out, endOfInput);
820 assertTrue(r.isMalformed());
825 encoder.onMalformedInput(CodingErrorAction.IGNORE);
826 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out,
828 assertCodingErrorAction(endOfInput, out, in, unibytes);
833 encoder.onMalformedInput(CodingErrorAction.REPLACE);
834 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out,
836 assertCodingErrorAction(endOfInput, out, in, unibytesWithRep);
838 // System.out.println("Cannot find malformed char buffer for "
842 // Unmapped Exception:
843 in = getUnmapCharBuffer();
844 encoder.onMalformedInput(CodingErrorAction.REPORT);
845 encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
849 assertTrue(encoder.encode(in, out, endOfInput).isUnmappable());
854 encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
855 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out,
857 assertCodingErrorAction(endOfInput, out, in, unibytes);
862 encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
863 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out,
865 assertCodingErrorAction(endOfInput, out, in, unibytesWithRep);
867 // System.out.println("Cannot find unmapped char buffer for "
873 encoder.encode(getExceptionCharBuffer());
874 fail("should throw runtime exception");
875 } catch (RuntimeException e) {
879 private void assertCodingErrorAction(boolean endOfInput, ByteBuffer out,
880 CharBuffer in, byte[] expect) {
882 assertByteArray(out, addSurrogate(expect));
885 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out,
888 assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, true));
889 assertByteArray(out, addSurrogate(duplicateByteArray(expect, 3)));
894 * Class under test for CoderResult flush(ByteBuffer)
897 level = TestLevel.PARTIAL_COMPLETE,
899 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
901 public void testFlush() throws CharacterCodingException {
902 ByteBuffer out = ByteBuffer.allocate(6);
903 CharBuffer in = CharBuffer.wrap("aaa");
904 assertEquals(in.remaining(), 3);
906 // by encode facade, so that internal state will be wrong
907 encoder.encode(CharBuffer.wrap("testFlush"), ByteBuffer.allocate(20),
909 assertSame(CoderResult.UNDERFLOW, encoder
910 .flush(ByteBuffer.allocate(50)));
914 * test isLegalReplacement(byte[])
917 level = TestLevel.PARTIAL_COMPLETE,
918 method = "isLegalReplacement",
919 args = {byte[].class}
921 public void testIsLegalReplacement() {
923 encoder.isLegalReplacement(null);
924 fail("should throw null pointer exception");
925 } catch (NullPointerException e) {
927 assertTrue(encoder.isLegalReplacement(specifiedReplacement));
929 assertTrue(encoder.isLegalReplacement(new byte[200]));
930 byte[] ba = getIllegalByteArray();
932 assertFalse(encoder.isLegalReplacement(ba));
937 level = TestLevel.PARTIAL_COMPLETE,
938 method = "isLegalReplacement",
939 args = {byte[].class}
941 public void testIsLegalReplacementEmptyArray() {
942 // ISO, ASC, GB, UTF8 encoder will throw exception in RI
945 assertTrue(encoder.isLegalReplacement(new byte[0]));
946 // fail("should throw ArrayIndexOutOfBoundsException");
947 // } catch (ArrayIndexOutOfBoundsException e) {
953 level = TestLevel.COMPLETE,
954 method = "onMalformedInput",
955 args = {java.nio.charset.CodingErrorAction.class}
958 level = TestLevel.COMPLETE,
959 method = "malformedInputAction",
963 public void testOnMalformedInput() {
964 assertSame(CodingErrorAction.REPORT, encoder.malformedInputAction());
966 encoder.onMalformedInput(null);
967 fail("should throw null pointer exception");
968 } catch (IllegalArgumentException e) {
970 encoder.onMalformedInput(CodingErrorAction.IGNORE);
971 assertSame(CodingErrorAction.IGNORE, encoder.malformedInputAction());
976 level = TestLevel.COMPLETE,
977 method = "onUnmappableCharacter",
978 args = {java.nio.charset.CodingErrorAction.class}
981 level = TestLevel.COMPLETE,
982 method = "unmappableCharacterAction",
986 public void testOnUnmappableCharacter() {
987 assertSame(CodingErrorAction.REPORT, encoder
988 .unmappableCharacterAction());
990 encoder.onUnmappableCharacter(null);
991 fail("should throw null pointer exception");
992 } catch (IllegalArgumentException e) {
994 encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
995 assertSame(CodingErrorAction.IGNORE, encoder
996 .unmappableCharacterAction());
1001 level = TestLevel.COMPLETE,
1002 method = "replaceWith",
1003 args = {byte[].class}
1006 level = TestLevel.COMPLETE,
1007 method = "replacement",
1011 public void testReplacement() {
1013 encoder.replaceWith(null);
1014 fail("should throw null pointer exception");
1015 } catch (IllegalArgumentException e) {
1018 encoder.replaceWith(new byte[0]);
1019 fail("should throw null pointer exception");
1020 } catch (IllegalArgumentException e) {
1023 encoder.replaceWith(new byte[100]);
1024 fail("should throw null pointer exception");
1025 } catch (IllegalArgumentException e) {
1028 byte[] nr = getLegalByteArray();
1029 assertSame(encoder, encoder.replaceWith(nr));
1030 assertSame(nr, encoder.replacement());
1032 nr = getIllegalByteArray();
1034 encoder.replaceWith(new byte[100]);
1035 fail("should throw null pointer exception");
1036 } catch (IllegalArgumentException e) {
1040 protected byte[] getLegalByteArray() {
1041 return new byte[] { 'a' };
1044 protected byte[] getIllegalByteArray() {
1045 return new byte[155];