harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r386058 [41/49] - in /incubator/harmony/enhanced/classlib/trunk: make/ modules/archive/make/common/ modules/archive/src/test/java/tests/ modules/archive/src/test/java/tests/api/ modules/archive/src/test/java/tests/api/java/ modules/archive/...
Date Wed, 15 Mar 2006 11:47:39 GMT
Added: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharacterCodingExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharacterCodingExceptionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharacterCodingExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharacterCodingExceptionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,56 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.nio.charset;
+
+import java.io.IOException;
+import java.nio.charset.CharacterCodingException;
+
+import junit.framework.TestCase;
+import tests.util.SerializationTester;
+
+/**
+ * Test CharacterCodingException
+ */
+public class CharacterCodingExceptionTest extends TestCase {
+
+	public void testConstructor() {
+		CharacterCodingException ex = new CharacterCodingException();
+		assertTrue(ex instanceof IOException);
+		assertNull(ex.getCause());
+		assertNull(ex.getMessage());
+	}
+
+	/*
+	 * Test serialization/deserialization.
+	 */
+	public void testSerialization() throws Exception {
+		CharacterCodingException ex = new CharacterCodingException();
+
+		CharacterCodingException deEx = (CharacterCodingException) SerializationTester
+				.getDeserilizedObject(ex);
+	}
+
+	/*
+	 * Test serialization/deserialization.
+	 */
+	public void testSerializationCompatibility() throws Exception {
+		CharacterCodingException ex = new CharacterCodingException();
+
+		CharacterCodingException deEx = (CharacterCodingException) SerializationTester
+				.readObject(ex,
+						"tests/api/java/nio/charset/CharacterCodingException.ser");
+	}
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,782 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.nio.charset;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CoderResult;
+import java.nio.charset.CodingErrorAction;
+import java.nio.charset.MalformedInputException;
+import java.nio.charset.UnmappableCharacterException;
+
+import junit.framework.TestCase;
+
+/**
+ * API unit test for java.nio.CharsetDecoder
+ */
+public class CharsetDecoderTest extends TestCase {
+
+	static final String unistr = " buffer";// \u8000\u8001\u00a5\u3000\r\n";
+
+	byte[] unibytes = new byte[] { 32, 98, 117, 102, 102, 101, 114 };
+
+	protected static final int MAX_BYTES = 3;
+
+	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);
+		assertTrue(ec.averageCharsPerByte() == 1);
+		assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
+
+		/*
+		 * ------------------------ Exceptional cases -------------------------
+		 */
+		// Normal case: null charset
+		ec = new MockCharsetDecoder(null, 1, MAX_BYTES);
+		assertNull(ec.charset());
+		assertTrue(ec.averageCharsPerByte() == 1);
+		assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
+
+		ec = new MockCharsetDecoder(new CharsetEncoderTest.MockCharset("mock",
+				new String[0]), 1, MAX_BYTES);
+
+		// 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 + "malform".length());
+				// set the error length
+				return CoderResult.malformedForLength("malform".length());
+			} else if (result.startsWith("unmap")) {
+				// reset the cursor to the error position
+				in.position(inPosition + "unmap".length());
+				// 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();
+		}
+
+	}
+
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,1107 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.nio.charset;
+
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CharsetEncoder;
+import java.nio.charset.CoderResult;
+import java.nio.charset.CodingErrorAction;
+import java.nio.charset.MalformedInputException;
+import java.nio.charset.UnmappableCharacterException;
+import java.nio.charset.UnsupportedCharsetException;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+/**
+ * API unit test for java.nio.charset.CharsetEncoder
+ */
+public class CharsetEncoderTest extends TestCase {
+
+	static final int MAX_BYTES = 3;
+
+	static final float AVER_BYTES = 0.5f;
+
+	// charset for mock class
+	private static final Charset MOCKCS = new MockCharset("mock", new String[0]);
+
+	Charset cs = MOCKCS;
+
+	// default encoder
+	CharsetEncoder encoder;
+
+	// default for Charset abstract class
+	byte[] defaultReplacement = new byte[] { 63 };
+
+	// specific for Charset implementation subclass
+	byte[] specifiedReplacement = new byte[] { 26 };
+
+	static final String unistr = " buffer";// \u8000\u8001\u00a5\u3000\r\n";
+
+	byte[] unibytes = new byte[] { 32, 98, 117, 102, 102, 101, 114 };
+
+	byte[] unibytesWithRep = null;
+
+	byte[] surrogate = new byte[0];
+
+	protected void setUp() throws Exception {
+		super.setUp();
+		encoder = cs.newEncoder();
+		if (null == unibytesWithRep) {
+			byte[] replacement = encoder.replacement();
+			unibytesWithRep = new byte[replacement.length + unibytes.length];
+			System.arraycopy(replacement, 0, unibytesWithRep, 0,
+					replacement.length);
+			System.arraycopy(unibytes, 0, unibytesWithRep, replacement.length,
+					unibytes.length);
+		}
+	}
+
+	/*
+	 * @see TestCase#tearDown()
+	 */
+	protected void tearDown() throws Exception {
+		super.tearDown();
+	}
+
+	public void testSpecificDefaultValue() {
+		assertTrue(encoder.averageBytesPerChar() == AVER_BYTES);
+		assertTrue(encoder.maxBytesPerChar() == MAX_BYTES);
+	}
+
+	public void testDefaultValue() {
+		assertEquals(CodingErrorAction.REPORT, encoder.malformedInputAction());
+		assertEquals(CodingErrorAction.REPORT, encoder
+				.unmappableCharacterAction());
+		assertSame(encoder, encoder.onMalformedInput(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));
+		}
+
+	}
+
+	/*
+	 * Class under test for constructor CharsetEncoder(Charset, float, float)
+	 */
+	public void testCharsetEncoderCharsetfloatfloat() {
+		// default value
+		encoder = new MockCharsetEncoder(cs, (float) AVER_BYTES, MAX_BYTES);
+		assertSame(encoder.charset(), cs);
+		assertTrue(encoder.averageBytesPerChar() == AVER_BYTES);
+		assertTrue(encoder.maxBytesPerChar() == MAX_BYTES);
+		assertEquals(CodingErrorAction.REPORT, encoder.malformedInputAction());
+		assertEquals(CodingErrorAction.REPORT, encoder
+				.unmappableCharacterAction());
+		assertEquals(new String(encoder.replacement()), new String(
+				defaultReplacement));
+		assertSame(encoder, encoder.onMalformedInput(CodingErrorAction.IGNORE));
+		assertSame(encoder, encoder
+				.onUnmappableCharacter(CodingErrorAction.IGNORE));
+
+		// normal case
+		CharsetEncoder ec = new MockCharsetEncoder(cs, 1, MAX_BYTES);
+		assertSame(ec.charset(), cs);
+		assertTrue(ec.averageBytesPerChar() == 1);
+		assertTrue(ec.maxBytesPerChar() == MAX_BYTES);
+
+		/*
+		 * ------------------------ Exceptional cases -------------------------
+		 */
+		// NullPointerException: null charset
+		try {
+			ec = new MockCharsetEncoder(null, 1, MAX_BYTES);
+			fail("should throw null pointer exception");
+		} catch (NullPointerException e) {
+		}
+
+		ec = new MockCharsetEncoder(new MockCharset("mock", new String[0]), 1,
+				MAX_BYTES);
+
+		// OK: average length less than max length
+		ec = new MockCharsetEncoder(cs, MAX_BYTES, 1);
+		assertTrue(ec.averageBytesPerChar() == MAX_BYTES);
+		assertTrue(ec.maxBytesPerChar() == 1);
+
+		// Illegal Argument: zero length
+		try {
+			ec = new MockCharsetEncoder(cs, 0, MAX_BYTES);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			ec = new MockCharsetEncoder(cs, 1, 0);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+
+		// Illegal Argument: negative length
+		try {
+			ec = new MockCharsetEncoder(cs, -1, MAX_BYTES);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			ec = new MockCharsetEncoder(cs, 1, -1);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+	}
+
+	/*
+	 * Class under test for constructor CharsetEncoder(Charset, float, float,
+	 * byte[])
+	 */
+	public void testCharsetEncoderCharsetfloatfloatbyteArray() {
+		byte[] ba = getLegalByteArray();
+		// normal case
+		CharsetEncoder ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, ba);
+		assertSame(ec.charset(), cs);
+		assertTrue(ec.averageBytesPerChar() == 1);
+		assertTrue(ec.maxBytesPerChar() == MAX_BYTES);
+		assertSame(ba, ec.replacement());
+
+		/*
+		 * ------------------------ Exceptional cases -------------------------
+		 */
+		// NullPointerException: null charset
+		try {
+			ec = new MockCharsetEncoder(null, 1, MAX_BYTES, ba);
+			fail("should throw null pointer exception");
+		} catch (NullPointerException e) {
+		}
+
+		// Illegal Argument: null byte array
+		try {
+			ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, null);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+		// Illegal Argument: empty byte array
+		try {
+			ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, new byte[0]);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+		// Illegal Argument: byte array is longer than max length
+		try {
+			ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, new byte[] { 1, 2,
+					MAX_BYTES, 4 });
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+
+		// OK: average length less than max length
+		ec = new MockCharsetEncoder(cs, MAX_BYTES, ba.length, ba);
+		assertTrue(ec.averageBytesPerChar() == MAX_BYTES);
+		assertTrue(ec.maxBytesPerChar() == ba.length);
+
+		// Illegal Argument: zero length
+		try {
+			ec = new MockCharsetEncoder(cs, 0, MAX_BYTES, ba);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			ec = new MockCharsetEncoder(cs, 1, 0, ba);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+
+		// Illegal Argument: negative length
+		try {
+			ec = new MockCharsetEncoder(cs, -1, MAX_BYTES, ba);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			ec = new MockCharsetEncoder(cs, 1, -1, ba);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+	}
+
+	/*
+	 * Class under test for boolean canEncode(char)
+	 */
+	public void testCanEncodechar() throws CharacterCodingException {
+		// for non-mapped char
+		assertTrue(encoder.canEncode('\uc2c0'));
+		// surrogate char for unicode
+		// 1st byte: d800-dbff
+		// 2nd byte: dc00-dfff
+		assertTrue(encoder.canEncode('\ud800'));
+		// valid surrogate pair
+		assertTrue(encoder.canEncode('\udc00'));
+	}
+
+	/*-----------------------------------------
+	 * Class under test for illegal state case
+	 * methods which can change internal states are two encode, flush, two canEncode, reset
+	 * -----------------------------------------
+	 */
+
+	// Normal case: just after reset, and it also means reset can be done
+	// anywhere
+	public void testResetIllegalState() throws CharacterCodingException {
+		assertSame(encoder, encoder.reset());
+		encoder.canEncode('\ud901');
+		assertSame(encoder, encoder.reset());
+		encoder.canEncode("\ud901\udc00");
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("aaa"));
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("aaa"), ByteBuffer.allocate(3), false);
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("aaa"), ByteBuffer.allocate(3), true);
+		assertSame(encoder, encoder.reset());
+	}
+
+	public void testFlushIllegalState() throws CharacterCodingException {
+		CharBuffer in = CharBuffer.wrap("aaa");
+		ByteBuffer out = ByteBuffer.allocate(5);
+
+		// Normal case: after encode with endOfInput is true
+		assertSame(encoder, encoder.reset());
+		encoder.encode(in, out, true);
+		out.rewind();
+		CoderResult result = encoder.flush(out);
+
+		// Illegal state: flush twice
+		try {
+			encoder.flush(out);
+			fail("should throw IllegalStateException");
+		} catch (IllegalStateException e) {
+		}
+
+		// Illegal state: flush after encode with endOfInput is false
+		assertSame(encoder, encoder.reset());
+		encoder.encode(in, out, false);
+		try {
+			encoder.flush(out);
+			fail("should throw IllegalStateException");
+		} catch (IllegalStateException e) {
+		}
+	}
+
+	// test illegal states for encode facade
+	public void testEncodeFacadeIllegalState() throws CharacterCodingException {
+		// encode facade can be execute in anywhere
+		CharBuffer in = CharBuffer.wrap("aaa");
+		// Normal case: just created
+		encoder.encode(in);
+		in.rewind();
+
+		// Normal case: just after encode facade
+		encoder.encode(in);
+		in.rewind();
+
+		// Normal case: just after canEncode
+		assertSame(encoder, encoder.reset());
+		encoder.canEncode("\ud902\udc00");
+		encoder.encode(in);
+		in.rewind();
+		assertSame(encoder, encoder.reset());
+		encoder.canEncode('\ud902');
+		encoder.encode(in);
+		in.rewind();
+
+		// Normal case: just after encode with that endOfInput is true
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState2"),
+				ByteBuffer.allocate(30), true);
+		encoder.encode(in);
+		in.rewind();
+
+		// Normal case:just after encode with that endOfInput is false
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState3"),
+				ByteBuffer.allocate(30), false);
+		encoder.encode(in);
+		in.rewind();
+
+		// Normal case: just after flush
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState4"),
+				ByteBuffer.allocate(30), true);
+		encoder.flush(ByteBuffer.allocate(10));
+		encoder.encode(in);
+		in.rewind();
+	}
+
+	// test illegal states for two encode method with endOfInput is true
+	public void testEncodeTrueIllegalState() throws CharacterCodingException {
+		CharBuffer in = CharBuffer.wrap("aaa");
+		ByteBuffer out = ByteBuffer.allocate(5);
+		// Normal case: just created
+		encoder.encode(in, out, true);
+		in.rewind();
+		out.rewind();
+
+		in.rewind();
+		out.rewind();
+
+		// Normal case: just after encode with that endOfInput is true
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState2"),
+				ByteBuffer.allocate(30), true);
+		encoder.encode(in, out, true);
+		in.rewind();
+		out.rewind();
+
+		// Normal case:just after encode with that endOfInput is false
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState3"),
+				ByteBuffer.allocate(30), false);
+		encoder.encode(in, out, true);
+		in.rewind();
+		out.rewind();
+
+		// Illegal state: just after flush
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState4"),
+				ByteBuffer.allocate(30), true);
+		encoder.flush(ByteBuffer.allocate(10));
+		try {
+			encoder.encode(in, out, true);
+			fail("should illegal state");
+		} catch (IllegalStateException e) {
+		}
+
+		// Normal case: after canEncode
+		assertSame(encoder, encoder.reset());
+		encoder.canEncode("\ud906\udc00");
+		encoder.encode(in, out, true);
+		in.rewind();
+		out.rewind();
+		assertSame(encoder, encoder.reset());
+		encoder.canEncode('\ud905');
+		encoder.encode(in, out, true);
+	}
+
+	// test illegal states for two encode method with endOfInput is false
+	public void testEncodeFalseIllegalState() throws CharacterCodingException {
+		CharBuffer in = CharBuffer.wrap("aaa");
+		ByteBuffer out = ByteBuffer.allocate(5);
+		// Normal case: just created
+		encoder.encode(in, out, false);
+		in.rewind();
+		out.rewind();
+
+		// Illegal state: just after encode facade
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState1"));
+		try {
+			encoder.encode(in, out, false);
+			fail("should illegal state");
+		} catch (IllegalStateException e) {
+		}
+
+		// Illegal state: just after encode with that endOfInput is true
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState2"),
+				ByteBuffer.allocate(30), true);
+		try {
+			encoder.encode(in, out, false);
+			fail("should illegal state");
+		} catch (IllegalStateException e) {
+		}
+
+		// Normal case:just after encode with that endOfInput is false
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState3"),
+				ByteBuffer.allocate(30), false);
+		encoder.encode(in, out, false);
+		in.rewind();
+		out.rewind();
+
+		// Illegal state: just after flush
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState4"),
+				ByteBuffer.allocate(30), true);
+		encoder.flush(ByteBuffer.allocate(10));
+		try {
+			encoder.encode(in, out, false);
+			fail("should illegal state");
+		} catch (IllegalStateException e) {
+		}
+
+		// Normal case: after canEncode
+		assertSame(encoder, encoder.reset());
+		encoder.canEncode("\ud906\udc00");
+		encoder.encode(in, out, false);
+		in.rewind();
+		out.rewind();
+		assertSame(encoder, encoder.reset());
+		encoder.canEncode('\ud905');
+		encoder.encode(in, out, false);
+	}
+
+	// test illegal states for two canEncode methods
+	public void testCanEncodeIllegalState() throws CharacterCodingException {
+		// Normal case: just created
+		encoder.canEncode("\ud900\udc00");
+		encoder.canEncode('\ud900');
+
+		// Illegal state: just after encode with that endOfInput is true
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState2"),
+				ByteBuffer.allocate(30), true);
+		try {
+			encoder.canEncode("\ud903\udc00");
+			fail("should throw illegal state exception");
+		} catch (IllegalStateException e) {
+		}
+
+		// Illegal state:just after encode with that endOfInput is false
+		assertSame(encoder, encoder.reset());
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState3"),
+				ByteBuffer.allocate(30), false);
+		try {
+			encoder.canEncode("\ud904\udc00");
+			fail("should throw illegal state exception");
+		} catch (IllegalStateException e) {
+		}
+
+		// Normal case: just after flush
+		encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState4"),
+				ByteBuffer.allocate(30), true);
+		encoder.flush(ByteBuffer.allocate(10));
+		encoder.canEncode("\ud905\udc00");
+		encoder.canEncode('\ud906');
+
+		// Normal case: after reset again
+		assertSame(encoder, encoder.reset());
+		encoder.canEncode("\ud906\udc00");
+		encoder.canEncode('\ud905');
+	}
+
+	/*
+	 * --------------------------------- illegal state test end
+	 * ---------------------------------
+	 */
+
+	/*
+	 * Class under test for boolean canEncode(CharSequence)
+	 */
+	public void testCanEncodeCharSequence() {
+		// for non-mapped char
+		assertTrue(encoder.canEncode("\uc2c0"));
+		// 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"));
+	}
+
+	/*
+	 * Class under test for Charset charset()
+	 */
+	public void testCharset() {
+		try {
+			encoder = new MockCharsetEncoder(Charset.forName("gbk"), 1,
+					MAX_BYTES);
+			// assertSame(encoder.charset(), Charset.forName("gbk"));
+		} catch (UnsupportedCharsetException e) {
+			System.err
+					.println("Don't support GBK encoding, ignore current test");
+		}
+	}
+
+	/*
+	 * Class under test for ByteBuffer encode(CharBuffer)
+	 */
+	public void testEncodeCharBuffer() throws CharacterCodingException {
+		// Null pointer
+		try {
+			encoder.encode(null);
+			fail("should throw null pointer exception");
+		} catch (NullPointerException e) {
+		}
+
+		// empty input buffer
+		ByteBuffer out = encoder.encode(CharBuffer.wrap(""));
+		assertEquals(out.position(), 0);
+		assertByteArray(out, new byte[0]);
+		// assertByteArray(out, surrogate);
+
+		// normal case
+		out = encoder.encode(CharBuffer.wrap(unistr));
+		assertEquals(out.position(), 0);
+		assertByteArray(out, addSurrogate(unibytes));
+	}
+
+	private byte[] addSurrogate(byte[] expected) {
+		if (surrogate.length > 0) {
+			byte[] temp = new byte[surrogate.length + expected.length];
+			System.arraycopy(surrogate, 0, temp, 0, surrogate.length);
+			System.arraycopy(expected, 0, temp, surrogate.length,
+					expected.length);
+			expected = temp;
+		}
+		return expected;
+	}
+
+	/**
+	 * @return
+	 */
+	protected byte[] getEmptyByteArray() {
+		return new byte[0];
+	}
+
+	CharBuffer getMalformedCharBuffer() {
+		return CharBuffer.wrap("malform buffer");
+	}
+
+	CharBuffer getUnmapCharBuffer() {
+		return CharBuffer.wrap("unmap buffer");
+	}
+
+	CharBuffer getExceptionCharBuffer() {
+		return CharBuffer.wrap("runtime buffer");
+	}
+
+	public void testEncodeCharBufferException() throws CharacterCodingException {
+		ByteBuffer out;
+		CharBuffer in;
+		// MalformedException:
+		in = getMalformedCharBuffer();
+		encoder.onMalformedInput(CodingErrorAction.REPORT);
+		encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+		if (in != null) {
+			try {
+				encoder.encode(in);
+				fail("should throw MalformedInputException");
+			} catch (MalformedInputException e) {
+			}
+
+			encoder.reset();
+			in.rewind();
+			encoder.onMalformedInput(CodingErrorAction.IGNORE);
+			out = encoder.encode(in);
+			assertByteArray(out, addSurrogate(unibytes));
+
+			encoder.reset();
+			in.rewind();
+			encoder.onMalformedInput(CodingErrorAction.REPLACE);
+			out = encoder.encode(in);
+			assertByteArray(out, addSurrogate(unibytesWithRep));
+		}
+
+		// Unmapped Exception:
+		in = getUnmapCharBuffer();
+		encoder.onMalformedInput(CodingErrorAction.REPORT);
+		encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+		if (in != null) {
+			encoder.reset();
+			try {
+				encoder.encode(in);
+				fail("should throw UnmappableCharacterException");
+			} catch (UnmappableCharacterException e) {
+			}
+
+			encoder.reset();
+			in.rewind();
+			encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+			out = encoder.encode(in);
+			assertByteArray(out, unibytes);
+
+			encoder.reset();
+			in.rewind();
+			encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
+			out = encoder.encode(in);
+			assertByteArray(out, unibytesWithRep);
+		}
+
+		// RuntimeException
+		try {
+			encoder.encode(getExceptionCharBuffer());
+			fail("should throw runtime exception");
+		} catch (RuntimeException e) {
+		}
+	}
+
+	/*
+	 * utility method, extract given bytebuffer to a string and compare with
+	 * give string
+	 */
+	void assertByteArray(ByteBuffer out, byte[] expected) {
+		out = out.duplicate();
+		if (out.position() != 0) {
+			out.flip();
+		}
+		byte[] ba = new byte[out.limit() - out.position()];
+		out.get(ba);
+		// byte[] ba = out.array();
+		assertTrue(Arrays.equals(ba, expected));
+	}
+
+	/*
+	 * Class under test for CoderResult encode(CharBuffer, ByteBuffer, boolean)
+	 */
+	public void testEncodeCharBufferByteBufferboolean()
+			throws CharacterCodingException {
+		ByteBuffer out = ByteBuffer.allocate(200);
+		CharBuffer in = CharBuffer.wrap(unistr);
+		// Null pointer
+		try {
+			encoder.encode(null, out, true);
+			fail("should throw null pointer exception");
+		} catch (NullPointerException e) {
+		}
+		try {
+			encoder.encode(in, null, true);
+			fail("should throw null pointer exception");
+		} catch (NullPointerException e) {
+		}
+
+		// normal case, one complete operation
+		assertSame(encoder, encoder.reset());
+		in.rewind();
+		out.rewind();
+		assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, true));
+		assertEquals(out.limit(), 200);
+		assertTrue(out.position() > 0);
+		assertTrue(out.remaining() > 0);
+		assertEquals(out.capacity(), 200);
+		assertByteArray(out, addSurrogate(unibytes));
+		in.rewind();
+
+		encoder.flush(out);
+
+		// normal case, one complete operation, but call twice, first time set
+		// endOfInput to false
+		assertSame(encoder, encoder.reset());
+		in.rewind();
+		out = ByteBuffer.allocate(200);
+		assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, false));
+		assertEquals(out.limit(), 200);
+		assertTrue(out.position() > 0);
+		assertTrue(out.remaining() > 0);
+		assertEquals(out.capacity(), 200);
+		assertByteArray(out, addSurrogate(unibytes));
+
+		in.rewind();
+		assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, false));
+		in.rewind();
+		assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, true));
+		assertEquals(out.limit(), 200);
+		assertTrue(out.position() > 0);
+		assertTrue(out.remaining() > 0);
+		assertEquals(out.capacity(), 200);
+
+		assertByteArray(out, addSurrogate(duplicateByteArray(unibytes, 3)));
+
+		// overflow
+		out = ByteBuffer.allocate(4);
+		assertSame(encoder, encoder.reset());
+		in.rewind();
+		out.rewind();
+		assertSame(CoderResult.OVERFLOW, encoder.encode(in, out, true));
+		assertEquals(out.limit(), 4);
+		assertEquals(out.position(), 4);
+		assertEquals(out.remaining(), 0);
+		assertEquals(out.capacity(), 4);
+		ByteBuffer temp = ByteBuffer.allocate(200);
+		out.flip();
+		temp.put(out);
+		out = temp;
+		assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, true));
+		assertEquals(out.limit(), 200);
+		assertTrue(out.position() > 0);
+		assertTrue(out.remaining() > 0);
+		assertEquals(out.capacity(), 200);
+		assertByteArray(out, addSurrogate(unibytes));
+
+		assertSame(encoder, encoder.reset());
+		in.rewind();
+		out = ByteBuffer.allocate(4);
+		assertSame(CoderResult.OVERFLOW, encoder.encode(in, out, false));
+		assertEquals(out.limit(), 4);
+		assertEquals(out.position(), 4);
+		assertEquals(out.remaining(), 0);
+		assertEquals(out.capacity(), 4);
+		temp = ByteBuffer.allocate(200);
+		out.flip();
+		temp.put(out);
+		out = temp;
+		assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, false));
+		assertEquals(out.limit(), 200);
+		assertTrue(out.position() > 0);
+		assertTrue(out.remaining() > 0);
+		assertEquals(out.capacity(), 200);
+		assertByteArray(out, addSurrogate(unibytes));
+	}
+
+	void printByteBuffer(ByteBuffer buffer) {
+		System.out.println("print buffer");
+		if (buffer.position() != 0) {
+			buffer.flip();
+		}
+		byte[] ba = buffer.array();
+		for (int i = 0; i < ba.length; i++) {
+			System.out.println(Integer.toHexString(ba[i]));
+		}
+	}
+
+	public void testEncodeCharBufferByteBufferbooleanExceptionFalse()
+			throws CharacterCodingException {
+		implTestEncodeCharBufferByteBufferbooleanException(false);
+	}
+
+	public void testEncodeCharBufferByteBufferbooleanExceptionTrue()
+			throws CharacterCodingException {
+		implTestEncodeCharBufferByteBufferbooleanException(true);
+	}
+
+	private byte[] duplicateByteArray(byte[] ba, int times) {
+		byte[] result = new byte[ba.length * times];
+		for (int i = 0; i < times; i++) {
+			System.arraycopy(ba, 0, result, i * ba.length, ba.length);
+		}
+		return result;
+	}
+
+	protected void implTestEncodeCharBufferByteBufferbooleanException(
+			boolean endOfInput) throws CharacterCodingException {
+		ByteBuffer out = ByteBuffer.allocate(100);
+
+		// MalformedException:
+		CharBuffer in = getMalformedCharBuffer();
+		encoder.onMalformedInput(CodingErrorAction.REPORT);
+		encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+		if (in != null) {
+			encoder.reset();
+			CoderResult r = encoder.encode(in, out, endOfInput);
+			assertTrue(r.isMalformed());
+
+			encoder.reset();
+			out.clear();
+			in.rewind();
+			encoder.onMalformedInput(CodingErrorAction.IGNORE);
+			assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out,
+					endOfInput));
+			assertCodingErrorAction(endOfInput, out, in, unibytes);
+
+			encoder.reset();
+			out.clear();
+			in.rewind();
+			encoder.onMalformedInput(CodingErrorAction.REPLACE);
+			assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out,
+					endOfInput));
+			assertCodingErrorAction(endOfInput, out, in, unibytesWithRep);
+		} else {
+			System.out.println("Cannot find malformed char buffer for "
+					+ cs.name());
+		}
+
+		// Unmapped Exception:
+		in = getUnmapCharBuffer();
+		encoder.onMalformedInput(CodingErrorAction.REPORT);
+		encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+		if (in != null) {
+			encoder.reset();
+			out.clear();
+			assertTrue(encoder.encode(in, out, endOfInput).isUnmappable());
+
+			encoder.reset();
+			out.clear();
+			in.rewind();
+			encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+			assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out,
+					endOfInput));
+			assertCodingErrorAction(endOfInput, out, in, unibytes);
+
+			encoder.reset();
+			out.clear();
+			in.rewind();
+			encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
+			assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out,
+					endOfInput));
+			assertCodingErrorAction(endOfInput, out, in, unibytesWithRep);
+		} else {
+			System.out.println("Cannot find unmapped char buffer for "
+					+ cs.name());
+		}
+
+		// RuntimeException
+		try {
+			encoder.encode(getExceptionCharBuffer());
+			fail("should throw runtime exception");
+		} catch (RuntimeException e) {
+		}
+	}
+
+	private void assertCodingErrorAction(boolean endOfInput, ByteBuffer out,
+			CharBuffer in, byte[] expect) {
+		if (endOfInput) {
+			assertByteArray(out, addSurrogate(expect));
+		} else {
+			in.rewind();
+			assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out,
+					endOfInput));
+			in.rewind();
+			assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, true));
+			assertByteArray(out, addSurrogate(duplicateByteArray(expect, 3)));
+		}
+	}
+
+	/*
+	 * Class under test for CoderResult flush(ByteBuffer)
+	 */
+	public void testFlush() throws CharacterCodingException {
+		ByteBuffer out = ByteBuffer.allocate(6);
+		CharBuffer in = CharBuffer.wrap("aaa");
+		assertEquals(in.remaining(), 3);
+
+		// by encode facade, so that internal state will be wrong
+		encoder.encode(CharBuffer.wrap("testFlush"), ByteBuffer.allocate(20),
+				true);
+		assertSame(CoderResult.UNDERFLOW, encoder
+				.flush(ByteBuffer.allocate(50)));
+	}
+
+	/*
+	 * test isLegalReplacement(byte[])
+	 */
+	public void testIsLegalReplacement() {
+		try {
+			encoder.isLegalReplacement(null);
+			fail("should throw null pointer exception");
+		} catch (NullPointerException e) {
+		}
+		assertTrue(encoder.isLegalReplacement(specifiedReplacement));
+
+		assertTrue(encoder.isLegalReplacement(new byte[200]));
+		byte[] ba = getIllegalByteArray();
+		if (ba != null) {
+			assertFalse(encoder.isLegalReplacement(ba));
+		}
+	}
+
+	public void testIsLegalReplacementEmptyArray() {
+		// 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() {
+		assertSame(CodingErrorAction.REPORT, encoder.malformedInputAction());
+		try {
+			encoder.onMalformedInput(null);
+			fail("should throw null pointer exception");
+		} catch (IllegalArgumentException e) {
+		}
+		encoder.onMalformedInput(CodingErrorAction.IGNORE);
+		assertSame(CodingErrorAction.IGNORE, encoder.malformedInputAction());
+	}
+
+	public void testOnUnmappableCharacter() {
+		assertSame(CodingErrorAction.REPORT, encoder
+				.unmappableCharacterAction());
+		try {
+			encoder.onUnmappableCharacter(null);
+			fail("should throw null pointer exception");
+		} catch (IllegalArgumentException e) {
+		}
+		encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+		assertSame(CodingErrorAction.IGNORE, encoder
+				.unmappableCharacterAction());
+	}
+
+	public void testMultiStepEncode() throws CharacterCodingException {
+		if (!cs.name().equals("mock")) {
+			encoder.onMalformedInput(CodingErrorAction.REPORT);
+			encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+			ByteBuffer out = ByteBuffer.allocate(10);
+			encoder.encode(CharBuffer.wrap("\ud800\udc00"));
+			encoder.reset();
+			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("\udc01"), out, true)
+					.isMalformed());
+		}
+	}
+
+	public void testReplacement() {
+		try {
+			encoder.replaceWith(null);
+			fail("should throw null pointer exception");
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			encoder.replaceWith(new byte[0]);
+			fail("should throw null pointer exception");
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			encoder.replaceWith(new byte[100]);
+			fail("should throw null pointer exception");
+		} catch (IllegalArgumentException e) {
+		}
+
+		byte[] nr = getLegalByteArray();
+		assertSame(encoder, encoder.replaceWith(nr));
+		assertSame(nr, encoder.replacement());
+
+		nr = getIllegalByteArray();
+		try {
+			encoder.replaceWith(new byte[100]);
+			fail("should throw null pointer exception");
+		} catch (IllegalArgumentException e) {
+		}
+	}
+
+	protected byte[] getLegalByteArray() {
+		return new byte[] { 'a' };
+	}
+
+	protected byte[] getIllegalByteArray() {
+		return new byte[155];
+	}
+
+	/*
+	 * Mock subclass of CharsetEncoder For protected method test
+	 */
+	public static class MockCharsetEncoder extends CharsetEncoder {
+
+		boolean flushed = false;
+
+		public boolean isFlushed() {
+			boolean result = flushed;
+			flushed = false;
+			return result;
+		}
+
+		public boolean isLegalReplacement(byte[] ba) {
+			if (ba.length == 155) {// specified magic number, return false
+				return false;
+			}
+			return super.isLegalReplacement(ba);
+		}
+
+		public MockCharsetEncoder(Charset cs, float aver, float max) {
+			super(cs, aver, max);
+		}
+
+		public MockCharsetEncoder(Charset cs, float aver, float max,
+				byte[] replacement) {
+			super(cs, aver, max, replacement);
+		}
+
+		protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
+			int inPosition = in.position();
+			char[] input = new char[in.remaining()];
+			in.get(input);
+			String result = new String(input);
+			if (result.startsWith("malform")) {
+				// reset the cursor to the error position
+				in.position(inPosition + "malform".length());
+				// in.position(0);
+				// set the error length
+				return CoderResult.malformedForLength("malform".length());
+			} else if (result.startsWith("unmap")) {
+				// reset the cursor to the error position
+				in.position(inPosition + "unmap".length());
+				// in.position(0);
+				// 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((byte) input[i]);
+			}
+			return r;
+		}
+
+		protected CoderResult implFlush(ByteBuffer out) {
+			CoderResult result = super.implFlush(out);
+			int length = 0;
+			if (out.remaining() >= 5) {
+				length = 5;
+				result = CoderResult.UNDERFLOW;
+				flushed = true;
+				// for (int i = 0; i < length; i++) {
+				// out.put((byte)'f');
+				// }
+			} else {
+				length = out.remaining();
+				result = CoderResult.OVERFLOW;
+			}
+			return result;
+		}
+
+		protected void implReplaceWith(byte[] ba) {
+			assertSame(ba, replacement());
+		}
+
+	}
+
+	/*
+	 * mock charset for test encoder initialization
+	 */
+	public static class MockCharset extends Charset {
+		protected MockCharset(String arg0, String[] arg1) {
+			super(arg0, arg1);
+		}
+
+		public boolean contains(Charset arg0) {
+			return false;
+		}
+
+		public CharsetDecoder newDecoder() {
+			return new CharsetDecoderTest.MockCharsetDecoder(this,
+					(float) AVER_BYTES, MAX_BYTES);
+		}
+
+		public CharsetEncoder newEncoder() {
+			return new MockCharsetEncoder(this, (float) AVER_BYTES, MAX_BYTES);
+		}
+	}
+
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetProviderTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetProviderTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetProviderTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetProviderTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,513 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.nio.charset;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
+
+import tests.api.java.nio.charset.CharsetTest.MockCharset;
+import tests.api.java.nio.charset.CharsetTest.MockSecurityManager;
+import junit.framework.TestCase;
+
+/**
+ * Test charset providers managed by Charset.
+ */
+public class CharsetProviderTest extends TestCase {
+
+	// need to be modified, e.g., read from system property
+	static String PROP_CONFIG_FILE1 = "clear.tests.cp1";
+
+	static String PROP_CONFIG_FILE2 = "clear.tests.cp2";
+
+	static String CONFIG_FILE1 = null;
+
+	static String CONFIG_FILE2 = null;
+
+	static MockCharset charset1 = new MockCharset("mockCharset00",
+			new String[] { "mockCharset01", "mockCharset02" });
+
+	static MockCharset charset2 = new MockCharset("mockCharset10",
+			new String[] { "mockCharset11", "mockCharset12" });
+
+	/**
+	 * @param arg0
+	 */
+	public CharsetProviderTest(String arg0) {
+		super(arg0);
+		if (null == System.getProperty(PROP_CONFIG_FILE1)
+				|| null == System.getProperty(PROP_CONFIG_FILE2)) {
+			throw new RuntimeException(
+					"Please set system property clear.tests.cp1 and clear.tests.cp2, i.e. two classpaths in order to run this test suite.");
+		}
+		String sep = System.getProperty("file.separator");
+
+		CONFIG_FILE1 = System.getProperty(PROP_CONFIG_FILE1);
+		if (!CONFIG_FILE1.endsWith(sep)) {
+			CONFIG_FILE1 += sep;
+		}
+		CONFIG_FILE1 += "META-INF" + sep + "services" + sep
+				+ "java.nio.charset.spi.CharsetProvider";
+		CONFIG_FILE2 = System.getProperty(PROP_CONFIG_FILE2);
+		if (!CONFIG_FILE2.endsWith(sep)) {
+			CONFIG_FILE2 += sep;
+		}
+		CONFIG_FILE2 += "META-INF" + sep + "services" + sep
+				+ "java.nio.charset.spi.CharsetProvider";
+	}
+
+	/*
+	 * Write the string to the config file.
+	 */
+	private void setupFile(String path, String content) throws Exception {
+		String sep = System.getProperty("file.separator");
+		int sepIndex = path.lastIndexOf(sep);
+		File f = new File(path.substring(0, sepIndex));
+		f.mkdirs();
+
+		FileOutputStream fos = new FileOutputStream(path);
+		OutputStreamWriter writer = new OutputStreamWriter(fos);// , "UTF-8");
+		try {
+			writer.write(content);
+		} finally {
+			writer.close();
+		}
+	}
+
+	/*
+	 * Write the string to the config file.
+	 */
+	private void cleanupFile(String path) throws Exception {
+		File f = new File(path);
+		f.delete();
+	}
+
+	/*
+	 * Test the method isSupported(String) with charset supported by some
+	 * providers (multiple).
+	 */
+	public void testIsSupported_NormalProvider() throws Exception {
+		try {
+			assertFalse(Charset.isSupported("mockCharset00"));
+			assertFalse(Charset.isSupported("mockCharset01"));
+			assertFalse(Charset.isSupported("mockCharset02"));
+
+			assertFalse(Charset.isSupported("mockCharset10"));
+			assertFalse(Charset.isSupported("mockCharset11"));
+			assertFalse(Charset.isSupported("mockCharset12"));
+
+			StringBuffer sb = new StringBuffer();
+			sb.append("#comment\r");
+			sb.append("\n");
+			sb.append("\r\n");
+			sb
+					.append(" \ttests.api.java.nio.charset.CharsetTest$MockCharsetProvider \t\n\r");
+			sb
+					.append(" \ttests.api.java.nio.charset.CharsetTest$MockCharsetProvider \t");
+			setupFile(CONFIG_FILE1, sb.toString());
+
+			sb = new StringBuffer();
+			sb.append(" #comment\r");
+			sb.append("\n");
+			sb.append("\r\n");
+			sb
+					.append(" \ttests.api.java.nio.charset.CharsetTest$MockCharsetProvider2 \t\n\r");
+			setupFile(CONFIG_FILE2, sb.toString());
+
+			assertTrue(Charset.isSupported("mockCharset00"));
+			assertTrue(Charset.isSupported("mockCharset01"));
+			assertTrue(Charset.isSupported("mockCharset02"));
+			assertTrue(Charset.isSupported("MOCKCharset00"));
+			assertTrue(Charset.isSupported("MOCKCharset01"));
+			assertTrue(Charset.isSupported("MOCKCharset02"));
+
+			assertTrue(Charset.isSupported("mockCharset10"));
+			// ignore case problem in mock, intended
+			assertFalse(Charset.isSupported("mockCharset11"));
+			assertTrue(Charset.isSupported("mockCharset12"));
+			assertTrue(Charset.isSupported("MOCKCharset10"));
+			// intended case problem in mock
+			assertFalse(Charset.isSupported("MOCKCharset11"));
+			assertTrue(Charset.isSupported("MOCKCharset12"));
+
+			// cleanupFile(CONFIG_FILE1);
+			// assertFalse(Charset.isSupported("mockCharset00"));
+			// assertFalse(Charset.isSupported("mockCharset01"));
+			// assertTrue(Charset.isSupported("mockCharset02"));
+		} finally {
+			cleanupFile(CONFIG_FILE1);
+			cleanupFile(CONFIG_FILE2);
+		}
+	}
+
+	/*
+	 * Test the method isSupported(String) when the configuration file contains
+	 * a non-existing class name.
+	 */
+	public void testIsSupported_NonExistingClass() throws Exception {
+		try {
+			StringBuffer sb = new StringBuffer();
+			sb.append("impossible\r");
+			setupFile(CONFIG_FILE1, sb.toString());
+
+			Charset.isSupported("impossible");
+			fail("Should throw Error!");
+		} catch (Error e) {
+			// expected
+		} finally {
+			cleanupFile(CONFIG_FILE1);
+		}
+	}
+
+	/*
+	 * Test the method isSupported(String) when the configuration file contains
+	 * a non-CharsetProvider class name.
+	 */
+	public void testIsSupported_NotCharsetProviderClass() throws Exception {
+		try {
+			StringBuffer sb = new StringBuffer();
+			sb.append("java.lang.String\r");
+			setupFile(CONFIG_FILE1, sb.toString());
+
+			Charset.isSupported("impossible");
+			fail("Should throw ClassCastException!");
+		} catch (ClassCastException e) {
+			// expected
+		} finally {
+			cleanupFile(CONFIG_FILE1);
+		}
+	}
+
+	/*
+	 * Test the method isSupported(String) with insufficient prililege to use
+	 * charset provider.
+	 */
+	public void testIsSupported_InsufficientPrililege() throws Exception {
+		SecurityManager oldMan = System.getSecurityManager();
+		System.setSecurityManager(new MockSecurityManager());
+		try {
+			Charset.isSupported("UTF-8");
+
+			try {
+				StringBuffer sb = new StringBuffer();
+				sb
+						.append("tests.api.java.nio.charset.CharsetTest$MockCharsetProvider2\r");
+				setupFile(CONFIG_FILE1, sb.toString());
+
+				Charset.isSupported("gb180300000");
+				// fail("Should throw SecurityException!");
+			} catch (SecurityException e) {
+				// expected
+			} finally {
+				cleanupFile(CONFIG_FILE1);
+			}
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+	}
+
+	/*
+	 * Test the method forName(String) with charset supported by some providers
+	 * (multiple).
+	 */
+	public void testForName_NormalProvider() throws Exception {
+		try {
+			try {
+				Charset.forName("mockCharset00");
+				fail("Should throw UnsupportedCharsetException!");
+			} catch (UnsupportedCharsetException e) {
+				// expected
+			}
+			try {
+				Charset.forName("mockCharset01");
+				fail("Should throw UnsupportedCharsetException!");
+			} catch (UnsupportedCharsetException e) {
+				// expected
+			}
+			try {
+				Charset.forName("mockCharset02");
+				fail("Should throw UnsupportedCharsetException!");
+			} catch (UnsupportedCharsetException e) {
+				// expected
+			}
+			try {
+				Charset.forName("mockCharset10");
+				fail("Should throw UnsupportedCharsetException!");
+			} catch (UnsupportedCharsetException e) {
+				// expected
+			}
+			try {
+				Charset.forName("mockCharset11");
+				fail("Should throw UnsupportedCharsetException!");
+			} catch (UnsupportedCharsetException e) {
+				// expected
+			}
+
+			// try {
+			// Charset.forName("mockCharset12");
+			// fail("Should throw UnsupportedCharsetException!");
+			// } catch (UnsupportedCharsetException e) {
+			// // expected
+			// }
+
+			StringBuffer sb = new StringBuffer();
+			sb.append("#comment\r");
+			sb.append("\n");
+			sb.append("\r\n");
+			sb
+					.append(" \ttests.api.java.nio.charset.CharsetTest$MockCharsetProvider \t\n\r");
+			sb
+					.append(" \ttests.api.java.nio.charset.CharsetTest$MockCharsetProvider \t");
+			setupFile(CONFIG_FILE1, sb.toString());
+
+			sb = new StringBuffer();
+			sb.append("#comment\r");
+			sb.append("\n");
+			sb.append("\r\n");
+			sb
+					.append(" \ttests.api.java.nio.charset.CharsetTest$MockCharsetProvider2 \t\n\r");
+			setupFile(CONFIG_FILE2, sb.toString());
+
+			assertTrue(Charset.forName("mockCharset00") instanceof MockCharset);
+			assertTrue(Charset.forName("mockCharset01") instanceof MockCharset);
+			assertTrue(Charset.forName("mockCharset02") instanceof MockCharset);
+
+			assertTrue(Charset.forName("mockCharset10") == charset2);
+			// intended case problem in mock
+			try {
+				Charset.forName("mockCharset11");
+				fail("Should throw UnsupportedCharsetException!");
+			} catch (UnsupportedCharsetException e) {
+				// expected
+			}
+			assertTrue(Charset.forName("mockCharset12") == charset2);
+
+			cleanupFile(CONFIG_FILE1);
+			try {
+				Charset.forName("mockCharset00");
+				fail("Should throw UnsupportedCharsetException!");
+			} catch (UnsupportedCharsetException e) {
+				// expected
+			}
+			try {
+				Charset.forName("mockCharset01");
+				fail("Should throw UnsupportedCharsetException!");
+			} catch (UnsupportedCharsetException e) {
+				// expected
+			}
+			try {
+				Charset.forName("mockCharset02");
+				fail("Should throw UnsupportedCharsetException!");
+			} catch (UnsupportedCharsetException e) {
+				// expected
+			}
+		} finally {
+			cleanupFile(CONFIG_FILE1);
+			cleanupFile(CONFIG_FILE2);
+		}
+	}
+
+	/*
+	 * Test the method forName(String) when the charset provider supports a
+	 * built-in charset.
+	 */
+	public void testForName_DuplicateWithBuiltInCharset() throws Exception {
+		try {
+			StringBuffer sb = new StringBuffer();
+			sb
+					.append("tests.api.java.nio.charset.CharsetTest$MockCharsetProvider3\r");
+			setupFile(CONFIG_FILE1, sb.toString());
+
+			assertFalse(Charset.forName("us-ascii") instanceof MockCharset);
+			assertFalse(Charset.availableCharsets().get("us-ascii") instanceof MockCharset);
+		} finally {
+			cleanupFile(CONFIG_FILE1);
+		}
+	}
+
+	/*
+	 * Test the method forName(String) when the configuration file contains a
+	 * non-existing class name.
+	 */
+	public void testForName_NonExistingClass() throws Exception {
+		try {
+			StringBuffer sb = new StringBuffer();
+			sb.append("impossible\r");
+			setupFile(CONFIG_FILE1, sb.toString());
+
+			Charset.forName("impossible");
+			fail("Should throw Error!");
+		} catch (Error e) {
+			// expected
+		} finally {
+			cleanupFile(CONFIG_FILE1);
+		}
+	}
+
+	/*
+	 * Test the method forName(String) when the configuration file contains a
+	 * non-CharsetProvider class name.
+	 */
+	public void testForName_NotCharsetProviderClass() throws Exception {
+		try {
+			StringBuffer sb = new StringBuffer();
+			sb.append("java.lang.String\r");
+			setupFile(CONFIG_FILE1, sb.toString());
+
+			Charset.forName("impossible");
+			fail("Should throw ClassCastException!");
+		} catch (ClassCastException e) {
+			// expected
+		} finally {
+			cleanupFile(CONFIG_FILE1);
+		}
+	}
+
+	/*
+	 * Test the method availableCharsets() with charset supported by some
+	 * providers (multiple).
+	 */
+	public void testAvailableCharsets_NormalProvider() throws Exception {
+		try {
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset00"));
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset01"));
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset02"));
+
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset10"));
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset11"));
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset12"));
+
+			StringBuffer sb = new StringBuffer();
+			sb.append("#comment\r");
+			sb.append("\n");
+			sb.append("\r\n");
+			sb
+					.append(" \ttests.api.java.nio.charset.CharsetTest$MockCharsetProvider \t\n\r");
+			sb
+					.append(" \ttests.api.java.nio.charset.CharsetTest$MockCharsetProvider \t");
+			setupFile(CONFIG_FILE1, sb.toString());
+
+			sb = new StringBuffer();
+			sb.append("#comment\r");
+			sb.append("\n");
+			sb.append("\r\n");
+			sb
+					.append(" \ttests.api.java.nio.charset.CharsetTest$MockCharsetProvider2 \t\n\r");
+			setupFile(CONFIG_FILE2, sb.toString());
+
+			assertTrue(Charset.availableCharsets().containsKey("mockCharset00"));
+			assertTrue(Charset.availableCharsets().containsKey("MOCKCharset00"));
+			assertTrue(Charset.availableCharsets().get("mockCharset00") instanceof MockCharset);
+			assertTrue(Charset.availableCharsets().get("MOCKCharset00") instanceof MockCharset);
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset01"));
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset02"));
+
+			assertTrue(Charset.availableCharsets().get("mockCharset10") == charset2);
+			assertTrue(Charset.availableCharsets().get("MOCKCharset10") == charset2);
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset11"));
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset12"));
+
+			// delete one config file
+			cleanupFile(CONFIG_FILE1);
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset00"));
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset01"));
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset02"));
+			assertTrue(Charset.availableCharsets().containsKey("mockCharset10"));
+			assertTrue(Charset.availableCharsets().containsKey("MOCKCharset10"));
+			assertTrue(Charset.availableCharsets().get("mockCharset10") == charset2);
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset11"));
+			assertFalse(Charset.availableCharsets()
+					.containsKey("mockCharset12"));
+		} finally {
+			cleanupFile(CONFIG_FILE1);
+			cleanupFile(CONFIG_FILE2);
+		}
+	}
+
+	/*
+	 * Test the method availableCharsets(String) when the configuration file
+	 * contains a non-existing class name.
+	 */
+	public void testAvailableCharsets_NonExistingClass() throws Exception {
+		try {
+			StringBuffer sb = new StringBuffer();
+			sb.append("impossible\r");
+			setupFile(CONFIG_FILE1, sb.toString());
+
+			Charset.availableCharsets();
+			fail("Should throw Error!");
+		} catch (Error e) {
+			// expected
+		} finally {
+			cleanupFile(CONFIG_FILE1);
+		}
+	}
+
+	/*
+	 * Test the method availableCharsets(String) when the configuration file
+	 * contains a non-CharsetProvider class name.
+	 */
+	public void testAvailableCharsets_NotCharsetProviderClass()
+			throws Exception {
+		try {
+			StringBuffer sb = new StringBuffer();
+			sb.append("java.lang.String\r");
+			setupFile(CONFIG_FILE1, sb.toString());
+
+			Charset.availableCharsets();
+			fail("Should throw ClassCastException!");
+		} catch (ClassCastException e) {
+			// expected
+		} finally {
+			cleanupFile(CONFIG_FILE1);
+		}
+	}
+
+	/*
+	 * Test the method availableCharsets(String) when the configuration file
+	 * contains an illegal string.
+	 */
+	public void testAvailableCharsets_IllegalString() throws Exception {
+		try {
+			StringBuffer sb = new StringBuffer();
+			sb.append("java String\r");
+			setupFile(CONFIG_FILE1, sb.toString());
+
+			Charset.availableCharsets();
+			fail("Should throw Error!");
+		} catch (Error e) {
+			// expected
+		} finally {
+			cleanupFile(CONFIG_FILE1);
+		}
+	}
+
+}



Mime
View raw message