Home | History | Annotate | Download | only in charset
      1 /* Licensed to the Apache Software Foundation (ASF) under one or more
      2  * contributor license agreements.  See the NOTICE file distributed with
      3  * this work for additional information regarding copyright ownership.
      4  * The ASF licenses this file to You under the Apache License, Version 2.0
      5  * (the "License"); you may not use this file except in compliance with
      6  * the License.  You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package tests.api.java.nio.charset;
     17 
     18 import java.nio.ByteBuffer;
     19 import java.nio.CharBuffer;
     20 import java.nio.charset.CharacterCodingException;
     21 import java.nio.charset.Charset;
     22 import java.nio.charset.CoderResult;
     23 import java.nio.charset.CodingErrorAction;
     24 import java.nio.charset.UnmappableCharacterException;
     25 
     26 /**
     27  * test case specific activity of iso-8859-1 charset encoder
     28  */
     29 public class ISOCharsetEncoderTest extends CharsetEncoderTest {
     30 
     31 	// charset for iso-8859-1
     32 	private static final Charset CS = Charset.forName("iso-8859-1");
     33 
     34 	/*
     35 	 * @see CharsetEncoderTest#setUp()
     36 	 */
     37 	protected void setUp() throws Exception {
     38 		cs = CS;
     39 		super.setUp();
     40 	}
     41 
     42 	/*
     43 	 * @see CharsetEncoderTest#tearDown()
     44 	 */
     45 	protected void tearDown() throws Exception {
     46 		super.tearDown();
     47 	}
     48 
     49 	public void testCanEncodeCharSequence() {
     50 		// normal case for isoCS
     51 		assertTrue(encoder.canEncode("\u0077"));
     52 		assertFalse(encoder.canEncode("\uc2a3"));
     53 		assertFalse(encoder.canEncode("\ud800\udc00"));
     54 		try {
     55 			encoder.canEncode(null);
     56 		} catch (NullPointerException e) {
     57 		}
     58 		assertTrue(encoder.canEncode(""));
     59 	}
     60 
     61 	public void testCanEncodeICUBug() {
     62 		assertFalse(encoder.canEncode((char) '\ud800'));
     63 		assertFalse(encoder.canEncode((String) "\ud800"));
     64 	}
     65 
     66 	public void testCanEncodechar() throws CharacterCodingException {
     67 		assertTrue(encoder.canEncode('\u0077'));
     68 		assertFalse(encoder.canEncode('\uc2a3'));
     69 	}
     70 
     71 	public void testSpecificDefaultValue() {
     72 		assertEquals(1, encoder.averageBytesPerChar(), 0.001);
     73 		assertEquals(1, encoder.maxBytesPerChar(), 0.001);
     74 	}
     75 
     76 	CharBuffer getMalformedCharBuffer() {
     77 		return CharBuffer.wrap("\ud800 buffer");
     78 	}
     79 
     80 	CharBuffer getUnmapCharBuffer() {
     81 		return CharBuffer.wrap("\ud800\udc00 buffer");
     82 	}
     83 
     84 	CharBuffer getExceptionCharBuffer() {
     85 		return null;
     86 	}
     87 
     88 	protected byte[] getIllegalByteArray() {
     89 		return null;
     90 	}
     91 
     92 	public void testMultiStepEncode() throws CharacterCodingException {
     93 		encoder.onMalformedInput(CodingErrorAction.REPORT);
     94 		encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
     95 		try {
     96 			encoder.encode(CharBuffer.wrap("\ud800\udc00"));
     97 			fail("should unmappable");
     98 		} catch (UnmappableCharacterException e) {
     99 		}
    100 		encoder.reset();
    101 		ByteBuffer out = ByteBuffer.allocate(10);
    102 		assertTrue(encoder.encode(CharBuffer.wrap("\ud800"), out, true)
    103 				.isMalformed());
    104 		encoder.flush(out);
    105 		encoder.reset();
    106 		out = ByteBuffer.allocate(10);
    107 		assertSame(CoderResult.UNDERFLOW, encoder.encode(CharBuffer
    108 				.wrap("\ud800"), out, false));
    109 		assertTrue(encoder.encode(CharBuffer.wrap("\udc00"), out, true)
    110 				.isMalformed());
    111 	}
    112 }
    113