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.BufferedInputStream;
21import java.io.FilterInputStream;
22import java.io.IOException;
23import java.util.Arrays;
24import tests.support.Support_ASimpleInputStream;
25import tests.support.Support_PlatformFile;
26
27public class OldFilterInputStreamTest extends junit.framework.TestCase {
28
29    static class MyFilterInputStream extends java.io.FilterInputStream {
30        public MyFilterInputStream(java.io.InputStream is) {
31            super(is);
32        }
33    }
34
35    private String fileName;
36
37    private FilterInputStream is;
38
39    byte[] ibuf = new byte[4096];
40
41    private static final String testString = "Lorem ipsum dolor sit amet,\n" +
42    "consectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut" +
43    "labore et dolore magna aliqua.\n";
44
45    private static final int testLength = testString.length();
46
47    public void test_Constructor() {
48        // The FilterInputStream object has already been created in setUp().
49        // If anything has gone wrong, closing it should throw a
50        // NullPointerException.
51        try {
52            is.close();
53        } catch (IOException e) {
54            fail("Unexpected IOException: " + e.getMessage());
55        } catch (NullPointerException npe) {
56            fail("Unexpected NullPointerException.");
57        }
58    }
59
60    public void test_available() throws IOException {
61        assertEquals("Test 1: Returned incorrect number of available bytes;",
62                testLength, is.available());
63
64        is.close();
65        try {
66            is.available();
67            fail("Test 2: IOException expected.");
68        } catch (IOException e) {
69            // Expected.
70        }
71    }
72
73    public void test_close() throws IOException {
74        is.close();
75
76        try {
77            is.read();
78            fail("Test 1: Read from closed stream succeeded.");
79        } catch (IOException e) {
80            // Expected.
81        }
82
83        Support_ASimpleInputStream sis = new Support_ASimpleInputStream(true);
84        is = new MyFilterInputStream(sis);
85        try {
86            is.close();
87            fail("Test 2: IOException expected.");
88        } catch (IOException e) {
89            // Expected.
90        }
91        sis.throwExceptionOnNextUse = false;
92    }
93
94    public void test_markI() throws Exception {
95        // Test for method void java.io.FilterInputStream.mark(int)
96        final int bufSize = 10;
97        byte[] buf1 = new byte[bufSize];
98        byte[] buf2 = new byte[bufSize];
99
100        // Purpose 1: Check that mark() does nothing if the filtered stream
101        // is a FileInputStream.
102        is.read(buf1, 0, bufSize);
103        is.mark(2 * bufSize);
104        is.read(buf1, 0, bufSize);
105        try {
106            is.reset();
107        } catch (IOException e) {
108            // Expected
109        }
110        is.read(buf2, 0, bufSize);
111        assertFalse("Test 1: mark() should have no effect.",
112                Arrays.equals(buf1, buf2));
113        is.close();
114
115        // Purpose 2: Check that mark() in combination with reset() works if
116        // the filtered stream is a BufferedInputStream.
117        is = new MyFilterInputStream(new BufferedInputStream(
118                new java.io.FileInputStream(fileName), 100));
119        is.read(buf1, 0, bufSize);
120        is.mark(2 * bufSize);
121        is.read(buf1, 0, bufSize);
122        is.reset();
123        is.read(buf2, 0, bufSize);
124        assertTrue("Test 2: mark() or reset() has failed.",
125                Arrays.equals(buf1, buf2));
126    }
127
128    public void test_markSupported() throws Exception {
129        // Test for method boolean java.io.FilterInputStream.markSupported()
130
131        // Test 1: Check that markSupported() returns false for a filtered
132        // input stream that is known to not support mark().
133        assertFalse("Test 1: markSupported() incorrectly returned true " +
134                "for a FileInputStream.", is.markSupported());
135        is.close();
136        // Test 2: Check that markSupported() returns true for a filtered
137        // input stream that is known to support mark().
138        is = new MyFilterInputStream(new BufferedInputStream(
139                new java.io.FileInputStream(fileName), 100));
140        assertTrue("Test 2: markSupported() incorrectly returned false " +
141                "for a BufferedInputStream.", is.markSupported());
142    }
143
144    public void test_read() throws IOException {
145        int c = is.read();
146        assertEquals("Test 1: Read returned incorrect char;",
147                testString.charAt(0), c);
148
149        is.close();
150        try {
151            is.read();
152            fail("Test 2: IOException expected.");
153        } catch (IOException e) {
154            // Expected.
155        }
156    }
157
158    public void test_read$B() throws IOException {
159        // Test for method int java.io.FilterInputStream.read(byte [])
160        byte[] buf1 = new byte[100];
161        is.read(buf1);
162        assertTrue("Test 1: Failed to read correct data.",
163                new String(buf1, 0, buf1.length).equals(
164                        testString.substring(0, 100)));
165
166        is.close();
167        try {
168            is.read(buf1);
169            fail("Test 2: IOException expected.");
170        } catch (IOException e) {
171            // Expected.
172        }
173    }
174
175    public void test_read$BII_Exception() throws IOException {
176        byte[] buf = null;
177        try {
178            is.read(buf, -1, 0);
179            fail("Test 1: NullPointerException expected.");
180        } catch (NullPointerException e) {
181            // Expected.
182        }
183
184        buf = new byte[1000];
185        try {
186            is.read(buf, -1, 0);
187            fail("Test 2: IndexOutOfBoundsException expected.");
188        } catch (IndexOutOfBoundsException e) {
189            // Expected.
190        }
191
192        try {
193            is.read(buf, 0, -1);
194            fail("Test 3: IndexOutOfBoundsException expected.");
195        } catch (IndexOutOfBoundsException e) {
196            // Expected.
197        }
198
199        try {
200            is.read(buf, -1, -1);
201            fail("Test 4: IndexOutOfBoundsException expected.");
202        } catch (IndexOutOfBoundsException e) {
203            // Expected.
204        }
205
206        try {
207            is.read(buf, 0, 1001);
208            fail("Test 5: IndexOutOfBoundsException expected.");
209        } catch (IndexOutOfBoundsException e) {
210            // Expected.
211        }
212
213        try {
214            is.read(buf, 1001, 0);
215            fail("Test 6: IndexOutOfBoundsException expected.");
216        } catch (IndexOutOfBoundsException e) {
217            // Expected.
218        }
219
220        try {
221            is.read(buf, 500, 501);
222            fail("Test 7: IndexOutOfBoundsException expected.");
223        } catch (IndexOutOfBoundsException e) {
224            // Expected.
225        }
226
227        is.close();
228        try {
229            is.read(buf, 0, 100);
230            fail("Test 8: IOException expected.");
231        } catch (IOException e) {
232            // Expected.
233        }
234    }
235
236    public void test_reset() throws Exception {
237        // Test for method void java.io.FilterInputStream.reset()
238
239        // Test 1: Check that reset() throws an IOException if the
240        // filtered stream is a FileInputStream.
241        try {
242            is.reset();
243            fail("Test 1: IOException expected.");
244        } catch (IOException e) {
245            // expected
246        }
247
248        // Test 2: Check that reset() throws an IOException if the
249        // filtered stream is a BufferedInputStream but mark() has not
250        // yet been called.
251        is = new MyFilterInputStream(new BufferedInputStream(
252                new java.io.FileInputStream(fileName), 100));
253        try {
254            is.reset();
255            fail("Test 2: IOException expected.");
256        } catch (IOException e) {
257            // expected
258        }
259
260        // Test 3: Check that reset() in combination with mark()
261        // works correctly.
262        final int bufSize = 10;
263        byte[] buf1 = new byte[bufSize];
264        byte[] buf2 = new byte[bufSize];
265        is.read(buf1, 0, bufSize);
266        is.mark(2 * bufSize);
267        is.read(buf1, 0, bufSize);
268        try {
269            is.reset();
270        } catch (IOException e) {
271            fail("Test 3: Unexpected IOException.");
272        }
273        is.read(buf2, 0, bufSize);
274        assertTrue("Test 4: mark() or reset() has failed.",
275                Arrays.equals(buf1, buf2));
276    }
277
278    public void test_skipJ() throws IOException {
279        byte[] buf1 = new byte[10];
280        is.skip(10);
281        is.read(buf1, 0, buf1.length);
282        assertTrue("Test 1: Failed to skip to the correct position.",
283                new String(buf1, 0, buf1.length).equals(
284                        testString.substring(10, 20)));
285
286        is.close();
287        try {
288            is.read();
289            fail("Test 2: IOException expected.");
290        } catch (IOException e) {
291            // Expected.
292        }
293    }
294
295    protected void setUp() {
296        try {
297            fileName = System.getProperty("java.io.tmpdir");
298            String separator = System.getProperty("file.separator");
299            if (fileName.charAt(fileName.length() - 1) == separator.charAt(0))
300                fileName = Support_PlatformFile.getNewPlatformFile(fileName,
301                        "input.tst");
302            else
303                fileName = Support_PlatformFile.getNewPlatformFile(fileName
304                        + separator, "input.tst");
305            java.io.OutputStream fos = new java.io.FileOutputStream(fileName);
306            fos.write(testString.getBytes());
307            fos.close();
308            is = new MyFilterInputStream(new java.io.FileInputStream(fileName));
309        } catch (java.io.IOException e) {
310            System.out.println("Exception during setup");
311            e.printStackTrace();
312        }
313    }
314
315    protected void tearDown() {
316        try {
317            is.close();
318        } catch (Exception e) {
319            System.out.println("Unexpected exception in tearDown().");
320        }
321        new java.io.File(fileName).delete();
322    }
323}
324