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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package tests.api.java.io;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import dalvik.annotation.TestLevel;
25 import dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestTargetNew;
28 @TestTargetClass(ByteArrayInputStream.class)
29 public class ByteArrayInputStreamTest extends junit.framework.TestCase {
31 private ByteArrayInputStream is;
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";
36 * @tests java.io.ByteArrayInputStream#ByteArrayInputStream(byte[])
39 level = TestLevel.COMPLETE,
40 method = "ByteArrayInputStream",
43 public void test_Constructor$B() {
44 // Test for method java.io.ByteArrayInputStream(byte [])
46 java.io.InputStream bis = new java.io.ByteArrayInputStream(fileString
50 assertTrue("Unable to create ByteArrayInputStream",
51 bis.available() == fileString.length());
52 } catch (Exception e) {
53 System.out.println("Exception during Constructor test");
59 * @tests java.io.ByteArrayInputStream#ByteArrayInputStream(byte[], int,
63 level = TestLevel.COMPLETE,
64 method = "ByteArrayInputStream",
65 args = {byte[].class, int.class, int.class}
67 public void test_Constructor$BII() throws IOException {
68 // Test for method java.io.ByteArrayInputStream(byte [], int, int)
70 byte[] zz = fileString.getBytes();
71 java.io.InputStream bis = new java.io.ByteArrayInputStream(zz, 0, 100);
74 assertEquals("Unable to create ByteArrayInputStream",
75 100, bis.available());
76 } catch (Exception e) {
77 fail("Exception during Constructor test");
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);
87 static class SubByteArrayInputStream extends ByteArrayInputStream {
88 public static byte[] buf;
90 public static int mark, pos, count;
92 SubByteArrayInputStream(byte[] buf, int offset, int length)
94 super(buf, offset, length);
103 * @tests java.io.ByteArrayInputStream#available()
106 level = TestLevel.COMPLETE,
107 notes = "Verifies available() method.",
108 method = "available",
111 public void test_available() {
112 // Test for method int java.io.ByteArrayInputStream.available()
114 assertTrue("Returned incorrect number of available bytes", is
115 .available() == fileString.length());
116 } catch (Exception e) {
117 fail("Exception during available test");
122 * @tests java.io.ByteArrayInputStream#close()
125 level = TestLevel.COMPLETE,
129 public void test_close() {
133 } catch (java.io.IOException e) {
134 fail("Test 1: Failed to close the input stream.");
138 } catch (Exception e) {
139 fail("Test 2: Should be able to read from closed stream.");
144 * @tests java.io.ByteArrayInputStream#mark(int)
147 level = TestLevel.COMPLETE,
148 notes = "Verifies mark(int readAheadLimit) method.",
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];
159 is.read(buf1, 0, buf1.length);
161 is.read(buf2, 0, buf2.length);
163 assertTrue("Failed to mark correct position", new String(buf1, 0,
164 buf1.length).equals(new String(buf2, 0, buf2.length)));
166 } catch (Exception e) {
167 fail("Exception during mark test");
173 * @tests java.io.ByteArrayInputStream#markSupported()
176 level = TestLevel.COMPLETE,
177 notes = "Verifies markSupported() method.",
178 method = "markSupported",
181 public void test_markSupported() {
182 // Test for method boolean java.io.ByteArrayInputStream.markSupported()
183 assertTrue("markSupported returned incorrect value", is.markSupported());
187 * @tests java.io.ByteArrayInputStream#read()
190 level = TestLevel.COMPLETE,
191 notes = "Verifies read() method.",
195 public void test_read() {
196 // Test for method int java.io.ByteArrayInputStream.read()
201 assertTrue("read returned incorrect char", c == fileString
203 } catch (Exception e) {
204 fail("Exception during read test");
209 * @tests java.io.ByteArrayInputStream#read(byte[], int, int)
212 level = TestLevel.COMPLETE,
214 args = {byte[].class, int.class, int.class}
216 public void test_read$BII() throws IOException {
217 byte[] buf1 = new byte[20];
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)));
225 // Illegal argument checks.
228 fail("Test 2: NullPointerException expected.");
229 } catch (NullPointerException e) {
234 is.read(buf1 , -1, 1);
235 fail("Test 3: IndexOutOfBoundsException expected.");
236 } catch (IndexOutOfBoundsException e) {
241 is.read(buf1 , 1, -1);
242 fail("Test 4: IndexOutOfBoundsException expected.");
243 } catch (IndexOutOfBoundsException e) {
248 is.read(buf1, 1, buf1.length);
249 fail("Test 5: IndexOutOfBoundsException expected.");
250 } catch (IndexOutOfBoundsException e) {
256 * @tests java.io.ByteArrayInputStream#reset()
259 level = TestLevel.COMPLETE,
260 notes = "The test verifies reset() method.",
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];
270 is.read(buf1, 0, 10);
272 is.read(buf2, 0, 10);
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());
282 * @tests java.io.ByteArrayInputStream#skip(long)
285 level = TestLevel.COMPLETE,
290 public void test_skipJ() {
291 // Test for method long java.io.ByteArrayInputStream.skip(long)
292 byte[] buf1 = new byte[10];
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());
304 * Sets up the fixture, for example, open a network connection. This method
305 * is called before a test is executed.
307 protected void setUp() {
309 is = new java.io.ByteArrayInputStream(fileString.getBytes());
314 * Tears down the fixture, for example, close a network connection. This
315 * method is called after a test is executed.
317 protected void tearDown() {
322 } catch (Exception e) {