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 libcore.java.io;
19
20import java.io.IOException;
21import java.io.InputStream;
22
23public class OldInputStreamTest extends junit.framework.TestCase {
24
25    public static final String testString = "Lorem ipsum dolor sit amet,\n" +
26        "consectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut" +
27        "labore et dolore magna aliqua.\n";
28
29    private InputStream is;
30
31    class MockInputStream extends java.io.InputStream {
32
33        private byte[] input;
34        private int position;
35
36        public MockInputStream() {
37            super();
38            input = testString.getBytes();
39            position = 0;
40        }
41
42        public int read() throws IOException {
43            int result = -1;
44            if (position < input.length) {
45                result = input[position];
46                position++;
47            }
48            return result;
49        }
50    }
51
52    public void test_Constructor() {
53        try {
54            InputStream myIS = new MockInputStream();
55            myIS.close();
56        } catch (Exception e) {
57            fail("Unexpected exception: " + e.getMessage());
58        }
59    }
60
61    public void test_available() throws IOException {
62        assertEquals(is.available(), 0);
63    }
64
65    public void test_close() {
66        try {
67            is.close();
68        } catch (Exception e) {
69            fail("Unexpected exception: " + e.getMessage());
70        }
71    }
72
73    public void test_markI() {
74        try {
75            is.mark(10);
76        } catch (Exception e) {
77            fail("Unexpected exception: " + e.getMessage());
78        }
79    }
80
81    public void test_markSupported() throws IOException {
82        assertFalse("markSupported() has returned the wrong default value.",
83                is.markSupported());
84    }
85
86    public void test_read$B() throws IOException {
87        byte[] b = new byte[10];
88        byte[] ref = testString.getBytes();
89        boolean equal = true;
90        int bytesRead = 0;
91        int i;
92
93        // Test 1: This read operation should complete without an error.
94        assertEquals("Test 1: Incorrect count of bytes read.",
95                is.read(b), 10);
96
97        for (i = 0; i < 10; i++) {
98            equal &= (b[i] == ref[i]);
99        }
100        assertTrue("Test 1: Wrong bytes read.", equal);
101
102        // Test 2: Test that the correct number of bytes read is returned
103        // if the source has less bytes available than fit in the buffer.
104        b = new byte[ref.length];
105        bytesRead = is.read(b);
106        assertEquals("Test 2: Incorrect count of bytes read.",
107                bytesRead, ref.length - 10);
108
109        for (i = 0; i < bytesRead; i++) {
110            equal &= (b[i] == ref[i + 10]);
111        }
112        assertTrue("Test 2: Wrong bytes read.", equal);
113
114        // Test 3: Since we have now reached the end of the source,
115        // the next call of read(byte[]) should return -1.
116        bytesRead = is.read(b);
117        assertEquals("Test 3:", bytesRead, -1);
118    }
119
120    public void test_read$BII_Exception() throws IOException {
121        byte[] b = new byte[10];
122        int bytesRead = 0;
123
124        // Test 1: Invalid offset.
125        try {
126            bytesRead = is.read(b, -1, 5);
127            fail("Test 1: IndexOutOfBoundsException expected.");
128        } catch (IndexOutOfBoundsException e) {
129            // expected
130        }
131
132        // Test 2: Invalid length.
133        try {
134            bytesRead = is.read(b, 5, -1);
135            fail("Test 2: IndexOutOfBoundsException expected.");
136        } catch (IndexOutOfBoundsException e) {
137            // expected
138        }
139
140        // Test 3: Invalid offset and length combination (sum is larger
141        // than the length of b).
142        try {
143            bytesRead = is.read(b, 6, 5);
144            fail("Test 3: IndexOutOfBoundsException expected.");
145        } catch (IndexOutOfBoundsException e) {
146            // expected
147        }
148
149        // Test 4: Border case.
150        try {
151            bytesRead = is.read(b, 6, 4);
152        } catch (IndexOutOfBoundsException e) {
153            fail("Test 4: Unexpected IndexOutOfBoundsException.");
154        }
155        assertEquals("Test 4:", bytesRead, 4);
156
157        // Test 5: Border case.
158        try {
159            bytesRead = is.read(b, 6, 0);
160        } catch (Exception e) {
161            fail("Test 5: Unexpected Exception " + e.getMessage());
162        }
163        assertEquals("Test 5:", bytesRead, 0);
164    }
165
166    public void test_read$BII() throws IOException {
167        byte[] b = new byte[10];
168        byte[] ref = testString.getBytes();
169        boolean equal = true;
170        int bytesRead = 0;
171        int i;
172
173        // Test 1: This read operation should complete without an error.
174        assertEquals("Test 1: Incorrect count of bytes read.",
175                is.read(b, 0, 5), 5);
176
177        for (i = 0; i < 5; i++) {
178            equal &= (b[i] == ref[i]);
179        }
180        assertTrue("Test 1: Wrong bytes read.", equal);
181
182        // Test 2: Read operation with offset.
183        assertEquals("Test 2: Incorrect count of bytes read.",
184                is.read(b, 5, 3), 3);
185
186        for (i = 5; i < 8; i++) {
187            equal &= (b[i] == ref[i]);
188        }
189        assertTrue("Test 2: Wrong bytes read.", equal);
190
191        // Test 3: Test that the correct number of bytes read is returned
192        // if the source has less bytes available than fit in the buffer.
193        b = new byte[ref.length];
194        bytesRead = is.read(b, 2, b.length - 2);
195
196        // 8 bytes have been read during tests 1 and 2.
197        assertEquals("Test 3: Incorrect count of bytes read.",
198                bytesRead, ref.length - 8);
199
200        // The first two bytes of b should be 0 because of the offset.
201        assertEquals("Test 3:", b[0], 0);
202        assertEquals("Test 3:", b[1], 0);
203        for (i = 0; i < bytesRead; i++) {
204            equal &= (b[i + 2] == ref[i + 8]);
205        }
206        assertTrue("Test 2: Wrong bytes read.", equal);
207
208        // Test 4: Since we have now reached the end of the source,
209        // the next call of read(byte[], int, int) should return -1.
210        assertEquals("Test 4:", is.read(b, 0, 2), -1);
211    }
212
213    public void test_reset() {
214        try {
215            is.reset();
216            fail("IOException expected.");
217        } catch (IOException e) {
218            // expected
219        }
220    }
221
222    public void test_skipL() throws IOException {
223        byte[] b = new byte[12];
224        byte[] ref = testString.getBytes();
225        int i;
226        boolean equal = true;
227
228        // Test 1: Check that skip(long) just returns 0 if called with a
229        // negative number.
230        assertEquals("Test 1:", is.skip(-42), 0);
231        assertEquals("Test 1: Incorrect count of bytes read.",
232                is.read(b), 12);
233        for (i = 0; i < 12; i++) {
234            equal &= (b[i] == ref[i]);
235        }
236        assertTrue("Test 1: Wrong bytes read.", equal);
237
238        // Test 2: Check if the number of bytes skipped matches the requested
239        // number of bytes to skip.
240        assertEquals("Test 2: Incorrect count of bytes skipped.",
241                is.skip(17), 17);
242
243        // Test 3: Check that bytes have actually been skipped
244        is.read(b, 0, 10);
245        for (i = 0; i < 10; i++) {
246            equal &= (b[i] == ref[i + 29]);
247        }
248        assertTrue("Test 3: Wrong bytes read.", equal);
249
250        // Test 4: Test that the correct number of bytes skipped is returned
251        // if the source has less bytes available than fit in the buffer.
252        assertEquals("Test 4: Incorrect count of bytes skipped.",
253                is.skip(ref.length), ref.length - 39);
254
255        // Test 5: Since we have now reached the end of the source,
256        // the next call of read(byte[]) should return -1 and calling
257        // skip(long) should return 0.
258        assertEquals("Test 5:", is.read(b), -1);
259        assertEquals("Test 5:", is.skip(10), 0);
260    }
261
262    protected void setUp() {
263        is = new MockInputStream();
264    }
265
266    protected void tearDown() {
267        try {
268            is.close();
269        } catch (Exception e) {
270        }
271    }
272}
273