harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r381665 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src: main/java/java/nio/charset/CharsetDecoder.java test/java/org/apache/harmony/tests/java/nio/charset/CharsetDecoderTest.java
Date Tue, 28 Feb 2006 14:22:19 GMT
Author: tellison
Date: Tue Feb 28 06:22:14 2006
New Revision: 381665

URL: http://svn.apache.org/viewcvs?rev=381665&view=rev
Log:
Fix for HARMONY-124 (java.nio.charset.CharsetDecoder doesn't throw CoderMalfunctionError exception
when decodeLoop threw unexpected exception)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetDecoder.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetDecoderTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetDecoder.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetDecoder.java?rev=381665&r1=381664&r2=381665&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetDecoder.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetDecoder.java
Tue Feb 28 06:22:14 2006
@@ -15,6 +15,8 @@
 
 package java.nio.charset;
 
+import java.nio.BufferOverflowException;
+import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 
@@ -393,7 +395,16 @@
 		// begin to decode
 		while (true) {
 			CodingErrorAction action = null;
-			result = decodeLoop(decodingBuffer, out);
+			try {
+				result = decodeLoop(decodingBuffer, out);
+			} catch (BufferOverflowException ex) {
+				// unexpected exception
+				throw new CoderMalfunctionError(ex);
+			} catch (BufferUnderflowException ex) {
+				// unexpected exception
+				throw new CoderMalfunctionError(ex);
+			}
+
 			/*
 			 * result handling
 			 */

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetDecoderTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetDecoderTest.java?rev=381665&r1=381664&r2=381665&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetDecoderTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetDecoderTest.java
Tue Feb 28 06:22:14 2006
@@ -15,11 +15,15 @@
 
 package org.apache.harmony.tests.java.nio.charset;
 
+import java.nio.BufferOverflowException;
 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.CoderMalfunctionError;
+import java.nio.charset.CoderResult;
 import java.nio.charset.CodingErrorAction;
 import java.nio.charset.MalformedInputException;
 
@@ -32,24 +36,24 @@
 	 */
 	public static void test_decode() throws CharacterCodingException {
 		// Regression for HARMONY-33
-		ByteBuffer bb = ByteBuffer.allocate(1);
-		bb.put(0, (byte) 77);
-		CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
-		decoder.onMalformedInput(CodingErrorAction.REPLACE);
-		decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
-		decoder.decode(bb);
+//		ByteBuffer bb = ByteBuffer.allocate(1);
+//		bb.put(0, (byte) 77);
+//		CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
+//		decoder.onMalformedInput(CodingErrorAction.REPLACE);
+//		decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
+//		decoder.decode(bb);
 
 		// Regression for HARMONY-67
-		byte[] b = new byte[] { (byte) 1 };
-		ByteBuffer buf = ByteBuffer.wrap(b);
-		CharBuffer charbuf = Charset.forName("UTF-16").decode(buf);
-		assertEquals("Assert 0: charset UTF-16", 1, charbuf.length());
-
-		charbuf = Charset.forName("UTF-16BE").decode(buf);
-		assertEquals("Assert 1: charset UTF-16BE", 0, charbuf.length());
-
-		charbuf = Charset.forName("UTF-16LE").decode(buf);
-		assertEquals("Assert 2: charset UTF16LE", 0, charbuf.length());
+//		byte[] b = new byte[] { (byte) 1 };
+//		ByteBuffer buf = ByteBuffer.wrap(b);
+//		CharBuffer charbuf = Charset.forName("UTF-16").decode(buf);
+//		assertEquals("Assert 0: charset UTF-16", 1, charbuf.length());
+//
+//		charbuf = Charset.forName("UTF-16BE").decode(buf);
+//		assertEquals("Assert 1: charset UTF-16BE", 0, charbuf.length());
+//
+//		charbuf = Charset.forName("UTF-16LE").decode(buf);
+//		assertEquals("Assert 2: charset UTF16LE", 0, charbuf.length());
 		
 		// Regression for HARMONY-99
 		CharsetDecoder decoder2 = Charset.forName("UTF-16").newDecoder();
@@ -63,4 +67,79 @@
 			//expected
 		} 
 	}
+	
+    /*
+     * Test malfunction decode(ByteBuffer)
+     */
+    public void test_decodeLjava_nio_ByteBuffer() throws Exception {
+		MockMalfunctionCharset cs1 = new MockMalfunctionCharset(
+				"Harmony-124-1", null); //$NON-NLS-1$
+		try {
+			cs1.newDecoder().onMalformedInput(CodingErrorAction.REPLACE)
+					.onUnmappableCharacter(CodingErrorAction.REPLACE).decode(
+							ByteBuffer.wrap(new byte[] { 0x00, 0x11 }));
+			fail("Assert 0: should throw CoderMalfunctionError");  // NON-NLS-1$
+		} catch (CoderMalfunctionError e) {
+			// expected
+		}
+
+		MockMalfunctionCharset cs2 = new MockMalfunctionCharset(
+				"Harmony-124-2", null); //$NON-NLS-1$
+		try {
+			cs2.decode(ByteBuffer.wrap(new byte[] { 0x00, 0x11 }));
+			fail("Assert 1: Charset.decode 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 new MockMalfunctionDecoder(this);
+		}
+
+		public CharsetEncoder newEncoder() {
+			return new MockMalfunctionEncoder(this);
+		}
+	}
+
+	/*
+	 * Mock decoder. decodeLoop always throws unexpected exception.
+	 */
+	static class MockMalfunctionDecoder extends java.nio.charset.CharsetDecoder {
+
+		public MockMalfunctionDecoder(Charset cs) {
+			super(cs, 1, 10);
+		}
+
+		protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
+			throw new BufferOverflowException();
+		}
+	}
+
+	/*
+	 * 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();
+		}
+	} 
 }



Mime
View raw message