harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lian...@apache.org
Subject svn commit: r517079 - in /harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset: ASCCharsetEncoderTest.java ASCIICharsetEncoderTest.java AllTests.java CharsetEncoderTest.java
Date Mon, 12 Mar 2007 02:37:46 GMT
Author: liangyx
Date: Sun Mar 11 19:37:45 2007
New Revision: 517079

URL: http://svn.apache.org/viewvc?view=rev&rev=517079
Log:
refactor CharsetEncoderTest

Added:
    harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCIICharsetEncoderTest.java
      - copied, changed from r515882, harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetEncoderTest.java
Removed:
    harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetEncoderTest.java
Modified:
    harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/AllTests.java
    harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java

Copied: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCIICharsetEncoderTest.java
(from r515882, harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetEncoderTest.java)
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCIICharsetEncoderTest.java?view=diff&rev=517079&p1=harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetEncoderTest.java&r1=515882&p2=harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCIICharsetEncoderTest.java&r2=517079
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetEncoderTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCIICharsetEncoderTest.java
Sun Mar 11 19:37:45 2007
@@ -20,29 +20,31 @@
 import java.nio.CharBuffer;
 import java.nio.charset.CharacterCodingException;
 import java.nio.charset.Charset;
+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;
 
-public class ASCCharsetEncoderTest extends CharsetEncoderTest {
+import junit.framework.TestCase;
 
-	// charset for ascii
-	private static final Charset CS = Charset.forName("ascii");
+public class ASCIICharsetEncoderTest extends TestCase {
 
+	// charset for ascii
+	private static final Charset cs = Charset.forName("ascii");
+    private static final CharsetEncoder encoder = cs.newEncoder();
+    private static final int MAXCODEPOINT = 0x7F; 
 	/*
 	 * @see CharsetEncoderTest#setUp()
 	 */
 	protected void setUp() throws Exception {
-		cs = CS;
-		super.setUp();
-	}
+    }
 
 	/*
 	 * @see CharsetEncoderTest#tearDown()
 	 */
 	protected void tearDown() throws Exception {
-		super.tearDown();
-	}
+    }
 
 	public void testCanEncodeCharSequence() {
 		// normal case for ascCS
@@ -56,9 +58,9 @@
 		assertTrue(encoder.canEncode(""));
 	}
 
-	public void testCanEncodeICUBug() {
+	public void testCanEncodeSurrogate () {
 		assertFalse(encoder.canEncode('\ud800'));
-		assertFalse(encoder.canEncode("\ud800"));
+		assertFalse(encoder.canEncode("\udc00"));
 	}
 
 	public void testCanEncodechar() throws CharacterCodingException {
@@ -71,22 +73,6 @@
 		assertEquals(1.0, encoder.maxBytesPerChar(), 0.0);
 	}
 
-	CharBuffer getMalformedCharBuffer() {
-		return CharBuffer.wrap("\ud800 buffer");
-	}
-
-	CharBuffer getUnmapCharBuffer() {
-		return CharBuffer.wrap("\ud800\udc00 buffer");
-	}
-
-	CharBuffer getExceptionCharBuffer() {
-		return null;
-	}
-
-	protected byte[] getIllegalByteArray() {
-		return new byte[] { (byte) -1 };
-	}
-
 	public void testMultiStepEncode() throws CharacterCodingException {
 		encoder.onMalformedInput(CodingErrorAction.REPORT);
 		encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
@@ -108,4 +94,358 @@
 				.isMalformed());
 	}
 
+    public void testEncodeMapping() throws CharacterCodingException {
+        encoder.reset();
+        
+        for (int i =0; i <= MAXCODEPOINT; i++) {
+            char[] chars = Character.toChars(i);
+            CharBuffer cb = CharBuffer.wrap(chars);
+            ByteBuffer bb = encoder.encode(cb);
+            assertEquals(i, bb.get(0));
+        }
+        
+        CharBuffer cb = CharBuffer.wrap("\u0080");
+        try {
+            encoder.encode(cb);
+        } catch (UnmappableCharacterException e) {
+            //expected
+        }
+        
+        cb = CharBuffer.wrap("\ud800");
+        try {
+            encoder.encode(cb);
+        } catch (MalformedInputException e) {
+            //expected
+        }
+
+        ByteBuffer bb = ByteBuffer.allocate(0x10);
+        cb = CharBuffer.wrap("A");
+        encoder.reset();
+        encoder.encode(cb, bb, false);
+        try {
+        encoder.encode(cb);
+        } catch (IllegalStateException e) {
+            //expected
+        }
+    }
+    
+    public void testInternalState() {
+        CharBuffer in = CharBuffer.wrap("A");
+        ByteBuffer out = ByteBuffer.allocate(0x10);
+        
+        //normal encoding process
+        encoder.reset();
+        encoder.encode(in, out, false);
+        in = CharBuffer.wrap("B");
+        encoder.encode(in, out, true);
+        encoder.flush(out);
+    }
+    
+    //reset could be called at any time
+    public void testInternalState_Reset() {
+        CharsetEncoder newEncoder = cs.newEncoder();
+        //Init - > reset
+        newEncoder.reset();
+        
+        //reset - > reset
+        newEncoder.reset();
+
+        //encoding - >reset
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, false);
+            newEncoder.reset();
+        }
+
+        //encoding end -> reset
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+            newEncoder.reset();
+        }
+        //flused -> reset
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+            newEncoder.flush(out);
+            newEncoder.reset();
+        }
+    }
+    
+    public void testInternalState_Encoding() {
+        CharsetEncoder newEncoder = cs.newEncoder();
+        //Init - > encoding
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, false);
+        }
+        
+        //reset - > encoding
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.reset();            
+            newEncoder.encode(in, out, false);
+        }
+        //reset - > encoding - > encoding
+        {
+            newEncoder.reset();            
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, false);
+            in = CharBuffer.wrap("BC");
+            newEncoder.encode(in, out, false);
+        }
+        
+        //encoding_end - > encoding
+        {
+            newEncoder.reset();            
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+            in = CharBuffer.wrap("BC");
+            try {
+                newEncoder.encode(in, out, false);
+                fail("Should throw IllegalStateException");
+            } catch (IllegalStateException e) {
+                //expected
+            }
+        }
+        //flushed - > encoding
+        {
+            newEncoder.reset();            
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+            newEncoder.flush(out);
+            in = CharBuffer.wrap("BC");
+            try {
+                newEncoder.encode(in, out, false);
+                fail("Should throw IllegalStateException");
+            } catch (IllegalStateException e) {
+                //expected
+            }
+        }
+    }
+    
+    public void testInternalState_Encoding_END() {
+        CharsetEncoder newEncoder = cs.newEncoder();
+
+        //Init - >encoding_end
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+        }
+        
+        //Reset -> encoding_end
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.reset();
+            newEncoder.encode(in, out, true);
+        }
+
+        //encoding -> encoding_end
+        {
+            newEncoder.reset();
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, false);
+            in = CharBuffer.wrap("BC");
+            newEncoder.encode(in, out, true);
+        }
+        
+        //Reset -> encoding_end
+        {
+            newEncoder.reset();
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+            in = CharBuffer.wrap("BC");
+            newEncoder.encode(in, out, true);
+        }
+        
+        //Flushed -> encoding_end
+        {
+            newEncoder.reset();
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+            newEncoder.flush(out);
+            in = CharBuffer.wrap("BC");
+            try {
+                newEncoder.encode(in, out, true);
+                fail("Should throw IllegalStateException");
+            } catch (IllegalStateException e) {
+                //expected
+            }
+        }
+    }
+    
+    public void testInternalState_Flushed() {
+        CharsetEncoder newEncoder = cs.newEncoder();
+        
+        //init -> flushed
+        {
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.flush(out);
+        }
+        
+        //reset - > flushed
+        {
+            newEncoder.reset();
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+            newEncoder.reset();
+            newEncoder.flush(out);
+        }
+        
+        //encoding - > flushed
+        {
+            newEncoder.reset();
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, false);
+            try {
+
+                newEncoder.flush(out);
+                fail("Should throw IllegalStateException");
+            } catch (IllegalStateException e) {
+                // expected
+            }
+        }
+        
+        //encoding_end -> flushed
+        {
+            newEncoder.reset();
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+            newEncoder.flush(out);
+        }
+        
+        //flushd - > flushed
+        {
+            newEncoder.reset();
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+            newEncoder.flush(out);
+            try {
+                newEncoder.flush(out);
+                fail("Should throw IllegalStateException");
+            } catch (IllegalStateException e) {
+                // expected
+            }
+        }
+    }
+    
+    public void testInternalState_Encode() throws CharacterCodingException {
+        CharsetEncoder newEncoder = cs.newEncoder();
+        //Init - > encode
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            newEncoder.encode(in);
+        }
+        
+        //Reset - > encode
+        {
+            newEncoder.reset();
+            CharBuffer in = CharBuffer.wrap("A");
+            newEncoder.encode(in);
+        }
+        
+        //Encoding -> encode
+        {
+            newEncoder.reset();
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, false);
+            in = CharBuffer.wrap("BC");
+            newEncoder.encode(in);
+        }
+        
+        //Encoding_end -> encode
+        {
+            newEncoder.reset();
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+            in = CharBuffer.wrap("BC");
+            newEncoder.encode(in);
+        }
+        
+        //Flushed -> reset
+        {
+            newEncoder.reset();
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            newEncoder.encode(in, out, true);
+            in = CharBuffer.wrap("BC");
+            newEncoder.flush(out);
+            out = newEncoder.encode(in);
+        }
+    }
+    
+    public void testInternalState_from_Encode() throws CharacterCodingException {
+        CharsetEncoder newEncoder = cs.newEncoder();
+        
+        //Encode -> Reset
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            newEncoder.encode(in);
+            newEncoder.reset();
+        }
+        
+        // Encode -> encoding
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            newEncoder.encode(in);
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            try {
+                newEncoder.encode(in, out, false);
+                fail("Should throw IllegalStateException");
+            } catch (IllegalStateException e) {
+                // expected
+            }
+        }
+        
+        //Encode -> Encoding_end
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            newEncoder.encode(in);
+            ByteBuffer out = ByteBuffer.allocate(0x10);
+            try {
+                newEncoder.encode(in, out, true);
+                fail("Should throw IllegalStateException");
+            } catch (IllegalStateException e) {
+                // expected
+            }
+        }
+        //Encode -> Flushed
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            ByteBuffer out = newEncoder.encode(in);
+            try {
+                newEncoder.flush(out);
+                fail("Should throw IllegalStateException");
+            } catch (IllegalStateException e) {
+                // expected
+            }
+        }
+        
+        //Encode - > encode
+        {
+            CharBuffer in = CharBuffer.wrap("A");
+            newEncoder.encode(in);
+            in = CharBuffer.wrap("BC");
+            newEncoder.encode(in);
+        }
+    }
 }

Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/AllTests.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/AllTests.java?view=diff&rev=517079&r1=517078&r2=517079
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/AllTests.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/AllTests.java
Sun Mar 11 19:37:45 2007
@@ -41,7 +41,7 @@
 		suite.addTestSuite(ISOCharsetEncoderTest.class);
 		suite.addTestSuite(UTFCharsetEncoderTest.class);
 		// suite.addTestSuite(GBCharsetEncoderTest.class);
-		suite.addTestSuite(ASCCharsetEncoderTest.class);
+		suite.addTestSuite(ASCIICharsetEncoderTest.class);
 		suite.addTestSuite(UTF16CharsetEncoderTest.class);
 		suite.addTestSuite(UTF16LECharsetEncoderTest.class);
 		suite.addTestSuite(UTF16BECharsetEncoderTest.class);

Modified: harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java?view=diff&rev=517079&r1=517078&r2=517079
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetEncoderTest.java
Sun Mar 11 19:37:45 2007
@@ -40,7 +40,7 @@
 	static final float AVER_BYTES = 0.5f;
 
 	// charset for mock class
-	private static final Charset MOCKCS = new MockCharset("mock", new String[0]);
+	private static final Charset MOCKCS = new MockCharset("CharsetEncoderTest_mock", new String[0]);
 
 	Charset cs = MOCKCS;
 
@@ -939,25 +939,6 @@
 		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() {



Mime
View raw message