You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2008/04/03 11:03:56 UTC
svn commit: r644228 - in
/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java:
org/apache/harmony/nio_char/tests/java/nio/charset/
tests/api/java/nio/charset/
Author: tellison
Date: Thu Apr 3 02:03:50 2008
New Revision: 644228
URL: http://svn.apache.org/viewvc?rev=644228&view=rev
Log:
Apply regression test for HARMONY-5679 ([classlib][archive] UTF-8 decoder & Attributes.Name.hashCode optimizations)
Modified:
harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetEncoderTest.java
harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetDecoderTest.java
harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java
harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetDecoderTest.java
harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java
harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ISOCharsetDecoderTest.java
harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16BECharsetDecoderTest.java
harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16CharsetDecoderTest.java
harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16LECharsetDecoderTest.java
harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTFCharsetDecoderTest.java
Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetEncoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetEncoderTest.java?rev=644228&r1=644227&r2=644228&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetEncoderTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetEncoderTest.java Thu Apr 3 02:03:50 2008
@@ -16,6 +16,7 @@
package org.apache.harmony.nio_char.tests.java.nio.charset;
+import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
@@ -29,29 +30,29 @@
public class CharsetEncoderTest extends TestCase {
- /**
- * @tests java.nio.charset.CharsetEncoder.CharsetEncoder(
- * java.nio.charset.Charset, float, float)
- */
- public void test_ConstructorLjava_nio_charset_CharsetFF() {
- // Regression for HARMONY-141
- try {
- Charset cs = Charset.forName("UTF-8"); //$NON-NLS-1$
- new MockCharsetEncoderForHarmony141(cs, 1.1f, 1);
- fail("Assert 0: Should throw IllegalArgumentException."); //$NON-NLS-1$
- } catch (IllegalArgumentException e) {
- // expected
- }
-
- try {
- Charset cs = Charset.forName("ISO8859-1"); //$NON-NLS-1$
- new MockCharsetEncoderForHarmony141(cs, 1.1f, 1,
- new byte[] { 0x1a });
- fail("Assert 1: Should throw IllegalArgumentException."); //$NON-NLS-1$
- } catch (IllegalArgumentException e) {
- // expected
- }
- }
+ /**
+ * @tests java.nio.charset.CharsetEncoder.CharsetEncoder(
+ * java.nio.charset.Charset, float, float)
+ */
+ public void test_ConstructorLjava_nio_charset_CharsetFF() {
+ // Regression for HARMONY-141
+ try {
+ Charset cs = Charset.forName("UTF-8"); //$NON-NLS-1$
+ new MockCharsetEncoderForHarmony141(cs, 1.1f, 1);
+ fail("Assert 0: Should throw IllegalArgumentException."); //$NON-NLS-1$
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ try {
+ Charset cs = Charset.forName("ISO8859-1"); //$NON-NLS-1$
+ new MockCharsetEncoderForHarmony141(cs, 1.1f, 1,
+ new byte[] { 0x1a });
+ fail("Assert 1: Should throw IllegalArgumentException."); //$NON-NLS-1$
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
/**
* @tests java.nio.charset.CharsetEncoder.CharsetEncoder(
@@ -69,21 +70,21 @@
public static class MockCharsetEncoderForHarmony141 extends CharsetEncoder {
- protected MockCharsetEncoderForHarmony141(Charset cs,
- float averageBytesPerChar, float maxBytesPerChar) {
- super(cs, averageBytesPerChar, maxBytesPerChar);
- }
-
- public MockCharsetEncoderForHarmony141(Charset cs,
- float averageBytesPerChar, float maxBytesPerChar,
- byte[] replacement) {
- super(cs, averageBytesPerChar, maxBytesPerChar, replacement);
- }
-
- protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
- return null;
- }
- }
+ protected MockCharsetEncoderForHarmony141(Charset cs,
+ float averageBytesPerChar, float maxBytesPerChar) {
+ super(cs, averageBytesPerChar, maxBytesPerChar);
+ }
+
+ public MockCharsetEncoderForHarmony141(Charset cs,
+ float averageBytesPerChar, float maxBytesPerChar,
+ byte[] replacement) {
+ super(cs, averageBytesPerChar, maxBytesPerChar, replacement);
+ }
+
+ protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
+ return null;
+ }
+ }
public static class MockCharsetEncoderForHarmony491 extends CharsetEncoder {
@@ -104,66 +105,85 @@
/*
* Test malfunction encode(CharBuffer)
*/
- public void test_EncodeLjava_nio_CharBuffer() throws Exception {
- MockMalfunctionCharset cs = new MockMalfunctionCharset("mock", null);
- try {
- cs.encode(CharBuffer.wrap("AB"));
- fail("should throw CoderMalfunctionError");// NON-NLS-1$
- } catch (CoderMalfunctionError e) {
- // expected
- }
- }
-
- /*
- * Mock charset class with malfunction decode & encode.
- */
- static final class MockMalfunctionCharset extends Charset {
-
- public MockMalfunctionCharset(String canonicalName, String[] aliases) {
- super(canonicalName, aliases);
- }
-
- public boolean contains(Charset cs) {
- return false;
- }
-
- public CharsetDecoder newDecoder() {
- return Charset.forName("UTF-8").newDecoder();
- }
-
- public CharsetEncoder newEncoder() {
- return new MockMalfunctionEncoder(this);
- }
- }
-
- /*
- * Mock encoder. encodeLoop always throws unexpected exception.
- */
- static class MockMalfunctionEncoder extends java.nio.charset.CharsetEncoder {
-
- public MockMalfunctionEncoder(Charset cs) {
- super(cs, 1, 3, new byte[] { (byte) '?' });
- }
-
- protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
- throw new BufferOverflowException();
- }
- }
-
- /*
- * Test reserve bytes encode(CharBuffer,ByteBuffer,boolean)
- */
- public void test_EncodeLjava_nio_CharBufferLjava_nio_ByteBufferB() {
- CharsetEncoder encoder = Charset.forName("utf-8").newEncoder();
- CharBuffer in1 = CharBuffer.wrap("\ud800");
- CharBuffer in2 = CharBuffer.wrap("\udc00");
- ByteBuffer out = ByteBuffer.allocate(4);
- encoder.reset();
- CoderResult result = encoder.encode(in1, out, false);
- assertEquals(4, out.remaining());
- assertTrue(result.isUnderflow());
- result = encoder.encode(in2, out, true);
- assertEquals(4, out.remaining());
- assertTrue(result.isMalformed());
- }
+ public void test_EncodeLjava_nio_CharBuffer() throws Exception {
+ MockMalfunctionCharset cs = new MockMalfunctionCharset("mock", null);
+ try {
+ cs.encode(CharBuffer.wrap("AB"));
+ fail("should throw CoderMalfunctionError");// NON-NLS-1$
+ } catch (CoderMalfunctionError e) {
+ // expected
+ }
+ }
+
+ /*
+ * Mock charset class with malfunction decode & encode.
+ */
+ static final class MockMalfunctionCharset extends Charset {
+
+ public MockMalfunctionCharset(String canonicalName, String[] aliases) {
+ super(canonicalName, aliases);
+ }
+
+ public boolean contains(Charset cs) {
+ return false;
+ }
+
+ public CharsetDecoder newDecoder() {
+ return Charset.forName("UTF-8").newDecoder();
+ }
+
+ public CharsetEncoder newEncoder() {
+ return new MockMalfunctionEncoder(this);
+ }
+ }
+
+ /*
+ * Mock encoder. encodeLoop always throws unexpected exception.
+ */
+ static class MockMalfunctionEncoder extends java.nio.charset.CharsetEncoder {
+
+ public MockMalfunctionEncoder(Charset cs) {
+ super(cs, 1, 3, new byte[] { (byte) '?' });
+ }
+
+ protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
+ throw new BufferOverflowException();
+ }
+ }
+
+ /*
+ * Test reserve bytes encode(CharBuffer,ByteBuffer,boolean)
+ */
+ public void test_EncodeLjava_nio_CharBufferLjava_nio_ByteBufferB() {
+ CharsetEncoder encoder = Charset.forName("utf-8").newEncoder();
+ CharBuffer in1 = CharBuffer.wrap("\ud800");
+ CharBuffer in2 = CharBuffer.wrap("\udc00");
+ ByteBuffer out = ByteBuffer.allocate(4);
+ encoder.reset();
+ CoderResult result = encoder.encode(in1, out, false);
+ assertEquals(4, out.remaining());
+ assertTrue(result.isUnderflow());
+ result = encoder.encode(in2, out, true);
+ assertEquals(4, out.remaining());
+ assertTrue(result.isMalformed());
+ }
+
+ /**
+ * @tests {@link java.nio.charset.Charset#encode(java.nio.CharBuffer)
+ */
+ public void testUtf8Encoding() throws IOException {
+ byte[] orig = new byte[] { (byte) 0xed, (byte) 0xa0,
+ (byte) 0x80 };
+ String s = new String(orig, "UTF-8");
+ assertEquals(1, s.length());
+ assertEquals(55296, s.charAt(0));
+ Charset.forName("UTF-8").encode(CharBuffer.wrap(s));
+// ByteBuffer buf = <result>
+// for (byte o : orig) {
+// byte b = 0;
+// buf.get(b);
+// assertEquals(o, b);
+// }
+ }
+
}
Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetDecoderTest.java?rev=644228&r1=644227&r2=644228&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetDecoderTest.java Thu Apr 3 02:03:50 2008
@@ -42,7 +42,7 @@
// assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
// }
- ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
+ ByteBuffer getUnmappedByteBuffer() {
// FIXME: different here
return null;
// ByteBuffer buffer = ByteBuffer.allocate(8);
@@ -53,11 +53,11 @@
}
- ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
+ ByteBuffer getMalformedByteBuffer() {
// FIXME: different here
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.put((byte) -1);
- buffer.put(unibytes);
+ buffer.put(getByteBuffer());
buffer.flip();
return buffer;
Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java?rev=644228&r1=644227&r2=644228&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java Thu Apr 3 02:03:50 2008
@@ -33,752 +33,833 @@
*/
public class CharsetDecoderTest extends TestCase {
- static final String unistr = " buffer";// \u8000\u8001\u00a5\u3000\r\n";
+ protected static final int MAX_BYTES = 3;
- byte[] unibytes = new byte[] { 32, 98, 117, 102, 102, 101, 114 };
+ protected static final double AVER_BYTES = 0.5;
- protected static final int MAX_BYTES = 3;
+ // default charset
+ private static final Charset MOCKCS = new CharsetEncoderTest.MockCharset(
+ "mock", new String[0]);
+
+ Charset cs = MOCKCS;
+
+ // default decoder
+ protected static CharsetDecoder decoder;
+
+ String bom = "";
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ decoder = cs.newDecoder();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ // FIXME: give up this tests
+ // /*
+ // * test default value
+ // */
+ // public void testDefaultCharsPerByte() {
+ // assertTrue(decoder.averageCharsPerByte() == AVER_BYTES);
+ // assertTrue(decoder.maxCharsPerByte() == MAX_BYTES);
+ // }
+
+ public void testDefaultValues() {
+ assertSame(cs, decoder.charset());
+ try {
+ decoder.detectedCharset();
+ fail("should unsupported");
+ } catch (UnsupportedOperationException e) {
+ }
+ try {
+ assertTrue(decoder.isCharsetDetected());
+ fail("should unsupported");
+ } catch (UnsupportedOperationException e) {
+ }
+ assertFalse(decoder.isAutoDetecting());
+ assertSame(CodingErrorAction.REPORT, decoder.malformedInputAction());
+ assertSame(CodingErrorAction.REPORT, decoder
+ .unmappableCharacterAction());
+ assertEquals(decoder.replacement(), "\ufffd");
+ }
+
+ /*
+ * test constructor
+ */
+ public void testCharsetDecoder() {
+ // default value
+ decoder = new MockCharsetDecoder(cs, (float) AVER_BYTES, MAX_BYTES);
+
+ // normal case
+ CharsetDecoder ec = new MockCharsetDecoder(cs, 1, MAX_BYTES);
+ assertSame(ec.charset(), cs);
+ assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
+ assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
+
+ /*
+ * ------------------------ Exceptional cases -------------------------
+ */
+ // Normal case: null charset
+ ec = new MockCharsetDecoder(null, 1, MAX_BYTES);
+ assertNull(ec.charset());
+ assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
+ assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
+
+ ec = new MockCharsetDecoder(new CharsetEncoderTest.MockCharset("mock",
+ new String[0]), 1, MAX_BYTES);
+
+ // Commented out since the comment is wrong since MAX_BYTES > 1
+ // // OK: average length less than max length
+ // ec = new MockCharsetDecoder(cs, MAX_BYTES, 1);
+ // assertTrue(ec.averageCharsPerByte() == MAX_BYTES);
+ // assertTrue(ec.maxCharsPerByte() == 1);
+
+ // Illegal Argument: zero length
+ try {
+ ec = new MockCharsetDecoder(cs, 0, MAX_BYTES);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ ec = new MockCharsetDecoder(cs, 1, 0);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+
+ // Illegal Argument: negative length
+ try {
+ ec = new MockCharsetDecoder(cs, -1, MAX_BYTES);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ ec = new MockCharsetDecoder(cs, 1, -1);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ /*
+ * test onMalformedInput
+ */
+ public void testOnMalformedInput() {
+ assertSame(CodingErrorAction.REPORT, decoder.malformedInputAction());
+ try {
+ decoder.onMalformedInput(null);
+ fail("should throw null pointer exception");
+ } catch (IllegalArgumentException e) {
+ }
+ decoder.onMalformedInput(CodingErrorAction.IGNORE);
+ assertSame(CodingErrorAction.IGNORE, decoder.malformedInputAction());
+ }
+
+ /*
+ * test unmappableCharacter
+ */
+ public void testOnUnmappableCharacter() {
+ assertSame(CodingErrorAction.REPORT, decoder
+ .unmappableCharacterAction());
+ try {
+ decoder.onUnmappableCharacter(null);
+ fail("should throw null pointer exception");
+ } catch (IllegalArgumentException e) {
+ }
+ decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+ assertSame(CodingErrorAction.IGNORE, decoder
+ .unmappableCharacterAction());
+ }
+
+ /*
+ * test replaceWith
+ */
+ public void testReplaceWith() {
+ try {
+ decoder.replaceWith(null);
+ fail("should throw null pointer exception");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ decoder.replaceWith("");
+ fail("should throw null pointer exception");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ decoder.replaceWith("testReplaceWith");
+ fail("should throw illegal argument exception");
+ } catch (IllegalArgumentException e) {
+ }
+
+ decoder.replaceWith("a");
+ assertSame("a", decoder.replacement());
+ }
+
+ /*
+ * Class under test for CharBuffer decode(ByteBuffer)
+ */
+ public void testDecodeByteBuffer() throws CharacterCodingException {
+ implTestDecodeByteBuffer();
+ }
+
+ void implTestDecodeByteBuffer() throws CharacterCodingException {
+ // Null pointer
+ try {
+ decoder.decode(null);
+ fail("should throw null pointer exception");
+ } catch (NullPointerException e) {
+ }
+
+ // empty input buffer
+ CharBuffer out = decoder.decode(ByteBuffer.allocate(0));
+ assertCharBufferValue("", out);
+
+ // normal case
+ ByteBuffer in = getByteBuffer();
+ out = decoder.decode(in);
+ assertEquals(0, out.position());
+ assertEquals(getString().length(), out.limit());
+ assertEquals(getString().length(), out.remaining());
+ assertCharBufferValue(getString(), out);
+
+ // normal read only case
+ in = getByteBuffer().asReadOnlyBuffer();
+ out = decoder.decode(in);
+ assertEquals(out.position(), 0);
+ assertEquals(out.limit(), getString().length());
+ assertEquals(out.remaining(), getString().length());
+ assertCharBufferValue(getString(), out);
+ }
+
+ public void testDecodeByteBufferException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ CharBuffer out;
+ ByteBuffer in;
+ String replaceStr = decoder.replacement() + getString();
+
+ // MalformedException:
+ decoder.onMalformedInput(CodingErrorAction.REPORT);
+ decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+ in = getMalformedByteBuffer();
+ if (in != null) {
+ try {
+ CharBuffer buffer = decoder.decode(in);
+ assertTrue(buffer.remaining() > 0);
+ fail("should throw MalformedInputException");
+ } catch (MalformedInputException e) {
+ }
+
+ decoder.reset();
+ in.rewind();
+ decoder.onMalformedInput(CodingErrorAction.IGNORE);
+ out = decoder.decode(in);
+ assertCharBufferValue(getString(), out);
+
+ decoder.reset();
+ in.rewind();
+ decoder.onMalformedInput(CodingErrorAction.REPLACE);
+ out = decoder.decode(in);
+ assertCharBufferValue(replaceStr, out);
+ }
+
+ // Unmapped Exception:
+ decoder.onMalformedInput(CodingErrorAction.REPORT);
+ decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+ in = getUnmappedByteBuffer();
+ if (in != null) {
+ try {
+ decoder.decode(in);
+ fail("should throw UnmappableCharacterException");
+ } catch (UnmappableCharacterException e) {
+ }
+
+ decoder.reset();
+ in.rewind();
+ decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+ out = decoder.decode(in);
+ assertCharBufferValue(getString(), out);
+
+ decoder.reset();
+ in.rewind();
+ decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
+ out = decoder.decode(in);
+ assertCharBufferValue(replaceStr, out);
+ }
+
+ // RuntimeException
+ try {
+ decoder.decode(getExceptionByteArray());
+ fail("should throw runtime exception");
+ } catch (RuntimeException e) {
+ }
+ }
+
+ /*
+ * Class under test for CoderResult decode(ByteBuffer, CharBuffer, boolean)
+ */
+ public void testDecodeByteBufferCharBuffer() {
+ implTestDecodeByteBufferCharBuffer(getByteBuffer());
+ }
+
+ public void testDecodeByteBufferCharBufferReadOnly() {
+ implTestDecodeByteBufferCharBuffer(getByteBuffer());
+ }
+
+ void implTestDecodeByteBufferCharBuffer(ByteBuffer in) {
+ CharBuffer out = CharBuffer.allocate(100);
+
+ // Null pointer
+ decoder.reset();
+ try {
+ decoder.decode(null, out, true);
+ fail("NullPointerException expected");
+ } catch (NullPointerException e) {
+ }
+ try {
+ decoder.decode(in, null, true);
+ fail("NullPointerException expected");
+ } catch (NullPointerException e) {
+ }
+
+ // normal case, one complete operation
+ decoder.reset();
+ in.rewind();
+ out.rewind();
+ assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
+ assertEquals(out.limit(), 100);
+ assertEquals(out.position(), getString().length());
+ assertEquals(out.remaining(), 100 - getString().length());
+ assertEquals(out.capacity(), 100);
+ assertCharBufferValue(getString(), out);
+ decoder.flush(out);
+
+ // normal case, one complete operation, but call twice, first time set
+ // endOfInput to false
+ decoder.reset();
+ in.rewind();
+ out.clear();
+ assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
+ assertEquals(out.limit(), 100);
+ assertEquals(out.position(), getString().length());
+ assertEquals(out.remaining(), 100 - getString().length());
+ assertEquals(out.capacity(), 100);
+ assertCharBufferValue(getString(), out);
+
+ decoder.reset();
+ in.rewind();
+ out.clear();
+ assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
+ in = getHeadlessByteBuffer();
+ assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
+ in.rewind();
+ assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
+ assertEquals(out.limit(), 100);
+ assertTrue(out.position() > 0);
+ assertEquals(out.remaining(), out.capacity() - out.position());
+ assertEquals(out.capacity(), 100);
+ assertCharBufferValue(getString() + getString() + getString(), out);
+
+ // overflow
+ out = CharBuffer.allocate(4);
+ decoder.reset();
+ in = getByteBuffer();
+ out.rewind();
+ assertSame(CoderResult.OVERFLOW, decoder.decode(in, out, false));
+
+ assertCharBufferValue(getString().substring(0, 4), out);
+ out = CharBuffer.allocate(100);
+ assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
+ assertCharBufferValue(getString().substring(4), out);
+ in.rewind();
+ out = CharBuffer.allocate(100);
+ assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
+ assertCharBufferValue(bom + getString(), out);
+ }
+
+ public void testDecodeCharBufferByteBufferUnmappedException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ implTestDecodeCharBufferByteBufferUnmappedException(
+ getUnmappedByteBuffer(), true);
+ }
+
+ public void testDecodeCharBufferByteIncompleteBufferUnmappedException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ implTestDecodeCharBufferByteBufferUnmappedException(
+ getUnmappedByteBuffer(), false);
+ }
+
+ public void testDecodeCharBufferByteReadOnlyBufferUnmappedException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ implTestDecodeCharBufferByteBufferUnmappedException(
+ readOnly(getUnmappedByteBuffer()), true);
+ }
+
+ public void testDecodeCharBufferByteReadOnlyIncompleteBufferUnmappedException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ implTestDecodeCharBufferByteBufferUnmappedException(
+ readOnly(getUnmappedByteBuffer()), false);
+ }
+
+ void implTestDecodeCharBufferByteBufferUnmappedException(ByteBuffer in,
+ boolean endOfInput) throws CharacterCodingException,
+ UnsupportedEncodingException {
+ if (null == in) {
+ return;
+ }
+ CharBuffer out = CharBuffer.allocate(50);
+
+ decoder.onMalformedInput(CodingErrorAction.REPORT);
+ decoder.reset();
+ decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+ CoderResult result = decoder.decode(in, out, endOfInput);
+ assertTrue(result.isUnmappable());
+
+ decoder.reset();
+ out.clear();
+ in.rewind();
+ decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+ assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, endOfInput));
+ assertCharBufferValue(getString(), out);
+
+ decoder.reset();
+ out.clear();
+ in.rewind();
+ decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
+ assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, endOfInput));
+ assertCharBufferValue(decoder.replacement() + getString(), out);
+ }
+
+ public void testDecodeCharBufferByteBufferMalformedException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ implTestDecodeCharBufferByteBufferMalformedException(
+ getMalformedByteBuffer(), true);
+ }
+
+ public void testDecodeCharBufferByteIncompleteBufferMalformedException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+
+ implTestDecodeCharBufferByteBufferMalformedException(
+ getMalformedByteBuffer(), false);
+ }
+
+ public void testDecodeCharBufferByteReadOnlyBufferMalformedException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ implTestDecodeCharBufferByteBufferMalformedException(
+ readOnly(getMalformedByteBuffer()), true);
+ }
+
+ public void testDecodeCharBufferByteReadOnlyIncompleteBufferMalformedException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ implTestDecodeCharBufferByteBufferMalformedException(
+ readOnly(getMalformedByteBuffer()), false);
+ }
+
+ void implTestDecodeCharBufferByteBufferMalformedException(ByteBuffer in,
+ boolean endOfInput) throws CharacterCodingException,
+ UnsupportedEncodingException {
+ if (null == in) {
+ return;
+ }
+ CharBuffer out = CharBuffer.allocate(getString().length() * 3);
+ decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+ decoder.reset();
+ decoder.onMalformedInput(CodingErrorAction.REPORT);
+ CoderResult result = decoder.decode(in, out, endOfInput);
+ assertTrue(result.isMalformed());
+
+ decoder.reset();
+ out.clear();
+ in.rewind();
+ decoder.onMalformedInput(CodingErrorAction.IGNORE);
+ assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, endOfInput));
+ assertCharBufferValue(getString(), out);
+
+ decoder.reset();
+ out.clear();
+ in.rewind();
+ decoder.onMalformedInput(CodingErrorAction.REPLACE);
+ assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, endOfInput));
+ assertCharBufferValue(decoder.replacement() + getString(), out);
+ }
+
+ public void testDecodeCharBufferByteBufferException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ implTestDecodeCharBufferByteBufferException(getExceptionByteArray(),
+ true);
+ }
+
+ public void testDecodeCharBufferByteIncompleteBufferException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ implTestDecodeCharBufferByteBufferException(getExceptionByteArray(),
+ false);
+ }
+
+ public void testDecodeCharBufferByteReadOnlyBufferException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ implTestDecodeCharBufferByteBufferException(
+ readOnly(getExceptionByteArray()), true);
+ }
+
+ public void testDecodeCharBufferByteReadOnlyIncompleteBufferException()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ implTestDecodeCharBufferByteBufferException(
+ readOnly(getExceptionByteArray()), false);
+ }
+
+ void implTestDecodeCharBufferByteBufferException(ByteBuffer in,
+ boolean endOfInput) throws CharacterCodingException,
+ UnsupportedEncodingException {
+ CharBuffer out = CharBuffer.allocate(50);
+ decoder.reset();
+ try {
+ decoder.decode(in, out, endOfInput);
+ fail("should throw runtime exception");
+ } catch (RuntimeException e) {
+ }
+ }
+
+ private ByteBuffer readOnly(ByteBuffer b) {
+ if (null == b) {
+ return null;
+ }
+ return b.asReadOnlyBuffer();
+ }
+
+ protected String getString() {
+ return " buffer";
+ }
+
+ protected ByteBuffer getByteBuffer() {
+ return ByteBuffer.wrap(new byte[] { 32, 98, 117, 102, 102, 101, 114 });
+ }
+
+ protected ByteBuffer getHeadlessByteBuffer() {
+ return getByteBuffer();
+ }
+
+ ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
+ // "runtime"
+ return ByteBuffer
+ .wrap(new byte[] { 114, 117, 110, 116, 105, 109, 101 });
+ }
+
+ ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
+ // "unmap buffer"
+ byte[] ba = new byte[] { 117, 110, 109, 97, 112, 32, 98, 117, 102, 102,
+ 101, 114 };
+ return ByteBuffer.wrap(ba);
+ }
+
+ ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
+ // "malform buffer"
+ byte[] ba = new byte[] { 109, 97, 108, 102, 111, 114, 109, 32, 98, 117,
+ 102, 102, 101, 114 };
+ return ByteBuffer.wrap(ba);
+ }
+
+ private void assertCharBufferValue(String expected, CharBuffer out) {
+ if (out.position() != 0) {
+ out.flip();
+ }
+ assertEquals(expected, new String(out.array(), out.arrayOffset(), out
+ .arrayOffset() + out.limit()));
+ }
+
+ /*
+ * test flush
+ */
+ public void testFlush() throws CharacterCodingException {
+ CharBuffer out = CharBuffer.allocate(10);
+ ByteBuffer in = ByteBuffer.wrap(new byte[] { 12, 12 });
+ decoder.decode(in, out, true);
+ assertSame(CoderResult.UNDERFLOW, decoder.flush(out));
+
+ decoder.reset();
+ decoder.decode((ByteBuffer) in.rewind(), (CharBuffer) out.rewind(),
+ true);
+ assertSame(CoderResult.UNDERFLOW, decoder
+ .flush(CharBuffer.allocate(10)));
+ }
+
+ /*
+ * ---------------------------------- methods to test illegal state
+ * -----------------------------------
+ */
+ // Normal case: just after reset, and it also means reset can be done
+ // anywhere
+ public void testResetIllegalState() throws CharacterCodingException {
+ decoder.reset();
+ decoder.decode(getByteBuffer());
+ decoder.reset();
+ decoder.decode(getByteBuffer(), CharBuffer.allocate(3), false);
+ decoder.reset();
+ decoder.decode(getByteBuffer(), CharBuffer.allocate(3), true);
+ decoder.reset();
+ }
+
+ public void testFlushIllegalState() throws CharacterCodingException {
+ ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
+ CharBuffer out = CharBuffer.allocate(5);
+ // Normal case: after decode with endOfInput is true
+ decoder.reset();
+ decoder.decode(in, out, true);
+ out.rewind();
+ CoderResult result = decoder.flush(out);
+ assertSame(result, CoderResult.UNDERFLOW);
+
+ // Illegal state: flush twice
+ try {
+ decoder.flush(out);
+ fail("should throw IllegalStateException");
+ } catch (IllegalStateException e) {
+ }
+
+ // Illegal state: flush after decode with endOfInput is false
+ decoder.reset();
+ decoder.decode(in, out, false);
+ try {
+ decoder.flush(out);
+ fail("should throw IllegalStateException");
+ } catch (IllegalStateException e) {
+ }
+ }
+
+ // test illegal states for decode facade
+ public void testDecodeFacadeIllegalState() throws CharacterCodingException {
+ // decode facade can be execute in anywhere
+ ByteBuffer in = getByteBuffer();
+
+ // Normal case: just created
+ decoder.decode(in);
+ in.rewind();
+
+ // Normal case: just after decode facade
+ decoder.decode(in);
+ in.rewind();
+
+ // Normal case: just after decode with that endOfInput is true
+ decoder.reset();
+ decoder.decode(getByteBuffer(), CharBuffer.allocate(30), true);
+ decoder.decode(in);
+ in.rewind();
+
+ // Normal case:just after decode with that endOfInput is false
+ decoder.reset();
+ decoder.decode(getByteBuffer(), CharBuffer.allocate(30), false);
+ decoder.decode(in);
+ in.rewind();
+
+ // Normal case: just after flush
+ decoder.reset();
+ decoder.decode(getByteBuffer(), CharBuffer.allocate(30), true);
+ decoder.flush(CharBuffer.allocate(10));
+ decoder.decode(in);
+ in.rewind();
+ }
+
+ // test illegal states for two decode method with endOfInput is true
+ public void testDecodeTrueIllegalState() throws CharacterCodingException {
+ ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
+ CharBuffer out = CharBuffer.allocate(100);
+ // Normal case: just created
+ decoder.decode(in, out, true);
+ in.rewind();
+ out.rewind();
+
+ // Normal case: just after decode with that endOfInput is true
+ decoder.reset();
+ decoder.decode(in, CharBuffer.allocate(30), true);
+ in.rewind();
+ decoder.decode(in, out, true);
+ in.rewind();
+ out.rewind();
+
+ // Normal case:just after decode with that endOfInput is false
+ decoder.reset();
+ decoder.decode(in, CharBuffer.allocate(30), false);
+ in.rewind();
+ decoder.decode(in, out, true);
+ in.rewind();
+ out.rewind();
+
+ // Illegal state: just after flush
+ decoder.reset();
+ decoder.decode(in, CharBuffer.allocate(30), true);
+ decoder.flush(CharBuffer.allocate(10));
+ in.rewind();
+ try {
+ decoder.decode(in, out, true);
+ fail("should illegal state");
+ } catch (IllegalStateException e) {
+ }
+ in.rewind();
+ out.rewind();
+
+ }
+
+ // test illegal states for two decode method with endOfInput is false
+ public void testDecodeFalseIllegalState() throws CharacterCodingException {
+ ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
+ CharBuffer out = CharBuffer.allocate(5);
+ // Normal case: just created
+ decoder.decode(in, out, false);
+ in.rewind();
+ out.rewind();
+
+ // Illegal state: just after decode facade
+ decoder.reset();
+ decoder.decode(in);
+ in.rewind();
+ try {
+ decoder.decode(in, out, false);
+ fail("should illegal state");
+ } catch (IllegalStateException e) {
+ }
+ in.rewind();
+ out.rewind();
+
+ // Illegal state: just after decode with that endOfInput is true
+ decoder.reset();
+ decoder.decode(in, CharBuffer.allocate(30), true);
+ in.rewind();
+ try {
+ decoder.decode(in, out, false);
+ fail("should illegal state");
+ } catch (IllegalStateException e) {
+ }
+ in.rewind();
+ out.rewind();
+
+ // Normal case:just after decode with that endOfInput is false
+ decoder.reset();
+ decoder.decode(in, CharBuffer.allocate(30), false);
+ in.rewind();
+ decoder.decode(in, out, false);
+ in.rewind();
+ out.rewind();
+
+ // Illegal state: just after flush
+ decoder.reset();
+ decoder.decode(in, CharBuffer.allocate(30), true);
+ in.rewind();
+ decoder.flush(CharBuffer.allocate(10));
+ try {
+ decoder.decode(in, out, false);
+ fail("should illegal state");
+ } catch (IllegalStateException e) {
+ }
+ }
+
+ /*
+ * --------------------------------- illegal state test end
+ * ---------------------------------
+ */
+
+ public void testImplFlush() {
+ decoder = new MockCharsetDecoder(cs, 1, 3);
+ assertEquals(CoderResult.UNDERFLOW, ((MockCharsetDecoder) decoder)
+ .pubImplFlush(null));
+ }
+
+ public void testImplOnMalformedInput() {
+ decoder = new MockCharsetDecoder(cs, 1, 3);
+ assertEquals(CoderResult.UNDERFLOW, ((MockCharsetDecoder) decoder)
+ .pubImplFlush(null));
+
+ }
+
+ public void testImplOnUnmappableCharacter() {
+ decoder = new MockCharsetDecoder(cs, 1, 3);
+ ((MockCharsetDecoder) decoder).pubImplOnUnmappableCharacter(null);
+ }
+
+ public void testImplReplaceWith() {
+ decoder = new MockCharsetDecoder(cs, 1, 3);
+ ((MockCharsetDecoder) decoder).pubImplReplaceWith(null);
+ }
+
+ public void testImplReset() {
+ decoder = new MockCharsetDecoder(cs, 1, 3);
+ ((MockCharsetDecoder) decoder).pubImplReset();
+ }
+
+ /*
+ * mock decoder
+ */
+ public static class MockCharsetDecoder extends CharsetDecoder {
+ public MockCharsetDecoder(Charset cs, float ave, float max) {
+ super(cs, ave, max);
+ }
+
+ protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
+ int inPosition = in.position();
+ byte[] input = new byte[in.remaining()];
+ in.get(input);
+ String result = new String(input);
+ if (result.startsWith("malform")) {
+ // reset the cursor to the error position
+ in.position(inPosition);
+ // set the error length
+ return CoderResult.malformedForLength("malform".length());
+ } else if (result.startsWith("unmap")) {
+ // reset the cursor to the error position
+ in.position(inPosition);
+ // set the error length
+ return CoderResult.unmappableForLength("unmap".length());
+ } else if (result.startsWith("runtime")) {
+ // reset the cursor to the error position
+ in.position(0);
+ // set the error length
+ throw new RuntimeException("runtime");
+ }
+ int inLeft = input.length;
+ int outLeft = out.remaining();
+ CoderResult r = CoderResult.UNDERFLOW;
+ int length = inLeft;
+ if (outLeft < inLeft) {
+ r = CoderResult.OVERFLOW;
+ length = outLeft;
+ in.position(inPosition + outLeft);
+ }
+ for (int i = 0; i < length; i++) {
+ out.put((char) input[i]);
+ }
+ return r;
+ }
+
+ protected CoderResult implFlush(CharBuffer out) {
+ CoderResult result = super.implFlush(out);
+ if (out.remaining() >= 5) {
+ // TODO
+ // out.put("flush");
+ result = CoderResult.UNDERFLOW;
+ } else {
+ // out.put("flush", 0, out.remaining());
+ result = CoderResult.OVERFLOW;
+ }
+ return result;
+ }
+
+ public CoderResult pubImplFlush(CharBuffer out) {
+ return super.implFlush(out);
+ }
+
+ public void pubImplOnMalformedInput(CodingErrorAction newAction) {
+ super.implOnMalformedInput(newAction);
+ }
+
+ public void pubImplOnUnmappableCharacter(CodingErrorAction newAction) {
+ super.implOnUnmappableCharacter(newAction);
+ }
+
+ public void pubImplReplaceWith(String newReplacement) {
+ super.implReplaceWith(newReplacement);
+ }
+
+ public void pubImplReset() {
+ super.implReset();
+ }
- protected static final double AVER_BYTES = 0.5;
-
- // default charset
- private static final Charset MOCKCS = new CharsetEncoderTest.MockCharset(
- "mock", new String[0]);
-
- Charset cs = MOCKCS;
-
- // default decoder
- protected static CharsetDecoder decoder;
-
- String bom = "";
-
- protected void setUp() throws Exception {
- super.setUp();
- decoder = cs.newDecoder();
- }
-
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
- // FIXME: give up this tests
- // /*
- // * test default value
- // */
- // public void testDefaultCharsPerByte() {
- // assertTrue(decoder.averageCharsPerByte() == AVER_BYTES);
- // assertTrue(decoder.maxCharsPerByte() == MAX_BYTES);
- // }
-
- public void testDefaultValues() {
- assertSame(cs, decoder.charset());
- try {
- decoder.detectedCharset();
- fail("should unsupported");
- } catch (UnsupportedOperationException e) {
- }
- try {
- assertTrue(decoder.isCharsetDetected());
- fail("should unsupported");
- } catch (UnsupportedOperationException e) {
- }
- assertFalse(decoder.isAutoDetecting());
- assertSame(CodingErrorAction.REPORT, decoder.malformedInputAction());
- assertSame(CodingErrorAction.REPORT, decoder
- .unmappableCharacterAction());
- assertEquals(decoder.replacement(), "\ufffd");
- }
-
- /*
- * test constructor
- */
- public void testCharsetDecoder() {
- // default value
- decoder = new MockCharsetDecoder(cs, (float) AVER_BYTES, MAX_BYTES);
-
- // normal case
- CharsetDecoder ec = new MockCharsetDecoder(cs, 1, MAX_BYTES);
- assertSame(ec.charset(), cs);
- assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
- assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
-
- /*
- * ------------------------ Exceptional cases -------------------------
- */
- // Normal case: null charset
- ec = new MockCharsetDecoder(null, 1, MAX_BYTES);
- assertNull(ec.charset());
- assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
- assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
-
- ec = new MockCharsetDecoder(new CharsetEncoderTest.MockCharset("mock",
- new String[0]), 1, MAX_BYTES);
-
- // Commented out since the comment is wrong since MAX_BYTES > 1
- // // OK: average length less than max length
- // ec = new MockCharsetDecoder(cs, MAX_BYTES, 1);
- // assertTrue(ec.averageCharsPerByte() == MAX_BYTES);
- // assertTrue(ec.maxCharsPerByte() == 1);
-
- // Illegal Argument: zero length
- try {
- ec = new MockCharsetDecoder(cs, 0, MAX_BYTES);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- }
- try {
- ec = new MockCharsetDecoder(cs, 1, 0);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- }
-
- // Illegal Argument: negative length
- try {
- ec = new MockCharsetDecoder(cs, -1, MAX_BYTES);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- }
- try {
- ec = new MockCharsetDecoder(cs, 1, -1);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- }
- }
-
- /*
- * test onMalformedInput
- */
- public void testOnMalformedInput() {
- assertSame(CodingErrorAction.REPORT, decoder.malformedInputAction());
- try {
- decoder.onMalformedInput(null);
- fail("should throw null pointer exception");
- } catch (IllegalArgumentException e) {
- }
- decoder.onMalformedInput(CodingErrorAction.IGNORE);
- assertSame(CodingErrorAction.IGNORE, decoder.malformedInputAction());
- }
-
- /*
- * test unmappableCharacter
- */
- public void testOnUnmappableCharacter() {
- assertSame(CodingErrorAction.REPORT, decoder
- .unmappableCharacterAction());
- try {
- decoder.onUnmappableCharacter(null);
- fail("should throw null pointer exception");
- } catch (IllegalArgumentException e) {
- }
- decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
- assertSame(CodingErrorAction.IGNORE, decoder
- .unmappableCharacterAction());
- }
-
- /*
- * test replaceWith
- */
- public void testReplaceWith() {
- try {
- decoder.replaceWith(null);
- fail("should throw null pointer exception");
- } catch (IllegalArgumentException e) {
- }
- try {
- decoder.replaceWith("");
- fail("should throw null pointer exception");
- } catch (IllegalArgumentException e) {
- }
- try {
- decoder.replaceWith("testReplaceWith");
- fail("should throw illegal argument exception");
- } catch (IllegalArgumentException e) {
- }
-
- decoder.replaceWith("a");
- assertSame("a", decoder.replacement());
- }
-
- /*
- * Class under test for CharBuffer decode(ByteBuffer)
- */
- public void testDecodeByteBuffer() throws CharacterCodingException {
- implTestDecodeByteBuffer();
- }
-
- void implTestDecodeByteBuffer() throws CharacterCodingException {
- // Null pointer
- try {
- decoder.decode(null);
- fail("should throw null pointer exception");
- } catch (NullPointerException e) {
- }
-
- // empty input buffer
- CharBuffer out = decoder.decode(ByteBuffer.allocate(0));
- assertCharBufferValue(out, "");
-
- // normal case
- ByteBuffer in = ByteBuffer.wrap(getUnibytes());
- out = decoder.decode(in);
- assertEquals(out.position(), 0);
- assertEquals(out.limit(), unistr.length());
- assertEquals(out.remaining(), unistr.length());
- assertEquals(new String(out.array(), 0, out.limit()), unistr);
- }
-
- public void testDecodeByteBufferException()
- throws CharacterCodingException, UnsupportedEncodingException {
- CharBuffer out;
- ByteBuffer in;
- String replaceStr = decoder.replacement() + " buffer";
-
- // MalformedException:
- decoder.onMalformedInput(CodingErrorAction.REPORT);
- decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
- in = getMalformByteBuffer();
- if (in != null) {
- try {
- CharBuffer buffer = decoder.decode(in);
- assertTrue(buffer.remaining() > 0);
- fail("should throw MalformedInputException");
- } catch (MalformedInputException e) {
- }
-
- decoder.reset();
- in.rewind();
- decoder.onMalformedInput(CodingErrorAction.IGNORE);
- out = decoder.decode(in);
- assertCharBufferValue(out, " buffer");
-
- decoder.reset();
- in.rewind();
- decoder.onMalformedInput(CodingErrorAction.REPLACE);
- out = decoder.decode(in);
- assertCharBufferValue(out, replaceStr);
- }
-
- // Unmapped Exception:
- decoder.onMalformedInput(CodingErrorAction.REPORT);
- decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
- in = getUnmappedByteBuffer();
- if (in != null) {
- try {
- decoder.decode(in);
- fail("should throw UnmappableCharacterException");
- } catch (UnmappableCharacterException e) {
- }
-
- decoder.reset();
- in.rewind();
- decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
- out = decoder.decode(in);
- assertCharBufferValue(out, " buffer");
-
- decoder.reset();
- in.rewind();
- decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
- out = decoder.decode(in);
- assertCharBufferValue(out, replaceStr);
- }
-
- // RuntimeException
- try {
- decoder.decode(getExceptionByteArray());
- fail("should throw runtime exception");
- } catch (RuntimeException e) {
- }
- }
-
- /*
- * Class under test for CoderResult decode(ByteBuffer, CharBuffer, boolean)
- */
- public void testDecodeByteBufferCharBufferboolean() {
- implTestDecodeByteBufferCharBufferboolean();
- }
-
- void implTestDecodeByteBufferCharBufferboolean() {
- byte[] gb = getUnibytes();
- ByteBuffer in = ByteBuffer.wrap(gb);
- CharBuffer out = CharBuffer.allocate(100);
-
- // Null pointer
- try {
- decoder.decode(null, out, true);
- fail("should throw null pointer exception");
- } catch (NullPointerException e) {
- }
- try {
- decoder.decode(in, null, true);
- fail("should throw null pointer exception");
- } catch (NullPointerException e) {
- }
-
- // normal case, one complete operation
- decoder.reset();
- in.rewind();
- out.rewind();
- assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
- assertEquals(out.limit(), 100);
- assertEquals(out.position(), unistr.length());
- assertEquals(out.remaining(), 100 - unistr.length());
- assertEquals(out.capacity(), 100);
- assertCharBufferValue(out, unistr);
- decoder.flush(out);
-
- // normal case, one complete operation, but call twice, first time set
- // endOfInput to false
- decoder.reset();
- in.rewind();
- out.clear();
- assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
- assertEquals(out.limit(), 100);
- assertEquals(out.position(), unistr.length());
- assertEquals(out.remaining(), 100 - unistr.length());
- assertEquals(out.capacity(), 100);
- assertCharBufferValue(out, unistr);
-
- decoder.reset();
- in.rewind();
- out.clear();
- assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
- in = ByteBuffer.wrap(unibytes);
- assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
- in.rewind();
- assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
- assertEquals(out.limit(), 100);
- assertTrue(out.position() > 0);
- assertEquals(out.remaining(), out.capacity() - out.position());
- assertEquals(out.capacity(), 100);
- assertCharBufferValue(out, unistr + unistr + unistr);
-
- // overflow
- out = CharBuffer.allocate(4);
- decoder.reset();
- in = ByteBuffer.wrap(getUnibytes());
- out.rewind();
- assertSame(CoderResult.OVERFLOW, decoder.decode(in, out, false));
-
- assertEquals(new String(out.array()), unistr.substring(0, 4));
- out = CharBuffer.allocate(100);
- assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
- assertCharBufferValue(out, unistr.substring(4));
- in.rewind();
- out = CharBuffer.allocate(100);
- assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
- assertCharBufferValue(out, bom + unistr);
- }
-
- public void testDecodeCharBufferByteBufferbooleanExceptionTrue()
- throws CharacterCodingException, UnsupportedEncodingException {
- implTestDecodeCharBufferByteBufferbooleanException(true);
- }
-
- public void testDecodeCharBufferByteBufferbooleanExceptionFalse()
- throws CharacterCodingException, UnsupportedEncodingException {
- implTestDecodeCharBufferByteBufferbooleanException(false);
- }
-
- void implTestDecodeCharBufferByteBufferbooleanException(boolean endOfInput)
- throws CharacterCodingException, UnsupportedEncodingException {
- CharBuffer out;
- ByteBuffer in;
-
- // Unmapped Exception:
- in = getUnmappedByteBuffer();
- out = CharBuffer.allocate(50);
- decoder.onMalformedInput(CodingErrorAction.REPORT);
- if (null != in) {
- decoder.reset();
- decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
- CoderResult result = decoder.decode(in, out, endOfInput);
- assertTrue(result.isUnmappable());
-
- decoder.reset();
- out.clear();
- in.rewind();
- decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
- assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out,
- endOfInput));
- assertCharBufferValue(out, " buffer");
-
- decoder.reset();
- out.clear();
- in.rewind();
- decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
- assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out,
- endOfInput));
- assertCharBufferValue(out, decoder.replacement() + " buffer");
- } else if (endOfInput) {
- System.err.println("Cannot find unmappable byte array for "
- + cs.name());
- }
-
- // MalformedException:
- in = getMalformByteBuffer();
- out = CharBuffer.allocate(50);
- decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
- if (null != in) {
- decoder.onMalformedInput(CodingErrorAction.REPORT);
- CoderResult result = decoder.decode(in, out, endOfInput);
- assertTrue(result.isMalformed());
-
- decoder.reset();
- out.clear();
- in.rewind();
- decoder.onMalformedInput(CodingErrorAction.IGNORE);
- assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out,
- endOfInput));
- assertCharBufferValue(out, " buffer");
-
- decoder.reset();
- out.clear();
- in.rewind();
- decoder.onMalformedInput(CodingErrorAction.REPLACE);
- assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out,
- endOfInput));
- assertCharBufferValue(out, decoder.replacement() + " buffer");
- } else if (endOfInput) {
- System.err.println("Cannot find malform byte array for "
- + cs.name());
- }
-
- // RuntimeException
- in = getExceptionByteArray();
- try {
- decoder.decode(in, out, endOfInput);
- fail("should throw runtime exception");
- } catch (RuntimeException e) {
- }
- }
-
- ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
- // "runtime"
- return ByteBuffer
- .wrap(new byte[] { 114, 117, 110, 116, 105, 109, 101 });
- }
-
- ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
- // "unmap buffer"
- byte[] ba = new byte[] { 117, 110, 109, 97, 112, 32, 98, 117, 102, 102,
- 101, 114 };
- return ByteBuffer.wrap(ba);
- }
-
- ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
- // "malform buffer"
- byte[] ba = new byte[] { 109, 97, 108, 102, 111, 114, 109, 32, 98, 117,
- 102, 102, 101, 114 };
- return ByteBuffer.wrap(ba);
- }
-
- void assertCharBufferValue(CharBuffer out, String expected) {
- if (out.position() != 0) {
- out.flip();
- }
- assertEquals(new String(out.array(), 0, out.limit()), expected);
- }
-
- /*
- * test flush
- */
- public void testFlush() throws CharacterCodingException {
- CharBuffer out = CharBuffer.allocate(10);
- ByteBuffer in = ByteBuffer.wrap(new byte[] { 12, 12 });
- decoder.decode(in, out, true);
- assertSame(CoderResult.UNDERFLOW, decoder.flush(out));
-
- decoder.reset();
- decoder.decode((ByteBuffer) in.rewind(), (CharBuffer) out.rewind(),
- true);
- assertSame(CoderResult.UNDERFLOW, decoder
- .flush(CharBuffer.allocate(10)));
- }
-
- /*
- * ---------------------------------- methods to test illegal state
- * -----------------------------------
- */
- // Normal case: just after reset, and it also means reset can be done
- // anywhere
- public void testResetIllegalState() throws CharacterCodingException {
- byte[] gb = getUnibytes();
- decoder.reset();
- decoder.decode(ByteBuffer.wrap(gb));
- decoder.reset();
- decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(3), false);
- decoder.reset();
- decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(3), true);
- decoder.reset();
- }
-
- public void testFlushIllegalState() throws CharacterCodingException {
- ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
- CharBuffer out = CharBuffer.allocate(5);
- // Normal case: after decode with endOfInput is true
- decoder.reset();
- decoder.decode(in, out, true);
- out.rewind();
- CoderResult result = decoder.flush(out);
- assertSame(result, CoderResult.UNDERFLOW);
-
- // Illegal state: flush twice
- try {
- decoder.flush(out);
- fail("should throw IllegalStateException");
- } catch (IllegalStateException e) {
- }
-
- // Illegal state: flush after decode with endOfInput is false
- decoder.reset();
- decoder.decode(in, out, false);
- try {
- decoder.flush(out);
- fail("should throw IllegalStateException");
- } catch (IllegalStateException e) {
- }
- }
-
- byte[] getUnibytes() {
- return unibytes;
- }
-
- // test illegal states for decode facade
- public void testDecodeFacadeIllegalState() throws CharacterCodingException {
- // decode facade can be execute in anywhere
- byte[] gb = getUnibytes();
- ByteBuffer in = ByteBuffer.wrap(gb);
- // Normal case: just created
- decoder.decode(in);
- in.rewind();
-
- // Normal case: just after decode facade
- decoder.decode(in);
- in.rewind();
-
- // Normal case: just after decode with that endOfInput is true
- decoder.reset();
- decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(30), true);
- decoder.decode(in);
- in.rewind();
-
- // Normal case:just after decode with that endOfInput is false
- decoder.reset();
- decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(30), false);
- decoder.decode(in);
- in.rewind();
-
- // Normal case: just after flush
- decoder.reset();
- decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(30), true);
- decoder.flush(CharBuffer.allocate(10));
- decoder.decode(in);
- in.rewind();
- }
-
- // test illegal states for two decode method with endOfInput is true
- public void testDecodeTrueIllegalState() throws CharacterCodingException {
- ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
- CharBuffer out = CharBuffer.allocate(100);
- // Normal case: just created
- decoder.decode(in, out, true);
- in.rewind();
- out.rewind();
-
- // Normal case: just after decode with that endOfInput is true
- decoder.reset();
- decoder.decode(in, CharBuffer.allocate(30), true);
- in.rewind();
- decoder.decode(in, out, true);
- in.rewind();
- out.rewind();
-
- // Normal case:just after decode with that endOfInput is false
- decoder.reset();
- decoder.decode(in, CharBuffer.allocate(30), false);
- in.rewind();
- decoder.decode(in, out, true);
- in.rewind();
- out.rewind();
-
- // Illegal state: just after flush
- decoder.reset();
- decoder.decode(in, CharBuffer.allocate(30), true);
- decoder.flush(CharBuffer.allocate(10));
- in.rewind();
- try {
- decoder.decode(in, out, true);
- fail("should illegal state");
- } catch (IllegalStateException e) {
- }
- in.rewind();
- out.rewind();
-
- }
-
- // test illegal states for two decode method with endOfInput is false
- public void testDecodeFalseIllegalState() throws CharacterCodingException {
- ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
- CharBuffer out = CharBuffer.allocate(5);
- // Normal case: just created
- decoder.decode(in, out, false);
- in.rewind();
- out.rewind();
-
- // Illegal state: just after decode facade
- decoder.reset();
- decoder.decode(in);
- in.rewind();
- try {
- decoder.decode(in, out, false);
- fail("should illegal state");
- } catch (IllegalStateException e) {
- }
- in.rewind();
- out.rewind();
-
- // Illegal state: just after decode with that endOfInput is true
- decoder.reset();
- decoder.decode(in, CharBuffer.allocate(30), true);
- in.rewind();
- try {
- decoder.decode(in, out, false);
- fail("should illegal state");
- } catch (IllegalStateException e) {
- }
- in.rewind();
- out.rewind();
-
- // Normal case:just after decode with that endOfInput is false
- decoder.reset();
- decoder.decode(in, CharBuffer.allocate(30), false);
- in.rewind();
- decoder.decode(in, out, false);
- in.rewind();
- out.rewind();
-
- // Illegal state: just after flush
- decoder.reset();
- decoder.decode(in, CharBuffer.allocate(30), true);
- in.rewind();
- decoder.flush(CharBuffer.allocate(10));
- try {
- decoder.decode(in, out, false);
- fail("should illegal state");
- } catch (IllegalStateException e) {
- }
- }
-
- /*
- * --------------------------------- illegal state test end
- * ---------------------------------
- */
-
- public void testImplFlush() {
- decoder = new MockCharsetDecoder(cs, 1, 3);
- assertEquals(CoderResult.UNDERFLOW, ((MockCharsetDecoder) decoder)
- .pubImplFlush(null));
- }
-
- public void testImplOnMalformedInput() {
- decoder = new MockCharsetDecoder(cs, 1, 3);
- assertEquals(CoderResult.UNDERFLOW, ((MockCharsetDecoder) decoder)
- .pubImplFlush(null));
-
- }
-
- public void testImplOnUnmappableCharacter() {
- decoder = new MockCharsetDecoder(cs, 1, 3);
- ((MockCharsetDecoder) decoder).pubImplOnUnmappableCharacter(null);
- }
-
- public void testImplReplaceWith() {
- decoder = new MockCharsetDecoder(cs, 1, 3);
- ((MockCharsetDecoder) decoder).pubImplReplaceWith(null);
- }
-
- public void testImplReset() {
- decoder = new MockCharsetDecoder(cs, 1, 3);
- ((MockCharsetDecoder) decoder).pubImplReset();
- }
-
- /*
- * mock decoder
- */
- public static class MockCharsetDecoder extends CharsetDecoder {
- public MockCharsetDecoder(Charset cs, float ave, float max) {
- super(cs, ave, max);
- }
-
- protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
- int inPosition = in.position();
- byte[] input = new byte[in.remaining()];
- in.get(input);
- String result = new String(input);
- if (result.startsWith("malform")) {
- // reset the cursor to the error position
- in.position(inPosition);
- // set the error length
- return CoderResult.malformedForLength("malform".length());
- } else if (result.startsWith("unmap")) {
- // reset the cursor to the error position
- in.position(inPosition);
- // set the error length
- return CoderResult.unmappableForLength("unmap".length());
- } else if (result.startsWith("runtime")) {
- // reset the cursor to the error position
- in.position(0);
- // set the error length
- throw new RuntimeException("runtime");
- }
- int inLeft = input.length;
- int outLeft = out.remaining();
- CoderResult r = CoderResult.UNDERFLOW;
- int length = inLeft;
- if (outLeft < inLeft) {
- r = CoderResult.OVERFLOW;
- length = outLeft;
- in.position(inPosition + outLeft);
- }
- for (int i = 0; i < length; i++) {
- out.put((char) input[i]);
- }
- return r;
- }
-
- protected CoderResult implFlush(CharBuffer out) {
- CoderResult result = super.implFlush(out);
- if (out.remaining() >= 5) {
- // TODO
- // out.put("flush");
- result = CoderResult.UNDERFLOW;
- } else {
- // out.put("flush", 0, out.remaining());
- result = CoderResult.OVERFLOW;
- }
- return result;
- }
-
- public CoderResult pubImplFlush(CharBuffer out) {
- return super.implFlush(out);
- }
-
- public void pubImplOnMalformedInput(CodingErrorAction newAction) {
- super.implOnMalformedInput(newAction);
- }
-
- public void pubImplOnUnmappableCharacter(CodingErrorAction newAction) {
- super.implOnUnmappableCharacter(newAction);
- }
-
- public void pubImplReplaceWith(String newReplacement) {
- super.implReplaceWith(newReplacement);
- }
-
- public void pubImplReset() {
- super.implReset();
- }
-
- }
+ }
}
Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetDecoderTest.java?rev=644228&r1=644227&r2=644228&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetDecoderTest.java Thu Apr 3 02:03:50 2008
@@ -49,10 +49,10 @@
return null;
}
- ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
+ ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
ByteBuffer buffer = ByteBuffer.allocate(20);
buffer.put(new byte[] { (byte) 0xd8 });
- buffer.put(unibytes);
+ buffer.put(getByteBuffer());
buffer.flip();
return buffer;
}
Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java?rev=644228&r1=644227&r2=644228&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java Thu Apr 3 02:03:50 2008
@@ -43,7 +43,7 @@
super.tearDown();
}
- public void testCanEncodechar() throws CharacterCodingException {
+ public void testCanEncodechar() {
// normal case for utfCS
assertTrue(encoder.canEncode('\u0077'));
assertTrue(encoder.canEncode('\uc2a3'));
Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ISOCharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ISOCharsetDecoderTest.java?rev=644228&r1=644227&r2=644228&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ISOCharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ISOCharsetDecoderTest.java Thu Apr 3 02:03:50 2008
@@ -49,7 +49,7 @@
}
- ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
+ ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
// TODO how malform
return null;
}
Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16BECharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16BECharsetDecoderTest.java?rev=644228&r1=644227&r2=644228&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16BECharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16BECharsetDecoderTest.java Thu Apr 3 02:03:50 2008
@@ -25,50 +25,48 @@
*/
public class UTF16BECharsetDecoderTest extends CharsetDecoderTest {
- protected void setUp() throws Exception {
- cs = Charset.forName("utf-16be");
- unibytes = new byte[] { 0, 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101,
- 0, 114 };
- super.setUp();
- }
-
- /*
- * @see CharsetDecoderTest#tearDown()
- */
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
- // FIXME: give up this tests
- // public void testDefaultCharsPerByte() {
- // // assertEquals(1, decoder.averageCharsPerByte());
- // // assertEquals(1, decoder.maxCharsPerByte());
- // assertEquals(decoder.averageCharsPerByte(), 0.5, 0.001);
- // assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
- // }
-
- ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
- // no unmap byte buffer
- return null;
- }
-
- ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
- // FIXME: different here, RI can parse 0xd8d8
- // ByteBuffer buffer = ByteBuffer.allocate(100);
- // buffer.put((byte)0xd8);
- // buffer.put((byte)0xd8);
- // buffer.put(unibytes);
- // buffer.flip();
- // return buffer;
- return null;
- }
-
- ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
- return null;
- }
-
- byte[] getUnibytes() {
- return new byte[] { 0, 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0,
- 114 };
- }
+ protected void setUp() throws Exception {
+ cs = Charset.forName("utf-16be");
+ super.setUp();
+ }
+
+ /*
+ * @see CharsetDecoderTest#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ // FIXME: give up this tests
+ // public void testDefaultCharsPerByte() {
+ // // assertEquals(1, decoder.averageCharsPerByte());
+ // // assertEquals(1, decoder.maxCharsPerByte());
+ // assertEquals(decoder.averageCharsPerByte(), 0.5, 0.001);
+ // assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
+ // }
+
+ ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
+ // no unmap byte buffer
+ return null;
+ }
+
+ ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
+ // FIXME: different here, RI can parse 0xd8d8
+ // ByteBuffer buffer = ByteBuffer.allocate(100);
+ // buffer.put((byte)0xd8);
+ // buffer.put((byte)0xd8);
+ // buffer.put(unibytes);
+ // buffer.flip();
+ // return buffer;
+ return null;
+ }
+
+ ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
+ return null;
+ }
+
+ protected ByteBuffer getByteBuffer() {
+ return ByteBuffer.wrap(new byte[] { 0, 32, 0, 98, 0, 117, 0, 102, 0,
+ 102, 0, 101, 0, 114 });
+ }
}
Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16CharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16CharsetDecoderTest.java?rev=644228&r1=644227&r2=644228&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16CharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16CharsetDecoderTest.java Thu Apr 3 02:03:50 2008
@@ -29,124 +29,133 @@
*/
public class UTF16CharsetDecoderTest extends CharsetDecoderTest {
- boolean bigEndian = true;
+ boolean bigEndian = true;
- protected void setUp() throws Exception {
- cs = Charset.forName("utf-16");
- unibytes = new byte[] { 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0,
- 114, 0 };
- bom = "\ufeff";
-
- // unibytes = new byte[] {-1, -2, 0, 32, 0, 98, 0, 117, 0, 102, 0, 102,
- // 0, 101, 0, 114};
- super.setUp();
- }
-
- /*
- * @see CharsetDecoderTest#tearDown()
- */
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
- byte[] getUnibytes() {
- // FIXME: different here
- // if don't specified BOM
- // ICU default is LE
- // JDK default is BE
-
- // maybe start with 0xFEFF, which means big endian
- // 0xFFFE, which means little endian
- if (bigEndian) {
- return new byte[] { -1, -2, 32, 0, 98, 0, 117, 0, 102, 0, 102, 0,
- 101, 0, 114, 0 };
- } else {
- unibytes = new byte[] { 0, 32, 0, 98, 0, 117, 0, 102, 0, 102, 0,
- 101, 0, 114 };
- return new byte[] { -2, -1, 0, 32, 0, 98, 0, 117, 0, 102, 0, 102,
- 0, 101, 0, 114 };
- }
- }
-
- 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 testLittleEndian() throws CharacterCodingException,
- UnsupportedEncodingException {
- bigEndian = false;
- implTestDecodeByteBufferCharBufferboolean();
- decoder.reset();
- implTestDecodeByteBuffer();
- bigEndian = true;
- }
-
- // FIXME: give up this tests
- // public void testDefaultCharsPerByte() {
- // // assertEquals(1, decoder.averageCharsPerByte());
- // // assertEquals(1, decoder.maxCharsPerByte());
- // assertEquals(decoder.averageCharsPerByte(), 0.5, 0.001);
- // assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
- // }
-
- ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
- return null;
- }
-
- ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
- return null;
- // FIXME: different here, RI can parse 0xd8d8
- // ByteBuffer buffer = ByteBuffer.allocate(100);
- // buffer.put((byte) -1);
- // buffer.put((byte) -2);
- // buffer.put((byte) 0xdc);
- // buffer.put((byte) 0xdc);
- // buffer.put(unibytes);
- // buffer.flip();
- // return buffer;
- }
-
- ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
- return null;
- }
+ protected void setUp() throws Exception {
+ cs = Charset.forName("utf-16");
+ bom = "\ufeff";
+ super.setUp();
+ }
+
+ /*
+ * @see CharsetDecoderTest#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ protected ByteBuffer getByteBuffer() {
+ // FIXME: different here
+ // if don't specified BOM
+ // ICU default is LE
+ // JDK default is BE
+
+ // maybe start with 0xFEFF, which means big endian
+ // 0xFFFE, which means little endian
+ byte[] b = (bigEndian) ? new byte[] { -1, -2, 32, 0, 98, 0, 117, 0,
+ 102, 0, 102, 0, 101, 0, 114, 0 } : new byte[] { -2, -1, 0, 32,
+ 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0, 114 };
+ return ByteBuffer.wrap(b);
+ }
+
+ protected ByteBuffer getHeadlessByteBuffer() {
+ ByteBuffer b = getByteBuffer();
+ b.position(2);
+ byte[] bytes = new byte[b.remaining()];
+ b.get(bytes);
+ 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;
+ implTestDecodeByteBufferCharBuffer(getByteBuffer());
+ bigEndian = true;
+ }
+
+ public void testLittleEndianReadOnlyByteBufferCharBuffer()
+ throws CharacterCodingException, UnsupportedEncodingException {
+ bigEndian = false;
+ implTestDecodeByteBufferCharBuffer(getByteBuffer().asReadOnlyBuffer());
+ bigEndian = true;
+ }
+
+ public void testLittleEndian() throws CharacterCodingException,
+ UnsupportedEncodingException {
+ bigEndian = false;
+ implTestDecodeByteBuffer();
+ bigEndian = true;
+ }
+
+ // FIXME: give up this tests
+ // public void testDefaultCharsPerByte() {
+ // // assertEquals(1, decoder.averageCharsPerByte());
+ // // assertEquals(1, decoder.maxCharsPerByte());
+ // assertEquals(decoder.averageCharsPerByte(), 0.5, 0.001);
+ // assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
+ // }
+
+ ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
+ return null;
+ }
+
+ ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
+ return null;
+ // FIXME: different here, RI can parse 0xd8d8
+ // ByteBuffer buffer = ByteBuffer.allocate(100);
+ // buffer.put((byte) -1);
+ // buffer.put((byte) -2);
+ // buffer.put((byte) 0xdc);
+ // buffer.put((byte) 0xdc);
+ // buffer.put(unibytes);
+ // buffer.flip();
+ // return buffer;
+ }
+
+ ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
+ return null;
+ }
}
Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16LECharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16LECharsetDecoderTest.java?rev=644228&r1=644227&r2=644228&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16LECharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16LECharsetDecoderTest.java Thu Apr 3 02:03:50 2008
@@ -25,50 +25,48 @@
*/
public class UTF16LECharsetDecoderTest extends CharsetDecoderTest {
- protected void setUp() throws Exception {
- cs = Charset.forName("utf-16le");
- unibytes = new byte[] { 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0,
- 114, 0 };
- super.setUp();
- }
-
- /*
- * @see CharsetDecoderTest#tearDown()
- */
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
- // // FIXME: give up this tests
- // public void testDefaultCharsPerByte(){
- // // assertEquals(1, decoder.averageCharsPerByte());
- // // assertEquals(1, decoder.maxCharsPerByte());
- // assertEquals(decoder.averageCharsPerByte(), 0.5, 0.001);
- // assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
- // }
-
- ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
- // no unmap byte buffer
- return null;
- }
-
- ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
- // FIXME: different here, JDK can parse 0xd8d8
- // ByteBuffer buffer = ByteBuffer.allocate(100);
- // buffer.put((byte)0xd8);
- // buffer.put((byte)0xd8);
- // buffer.put(unibytes);
- // buffer.flip();
- // return buffer;
- return null;
- }
-
- ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
- return null;
- }
-
- byte[] getUnibytes() {
- return new byte[] { 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0, 114,
- 0 };
- }
+ protected void setUp() throws Exception {
+ cs = Charset.forName("utf-16le");
+ super.setUp();
+ }
+
+ /*
+ * @see CharsetDecoderTest#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ // // FIXME: give up this tests
+ // public void testDefaultCharsPerByte(){
+ // // assertEquals(1, decoder.averageCharsPerByte());
+ // // assertEquals(1, decoder.maxCharsPerByte());
+ // assertEquals(decoder.averageCharsPerByte(), 0.5, 0.001);
+ // assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
+ // }
+
+ ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
+ // no unmap byte buffer
+ return null;
+ }
+
+ ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
+ // FIXME: different here, JDK can parse 0xd8d8
+ // ByteBuffer buffer = ByteBuffer.allocate(100);
+ // buffer.put((byte)0xd8);
+ // buffer.put((byte)0xd8);
+ // buffer.put(unibytes);
+ // buffer.flip();
+ // return buffer;
+ return null;
+ }
+
+ ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
+ return null;
+ }
+
+ protected ByteBuffer getByteBuffer() {
+ return ByteBuffer.wrap(new byte[] { 32, 0, 98, 0, 117, 0, 102, 0, 102,
+ 0, 101, 0, 114, 0 });
+ }
}
Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTFCharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTFCharsetDecoderTest.java?rev=644228&r1=644227&r2=644228&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTFCharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTFCharsetDecoderTest.java Thu Apr 3 02:03:50 2008
@@ -25,39 +25,52 @@
*/
public class UTFCharsetDecoderTest extends CharsetDecoderTest {
- protected void setUp() throws Exception {
- cs = Charset.forName("utf-8");
- super.setUp();
- }
-
- /*
- * @see CharsetDecoderTest#tearDown()
- */
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
- // FIXME: give up this tests
- // public void testDefaultCharsPerByte(){
- // assertEquals(decoder.averageCharsPerByte(), 0.333, 0.001);
- // assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
- // // assertEquals(1, decoder.averageCharsPerByte());
- // // assertEquals(1, decoder.maxCharsPerByte());
- // }
-
- ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
- return null;
- }
-
- ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
- ByteBuffer buffer = ByteBuffer.allocate(20);
- buffer.put((byte) 0xd8);
- buffer.put(unibytes);
- buffer.flip();
- return buffer;
- }
-
- ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
- return null;
- }
+ protected void setUp() throws Exception {
+ cs = Charset.forName("utf-8");
+ super.setUp();
+ }
+
+ /*
+ * @see CharsetDecoderTest#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ // FIXME: give up this tests
+ // public void testDefaultCharsPerByte(){
+ // assertEquals(decoder.averageCharsPerByte(), 0.333, 0.001);
+ // assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
+ // // assertEquals(1, decoder.averageCharsPerByte());
+ // // assertEquals(1, decoder.maxCharsPerByte());
+ // }
+
+ ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
+ return null;
+ }
+
+ ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
+ ByteBuffer buffer = ByteBuffer.allocate(getByteBuffer().remaining() + 1);
+ buffer.put((byte) 0xd8);
+ buffer.put(getByteBuffer());
+ buffer.flip();
+ return buffer;
+ }
+
+ ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
+ return null;
+ }
+
+ protected String getString() {
+ return " buffer \u041c\u0430\u0441\u044e\u043b\u044f \u611b";
+ }
+
+ protected ByteBuffer getByteBuffer() {
+ return ByteBuffer.wrap(new byte[] { 32, 98, 117, 102, 102, 101, 114,
+ 32, (byte) 0xd0, (byte) 0x9c, (byte) 0xd0, (byte) 0xb0,
+ (byte) 0xd1, (byte) 0x81, (byte) 0xd1, (byte) 0x8e,
+ (byte) 0xd0, (byte) 0xbb, (byte) 0xd1, (byte) 0x8f, 32,
+ (byte) 0xe6, (byte) 0x84, (byte) 0x9b });
+ }
+
}