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.BufferedInputStream; 21import java.io.FilterInputStream; 22import java.io.IOException; 23import java.util.Arrays; 24 25import tests.support.Support_ASimpleInputStream; 26import tests.support.Support_PlatformFile; 27import dalvik.annotation.TestLevel; 28import dalvik.annotation.TestTargetClass; 29import dalvik.annotation.TestTargetNew; 30import dalvik.annotation.TestTargets; 31 32@TestTargetClass(FilterInputStream.class) 33public class FilterInputStreamTest extends junit.framework.TestCase { 34 35 static class MyFilterInputStream extends java.io.FilterInputStream { 36 public MyFilterInputStream(java.io.InputStream is) { 37 super(is); 38 } 39 } 40 41 private String fileName; 42 43 private FilterInputStream is; 44 45 byte[] ibuf = new byte[4096]; 46 47 private static final String testString = "Lorem ipsum dolor sit amet,\n" + 48 "consectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut" + 49 "labore et dolore magna aliqua.\n"; 50 51 private static final int testLength = testString.length(); 52 53 /** 54 * @tests java.io.FilterInputStream#InputStream() 55 */ 56 @TestTargetNew( 57 level = TestLevel.COMPLETE, 58 notes = "Verifies constructor FilterInputStream(InputStream).", 59 method = "FilterInputStream", 60 args = {java.io.InputStream.class} 61 ) 62 public void test_Constructor() { 63 // The FilterInputStream object has already been created in setUp(). 64 // If anything has gone wrong, closing it should throw a 65 // NullPointerException. 66 try { 67 is.close(); 68 } catch (IOException e) { 69 fail("Unexpected IOException: " + e.getMessage()); 70 } catch (NullPointerException npe) { 71 fail("Unexpected NullPointerException."); 72 } 73 } 74 75 /** 76 * @tests java.io.FilterInputStream#available() 77 */ 78 @TestTargetNew( 79 level = TestLevel.COMPLETE, 80 method = "available", 81 args = {} 82 ) 83 public void test_available() throws IOException { 84 assertEquals("Test 1: Returned incorrect number of available bytes;", 85 testLength, is.available()); 86 87 is.close(); 88 try { 89 is.available(); 90 fail("Test 2: IOException expected."); 91 } catch (IOException e) { 92 // Expected. 93 } 94 } 95 96 /** 97 * @tests java.io.FilterInputStream#close() 98 */ 99 @TestTargetNew( 100 level = TestLevel.COMPLETE, 101 method = "close", 102 args = {} 103 ) 104 public void test_close() throws IOException { 105 is.close(); 106 107 try { 108 is.read(); 109 fail("Test 1: Read from closed stream succeeded."); 110 } catch (IOException e) { 111 // Expected. 112 } 113 114 Support_ASimpleInputStream sis = new Support_ASimpleInputStream(true); 115 is = new MyFilterInputStream(sis); 116 try { 117 is.close(); 118 fail("Test 2: IOException expected."); 119 } catch (IOException e) { 120 // Expected. 121 } 122 sis.throwExceptionOnNextUse = false; 123 } 124 125 /** 126 * @tests java.io.FilterInputStream#mark(int) 127 */ 128 @TestTargets({ 129 @TestTargetNew( 130 level = TestLevel.COMPLETE, 131 notes = "Verifies mark(int) in combination with reset().", 132 method = "mark", 133 args = {int.class} 134 ), 135 @TestTargetNew( 136 level = TestLevel.COMPLETE, 137 notes = "Verifies mark(int) in combination with reset().", 138 method = "reset", 139 args = {} 140 ) 141 }) 142 public void test_markI() throws Exception { 143 // Test for method void java.io.FilterInputStream.mark(int) 144 final int bufSize = 10; 145 byte[] buf1 = new byte[bufSize]; 146 byte[] buf2 = new byte[bufSize]; 147 148 // Purpose 1: Check that mark() does nothing if the filtered stream 149 // is a FileInputStream. 150 is.read(buf1, 0, bufSize); 151 is.mark(2 * bufSize); 152 is.read(buf1, 0, bufSize); 153 try { 154 is.reset(); 155 } catch (IOException e) { 156 // Expected 157 } 158 is.read(buf2, 0, bufSize); 159 assertFalse("Test 1: mark() should have no effect.", 160 Arrays.equals(buf1, buf2)); 161 is.close(); 162 163 // Purpose 2: Check that mark() in combination with reset() works if 164 // the filtered stream is a BufferedInputStream. 165 is = new MyFilterInputStream(new BufferedInputStream( 166 new java.io.FileInputStream(fileName), 100)); 167 is.read(buf1, 0, bufSize); 168 is.mark(2 * bufSize); 169 is.read(buf1, 0, bufSize); 170 is.reset(); 171 is.read(buf2, 0, bufSize); 172 assertTrue("Test 2: mark() or reset() has failed.", 173 Arrays.equals(buf1, buf2)); 174 } 175 176 /** 177 * @tests java.io.FilterInputStream#markSupported() 178 */ 179 @TestTargetNew( 180 level = TestLevel.COMPLETE, 181 notes = "Verifies markSupported() method.", 182 method = "markSupported", 183 args = {} 184 ) 185 public void test_markSupported() throws Exception { 186 // Test for method boolean java.io.FilterInputStream.markSupported() 187 188 // Test 1: Check that markSupported() returns false for a filtered 189 // input stream that is known to not support mark(). 190 assertFalse("Test 1: markSupported() incorrectly returned true " + 191 "for a FileInputStream.", is.markSupported()); 192 is.close(); 193 // Test 2: Check that markSupported() returns true for a filtered 194 // input stream that is known to support mark(). 195 is = new MyFilterInputStream(new BufferedInputStream( 196 new java.io.FileInputStream(fileName), 100)); 197 assertTrue("Test 2: markSupported() incorrectly returned false " + 198 "for a BufferedInputStream.", is.markSupported()); 199 } 200 201 /** 202 * @tests java.io.FilterInputStream#read() 203 */ 204 @TestTargetNew( 205 level = TestLevel.COMPLETE, 206 method = "read", 207 args = {} 208 ) 209 public void test_read() throws IOException { 210 int c = is.read(); 211 assertEquals("Test 1: Read returned incorrect char;", 212 testString.charAt(0), c); 213 214 is.close(); 215 try { 216 is.read(); 217 fail("Test 2: IOException expected."); 218 } catch (IOException e) { 219 // Expected. 220 } 221 } 222 223 /** 224 * @tests java.io.FilterInputStream#read(byte[]) 225 */ 226 @TestTargetNew( 227 level = TestLevel.COMPLETE, 228 method = "read", 229 args = {byte[].class} 230 ) 231 public void test_read$B() throws IOException { 232 // Test for method int java.io.FilterInputStream.read(byte []) 233 byte[] buf1 = new byte[100]; 234 is.read(buf1); 235 assertTrue("Test 1: Failed to read correct data.", 236 new String(buf1, 0, buf1.length).equals( 237 testString.substring(0, 100))); 238 239 is.close(); 240 try { 241 is.read(buf1); 242 fail("Test 2: IOException expected."); 243 } catch (IOException e) { 244 // Expected. 245 } 246 } 247 248 /** 249 * @tests java.io.FilterInputStream#read(byte[], int, int) 250 */ 251 @TestTargetNew( 252 level = TestLevel.PARTIAL_COMPLETE, 253 method = "read", 254 args = {byte[].class, int.class, int.class} 255 ) 256 public void test_read$BII() throws IOException { 257 byte[] buf1 = new byte[20]; 258 is.skip(10); 259 is.read(buf1, 0, buf1.length); 260 assertTrue("Failed to read correct data", new String(buf1, 0, 261 buf1.length).equals(testString.substring(10, 30))); 262 } 263 264 /** 265 * @tests FilterInputStream#read(byte[], int, int) 266 */ 267 @TestTargetNew( 268 level = TestLevel.PARTIAL_COMPLETE, 269 notes = "Illegal argument checks.", 270 method = "read", 271 args = {byte[].class, int.class, int.class} 272 ) 273 public void test_read$BII_Exception() throws IOException { 274 byte[] buf = null; 275 try { 276 is.read(buf, -1, 0); 277 fail("Test 1: NullPointerException expected."); 278 } catch (NullPointerException e) { 279 // Expected. 280 } 281 282 buf = new byte[1000]; 283 try { 284 is.read(buf, -1, 0); 285 fail("Test 2: IndexOutOfBoundsException expected."); 286 } catch (IndexOutOfBoundsException e) { 287 // Expected. 288 } 289 290 try { 291 is.read(buf, 0, -1); 292 fail("Test 3: IndexOutOfBoundsException expected."); 293 } catch (IndexOutOfBoundsException e) { 294 // Expected. 295 } 296 297 try { 298 is.read(buf, -1, -1); 299 fail("Test 4: IndexOutOfBoundsException expected."); 300 } catch (IndexOutOfBoundsException e) { 301 // Expected. 302 } 303 304 try { 305 is.read(buf, 0, 1001); 306 fail("Test 5: IndexOutOfBoundsException expected."); 307 } catch (IndexOutOfBoundsException e) { 308 // Expected. 309 } 310 311 try { 312 is.read(buf, 1001, 0); 313 fail("Test 6: IndexOutOfBoundsException expected."); 314 } catch (IndexOutOfBoundsException e) { 315 // Expected. 316 } 317 318 try { 319 is.read(buf, 500, 501); 320 fail("Test 7: IndexOutOfBoundsException expected."); 321 } catch (IndexOutOfBoundsException e) { 322 // Expected. 323 } 324 325 is.close(); 326 try { 327 is.read(buf, 0, 100); 328 fail("Test 8: IOException expected."); 329 } catch (IOException e) { 330 // Expected. 331 } 332 } 333 334 /** 335 * @tests java.io.FilterInputStream#reset() 336 */ 337 @TestTargets({ 338 @TestTargetNew( 339 level = TestLevel.COMPLETE, 340 notes = "Verifies reset() in combination with mark().", 341 method = "reset", 342 args = {} 343 ), 344 @TestTargetNew( 345 level = TestLevel.COMPLETE, 346 notes = "Verifies reset() in combination with mark().", 347 method = "mark", 348 args = { int.class } 349 ) 350 }) 351 public void test_reset() throws Exception { 352 // Test for method void java.io.FilterInputStream.reset() 353 354 // Test 1: Check that reset() throws an IOException if the 355 // filtered stream is a FileInputStream. 356 try { 357 is.reset(); 358 fail("Test 1: IOException expected."); 359 } catch (IOException e) { 360 // expected 361 } 362 363 // Test 2: Check that reset() throws an IOException if the 364 // filtered stream is a BufferedInputStream but mark() has not 365 // yet been called. 366 is = new MyFilterInputStream(new BufferedInputStream( 367 new java.io.FileInputStream(fileName), 100)); 368 try { 369 is.reset(); 370 fail("Test 2: IOException expected."); 371 } catch (IOException e) { 372 // expected 373 } 374 375 // Test 3: Check that reset() in combination with mark() 376 // works correctly. 377 final int bufSize = 10; 378 byte[] buf1 = new byte[bufSize]; 379 byte[] buf2 = new byte[bufSize]; 380 is.read(buf1, 0, bufSize); 381 is.mark(2 * bufSize); 382 is.read(buf1, 0, bufSize); 383 try { 384 is.reset(); 385 } catch (IOException e) { 386 fail("Test 3: Unexpected IOException."); 387 } 388 is.read(buf2, 0, bufSize); 389 assertTrue("Test 4: mark() or reset() has failed.", 390 Arrays.equals(buf1, buf2)); 391 } 392 393 /** 394 * @tests java.io.FilterInputStream#skip(long) 395 */ 396 @TestTargetNew( 397 level = TestLevel.COMPLETE, 398 method = "skip", 399 args = {long.class} 400 ) 401 public void test_skipJ() throws IOException { 402 byte[] buf1 = new byte[10]; 403 is.skip(10); 404 is.read(buf1, 0, buf1.length); 405 assertTrue("Test 1: Failed to skip to the correct position.", 406 new String(buf1, 0, buf1.length).equals( 407 testString.substring(10, 20))); 408 409 is.close(); 410 try { 411 is.read(); 412 fail("Test 2: IOException expected."); 413 } catch (IOException e) { 414 // Expected. 415 } 416 } 417 418 /** 419 * Sets up the fixture, for example, open a network connection. This method 420 * is called before a test is executed. 421 */ 422 protected void setUp() { 423 try { 424 fileName = System.getProperty("java.io.tmpdir"); 425 String separator = System.getProperty("file.separator"); 426 if (fileName.charAt(fileName.length() - 1) == separator.charAt(0)) 427 fileName = Support_PlatformFile.getNewPlatformFile(fileName, 428 "input.tst"); 429 else 430 fileName = Support_PlatformFile.getNewPlatformFile(fileName 431 + separator, "input.tst"); 432 java.io.OutputStream fos = new java.io.FileOutputStream(fileName); 433 fos.write(testString.getBytes()); 434 fos.close(); 435 is = new MyFilterInputStream(new java.io.FileInputStream(fileName)); 436 } catch (java.io.IOException e) { 437 System.out.println("Exception during setup"); 438 e.printStackTrace(); 439 } 440 } 441 442 /** 443 * Tears down the fixture, for example, close a network connection. This 444 * method is called after a test is executed. 445 */ 446 protected void tearDown() { 447 try { 448 is.close(); 449 } catch (Exception e) { 450 System.out.println("Unexpected exception in tearDown()."); 451 } 452 new java.io.File(fileName).delete(); 453 } 454} 455