CharsetDecoderTest.java revision 4557728efb66c455a52b7669a8eefef7a9e54854
1b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes/* 2b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes * Copyright (C) 2009 The Android Open Source Project 3f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 4b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes * you may not use this file except in compliance with the License. 6b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes * You may obtain a copy of the License at 7f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 8b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 10b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes * Unless required by applicable law or agreed to in writing, software 11b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes * See the License for the specific language governing permissions and 14b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes * limitations under the License. 15b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes */ 16b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes 174557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonpackage libcore.java.nio.charset; 18b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes 19b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughesimport java.nio.ByteBuffer; 20b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughesimport java.nio.CharBuffer; 214557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.nio.charset.CharacterCodingException; 224557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.nio.charset.Charset; 234557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.nio.charset.CharsetDecoder; 244557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.nio.charset.CharsetEncoder; 254557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.nio.charset.CoderResult; 264557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.nio.charset.CodingErrorAction; 27b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes 28b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughespublic class CharsetDecoderTest extends junit.framework.TestCase { 29b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes private static final String CHARSET = "UTF-16"; 30f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 31b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes private static final String SAMPLE_STRING = "Android"; 32f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 33cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes // None of the harmony or jtreg tests actually check that replaceWith does the right thing! 34cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes public void test_replaceWith() throws Exception { 35cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes CharsetDecoder d = Charset.forName("UTF-16").newDecoder(); 36cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes d.replaceWith("x"); 37cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes d.onMalformedInput(CodingErrorAction.REPLACE); 38cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes d.onUnmappableCharacter(CodingErrorAction.REPLACE); 39cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes ByteBuffer in = ByteBuffer.wrap(new byte[] { 109, 97, 109 }); 40cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes assertEquals("\u6d61x", d.decode(in).toString()); 41cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes } 42cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes 43b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes // http://code.google.com/p/android/issues/detail?id=4237 44b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes public void test_ByteArray_decode_no_offset() throws Exception { 45b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes CharsetDecoder decoder = getCharsetDecoderUnderTest(); 46b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes byte[] arr = getEncodedByteArrayFixture(); 47b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes ByteBuffer inBuffer = ByteBuffer.wrap(arr, 0, arr.length).slice(); 48b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes CharBuffer outBuffer = CharBuffer.allocate(arr.length); 49b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes decoder.reset(); 50b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true); 51b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes assertFalse(coderResult.toString(), coderResult.isError()); 52b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes decoder.flush(outBuffer); 53b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes outBuffer.flip(); 54b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes assertEquals(SAMPLE_STRING, outBuffer.toString().trim()); 55b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes } 56f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 57b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes // http://code.google.com/p/android/issues/detail?id=4237 58b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes public void test_ByteArray_decode_with_offset() throws Exception { 59b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes CharsetDecoder decoder = getCharsetDecoderUnderTest(); 60b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes byte[] arr = getEncodedByteArrayFixture(); 61b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes arr = prependByteToByteArray(arr, new Integer(1).byteValue()); 62b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes int offset = 1; 63b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice(); 64b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset); 65b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes decoder.reset(); 66b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true); 67b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes assertFalse(coderResult.toString(), coderResult.isError()); 68b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes decoder.flush(outBuffer); 69b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes outBuffer.flip(); 70b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes assertEquals(SAMPLE_STRING, outBuffer.toString().trim()); 71b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes } 72f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 73b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes // http://code.google.com/p/android/issues/detail?id=4237 74b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes public void test_ByteArray_decode_with_offset_using_facade_method() throws Exception { 75b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes CharsetDecoder decoder = getCharsetDecoderUnderTest(); 76b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes byte[] arr = getEncodedByteArrayFixture(); 77b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes arr = prependByteToByteArray(arr, new Integer(1).byteValue()); 78b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes int offset = 1; 79b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes CharBuffer outBuffer = decoder.decode(ByteBuffer.wrap(arr, offset, arr.length - offset)); 80b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes assertEquals(SAMPLE_STRING, outBuffer.toString().trim()); 81b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes } 82f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 83b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes private static byte[] prependByteToByteArray(byte[] arr, byte b) { 84b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes byte[] result = new byte[arr.length + 1]; 85b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes result[0] = b; 86b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes System.arraycopy(arr, 0, result, 1, arr.length); 87b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes return result; 88b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes } 89f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 90b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes private static CharsetDecoder getCharsetDecoderUnderTest() { 91b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes return Charset.forName(CHARSET).newDecoder(); 92b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes } 93f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 94b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes private byte[] getEncodedByteArrayFixture() throws CharacterCodingException { 95b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes CharsetEncoder encoder = Charset.forName(CHARSET).newEncoder(); 96b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes return encoder.encode(CharBuffer.wrap(SAMPLE_STRING)).array(); 97b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes } 98b7bfb47e9720ecc6e10f43878f27e40542a9c800Elliott Hughes} 99