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