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.luni.tests.java.io;
19
20import java.io.File;
21import java.io.FilterInputStream;
22import java.io.IOException;
23import java.io.InputStream;
24
25import junit.framework.TestCase;
26import tests.support.Support_PlatformFile;
27
28public class FilterInputStreamTest extends TestCase {
29
30    static class MyFilterInputStream extends FilterInputStream {
31        public MyFilterInputStream(InputStream is) {
32            super(is);
33        }
34    }
35
36    private String fileName;
37
38    private InputStream is;
39
40    byte[] ibuf = new byte[4096];
41
42    public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
43
44    /**
45     * @tests java.io.FilterInputStream#available()
46     */
47    public void test_available() throws IOException {
48        assertTrue("Returned incorrect number of available bytes", is
49                .available() == fileString.length());
50    }
51
52    /**
53     * @tests java.io.FilterInputStream#close()
54     */
55    public void test_close() throws IOException {
56        is.close();
57
58        try {
59            is.read();
60            fail("Able to read from closed stream");
61        } catch (java.io.IOException e) {
62            // Expected
63        }
64    }
65
66    /**
67     * @tests java.io.FilterInputStream#mark(int)
68     */
69    public void test_markI() {
70        assertTrue("Mark not supported by parent InputStream", true);
71    }
72
73    /**
74     * @tests java.io.FilterInputStream#markSupported()
75     */
76    public void test_markSupported() {
77        assertTrue("markSupported returned true", !is.markSupported());
78    }
79
80    /**
81     * @tests java.io.FilterInputStream#read()
82     */
83    public void test_read() throws Exception {
84        int c = is.read();
85        assertTrue("read returned incorrect char", c == fileString.charAt(0));
86    }
87
88    /**
89     * @tests java.io.FilterInputStream#read(byte[])
90     */
91    public void test_read$B() throws Exception {
92        byte[] buf1 = new byte[100];
93        is.read(buf1);
94        assertTrue("Failed to read correct data", new String(buf1, 0,
95                buf1.length, "UTF-8").equals(fileString.substring(0, 100)));
96    }
97
98    /**
99     * @tests java.io.FilterInputStream#read(byte[], int, int)
100     */
101    public void test_read$BII() throws Exception {
102        byte[] buf1 = new byte[100];
103        is.skip(3000);
104        is.mark(1000);
105        is.read(buf1, 0, buf1.length);
106        assertTrue("Failed to read correct data", new String(buf1, 0,
107                buf1.length, "UTF-8").equals(fileString.substring(3000, 3100)));
108    }
109
110    /**
111     * @tests java.io.FilterInputStream#reset()
112     */
113    public void test_reset() {
114        try {
115            is.reset();
116            fail("should throw IOException");
117        } catch (IOException e) {
118            // expected
119        }
120    }
121
122    /**
123     * @tests java.io.FilterInputStream#skip(long)
124     */
125    public void test_skipJ() throws Exception {
126        byte[] buf1 = new byte[10];
127        is.skip(1000);
128        is.read(buf1, 0, buf1.length);
129        assertTrue("Failed to skip to correct position", new String(buf1, 0,
130                buf1.length, "UTF-8").equals(fileString.substring(1000, 1010)));
131    }
132
133    /**
134     * Sets up the fixture, for example, open a network connection. This method
135     * is called before a test is executed.
136     */
137    @Override
138    protected void setUp() throws IOException {
139        fileName = System.getProperty("user.dir");
140        String separator = System.getProperty("file.separator");
141        if (fileName.charAt(fileName.length() - 1) == separator.charAt(0)) {
142            fileName = Support_PlatformFile.getNewPlatformFile(fileName,
143                    "input.tst");
144        } else {
145            fileName = Support_PlatformFile.getNewPlatformFile(fileName
146                    + separator, "input.tst");
147        }
148        java.io.OutputStream fos = new java.io.FileOutputStream(fileName);
149        fos.write(fileString.getBytes("UTF-8"));
150        fos.close();
151        is = new MyFilterInputStream(new java.io.FileInputStream(fileName));
152    }
153
154    /**
155     * Tears down the fixture, for example, close a network connection. This
156     * method is called after a test is executed.
157     */
158    @Override
159    protected void tearDown() {
160        try {
161            is.close();
162        } catch (Exception e) {
163            // Ignored
164        }
165        new File(fileName).delete();
166    }
167}
168