1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.api.java.nio.charset;
19
20import java.nio.ByteBuffer;
21import java.nio.CharBuffer;
22import java.nio.charset.Charset;
23
24import junit.framework.TestCase;
25import dalvik.annotation.TestLevel;
26import dalvik.annotation.TestTargetClass;
27import dalvik.annotation.TestTargetNew;
28
29@TestTargetClass(Charset.class)
30
31/**
32 * Super class for concrete charset test suites.
33 */
34public abstract class AbstractCharsetTestCase extends TestCase {
35
36    // the canonical name of this charset
37    protected final String canonicalName;
38
39    // the aliases set
40    protected final String[] aliases;
41
42    // canEncode
43    protected final boolean canEncode;
44
45    // isRegistered
46    protected final boolean isRegistered;
47
48    // charset instance
49    protected Charset testingCharset;
50
51    /*
52     * Initialize the field "testingCharset" here.
53     *
54     * @see TestCase#setUp()
55     */
56    protected void setUp() throws Exception {
57        super.setUp();
58        this.testingCharset = Charset.forName(this.canonicalName);
59    }
60
61    /*
62     * @see TestCase#tearDown()
63     */
64    protected void tearDown() throws Exception {
65        super.tearDown();
66    }
67
68    /**
69     * Constructor for ConcreteCharsetTest.
70     *
71     */
72    public AbstractCharsetTestCase(String canonicalName,
73            String[] aliases, boolean canEncode, boolean isRegistered) {
74        this.canonicalName = canonicalName;
75        this.canEncode = canEncode;
76        this.isRegistered = isRegistered;
77        this.aliases = aliases;
78    }
79
80    /*
81     * Test canEncode.
82     */
83    @TestTargetNew(
84        level = TestLevel.COMPLETE,
85        method = "canEncode",
86        args = {}
87    )
88    public void testCanEncode() {
89        assertEquals(this.canEncode, this.testingCharset.canEncode());
90    }
91
92    /*
93     * Test isRegistered.
94     */
95    @TestTargetNew(
96        level = TestLevel.COMPLETE,
97        method = "isRegistered",
98        args = {}
99    )
100    public void testIsRegistered() {
101        assertEquals(this.isRegistered, this.testingCharset.isRegistered());
102    }
103
104    /*
105     * Test name.
106     */
107    @TestTargetNew(
108        level = TestLevel.COMPLETE,
109        notes = "",
110        method = "name",
111        args = {}
112    )
113    public void testName() {
114        assertEquals(this.canonicalName, this.testingCharset.name());
115        // assertEquals(this.canonicalName, this.testingCharset.displayName());
116        // assertEquals(this.canonicalName,
117        // this.testingCharset.displayName(null));
118    }
119
120    /*
121     * Test aliases.
122     */
123    @TestTargetNew(
124        level = TestLevel.PARTIAL,
125        notes = "Test functionality completely missed.",
126        method = "aliases",
127        args = {}
128    )
129    public void testAliases() {
130        for (int i = 0; i < this.aliases.length; i++) {
131            Charset c = Charset.forName(this.aliases[i]);
132            assertEquals(this.canonicalName, c.name());
133            // TODO
134            // assertTrue(this.testingCharset.aliases().contains(this.aliases[i]));
135        }
136    }
137
138    /*
139     * Test the method encode(String) with null.
140     */
141    @TestTargetNew(
142        level = TestLevel.PARTIAL,
143        notes = "",
144        method = "encode",
145        args = {java.lang.String.class}
146    )
147    public void testEncode_String_Null() {
148        try {
149            this.testingCharset.encode((String) null);
150            fail("Should throw NullPointerException!");
151        } catch (NullPointerException e) {
152            // expected
153        }
154    }
155
156    /*
157     * Test the method encode(CharBuffer) with null.
158     */
159    @TestTargetNew(
160        level = TestLevel.PARTIAL,
161        notes = "",
162        method = "encode",
163        args = {java.nio.CharBuffer.class}
164    )
165    public void testEncode_CharBuffer_Null() {
166        try {
167            this.testingCharset.encode((CharBuffer) null);
168            fail("Should throw NullPointerException!");
169        } catch (NullPointerException e) {
170            // expected
171        }
172    }
173
174    /*
175     * Test encoding.
176     */
177    protected void internalTestEncode(String input, byte[] output) {
178        ByteBuffer bb = this.testingCharset.encode(input);
179        int i = 0;
180        bb.rewind();
181        while (bb.hasRemaining() && i < output.length) {
182            assertEquals(output[i], bb.get());
183            i++;
184        }
185        assertFalse(bb.hasRemaining());
186        assertEquals(output.length, i);
187    }
188
189    /*
190     * Test encoding.
191     */
192    @TestTargetNew(
193        level = TestLevel.COMPLETE,
194        method = "newEncoder",
195        args = {}
196    )
197    public abstract void testEncode_Normal();
198
199    /*
200     * Test decoding.
201     */
202    protected void internalTestDecode(byte[] input, char[] output) {
203        CharBuffer chb = this.testingCharset.decode(ByteBuffer.wrap(input));
204        int i = 0;
205        chb.rewind();
206        while (chb.hasRemaining() && i < output.length) {
207            assertEquals(output[i], chb.get());
208            i++;
209        }
210        assertFalse(chb.hasRemaining());
211        assertEquals(output.length, i);
212    }
213
214    /*
215     * Test decoding.
216     */
217    @TestTargetNew(
218        level = TestLevel.COMPLETE,
219        method = "newDecoder",
220        args = {}
221    )
222    public abstract void testDecode_Normal();
223}
224