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 
17 package org.apache.harmony.tests.java.nio.charset;
18 
19 import java.nio.CharBuffer;
20 import java.nio.charset.CharacterCodingException;
21 import java.nio.charset.Charset;
22 
23 /**
24  * test case specific activity of utf-8 charset encoder
25  */
26 public class UTFCharsetEncoderTest extends CharsetEncoderTest {
27 
28 	// charset for UTF-8
29 	private static final Charset CS = Charset.forName("utf-8");
30 
31 	/*
32 	 * @see CharsetEncoderTest#setUp()
33 	 */
setUp()34 	protected void setUp() throws Exception {
35 		cs = CS;
36 		specifiedReplacement = new byte[] { 63 };
37 		super.setUp();
38 	}
39 
40 	/*
41 	 * @see CharsetEncoderTest#tearDown()
42 	 */
tearDown()43 	protected void tearDown() throws Exception {
44 		super.tearDown();
45 	}
46 
testCanEncodechar()47 	public void testCanEncodechar() throws CharacterCodingException {
48 		// normal case for utfCS
49 		assertTrue(encoder.canEncode('\u0077'));
50 		assertTrue(encoder.canEncode('\uc2a3'));
51 
52 		// for non-mapped char
53 		assertTrue(encoder.canEncode('\uc2c0'));
54 	}
55 
testCanEncodeCharSequence()56 	public void testCanEncodeCharSequence() {
57 		// normal case for utfCS
58 		assertTrue(encoder.canEncode("\u0077"));
59 		assertTrue(encoder.canEncode("\uc2a3"));
60 
61 		// for non-mapped char
62 		assertTrue(encoder.canEncode("\uc2c0"));
63 
64 		// surrogate char for unicode
65 		// 1st byte: d800-dbff
66 		// 2nd byte: dc00-dfff
67 		// valid surrogate pair
68 		assertTrue(encoder.canEncode("\ud800\udc00"));
69 		// invalid surrogate pair
70 		assertFalse(encoder.canEncode("\ud800\udb00"));
71 	}
72 
testSpecificDefaultValue()73 	public void testSpecificDefaultValue() {
74 		assertEquals(2, encoder.averageBytesPerChar(), 0);
75 		assertEquals(3, encoder.maxBytesPerChar(), 0);
76 	}
77 
getMalformedCharBuffer()78 	CharBuffer getMalformedCharBuffer() {
79 		return CharBuffer.wrap("\ud800 buffer");
80 	}
81 
getUnmapCharBuffer()82 	CharBuffer getUnmapCharBuffer() {
83 		return null;
84 	}
85 
getExceptionCharBuffer()86 	CharBuffer getExceptionCharBuffer() {
87 		return null;
88 	}
89 
getIllegalByteArray()90 	protected byte[] getIllegalByteArray() {
91 		return new byte[] { (byte) 0xd8, (byte) 0x00 };
92 	}
93 
assertFlushed()94 	protected void assertFlushed() {
95 	}
96 }
97