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 java.nio.ByteBuffer;
20import java.nio.CharBuffer;
21import java.nio.charset.CharacterCodingException;
22import java.nio.charset.Charset;
23import java.nio.charset.CharsetDecoder;
24
25/**
26 * TODO type def
27 */
28public class UTF16CharsetEncoderTest extends CharsetEncoderTest {
29
30	// charset for utf-16
31	// charset for utf-16be
32	private static final Charset CS = Charset.forName("utf-16");
33
34	private static final CharsetDecoder decoder = CS.newDecoder();
35
36	/*
37	 * @see CharsetEncoderTest#setUp()
38	 */
39	protected void setUp() throws Exception {
40		cs = CS;
41		specifiedReplacement = new byte[] { -1, -3 };
42		surrogate = new byte[] { -1, -2 };
43		unibytes = new byte[] { 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0,
44				114, 0 };
45		unibytesWithRep = new byte[] { -3, -1, 32, 0, 98, 0, 117, 0, 102, 0,
46				102, 0, 101, 0, 114, 0 };
47		super.setUp();
48	}
49
50	/*
51	 * @see CharsetEncoderTest#tearDown()
52	 */
53	protected void tearDown() throws Exception {
54		super.tearDown();
55	}
56
57	public void testCharsetEncoderCharsetfloatfloat() {
58		// this constructor is invalid for UTF16LE CharsetEncoder
59	}
60
61	public void testCanEncodechar() throws CharacterCodingException {
62		// normal case for utfCS
63		assertTrue(encoder.canEncode('\u0077'));
64		assertTrue(encoder.canEncode('\uc2a3'));
65
66		// for non-mapped char
67		assertTrue(encoder.canEncode('\uc2c0'));
68	}
69
70	public void testCanEncodeCharSequence() {
71		// normal case for utfCS
72		assertTrue(encoder.canEncode("\u0077"));
73		assertTrue(encoder.canEncode("\uc2a3"));
74		assertTrue(encoder.canEncode(""));
75
76		// for non-mapped char
77		assertTrue(encoder.canEncode("\uc2c0"));
78
79		// surrogate char for unicode
80		// 1st byte: d800-dbff
81		// 2nd byte: dc00-dfff
82		// valid surrogate pair
83		assertTrue(encoder.canEncode("\ud800\udc00"));
84		// invalid surrogate pair
85		assertFalse(encoder.canEncode("\ud800\udb00"));
86	}
87
88	public void testCanEncodeICUBug() {
89		assertFalse(encoder.canEncode('\ud800'));
90		assertFalse(encoder.canEncode("\ud800"));
91	}
92
93	public void testSpecificDefaultValue() {
94		assertEquals(encoder.averageBytesPerChar(), 2, 0.001);
95		// assertEquals(4, encoder.maxBytesPerChar());
96		// FIXME: different here!
97		assertEquals(encoder.maxBytesPerChar(), 2, 0.001);
98	}
99
100	CharBuffer getMalformedCharBuffer() {
101		return CharBuffer.wrap("\ud800 buffer");
102	}
103
104	CharBuffer getUnmapCharBuffer() {
105		return null;
106	}
107
108	CharBuffer getExceptionCharBuffer() {
109		return null;
110	}
111
112	public void testIsLegalReplacementEmptyArray() {
113		assertTrue(encoder.isLegalReplacement(new byte[0]));
114	}
115
116	protected byte[] getIllegalByteArray() {
117		return new byte[] { 0x00 };
118	}
119
120	protected byte[] getLegalByteArray() {
121		// FIXME: Different Here!
122		// return new byte[]{(byte)0xd8, 0x00};
123		return new byte[] { (byte) 0x00, (byte) 0xd8 };
124	}
125
126	void assertByteArray(ByteBuffer out, byte[] expected) {
127		out = out.duplicate();
128		if (out.position() > 0) {
129			out.flip();
130		}
131		try {
132			assertEquals(decoder.decode(out), decoder.decode(ByteBuffer
133					.wrap(expected)));
134		} catch (CharacterCodingException e) {
135			fail(e.toString());
136		}
137	}
138}
139