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
17package tests.api.java.nio.charset;
18
19import dalvik.annotation.TestTargetClass;
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestLevel;
23
24import java.nio.CharBuffer;
25import java.nio.charset.CharacterCodingException;
26import java.nio.charset.Charset;
27
28/**
29 * test case specific activity of utf-8 charset encoder
30 */
31@TestTargetClass(java.nio.charset.CharsetEncoder.class)
32public class UTFCharsetEncoderTest extends AbstractCharsetEncoderTestCase {
33
34    // charset for UTF-8
35    private static final Charset CS = Charset.forName("utf-8");
36
37    /*
38     * @see CharsetEncoderTest#setUp()
39     */
40    protected void setUp() throws Exception {
41        cs = CS;
42        specifiedReplacement = new byte[] { -17, -65, -67 };
43        super.setUp();
44    }
45
46    /*
47     * @see CharsetEncoderTest#tearDown()
48     */
49    protected void tearDown() throws Exception {
50        super.tearDown();
51    }
52
53    @TestTargetNew(
54        level = TestLevel.PARTIAL,
55        notes = "IllegalStateException checking missed.",
56        method = "canEncode",
57        args = {char.class}
58    )
59    public void testCanEncodechar() throws CharacterCodingException {
60        // normal case for utfCS
61        assertTrue(encoder.canEncode('\u0077'));
62        assertTrue(encoder.canEncode('\uc2a3'));
63
64        // for non-mapped char
65        assertTrue(encoder.canEncode('\uc2c0'));
66    }
67
68    @TestTargetNew(
69        level = TestLevel.PARTIAL,
70        notes = "IllegalStateException checking missed.",
71        method = "canEncode",
72        args = {java.lang.CharSequence.class}
73    )
74    public void testCanEncodeCharSequence() {
75        // normal case for utfCS
76        assertTrue(encoder.canEncode("\u0077"));
77        assertTrue(encoder.canEncode("\uc2a3"));
78        assertTrue(encoder.canEncode(""));
79
80        // for non-mapped char
81        assertTrue(encoder.canEncode("\uc2c0"));
82
83        // surrogate char for unicode
84        // 1st byte: d800-dbff
85        // 2nd byte: dc00-dfff
86        // valid surrogate pair
87        assertTrue(encoder.canEncode("\ud800\udc00"));
88        // invalid surrogate pair
89        assertFalse(encoder.canEncode("\ud800\udb00"));
90    }
91
92    @TestTargetNew(
93        level = TestLevel.PARTIAL,
94        notes = "Regression test. IllegalStateException checking missed.",
95        method = "canEncode",
96        args = {java.lang.CharSequence.class}
97    )
98    public void testCanEncodeICUBug() {
99        assertFalse(encoder.canEncode("\ud800"));
100    }
101
102    @TestTargets({
103        @TestTargetNew(
104            level = TestLevel.COMPLETE,
105            notes = "",
106            method = "averageBytesPerChar",
107            args = {}
108        ),
109        @TestTargetNew(
110            level = TestLevel.COMPLETE,
111            notes = "",
112            method = "maxBytesPerChar",
113            args = {}
114        )
115    })
116    public void testSpecificDefaultValue() {
117        // assertEquals(1.1, encoder.averageBytesPerChar(), 0.0001);
118        assertEquals(2, encoder.averageBytesPerChar(), 0.0001);
119        assertEquals(3, encoder.maxBytesPerChar(), 0);
120    }
121
122    CharBuffer getMalformedCharBuffer() {
123        return CharBuffer.wrap("\ud800 buffer");
124    }
125
126    CharBuffer getUnmapCharBuffer() {
127        return null;
128    }
129
130    CharBuffer getExceptionCharBuffer() {
131        return null;
132    }
133
134    protected byte[] getIllegalByteArray() {
135        return new byte[] { (byte) 0xd8, (byte) 0x00 };
136    }
137
138    protected void assertFlushed() {
139    }
140}
141