PushbackInputStreamTest.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.ByteArrayInputStream;
26import java.io.IOException;
27import java.io.PushbackInputStream;
28
29@TestTargetClass(PushbackInputStream.class)
30public class PushbackInputStreamTest extends junit.framework.TestCase {
31
32    PushbackInputStream pis;
33
34    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_java_io_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";
35
36    /**
37     * @tests java.io.PushbackInputStream#PushbackInputStream(java.io.InputStream)
38     */
39    @TestInfo(
40      level = TestLevel.COMPLETE,
41      purpose = "",
42      targets = {
43        @TestTarget(
44          methodName = "PushbackInputStream",
45          methodArgs = {java.io.InputStream.class}
46        )
47    })
48    public void test_ConstructorLjava_io_InputStream() {
49        // Test for method java.io.PushbackInputStream(java.io.InputStream)
50        try {
51            pis = new PushbackInputStream(new ByteArrayInputStream("Hello"
52                    .getBytes()));
53            pis.unread("He".getBytes());
54        } catch (IOException e) {
55            // Correct
56            // Pushback buffer should be full
57            return;
58
59        }
60        fail("Failed to throw exception on unread when buffer full");
61    }
62
63    /**
64     * @tests java.io.PushbackInputStream#PushbackInputStream(java.io.InputStream,
65     *        int)
66     */
67    @TestInfo(
68      level = TestLevel.PARTIAL,
69      purpose = "IllegalArgumentException checking missed.",
70      targets = {
71        @TestTarget(
72          methodName = "PushbackInputStream",
73          methodArgs = {java.io.InputStream.class, int.class}
74        )
75    })
76    public void test_ConstructorLjava_io_InputStreamI() {
77        // Test for method java.io.PushbackInputStream(java.io.InputStream, int)
78        try {
79            pis = new PushbackInputStream(new ByteArrayInputStream("Hello"
80                    .getBytes()), 5);
81            pis.unread("Hellos".getBytes());
82        } catch (IOException e) {
83            // Correct
84            // Pushback buffer should be full
85            return;
86
87        }
88        fail("Failed to throw exception on unread when buffer full");
89    }
90
91    /**
92     * @tests java.io.PushbackInputStream#available()
93     */
94    @TestInfo(
95      level = TestLevel.PARTIAL,
96      purpose = "IOException checking missed.",
97      targets = {
98        @TestTarget(
99          methodName = "available",
100          methodArgs = {}
101        )
102    })
103    public void test_available() {
104        // Test for method int java.io.PushbackInputStream.available()
105        try {
106            assertTrue("Available returned incorrect number of bytes", pis
107                    .available() == fileString.getBytes().length);
108        } catch (IOException e) {
109            fail("Exception during available test: " + e.toString());
110        }
111    }
112
113    /**
114     * @tests java.io.PushbackInputStream#markSupported()
115     */
116    @TestInfo(
117      level = TestLevel.COMPLETE,
118      purpose = "",
119      targets = {
120        @TestTarget(
121          methodName = "markSupported",
122          methodArgs = {}
123        )
124    })
125    public void test_markSupported() {
126        // Test for method boolean java.io.PushbackInputStream.markSupported()
127        assertTrue("markSupported returned true", !pis.markSupported());
128    }
129
130    /**
131     * @tests java.io.PushbackInputStream#read()
132     */
133    @TestInfo(
134      level = TestLevel.PARTIAL,
135      purpose = "IOException checking missed.",
136      targets = {
137        @TestTarget(
138          methodName = "read",
139          methodArgs = {}
140        )
141    })
142    public void test_read() {
143        // Test for method int java.io.PushbackInputStream.read()
144        try {
145            assertTrue("Incorrect byte read", pis.read() == fileString
146                    .getBytes()[0]);
147        } catch (IOException e) {
148            fail("Exception during read test : " + e.getMessage());
149        }
150    }
151
152    /**
153     * @tests java.io.PushbackInputStream#read(byte[], int, int)
154     */
155    @TestInfo(
156      level = TestLevel.PARTIAL_OK,
157      purpose = "IOException checking missed.",
158      targets = {
159        @TestTarget(
160          methodName = "read",
161          methodArgs = {byte[].class, int.class, int.class}
162        )
163    })
164    public void test_read$BII() {
165        // Test for method int java.io.PushbackInputStream.read(byte [], int,
166        // int)
167        try {
168            byte[] buf = new byte[100];
169            pis.read(buf, 0, buf.length);
170            assertTrue("Incorrect bytes read", new String(buf)
171                    .equals(fileString.substring(0, 100)));
172        } catch (IOException e) {
173            fail("Exception during read test : " + e.getMessage());
174        }
175    }
176
177    /**
178     * @tests java.io.PushbackInputStream#skip(long)
179     */
180    @TestInfo(
181      level = TestLevel.PARTIAL,
182      purpose = "IOException checking missed.",
183      targets = {
184        @TestTarget(
185          methodName = "skip",
186          methodArgs = {long.class}
187        )
188    })
189    public void test_skipJ() {
190        // Test for method long java.io.PushbackInputStream.skip(long)
191        try {
192            byte[] buf = new byte[50];
193            pis.skip(50);
194            pis.read(buf, 0, buf.length);
195            assertTrue("a) Incorrect bytes read", new String(buf)
196                    .equals(fileString.substring(50, 100)));
197            pis.unread(buf);
198            pis.skip(25);
199            byte[] buf2 = new byte[25];
200            pis.read(buf2, 0, buf2.length);
201            assertTrue("b) Incorrect bytes read", new String(buf2)
202                    .equals(fileString.substring(75, 100)));
203        } catch (Exception e) {
204            fail("Exception during test : " + e.getMessage());
205        }
206    }
207
208    /**
209     * @tests java.io.PushbackInputStream#unread(byte[])
210     */
211    @TestInfo(
212      level = TestLevel.PARTIAL,
213      purpose = "IOException checking missed.",
214      targets = {
215        @TestTarget(
216          methodName = "unread",
217          methodArgs = {byte[].class}
218        )
219    })
220    public void test_unread$B() {
221        // Test for method void java.io.PushbackInputStream.unread(byte [])
222        try {
223            byte[] buf = new byte[100];
224            pis.read(buf, 0, buf.length);
225            assertTrue("Incorrect bytes read", new String(buf)
226                    .equals(fileString.substring(0, 100)));
227            pis.unread(buf);
228            pis.read(buf, 0, 50);
229            assertTrue("Failed to unread bytes", new String(buf, 0, 50)
230                    .equals(fileString.substring(0, 50)));
231        } catch (IOException e) {
232            fail("IOException during unread test : " + e.getMessage());
233        }
234    }
235
236    /**
237     * @tests java.io.PushbackInputStream#unread(byte[], int, int)
238     */
239    @TestInfo(
240      level = TestLevel.PARTIAL,
241      purpose = "IOException checking missed.",
242      targets = {
243        @TestTarget(
244          methodName = "unread",
245          methodArgs = {byte[].class, int.class, int.class}
246        )
247    })
248    public void test_unread$BII() {
249        // Test for method void java.io.PushbackInputStream.unread(byte [], int,
250        // int)
251        try {
252            byte[] buf = new byte[100];
253            pis.read(buf, 0, buf.length);
254            assertTrue("Incorrect bytes read", new String(buf)
255                    .equals(fileString.substring(0, 100)));
256            pis.unread(buf, 50, 50);
257            pis.read(buf, 0, 50);
258            assertTrue("Failed to unread bytes", new String(buf, 0, 50)
259                    .equals(fileString.substring(50, 100)));
260        } catch (IOException e) {
261            fail("IOException during unread test : " + e.getMessage());
262        }
263    }
264
265    /**
266     * @tests java.io.PushbackInputStream#unread(int)
267     */
268    @TestInfo(
269      level = TestLevel.PARTIAL,
270      purpose = "IOException checking missed.",
271      targets = {
272        @TestTarget(
273          methodName = "unread",
274          methodArgs = {int.class}
275        )
276    })
277    public void test_unreadI() {
278        // Test for method void java.io.PushbackInputStream.unread(int)
279        try {
280            int x;
281            assertTrue("Incorrect byte read", (x = pis.read()) == fileString
282                    .getBytes()[0]);
283            pis.unread(x);
284            assertTrue("Failed to unread", pis.read() == x);
285        } catch (IOException e) {
286            fail("IOException during read test : " + e.getMessage());
287        }
288    }
289
290    /**
291     * Sets up the fixture, for example, open a network connection. This method
292     * is called before a test is executed.
293     */
294    protected void setUp() {
295
296        pis = new PushbackInputStream(new ByteArrayInputStream(fileString
297                .getBytes()), 65535);
298    }
299
300    /**
301     * Tears down the fixture, for example, close a network connection. This
302     * method is called after a test is executed.
303     */
304    protected void tearDown() {
305        try {
306            pis.close();
307        } catch (IOException e) {
308            fail("IOException during tearDown : " + e.getMessage());
309        }
310    }
311}
312