FilterInputStreamTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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 tests.api.java.io;
19
20import dalvik.annotation.TestInfo;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTarget;
23import dalvik.annotation.TestTargetClass;
24
25import java.io.FilterInputStream;
26import java.io.IOException;
27
28import tests.support.Support_PlatformFile;
29
30@TestTargetClass(FilterInputStream.class)
31public class FilterInputStreamTest extends junit.framework.TestCase {
32
33    static class MyFilterInputStream extends java.io.FilterInputStream {
34        public MyFilterInputStream(java.io.InputStream is) {
35            super(is);
36        }
37    }
38
39    private String fileName;
40
41    private java.io.InputStream is;
42
43    byte[] ibuf = new byte[4096];
44
45    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";
46
47    /**
48     * @tests java.io.FilterInputStream#available()
49     */
50    @TestInfo(
51            level = TestLevel.PARTIAL,
52            purpose = "IOException checking missed.",
53            targets = { @TestTarget(methodName = "available",
54                                    methodArgs = {})
55            }
56        )
57    public void test_available() {
58        // Test for method int java.io.FilterInputStream.available()
59        try {
60            assertTrue("Returned incorrect number of available bytes", is
61                    .available() == fileString.length());
62        } catch (Exception e) {
63            fail("Exception during available test : " + e.getMessage());
64        }
65    }
66
67    /**
68     * @tests java.io.FilterInputStream#close()
69     */
70    @TestInfo(
71            level = TestLevel.PARTIAL,
72            purpose = "IOException checking missed.",
73            targets = { @TestTarget(methodName = "close",
74                                    methodArgs = {})
75            }
76        )
77    public void test_close() {
78        // Test for method void java.io.FilterInputStream.close()
79        try {
80            is.close();
81        } catch (java.io.IOException e) {
82            fail("Exception attempting to close stream : " + e.getMessage());
83        }
84
85        try {
86            is.read();
87        } catch (java.io.IOException e) {
88            return;
89        }
90        fail("Able to read from closed stream");
91    }
92
93    /**
94     * @tests java.io.FilterInputStream#mark(int)
95     */
96    @TestInfo(
97            level = TestLevel.TODO,
98            purpose = "Dummy test.",
99            targets = { @TestTarget(methodName = "mark",
100                                    methodArgs = {int.class})
101            }
102        )
103    public void test_markI() {
104        // Test for method void java.io.FilterInputStream.mark(int)
105        assertTrue("Mark not supported by parent InputStream", true);
106    }
107
108    /**
109     * @tests java.io.FilterInputStream#markSupported()
110     */
111    @TestInfo(
112            level = TestLevel.COMPLETE,
113            purpose = "Verifies markSupported() method.",
114            targets = { @TestTarget(methodName = "markSupported",
115                                    methodArgs = {})
116            }
117        )
118    public void test_markSupported() {
119        // Test for method boolean java.io.FilterInputStream.markSupported()
120        assertTrue("markSupported returned true", !is.markSupported());
121    }
122
123    /**
124     * @tests java.io.FilterInputStream#read()
125     */
126    @TestInfo(
127            level = TestLevel.PARTIAL,
128            purpose = "IOException checking missed.",
129            targets = { @TestTarget(methodName = "read",
130                                    methodArgs = {})
131            }
132        )
133    public void test_read() {
134        // Test for method int java.io.FilterInputStream.read()
135        try {
136            int c = is.read();
137            assertTrue("read returned incorrect char", c == fileString
138                    .charAt(0));
139        } catch (Exception e) {
140            fail("Exception during read test : " + e.getMessage());
141        }
142    }
143
144    /**
145     * @tests java.io.FilterInputStream#read(byte[])
146     */
147    @TestInfo(
148            level = TestLevel.PARTIAL,
149            purpose = "IOException checking missed.",
150            targets = { @TestTarget(methodName = "read",
151                                    methodArgs = {byte[].class})
152            }
153        )
154    public void test_read$B() {
155        // Test for method int java.io.FilterInputStream.read(byte [])
156        byte[] buf1 = new byte[100];
157        try {
158            is.read(buf1);
159            assertTrue("Failed to read correct data", new String(buf1, 0,
160                    buf1.length).equals(fileString.substring(0, 100)));
161        } catch (Exception e) {
162            fail("Exception during read test : " + e.getMessage());
163        }
164    }
165
166    /**
167     * @tests java.io.FilterInputStream#read(byte[], int, int)
168     */
169    @TestInfo(
170            level = TestLevel.PARTIAL,
171            purpose = "IOException checking missed.",
172            targets = { @TestTarget(methodName = "read",
173                                    methodArgs = {byte[].class, int.class, int.class})
174            }
175        )
176    public void test_read$BII() {
177        // Test for method int java.io.FilterInputStream.read(byte [], int, int)
178        byte[] buf1 = new byte[100];
179        try {
180            is.skip(3000);
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).equals(fileString.substring(3000, 3100)));
185        } catch (Exception e) {
186            fail("Exception during read test : " + e.getMessage());
187        }
188    }
189
190    /**
191     * @tests java.io.FilterInputStream#reset()
192     */
193    @TestInfo(
194            level = TestLevel.TODO,
195            purpose = "Dummy test.",
196            targets = { @TestTarget(methodName = "reset",
197                                    methodArgs = {})
198            }
199        )
200    public void test_reset() {
201        // Test for method void java.io.FilterInputStream.reset()
202        try {
203            is.reset();
204            fail("should throw IOException");
205        } catch (IOException e) {
206            // expected
207        }
208    }
209
210    /**
211     * @tests java.io.FilterInputStream#skip(long)
212     */
213    @TestInfo(
214            level = TestLevel.PARTIAL,
215            purpose = "IOException checking missed.",
216            targets = { @TestTarget(methodName = "skip",
217                                    methodArgs = {long.class})
218            }
219        )
220    public void test_skipJ() {
221        // Test for method long java.io.FilterInputStream.skip(long)
222        byte[] buf1 = new byte[10];
223        try {
224            is.skip(1000);
225            is.read(buf1, 0, buf1.length);
226            assertTrue("Failed to skip to correct position", new String(buf1,
227                    0, buf1.length).equals(fileString.substring(1000, 1010)));
228        } catch (Exception e) {
229            fail("Exception during skip test");
230        }
231    }
232
233    /**
234     * Sets up the fixture, for example, open a network connection. This method
235     * is called before a test is executed.
236     */
237    protected void setUp() {
238        try {
239            fileName = System.getProperty("user.dir");
240            String separator = System.getProperty("file.separator");
241            if (fileName.charAt(fileName.length() - 1) == separator.charAt(0))
242                fileName = Support_PlatformFile.getNewPlatformFile(fileName,
243                        "input.tst");
244            else
245                fileName = Support_PlatformFile.getNewPlatformFile(fileName
246                        + separator, "input.tst");
247            java.io.OutputStream fos = new java.io.FileOutputStream(fileName);
248            fos.write(fileString.getBytes());
249            fos.close();
250            is = new MyFilterInputStream(new java.io.FileInputStream(fileName));
251        } catch (java.io.IOException e) {
252            System.out.println("Exception during setup");
253            e.printStackTrace();
254        }
255    }
256
257    /**
258     * Tears down the fixture, for example, close a network connection. This
259     * method is called after a test is executed.
260     */
261    protected void tearDown() {
262        try {
263            is.close();
264        } catch (Exception e) {
265            System.out.println("Exception during BIS tearDown");
266        }
267        new java.io.File(fileName).delete();
268    }
269}
270