AbstractCharsetTestCase.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
1package tests.api.java.nio.charset;
2
3import dalvik.annotation.TestTargetClass;
4import dalvik.annotation.TestTargets;
5import dalvik.annotation.TestTargetNew;
6import dalvik.annotation.TestLevel;
7
8import java.nio.ByteBuffer;
9import java.nio.CharBuffer;
10import java.nio.charset.Charset;
11
12import junit.framework.TestCase;
13
14@TestTargetClass(Charset.class)
15
16/**
17 * Super class for concrete charset test suites.
18 */
19public abstract class AbstractCharsetTestCase extends TestCase {
20
21    // the canonical name of this charset
22    protected final String canonicalName;
23
24    // the aliases set
25    protected final String[] aliases;
26
27    // canEncode
28    protected final boolean canEncode;
29
30    // isRegistered
31    protected final boolean isRegistered;
32
33    // charset instance
34    protected Charset testingCharset;
35
36    /*
37     * Initialize the field "testingCharset" here.
38     *
39     * @see TestCase#setUp()
40     */
41    protected void setUp() throws Exception {
42        super.setUp();
43        this.testingCharset = Charset.forName(this.canonicalName);
44    }
45
46    /*
47     * @see TestCase#tearDown()
48     */
49    protected void tearDown() throws Exception {
50        super.tearDown();
51    }
52
53    /**
54     * Constructor for ConcreteCharsetTest.
55     *
56     */
57    public AbstractCharsetTestCase(String arg0, String canonicalName,
58            String[] aliases, boolean canEncode, boolean isRegistered) {
59        super(arg0);
60        this.canonicalName = canonicalName;
61        this.canEncode = canEncode;
62        this.isRegistered = isRegistered;
63        this.aliases = aliases;
64    }
65
66    /*
67     * Test canEncode.
68     */
69    @TestTargetNew(
70        level = TestLevel.COMPLETE,
71        method = "canEncode",
72        args = {}
73    )
74    public void testCanEncode() {
75        assertEquals(this.canEncode, this.testingCharset.canEncode());
76    }
77
78    /*
79     * Test isRegistered.
80     */
81    @TestTargetNew(
82        level = TestLevel.COMPLETE,
83        method = "isRegistered",
84        args = {}
85    )
86    public void testIsRegistered() {
87        assertEquals(this.isRegistered, this.testingCharset.isRegistered());
88    }
89
90    /*
91     * Test name.
92     */
93    @TestTargetNew(
94        level = TestLevel.COMPLETE,
95        notes = "",
96        method = "name",
97        args = {}
98    )
99    public void testName() {
100        assertEquals(this.canonicalName, this.testingCharset.name());
101        // assertEquals(this.canonicalName, this.testingCharset.displayName());
102        // assertEquals(this.canonicalName,
103        // this.testingCharset.displayName(null));
104    }
105
106    /*
107     * Test aliases.
108     */
109    @TestTargetNew(
110        level = TestLevel.PARTIAL,
111        notes = "Test functionality completely missed.",
112        method = "aliases",
113        args = {}
114    )
115    public void testAliases() {
116        for (int i = 0; i < this.aliases.length; i++) {
117            Charset c = Charset.forName(this.aliases[i]);
118            assertEquals(this.canonicalName, c.name());
119            // TODO
120            // assertTrue(this.testingCharset.aliases().contains(this.aliases[i]));
121        }
122    }
123
124    /*
125     * Test the method encode(String) with null.
126     */
127    @TestTargetNew(
128        level = TestLevel.PARTIAL,
129        notes = "",
130        method = "encode",
131        args = {java.lang.String.class}
132    )
133    public void testEncode_String_Null() {
134        try {
135            this.testingCharset.encode((String) null);
136            fail("Should throw NullPointerException!");
137        } catch (NullPointerException e) {
138            // expected
139        }
140    }
141
142    /*
143     * Test the method encode(CharBuffer) with null.
144     */
145    @TestTargetNew(
146        level = TestLevel.PARTIAL,
147        notes = "",
148        method = "encode",
149        args = {java.nio.CharBuffer.class}
150    )
151    public void testEncode_CharBuffer_Null() {
152        try {
153            this.testingCharset.encode((CharBuffer) null);
154            fail("Should throw NullPointerException!");
155        } catch (NullPointerException e) {
156            // expected
157        }
158    }
159
160    /*
161     * Test encoding.
162     */
163    protected void internalTestEncode(String input, byte[] output) {
164        ByteBuffer bb = this.testingCharset.encode(input);
165        int i = 0;
166        bb.rewind();
167        while (bb.hasRemaining() && i < output.length) {
168            assertEquals(output[i], bb.get());
169            i++;
170        }
171        assertFalse(bb.hasRemaining());
172        assertEquals(output.length, i);
173    }
174
175    /*
176     * Test encoding.
177     */
178    @TestTargetNew(
179        level = TestLevel.COMPLETE,
180        method = "newEncoder",
181        args = {}
182    )
183    public abstract void testEncode_Normal();
184
185    /*
186     * Test decoding.
187     */
188    protected void internalTestDecode(byte[] input, char[] output) {
189        CharBuffer chb = this.testingCharset.decode(ByteBuffer.wrap(input));
190        int i = 0;
191        chb.rewind();
192        while (chb.hasRemaining() && i < output.length) {
193            assertEquals(output[i], chb.get());
194            i++;
195        }
196        assertFalse(chb.hasRemaining());
197        assertEquals(output.length, i);
198    }
199
200    /*
201     * Test decoding.
202     */
203    @TestTargetNew(
204        level = TestLevel.COMPLETE,
205        method = "newDecoder",
206        args = {}
207    )
208    public abstract void testDecode_Normal();
209}
210