diff options
author | Elliott Hughes <enh@google.com> | 2013-08-07 19:19:53 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2013-08-07 19:23:11 -0700 |
commit | 5ec6bf8d033754e06a463adb091d2c0afd0755ac (patch) | |
tree | f9efbc4125f365b2ec6366410df06888cf17f454 /harmony-tests/src/test/java | |
parent | c5ee4c34f703a21e0332261827820fe669c97dc8 (diff) | |
download | libcore-5ec6bf8d033754e06a463adb091d2c0afd0755ac.zip libcore-5ec6bf8d033754e06a463adb091d2c0afd0755ac.tar.gz libcore-5ec6bf8d033754e06a463adb091d2c0afd0755ac.tar.bz2 |
Fix various Charset tests, clean up the implementation.
Bug: 10211558
Bug: 10211378
Change-Id: Ib3f97430f62163c0459c53e0c282ae0ca840e0af
Diffstat (limited to 'harmony-tests/src/test/java')
9 files changed, 62 insertions, 145 deletions
diff --git a/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java b/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java index e48c4ef..ef219be 100644 --- a/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java +++ b/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java @@ -4,9 +4,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -88,16 +88,13 @@ public class CharsetEncoderTest extends TestCase { public void testDefaultValue() { assertEquals(CodingErrorAction.REPORT, encoder.malformedInputAction()); - assertEquals(CodingErrorAction.REPORT, encoder - .unmappableCharacterAction()); + assertEquals(CodingErrorAction.REPORT, encoder.unmappableCharacterAction()); assertSame(encoder, encoder.onMalformedInput(CodingErrorAction.IGNORE)); - assertSame(encoder, encoder - .onUnmappableCharacter(CodingErrorAction.IGNORE)); + assertSame(encoder, encoder.onUnmappableCharacter(CodingErrorAction.IGNORE)); if (encoder instanceof MockCharsetEncoder) { assertTrue(Arrays.equals(encoder.replacement(), defaultReplacement)); } else { - assertTrue(Arrays.equals(encoder.replacement(), - specifiedReplacement)); + assertTrue(Arrays.equals(encoder.replacement(), specifiedReplacement)); } } @@ -309,13 +306,13 @@ public class CharsetEncoderTest extends TestCase { // Expected } } - + public void testFlushAfterConstructing() { ByteBuffer out = ByteBuffer.allocate(5); - + //Illegal state: flush after instance created try { - encoder.flush(out); + encoder.flush(out); fail("should throw IllegalStateException"); } catch (IllegalStateException e) { // Expected @@ -529,13 +526,34 @@ public class CharsetEncoderTest extends TestCase { // surrogate char for unicode // 1st byte: d800-dbff // 2nd byte: dc00-dfff - assertTrue(encoder.canEncode("\ud800")); // valid surrogate pair assertTrue(encoder.canEncode("\ud800\udc00")); // invalid surrogate pair assertTrue(encoder.canEncode("\ud800\udb00")); } + public void test_canEncode_char_ICUBug() { + // The RI doesn't allow this, but icu4c does. + assertTrue(encoder.canEncode('\ud800')); + } + + public void test_canEncode_CharSequence_ICUBug() { + // The RI doesn't allow this, but icu4c does. + assertTrue(encoder.canEncode("\ud800")); + } + + public void test_canEncode_empty() throws Exception { + assertTrue(encoder.canEncode("")); + } + + public void test_canEncode_null() throws Exception { + try { + encoder.canEncode(null); + fail(); + } catch (NullPointerException e) { + } + } + /* * Class under test for Charset charset() */ @@ -571,7 +589,7 @@ public class CharsetEncoderTest extends TestCase { out = encoder.encode(CharBuffer.wrap(unistr)); assertEquals(out.position(), 0); assertByteArray(out, addSurrogate(unibytes)); - + // Regression test for harmony-3378 Charset cs = Charset.forName("UTF-8"); CharsetEncoder encoder = cs.newEncoder(); @@ -580,7 +598,7 @@ public class CharsetEncoderTest extends TestCase { (byte) 0xbd, }); CharBuffer in = CharBuffer.wrap("\ud800"); out = encoder.encode(in); - assertNotNull(out); + assertNotNull(out); } private byte[] addSurrogate(byte[] expected) { @@ -917,14 +935,19 @@ public class CharsetEncoderTest extends TestCase { /* * test isLegalReplacement(byte[]) */ - public void testIsLegalReplacement() { + public void test_isLegalReplacement_null() { try { encoder.isLegalReplacement(null); fail("should throw null pointer exception"); } catch (NullPointerException e) { } + } + + public void test_isLegalReplacement_good() { assertTrue(encoder.isLegalReplacement(specifiedReplacement)); + } + public void test_isLegalReplacement_bad() { assertTrue(encoder.isLegalReplacement(new byte[200])); byte[] ba = getIllegalByteArray(); if (ba != null) { @@ -932,14 +955,10 @@ public class CharsetEncoderTest extends TestCase { } } - public void testIsLegalReplacementEmptyArray() { + public void test_isLegalReplacement_empty_array() { // ISO, ASC, GB, UTF8 encoder will throw exception in RI // others will pass - // try { assertTrue(encoder.isLegalReplacement(new byte[0])); - // fail("should throw ArrayIndexOutOfBoundsException"); - // } catch (ArrayIndexOutOfBoundsException e) { - // } } public void testOnMalformedInput() { @@ -990,7 +1009,7 @@ public class CharsetEncoderTest extends TestCase { nr = getIllegalByteArray(); try { encoder.replaceWith(new byte[100]); - fail("should throw null pointer exception"); + fail(); } catch (IllegalArgumentException e) { } } diff --git a/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetTest.java b/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetTest.java index 34d84ba..c658c48 100644 --- a/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetTest.java +++ b/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetTest.java @@ -708,8 +708,7 @@ public class CharsetTest extends TestCase { */ public void test_availableCharsets() throws Exception { // regression test for Harmony-1051 - ClassLoader originalClassLoader = Thread.currentThread() - .getContextClassLoader(); + ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(null); SortedMap<String, Charset> charsets = Charset.availableCharsets(); diff --git a/harmony-tests/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java b/harmony-tests/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java index 3caabde..ab4eeea 100644 --- a/harmony-tests/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java +++ b/harmony-tests/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java @@ -57,15 +57,12 @@ public class GBCharsetEncoderTest extends CharsetEncoderTest { * Class under test for boolean canEncode(CharSequence) */ public void testCanEncodeCharSequence() { - assertTrue(encoder.canEncode("")); // surrogate char // valid surrogate pair assertTrue(encoder.canEncode("\ud800\udc00")); // invalid surrogate pair assertFalse(encoder.canEncode("\ud800\udb00")); - // The RI doesn't allow this, but we do. - //assertFalse(encoder.canEncode("\ud800")); } public void testSpecificDefaultValue() { diff --git a/harmony-tests/src/test/java/tests/api/java/nio/charset/ISOCharsetEncoderTest.java b/harmony-tests/src/test/java/tests/api/java/nio/charset/ISOCharsetEncoderTest.java index 7d7bca3..8a84938 100644 --- a/harmony-tests/src/test/java/tests/api/java/nio/charset/ISOCharsetEncoderTest.java +++ b/harmony-tests/src/test/java/tests/api/java/nio/charset/ISOCharsetEncoderTest.java @@ -4,9 +4,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,6 +21,7 @@ import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CoderResult; import java.nio.charset.CodingErrorAction; +import java.nio.charset.MalformedInputException; import java.nio.charset.UnmappableCharacterException; /** @@ -46,29 +47,19 @@ public class ISOCharsetEncoderTest extends CharsetEncoderTest { super.tearDown(); } - public void testCanEncodeCharSequence() { + @Override public void testCanEncodeCharSequence() { // normal case for isoCS assertTrue(encoder.canEncode("\u0077")); assertFalse(encoder.canEncode("\uc2a3")); assertFalse(encoder.canEncode("\ud800\udc00")); - try { - encoder.canEncode(null); - } catch (NullPointerException e) { - } - assertTrue(encoder.canEncode("")); } - public void testCanEncodeICUBug() { - assertFalse(encoder.canEncode((char) '\ud800')); - assertFalse(encoder.canEncode((String) "\ud800")); - } - - public void testCanEncodechar() throws CharacterCodingException { + @Override public void testCanEncodechar() throws CharacterCodingException { assertTrue(encoder.canEncode('\u0077')); assertFalse(encoder.canEncode('\uc2a3')); } - public void testSpecificDefaultValue() { + @Override public void testSpecificDefaultValue() { assertEquals(1, encoder.averageBytesPerChar(), 0.001); assertEquals(1, encoder.maxBytesPerChar(), 0.001); } @@ -98,15 +89,5 @@ public class ISOCharsetEncoderTest extends CharsetEncoderTest { } catch (UnmappableCharacterException e) { } encoder.reset(); - ByteBuffer out = ByteBuffer.allocate(10); - assertTrue(encoder.encode(CharBuffer.wrap("\ud800"), out, true) - .isMalformed()); - encoder.flush(out); - encoder.reset(); - out = ByteBuffer.allocate(10); - assertSame(CoderResult.UNDERFLOW, encoder.encode(CharBuffer - .wrap("\ud800"), out, false)); - assertTrue(encoder.encode(CharBuffer.wrap("\udc00"), out, true) - .isMalformed()); } } diff --git a/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16BECharsetEncoderTest.java b/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16BECharsetEncoderTest.java index 70a1786..68bd2f5 100644 --- a/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16BECharsetEncoderTest.java +++ b/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16BECharsetEncoderTest.java @@ -4,9 +4,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -68,7 +68,6 @@ public class UTF16BECharsetEncoderTest extends CharsetEncoderTest { // normal case for utfCS assertTrue(encoder.canEncode("\u0077")); assertTrue(encoder.canEncode("\uc2a3")); - assertTrue(encoder.canEncode("")); // for non-mapped char assertTrue(encoder.canEncode("\uc2c0")); @@ -82,10 +81,6 @@ public class UTF16BECharsetEncoderTest extends CharsetEncoderTest { assertFalse(encoder.canEncode("\ud800\udb00")); } - public void testCanEncodeICUBug() { - assertFalse(encoder.canEncode("\ud800")); - } - public void testSpecificDefaultValue() { assertEquals(2, encoder.averageBytesPerChar(), 0.001); assertEquals(2, encoder.maxBytesPerChar(), 0.001); @@ -103,15 +98,8 @@ public class UTF16BECharsetEncoderTest extends CharsetEncoderTest { return null; } - public void testIsLegalReplacementEmptyArray() { - assertTrue(encoder.isLegalReplacement(new byte[0])); - } - protected byte[] getIllegalByteArray() { - // FIXME: different here - // cannot replace by 0xd8d8, but RI can - // return new byte[]{(byte)0xd8, (byte)0xd8}; - return new byte[] { 0 }; + return new byte[] { (byte)0xd8, (byte)0x00, (byte)0xdb, (byte)0x00 }; } protected byte[] getLegalByteArray() { diff --git a/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16CharsetDecoderTest.java b/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16CharsetDecoderTest.java index 51daaf6..c5bb408 100644 --- a/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16CharsetDecoderTest.java +++ b/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16CharsetDecoderTest.java @@ -4,9 +4,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -25,7 +25,7 @@ import java.nio.charset.CoderResult; import java.nio.charset.CodingErrorAction; /** - * + * */ public class UTF16CharsetDecoderTest extends CharsetDecoderTest { @@ -66,49 +66,6 @@ public class UTF16CharsetDecoderTest extends CharsetDecoderTest { return ByteBuffer.wrap(bytes); } - public void testMultiStepDecode() throws CharacterCodingException { - if (!cs.name().equals("mock")) { - decoder.onMalformedInput(CodingErrorAction.REPORT); - decoder.onUnmappableCharacter(CodingErrorAction.REPORT); - CharBuffer out = CharBuffer.allocate(10); - assertTrue(decoder.decode( - ByteBuffer.wrap(new byte[] { -1, -2, 32, 0, 98 }), out, - true).isMalformed()); - - decoder.flush(out); - decoder.reset(); - out.clear(); - assertSame(CoderResult.UNDERFLOW, decoder.decode(ByteBuffer - .wrap(new byte[] { -1, -2, 32, 0 }), out, false)); - assertTrue(decoder.decode(ByteBuffer.wrap(new byte[] { 98 }), out, - true).isMalformed()); - - decoder.flush(out); - decoder.reset(); - out.clear(); - assertSame(CoderResult.UNDERFLOW, decoder.decode(ByteBuffer - .wrap(new byte[] { -1, -2, 32, 0, 98 }), out, false)); - assertFalse(decoder.decode(ByteBuffer.wrap(new byte[] {}), out, - true).isMalformed()); - - decoder.flush(out); - decoder.reset(); - out.clear(); - assertFalse(decoder.decode( - ByteBuffer.wrap(new byte[] { -1, -2, 32, 0, 98, 0 }), out, - true).isError()); - - decoder.flush(out); - decoder.reset(); - out.clear(); - assertSame(CoderResult.UNDERFLOW, decoder.decode(ByteBuffer - .wrap(new byte[] { -1, -2, 32, 0, 98 }), out, false)); - assertTrue(decoder.decode(ByteBuffer.wrap(new byte[] { 0 }), out, - true).isMalformed()); - - } - } - public void testLittleEndianByteBufferCharBuffer() throws CharacterCodingException, UnsupportedEncodingException { bigEndian = false; diff --git a/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16CharsetEncoderTest.java b/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16CharsetEncoderTest.java index c113c08..6a42d41 100644 --- a/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16CharsetEncoderTest.java +++ b/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16CharsetEncoderTest.java @@ -4,9 +4,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -38,7 +38,7 @@ public class UTF16CharsetEncoderTest extends CharsetEncoderTest { */ protected void setUp() throws Exception { cs = CS; - specifiedReplacement = new byte[] { -1, -3 }; + specifiedReplacement = new byte[] { -3, -1 }; surrogate = new byte[] { -1, -2 }; unibytes = new byte[] { 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0, 114, 0 }; @@ -71,7 +71,6 @@ public class UTF16CharsetEncoderTest extends CharsetEncoderTest { // normal case for utfCS assertTrue(encoder.canEncode("\u0077")); assertTrue(encoder.canEncode("\uc2a3")); - assertTrue(encoder.canEncode("")); // for non-mapped char assertTrue(encoder.canEncode("\uc2c0")); @@ -85,11 +84,6 @@ public class UTF16CharsetEncoderTest extends CharsetEncoderTest { assertFalse(encoder.canEncode("\ud800\udb00")); } - public void testCanEncodeICUBug() { - assertFalse(encoder.canEncode('\ud800')); - assertFalse(encoder.canEncode("\ud800")); - } - public void testSpecificDefaultValue() { assertEquals(encoder.averageBytesPerChar(), 2, 0.001); // assertEquals(4, encoder.maxBytesPerChar()); @@ -109,12 +103,8 @@ public class UTF16CharsetEncoderTest extends CharsetEncoderTest { return null; } - public void testIsLegalReplacementEmptyArray() { - assertTrue(encoder.isLegalReplacement(new byte[0])); - } - protected byte[] getIllegalByteArray() { - return new byte[] { 0x00 }; + return new byte[] { (byte)0xd8, (byte)0x00, (byte)0xdb, (byte)0x00 }; } protected byte[] getLegalByteArray() { diff --git a/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16LECharsetEncoderTest.java b/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16LECharsetEncoderTest.java index 26bae5c..1e9187d 100644 --- a/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16LECharsetEncoderTest.java +++ b/harmony-tests/src/test/java/tests/api/java/nio/charset/UTF16LECharsetEncoderTest.java @@ -4,9 +4,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -68,7 +68,6 @@ public class UTF16LECharsetEncoderTest extends CharsetEncoderTest { // normal case for utfCS assertTrue(encoder.canEncode("\u0077")); assertTrue(encoder.canEncode("\uc2a3")); - assertTrue(encoder.canEncode("")); // for non-mapped char assertTrue(encoder.canEncode("\uc2c0")); @@ -82,19 +81,11 @@ public class UTF16LECharsetEncoderTest extends CharsetEncoderTest { assertFalse(encoder.canEncode("\ud800\udb00")); } - public void testCanEncodeICUBug() { - assertFalse(encoder.canEncode("\ud800")); - } - public void testSpecificDefaultValue() { assertEquals(2, encoder.averageBytesPerChar(), 0.001); assertEquals(2, encoder.maxBytesPerChar(), 0.001); } - public void testIsLegalReplacementEmptyArray() { - assertTrue(encoder.isLegalReplacement(new byte[0])); - } - CharBuffer getMalformedCharBuffer() { return CharBuffer.wrap("\ud800 buffer"); } @@ -108,7 +99,7 @@ public class UTF16LECharsetEncoderTest extends CharsetEncoderTest { } protected byte[] getIllegalByteArray() { - return new byte[] { 0x00 }; + return new byte[] { (byte)0x00, (byte)0xd8, (byte)0x00, (byte)0xdb }; } protected byte[] getLegalByteArray() { diff --git a/harmony-tests/src/test/java/tests/api/java/nio/charset/UTFCharsetEncoderTest.java b/harmony-tests/src/test/java/tests/api/java/nio/charset/UTFCharsetEncoderTest.java index 0096856..798a048 100644 --- a/harmony-tests/src/test/java/tests/api/java/nio/charset/UTFCharsetEncoderTest.java +++ b/harmony-tests/src/test/java/tests/api/java/nio/charset/UTFCharsetEncoderTest.java @@ -4,9 +4,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -57,7 +57,6 @@ public class UTFCharsetEncoderTest extends CharsetEncoderTest { // normal case for utfCS assertTrue(encoder.canEncode("\u0077")); assertTrue(encoder.canEncode("\uc2a3")); - assertTrue(encoder.canEncode("")); // for non-mapped char assertTrue(encoder.canEncode("\uc2c0")); @@ -71,10 +70,6 @@ public class UTFCharsetEncoderTest extends CharsetEncoderTest { assertFalse(encoder.canEncode("\ud800\udb00")); } - public void testCanEncodeICUBug() { - assertFalse(encoder.canEncode("\ud800")); - } - public void testSpecificDefaultValue() { assertEquals(1.1, encoder.averageBytesPerChar(), 0.0001); // assertEquals(2, encoder.averageBytesPerChar(), 0.0001); |