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.EOFException; 21import java.io.File; 22import java.io.FileInputStream; 23import java.io.FileNotFoundException; 24import java.io.FileOutputStream; 25import java.io.IOException; 26import java.io.RandomAccessFile; 27import java.nio.channels.FileChannel; 28import java.nio.channels.NonWritableChannelException; 29import libcore.junit.junit3.TestCaseWithRules; 30import libcore.junit.util.ResourceLeakageDetector; 31import org.junit.Rule; 32import org.junit.rules.TestRule; 33 34public class RandomAccessFileTest extends TestCaseWithRules { 35 @Rule 36 public TestRule resourceLeakageDetectorRule = ResourceLeakageDetector.getRule(); 37 38 public String fileName; 39 40 public boolean ufile = true; 41 42 java.io.RandomAccessFile raf; 43 44 java.io.File f; 45 46 String unihw = "\u0048\u0065\u006C\u0801\u006C\u006F\u0020\u0057\u0081\u006F\u0072\u006C\u0064"; 47 48 //java.io.FileOutputStream fos; 49 50 public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n"; 51 52 /** 53 * java.io.RandomAccessFile#RandomAccessFile(java.io.File, 54 *java.lang.String) 55 */ 56 public void test_ConstructorLjava_io_FileLjava_lang_String() 57 throws Exception { 58 // Test for method java.io.RandomAccessFile(java.io.File, 59 // java.lang.String) 60 RandomAccessFile raf = new java.io.RandomAccessFile(f, "rw"); 61 raf.write(20); 62 raf.seek(0); 63 assertEquals("Incorrect int read/written", 20, raf.read()); 64 raf.close(); 65 66 raf = new java.io.RandomAccessFile(f, "rwd"); 67 raf.write(20); 68 raf.seek(0); 69 assertEquals("Incorrect int read/written", 20, raf.read()); 70 raf.close(); 71 72 raf = new java.io.RandomAccessFile(f, "rws"); 73 raf.write(20); 74 raf.seek(0); 75 assertEquals("Incorrect int read/written", 20, raf.read()); 76 raf.close(); 77 78 // Regression for HARMONY-50 79 File f = File.createTempFile("xxx", "yyy"); 80 f.deleteOnExit(); 81 raf = new RandomAccessFile(f, "rws"); 82 raf.close(); 83 84 f = File.createTempFile("xxx", "yyy"); 85 f.deleteOnExit(); 86 raf = new RandomAccessFile(f, "rwd"); 87 raf.close(); 88 } 89 90 /** 91 * java.io.RandomAccessFile#RandomAccessFile(java.lang.String, 92 *java.lang.String) 93 */ 94 public void test_ConstructorLjava_lang_StringLjava_lang_String() 95 throws IOException { 96 // Test for method java.io.RandomAccessFile(java.lang.String, 97 // java.lang.String) 98 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 99 raf.write("Test".getBytes(), 0, 4); 100 raf.close(); 101 102 raf = new java.io.RandomAccessFile(fileName, "rwd"); 103 raf.write("Test".getBytes(), 0, 4); 104 raf.close(); 105 106 raf = new java.io.RandomAccessFile(fileName, "rws"); 107 raf.write("Test".getBytes(), 0, 4); 108 raf.close(); 109 } 110 111 /** 112 * java.io.RandomAccessFile#RandomAccessFile(java.lang.String, 113 *java.lang.String) 114 */ 115 public void test_ConstructorLjava_lang_StringLjava_lang_String_I() 116 throws IOException { 117 RandomAccessFile raf = null; 118 try { 119 raf = new RandomAccessFile("", "r"); 120 fail("should throw FileNotFoundException."); 121 } catch (FileNotFoundException e) { 122 // Expected 123 } finally { 124 if (raf != null) { 125 raf.close(); 126 raf = null; 127 } 128 } 129 try { 130 raf = new RandomAccessFile(new File(""), "r"); 131 fail("should throw FileNotFoundException."); 132 } catch (FileNotFoundException e) { 133 // Expected 134 } finally { 135 if (raf != null) { 136 raf.close(); 137 raf = null; 138 } 139 } 140 File dir = new File("/"); 141 assertTrue(dir.isDirectory()); 142 try { 143 raf = new RandomAccessFile(dir.getPath(), "r"); 144 fail(); 145 } catch (FileNotFoundException expected) { 146 } finally { 147 if (raf != null) { 148 raf.close(); 149 raf = null; 150 } 151 } 152 } 153 154 /** 155 * java.io.RandomAccessFile#close() 156 */ 157 public void test_close() { 158 // Test for method void java.io.RandomAccessFile.close() 159 try { 160 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 161 raf.close(); 162 raf.write("Test".getBytes(), 0, 4); 163 fail("Failed to close file properly"); 164 } catch (IOException e) { 165 } 166 } 167 168 /** 169 * java.io.RandomAccessFile#getFD() 170 */ 171 public void test_getFD() throws IOException { 172 // Test for method java.io.FileDescriptor 173 // java.io.RandomAccessFile.getFD() 174 175 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 176 assertTrue("Returned invalid fd", raf.getFD().valid()); 177 178 raf.close(); 179 assertFalse("Returned valid fd after close", raf.getFD().valid()); 180 } 181 182 /** 183 * java.io.RandomAccessFile#getFilePointer() 184 */ 185 public void test_getFilePointer() throws IOException { 186 // Test for method long java.io.RandomAccessFile.getFilePointer() 187 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 188 raf.write(fileString.getBytes(), 0, 1000); 189 assertEquals("Incorrect filePointer returned", 1000, raf 190 .getFilePointer()); 191 raf.close(); 192 } 193 194 /** 195 * java.io.RandomAccessFile#length() 196 */ 197 public void test_length() throws IOException { 198 // Test for method long java.io.RandomAccessFile.length() 199 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 200 raf.write(fileString.getBytes()); 201 assertEquals("Incorrect length returned", fileString.length(), raf 202 .length()); 203 raf.close(); 204 } 205 206 /** 207 * java.io.RandomAccessFile#read() 208 */ 209 public void test_read() throws IOException { 210 // Test for method int java.io.RandomAccessFile.read() 211 FileOutputStream fos = new java.io.FileOutputStream(fileName); 212 fos.write(fileString.getBytes("UTF-8"), 0, fileString.length()); 213 fos.close(); 214 215 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r"); 216 assertEquals("Incorrect bytes returned from read", 217 fileString.charAt(0), raf.read()); 218 raf.close(); 219 } 220 221 /** 222 * java.io.RandomAccessFile#read(byte[]) 223 */ 224 public void test_read$B() throws IOException { 225 // Test for method int java.io.RandomAccessFile.read(byte []) 226 FileOutputStream fos = new java.io.FileOutputStream(fileName); 227 fos.write(fileString.getBytes(), 0, fileString.length()); 228 fos.close(); 229 230 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r"); 231 byte[] rbuf = new byte[4000]; 232 raf.read(rbuf); 233 assertEquals("Incorrect bytes returned from read", fileString, 234 new String(rbuf, 0, fileString.length())); 235 raf.close(); 236 } 237 238 /** 239 * java.io.RandomAccessFile#read(byte[], int, int) 240 */ 241 public void test_read$BII() throws IOException { 242 // Test for method int java.io.RandomAccessFile.read(byte [], int, int) 243 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 244 byte[] rbuf = new byte[4000]; 245 FileOutputStream fos = new java.io.FileOutputStream(fileName); 246 fos.write(fileString.getBytes(), 0, fileString.length()); 247 fos.close(); 248 raf.read(rbuf, 0, fileString.length()); 249 assertEquals("Incorrect bytes returned from read", fileString, 250 new String(rbuf, 0, fileString.length())); 251 raf.close(); 252 } 253 254 /** 255 * java.io.RandomAccessFile#readBoolean() 256 */ 257 public void test_readBoolean() throws IOException { 258 // Test for method boolean java.io.RandomAccessFile.readBoolean() 259 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 260 raf.writeBoolean(true); 261 raf.seek(0); 262 assertTrue("Incorrect boolean read/written", raf.readBoolean()); 263 raf.close(); 264 } 265 266 /** 267 * java.io.RandomAccessFile#readByte() 268 */ 269 public void test_readByte() throws IOException { 270 // Test for method byte java.io.RandomAccessFile.readByte() 271 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 272 raf.writeByte(127); 273 raf.seek(0); 274 assertEquals("Incorrect bytes read/written", 127, raf.readByte()); 275 raf.close(); 276 } 277 278 /** 279 * java.io.RandomAccessFile#readChar() 280 */ 281 public void test_readChar() throws IOException { 282 // Test for method char java.io.RandomAccessFile.readChar() 283 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 284 raf.writeChar('T'); 285 raf.seek(0); 286 assertEquals("Incorrect char read/written", 'T', raf.readChar()); 287 raf.close(); 288 } 289 290 /** 291 * java.io.RandomAccessFile#readDouble() 292 */ 293 public void test_readDouble() throws IOException { 294 // Test for method double java.io.RandomAccessFile.readDouble() 295 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 296 raf.writeDouble(Double.MAX_VALUE); 297 raf.seek(0); 298 assertEquals("Incorrect double read/written", Double.MAX_VALUE, raf 299 .readDouble(), 0); 300 raf.close(); 301 } 302 303 /** 304 * java.io.RandomAccessFile#readFloat() 305 */ 306 public void test_readFloat() throws IOException { 307 // Test for method float java.io.RandomAccessFile.readFloat() 308 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 309 raf.writeFloat(Float.MAX_VALUE); 310 raf.seek(0); 311 assertEquals("Incorrect float read/written", Float.MAX_VALUE, raf 312 .readFloat(), 0); 313 raf.close(); 314 } 315 316 /** 317 * java.io.RandomAccessFile#readFully(byte[]) 318 */ 319 public void test_readFully$B() throws IOException { 320 // Test for method void java.io.RandomAccessFile.readFully(byte []) 321 byte[] buf = new byte[10]; 322 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 323 raf.writeBytes("HelloWorld"); 324 raf.seek(0); 325 raf.readFully(buf); 326 assertEquals("Incorrect bytes read/written", "HelloWorld", new String( 327 buf, 0, 10, "UTF-8")); 328 raf.close(); 329 } 330 331 /** 332 * java.io.RandomAccessFile#readFully(byte[], int, int) 333 */ 334 public void test_readFully$BII() throws IOException { 335 // Test for method void java.io.RandomAccessFile.readFully(byte [], int, 336 // int) 337 byte[] buf = new byte[10]; 338 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 339 raf.writeBytes("HelloWorld"); 340 raf.seek(0); 341 raf.readFully(buf, 0, buf.length); 342 assertEquals("Incorrect bytes read/written", "HelloWorld", new String( 343 buf, 0, 10, "UTF-8")); 344 try { 345 raf.readFully(buf, 0, buf.length); 346 fail("Reading past end of buffer did not throw EOFException"); 347 } catch (EOFException e) { 348 } 349 raf.close(); 350 } 351 352 /** 353 * java.io.RandomAccessFile#readInt() 354 */ 355 public void test_readInt() throws IOException { 356 // Test for method int java.io.RandomAccessFile.readInt() 357 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 358 raf.writeInt(Integer.MIN_VALUE); 359 raf.seek(0); 360 assertEquals("Incorrect int read/written", Integer.MIN_VALUE, raf 361 .readInt()); 362 raf.close(); 363 } 364 365 /** 366 * java.io.RandomAccessFile#readLine() 367 */ 368 public void test_readLine() throws IOException { 369 // Test for method java.lang.String java.io.RandomAccessFile.readLine() 370 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 371 String s = "Goodbye\nCruel\nWorld\n"; 372 raf.write(s.getBytes("UTF-8"), 0, s.length()); 373 raf.seek(0); 374 375 assertEquals("Goodbye", raf.readLine()); 376 assertEquals("Cruel", raf.readLine()); 377 assertEquals("World", raf.readLine()); 378 379 raf.close(); 380 } 381 382 /** 383 * java.io.RandomAccessFile#readLong() 384 */ 385 public void test_readLong() throws IOException { 386 // Test for method long java.io.RandomAccessFile.readLong() 387 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 388 raf.writeLong(Long.MAX_VALUE); 389 raf.seek(0); 390 assertEquals("Incorrect long read/written", Long.MAX_VALUE, raf 391 .readLong()); 392 raf.close(); 393 } 394 395 /** 396 * java.io.RandomAccessFile#readShort() 397 */ 398 public void test_readShort() throws IOException { 399 // Test for method short java.io.RandomAccessFile.readShort() 400 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 401 raf.writeShort(Short.MIN_VALUE); 402 raf.seek(0); 403 assertEquals("Incorrect long read/written", Short.MIN_VALUE, raf 404 .readShort()); 405 raf.close(); 406 } 407 408 /** 409 * java.io.RandomAccessFile#readUnsignedByte() 410 */ 411 public void test_readUnsignedByte() throws IOException { 412 // Test for method int java.io.RandomAccessFile.readUnsignedByte() 413 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 414 raf.writeByte(-1); 415 raf.seek(0); 416 assertEquals("Incorrect byte read/written", 255, raf.readUnsignedByte()); 417 raf.close(); 418 } 419 420 /** 421 * java.io.RandomAccessFile#readUnsignedShort() 422 */ 423 public void test_readUnsignedShort() throws IOException { 424 // Test for method int java.io.RandomAccessFile.readUnsignedShort() 425 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 426 raf.writeShort(-1); 427 raf.seek(0); 428 assertEquals("Incorrect byte read/written", 65535, raf 429 .readUnsignedShort()); 430 raf.close(); 431 } 432 433 /** 434 * java.io.RandomAccessFile#readUTF() 435 */ 436 public void test_readUTF() throws IOException { 437 // Test for method java.lang.String java.io.RandomAccessFile.readUTF() 438 439 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 440 raf.writeUTF(unihw); 441 raf.seek(0); 442 assertEquals("Incorrect utf string read", unihw, raf.readUTF()); 443 raf.close(); 444 } 445 446 /** 447 * java.io.RandomAccessFile#seek(long) 448 */ 449 public void test_seekJ() throws IOException { 450 // Test for method void java.io.RandomAccessFile.seek(long) 451 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 452 raf.write(fileString.getBytes(), 0, fileString.length()); 453 raf.seek(12); 454 assertEquals("Seek failed to set filePointer", 12, raf.getFilePointer()); 455 raf.close(); 456 } 457 458 /** 459 * java.io.RandomAccessFile#skipBytes(int) 460 */ 461 public void test_skipBytesI() throws IOException { 462 // Test for method int java.io.RandomAccessFile.skipBytes(int) 463 byte[] buf = new byte[5]; 464 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 465 raf.writeBytes("HelloWorld"); 466 raf.seek(0); 467 raf.skipBytes(5); 468 raf.readFully(buf); 469 assertEquals("Failed to skip bytes", "World", new String(buf, 0, 5, "UTF-8")); 470 raf.close(); 471 } 472 473 /** 474 * java.io.RandomAccessFile#write(byte[]) 475 */ 476 public void test_write$B() throws IOException { 477 // Test for method void java.io.RandomAccessFile.write(byte []) 478 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 479 480 byte[] nullByteArray = null; 481 try { 482 raf.write(nullByteArray); 483 fail("should throw NullPointerException"); 484 } catch (NullPointerException e) { 485 //expected 486 } 487 488 byte[] rbuf = new byte[4000]; 489 raf.write(fileString.getBytes()); 490 raf.close(); 491 492 try { 493 raf.write(nullByteArray); 494 fail("should throw NullPointerException"); 495 } catch (NullPointerException e) { 496 //expected 497 } 498 499 //will not throw IOException if array's length is 0 500 raf.write(new byte[0]); 501 502 try { 503 raf.write(fileString.getBytes()); 504 fail("should throw IOException"); 505 } catch (IOException e) { 506 //expected 507 } 508 509 FileInputStream fis = new java.io.FileInputStream(fileName); 510 fis.read(rbuf, 0, fileString.length()); 511 assertEquals("Incorrect bytes written", fileString, new String(rbuf, 0, 512 fileString.length())); 513 fis.close(); 514 } 515 516 /** 517 * java.io.RandomAccessFile#write(byte[], int, int) 518 */ 519 public void test_write$BII() throws IOException { 520 // Test for method void java.io.RandomAccessFile.write(byte [], int, 521 // int) 522 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 523 byte[] rbuf = new byte[4000]; 524 raf.write(fileString.getBytes(), 0, fileString.length()); 525 raf.close(); 526 FileInputStream fis = new java.io.FileInputStream(fileName); 527 fis.read(rbuf, 0, fileString.length()); 528 assertEquals("Incorrect bytes written", fileString, new String(rbuf, 0, 529 fileString.length())); 530 fis.close(); 531 } 532 533 /** 534 * java.io.RandomAccessFile#write(byte[], int, int) 535 */ 536 public void test_write_$BII_Exception() throws IOException { 537 raf = new java.io.RandomAccessFile(f, "rw"); 538 byte[] nullByteArray = null; 539 byte[] byteArray = new byte[10]; 540 541 try { 542 raf.write(nullByteArray, -1, -1); 543 fail("should throw NullPointerException"); 544 } catch (NullPointerException e) { 545 // expected 546 } 547 548 try { 549 raf.write(nullByteArray, 0, 0); 550 fail("should throw NullPointerException"); 551 } catch (NullPointerException e) { 552 // expected 553 } 554 555 try { 556 raf.write(nullByteArray, 1, -1); 557 fail("should throw NullPointerException"); 558 } catch (NullPointerException e) { 559 // expected 560 } 561 562 try { 563 raf.write(nullByteArray, 1, 0); 564 fail("should throw NullPointerException"); 565 } catch (NullPointerException e) { 566 // expected 567 } 568 569 try { 570 raf.write(nullByteArray, 1, 1); 571 fail("should throw NullPointerException"); 572 } catch (NullPointerException e) { 573 // expected 574 } 575 576 try { 577 raf.write(byteArray, -1, -1); 578 fail("should throw IndexOutOfBoundsException"); 579 } catch (IndexOutOfBoundsException e) { 580 // expected 581 } 582 583 try { 584 raf.write(byteArray, -1, 0); 585 fail("should throw IndexOutOfBoundsException"); 586 } catch (IndexOutOfBoundsException e) { 587 // expected 588 } 589 590 try { 591 raf.write(byteArray, -1, 1); 592 fail("should throw IndexOutOfBoundsException"); 593 } catch (IndexOutOfBoundsException e) { 594 // expected 595 } 596 597 try { 598 raf.write(byteArray, 0, -1); 599 fail("should throw IndexOutOfBoundsException"); 600 } catch (IndexOutOfBoundsException e) { 601 // expected 602 } 603 604 raf.write(byteArray, 0, 0); 605 raf.write(byteArray, 0, byteArray.length); 606 raf.write(byteArray, 1, 0); 607 raf.write(byteArray, byteArray.length, 0); 608 609 try { 610 raf.write(byteArray, byteArray.length + 1, 0); 611 fail("should throw IndexOutOfBoundsException"); 612 } catch (IndexOutOfBoundsException e) { 613 //expected 614 } 615 616 try { 617 raf.write(byteArray, byteArray.length + 1, 1); 618 fail("should throw IndexOutOfBoundsException"); 619 } catch (IndexOutOfBoundsException e) { 620 //expected 621 } 622 623 raf.close(); 624 625 try { 626 raf.write(nullByteArray, -1, -1); 627 fail("should throw NullPointerException"); 628 } catch (NullPointerException e) { 629 // expected 630 } 631 632 try { 633 raf.write(byteArray, -1, -1); 634 fail("should throw IndexOutOfBoundsException"); 635 } catch (IndexOutOfBoundsException e) { 636 // expected 637 } 638 639 try { 640 raf.write(byteArray, 0, 1); 641 fail("should throw IOException"); 642 } catch (IOException e) { 643 //expected 644 } 645 646 try { 647 raf.write(byteArray, 0, byteArray.length); 648 fail("should throw IOException"); 649 } catch (IOException e) { 650 //expected 651 } 652 653 try { 654 raf.write(byteArray, 1, 1); 655 fail("should throw IOException"); 656 } catch (IOException e) { 657 //expected 658 } 659 660 try { 661 raf.write(byteArray, byteArray.length + 1, 0); 662 fail("should throw IndexOutOfBoundsException"); 663 } catch (IndexOutOfBoundsException e) { 664 //expected 665 } 666 667 // will not throw IOException if count = 0 668 raf.write(byteArray, 0, 0); 669 raf.write(byteArray, byteArray.length, 0); 670 } 671 672 673 /** 674 * java.io.RandomAccessFile#write(int) 675 */ 676 public void test_writeI() throws IOException { 677 // Test for method void java.io.RandomAccessFile.write(int) 678 byte[] rbuf = new byte[4000]; 679 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 680 raf.write('t'); 681 raf.close(); 682 FileInputStream fis = new java.io.FileInputStream(fileName); 683 fis.read(rbuf, 0, 1); 684 assertEquals("Incorrect byte written", 't', rbuf[0]); 685 fis.close(); 686 } 687 688 /** 689 * java.io.RandomAccessFile#writeBoolean(boolean) 690 */ 691 public void test_writeBooleanZ() throws IOException { 692 // Test for method void java.io.RandomAccessFile.writeBoolean(boolean) 693 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 694 raf.writeBoolean(true); 695 raf.seek(0); 696 assertTrue("Incorrect boolean read/written", raf.readBoolean()); 697 raf.close(); 698 } 699 700 /** 701 * java.io.RandomAccessFile#writeByte(int) 702 */ 703 public void test_writeByteI() throws IOException { 704 // Test for method void java.io.RandomAccessFile.writeByte(int) 705 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 706 raf.writeByte(127); 707 raf.seek(0); 708 assertEquals("Incorrect byte read/written", 127, raf.readByte()); 709 raf.close(); 710 } 711 712 /** 713 * java.io.RandomAccessFile#writeBytes(java.lang.String) 714 */ 715 public void test_writeBytesLjava_lang_String() throws IOException { 716 // Test for method void 717 // java.io.RandomAccessFile.writeBytes(java.lang.String) 718 byte[] buf = new byte[10]; 719 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 720 raf.writeBytes("HelloWorld"); 721 raf.seek(0); 722 raf.readFully(buf); 723 assertEquals("Incorrect bytes read/written", "HelloWorld", new String( 724 buf, 0, 10, "UTF-8")); 725 raf.close(); 726 727 } 728 729 /** 730 * java.io.RandomAccessFile#writeChar(int) 731 */ 732 public void test_writeCharI() throws IOException { 733 // Test for method void java.io.RandomAccessFile.writeChar(int) 734 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 735 raf.writeChar('T'); 736 raf.seek(0); 737 assertEquals("Incorrect char read/written", 'T', raf.readChar()); 738 raf.close(); 739 } 740 741 /** 742 * java.io.RandomAccessFile#writeChars(java.lang.String) 743 */ 744 public void test_writeCharsLjava_lang_String() throws IOException { 745 // Test for method void 746 // java.io.RandomAccessFile.writeChars(java.lang.String) 747 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 748 raf.writeChars("HelloWorld"); 749 char[] hchars = new char[10]; 750 "HelloWorld".getChars(0, 10, hchars, 0); 751 raf.seek(0); 752 for (int i = 0; i < hchars.length; i++) 753 assertEquals("Incorrect string written", hchars[i], raf.readChar()); 754 raf.close(); 755 } 756 757 /** 758 * java.io.RandomAccessFile#writeDouble(double) 759 */ 760 public void test_writeDoubleD() throws IOException { 761 // Test for method void java.io.RandomAccessFile.writeDouble(double) 762 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 763 raf.writeDouble(Double.MAX_VALUE); 764 raf.seek(0); 765 assertEquals("Incorrect double read/written", Double.MAX_VALUE, raf 766 .readDouble(), 0); 767 raf.close(); 768 } 769 770 /** 771 * java.io.RandomAccessFile#writeFloat(float) 772 */ 773 public void test_writeFloatF() throws IOException { 774 // Test for method void java.io.RandomAccessFile.writeFloat(float) 775 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 776 raf.writeFloat(Float.MAX_VALUE); 777 raf.seek(0); 778 assertEquals("Incorrect float read/written", Float.MAX_VALUE, raf 779 .readFloat(), 0); 780 raf.close(); 781 } 782 783 /** 784 * java.io.RandomAccessFile#writeInt(int) 785 */ 786 public void test_writeIntI() throws IOException { 787 // Test for method void java.io.RandomAccessFile.writeInt(int) 788 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 789 raf.writeInt(Integer.MIN_VALUE); 790 raf.seek(0); 791 assertEquals("Incorrect int read/written", Integer.MIN_VALUE, raf 792 .readInt()); 793 raf.close(); 794 } 795 796 /** 797 * java.io.RandomAccessFile#writeLong(long) 798 */ 799 public void test_writeLongJ() throws IOException { 800 // Test for method void java.io.RandomAccessFile.writeLong(long) 801 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 802 raf.writeLong(Long.MAX_VALUE); 803 raf.seek(0); 804 assertEquals("Incorrect long read/written", Long.MAX_VALUE, raf 805 .readLong()); 806 raf.close(); 807 } 808 809 /** 810 * java.io.RandomAccessFile#writeShort(int) 811 */ 812 public void test_writeShortI() throws IOException { 813 // Test for method void java.io.RandomAccessFile.writeShort(int) 814 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 815 raf.writeShort(Short.MIN_VALUE); 816 raf.seek(0); 817 assertEquals("Incorrect long read/written", Short.MIN_VALUE, raf 818 .readShort()); 819 raf.close(); 820 } 821 822 /** 823 * java.io.RandomAccessFile#writeUTF(java.lang.String) 824 */ 825 public void test_writeUTFLjava_lang_String() throws IOException { 826 // Test for method void 827 // java.io.RandomAccessFile.writeUTF(java.lang.String) 828 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 829 raf.writeUTF(unihw); 830 raf.seek(0); 831 assertEquals("Incorrect utf string", unihw, raf.readUTF()); 832 raf.close(); 833 } 834 835 /** 836 * java.io.RandomAccessFile#seek(long) 837 * <p/> 838 * Regression for HARMONY-374 839 */ 840 public void test_seekI() throws IOException { 841 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 842 try { 843 raf.seek(-1); 844 fail("IOException must be thrown if pos < 0"); 845 } catch (IOException e) { 846 } 847 raf.close(); 848 } 849 850 /** 851 * java.io.RandomAccessFile#read(byte[], int, int) 852 * <p/> 853 * Regression for HARMONY-377 854 */ 855 public void test_readBII() throws IOException { 856 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 857 try { 858 raf.read(new byte[1], -1, 1); 859 fail("IndexOutOfBoundsException must be thrown if off <0"); 860 } catch (IndexOutOfBoundsException e) { 861 } 862 863 try { 864 raf.read(new byte[1], 0, -1); 865 fail("IndexOutOfBoundsException must be thrown if len <0"); 866 } catch (IndexOutOfBoundsException e) { 867 } 868 869 try { 870 raf.read(new byte[1], 0, 5); 871 fail("IndexOutOfBoundsException must be thrown if off+len > b.length"); 872 } catch (IndexOutOfBoundsException e) { 873 } 874 875 try { 876 raf.read(new byte[10], Integer.MAX_VALUE, 5); 877 fail("IndexOutOfBoundsException expected"); 878 } catch (IndexOutOfBoundsException e) { 879 } 880 881 try { 882 raf.read(new byte[10], 5, Integer.MAX_VALUE); 883 fail("IndexOutOfBoundsException expected"); 884 } catch (IndexOutOfBoundsException e) { 885 } 886 887 raf.close(); 888 } 889 890 /** 891 * java.io.RandomAccessFile#read(byte[], int, int) 892 */ 893 public void test_read_$BII_IndexOutOfBoundsException() throws IOException { 894 FileOutputStream fos = new java.io.FileOutputStream(fileName); 895 fos.write(fileString.getBytes(), 0, fileString.length()); 896 fos.close(); 897 898 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r"); 899 byte[] rbuf = new byte[100]; 900 raf.close(); 901 try { 902 raf.read(rbuf, -1, 0); 903 fail("should throw IndexOutOfBoundsException"); 904 } catch (IndexOutOfBoundsException e) { 905 //expected 906 } 907 } 908 909 /** 910 * java.io.RandomAccessFile#read(byte[], int, int) 911 */ 912 public void test_read_$BII_IOException() throws IOException { 913 FileOutputStream fos = new java.io.FileOutputStream(fileName); 914 fos.write(fileString.getBytes(), 0, fileString.length()); 915 fos.close(); 916 917 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r"); 918 byte[] rbuf = new byte[100]; 919 raf.close(); 920 int read = raf.read(rbuf, 0, 0); 921 assertEquals(0, read); 922 } 923 924 /** 925 * java.io.RandomAccessFile#read(byte[]) 926 */ 927 public void test_read_$B_IOException() throws IOException { 928 FileOutputStream fos = new java.io.FileOutputStream(fileName); 929 fos.write(fileString.getBytes(), 0, fileString.length()); 930 fos.close(); 931 932 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r"); 933 byte[] rbuf = new byte[0]; 934 raf.close(); 935 int read = raf.read(rbuf); 936 assertEquals(0, read); 937 } 938 939 /** 940 * java.io.RandomAccessFile#read(byte[], int, int) 941 */ 942 public void test_read_$BII_NullPointerException() throws IOException { 943 File f = File.createTempFile("tmp", "tmp"); 944 f.deleteOnExit(); 945 RandomAccessFile raf = new RandomAccessFile(f, "r"); 946 byte[] rbuf = null; 947 try { 948 raf.read(rbuf, 0, -1); 949 fail("should throw NullPointerException"); 950 } catch (NullPointerException e) { 951 // expected 952 } 953 raf.close(); 954 } 955 956 /** 957 * java.io.RandomAccessFile#write(byte[], int, int) 958 * <p/> 959 * Regression for HARMONY-377 960 */ 961 public void test_writeBII() throws IOException { 962 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw"); 963 try { 964 raf.write(new byte[1], -1, 1); 965 fail("IndexOutOfBoundsException must be thrown if off <0"); 966 } catch (IndexOutOfBoundsException e) { 967 } 968 969 try { 970 raf.write(new byte[1], 0, -1); 971 fail("IndexOutOfBoundsException must be thrown if len <0"); 972 } catch (IndexOutOfBoundsException e) { 973 } 974 975 try { 976 raf.write(new byte[1], 0, 5); 977 fail("IndexOutOfBoundsException must be thrown if off+len > b.length"); 978 } catch (IndexOutOfBoundsException e) { 979 } 980 981 try { 982 raf.write(new byte[10], Integer.MAX_VALUE, 5); 983 fail("IndexOutOfBoundsException expected"); 984 } catch (IndexOutOfBoundsException e) { 985 } 986 987 try { 988 raf.write(new byte[10], 5, Integer.MAX_VALUE); 989 fail("IndexOutOfBoundsException expected"); 990 } catch (IndexOutOfBoundsException e) { 991 } 992 raf.close(); 993 } 994 995 /** 996 * Regression for HARMONY-69 997 */ 998 public void testRandomAccessFile_String_String() throws IOException { 999 f.createNewFile(); 1000 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r"); 1001 FileChannel fcr = raf.getChannel(); 1002 1003 try { 1004 fcr.lock(0L, Long.MAX_VALUE, false); 1005 fail("NonWritableChannelException expected!"); 1006 } catch (NonWritableChannelException e) { 1007 } 1008 raf.close(); 1009 } 1010 1011 // Regression test for HARMONY-6542 1012 public void testRandomAccessFile_seekMoreThan2gb() throws IOException { 1013 if (File.separator != "/") { 1014 // skip windows until a test can be implemented that doesn't 1015 // require 2GB of free disk space 1016 return; 1017 } 1018 // (all?) unix platforms support sparse files so this should not 1019 // need to have 2GB free disk space to pass 1020 RandomAccessFile raf = new RandomAccessFile(f, "rw"); 1021 // write a few bytes so we get more helpful error messages 1022 // if we land in the wrong places 1023 raf.write(1); 1024 raf.write(2); 1025 raf.seek(2147483647); 1026 raf.write(3); 1027 raf.write(4); 1028 raf.write(5); 1029 raf.write(6); 1030 raf.seek(0); 1031 assertEquals("seek 0", 1, raf.read()); 1032 raf.seek(2147483649L); 1033 assertEquals("seek >2gb", 5, raf.read()); 1034 raf.seek(0); 1035 assertEquals("seek back to 0", 1, raf.read()); 1036 raf.close(); 1037 } 1038 1039 /** 1040 * Sets up the fixture, for example, open a network connection. This method 1041 * is called before a test is executed. 1042 */ 1043 protected void setUp() throws Exception { 1044 super.setUp(); 1045 f = File.createTempFile("raf", "tst"); 1046 if (!f.delete()) { 1047 fail("Unable to delete test file : " + f); 1048 } 1049 fileName = f.getAbsolutePath(); 1050 } 1051 1052 /** 1053 * Tears down the fixture, for example, close a network connection. This 1054 * method is called after a test is executed. 1055 * 1056 * @throws Exception 1057 */ 1058 protected void tearDown() throws Exception { 1059 if (f.exists()) { 1060 f.delete(); 1061 } 1062 super.tearDown(); 1063 } 1064 1065} 1066