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 org.apache.harmony.luni.tests.java.io;
19
20import java.io.CharArrayReader;
21import java.io.IOException;
22
23public class CharArrayReaderTest extends junit.framework.TestCase {
24
25    char[] hw = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
26
27    CharArrayReader cr;
28
29    /**
30     * @tests java.io.CharArrayReader#CharArrayReader(char[])
31     */
32    public void test_Constructor$C() throws IOException {
33        cr = new CharArrayReader(hw);
34        assertTrue("Failed to create reader", cr.ready());
35    }
36
37    /**
38     * @tests java.io.CharArrayReader#CharArrayReader(char[], int, int)
39     */
40    public void test_Constructor$CII() throws IOException {
41        cr = new CharArrayReader(hw, 5, 5);
42        assertTrue("Failed to create reader", cr.ready());
43
44        int c = cr.read();
45        assertTrue("Created incorrect reader--returned '" + (char) c
46                + "' intsead of 'W'", c == 'W');
47    }
48
49    /**
50     * @tests java.io.CharArrayReader#close()
51     */
52    public void test_close() {
53        cr = new CharArrayReader(hw);
54        cr.close();
55        try {
56            cr.read();
57            fail("Failed to throw exception on read from closed stream");
58        } catch (IOException e) {
59            // Expected
60        }
61
62        // No-op
63        cr.close();
64    }
65
66    /**
67     * @tests java.io.CharArrayReader#mark(int)
68     */
69    public void test_markI() throws IOException {
70        cr = new CharArrayReader(hw);
71        cr.skip(5L);
72        cr.mark(100);
73        cr.read();
74        cr.reset();
75        assertEquals("Failed to mark correct position", 'W', cr.read());
76    }
77
78    /**
79     * @tests java.io.CharArrayReader#markSupported()
80     */
81    public void test_markSupported() {
82        cr = new CharArrayReader(hw);
83        assertTrue("markSupported returned false", cr.markSupported());
84    }
85
86    /**
87     * @tests java.io.CharArrayReader#read()
88     */
89    public void test_read() throws IOException {
90        cr = new CharArrayReader(hw);
91        assertEquals("Read returned incorrect char", 'H', cr.read());
92        cr = new CharArrayReader(new char[] { '\u8765' });
93        assertTrue("Incorrect double byte char", cr.read() == '\u8765');
94    }
95
96    /**
97     * @tests java.io.CharArrayReader#read(char[], int, int)
98     */
99    public void test_read$CII() throws IOException {
100        char[] c = new char[11];
101        cr = new CharArrayReader(hw);
102        cr.read(c, 1, 10);
103        assertTrue("Read returned incorrect chars", new String(c, 1, 10)
104                .equals(new String(hw, 0, 10)));
105    }
106
107    /**
108     * @tests java.io.CharArrayReader#ready()
109     */
110    public void test_ready() throws IOException {
111        cr = new CharArrayReader(hw);
112        assertTrue("ready returned false", cr.ready());
113        cr.skip(1000);
114        assertTrue("ready returned true", !cr.ready());
115        cr.close();
116
117        try {
118            cr.ready();
119            fail("No exception 1");
120        } catch (IOException e) {
121            // expected
122        }
123        try {
124            cr = new CharArrayReader(hw);
125            cr.close();
126            cr.ready();
127            fail("No exception 2");
128        } catch (IOException e) {
129            // expected
130        }
131    }
132
133    /**
134     * @tests java.io.CharArrayReader#reset()
135     */
136    public void test_reset() throws IOException {
137        cr = new CharArrayReader(hw);
138        cr.skip(5L);
139        cr.mark(100);
140        cr.read();
141        cr.reset();
142        assertEquals("Reset failed to return to marker position", 'W', cr
143                .read());
144
145        // Regression for HARMONY-4357
146        String str = "offsetHello world!";
147        char[] data = new char[str.length()];
148        str.getChars(0, str.length(), data, 0);
149        int offsetLength = 6;
150        int length = data.length - offsetLength;
151
152        CharArrayReader reader = new CharArrayReader(data, offsetLength, length);
153        reader.reset();
154        for (int i = 0; i < length; i++) {
155            assertEquals(data[offsetLength + i], (char) reader.read());
156        }
157    }
158
159    /**
160     * @tests java.io.CharArrayReader#skip(long)
161     */
162    public void test_skipJ() throws IOException {
163        cr = new CharArrayReader(hw);
164        long skipped = cr.skip(5L);
165
166        assertEquals("Failed to skip correct number of chars", 5L, skipped);
167        assertEquals("Skip skipped wrong chars", 'W', cr.read());
168    }
169
170    /**
171     * Tears down the fixture, for example, close a network connection. This
172     * method is called after a test is executed.
173     */
174    protected void tearDown() {
175        if (cr != null)
176            cr.close();
177    }
178}
179