ByteArrayInputStreamTest.java revision 5d709784bbf5001012d7f25172927d46f6c1abe1
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 java.io.ByteArrayInputStream;
21import java.io.IOException;
22import java.io.InputStream;
23
24import dalvik.annotation.TestLevel;
25import dalvik.annotation.TestTargetClass;
26import dalvik.annotation.TestTargetNew;
27
28@TestTargetClass(ByteArrayInputStream.class)
29public class ByteArrayInputStreamTest extends junit.framework.TestCase {
30
31    private ByteArrayInputStream is;
32
33    public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\n";
34
35    /**
36     * @tests java.io.ByteArrayInputStream#ByteArrayInputStream(byte[])
37     */
38    @TestTargetNew(
39        level = TestLevel.COMPLETE,
40        method = "ByteArrayInputStream",
41        args = {byte[].class}
42    )
43    public void test_Constructor$B() {
44        // Test for method java.io.ByteArrayInputStream(byte [])
45
46        java.io.InputStream bis = new java.io.ByteArrayInputStream(fileString
47                .getBytes());
48
49        try {
50            assertTrue("Unable to create ByteArrayInputStream",
51                    bis.available() == fileString.length());
52        } catch (Exception e) {
53            System.out.println("Exception during Constructor test");
54        }
55    }
56
57    /**
58     * @throws IOException
59     * @tests java.io.ByteArrayInputStream#ByteArrayInputStream(byte[], int,
60     *        int)
61     */
62    @TestTargetNew(
63        level = TestLevel.COMPLETE,
64        method = "ByteArrayInputStream",
65        args = {byte[].class, int.class, int.class}
66    )
67    public void test_Constructor$BII() throws IOException {
68        // Test for method java.io.ByteArrayInputStream(byte [], int, int)
69
70        byte[] zz = fileString.getBytes();
71        java.io.InputStream bis = new java.io.ByteArrayInputStream(zz, 0, 100);
72
73        try {
74            assertEquals("Unable to create ByteArrayInputStream",
75                    100, bis.available());
76        } catch (Exception e) {
77            fail("Exception during Constructor test");
78        }
79
80        // Regression test for Harmony-2405
81        new SubByteArrayInputStream(new byte[] { 1, 2 }, 444, 13);
82        assertEquals(444, SubByteArrayInputStream.pos);
83        assertEquals(444, SubByteArrayInputStream.mark);
84        assertEquals(2, SubByteArrayInputStream.count);
85    }
86
87    static class SubByteArrayInputStream extends ByteArrayInputStream {
88        public static byte[] buf;
89
90        public static int mark, pos, count;
91
92        SubByteArrayInputStream(byte[] buf, int offset, int length)
93                throws IOException {
94            super(buf, offset, length);
95            buf = super.buf;
96            mark = super.mark;
97            pos = super.pos;
98            count = super.count;
99        }
100    }
101
102    /**
103     * @tests java.io.ByteArrayInputStream#available()
104     */
105    @TestTargetNew(
106        level = TestLevel.COMPLETE,
107        notes = "Verifies available() method.",
108        method = "available",
109        args = {}
110    )
111    public void test_available() {
112        // Test for method int java.io.ByteArrayInputStream.available()
113        try {
114            assertTrue("Returned incorrect number of available bytes", is
115                    .available() == fileString.length());
116        } catch (Exception e) {
117            fail("Exception during available test");
118        }
119    }
120
121    /**
122     * @tests java.io.ByteArrayInputStream#close()
123     */
124    @TestTargetNew(
125        level = TestLevel.COMPLETE,
126        method = "close",
127        args = {}
128    )
129    public void test_close() {
130        is.read();
131        try {
132            is.close();
133        } catch (java.io.IOException e) {
134            fail("Test 1: Failed to close the input stream.");
135        }
136        try {
137            is.read();
138        } catch (Exception e) {
139            fail("Test 2: Should be able to read from closed stream.");
140        }
141    }
142
143    /**
144     * @tests java.io.ByteArrayInputStream#mark(int)
145     */
146    @TestTargetNew(
147        level = TestLevel.COMPLETE,
148        notes = "Verifies mark(int readAheadLimit) method.",
149        method = "mark",
150        args = {int.class}
151    )
152    public void test_markI() {
153        // Test for method void java.io.ByteArrayInputStream.mark(int)
154        byte[] buf1 = new byte[100];
155        byte[] buf2 = new byte[100];
156        try {
157            is.skip(3000);
158            is.mark(1000);
159            is.read(buf1, 0, buf1.length);
160            is.reset();
161            is.read(buf2, 0, buf2.length);
162            is.reset();
163            assertTrue("Failed to mark correct position", new String(buf1, 0,
164                    buf1.length).equals(new String(buf2, 0, buf2.length)));
165
166        } catch (Exception e) {
167            fail("Exception during mark test");
168        }
169
170    }
171
172    /**
173     * @tests java.io.ByteArrayInputStream#markSupported()
174     */
175    @TestTargetNew(
176        level = TestLevel.COMPLETE,
177        notes = "Verifies markSupported() method.",
178        method = "markSupported",
179        args = {}
180    )
181    public void test_markSupported() {
182        // Test for method boolean java.io.ByteArrayInputStream.markSupported()
183        assertTrue("markSupported returned incorrect value", is.markSupported());
184    }
185
186    /**
187     * @tests java.io.ByteArrayInputStream#read()
188     */
189    @TestTargetNew(
190        level = TestLevel.COMPLETE,
191        notes = "Verifies read() method.",
192        method = "read",
193        args = {}
194    )
195    public void test_read() {
196        // Test for method int java.io.ByteArrayInputStream.read()
197        try {
198
199            int c = is.read();
200            is.reset();
201            assertTrue("read returned incorrect char", c == fileString
202                    .charAt(0));
203        } catch (Exception e) {
204            fail("Exception during read test");
205        }
206    }
207
208    /**
209     * @tests java.io.ByteArrayInputStream#read(byte[], int, int)
210     */
211    @TestTargetNew(
212        level = TestLevel.COMPLETE,
213        method = "read",
214        args = {byte[].class, int.class, int.class}
215    )
216    public void test_read$BII() throws IOException {
217        byte[] buf1 = new byte[20];
218        is.skip(50);
219        is.mark(100);
220        is.read(buf1, 0, buf1.length);
221        assertTrue("Test 1: Failed to read correct data.",
222                new String(buf1, 0, buf1.length).equals(
223                        fileString.substring(50, 70)));
224
225        // Illegal argument checks.
226        try {
227            is.read(null, 1, 0);
228            fail("Test 2: NullPointerException expected.");
229        } catch (NullPointerException e) {
230            // Expected.
231        }
232
233        try {
234            is.read(buf1 , -1, 1);
235            fail("Test 3: IndexOutOfBoundsException expected.");
236        } catch (IndexOutOfBoundsException e) {
237            // Expected
238        }
239
240        try {
241            is.read(buf1 , 1, -1);
242            fail("Test 4: IndexOutOfBoundsException expected.");
243        } catch (IndexOutOfBoundsException e) {
244            // Expected
245        }
246
247        try {
248            is.read(buf1, 1, buf1.length);
249            fail("Test 5: IndexOutOfBoundsException expected.");
250        } catch (IndexOutOfBoundsException e) {
251            // Expected
252        }
253    }
254
255    /**
256     * @tests java.io.ByteArrayInputStream#reset()
257     */
258    @TestTargetNew(
259        level = TestLevel.COMPLETE,
260        notes = "The test verifies reset() method.",
261        method = "reset",
262        args = {}
263    )
264    public void test_reset() {
265        // Test for method void java.io.ByteArrayInputStream.reset()
266        byte[] buf1 = new byte[10];
267        byte[] buf2 = new byte[10];
268        try {
269            is.mark(200);
270            is.read(buf1, 0, 10);
271            is.reset();
272            is.read(buf2, 0, 10);
273            is.reset();
274            assertTrue("Reset failed", new String(buf1, 0, buf1.length)
275                    .equals(new String(buf2, 0, buf2.length)));
276        } catch (Exception e) {
277            fail("Exception during reset test : " + e.getMessage());
278        }
279    }
280
281    /**
282     * @tests java.io.ByteArrayInputStream#skip(long)
283     */
284    @TestTargetNew(
285        level = TestLevel.COMPLETE,
286        notes = "",
287        method = "skip",
288        args = {long.class}
289    )
290    public void test_skipJ() {
291        // Test for method long java.io.ByteArrayInputStream.skip(long)
292        byte[] buf1 = new byte[10];
293        try {
294            is.skip(100);
295            is.read(buf1, 0, buf1.length);
296            assertTrue("Failed to skip to correct position", new String(buf1,
297                    0, buf1.length).equals(fileString.substring(100, 110)));
298        } catch (Exception e) {
299            fail("Exception during skip test : " + e.getMessage());
300        }
301    }
302
303    /**
304     * Sets up the fixture, for example, open a network connection. This method
305     * is called before a test is executed.
306     */
307    protected void setUp() {
308
309        is = new java.io.ByteArrayInputStream(fileString.getBytes());
310
311    }
312
313    /**
314     * Tears down the fixture, for example, close a network connection. This
315     * method is called after a test is executed.
316     */
317    protected void tearDown() {
318
319        try {
320            is.close();
321
322        } catch (Exception e) {
323        }
324    }
325}
326