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 17package tests.api.java.nio.charset; 18 19import java.io.UnsupportedEncodingException; 20import java.nio.ByteBuffer; 21import java.nio.CharBuffer; 22import java.nio.charset.CharacterCodingException; 23import java.nio.charset.Charset; 24import java.nio.charset.CoderResult; 25import java.nio.charset.CodingErrorAction; 26 27/** 28 * 29 */ 30public class UTF16CharsetDecoderTest extends CharsetDecoderTest { 31 32 boolean bigEndian = true; 33 34 protected void setUp() throws Exception { 35 cs = Charset.forName("utf-16"); 36 bom = "\ufeff"; 37 super.setUp(); 38 } 39 40 /* 41 * @see CharsetDecoderTest#tearDown() 42 */ 43 protected void tearDown() throws Exception { 44 super.tearDown(); 45 } 46 47 protected ByteBuffer getByteBuffer() { 48 // FIXME: different here 49 // if don't specified BOM 50 // ICU default is LE 51 // JDK default is BE 52 53 // maybe start with 0xFEFF, which means big endian 54 // 0xFFFE, which means little endian 55 byte[] b = (bigEndian) ? new byte[] { -1, -2, 32, 0, 98, 0, 117, 0, 56 102, 0, 102, 0, 101, 0, 114, 0 } : new byte[] { -2, -1, 0, 32, 57 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0, 114 }; 58 return ByteBuffer.wrap(b); 59 } 60 61 protected ByteBuffer getHeadlessByteBuffer() { 62 ByteBuffer b = getByteBuffer(); 63 b.position(2); 64 byte[] bytes = new byte[b.remaining()]; 65 b.get(bytes); 66 return ByteBuffer.wrap(bytes); 67 } 68 69 public void testMultiStepDecode() throws CharacterCodingException { 70 if (!cs.name().equals("mock")) { 71 decoder.onMalformedInput(CodingErrorAction.REPORT); 72 decoder.onUnmappableCharacter(CodingErrorAction.REPORT); 73 CharBuffer out = CharBuffer.allocate(10); 74 assertTrue(decoder.decode( 75 ByteBuffer.wrap(new byte[] { -1, -2, 32, 0, 98 }), out, 76 true).isMalformed()); 77 78 decoder.flush(out); 79 decoder.reset(); 80 out.clear(); 81 assertSame(CoderResult.UNDERFLOW, decoder.decode(ByteBuffer 82 .wrap(new byte[] { -1, -2, 32, 0 }), out, false)); 83 assertTrue(decoder.decode(ByteBuffer.wrap(new byte[] { 98 }), out, 84 true).isMalformed()); 85 86 decoder.flush(out); 87 decoder.reset(); 88 out.clear(); 89 assertSame(CoderResult.UNDERFLOW, decoder.decode(ByteBuffer 90 .wrap(new byte[] { -1, -2, 32, 0, 98 }), out, false)); 91 assertFalse(decoder.decode(ByteBuffer.wrap(new byte[] {}), out, 92 true).isMalformed()); 93 94 decoder.flush(out); 95 decoder.reset(); 96 out.clear(); 97 assertFalse(decoder.decode( 98 ByteBuffer.wrap(new byte[] { -1, -2, 32, 0, 98, 0 }), out, 99 true).isError()); 100 101 decoder.flush(out); 102 decoder.reset(); 103 out.clear(); 104 assertSame(CoderResult.UNDERFLOW, decoder.decode(ByteBuffer 105 .wrap(new byte[] { -1, -2, 32, 0, 98 }), out, false)); 106 assertTrue(decoder.decode(ByteBuffer.wrap(new byte[] { 0 }), out, 107 true).isMalformed()); 108 109 } 110 } 111 112 public void testLittleEndianByteBufferCharBuffer() 113 throws CharacterCodingException, UnsupportedEncodingException { 114 bigEndian = false; 115 implTestDecodeByteBufferCharBuffer(getByteBuffer()); 116 bigEndian = true; 117 } 118 119 public void testLittleEndianReadOnlyByteBufferCharBuffer() 120 throws CharacterCodingException, UnsupportedEncodingException { 121 bigEndian = false; 122 implTestDecodeByteBufferCharBuffer(getByteBuffer().asReadOnlyBuffer()); 123 bigEndian = true; 124 } 125 126 public void testLittleEndian() throws CharacterCodingException, 127 UnsupportedEncodingException { 128 bigEndian = false; 129 implTestDecodeByteBuffer(); 130 bigEndian = true; 131 } 132 133 // FIXME: give up this tests 134 // public void testDefaultCharsPerByte() { 135 // // assertEquals(1, decoder.averageCharsPerByte()); 136 // // assertEquals(1, decoder.maxCharsPerByte()); 137 // assertEquals(decoder.averageCharsPerByte(), 0.5, 0.001); 138 // assertEquals(decoder.maxCharsPerByte(), 2, 0.001); 139 // } 140 141 ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException { 142 return null; 143 } 144 145 ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException { 146 return null; 147 // FIXME: different here, RI can parse 0xd8d8 148 // ByteBuffer buffer = ByteBuffer.allocate(100); 149 // buffer.put((byte) -1); 150 // buffer.put((byte) -2); 151 // buffer.put((byte) 0xdc); 152 // buffer.put((byte) 0xdc); 153 // buffer.put(unibytes); 154 // buffer.flip(); 155 // return buffer; 156 } 157 158 ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException { 159 return null; 160 } 161} 162