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 libcore.java.io;
18
19import java.io.IOException;
20import java.io.Reader;
21import java.nio.CharBuffer;
22
23import junit.framework.TestCase;
24import tests.support.Support_ASimpleReader;
25
26public class OldReaderTest extends TestCase {
27
28    public void test_Reader() {
29        MockReader r = new MockReader();
30        assertTrue("Test 1: Lock has not been set correctly.", r.lockSet(r));
31    }
32
33    public void test_Reader_CharBufferChar() throws IOException {
34        Support_ASimpleReader simple;
35        simple = new Support_ASimpleReader("Bla bla, what else?");
36        CharBuffer buf = CharBuffer.allocate(4);
37        assertEquals("Wrong return value!", 4, simple.read(buf));
38        buf.rewind();
39        assertEquals("Wrong stuff read!", "Bla ", String.valueOf(buf));
40        simple.read(buf);
41        buf.rewind();
42        assertEquals("Wrong stuff read!", "bla,", String.valueOf(buf));
43        simple.throwExceptionOnNextUse = true;
44        try {
45            simple.read(buf);
46            fail("IOException not thrown!");
47        } catch (IOException expected) {
48        }
49    }
50
51    public void test_Read_$C() throws IOException {
52        Support_ASimpleReader simple;
53        simple = new Support_ASimpleReader("Bla bla, what else?");
54        char[] buf = new char[4];
55        assertEquals("Wrong return value!", 4, simple.read(buf));
56        assertEquals("Wrong stuff read!", "Bla ", new String(buf));
57        simple.read(buf);
58        assertEquals("Wrong stuff read!", "bla,", new String(buf));
59        simple.throwExceptionOnNextUse = true;
60        try {
61            simple.read(buf);
62            fail("IOException not thrown!");
63        } catch (IOException expected) {
64        }
65    }
66
67
68    public void test_markSupported() {
69        assertFalse("markSupported must return false", new MockReader().markSupported());
70    }
71
72    public void test_read() throws IOException {
73        Support_ASimpleReader simple = new Support_ASimpleReader("Bla bla, what else?");
74        int res = simple.read();
75        assertEquals("Wrong stuff read!", 'B', res);
76        res = simple.read();
77        assertEquals("Wrong stuff read!", 'l', res);
78        simple.throwExceptionOnNextUse = true;
79        try {
80            simple.read();
81            fail("IOException not thrown!");
82        } catch (IOException expected) {
83        }
84    }
85
86    public void test_ready() throws IOException {
87        Support_ASimpleReader simple = new Support_ASimpleReader("Bla bla, what else?");
88        simple.throwExceptionOnNextUse = true;
89        try {
90            simple.ready();
91            fail("IOException not thrown!");
92        } catch (IOException expected) {
93        }
94    }
95
96    public void test_skip() throws IOException {
97        Support_ASimpleReader simple = new Support_ASimpleReader("Bla bla, what else?");
98        char[] buf = new char[4];
99        simple.read(buf);
100        assertEquals("Wrong stuff read!", "Bla ", new String(buf));
101        simple.skip(5);
102        simple.read(buf);
103        assertEquals("Wrong stuff read!", "what", new String(buf));
104        simple.throwExceptionOnNextUse = true;
105        try {
106            simple.skip(1);
107            fail("IOException not thrown!");
108        } catch (IOException expected) {
109        }
110    }
111
112    class MockReader extends Reader {
113
114        @Override public void close() {}
115
116        @Override public int read(char[] buf, int offset, int count) {
117            throw new UnsupportedOperationException();
118        }
119
120        public boolean lockSet(Object o) {
121            return (lock == o);
122        }
123    }
124}
125