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 libcore.java.io;
19
20import java.io.ByteArrayInputStream;
21import java.io.IOException;
22
23public class OldByteArrayInputStreamTest extends junit.framework.TestCase {
24
25    private ByteArrayInputStream is;
26
27    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";
28
29    public void test_Constructor$B() {
30        // Test for method java.io.ByteArrayInputStream(byte [])
31
32        java.io.InputStream bis = new java.io.ByteArrayInputStream(fileString
33                .getBytes());
34
35        try {
36            assertTrue("Unable to create ByteArrayInputStream",
37                    bis.available() == fileString.length());
38        } catch (Exception e) {
39            System.out.println("Exception during Constructor test");
40        }
41    }
42
43    public void test_Constructor$BII() throws IOException {
44        // Test for method java.io.ByteArrayInputStream(byte [], int, int)
45
46        byte[] zz = fileString.getBytes();
47        java.io.InputStream bis = new java.io.ByteArrayInputStream(zz, 0, 100);
48
49        try {
50            assertEquals("Unable to create ByteArrayInputStream",
51                    100, bis.available());
52        } catch (Exception e) {
53            fail("Exception during Constructor test");
54        }
55
56        // Regression test for Harmony-2405
57        new SubByteArrayInputStream(new byte[] { 1, 2 }, 444, 13);
58        assertEquals(444, SubByteArrayInputStream.pos);
59        assertEquals(444, SubByteArrayInputStream.mark);
60        assertEquals(2, SubByteArrayInputStream.count);
61    }
62
63    static class SubByteArrayInputStream extends ByteArrayInputStream {
64        public static byte[] buf;
65
66        public static int mark, pos, count;
67
68        SubByteArrayInputStream(byte[] buf, int offset, int length)
69                throws IOException {
70            super(buf, offset, length);
71            buf = super.buf;
72            mark = super.mark;
73            pos = super.pos;
74            count = super.count;
75        }
76    }
77
78    public void test_available() {
79        // Test for method int java.io.ByteArrayInputStream.available()
80        try {
81            assertTrue("Returned incorrect number of available bytes", is
82                    .available() == fileString.length());
83        } catch (Exception e) {
84            fail("Exception during available test");
85        }
86    }
87
88    public void test_close() {
89        is.read();
90        try {
91            is.close();
92        } catch (java.io.IOException e) {
93            fail("Test 1: Failed to close the input stream.");
94        }
95        try {
96            is.read();
97        } catch (Exception e) {
98            fail("Test 2: Should be able to read from closed stream.");
99        }
100    }
101
102    public void test_markI() {
103        // Test for method void java.io.ByteArrayInputStream.mark(int)
104        byte[] buf1 = new byte[100];
105        byte[] buf2 = new byte[100];
106        try {
107            is.skip(3000);
108            is.mark(1000);
109            is.read(buf1, 0, buf1.length);
110            is.reset();
111            is.read(buf2, 0, buf2.length);
112            is.reset();
113            assertTrue("Failed to mark correct position", new String(buf1, 0,
114                    buf1.length).equals(new String(buf2, 0, buf2.length)));
115
116        } catch (Exception e) {
117            fail("Exception during mark test");
118        }
119
120    }
121
122    public void test_markSupported() {
123        // Test for method boolean java.io.ByteArrayInputStream.markSupported()
124        assertTrue("markSupported returned incorrect value", is.markSupported());
125    }
126
127    public void test_read() {
128        // Test for method int java.io.ByteArrayInputStream.read()
129        try {
130
131            int c = is.read();
132            is.reset();
133            assertTrue("read returned incorrect char", c == fileString
134                    .charAt(0));
135        } catch (Exception e) {
136            fail("Exception during read test");
137        }
138    }
139
140    public void test_read$BII() throws IOException {
141        byte[] buf1 = new byte[20];
142        is.skip(50);
143        is.mark(100);
144        is.read(buf1, 0, buf1.length);
145        assertTrue("Test 1: Failed to read correct data.",
146                new String(buf1, 0, buf1.length).equals(
147                        fileString.substring(50, 70)));
148
149        // Illegal argument checks.
150        try {
151            is.read(null, 1, 0);
152            fail("Test 2: NullPointerException expected.");
153        } catch (NullPointerException e) {
154            // Expected.
155        }
156
157        try {
158            is.read(buf1 , -1, 1);
159            fail("Test 3: IndexOutOfBoundsException expected.");
160        } catch (IndexOutOfBoundsException e) {
161            // Expected
162        }
163
164        try {
165            is.read(buf1 , 1, -1);
166            fail("Test 4: IndexOutOfBoundsException expected.");
167        } catch (IndexOutOfBoundsException e) {
168            // Expected
169        }
170
171        try {
172            is.read(buf1, 1, buf1.length);
173            fail("Test 5: IndexOutOfBoundsException expected.");
174        } catch (IndexOutOfBoundsException e) {
175            // Expected
176        }
177    }
178
179    public void test_reset() {
180        // Test for method void java.io.ByteArrayInputStream.reset()
181        byte[] buf1 = new byte[10];
182        byte[] buf2 = new byte[10];
183        try {
184            is.mark(200);
185            is.read(buf1, 0, 10);
186            is.reset();
187            is.read(buf2, 0, 10);
188            is.reset();
189            assertTrue("Reset failed", new String(buf1, 0, buf1.length)
190                    .equals(new String(buf2, 0, buf2.length)));
191        } catch (Exception e) {
192            fail("Exception during reset test : " + e.getMessage());
193        }
194    }
195
196    public void test_skipJ() {
197        // Test for method long java.io.ByteArrayInputStream.skip(long)
198        byte[] buf1 = new byte[10];
199        try {
200            is.skip(100);
201            is.read(buf1, 0, buf1.length);
202            assertTrue("Failed to skip to correct position", new String(buf1,
203                    0, buf1.length).equals(fileString.substring(100, 110)));
204        } catch (Exception e) {
205            fail("Exception during skip test : " + e.getMessage());
206        }
207    }
208
209    protected void setUp() {
210        is = new ByteArrayInputStream(fileString.getBytes());
211
212    }
213
214    protected void tearDown() {
215        try {
216            is.close();
217        } catch (Exception e) {
218        }
219    }
220}
221