CharsetDecoderTest.java revision 71684d91a50ae210cec2cb8eccdc632bc494c786
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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 java.nio.charset;
18
19import java.nio.ByteBuffer;
20import java.nio.CharBuffer;
21
22import junit.framework.Test;
23import junit.framework.TestSuite;
24
25public class CharsetDecoderTest extends junit.framework.TestCase {
26    private static final String CHARSET = "UTF-16";
27
28    private static final String SAMPLE_STRING = "Android";
29
30    // http://code.google.com/p/android/issues/detail?id=4237
31    public void test_ByteArray_decode_no_offset() throws Exception {
32        CharsetDecoder decoder = getCharsetDecoderUnderTest();
33        byte[] arr = getEncodedByteArrayFixture();
34        ByteBuffer inBuffer = ByteBuffer.wrap(arr, 0, arr.length).slice();
35        CharBuffer outBuffer = CharBuffer.allocate(arr.length);
36        decoder.reset();
37        CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
38        assertFalse(coderResult.toString(), coderResult.isError());
39        decoder.flush(outBuffer);
40        outBuffer.flip();
41        assertEquals(SAMPLE_STRING, outBuffer.toString().trim());
42    }
43
44    // http://code.google.com/p/android/issues/detail?id=4237
45    public void test_ByteArray_decode_with_offset() throws Exception {
46        CharsetDecoder decoder = getCharsetDecoderUnderTest();
47        byte[] arr = getEncodedByteArrayFixture();
48        arr = prependByteToByteArray(arr, new Integer(1).byteValue());
49        int offset = 1;
50        ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice();
51        CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset);
52        decoder.reset();
53        CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
54        assertFalse(coderResult.toString(), coderResult.isError());
55        decoder.flush(outBuffer);
56        outBuffer.flip();
57        assertEquals(SAMPLE_STRING, outBuffer.toString().trim());
58    }
59
60    // http://code.google.com/p/android/issues/detail?id=4237
61    public void test_ByteArray_decode_with_offset_using_facade_method() throws Exception {
62        CharsetDecoder decoder = getCharsetDecoderUnderTest();
63        byte[] arr = getEncodedByteArrayFixture();
64        arr = prependByteToByteArray(arr, new Integer(1).byteValue());
65        int offset = 1;
66        CharBuffer outBuffer = decoder.decode(ByteBuffer.wrap(arr, offset, arr.length - offset));
67        assertEquals(SAMPLE_STRING, outBuffer.toString().trim());
68    }
69
70    private static byte[] prependByteToByteArray(byte[] arr, byte b) {
71        byte[] result = new byte[arr.length + 1];
72        result[0] = b;
73        System.arraycopy(arr, 0, result, 1, arr.length);
74        return result;
75    }
76
77    private static CharsetDecoder getCharsetDecoderUnderTest() {
78        return Charset.forName(CHARSET).newDecoder();
79    }
80
81    private byte[] getEncodedByteArrayFixture() throws CharacterCodingException {
82        CharsetEncoder encoder = Charset.forName(CHARSET).newEncoder();
83        return encoder.encode(CharBuffer.wrap(SAMPLE_STRING)).array();
84    }
85}
86