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 org.apache.harmony.tests.java.io; 19 20import java.io.File; 21import java.io.FileInputStream; 22import java.io.FileNotFoundException; 23import java.io.FileOutputStream; 24import java.io.IOException; 25import java.io.InputStreamReader; 26import java.nio.charset.StandardCharsets; 27import junit.framework.TestCase; 28 29public class FileInputStreamTest extends TestCase { 30 31 private String fileName; 32 33 private java.io.InputStream is; 34 35 private static final String INPUT = 36 "Test_All_Tests\n" + 37 "Test_BufferedInputStream\n" + 38 "Test_java_io_BufferedOutputStream\n" + 39 "Test_java_io_ByteArrayInputStream\n" + 40 "Test_java_io_ByteArrayOutputStream\n" + 41 "Test_java_io_DataInputStream\n" + 42 "Test_java_io_File\n" + 43 "Test_java_io_FileDescriptor\n" + 44 "Test_java_io_FileInputStream\n" + 45 "Test_java_io_FileNotFoundException\n" + 46 "Test_java_io_FileOutputStream\n" + 47 "Test_java_io_FilterInputStream\n" + 48 "Test_java_io_FilterOutputStream\n" + 49 "Test_java_io_InputStream\n" + 50 "Test_java_io_IOException\n" + 51 "Test_java_io_OutputStream\n" + 52 "Test_java_io_PrintStream\n" + 53 "Test_java_io_RandomAccessFile\n" + 54 "Test_java_io_SyncFailedException\n" + 55 "Test_java_lang_AbstractMethodError\n" + 56 "Test_java_lang_ArithmeticException\n" + 57 "Test_java_lang_ArrayIndexOutOfBoundsException\n" + 58 "Test_java_lang_ArrayStoreException\n" + 59 "Test_java_lang_Boolean\n" + 60 "Test_java_lang_Byte\n" + 61 "Test_java_lang_Character\n" + 62 "Test_All_Tests\n" + 63 "Test_BufferedInputStream\n" + 64 "Test_java_io_BufferedOutputStream\n" + 65 "Test_java_io_ByteArrayInputStream\n" + 66 "Test_java_io_ByteArrayOutputStream\n" + 67 "Test_java_io_DataInputStream\n" + 68 "Test_java_io_File\n" + 69 "Test_java_io_FileDescriptor\n" + 70 "Test_java_io_FileInputStream\n" + 71 "Test_java_io_FileNotFoundException\n" + 72 "Test_java_io_FileOutputStream\n" + 73 "Test_java_io_FilterInputStream\n" + 74 "Test_java_io_FilterOutputStream\n" + 75 "Test_java_io_InputStream\n" + 76 "Test_java_io_IOException\n" + 77 "Test_java_io_OutputStream\n" + 78 "Test_java_io_PrintStream\n" + 79 "Test_java_io_RandomAccessFile\n" + 80 "Test_java_io_SyncFailedException\n" + 81 "Test_java_lang_AbstractMethodError\n" + 82 "Test_java_lang_ArithmeticException\n" + 83 "Test_java_lang_ArrayIndexOutOfBoundsException\n" + 84 "Test_java_lang_ArrayStoreException\n" + 85 "Test_java_lang_Boolean\n" + 86 "Test_java_lang_Byte\n" + 87 "Test_java_lang_Character\n"; 88 89 @Override 90 protected void setUp() throws IOException { 91 File f = File.createTempFile("FileInputStreamTest", "tst"); 92 93 FileOutputStream fos = null; 94 try { 95 fos = new FileOutputStream(f.getAbsolutePath()); 96 fos.write(INPUT.getBytes(StandardCharsets.US_ASCII)); 97 } finally { 98 fos.close(); 99 } 100 101 fileName = f.getAbsolutePath(); 102 } 103 104 /** 105 * java.io.FileInputStream#FileInputStream(java.io.File) 106 */ 107 public void test_ConstructorLjava_io_File() throws IOException { 108 java.io.File f = new File(fileName); 109 is = new FileInputStream(f); 110 is.close(); 111 } 112 113 /** 114 * java.io.FileInputStream#FileInputStream(java.io.FileDescriptor) 115 */ 116 public void test_ConstructorLjava_io_FileDescriptor() throws IOException { 117 FileOutputStream fos = new FileOutputStream(fileName); 118 FileInputStream fis = new FileInputStream(fos.getFD()); 119 fos.close(); 120 fis.close(); 121 } 122 123 /** 124 * java.io.FileInputStream#FileInputStream(java.lang.String) 125 */ 126 public void test_ConstructorLjava_lang_String() throws IOException { 127 is = new FileInputStream(fileName); 128 is.close(); 129 } 130 131 /** 132 * java.io.FileInputStream#FileInputStream(java.lang.String) 133 */ 134 public void test_ConstructorLjava_lang_String_I() throws IOException { 135 try { 136 is = new FileInputStream(""); 137 fail("should throw FileNotFoundException."); 138 } catch (FileNotFoundException e) { 139 // Expected 140 } finally { 141 if (is != null) { 142 is.close(); 143 } 144 } 145 try { 146 is = new FileInputStream(new File("")); 147 fail("should throw FileNotFoundException."); 148 } catch (FileNotFoundException e) { 149 // Expected 150 } finally { 151 if (is != null) { 152 is.close(); 153 } 154 } 155 } 156 157 /** 158 * java.io.FileInputStream#available() 159 */ 160 public void test_available() throws IOException { 161 try { 162 is = new FileInputStream(fileName); 163 assertTrue(is.available() == INPUT.length()); 164 } finally { 165 try { 166 is.close(); 167 } catch (IOException e) { 168 } 169 } 170 } 171 172 public void test_close() throws IOException { 173 is = new FileInputStream(fileName); 174 is.close(); 175 try { 176 is.read(); 177 fail(); 178 } catch (IOException expected) { 179 } 180 } 181 182 public void test_close_shared_fd() throws IOException { 183 // Regression test for HARMONY-6642 184 FileInputStream fis1 = new FileInputStream(fileName); 185 FileInputStream fis2 = new FileInputStream(fis1.getFD()); 186 187 try { 188 fis2.close(); 189 // Should not throw, since the FD is owned by fis1. 190 fis1.read(); 191 } finally { 192 try { 193 fis1.close(); 194 } catch (IOException e) { 195 } 196 } 197 } 198 199 /** 200 * java.io.FileInputStream#getFD() 201 */ 202 public void test_getFD() throws IOException { 203 FileInputStream fis = new FileInputStream(fileName); 204 assertTrue("Returned invalid fd", fis.getFD().valid()); 205 fis.close(); 206 assertTrue("Returned invalid fd", !fis.getFD().valid()); 207 } 208 209 /** 210 * java.io.FileInputStream#read() 211 */ 212 public void test_read() throws IOException { 213 InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName)); 214 int c = isr.read(); 215 isr.close(); 216 assertEquals(INPUT.charAt(0), c); 217 } 218 219 /** 220 * java.io.FileInputStream#read(byte[]) 221 */ 222 public void test_read$B() throws IOException { 223 byte[] buf1 = new byte[100]; 224 is = new FileInputStream(fileName); 225 is.skip(500); 226 is.read(buf1); 227 is.close(); 228 assertEquals(INPUT.substring(500, 600), 229 new String(buf1, 0, buf1.length, StandardCharsets.US_ASCII)); 230 } 231 232 /** 233 * java.io.FileInputStream#read(byte[], int, int) 234 */ 235 public void test_read$BII() throws IOException { 236 byte[] buf1 = new byte[100]; 237 is = new FileInputStream(fileName); 238 is.skip(500); 239 is.read(buf1, 0, buf1.length); 240 is.close(); 241 assertEquals(INPUT.substring(500, 600), 242 new String(buf1, 0, buf1.length, StandardCharsets.US_ASCII)); 243 244 // Regression test for HARMONY-285 245 File tmpFile = File.createTempFile("FileOutputStream", "tmp"); 246 FileInputStream in = new FileInputStream(tmpFile); 247 try { 248 in.read(null, 0, 0); 249 fail("Should throw NullPointerException"); 250 } catch (NullPointerException e) { 251 // Expected 252 } finally { 253 in.close(); 254 tmpFile.delete(); 255 } 256 } 257 258 /** 259 * java.io.FileInputStream#read(byte[], int, int) 260 */ 261 public void test_read_$BII_IOException() throws IOException { 262 byte[] buf = new byte[1000]; 263 try { 264 is = new FileInputStream(fileName); 265 is.read(buf, -1, 0); 266 fail("should throw IndexOutOfBoundsException"); 267 } catch (IndexOutOfBoundsException e) { 268 // Expected 269 } finally { 270 is.close(); 271 } 272 273 try { 274 is = new FileInputStream(fileName); 275 is.read(buf, 0, -1); 276 fail("should throw IndexOutOfBoundsException"); 277 } catch (IndexOutOfBoundsException e) { 278 // Expected 279 } finally { 280 is.close(); 281 } 282 283 try { 284 is = new FileInputStream(fileName); 285 is.read(buf, -1, -1); 286 fail("should throw IndexOutOfBoundsException"); 287 } catch (IndexOutOfBoundsException e) { 288 // Expected 289 } finally { 290 is.close(); 291 } 292 293 try { 294 is = new FileInputStream(fileName); 295 is.read(buf, 0, 1001); 296 fail("should throw IndexOutOfBoundsException"); 297 } catch (IndexOutOfBoundsException e) { 298 // Expected 299 } finally { 300 is.close(); 301 } 302 303 try { 304 is = new FileInputStream(fileName); 305 is.read(buf, 1001, 0); 306 fail("should throw IndexOutOfBoundsException"); 307 } catch (IndexOutOfBoundsException e) { 308 // Expected 309 } finally { 310 is.close(); 311 } 312 313 try { 314 is = new FileInputStream(fileName); 315 is.read(buf, 500, 501); 316 fail("should throw IndexOutOfBoundsException"); 317 } catch (IndexOutOfBoundsException e) { 318 // Expected 319 } finally { 320 is.close(); 321 } 322 323 try { 324 is = new FileInputStream(fileName); 325 is.close(); 326 is.read(buf, 0, 100); 327 fail("should throw IOException"); 328 } catch (IOException e) { 329 // Expected 330 } finally { 331 is.close(); 332 } 333 334 try { 335 is = new FileInputStream(fileName); 336 is.close(); 337 is.read(buf, 0, 0); 338 } finally { 339 is.close(); 340 } 341 } 342 343 /** 344 * java.io.FileInputStream#read(byte[], int, int) 345 */ 346 public void test_read_$BII_NullPointerException() throws IOException { 347 byte[] buf = null; 348 try { 349 is = new FileInputStream(fileName); 350 is.read(buf, -1, 0); 351 fail("should throw NullPointerException"); 352 } catch (NullPointerException e) { 353 // Expected 354 } finally { 355 is.close(); 356 } 357 } 358 359 /** 360 * java.io.FileInputStream#read(byte[], int, int) 361 */ 362 public void test_read_$BII_IndexOutOfBoundsException() throws IOException { 363 byte[] buf = new byte[1000]; 364 try { 365 is = new FileInputStream(fileName); 366 is.close(); 367 is.read(buf, -1, -1); 368 fail("should throw IndexOutOfBoundsException"); 369 } catch (IndexOutOfBoundsException e) { 370 // Expected 371 } finally { 372 is.close(); 373 } 374 } 375 376 /** 377 * java.io.FileInputStream#skip(long) 378 */ 379 public void test_skipJ() throws IOException { 380 byte[] buf1 = new byte[10]; 381 is = new FileInputStream(fileName); 382 is.skip(1000); 383 is.read(buf1, 0, buf1.length); 384 is.close(); 385 assertEquals(INPUT.substring(1000, 1010), 386 new String(buf1, 0, buf1.length, StandardCharsets.US_ASCII)); 387 388 } 389 390 /** 391 * java.io.FileInputStream#read(byte[], int, int)) 392 */ 393 public void test_regressionNNN() throws IOException { 394 // Regression for HARMONY-434 395 FileInputStream fis = new FileInputStream(fileName); 396 397 try { 398 fis.read(new byte[1], -1, 1); 399 fail("IndexOutOfBoundsException must be thrown if off <0"); 400 } catch (IndexOutOfBoundsException e) { 401 // Expected 402 } 403 404 try { 405 fis.read(new byte[1], 0, -1); 406 fail("IndexOutOfBoundsException must be thrown if len <0"); 407 } catch (IndexOutOfBoundsException e) { 408 // Expected 409 } 410 411 try { 412 fis.read(new byte[1], 0, 5); 413 fail("IndexOutOfBoundsException must be thrown if off+len > b.length"); 414 } catch (IndexOutOfBoundsException e) { 415 // Expected 416 } 417 418 try { 419 fis.read(new byte[10], Integer.MAX_VALUE, 5); 420 fail("IndexOutOfBoundsException expected"); 421 } catch (IndexOutOfBoundsException e) { 422 // Expected 423 } 424 425 try { 426 fis.read(new byte[10], 5, Integer.MAX_VALUE); 427 fail("IndexOutOfBoundsException expected"); 428 } catch (IndexOutOfBoundsException e) { 429 // Expected 430 } 431 fis.close(); 432 } 433 434 /** 435 * java.io.FileInputStream#skip(long) 436 */ 437 public void test_skipNegativeArgumentJ() throws IOException { 438 FileInputStream fis = new FileInputStream(fileName); 439 try { 440 fis.skip(-5); 441 fail("IOException must be thrown if number of bytes to skip <0"); 442 } catch (IOException e) { 443 // Expected IOException 444 } finally { 445 fis.close(); 446 } 447 } 448 449 public void test_getChannel() throws Exception { 450 FileInputStream fis = new FileInputStream(fileName); 451 assertEquals(0, fis.getChannel().position()); 452 int r; 453 int count = 1; 454 while ((r = fis.read()) != -1) { 455 assertEquals(count++, fis.getChannel().position()); 456 } 457 fis.close(); 458 459 try { 460 fis.getChannel().position(); 461 fail("should throw ClosedChannelException"); 462 } catch (java.nio.channels.ClosedChannelException e) { 463 // Expected 464 } 465 466 fis = new FileInputStream(fileName); 467 assertEquals(0, fis.getChannel().position()); 468 byte[] bs = new byte[10]; 469 r = fis.read(bs); 470 assertEquals(10, fis.getChannel().position()); 471 fis.close(); 472 473 fis = new FileInputStream(fileName); 474 assertEquals(0, fis.getChannel().position()); 475 bs = new byte[10]; 476 fis.skip(100); 477 assertEquals(100, fis.getChannel().position()); 478 r = fis.read(bs); 479 assertEquals(110, fis.getChannel().position()); 480 fis.close(); 481 } 482} 483