InputStreamTest.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 dalvik.annotation.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import java.io.InputStream; 26import java.io.IOException; 27import java.io.ObjectInput; 28 29@TestTargetClass(InputStream.class) 30public class InputStreamTest extends junit.framework.TestCase { 31 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"; 35 36 private InputStream is; 37 38 class MockInputStream extends java.io.InputStream { 39 40 private byte[] input; 41 private int position; 42 43 public MockInputStream() { 44 super(); 45 input = testString.getBytes(); 46 position = 0; 47 } 48 49 public int read() throws IOException { 50 int result = -1; 51 if (position < input.length) { 52 result = input[position]; 53 position++; 54 } 55 return result; 56 } 57 } 58 59 /** 60 * @tests java.io.InputStream#InputStream() 61 */ 62 @TestTargetNew( 63 level = TestLevel.COMPLETE, 64 notes = "Verifies constructor InputStream(). Since this constructor does nothing, this test is intentionally kept basic.", 65 method = "InputStream", 66 args = {} 67 ) 68 public void test_Constructor() { 69 try { 70 InputStream myIS = new MockInputStream(); 71 myIS.close(); 72 } catch (Exception e) { 73 fail("Unexpected exception: " + e.getMessage()); 74 } 75 } 76 77 /** 78 * @tests java.io.InputStream#available() 79 */ 80 @TestTargetNew( 81 level = TestLevel.COMPLETE, 82 notes = "Verifies that available() returns 0.", 83 method = "available", 84 args = {} 85 ) 86 public void test_available() throws IOException { 87 assertEquals(is.available(), 0); 88 } 89 90 /** 91 * @tests java.io.InputStream#close() 92 */ 93 @TestTargetNew( 94 level = TestLevel.COMPLETE, 95 notes = "Verifies close(). Since this method does nothing, this test is intentionally kept basic.", 96 method = "close", 97 args = {} 98 ) 99 public void test_close() { 100 try { 101 is.close(); 102 } catch (Exception e) { 103 fail("Unexpected exception: " + e.getMessage()); 104 } 105 } 106 107 /** 108 * @tests java.io.InputStream#mark(int) 109 */ 110 @TestTargetNew( 111 level = TestLevel.COMPLETE, 112 notes = "Verifies mark(int). Since this method does nothing, this test is intentionally kept basic.", 113 method = "mark", 114 args = {int.class} 115 ) 116 public void test_markI() { 117 try { 118 is.mark(10); 119 } catch (Exception e) { 120 fail("Unexpected exception: " + e.getMessage()); 121 } 122 } 123 124 /** 125 * @tests java.io.InputStream#markSupported() 126 */ 127 @TestTargetNew( 128 level = TestLevel.COMPLETE, 129 notes = "Verifies that markSupported() returns false.", 130 method = "markSupported", 131 args = {} 132 ) 133 public void test_markSupported() throws IOException { 134 assertFalse("markSupported() has returned the wrong default value.", 135 is.markSupported()); 136 } 137 138 /** 139 * @tests java.io.InputStream#read(byte[]) 140 */ 141 @TestTargets ( 142 { @TestTargetNew( 143 level = TestLevel.COMPLETE, 144 notes = "Verifies read(byte[]).", 145 method = "read", 146 args = {byte[].class} 147 ), 148 @TestTargetNew( 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, 154 method = "read", 155 args = {byte[].class} 156 ) 157 } 158 159 ) 160 public void test_read$B() throws IOException { 161 byte[] b = new byte[10]; 162 byte[] ref = testString.getBytes(); 163 boolean equal = true; 164 int bytesRead = 0; 165 int i; 166 167 // Test 1: This read operation should complete without an error. 168 assertEquals("Test 1: Incorrect count of bytes read.", 169 is.read(b), 10); 170 171 for (i = 0; i < 10; i++) { 172 equal &= (b[i] == ref[i]); 173 } 174 assertTrue("Test 1: Wrong bytes read.", equal); 175 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); 182 183 for (i = 0; i < bytesRead; i++) { 184 equal &= (b[i] == ref[i + 10]); 185 } 186 assertTrue("Test 2: Wrong bytes read.", equal); 187 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); 192 } 193 194 /** 195 * @tests java.io.InputStream#read(byte[], int, int) 196 */ 197 @TestTargetNew( 198 level = TestLevel.PARTIAL, 199 notes = "Verifies argument checking of read(byte[], int, int).", 200 method = "read", 201 args = {byte[].class, int.class, int.class} 202 ) 203 public void test_read$BII_Exception() throws IOException { 204 byte[] b = new byte[10]; 205 int bytesRead = 0; 206 207 // Test 1: Invalid offset. 208 try { 209 bytesRead = is.read(b, -1, 5); 210 fail("Test 1: IndexOutOfBoundsException expected."); 211 } catch (IndexOutOfBoundsException e) { 212 // expected 213 } 214 215 // Test 2: Invalid length. 216 try { 217 bytesRead = is.read(b, 5, -1); 218 fail("Test 2: IndexOutOfBoundsException expected."); 219 } catch (IndexOutOfBoundsException e) { 220 // expected 221 } 222 223 // Test 3: Invalid offset and length combination (sum is larger 224 // than the length of b). 225 try { 226 bytesRead = is.read(b, 6, 5); 227 fail("Test 3: IndexOutOfBoundsException expected."); 228 } catch (IndexOutOfBoundsException e) { 229 // expected 230 } 231 232 // Test 4: Border case. 233 try { 234 bytesRead = is.read(b, 6, 4); 235 } catch (IndexOutOfBoundsException e) { 236 fail("Test 4: Unexpected IndexOutOfBoundsException."); 237 } 238 assertEquals("Test 4:", bytesRead, 4); 239 240 // Test 5: Border case. 241 try { 242 bytesRead = is.read(b, 6, 0); 243 } catch (Exception e) { 244 fail("Test 5: Unexpected Exception " + e.getMessage()); 245 } 246 assertEquals("Test 5:", bytesRead, 0); 247 } 248 249 /** 250 * @tests java.io.InputStream#read(byte[], int, int) 251 */ 252 @TestTargetNew( 253 level = TestLevel.COMPLETE, 254 notes = "Verifies read(byte[], int, int).", 255 method = "read", 256 args = {byte[].class, int.class, int.class} 257 ) 258 public void test_read$BII() throws IOException { 259 byte[] b = new byte[10]; 260 byte[] ref = testString.getBytes(); 261 boolean equal = true; 262 int bytesRead = 0; 263 int i; 264 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); 268 269 for (i = 0; i < 5; i++) { 270 equal &= (b[i] == ref[i]); 271 } 272 assertTrue("Test 1: Wrong bytes read.", equal); 273 274 // Test 2: Read operation with offset. 275 assertEquals("Test 2: Incorrect count of bytes read.", 276 is.read(b, 5, 3), 3); 277 278 for (i = 5; i < 8; i++) { 279 equal &= (b[i] == ref[i]); 280 } 281 assertTrue("Test 2: Wrong bytes read.", equal); 282 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); 287 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); 291 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]); 297 } 298 assertTrue("Test 2: Wrong bytes read.", equal); 299 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); 303 } 304 305 /** 306 * @tests java.io.InputStream#reset() 307 */ 308 @TestTargetNew( 309 level = TestLevel.COMPLETE, 310 notes = "Verifies that reset() throws an IOException.", 311 method = "reset", 312 args = {} 313 ) 314 public void test_reset() { 315 try { 316 is.reset(); 317 fail("IOException expected."); 318 } catch (IOException e) { 319 // expected 320 } 321 } 322 323 /** 324 * @tests java.io.InputStream#skip(long) 325 */ 326 @TestTargets ( 327 { @TestTargetNew( 328 level = TestLevel.COMPLETE, 329 notes = "Verifies skip(long).", 330 method = "skip", 331 args = {long.class} 332 ), 333 @TestTargetNew( 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, 339 method = "skip", 340 args = {long.class} 341 ) 342 } 343 ) 344 public void test_skipL() throws IOException { 345 byte[] b = new byte[12]; 346 byte[] ref = testString.getBytes(); 347 int i; 348 boolean equal = true; 349 350 // Test 1: Check that skip(long) just returns 0 if called with a 351 // negative number. 352 assertEquals("Test 1:", is.skip(-42), 0); 353 assertEquals("Test 1: Incorrect count of bytes read.", 354 is.read(b), 12); 355 for (i = 0; i < 12; i++) { 356 equal &= (b[i] == ref[i]); 357 } 358 assertTrue("Test 1: Wrong bytes read.", equal); 359 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.", 363 is.skip(17), 17); 364 365 // Test 3: Check that bytes have actually been skipped 366 is.read(b, 0, 10); 367 for (i = 0; i < 10; i++) { 368 equal &= (b[i] == ref[i + 29]); 369 } 370 assertTrue("Test 3: Wrong bytes read.", equal); 371 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); 376 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); 382 } 383 384 /** 385 * This method is called before a test is executed. It creates a 386 * MockInputStream instance. 387 */ 388 protected void setUp() { 389 is = new MockInputStream(); 390 } 391 392 /** 393 * This method is called after a test is executed. It closes the 394 * MockInputStream instance. 395 */ 396 protected void tearDown() { 397 try { 398 is.close(); 399 } catch (Exception e) { 400 } 401 } 402} 403