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 * TODO type def
30 */
31@TestTargetClass(java.nio.charset.CharsetEncoder.class)
32public class UTF16BECharsetEncoderTest extends AbstractCharsetEncoderTestCase {
33
34    // charset for utf-16be
35    private static final Charset CS = Charset.forName("utf-16be");
36
37    /*
38     * @see CharsetEncoderTest#setUp()
39     */
40    protected void setUp() throws Exception {
41        cs = CS;
42        specifiedReplacement = new byte[] { -1, -3 };
43        unibytes = new byte[] { 0, 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101,
44                0, 114 };
45
46        // unibytesWithRep = new byte[] {(byte)0xff, (byte)0xfd,0, 32, 0, 98, 0,
47        // 117, 0, 102, 0, 102, 0, 101, 0, 114};
48
49        super.setUp();
50    }
51
52    /*
53     * @see CharsetEncoderTest#tearDown()
54     */
55    protected void tearDown() throws Exception {
56        super.tearDown();
57    }
58
59    @TestTargetNew(
60        level = TestLevel.TODO,
61        notes = "Empty test.",
62        method = "CharsetEncoder",
63        args = {java.nio.charset.Charset.class, float.class, float.class}
64    )
65    public void testCharsetEncoderCharsetfloatfloat() {
66        // this constructor is invalid for UTF16LE CharsetEncoder
67// mc081121:
68// NO! CERTAINLY NOT LIKE FOLLOWS:
69//        new CharsetEncoderTest.MockCharsetEncoder(CS, 0.5f, 3.0f);
70    }
71
72    @TestTargetNew(
73        level = TestLevel.PARTIAL,
74        notes = "IllegalStateException checking missed.",
75        method = "canEncode",
76        args = {char.class}
77    )
78    public void testCanEncodechar() throws CharacterCodingException {
79        // normal case for utfCS
80        assertTrue(encoder.canEncode('\u0077'));
81        assertTrue(encoder.canEncode('\uc2a3'));
82
83        // for non-mapped char
84        assertTrue(encoder.canEncode('\uc2c0'));
85
86    }
87
88    @TestTargetNew(
89        level = TestLevel.PARTIAL,
90        notes = "IllegalStateException checking missed.",
91        method = "canEncode",
92        args = {java.lang.CharSequence.class}
93    )
94    public void testCanEncodeCharSequence() {
95        // normal case for utfCS
96        assertTrue(encoder.canEncode("\u0077"));
97        assertTrue(encoder.canEncode("\uc2a3"));
98        assertTrue(encoder.canEncode(""));
99
100        // for non-mapped char
101        assertTrue(encoder.canEncode("\uc2c0"));
102
103        // surrogate char for unicode
104        // 1st byte: d800-dbff
105        // 2nd byte: dc00-dfff
106        // valid surrogate pair
107        assertTrue(encoder.canEncode("\ud800\udc00"));
108        // invalid surrogate pair
109        assertFalse(encoder.canEncode("\ud800\udb00"));
110    }
111
112    @TestTargetNew(
113        level = TestLevel.PARTIAL,
114        notes = "Regression test. IllegalStateException checking missed.",
115        method = "canEncode",
116        args = {java.lang.CharSequence.class}
117    )
118    public void testCanEncodeICUBug() {
119        assertFalse(encoder.canEncode("\ud800"));
120    }
121
122    @TestTargetNew(
123        level = TestLevel.COMPLETE,
124        notes = "",
125        method = "maxBytesPerChar",
126        args = {}
127    )
128    public void testSpecificDefaultValue() {
129        // ??? TODO NIO adapt to the actually used UTF16BE charset Encoder
130        // assertEquals(2, encoder.averageBytesPerChar(), 0.001);
131        assertEquals(2, encoder.maxBytesPerChar(), 0.001);
132    }
133
134    CharBuffer getMalformedCharBuffer() {
135        return CharBuffer.wrap("\ud800 buffer");
136    }
137
138    CharBuffer getUnmapCharBuffer() {
139        return null;
140    }
141
142    CharBuffer getExceptionCharBuffer() {
143        return null;
144    }
145
146    @TestTargetNew(
147        level = TestLevel.COMPLETE,
148        notes = "",
149        method = "isLegalReplacement",
150        args = {byte[].class}
151    )
152    public void testIsLegalReplacementEmptyArray() {
153        assertTrue(encoder.isLegalReplacement(new byte[0]));
154    }
155
156    protected byte[] getIllegalByteArray() {
157        // FIXME: different here
158        // cannot replace by 0xd8d8, but RI can
159        // return new byte[]{(byte)0xd8, (byte)0xd8};
160        return new byte[] { 0 };
161    }
162
163    protected byte[] getLegalByteArray() {
164        return new byte[] { (byte) 0x00, (byte) 0xd8 };
165    }
166}
167