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 org.apache.harmony.luni.tests.java.io;
18
19import java.io.BufferedReader;
20import java.io.CharArrayReader;
21import java.io.IOException;
22import java.io.StringReader;
23
24import junit.framework.TestCase;
25import dalvik.annotation.TestLevel;
26import dalvik.annotation.TestTargetClass;
27import dalvik.annotation.TestTargetNew;
28@TestTargetClass(BufferedReader.class)
29public class BufferedReaderTest extends TestCase {
30
31    /**
32     * @tests java.io.BufferedReader#read(char[], int, int)
33     */
34    @TestTargetNew(
35        level = TestLevel.PARTIAL_COMPLETE,
36        notes = "Checks exceptions.",
37        method = "read",
38        args = {char[].class, int.class, int.class}
39    )
40    public void test_read$CII() throws IOException {
41        char[] in = {'L', 'o', 'r', 'e', 'm'};
42        char[] ch = new char[3];
43        BufferedReader reader = new BufferedReader(new CharArrayReader(in));
44
45        try {
46            reader.read(null, 1, 0);
47            fail("Test 1: NullPointerException expected.");
48        } catch (NullPointerException e) {
49            // Expected.
50        }
51
52        try {
53            reader.read(ch , -1, 1);
54            fail("Test 2: IndexOutOfBoundsException expected.");
55        } catch (IndexOutOfBoundsException e) {
56            // Expected.
57        }
58
59        try {
60            reader.read(ch , 1, -1);
61            fail("Test 3: IndexOutOfBoundsException expected.");
62        } catch (IndexOutOfBoundsException e) {
63            // Expected.
64        }
65
66        try {
67            reader.read(ch, 1, 3);
68            fail("Test 4: IndexOutOfBoundsException expected.");
69        } catch (IndexOutOfBoundsException e) {
70            // Expected.
71        }
72
73        reader.close();
74        try {
75            reader.read(ch, 1, 1);
76            fail("Test 5: IOException expected.");
77        } catch (IOException e) {
78            // Expected.
79        }
80    }
81
82    /**
83     * @tests java.io.BufferedReader#mark(int)
84     */
85    @TestTargetNew(
86        level = TestLevel.COMPLETE,
87        method = "mark",
88        args = {int.class}
89    )
90    public void test_markI() throws IOException {
91        BufferedReader buf = new BufferedReader(new StringReader("01234"), 2);
92
93        try {
94            buf.mark(-1);
95            fail("Test 1: IllegalArgumentException expected.");
96        } catch (IllegalArgumentException e) {
97            // Expected.
98        }
99
100        buf.mark(3);
101        char[] chars = new char[3];
102        int result = buf.read(chars);
103        assertEquals(3, result);
104        assertEquals("Assert 0:", '0', chars[0]);
105        assertEquals("Assert 1:", '1', chars[1]);
106        assertEquals("Assert 2:", '2', chars[2]);
107        assertEquals("Assert 3:", '3', buf.read());
108
109        buf = new BufferedReader(new StringReader("01234"), 2);
110        buf.mark(3);
111        chars = new char[4];
112        result = buf.read(chars);
113        assertEquals("Assert 4:", 4, result);
114        assertEquals("Assert 5:", '0', chars[0]);
115        assertEquals("Assert 6:", '1', chars[1]);
116        assertEquals("Assert 7:", '2', chars[2]);
117        assertEquals("Assert 8:", '3', chars[3]);
118        assertEquals("Assert 9:", '4', buf.read());
119        assertEquals("Assert 10:", -1, buf.read());
120
121        BufferedReader reader = new BufferedReader(new StringReader("01234"));
122        reader.mark(Integer.MAX_VALUE);
123        reader.read();
124        reader.close();
125
126        try {
127            reader.mark(1);
128            fail("Test 2: IOException expected.");
129        } catch (IOException e) {
130            // Expected.
131        }
132    }
133
134}
135