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 dalvik.annotation.TestTargets;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetNew;
23 import dalvik.annotation.TestTargetClass;
25 import java.io.InputStream;
26 import java.io.IOException;
27 import java.io.ObjectInput;
29 @TestTargetClass(InputStream.class)
30 public class InputStreamTest extends junit.framework.TestCase {
32 public static final String testString = "Lorem ipsum dolor sit amet,\n" +
33 "consectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut" +
34 "labore et dolore magna aliqua.\n";
36 private InputStream is;
38 class MockInputStream extends java.io.InputStream {
43 public MockInputStream() {
45 input = testString.getBytes();
49 public int read() throws IOException {
51 if (position < input.length) {
52 result = input[position];
60 * @tests java.io.InputStream#InputStream()
63 level = TestLevel.COMPLETE,
64 notes = "Verifies constructor InputStream(). Since this constructor does nothing, this test is intentionally kept basic.",
65 method = "InputStream",
68 public void test_Constructor() {
70 InputStream myIS = new MockInputStream();
72 } catch (Exception e) {
73 fail("Unexpected exception: " + e.getMessage());
78 * @tests java.io.InputStream#available()
81 level = TestLevel.COMPLETE,
82 notes = "Verifies that available() returns 0.",
86 public void test_available() throws IOException {
87 assertEquals(is.available(), 0);
91 * @tests java.io.InputStream#close()
94 level = TestLevel.COMPLETE,
95 notes = "Verifies close(). Since this method does nothing, this test is intentionally kept basic.",
99 public void test_close() {
102 } catch (Exception e) {
103 fail("Unexpected exception: " + e.getMessage());
108 * @tests java.io.InputStream#mark(int)
111 level = TestLevel.COMPLETE,
112 notes = "Verifies mark(int). Since this method does nothing, this test is intentionally kept basic.",
116 public void test_markI() {
119 } catch (Exception e) {
120 fail("Unexpected exception: " + e.getMessage());
125 * @tests java.io.InputStream#markSupported()
128 level = TestLevel.COMPLETE,
129 notes = "Verifies that markSupported() returns false.",
130 method = "markSupported",
133 public void test_markSupported() throws IOException {
134 assertFalse("markSupported() has returned the wrong default value.",
139 * @tests java.io.InputStream#read(byte[])
143 level = TestLevel.COMPLETE,
144 notes = "Verifies read(byte[]).",
146 args = {byte[].class}
149 level = TestLevel.COMPLETE,
150 notes = "Verifies ObjectInput.read(byte[]) since " +
151 "ObjectInputStream inherits the implementation " +
152 "of read(byte[]) from InputStream.",
153 clazz = ObjectInput.class,
155 args = {byte[].class}
160 public void test_read$B() throws IOException {
161 byte[] b = new byte[10];
162 byte[] ref = testString.getBytes();
163 boolean equal = true;
167 // Test 1: This read operation should complete without an error.
168 assertEquals("Test 1: Incorrect count of bytes read.",
171 for (i = 0; i < 10; i++) {
172 equal &= (b[i] == ref[i]);
174 assertTrue("Test 1: Wrong bytes read.", equal);
176 // Test 2: Test that the correct number of bytes read is returned
177 // if the source has less bytes available than fit in the buffer.
178 b = new byte[ref.length];
179 bytesRead = is.read(b);
180 assertEquals("Test 2: Incorrect count of bytes read.",
181 bytesRead, ref.length - 10);
183 for (i = 0; i < bytesRead; i++) {
184 equal &= (b[i] == ref[i + 10]);
186 assertTrue("Test 2: Wrong bytes read.", equal);
188 // Test 3: Since we have now reached the end of the source,
189 // the next call of read(byte[]) should return -1.
190 bytesRead = is.read(b);
191 assertEquals("Test 3:", bytesRead, -1);
195 * @tests java.io.InputStream#read(byte[], int, int)
198 level = TestLevel.PARTIAL,
199 notes = "Verifies argument checking of read(byte[], int, int).",
201 args = {byte[].class, int.class, int.class}
203 public void test_read$BII_Exception() throws IOException {
204 byte[] b = new byte[10];
207 // Test 1: Invalid offset.
209 bytesRead = is.read(b, -1, 5);
210 fail("Test 1: IndexOutOfBoundsException expected.");
211 } catch (IndexOutOfBoundsException e) {
215 // Test 2: Invalid length.
217 bytesRead = is.read(b, 5, -1);
218 fail("Test 2: IndexOutOfBoundsException expected.");
219 } catch (IndexOutOfBoundsException e) {
223 // Test 3: Invalid offset and length combination (sum is larger
224 // than the length of b).
226 bytesRead = is.read(b, 6, 5);
227 fail("Test 3: IndexOutOfBoundsException expected.");
228 } catch (IndexOutOfBoundsException e) {
232 // Test 4: Border case.
234 bytesRead = is.read(b, 6, 4);
235 } catch (IndexOutOfBoundsException e) {
236 fail("Test 4: Unexpected IndexOutOfBoundsException.");
238 assertEquals("Test 4:", bytesRead, 4);
240 // Test 5: Border case.
242 bytesRead = is.read(b, 6, 0);
243 } catch (Exception e) {
244 fail("Test 5: Unexpected Exception " + e.getMessage());
246 assertEquals("Test 5:", bytesRead, 0);
250 * @tests java.io.InputStream#read(byte[], int, int)
253 level = TestLevel.COMPLETE,
254 notes = "Verifies read(byte[], int, int).",
256 args = {byte[].class, int.class, int.class}
258 public void test_read$BII() throws IOException {
259 byte[] b = new byte[10];
260 byte[] ref = testString.getBytes();
261 boolean equal = true;
265 // Test 1: This read operation should complete without an error.
266 assertEquals("Test 1: Incorrect count of bytes read.",
267 is.read(b, 0, 5), 5);
269 for (i = 0; i < 5; i++) {
270 equal &= (b[i] == ref[i]);
272 assertTrue("Test 1: Wrong bytes read.", equal);
274 // Test 2: Read operation with offset.
275 assertEquals("Test 2: Incorrect count of bytes read.",
276 is.read(b, 5, 3), 3);
278 for (i = 5; i < 8; i++) {
279 equal &= (b[i] == ref[i]);
281 assertTrue("Test 2: Wrong bytes read.", equal);
283 // Test 3: Test that the correct number of bytes read is returned
284 // if the source has less bytes available than fit in the buffer.
285 b = new byte[ref.length];
286 bytesRead = is.read(b, 2, b.length - 2);
288 // 8 bytes have been read during tests 1 and 2.
289 assertEquals("Test 3: Incorrect count of bytes read.",
290 bytesRead, ref.length - 8);
292 // The first two bytes of b should be 0 because of the offset.
293 assertEquals("Test 3:", b[0], 0);
294 assertEquals("Test 3:", b[1], 0);
295 for (i = 0; i < bytesRead; i++) {
296 equal &= (b[i + 2] == ref[i + 8]);
298 assertTrue("Test 2: Wrong bytes read.", equal);
300 // Test 4: Since we have now reached the end of the source,
301 // the next call of read(byte[], int, int) should return -1.
302 assertEquals("Test 4:", is.read(b, 0, 2), -1);
306 * @tests java.io.InputStream#reset()
309 level = TestLevel.COMPLETE,
310 notes = "Verifies that reset() throws an IOException.",
314 public void test_reset() {
317 fail("IOException expected.");
318 } catch (IOException e) {
324 * @tests java.io.InputStream#skip(long)
328 level = TestLevel.COMPLETE,
329 notes = "Verifies skip(long).",
334 level = TestLevel.COMPLETE,
335 notes = "Verifies ObjectInput.skip(long) since " +
336 "ObjectInputStream inherits the implementation " +
337 "of skip(long) from InputStream.",
338 clazz = ObjectInput.class,
344 public void test_skipL() throws IOException {
345 byte[] b = new byte[12];
346 byte[] ref = testString.getBytes();
348 boolean equal = true;
350 // Test 1: Check that skip(long) just returns 0 if called with a
352 assertEquals("Test 1:", is.skip(-42), 0);
353 assertEquals("Test 1: Incorrect count of bytes read.",
355 for (i = 0; i < 12; i++) {
356 equal &= (b[i] == ref[i]);
358 assertTrue("Test 1: Wrong bytes read.", equal);
360 // Test 2: Check if the number of bytes skipped matches the requested
361 // number of bytes to skip.
362 assertEquals("Test 2: Incorrect count of bytes skipped.",
365 // Test 3: Check that bytes have actually been skipped
367 for (i = 0; i < 10; i++) {
368 equal &= (b[i] == ref[i + 29]);
370 assertTrue("Test 3: Wrong bytes read.", equal);
372 // Test 4: Test that the correct number of bytes skipped is returned
373 // if the source has less bytes available than fit in the buffer.
374 assertEquals("Test 4: Incorrect count of bytes skipped.",
375 is.skip(ref.length), ref.length - 39);
377 // Test 5: Since we have now reached the end of the source,
378 // the next call of read(byte[]) should return -1 and calling
379 // skip(long) should return 0.
380 assertEquals("Test 5:", is.read(b), -1);
381 assertEquals("Test 5:", is.skip(10), 0);
385 * This method is called before a test is executed. It creates a
386 * MockInputStream instance.
388 protected void setUp() {
389 is = new MockInputStream();
393 * This method is called after a test is executed. It closes the
394 * MockInputStream instance.
396 protected void tearDown() {
399 } catch (Exception e) {