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.tests.java.io;
19
20import java.io.File;
21import java.io.FileOutputStream;
22import java.io.FilterInputStream;
23import java.io.IOException;
24import java.io.InputStream;
25import java.io.OutputStream;
26import java.nio.charset.StandardCharsets;
27import junit.framework.TestCase;
28
29public class FilterInputStreamTest extends TestCase {
30
31    static class MyFilterInputStream extends FilterInputStream {
32        public MyFilterInputStream(InputStream is) {
33            super(is);
34        }
35    }
36
37    private String fileName;
38    private InputStream is;
39    private static final String INPUT =
40             "Test_All_Tests\n" +
41             "Test_BufferedInputStream\n" +
42             "Test_java_io_BufferedOutputStream\n" +
43             "Test_java_io_ByteArrayInputStream\n" +
44             "Test_java_io_ByteArrayOutputStream\n" +
45             "Test_java_io_DataInputStream\n" +
46             "Test_java_io_File\n" +
47             "Test_java_io_FileDescriptor\n" +
48             "Test_java_io_FileInputStream\n" +
49             "Test_java_io_FileNotFoundException\n" +
50             "Test_java_io_FileOutputStream\n" +
51             "Test_java_io_FilterInputStream\n" +
52             "Test_java_io_FilterOutputStream\n" +
53             "Test_java_io_InputStream\n" +
54             "Test_java_io_IOException\n" +
55             "Test_java_io_OutputStream\n" +
56             "Test_java_io_PrintStream\n" +
57             "Test_java_io_RandomAccessFile\n" +
58             "Test_java_io_SyncFailedException\n" +
59             "Test_java_lang_AbstractMethodError\n" +
60             "Test_java_lang_ArithmeticException\n" +
61             "Test_java_lang_ArrayIndexOutOfBoundsException\n" +
62             "Test_java_lang_ArrayStoreException\n" +
63             "Test_java_lang_Boolean\n" +
64             "Test_java_lang_Byte\n" +
65             "Test_java_lang_Character\n" +
66             "Test_All_Tests\n" +
67             "Test_BufferedInputStream\n" +
68             "Test_java_io_BufferedOutputStream\n" +
69             "Test_java_io_ByteArrayInputStream\n" +
70             "Test_java_io_ByteArrayOutputStream\n" +
71             "Test_java_io_DataInputStream\n" +
72             "Test_java_io_File\n" +
73             "Test_java_io_FileDescriptor\n" +
74             "Test_java_io_FileInputStream\n" +
75             "Test_java_io_FileNotFoundException\n" +
76             "Test_java_io_FileOutputStream\n" +
77             "Test_java_io_FilterInputStream\n" +
78             "Test_java_io_FilterOutputStream\n" +
79             "Test_java_io_InputStream\n" +
80             "Test_java_io_IOException\n" +
81             "Test_java_io_OutputStream\n" +
82             "Test_java_io_PrintStream\n" +
83             "Test_java_io_RandomAccessFile\n" +
84             "Test_java_io_SyncFailedException\n" +
85             "Test_java_lang_AbstractMethodError\n" +
86             "Test_java_lang_ArithmeticException\n" +
87             "Test_java_lang_ArrayIndexOutOfBoundsException\n" +
88             "Test_java_lang_ArrayStoreException\n" +
89             "Test_java_lang_Boolean\n" +
90             "Test_java_lang_Byte\n" +
91             "Test_java_lang_Character\n";
92
93    /**
94     * Sets up the fixture, for example, open a network connection. This method
95     * is called before a test is executed.
96     */
97    @Override
98    protected void setUp() throws IOException {
99        File temp = File.createTempFile("FilterInputStreamTest", "tst");
100        fileName = temp.getAbsolutePath();
101        OutputStream fos = new FileOutputStream(temp.getAbsolutePath());
102        fos.write(INPUT.getBytes(StandardCharsets.US_ASCII));
103        fos.close();
104        is = new MyFilterInputStream(new java.io.FileInputStream(fileName));
105    }
106
107    /**
108     * Tears down the fixture, for example, close a network connection. This
109     * method is called after a test is executed.
110     */
111    @Override
112    protected void tearDown() {
113        try {
114            is.close();
115        } catch (Exception e) {
116            // Ignored
117        }
118        new File(fileName).delete();
119    }
120
121    /**
122     * java.io.FilterInputStream#available()
123     */
124    public void test_available() throws IOException {
125        assertTrue("Returned incorrect number of available bytes", is
126                .available() == INPUT.length());
127    }
128
129    /**
130     * java.io.FilterInputStream#close()
131     */
132    public void test_close() throws IOException {
133        is.close();
134
135        try {
136            is.read();
137            fail("Able to read from closed stream");
138        } catch (java.io.IOException e) {
139            // Expected
140        }
141    }
142
143    /**
144     * java.io.FilterInputStream#mark(int)
145     */
146    public void test_markI() {
147        assertTrue("Mark not supported by parent InputStream", true);
148    }
149
150    /**
151     * java.io.FilterInputStream#markSupported()
152     */
153    public void test_markSupported() {
154        assertTrue("markSupported returned true", !is.markSupported());
155    }
156
157    /**
158     * java.io.FilterInputStream#read()
159     */
160    public void test_read() throws Exception {
161        int c = is.read();
162        assertTrue("read returned incorrect char", c == INPUT.charAt(0));
163    }
164
165    /**
166     * java.io.FilterInputStream#read(byte[])
167     */
168    public void test_read$B() throws Exception {
169        byte[] buf1 = new byte[100];
170        is.read(buf1);
171        assertTrue("Failed to read correct data", new String(buf1, 0,
172                buf1.length, "UTF-8").equals(INPUT.substring(0, 100)));
173    }
174
175    /**
176     * java.io.FilterInputStream#read(byte[], int, int)
177     */
178    public void test_read$BII() throws Exception {
179        byte[] buf1 = new byte[100];
180        is.skip(500);
181        is.mark(1000);
182        is.read(buf1, 0, buf1.length);
183        assertTrue("Failed to read correct data", new String(buf1, 0,
184                buf1.length, "UTF-8").equals(INPUT.substring(500, 600)));
185    }
186
187    /**
188     * java.io.FilterInputStream#reset()
189     */
190    public void test_reset() {
191        try {
192            is.reset();
193            fail("should throw IOException");
194        } catch (IOException e) {
195            // expected
196        }
197    }
198
199    /**
200     * java.io.FilterInputStream#skip(long)
201     */
202    public void test_skipJ() throws Exception {
203        byte[] buf1 = new byte[10];
204        is.skip(1000);
205        is.read(buf1, 0, buf1.length);
206        assertTrue("Failed to skip to correct position", new String(buf1, 0,
207                buf1.length, "UTF-8").equals(INPUT.substring(1000, 1010)));
208    }
209}
210