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 org.apache.harmony.tests.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 testLittleEndianByteBufferCharBuffer()
70            throws CharacterCodingException, UnsupportedEncodingException {
71        bigEndian = false;
72        implTestDecodeByteBufferCharBuffer(getByteBuffer());
73        bigEndian = true;
74    }
75
76    public void testLittleEndianReadOnlyByteBufferCharBuffer()
77            throws CharacterCodingException, UnsupportedEncodingException {
78        bigEndian = false;
79        implTestDecodeByteBufferCharBuffer(getByteBuffer().asReadOnlyBuffer());
80        bigEndian = true;
81    }
82
83    public void testLittleEndian() throws CharacterCodingException,
84            UnsupportedEncodingException {
85        bigEndian = false;
86        implTestDecodeByteBuffer();
87        bigEndian = true;
88    }
89
90    // FIXME: give up this tests
91    // public void testDefaultCharsPerByte() {
92    // // assertEquals(1, decoder.averageCharsPerByte());
93    // // assertEquals(1, decoder.maxCharsPerByte());
94    // assertEquals(decoder.averageCharsPerByte(), 0.5, 0.001);
95    // assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
96    // }
97
98    ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
99        return null;
100    }
101
102    ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
103        return null;
104        // FIXME: different here, RI can parse 0xd8d8
105        // ByteBuffer buffer = ByteBuffer.allocate(100);
106        // buffer.put((byte) -1);
107        // buffer.put((byte) -2);
108        // buffer.put((byte) 0xdc);
109        // buffer.put((byte) 0xdc);
110        // buffer.put(unibytes);
111        // buffer.flip();
112        // return buffer;
113    }
114
115    ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
116        return null;
117    }
118}
119