CharArrayReaderTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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.io;
19
20import dalvik.annotation.TestInfo;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTarget;
23import dalvik.annotation.TestTargetClass;
24
25import java.io.CharArrayReader;
26import java.io.IOException;
27
28@TestTargetClass(CharArrayReader.class)
29public class CharArrayReaderTest extends junit.framework.TestCase {
30
31    char[] hw = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
32
33    CharArrayReader cr;
34
35    /**
36     * @tests java.io.CharArrayReader#CharArrayReader(char[])
37     */
38    @TestInfo(
39            level = TestLevel.COMPLETE,
40            purpose = "Verifies CharArrayReader(char[] buf) constructor.",
41            targets = { @TestTarget(methodName = "CharArrayReader",
42                                    methodArgs = {char[].class})
43            }
44    )
45    public void test_Constructor$C() {
46        // Test for method java.io.CharArrayReader(char [])
47
48        try {
49            cr = new CharArrayReader(hw);
50            assertTrue("Failed to create reader", cr.ready());
51        } catch (IOException e) {
52            fail("Exception determining ready state : " + e.getMessage());
53        }
54    }
55
56    /**
57     * @tests java.io.CharArrayReader#CharArrayReader(char[], int, int)
58     */
59    @TestInfo(
60            level = TestLevel.PARTIAL,
61            purpose = "IllegalArgumentException checking missed.",
62            targets = { @TestTarget(methodName = "CharArrayReader",
63                                    methodArgs = {char[].class, int.class, int.class})
64            }
65    )
66    public void test_Constructor$CII() {
67        // Test for method java.io.CharArrayReader(char [], int, int)
68        try {
69            cr = new CharArrayReader(hw, 5, 5);
70            assertTrue("Failed to create reader", cr.ready());
71        } catch (IOException e) {
72            fail("Exception determining ready state : " + e.getMessage());
73        }
74        try {
75            int c = cr.read();
76            assertTrue("Created incorrect reader--returned '" + (char) c
77                    + "' intsead of 'W'", c == 'W');
78        } catch (IOException e) {
79            fail("Exception reading from new reader : " + e.getMessage());
80        }
81    }
82
83    /**
84     * @tests java.io.CharArrayReader#close()
85     */
86    @TestInfo(
87            level = TestLevel.COMPLETE,
88            purpose = "Verifies close() method.",
89            targets = { @TestTarget(methodName = "close",
90                                    methodArgs = {})
91            }
92    )
93    public void test_close() {
94        // Test for method void java.io.CharArrayReader.close()
95        cr = new CharArrayReader(hw);
96        cr.close();
97        try {
98            cr.read();
99            fail("Failed to throw exception on reqad from closed stream");
100        } catch (IOException e) { // Correct
101        }
102
103    }
104
105    /**
106     * @tests java.io.CharArrayReader#mark(int)
107     */
108    @TestInfo(
109            level = TestLevel.PARTIAL,
110            purpose = "IOException checking missed.",
111            targets = { @TestTarget(methodName = "mark",
112                                    methodArgs = { int.class })
113            }
114    )
115    public void test_markI() {
116        // Test for method void java.io.CharArrayReader.mark(int)
117        try {
118            cr = new CharArrayReader(hw);
119            cr.skip(5L);
120            cr.mark(100);
121            cr.read();
122            cr.reset();
123            assertEquals("Failed to mark correct position", 'W', cr.read());
124        } catch (IOException e) {
125            fail("Exception during mark test: " + e.getMessage());
126        }
127    }
128
129    /**
130     * @tests java.io.CharArrayReader#markSupported()
131     */
132    @TestInfo(
133            level = TestLevel.COMPLETE,
134            purpose = "Verifies markSupported() method.",
135            targets = { @TestTarget(methodName = "markSupported",
136                                    methodArgs = {})
137            }
138    )
139    public void test_markSupported() {
140        // Test for method boolean java.io.CharArrayReader.markSupported()
141        cr = new CharArrayReader(hw);
142        assertTrue("markSupported returned false", cr.markSupported());
143    }
144
145    /**
146     * @tests java.io.CharArrayReader#read()
147     */
148    @TestInfo(
149            level = TestLevel.PARTIAL,
150            purpose = "IOException checking missed.",
151            targets = { @TestTarget(methodName = "read",
152                                    methodArgs = {})
153            }
154    )
155    public void test_read() {
156        // Test for method int java.io.CharArrayReader.read()
157        try {
158            cr = new CharArrayReader(hw);
159            assertEquals("Read returned incorrect char", 'H', cr.read());
160            cr = new CharArrayReader(new char[] { '\u8765' });
161            assertTrue("Incorrect double byte char", cr.read() == '\u8765');
162        } catch (IOException e) {
163            fail("Exception during read test: " + e.getMessage());
164        }
165    }
166
167    /**
168     * @tests java.io.CharArrayReader#read(char[], int, int)
169     */
170    @TestInfo(
171            level = TestLevel.PARTIAL,
172            purpose = "IOException checking missed.",
173            targets = { @TestTarget(methodName = "read",
174                                    methodArgs = {char[].class, int.class, int.class})
175            }
176    )
177    public void test_read$CII() {
178        // Test for method int java.io.CharArrayReader.read(char [], int, int)
179        char[] c = new char[11];
180        try {
181            cr = new CharArrayReader(hw);
182            cr.read(c, 1, 10);
183            assertTrue("Read returned incorrect chars", new String(c, 1, 10)
184                    .equals(new String(hw, 0, 10)));
185        } catch (IOException e) {
186            fail("Exception during read test : " + e.getMessage());
187        }
188    }
189
190    /**
191     * @tests java.io.CharArrayReader#ready()
192     */
193    @TestInfo(
194            level = TestLevel.COMPLETE,
195            purpose = "Verifies ready() method.",
196            targets = { @TestTarget(methodName = "ready",
197                                    methodArgs = {})
198            }
199    )
200    public void test_ready() {
201        // Test for method boolean java.io.CharArrayReader.ready()
202        cr = new CharArrayReader(hw);
203        boolean expectException = false;
204        try {
205            assertTrue("ready returned false", cr.ready());
206            cr.skip(1000);
207            assertTrue("ready returned true", !cr.ready());
208            cr.close();
209            expectException = true;
210            cr.ready();
211            fail("No exception 1");
212        } catch (IOException e) {
213            if (!expectException)
214                fail("Unexpected: " + e);
215        }
216        try {
217            cr = new CharArrayReader(hw);
218            cr.close();
219            cr.ready();
220            fail("No exception 2");
221        } catch (IOException e) {
222        }
223
224    }
225
226    /**
227     * @tests java.io.CharArrayReader#reset()
228     */
229    @TestInfo(
230            level = TestLevel.PARTIAL,
231            purpose = "IOException checking missed.",
232            targets = { @TestTarget(methodName = "reset",
233                                    methodArgs = {})
234            }
235    )
236    public void test_reset() {
237        // Test for method void java.io.CharArrayReader.reset()
238        try {
239            cr = new CharArrayReader(hw);
240            cr.skip(5L);
241            cr.mark(100);
242            cr.read();
243            cr.reset();
244            assertEquals("Reset failed to return to marker position",
245                    'W', cr.read());
246        } catch (IOException e) {
247            fail("Exception during reset test : " + e.getMessage());
248        }
249    }
250
251    /**
252     * @tests java.io.CharArrayReader#skip(long)
253     */
254    @TestInfo(
255            level = TestLevel.PARTIAL,
256            purpose = "IOException checking missed.",
257            targets = { @TestTarget(methodName = "skip",
258                                    methodArgs = {long.class})
259            }
260    )
261    public void test_skipJ() {
262        // Test for method long java.io.CharArrayReader.skip(long)
263        long skipped = 0;
264        try {
265            cr = new CharArrayReader(hw);
266            skipped = cr.skip(5L);
267        } catch (IOException e) {
268            fail("Exception during skip test : " + e.getMessage());
269        }
270        assertEquals("Failed to skip correct number of chars", 5L, skipped);
271        try {
272            assertEquals("Skip skipped wrong chars", 'W', cr.read());
273        } catch (IOException e) {
274            fail("read exception during skip test : " + e.getMessage());
275        }
276    }
277
278    /**
279     * Sets up the fixture, for example, open a network connection. This method
280     * is called before a test is executed.
281     */
282    protected void setUp() {
283    }
284
285    /**
286     * Tears down the fixture, for example, close a network connection. This
287     * method is called after a test is executed.
288     */
289    protected void tearDown() {
290        if (cr != null)
291            cr.close();
292    }
293}
294