FileTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
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.File; 21import java.io.FileFilter; 22import java.io.FileInputStream; 23import java.io.FileOutputStream; 24import java.io.FilenameFilter; 25import java.io.IOException; 26import java.io.ObjectStreamClass; 27import java.io.ObjectStreamField; 28import java.io.RandomAccessFile; 29import java.net.MalformedURLException; 30import java.net.URI; 31import java.net.URISyntaxException; 32import java.net.URL; 33 34import tests.support.Support_Exec; 35import tests.support.Support_PlatformFile; 36 37public class FileTest extends junit.framework.TestCase { 38 39 /** Location to store tests in */ 40 private File tempDirectory; 41 42 /** Temp file that does exist */ 43 private File tempFile; 44 45 /** File separator */ 46 private String slash = File.separator; 47 48 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_File\nTest_FileDescriptor\nTest_FileInputStream\nTest_FileNotFoundException\nTest_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_java_io_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"; 49 50 private static String platformId = "JDK" 51 + System.getProperty("java.vm.version").replace('.', '-'); 52 53 { 54 // Delete all old temporary files 55 File tempDir = new File(System.getProperty("java.io.tmpdir")); 56 String[] files = tempDir.list(); 57 for (int i = 0; i < files.length; i++) { 58 File f = new File(tempDir, files[i]); 59 if (f.isDirectory()) { 60 if (files[i].startsWith("hyts_resources")) 61 deleteTempFolder(f); 62 } 63 if (files[i].startsWith("hyts_") || files[i].startsWith("hyjar_")) 64 new File(tempDir, files[i]).delete(); 65 } 66 } 67 68 private void deleteTempFolder(File dir) { 69 String files[] = dir.list(); 70 for (int i = 0; i < files.length; i++) { 71 File f = new File(dir, files[i]); 72 if (f.isDirectory()) 73 deleteTempFolder(f); 74 else { 75 f.delete(); 76 } 77 } 78 dir.delete(); 79 80 } 81 82 /** 83 * @tests java.io.File#File(java.io.File, java.lang.String) 84 */ 85 public void test_ConstructorLjava_io_FileLjava_lang_String() { 86 // Test for method java.io.File(java.io.File, java.lang.String) 87 String dirName = System.getProperty("user.dir"); 88 File d = new File(dirName); 89 File f = new File(d, "input.tst"); 90 if (!dirName.regionMatches((dirName.length() - 1), slash, 0, 1)) 91 dirName += slash; 92 dirName += "input.tst"; 93 assertTrue("Test 1: Created Incorrect File " + f.getPath(), f.getPath() 94 .equals(dirName)); 95 96 String fileName = null; 97 try { 98 f = new File(d, fileName); 99 fail("NullPointerException Not Thrown."); 100 } catch (NullPointerException e) { 101 } 102 103 d = null; 104 f = new File(d, "input.tst"); 105 assertTrue("Test 2: Created Incorrect File " + f.getPath(), f 106 .getAbsolutePath().equals(dirName)); 107 108 // Regression test for Harmony-382 109 File s = null; 110 f = new File("/abc"); 111 d = new File(s, "/abc"); 112 assertEquals("Test3: Created Incorrect File " + d.getAbsolutePath(), f 113 .getAbsolutePath(), d.getAbsolutePath()); 114 } 115 116 /** 117 * @tests java.io.File#File(java.lang.String) 118 */ 119 public void test_ConstructorLjava_lang_String() { 120 // Test for method java.io.File(java.lang.String) 121 String fileName = null; 122 try { 123 new File(fileName); 124 fail("NullPointerException Not Thrown."); 125 } catch (NullPointerException e) { 126 } 127 128 fileName = System.getProperty("user.dir"); 129 if (!fileName.regionMatches((fileName.length() - 1), slash, 0, 1)) 130 fileName += slash; 131 fileName += "input.tst"; 132 133 File f = new File(fileName); 134 assertTrue("Created incorrect File " + f.getPath(), f.getPath().equals( 135 fileName)); 136 } 137 138 /** 139 * @tests java.io.File#File(java.lang.String, java.lang.String) 140 */ 141 public void test_ConstructorLjava_lang_StringLjava_lang_String() { 142 // Test for method java.io.File(java.lang.String, java.lang.String) 143 String dirName = null; 144 String fileName = "input.tst"; 145 File f = new File(dirName, fileName); 146 String userDir = System.getProperty("user.dir"); 147 if (!userDir.regionMatches((userDir.length() - 1), slash, 0, 1)) 148 userDir += slash; 149 userDir += "input.tst"; 150 assertTrue("Test 1: Created Incorrect File.", f.getAbsolutePath() 151 .equals(userDir)); 152 153 dirName = System.getProperty("user.dir"); 154 fileName = null; 155 try { 156 f = new File(dirName, fileName); 157 fail("NullPointerException Not Thrown."); 158 } catch (NullPointerException e) { 159 } 160 161 fileName = "input.tst"; 162 f = new File(dirName, fileName); 163 assertTrue("Test 2: Created Incorrect File", f.getPath() 164 .equals(userDir)); 165 166 // Regression test for Harmony-382 167 String s = null; 168 f = new File("/abc"); 169 File d = new File(s, "/abc"); 170 assertEquals("Test3: Created Incorrect File", d.getAbsolutePath(), f 171 .getAbsolutePath()); 172 } 173 174 /** 175 * @tests java.io.File#File(java.lang.String, java.lang.String) 176 */ 177 public void test_Constructor_String_String_112270() { 178 File ref1 = new File("/dir1/file1"); 179 180 File file1 = new File("/", "/dir1/file1"); 181 assertEquals("wrong result 1: " + file1, ref1.getPath(), file1 182 .getPath()); 183 File file2 = new File("/", "//dir1/file1"); 184 assertTrue("wrong result 2: " + file2, file2.getPath().equals( 185 ref1.getPath())); 186 File file3 = new File("\\", "\\dir1\\file1"); 187 assertTrue("wrong result 3: " + file3, file3.getPath().equals( 188 ref1.getPath())); 189 File file4 = new File("\\", "\\\\dir1\\file1"); 190 assertTrue("wrong result 4: " + file4, file4.getPath().equals( 191 ref1.getPath())); 192 193 File ref2 = new File("/lib/content-types.properties"); 194 File file5 = new File("/", "lib/content-types.properties"); 195 assertTrue("wrong result 5: " + file5, file5.getPath().equals( 196 ref2.getPath())); 197 198 } 199 200 /** 201 * @tests java.io.File#File(java.io.File, java.lang.String) 202 */ 203 public void test_Constructor_File_String_112270() { 204 File ref1 = new File("/dir1/file1"); 205 206 File root = new File("/"); 207 File file1 = new File(root, "/dir1/file1"); 208 assertTrue("wrong result 1: " + file1, file1.getPath().equals( 209 ref1.getPath())); 210 File file2 = new File(root, "//dir1/file1"); 211 assertTrue("wrong result 2: " + file2, file2.getPath().equals( 212 ref1.getPath())); 213 File file3 = new File(root, "\\dir1\\file1"); 214 assertTrue("wrong result 3: " + file3, file3.getPath().equals( 215 ref1.getPath())); 216 File file4 = new File(root, "\\\\dir1\\file1"); 217 assertTrue("wrong result 4: " + file4, file4.getPath().equals( 218 ref1.getPath())); 219 220 File ref2 = new File("/lib/content-types.properties"); 221 File file5 = new File(root, "lib/content-types.properties"); 222 assertTrue("wrong result 5: " + file5, file5.getPath().equals( 223 ref2.getPath())); 224 } 225 226 /** 227 * @tests java.io.File#File(java.net.URI) 228 */ 229 public void test_ConstructorLjava_net_URI() { 230 // Test for method java.io.File(java.net.URI) 231 URI uri = null; 232 try { 233 new File(uri); 234 fail("NullPointerException Not Thrown."); 235 } catch (NullPointerException e) { 236 } 237 238 // invalid file URIs 239 String[] uris = new String[] { "mailto:user@domain.com", // not 240 // hierarchical 241 "ftp:///path", // not file scheme 242 "//host/path/", // not absolute 243 "file://host/path", // non empty authority 244 "file:///path?query", // non empty query 245 "file:///path#fragment", // non empty fragment 246 "file:///path?", "file:///path#" }; 247 248 for (int i = 0; i < uris.length; i++) { 249 try { 250 uri = new URI(uris[i]); 251 } catch (URISyntaxException e) { 252 fail("Unexpected exception:" + e); 253 } 254 try { 255 new File(uri); 256 fail("Expected IllegalArgumentException for new File(" + uri 257 + ")"); 258 } catch (IllegalArgumentException e) { 259 } 260 } 261 262 // a valid File URI 263 try { 264 File f = new File(new URI("file:///pa%20th/another\u20ac/pa%25th")); 265 assertTrue("Created incorrect File " + f.getPath(), f.getPath() 266 .equals( 267 slash + "pa th" + slash + "another\u20ac" + slash 268 + "pa%th")); 269 } catch (URISyntaxException e) { 270 fail("Unexpected exception:" + e); 271 } catch (IllegalArgumentException e) { 272 fail("Unexpected exception:" + e); 273 } 274 } 275 276 /** 277 * @tests java.io.File#canRead() 278 */ 279 public void test_canRead() { 280 // Test for method boolean java.io.File.canRead() 281 // canRead only returns if the file exists so cannot be fully tested. 282 File f = new File(System.getProperty("java.io.tmpdir"), platformId 283 + "canRead.tst"); 284 try { 285 FileOutputStream fos = new FileOutputStream(f); 286 fos.close(); 287 assertTrue("canRead returned false", f.canRead()); 288 f.delete(); 289 } catch (IOException e) { 290 fail("Unexpected IOException During Test: " + e); 291 } finally { 292 f.delete(); 293 } 294 } 295 296 /** 297 * @tests java.io.File#canWrite() 298 */ 299 public void test_canWrite() { 300 // Test for method boolean java.io.File.canWrite() 301 // canWrite only returns if the file exists so cannot be fully tested. 302 File f = new File(System.getProperty("java.io.tmpdir"), platformId 303 + "canWrite.tst"); 304 try { 305 FileOutputStream fos = new FileOutputStream(f); 306 fos.close(); 307 assertTrue("canWrite returned false", f.canWrite()); 308 } catch (IOException e) { 309 fail("Unexpected IOException During Test: " + e); 310 } finally { 311 f.delete(); 312 } 313 } 314 315 /** 316 * @tests java.io.File#compareTo(java.io.File) 317 */ 318 public void test_compareToLjava_io_File() { 319 File f1 = new File("thisFile.file"); 320 File f2 = new File("thisFile.file"); 321 File f3 = new File("thatFile.file"); 322 assertEquals("Equal files did not answer zero for compareTo", 0, f1 323 .compareTo(f2)); 324 assertTrue("f3.compareTo(f1) did not result in value < 0", f3 325 .compareTo(f1) < 0); 326 assertTrue("f1.compareTo(f3) did not result in vale > 0", f1 327 .compareTo(f3) > 0); 328 } 329 330 /** 331 * @tests java.io.File#createNewFile() 332 */ 333 public void test_createNewFile_EmptyString() { 334 File f = new File(""); 335 try { 336 f.createNewFile(); 337 fail("should throw IOException"); 338 } catch (IOException e) { 339 // expected 340 } 341 } 342 343 /** 344 * @tests java.io.File#createNewFile() 345 */ 346 public void test_createNewFile() throws IOException { 347 // Test for method java.io.File.createNewFile() 348 String base = System.getProperty("java.io.tmpdir"); 349 boolean dirExists = true; 350 int numDir = 1; 351 File dir = new File(base, String.valueOf(numDir)); 352 // Making sure that the directory does not exist. 353 while (dirExists) { 354 // If the directory exists, add one to the directory number 355 // (making 356 // it a new directory name.) 357 if (dir.exists()) { 358 numDir++; 359 dir = new File(base, String.valueOf(numDir)); 360 } else { 361 dirExists = false; 362 } 363 } 364 365 // Test for trying to create a file in a directory that does not 366 // exist. 367 try { 368 // Try to create a file in a directory that does not exist 369 File f1 = new File(dir, "tempfile.tst"); 370 f1.createNewFile(); 371 fail("IOException not thrown"); 372 } catch (IOException e) { 373 } 374 375 dir.mkdir(); 376 377 File f1 = new File(dir, "tempfile.tst"); 378 File f2 = new File(dir, "tempfile.tst"); 379 f1.deleteOnExit(); 380 f2.deleteOnExit(); 381 dir.deleteOnExit(); 382 assertFalse("File Should Not Exist", f1.isFile()); 383 f1.createNewFile(); 384 assertTrue("File Should Exist.", f1.isFile()); 385 assertTrue("File Should Exist.", f2.isFile()); 386 String dirName = f1.getParent(); 387 if (!dirName.endsWith(slash)) 388 dirName += slash; 389 assertTrue("File Saved To Wrong Directory.", dirName.equals(dir 390 .getPath() 391 + slash)); 392 assertEquals("File Saved With Incorrect Name.", "tempfile.tst", f1 393 .getName()); 394 395 // Test for creating a file that already exists. 396 assertFalse("File Already Exists, createNewFile Should Return False.", 397 f2.createNewFile()); 398 399 // Test create an illegal file 400 String sep = File.separator; 401 f1 = new File(sep+".."); 402 try { 403 f1.createNewFile(); 404 fail("should throw IOE"); 405 } catch (IOException e) { 406 // expected; 407 } 408 f1 = new File(sep+"a"+sep+".."+sep+".."+sep); 409 try { 410 f1.createNewFile(); 411 fail("should throw IOE"); 412 } catch (IOException e) { 413 // expected; 414 } 415 416 // Test create an exist path 417 f1 = new File(base); 418 try { 419 assertFalse(f1.createNewFile()); 420 fail("should throw IOE"); 421 } catch (IOException e) { 422 // expected; 423 } 424 } 425 426 /** 427 * @tests java.io.File#createTempFile(java.lang.String, java.lang.String) 428 */ 429 public void test_createTempFileLjava_lang_StringLjava_lang_String() { 430 // Test for method java.io.File.createTempFile(String, String) 431 // Error protection against using a suffix without a "."? 432 File f1 = null; 433 File f2 = null; 434 try { 435 f1 = File.createTempFile("hyts_abc", ".tmp"); 436 f2 = File.createTempFile("hyts_tf", null); 437 String fileLocation = f1.getParent(); 438 if (!fileLocation.endsWith(slash)) 439 ; 440 fileLocation += slash; 441 String tempDir = System.getProperty("java.io.tmpdir"); 442 if (!tempDir.endsWith(slash)) 443 tempDir += slash; 444 assertTrue( 445 "File did not save to the default temporary-file location.", 446 fileLocation.equals(tempDir)); 447 448 // Test to see if correct suffix was used to create the tempfile. 449 File currentFile; 450 String fileName; 451 // Testing two files, one with suffix ".tmp" and one with null 452 for (int i = 0; i < 2; i++) { 453 currentFile = i == 0 ? f1 : f2; 454 fileName = currentFile.getPath(); 455 assertTrue("File Created With Incorrect Suffix.", fileName 456 .endsWith(".tmp")); 457 } 458 459 // Tests to see if the correct prefix was used to create the 460 // tempfiles. 461 fileName = f1.getName(); 462 assertTrue("Test 1: File Created With Incorrect Prefix.", fileName 463 .startsWith("hyts_abc")); 464 fileName = f2.getName(); 465 assertTrue("Test 2: File Created With Incorrect Prefix.", fileName 466 .startsWith("hyts_tf")); 467 468 // Tests for creating a tempfile with a filename shorter than 3 469 // characters. 470 try { 471 File f3 = File.createTempFile("ab", ".tst"); 472 f3.delete(); 473 fail("IllegalArgumentException Not Thrown."); 474 } catch (IllegalArgumentException e) { 475 } 476 try { 477 File f3 = File.createTempFile("a", ".tst"); 478 f3.delete(); 479 fail("IllegalArgumentException Not Thrown."); 480 } catch (IllegalArgumentException e) { 481 } 482 try { 483 File f3 = File.createTempFile("", ".tst"); 484 f3.delete(); 485 fail("IllegalArgumentException Not Thrown."); 486 } catch (IllegalArgumentException e) { 487 } 488 489 } catch (IOException e) { 490 fail("Unexpected IOException During Test: " + e); 491 } finally { 492 if (f1 != null) 493 f1.delete(); 494 if (f2 != null) 495 f2.delete(); 496 } 497 } 498 499 /** 500 * @tests java.io.File#createTempFile(java.lang.String, java.lang.String, 501 * java.io.File) 502 */ 503 public void test_createTempFileLjava_lang_StringLjava_lang_StringLjava_io_File() { 504 // Test for method java.io.File.createTempFile(String, String, File) 505 File f1 = null; 506 File f2 = null; 507 String base = System.getProperty("java.io.tmpdir"); 508 try { 509 510 // Test to make sure that the tempfile was saved in the correct 511 // location 512 // and with the correct prefix/suffix. 513 f1 = File.createTempFile("hyts_tf", null, null); 514 File dir = new File(base); 515 f2 = File.createTempFile("hyts_tf", ".tmp", dir); 516 File currentFile; 517 String fileLocation; 518 String fileName; 519 for (int i = 0; i < 2; i++) { 520 currentFile = i == 0 ? f1 : f2; 521 fileLocation = currentFile.getParent(); 522 if (!fileLocation.endsWith(slash)) 523 fileLocation += slash; 524 if (!base.endsWith(slash)) 525 base += slash; 526 assertTrue( 527 "File not created in the default temporary-file location.", 528 fileLocation.equals(base)); 529 fileName = currentFile.getName(); 530 assertTrue("File created with incorrect suffix.", fileName 531 .endsWith(".tmp")); 532 assertTrue("File created with incorrect prefix.", fileName 533 .startsWith("hyts_tf")); 534 currentFile.delete(); 535 } 536 537 // Test for creating a tempfile in a directory that does not exist. 538 int dirNumber = 1; 539 boolean dirExists = true; 540 // Set dir to a non-existent directory inside the temporary 541 // directory 542 dir = new File(base, String.valueOf(dirNumber)); 543 // Making sure that the directory does not exist. 544 while (dirExists) { 545 // If the directory exists, add one to the directory number 546 // (making it 547 // a new directory name.) 548 if (dir.exists()) { 549 dirNumber++; 550 dir = new File(base, String.valueOf(dirNumber)); 551 } else { 552 dirExists = false; 553 } 554 } 555 try { 556 // Try to create a file in a directory that does not exist 557 File f3 = File.createTempFile("hyts_tf", null, dir); 558 f3.delete(); 559 fail("IOException not thrown"); 560 } catch (IOException e) { 561 } 562 dir.delete(); 563 564 // Tests for creating a tempfile with a filename shorter than 3 565 // characters. 566 try { 567 File f4 = File.createTempFile("ab", null, null); 568 f4.delete(); 569 fail("IllegalArgumentException not thrown."); 570 } catch (IllegalArgumentException e) { 571 } 572 try { 573 File f4 = File.createTempFile("a", null, null); 574 f4.delete(); 575 fail("IllegalArgumentException not thrown."); 576 } catch (IllegalArgumentException e) { 577 } 578 try { 579 File f4 = File.createTempFile("", null, null); 580 f4.delete(); 581 fail("IllegalArgumentException not thrown."); 582 } catch (IllegalArgumentException e) { 583 } 584 585 } catch (IOException e) { 586 fail("Unexpected IOException During Test: " + e); 587 } finally { 588 if (f1 != null) 589 f1.delete(); 590 if (f2 != null) 591 f1.delete(); 592 } 593 } 594 595 /** 596 * @tests java.io.File#delete() 597 */ 598 public void test_delete() { 599 // Test for method boolean java.io.File.delete() 600 try { 601 File dir = new File(System.getProperty("user.dir"), platformId 602 + "filechk"); 603 dir.mkdir(); 604 assertTrue("Directory Does Not Exist", dir.exists() 605 && dir.isDirectory()); 606 File f = new File(dir, "filechk.tst"); 607 FileOutputStream fos = new FileOutputStream(f); 608 fos.close(); 609 assertTrue("Error Creating File For Delete Test", f.exists()); 610 dir.delete(); 611 assertTrue("Directory Should Not Have Been Deleted.", dir.exists()); 612 f.delete(); 613 assertTrue("File Was Not Deleted", !f.exists()); 614 dir.delete(); 615 assertTrue("Directory Was Not Deleted", !dir.exists()); 616 } catch (IOException e) { 617 fail("Unexpected IOException During Delete Test : " 618 + e.getMessage()); 619 } 620 } 621 622 /** 623 * A partial test for deleteOnExit. Since we need to shutdown the VM to 624 * actually delete the files, we can never observe the results. 625 */ 626 public void test_DeleteOnExit() { 627 File f1 = new File(System.getProperty("java.io.tmpdir"), "DeleteOnExitF1-" + System.currentTimeMillis()); 628 File d1 = new File(System.getProperty("java.io.tmpdir"), "DeleteOnExitD1-" + System.currentTimeMillis()); 629 File f2 = new File(d1, "DeleteOnExitF2-" + System.currentTimeMillis()); 630 631 try { 632 (new FileOutputStream(f1)).close(); 633 d1.mkdirs(); 634 (new FileOutputStream(f2)).close(); 635 } catch (IOException ex) { 636 fail(ex.getMessage()); 637 } 638 639 f1.deleteOnExit(); 640 d1.deleteOnExit(); 641 f2.deleteOnExit(); 642 } 643 644// GCH 645// TODO : This test passes on Windows but fails on Linux with a 646// java.lang.NoClassDefFoundError. Temporarily removing from the test 647// suite while I investigate the cause. 648// /** 649// * @tests java.io.File#deleteOnExit() 650// */ 651// public void test_deleteOnExit() { 652// File f1 = new File(System.getProperty("java.io.tmpdir"), platformId 653// + "deleteOnExit.tst"); 654// try { 655// FileOutputStream fos = new FileOutputStream(f1); 656// fos.close(); 657// } catch (IOException e) { 658// fail("Unexpected IOException During Test : " + e.getMessage()); 659// } 660// assertTrue("File Should Exist.", f1.exists()); 661// 662// try { 663// Support_Exec.execJava(new String[] { 664// "tests.support.Support_DeleteOnExitTest", f1.getPath() }, 665// null, true); 666// } catch (IOException e) { 667// fail("Unexpected IOException During Test + " + e.getMessage()); 668// } catch (InterruptedException e) { 669// fail("Unexpected InterruptedException During Test: " + e); 670// } 671// 672// boolean gone = !f1.exists(); 673// f1.delete(); 674// assertTrue("File Should Already Be Deleted.", gone); 675// } 676 677 /** 678 * @tests java.io.File#equals(java.lang.Object) 679 */ 680 public void test_equalsLjava_lang_Object() { 681 // Test for method boolean java.io.File.equals(java.lang.Object) 682 File f1 = new File("filechk.tst"); 683 File f2 = new File("filechk.tst"); 684 File f3 = new File("xxxx"); 685 686 assertTrue("Equality test failed", f1.equals(f2)); 687 assertTrue("Files Should Not Return Equal.", !f1.equals(f3)); 688 689 f3 = new File("FiLeChK.tst"); 690 boolean onWindows = File.separatorChar == '\\'; 691 boolean onUnix = File.separatorChar == '/'; 692 if (onWindows) 693 assertTrue("Files Should Return Equal.", f1.equals(f3)); 694 else if (onUnix) 695 assertTrue("Files Should NOT Return Equal.", !f1.equals(f3)); 696 697 try { 698 f1 = new File(System.getProperty("java.io.tmpdir"), "casetest.tmp"); 699 f2 = new File(System.getProperty("java.io.tmpdir"), "CaseTest.tmp"); 700 new FileOutputStream(f1).close(); // create the file 701 if (f1.equals(f2)) { 702 try { 703 new FileInputStream(f2); 704 } catch (IOException e) { 705 fail("File system is case sensitive"); 706 } 707 } else { 708 boolean exception = false; 709 try { 710 new FileInputStream(f2); 711 } catch (IOException e) { 712 exception = true; 713 } 714 assertTrue("File system is case insensitive", exception); 715 } 716 f1.delete(); 717 } catch (IOException e) { 718 fail("Unexpected using case sensitive test : " + e.getMessage()); 719 } 720 } 721 722 /** 723 * @tests java.io.File#exists() 724 */ 725 public void test_exists() { 726 // Test for method boolean java.io.File.exists() 727 try { 728 File f = new File(System.getProperty("user.dir"), platformId 729 + "exists.tst"); 730 assertTrue("Exists returned true for non-existent file", !f 731 .exists()); 732 FileOutputStream fos = new FileOutputStream(f); 733 fos.close(); 734 assertTrue("Exists returned false file", f.exists()); 735 f.delete(); 736 } catch (IOException e) { 737 fail("Unexpected IOException During Test : " + e.getMessage()); 738 } 739 } 740 741 /** 742 * @tests java.io.File#getAbsoluteFile() 743 */ 744 public void test_getAbsoluteFile() { 745 // Test for method java.io.File getAbsoluteFile() 746 String base = System.getProperty("user.dir"); 747 if (!base.endsWith(slash)) 748 base += slash; 749 File f = new File(base, "temp.tst"); 750 File f2 = f.getAbsoluteFile(); 751 assertEquals("Test 1: Incorrect File Returned.", 0, f2.compareTo(f 752 .getAbsoluteFile())); 753 f = new File(base + "Temp" + slash + slash + "temp.tst"); 754 f2 = f.getAbsoluteFile(); 755 assertEquals("Test 2: Incorrect File Returned.", 0, f2.compareTo(f 756 .getAbsoluteFile())); 757 f = new File(base + slash + ".." + slash + "temp.tst"); 758 f2 = f.getAbsoluteFile(); 759 assertEquals("Test 3: Incorrect File Returned.", 0, f2.compareTo(f 760 .getAbsoluteFile())); 761 f.delete(); 762 f2.delete(); 763 } 764 765 /** 766 * @tests java.io.File#getAbsolutePath() 767 */ 768 public void test_getAbsolutePath() { 769 // Test for method java.lang.String java.io.File.getAbsolutePath() 770 String base = System.getProperty("user.dir"); 771 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 772 base += slash; 773 File f = new File(base, "temp.tst"); 774 assertTrue("Test 1: Incorrect Path Returned.", f.getAbsolutePath() 775 .equals(base + "temp.tst")); 776 f = new File(base + "Temp" + slash + slash + slash + "Testing" + slash 777 + "temp.tst"); 778 assertTrue("Test 2: Incorrect Path Returned.", f.getAbsolutePath() 779 .equals(base + "Temp" + slash + "Testing" + slash + "temp.tst")); 780 f = new File(base + "a" + slash + slash + ".." + slash + "temp.tst"); 781 assertTrue("Test 3: Incorrect Path Returned." + f.getAbsolutePath(), f 782 .getAbsolutePath().equals( 783 base + "a" + slash + ".." + slash + "temp.tst")); 784 f.delete(); 785 } 786 787 /** 788 * @tests java.io.File#getCanonicalFile() 789 */ 790 public void test_getCanonicalFile() { 791 // Test for method java.io.File.getCanonicalFile() 792 try { 793 String base = System.getProperty("user.dir"); 794 if (!base.endsWith(slash)) 795 base += slash; 796 File f = new File(base, "temp.tst"); 797 File f2 = f.getCanonicalFile(); 798 assertEquals("Test 1: Incorrect File Returned.", 0, f2 799 .getCanonicalFile().compareTo(f.getCanonicalFile())); 800 f = new File(base + "Temp" + slash + slash + "temp.tst"); 801 f2 = f.getCanonicalFile(); 802 assertEquals("Test 2: Incorrect File Returned.", 0, f2 803 .getCanonicalFile().compareTo(f.getCanonicalFile())); 804 f = new File(base + "Temp" + slash + slash + ".." + slash 805 + "temp.tst"); 806 f2 = f.getCanonicalFile(); 807 assertEquals("Test 3: Incorrect File Returned.", 0, f2 808 .getCanonicalFile().compareTo(f.getCanonicalFile())); 809 810 // Test for when long directory/file names in Windows 811 boolean onWindows = File.separatorChar == '\\'; 812 // String userDir = System.getProperty("user.dir"); 813 if (onWindows) { 814 File testdir = new File(base, "long-" + platformId); 815 testdir.mkdir(); 816 File dir = new File(testdir, "longdirectory" + platformId); 817 try { 818 dir.mkdir(); 819 f = new File(dir, "longfilename.tst"); 820 f2 = f.getCanonicalFile(); 821 assertEquals("Test 4: Incorrect File Returned.", 822 0, f2.getCanonicalFile().compareTo( 823 f.getCanonicalFile())); 824 FileOutputStream fos = new FileOutputStream(f); 825 fos.close(); 826 f2 = new File(testdir + slash + "longdi~1" + slash 827 + "longfi~1.tst"); 828 // System.out.println(""); 829 // System.out.println("test_getCanonicalFile"); 830 // System.out.println("f: " + f.getCanonicalFile()); 831 // System.out.println("f3: " + f3.getCanonicalFile()); 832 File canonicalf2 = f2.getCanonicalFile(); 833 /* 834 * If the "short file name" doesn't exist, then assume that 835 * the 8.3 file name compatibility is disabled. 836 */ 837 if (canonicalf2.exists()) { 838 assertTrue("Test 5: Incorrect File Returned: " 839 + canonicalf2, canonicalf2.compareTo(f 840 .getCanonicalFile()) == 0); 841 } 842 } finally { 843 f.delete(); 844 f2.delete(); 845 dir.delete(); 846 testdir.delete(); 847 } 848 } 849 } catch (IOException e) { 850 fail ("Unexpected IOException during Test : " + e.getMessage()); 851 } 852 } 853 854 /** 855 * @tests java.io.File#getCanonicalPath() 856 */ 857 public void test_getCanonicalPath() { 858 // Test for method java.lang.String java.io.File.getCanonicalPath() 859 // Should work for Unix/Windows. 860 String dots = ".."; 861 try { 862 String base = new File(System.getProperty("user.dir")).getCanonicalPath(); 863 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 864 base += slash; 865 File f = new File(base, "temp.tst"); 866 assertEquals("Test 1: Incorrect Path Returned.", base + "temp.tst", f 867 .getCanonicalPath()); 868 f = new File(base + "Temp" + slash + dots + slash + "temp.tst"); 869 assertEquals("Test 2: Incorrect Path Returned.", base + "temp.tst", f 870 .getCanonicalPath()); 871 872 // Finding a non-existent directory for tests 3 and 4 873 // This is necessary because getCanonicalPath is case sensitive and 874 // could 875 // cause a failure in the test if the directory exists but with 876 // different 877 // case letters (e.g "Temp" and "temp") 878 int dirNumber = 1; 879 boolean dirExists = true; 880 File dir1 = new File(base, String.valueOf(dirNumber)); 881 while (dirExists) { 882 if (dir1.exists()) { 883 dirNumber++; 884 dir1 = new File(base, String.valueOf(dirNumber)); 885 } else { 886 dirExists = false; 887 } 888 } 889 f = new File(base + dirNumber + slash + dots + slash + dirNumber + slash 890 + "temp.tst"); 891 // System.out.println(f.getCanonicalPath()); 892 // System.out.println(userDir + dirNumber + slash + "temp.tst"); 893 assertEquals("Test 3: Incorrect Path Returned.", base + dirNumber + slash 894 + "temp.tst", f.getCanonicalPath()); 895 f = new File(base + dirNumber + slash + "Temp" + slash + dots + slash + "Test" 896 + slash + "temp.tst"); 897 assertEquals("Test 4: Incorrect Path Returned.", base + dirNumber + slash + "Test" 898 + slash + "temp.tst", f.getCanonicalPath()); 899 900 f = new File("1234.567"); 901 assertEquals("Test 5: Incorrect Path Returned.", base + "1234.567", f 902 .getCanonicalPath()); 903 904 // Test for long file names on Windows 905 boolean onWindows = (File.separatorChar == '\\'); 906 if (onWindows) { 907 File testdir = new File(base, "long-" + platformId); 908 testdir.mkdir(); 909 File f1 = new File(testdir, "longfilename" + platformId + ".tst"); 910 FileOutputStream fos = new FileOutputStream(f1); 911 File f2 = null, f3 = null, dir2 = null; 912 try { 913 fos.close(); 914 String dirName1 = f1.getCanonicalPath(); 915 File f4 = new File(testdir, "longfi~1.tst"); 916 /* 917 * If the "short file name" doesn't exist, then assume that 918 * the 8.3 file name compatibility is disabled. 919 */ 920 if (f4.exists()) { 921 String dirName2 = f4.getCanonicalPath(); 922 assertEquals("Test 6: Incorrect Path Returned.", dirName1, dirName2); 923 dir2 = new File(testdir, "longdirectory" + platformId); 924 if (!dir2.exists()) 925 assertTrue("Could not create dir: " + dir2, dir2.mkdir()); 926 f2 = new File(testdir.getPath() + slash + "longdirectory" + platformId 927 + slash + "Test" + slash + dots + slash + "longfilename.tst"); 928 FileOutputStream fos2 = new FileOutputStream(f2); 929 fos2.close(); 930 dirName1 = f2.getCanonicalPath(); 931 f3 = new File(testdir.getPath() + slash + "longdi~1" + slash + "Test" 932 + slash + dots + slash + "longfi~1.tst"); 933 dirName2 = f3.getCanonicalPath(); 934 assertEquals("Test 7: Incorrect Path Returned.", dirName1, dirName2); 935 } 936 } finally { 937 f1.delete(); 938 if (f2 != null) 939 f2.delete(); 940 if (dir2 != null) 941 dir2.delete(); 942 testdir.delete(); 943 } 944 } 945 } catch (IOException e) { 946 fail("Unexpected IOException During Test : " + e.getMessage()); 947 } 948 } 949 950 /** 951 * @tests java.io.File#getName() 952 */ 953 public void test_getName() { 954 // Test for method java.lang.String java.io.File.getName() 955 File f = new File("name.tst"); 956 assertEquals("Test 1: Returned incorrect name", 957 "name.tst", f.getName()); 958 959 f = new File(""); 960 assertTrue("Test 2: Returned incorrect name", f.getName().equals("")); 961 962 f.delete(); 963 } 964 965 /** 966 * @tests java.io.File#getParent() 967 */ 968 public void test_getParent() { 969 // Test for method java.lang.String java.io.File.getParent() 970 File f = new File("p.tst"); 971 assertNull("Incorrect path returned", f.getParent()); 972 f = new File(System.getProperty("user.home"), "p.tst"); 973 assertTrue("Incorrect path returned", f.getParent().equals( 974 System.getProperty("user.home"))); 975 try { 976 f.delete(); 977 } catch (Exception e) { 978 fail("Unexpected exception during tests : " + e.getMessage()); 979 } 980 981 File f1 = new File("/directory"); 982 assertTrue("Wrong parent test 1", f1.getParent().equals(slash)); 983 f1 = new File("/directory/file"); 984 assertTrue("Wrong parent test 2", f1.getParent().equals( 985 slash + "directory")); 986 f1 = new File("directory/file"); 987 assertEquals("Wrong parent test 3", "directory", f1.getParent()); 988 f1 = new File("/"); 989 assertNull("Wrong parent test 4", f1.getParent()); 990 f1 = new File("directory"); 991 assertNull("Wrong parent test 5", f1.getParent()); 992 993 if (File.separatorChar == '\\' && new File("d:/").isAbsolute()) { 994 f1 = new File("d:/directory"); 995 assertTrue("Wrong parent test 1a", f1.getParent().equals( 996 "d:" + slash)); 997 f1 = new File("d:/directory/file"); 998 assertTrue("Wrong parent test 2a", f1.getParent().equals( 999 "d:" + slash + "directory")); 1000 f1 = new File("d:directory/file"); 1001 assertEquals("Wrong parent test 3a", 1002 "d:directory", f1.getParent()); 1003 f1 = new File("d:/"); 1004 assertNull("Wrong parent test 4a", f1.getParent()); 1005 f1 = new File("d:directory"); 1006 assertEquals("Wrong parent test 5a", "d:", f1.getParent()); 1007 } 1008 } 1009 1010 /** 1011 * @tests java.io.File#getParentFile() 1012 */ 1013 public void test_getParentFile() { 1014 // Test for method java.io.File.getParentFile() 1015 File f = new File("tempfile.tst"); 1016 assertNull("Incorrect path returned", f.getParentFile()); 1017 f = new File(System.getProperty("user.dir"), "tempfile1.tmp"); 1018 File f2 = new File(System.getProperty("user.dir"), "tempfile2.tmp"); 1019 File f3 = new File(System.getProperty("user.dir"), "/a/tempfile.tmp"); 1020 assertEquals("Incorrect File Returned", 0, f.getParentFile().compareTo( 1021 f2.getParentFile())); 1022 assertTrue("Incorrect File Returned", f.getParentFile().compareTo( 1023 f3.getParentFile()) != 0); 1024 f.delete(); 1025 f2.delete(); 1026 f3.delete(); 1027 } 1028 1029 /** 1030 * @tests java.io.File#getPath() 1031 */ 1032 public void test_getPath() { 1033 // Test for method java.lang.String java.io.File.getPath() 1034 String base = System.getProperty("user.home"); 1035 String fname; 1036 File f1; 1037 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 1038 base += slash; 1039 fname = base + "filechk.tst"; 1040 f1 = new File(base, "filechk.tst"); 1041 File f2 = new File("filechk.tst"); 1042 File f3 = new File("c:"); 1043 File f4 = new File(base + "a" + slash + slash + ".." + slash 1044 + "filechk.tst"); 1045 assertTrue("getPath returned incorrect path(f1) " + f1.getPath(), f1 1046 .getPath().equals(fname)); 1047 assertTrue("getPath returned incorrect path(f2) " + f2.getPath(), f2 1048 .getPath().equals("filechk.tst")); 1049 assertTrue("getPath returned incorrect path(f3) " + f3.getPath(), f3 1050 .getPath().equals("c:")); 1051 assertTrue("getPath returned incorrect path(f4) " + f4.getPath(), f4 1052 .getPath().equals( 1053 base + "a" + slash + ".." + slash + "filechk.tst")); 1054 f1.delete(); 1055 f2.delete(); 1056 f3.delete(); 1057 f4.delete(); 1058 } 1059 1060 /** 1061 * @tests java.io.File#isAbsolute() 1062 */ 1063 public void test_isAbsolute() { 1064 // Test for method boolean java.io.File.isAbsolute() 1065 if (File.separatorChar == '\\') { 1066 File f = new File("c:\\test"); 1067 File f1 = new File("\\test"); 1068 // One or the other should be absolute on Windows or CE 1069 assertTrue("Absolute returned false", (f.isAbsolute() && !f1 1070 .isAbsolute()) 1071 || (!f.isAbsolute() && f1.isAbsolute())); 1072 } else { 1073 File f = new File("/test"); 1074 assertTrue("Absolute returned false", f.isAbsolute()); 1075 } 1076 assertTrue("Non-Absolute returned true", !new File("../test") 1077 .isAbsolute()); 1078 } 1079 1080 /** 1081 * @tests java.io.File#isDirectory() 1082 */ 1083 public void test_isDirectory() { 1084 // Test for method boolean java.io.File.isDirectory() 1085 1086 String base = System.getProperty("user.dir"); 1087 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 1088 base += slash; 1089 File f = new File(base); 1090 assertTrue("Test 1: Directory Returned False", f.isDirectory()); 1091 f = new File(base + "zxzxzxz" + platformId); 1092 assertTrue("Test 2: (Not Created) Directory Returned True.", !f 1093 .isDirectory()); 1094 f.mkdir(); 1095 try { 1096 assertTrue("Test 3: Directory Returned False.", f.isDirectory()); 1097 } finally { 1098 f.delete(); 1099 } 1100 } 1101 1102 /** 1103 * @tests java.io.File#isFile() 1104 */ 1105 public void test_isFile() { 1106 // Test for method boolean java.io.File.isFile() 1107 try { 1108 String base = System.getProperty("user.dir"); 1109 File f = new File(base); 1110 assertTrue("Directory Returned True As Being A File.", !f.isFile()); 1111 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 1112 base += slash; 1113 f = new File(base, platformId + "amiafile"); 1114 assertTrue("Non-existent File Returned True", !f.isFile()); 1115 FileOutputStream fos = new FileOutputStream(f); 1116 fos.close(); 1117 assertTrue("File returned false", f.isFile()); 1118 f.delete(); 1119 } catch (IOException e) { 1120 fail("IOException during isFile " + e.getMessage()); 1121 } 1122 } 1123 1124 /** 1125 * @tests java.io.File#isHidden() 1126 */ 1127 public void test_isHidden() { 1128 // Test for method boolean java.io.File.isHidden() 1129 boolean onUnix = File.separatorChar == '/'; 1130 try { 1131 File f = File.createTempFile("hyts_", ".tmp"); 1132 // On Unix hidden files are marked with a "." at the beginning 1133 // of the file name. 1134 if (onUnix) { 1135 File f2 = new File(".test.tst" + platformId); 1136 FileOutputStream fos2 = new FileOutputStream(f2); 1137 fos2.close(); 1138 assertTrue("File returned hidden on Unix", !f.isHidden()); 1139 assertTrue("File returned visible on Unix", f2.isHidden()); 1140 assertTrue("File did not delete.", f2.delete()); 1141 } else { 1142 // For windows, the file is being set hidden by the attrib 1143 // command. 1144 Runtime r = Runtime.getRuntime(); 1145 assertTrue("File returned hidden", !f.isHidden()); 1146 Process p = r.exec("attrib +h \"" + f.getAbsolutePath() + "\""); 1147 p.waitFor(); 1148 assertTrue("File returned visible", f.isHidden()); 1149 p = r.exec("attrib -h \"" + f.getAbsolutePath() + "\""); 1150 p.waitFor(); 1151 assertTrue("File returned hidden", !f.isHidden()); 1152 } 1153 f.delete(); 1154 } catch (IOException e) { 1155 fail("Unexpected IOException during test : " + e.getMessage()); 1156 } catch (InterruptedException e) { 1157 fail("Unexpected InterruptedException during test : " 1158 + e.getMessage()); 1159 } 1160 } 1161 1162 /** 1163 * @tests java.io.File#lastModified() 1164 */ 1165 public void test_lastModified() { 1166 // Test for method long java.io.File.lastModified() 1167 try { 1168 File f = new File(System.getProperty("java.io.tmpdir"), platformId 1169 + "lModTest.tst"); 1170 f.delete(); 1171 long lastModifiedTime = f.lastModified(); 1172 assertEquals("LastModified Time Should Have Returned 0.", 1173 0, lastModifiedTime); 1174 FileOutputStream fos = new FileOutputStream(f); 1175 fos.close(); 1176 f.setLastModified(315550800000L); 1177 lastModifiedTime = f.lastModified(); 1178 assertTrue("LastModified Time Incorrect: " + lastModifiedTime, 1179 lastModifiedTime == 315550800000L); 1180 f.delete(); 1181 1182 // Regression for Harmony-2146 1183 f = new File("/../"); 1184 assertTrue(f.lastModified() > 0); 1185 } catch (IOException e) { 1186 fail("Unexpected IOException during test : " + e.getMessage()); 1187 } 1188 } 1189 1190 /** 1191 * @tests java.io.File#length() 1192 */ 1193 public void test_length() throws Exception { 1194 // Test for method long java.io.File.length() 1195 try { 1196 File f = new File(System.getProperty("user.dir"), platformId 1197 + "input.tst"); 1198 assertEquals("File Length Should Have Returned 0.", 0, f.length()); 1199 FileOutputStream fos = new FileOutputStream(f); 1200 fos.write(fileString.getBytes()); 1201 fos.close(); 1202 assertTrue("Incorrect file length returned: " + f.length(), f 1203 .length() == fileString.length()); 1204 f.delete(); 1205 } catch (IOException e) { 1206 fail("Unexpected IOException during test : " + e.getMessage()); 1207 } 1208 1209 // regression test for Harmony-1497 1210 File f = File.createTempFile("test", "tmp"); 1211 f.deleteOnExit(); 1212 RandomAccessFile raf = new RandomAccessFile(f, "rwd"); 1213 raf.write(0x41); 1214 assertEquals(1, f.length()); 1215 } 1216 1217 /** 1218 * @tests java.io.File#list() 1219 */ 1220 public void test_list() { 1221 // Test for method java.lang.String [] java.io.File.list() 1222 1223 String base = System.getProperty("user.dir"); 1224 // Old test left behind "garbage files" so this time it creates a 1225 // directory 1226 // that is guaranteed not to already exist (and deletes it afterward.) 1227 int dirNumber = 1; 1228 boolean dirExists = true; 1229 File dir = null; 1230 dir = new File(base, platformId + String.valueOf(dirNumber)); 1231 while (dirExists) { 1232 if (dir.exists()) { 1233 dirNumber++; 1234 dir = new File(base, String.valueOf(dirNumber)); 1235 } else { 1236 dirExists = false; 1237 } 1238 } 1239 1240 String[] flist = dir.list(); 1241 1242 assertNull("Method list() Should Have Returned null.", flist); 1243 1244 assertTrue("Could not create parent directory for list test", dir 1245 .mkdir()); 1246 1247 String[] files = { "mtzz1.xx", "mtzz2.xx", "mtzz3.yy", "mtzz4.yy" }; 1248 try { 1249 assertEquals("Method list() Should Have Returned An Array Of Length 0.", 1250 0, dir.list().length); 1251 1252 File file = new File(dir, "notADir.tst"); 1253 try { 1254 FileOutputStream fos = new FileOutputStream(file); 1255 fos.close(); 1256 assertNull( 1257 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1258 file.list()); 1259 } catch (IOException e) { 1260 fail("Unexpected IOException during test : " + e.getMessage()); 1261 } finally { 1262 file.delete(); 1263 } 1264 1265 try { 1266 for (int i = 0; i < files.length; i++) { 1267 File f = new File(dir, files[i]); 1268 FileOutputStream fos = new FileOutputStream(f); 1269 fos.close(); 1270 } 1271 } catch (IOException e) { 1272 fail("Unexpected IOException during test : " + e.getMessage()); 1273 } 1274 1275 flist = dir.list(); 1276 if (flist.length != files.length) { 1277 fail("Incorrect list returned"); 1278 } 1279 1280 // Checking to make sure the correct files were are listed in the 1281 // array. 1282 boolean[] check = new boolean[flist.length]; 1283 for (int i = 0; i < check.length; i++) 1284 check[i] = false; 1285 for (int i = 0; i < files.length; i++) { 1286 for (int j = 0; j < flist.length; j++) { 1287 if (flist[j].equals(files[i])) { 1288 check[i] = true; 1289 break; 1290 } 1291 } 1292 } 1293 int checkCount = 0; 1294 for (int i = 0; i < check.length; i++) { 1295 if (check[i] == false) 1296 checkCount++; 1297 } 1298 assertEquals("Invalid file returned in listing", 0, checkCount); 1299 1300 for (int i = 0; i < files.length; i++) { 1301 File f = new File(dir, files[i]); 1302 f.delete(); 1303 } 1304 1305 assertTrue("Could not delete parent directory for list test.", dir 1306 .delete()); 1307 } finally { 1308 for (int i = 0; i < files.length; i++) { 1309 File f = new File(dir, files[i]); 1310 f.delete(); 1311 } 1312 dir.delete(); 1313 } 1314 1315 } 1316 1317 /** 1318 * @tests java.io.File#listFiles() 1319 */ 1320 public void test_listFiles() { 1321 // Test for method java.io.File.listFiles() 1322 1323 try { 1324 String base = System.getProperty("user.dir"); 1325 // Finding a non-existent directory to create. 1326 int dirNumber = 1; 1327 boolean dirExists = true; 1328 File dir = new File(base, platformId + String.valueOf(dirNumber)); 1329 // Making sure that the directory does not exist. 1330 while (dirExists) { 1331 // If the directory exists, add one to the directory number 1332 // (making 1333 // it a new directory name.) 1334 if (dir.exists()) { 1335 dirNumber++; 1336 dir = new File(base, String.valueOf(dirNumber)); 1337 } else { 1338 dirExists = false; 1339 } 1340 } 1341 // Test for attempting to cal listFiles on a non-existent directory. 1342 assertNull("listFiles Should Return Null.", dir.listFiles()); 1343 1344 assertTrue("Failed To Create Parent Directory.", dir.mkdir()); 1345 1346 String[] files = { "1.tst", "2.tst", "3.tst", "" }; 1347 try { 1348 assertEquals("listFiles Should Return An Array Of Length 0.", 0, dir 1349 .listFiles().length); 1350 1351 File file = new File(dir, "notADir.tst"); 1352 try { 1353 FileOutputStream fos = new FileOutputStream(file); 1354 fos.close(); 1355 assertNull( 1356 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1357 file.listFiles()); 1358 } catch (IOException e) { 1359 fail("Unexpected IOException during test : " + e.getMessage()); 1360 } finally { 1361 file.delete(); 1362 } 1363 1364 for (int i = 0; i < (files.length - 1); i++) { 1365 File f = new File(dir, files[i]); 1366 FileOutputStream fos = new FileOutputStream(f); 1367 fos.close(); 1368 } 1369 1370 new File(dir, "doesNotExist.tst"); 1371 File[] flist = dir.listFiles(); 1372 1373 // Test to make sure that only the 3 files that were created are 1374 // listed. 1375 assertEquals("Incorrect Number Of Files Returned.", 1376 3, flist.length); 1377 1378 // Test to make sure that listFiles can read hidden files. 1379 boolean onUnix = File.separatorChar == '/'; 1380 boolean onWindows = File.separatorChar == '\\'; 1381 if (onWindows) { 1382 files[3] = "4.tst"; 1383 File f = new File(dir, "4.tst"); 1384 FileOutputStream fos = new FileOutputStream(f); 1385 fos.close(); 1386 Runtime r = Runtime.getRuntime(); 1387 Process p = r.exec("attrib +h \"" + f.getPath() + "\""); 1388 p.waitFor(); 1389 } 1390 if (onUnix) { 1391 files[3] = ".4.tst"; 1392 File f = new File(dir, ".4.tst"); 1393 FileOutputStream fos = new FileOutputStream(f); 1394 fos.close(); 1395 } 1396 flist = dir.listFiles(); 1397 assertEquals("Incorrect Number Of Files Returned.", 1398 4, flist.length); 1399 1400 // Checking to make sure the correct files were are listed in 1401 // the array. 1402 boolean[] check = new boolean[flist.length]; 1403 for (int i = 0; i < check.length; i++) 1404 check[i] = false; 1405 for (int i = 0; i < files.length; i++) { 1406 for (int j = 0; j < flist.length; j++) { 1407 if (flist[j].getName().equals(files[i])) { 1408 check[i] = true; 1409 break; 1410 } 1411 } 1412 } 1413 int checkCount = 0; 1414 for (int i = 0; i < check.length; i++) { 1415 if (check[i] == false) 1416 checkCount++; 1417 } 1418 assertEquals("Invalid file returned in listing", 0, checkCount); 1419 1420 if (onWindows) { 1421 Runtime r = Runtime.getRuntime(); 1422 Process p = r.exec("attrib -h \"" 1423 + new File(dir, files[3]).getPath() + "\""); 1424 p.waitFor(); 1425 } 1426 1427 for (int i = 0; i < files.length; i++) { 1428 File f = new File(dir, files[i]); 1429 f.delete(); 1430 } 1431 assertTrue("Parent Directory Not Deleted.", dir.delete()); 1432 } finally { 1433 for (int i = 0; i < files.length; i++) { 1434 File f = new File(dir, files[i]); 1435 f.delete(); 1436 } 1437 dir.delete(); 1438 } 1439 } catch (IOException e) { 1440 fail("Unexpected IOException during test : " + e.getMessage()); 1441 } catch (InterruptedException e) { 1442 fail("Unexpected InterruptedException during test : " + e.getMessage()); 1443 } 1444 } 1445 1446 /** 1447 * @tests java.io.File#listFiles(java.io.FileFilter) 1448 */ 1449 public void test_listFilesLjava_io_FileFilter() { 1450 // Test for method java.io.File.listFiles(File Filter filter) 1451 1452 String base = System.getProperty("java.io.tmpdir"); 1453 // Finding a non-existent directory to create. 1454 int dirNumber = 1; 1455 boolean dirExists = true; 1456 File baseDir = new File(base, platformId + String.valueOf(dirNumber)); 1457 // Making sure that the directory does not exist. 1458 while (dirExists) { 1459 // If the directory exists, add one to the directory number (making 1460 // it a new directory name.) 1461 if (baseDir.exists()) { 1462 dirNumber++; 1463 baseDir = new File(base, String.valueOf(dirNumber)); 1464 } else { 1465 dirExists = false; 1466 } 1467 } 1468 1469 // Creating a filter that catches directories. 1470 FileFilter dirFilter = new FileFilter() { 1471 public boolean accept(File f) { 1472 if (f.isDirectory()) 1473 return true; 1474 else 1475 return false; 1476 } 1477 }; 1478 1479 assertNull("listFiles Should Return Null.", baseDir 1480 .listFiles(dirFilter)); 1481 1482 assertTrue("Failed To Create Parent Directory.", baseDir.mkdir()); 1483 1484 File dir1 = null; 1485 String[] files = { "1.tst", "2.tst", "3.tst" }; 1486 try { 1487 assertEquals("listFiles Should Return An Array Of Length 0.", 0, baseDir 1488 .listFiles(dirFilter).length); 1489 1490 File file = new File(baseDir, "notADir.tst"); 1491 try { 1492 FileOutputStream fos = new FileOutputStream(file); 1493 fos.close(); 1494 assertNull( 1495 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1496 file.listFiles(dirFilter)); 1497 } catch (IOException e) { 1498 fail("Unexpected IOException During Test."); 1499 } finally { 1500 file.delete(); 1501 } 1502 1503 try { 1504 for (int i = 0; i < files.length; i++) { 1505 File f = new File(baseDir, files[i]); 1506 FileOutputStream fos = new FileOutputStream(f); 1507 fos.close(); 1508 } 1509 } catch (IOException e) { 1510 fail("Unexpected IOException during test : " + e.getMessage()); 1511 } 1512 dir1 = new File(baseDir, "Temp1"); 1513 dir1.mkdir(); 1514 1515 // Creating a filter that catches files. 1516 FileFilter fileFilter = new FileFilter() { 1517 public boolean accept(File f) { 1518 if (f.isFile()) 1519 return true; 1520 else 1521 return false; 1522 } 1523 }; 1524 1525 // Test to see if the correct number of directories are returned. 1526 File[] directories = baseDir.listFiles(dirFilter); 1527 assertEquals("Incorrect Number Of Directories Returned.", 1528 1, directories.length); 1529 1530 // Test to see if the directory was saved with the correct name. 1531 assertEquals("Incorrect Directory Returned.", 0, directories[0] 1532 .compareTo(dir1)); 1533 1534 // Test to see if the correct number of files are returned. 1535 File[] flist = baseDir.listFiles(fileFilter); 1536 assertTrue("Incorrect Number Of Files Returned.", 1537 flist.length == files.length); 1538 1539 // Checking to make sure the correct files were are listed in the 1540 // array. 1541 boolean[] check = new boolean[flist.length]; 1542 for (int i = 0; i < check.length; i++) 1543 check[i] = false; 1544 for (int i = 0; i < files.length; i++) { 1545 for (int j = 0; j < flist.length; j++) { 1546 if (flist[j].getName().equals(files[i])) { 1547 check[i] = true; 1548 break; 1549 } 1550 } 1551 } 1552 int checkCount = 0; 1553 for (int i = 0; i < check.length; i++) { 1554 if (check[i] == false) 1555 checkCount++; 1556 } 1557 assertEquals("Invalid file returned in listing", 0, checkCount); 1558 1559 for (int i = 0; i < files.length; i++) { 1560 File f = new File(baseDir, files[i]); 1561 f.delete(); 1562 } 1563 dir1.delete(); 1564 assertTrue("Parent Directory Not Deleted.", baseDir.delete()); 1565 } finally { 1566 for (int i = 0; i < files.length; i++) { 1567 File f = new File(baseDir, files[i]); 1568 f.delete(); 1569 } 1570 if (dir1 != null) 1571 dir1.delete(); 1572 baseDir.delete(); 1573 } 1574 } 1575 1576 /** 1577 * @tests java.io.File#listFiles(java.io.FilenameFilter) 1578 */ 1579 public void test_listFilesLjava_io_FilenameFilter() { 1580 // Test for method java.io.File.listFiles(FilenameFilter filter) 1581 1582 String base = System.getProperty("java.io.tmpdir"); 1583 // Finding a non-existent directory to create. 1584 int dirNumber = 1; 1585 boolean dirExists = true; 1586 File dir = new File(base, platformId + String.valueOf(dirNumber)); 1587 // Making sure that the directory does not exist. 1588 while (dirExists) { 1589 // If the directory exists, add one to the directory number (making 1590 // it a new directory name.) 1591 if (dir.exists()) { 1592 dirNumber++; 1593 dir = new File(base, platformId + String.valueOf(dirNumber)); 1594 } else { 1595 dirExists = false; 1596 } 1597 } 1598 1599 // Creating a filter that catches "*.tst" files. 1600 FilenameFilter tstFilter = new FilenameFilter() { 1601 public boolean accept(File f, String fileName) { 1602 // If the suffix is ".tst" then send it to the array 1603 if (fileName.endsWith(".tst")) 1604 return true; 1605 else 1606 return false; 1607 } 1608 }; 1609 1610 assertNull("listFiles Should Return Null.", 1611 dir.listFiles(tstFilter)); 1612 1613 assertTrue("Failed To Create Parent Directory.", dir.mkdir()); 1614 1615 String[] files = { "1.tst", "2.tst", "3.tmp" }; 1616 try { 1617 assertEquals("listFiles Should Return An Array Of Length 0.", 0, dir 1618 .listFiles(tstFilter).length); 1619 1620 File file = new File(dir, "notADir.tst"); 1621 try { 1622 FileOutputStream fos = new FileOutputStream(file); 1623 fos.close(); 1624 assertNull( 1625 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1626 file.listFiles(tstFilter)); 1627 } catch (IOException e) { 1628 fail("Unexpected IOException during test : " + e.getMessage()); 1629 } finally { 1630 file.delete(); 1631 } 1632 1633 try { 1634 for (int i = 0; i < files.length; i++) { 1635 File f = new File(dir, files[i]); 1636 FileOutputStream fos = new FileOutputStream(f); 1637 fos.close(); 1638 } 1639 } catch (IOException e) { 1640 fail("Unexpected IOException During Test : " + e.getMessage()); 1641 } 1642 1643 // Creating a filter that catches "*.tmp" files. 1644 FilenameFilter tmpFilter = new FilenameFilter() { 1645 public boolean accept(File f, String fileName) { 1646 // If the suffix is ".tmp" then send it to the array 1647 if (fileName.endsWith(".tmp")) 1648 return true; 1649 else 1650 return false; 1651 } 1652 }; 1653 1654 // Tests to see if the correct number of files were returned. 1655 File[] flist = dir.listFiles(tstFilter); 1656 assertEquals("Incorrect Number Of Files Passed Through tstFilter.", 1657 2, flist.length); 1658 for (int i = 0; i < flist.length; i++) 1659 assertTrue("File Should Not Have Passed The tstFilter.", 1660 flist[i].getPath().endsWith(".tst")); 1661 1662 flist = dir.listFiles(tmpFilter); 1663 assertEquals("Incorrect Number Of Files Passed Through tmpFilter.", 1664 1, flist.length); 1665 assertTrue("File Should Not Have Passed The tmpFilter.", flist[0] 1666 .getPath().endsWith(".tmp")); 1667 1668 for (int i = 0; i < files.length; i++) { 1669 File f = new File(dir, files[i]); 1670 f.delete(); 1671 } 1672 assertTrue("Parent Directory Not Deleted.", dir.delete()); 1673 } finally { 1674 for (int i = 0; i < files.length; i++) { 1675 File f = new File(dir, files[i]); 1676 f.delete(); 1677 } 1678 dir.delete(); 1679 } 1680 } 1681 1682 /** 1683 * @tests java.io.File#list(java.io.FilenameFilter) 1684 */ 1685 public void test_listLjava_io_FilenameFilter() { 1686 // Test for method java.lang.String [] 1687 // java.io.File.list(java.io.FilenameFilter) 1688 1689 String base = System.getProperty("user.dir"); 1690 // Old test left behind "garbage files" so this time it creates a 1691 // directory 1692 // that is guaranteed not to already exist (and deletes it afterward.) 1693 int dirNumber = 1; 1694 boolean dirExists = true; 1695 File dir = new File(base, platformId + String.valueOf(dirNumber)); 1696 while (dirExists) { 1697 if (dir.exists()) { 1698 dirNumber++; 1699 dir = new File(base, String.valueOf(dirNumber)); 1700 } else { 1701 dirExists = false; 1702 } 1703 } 1704 1705 FilenameFilter filter = new FilenameFilter() { 1706 public boolean accept(File dir, String name) { 1707 return !name.equals("mtzz1.xx"); 1708 } 1709 }; 1710 1711 String[] flist = dir.list(filter); 1712 assertNull("Method list(FilenameFilter) Should Have Returned Null.", 1713 flist); 1714 1715 assertTrue("Could not create parent directory for test", dir.mkdir()); 1716 1717 String[] files = { "mtzz1.xx", "mtzz2.xx", "mtzz3.yy", "mtzz4.yy" }; 1718 try { 1719 /* 1720 * Do not return null when trying to use list(Filename Filter) on a 1721 * file rather than a directory. All other "list" methods return 1722 * null for this test case. 1723 */ 1724 /* 1725 * File file = new File(dir, "notADir.tst"); try { FileOutputStream 1726 * fos = new FileOutputStream(file); fos.close(); } catch 1727 * (IOException e) { fail("Unexpected IOException During 1728 * Test."); } flist = dir.list(filter); assertNull("listFiles 1729 * Should Have Returned Null When Used On A File Instead Of A 1730 * Directory.", flist); file.delete(); 1731 */ 1732 1733 flist = dir.list(filter); 1734 assertEquals("Array Of Length 0 Should Have Returned.", 1735 0, flist.length); 1736 1737 try { 1738 for (int i = 0; i < files.length; i++) { 1739 File f = new File(dir, files[i]); 1740 FileOutputStream fos = new FileOutputStream(f); 1741 fos.close(); 1742 } 1743 } catch (IOException e) { 1744 fail("Unexpected IOException during test : " + e.getMessage()); 1745 } 1746 1747 flist = dir.list(filter); 1748 1749 if (flist.length != files.length - 1) { 1750 fail("Incorrect list returned"); 1751 } 1752 1753 // Checking to make sure the correct files were are listed in the 1754 // array. 1755 boolean[] check = new boolean[flist.length]; 1756 for (int i = 0; i < check.length; i++) 1757 check[i] = false; 1758 String[] wantedFiles = { "mtzz2.xx", "mtzz3.yy", "mtzz4.yy" }; 1759 for (int i = 0; i < wantedFiles.length; i++) { 1760 for (int j = 0; j < flist.length; j++) { 1761 if (flist[j].equals(wantedFiles[i])) { 1762 check[i] = true; 1763 break; 1764 } 1765 } 1766 } 1767 int checkCount = 0; 1768 for (int i = 0; i < check.length; i++) { 1769 if (check[i] == false) 1770 checkCount++; 1771 } 1772 assertEquals("Invalid file returned in listing", 0, checkCount); 1773 1774 for (int i = 0; i < files.length; i++) { 1775 File f = new File(dir, files[i]); 1776 f.delete(); 1777 } 1778 assertTrue("Could not delete parent directory for test.", dir 1779 .delete()); 1780 } finally { 1781 for (int i = 0; i < files.length; i++) { 1782 File f = new File(dir, files[i]); 1783 f.delete(); 1784 } 1785 dir.delete(); 1786 } 1787 } 1788 1789 /** 1790 * @tests java.io.File#listRoots() 1791 */ 1792 public void test_listRoots() { 1793 // Test for method java.io.File.listRoots() 1794 1795 File[] roots = File.listRoots(); 1796 boolean onUnix = File.separatorChar == '/'; 1797 boolean onWindows = File.separatorChar == '\\'; 1798 if (onUnix) { 1799 assertEquals("Incorrect Number Of Root Directories.", 1800 1, roots.length); 1801 String fileLoc = roots[0].getPath(); 1802 assertTrue("Incorrect Root Directory Returned.", fileLoc 1803 .startsWith(slash)); 1804 } else if (onWindows) { 1805 // Need better test for Windows 1806 assertTrue("Incorrect Number Of Root Directories.", 1807 roots.length > 0); 1808 } 1809 } 1810 1811 /** 1812 * @tests java.io.File#mkdir() 1813 */ 1814 public void test_mkdir() throws IOException { 1815 // Test for method boolean java.io.File.mkdir() 1816 1817 String base = System.getProperty("user.dir"); 1818 // Old test left behind "garbage files" so this time it creates a 1819 // directory 1820 // that is guaranteed not to already exist (and deletes it afterward.) 1821 int dirNumber = 1; 1822 boolean dirExists = true; 1823 File dir = new File(base, String.valueOf(dirNumber)); 1824 while (dirExists) { 1825 if (dir.exists()) { 1826 dirNumber++; 1827 dir = new File(base, String.valueOf(dirNumber)); 1828 } else { 1829 dirExists = false; 1830 } 1831 } 1832 1833 assertTrue("mkdir failed", dir.mkdir() && dir.exists()); 1834 dir.deleteOnExit(); 1835 1836 String longDirName = "abcdefghijklmnopqrstuvwx";// 24 chars 1837 StringBuilder sb = new StringBuilder(dir + File.separator); 1838 StringBuilder sb2 = new StringBuilder(dir + File.separator); 1839 1840 // Test make a long path 1841 while (dir.getCanonicalPath().length() < 256 - longDirName.length()) { 1842 sb.append(longDirName + File.separator); 1843 dir = new File(sb.toString()); 1844 assertTrue("mkdir failed", dir.mkdir() && dir.exists()); 1845 dir.deleteOnExit(); 1846 } 1847 1848 while (dir.getCanonicalPath().length() < 256) { 1849 sb.append(0); 1850 dir = new File(sb.toString()); 1851 assertTrue("mkdir " + dir.getCanonicalPath().length() + " failed", 1852 dir.mkdir() && dir.exists()); 1853 dir.deleteOnExit(); 1854 } 1855 1856 // Test make many paths 1857 while (dir.getCanonicalPath().length() < 256) { 1858 sb2.append(0); 1859 dir = new File(sb2.toString()); 1860 assertTrue("mkdir " + dir.getCanonicalPath().length() + " failed", 1861 dir.mkdir() && dir.exists()); 1862 dir.deleteOnExit(); 1863 } 1864 } 1865 1866 /** 1867 * @tests java.io.File#mkdirs() 1868 */ 1869 public void test_mkdirs() { 1870 // Test for method boolean java.io.File.mkdirs() 1871 1872 String userHome = System.getProperty("user.dir"); 1873 if (!userHome.endsWith(slash)) 1874 userHome += slash; 1875 File f = new File(userHome + "mdtest" + platformId + slash + "mdtest2", 1876 "p.tst"); 1877 File g = new File(userHome + "mdtest" + platformId + slash + "mdtest2"); 1878 File h = new File(userHome + "mdtest" + platformId); 1879 f.mkdirs(); 1880 try { 1881 assertTrue("Base Directory not created", h.exists()); 1882 assertTrue("Directories not created", g.exists()); 1883 assertTrue("File not created", f.exists()); 1884 } finally { 1885 f.delete(); 1886 g.delete(); 1887 h.delete(); 1888 } 1889 } 1890 1891 /** 1892 * @tests java.io.File#renameTo(java.io.File) 1893 */ 1894 public void test_renameToLjava_io_File() { 1895 // Test for method boolean java.io.File.renameTo(java.io.File) 1896 String base = System.getProperty("user.dir"); 1897 File dir = new File(base, platformId); 1898 dir.mkdir(); 1899 File f = new File(dir, "xxx.xxx"); 1900 File rfile = new File(dir, "yyy.yyy"); 1901 File f2 = new File(dir, "zzz.zzz"); 1902 try { 1903 FileOutputStream fos = new FileOutputStream(f); 1904 fos.write(fileString.getBytes()); 1905 fos.close(); 1906 long lengthOfFile = f.length(); 1907 1908 rfile.delete(); // in case it already exists 1909 1910 assertTrue("Test 1: File Rename Failed", f.renameTo(rfile)); 1911 assertTrue("Test 2: File Rename Failed.", rfile.exists()); 1912 assertTrue("Test 3: Size Of File Changed.", 1913 rfile.length() == lengthOfFile); 1914 1915 fos = new FileOutputStream(rfile); 1916 fos.close(); 1917 1918 f2.delete(); // in case it already exists 1919 assertTrue("Test 4: File Rename Failed", rfile.renameTo(f2)); 1920 assertTrue("Test 5: File Rename Failed.", f2.exists()); 1921 } catch (IOException e) { 1922 fail("Unexpected IOException during test : " + e.getMessage()); 1923 } finally { 1924 f.delete(); 1925 rfile.delete(); 1926 f2.delete(); 1927 dir.delete(); 1928 } 1929 } 1930 1931 /** 1932 * @tests java.io.File#setLastModified(long) 1933 */ 1934 public void test_setLastModifiedJ() { 1935 // Test for method java.io.File.setLastModified() 1936 File f1 = null; 1937 try { 1938 // f1 = File.createTempFile("hyts_tf" , ".tmp"); 1939 // jclRM does not include File.createTempFile 1940 f1 = new File(Support_PlatformFile.getNewPlatformFile( 1941 "hyts_tf_slm", ".tmp")); 1942 f1.createNewFile(); 1943 long orgTime = f1.lastModified(); 1944 // Subtracting 100 000 milliseconds from the orgTime of File f1 1945 f1.setLastModified(orgTime - 100000); 1946 long lastModified = f1.lastModified(); 1947 assertTrue("Test 1: LastModifed time incorrect: " + lastModified, 1948 lastModified == (orgTime - 100000)); 1949 // Subtracting 10 000 000 milliseconds from the orgTime of File f1 1950 f1.setLastModified(orgTime - 10000000); 1951 lastModified = f1.lastModified(); 1952 assertTrue("Test 2: LastModifed time incorrect: " + lastModified, 1953 lastModified == (orgTime - 10000000)); 1954 // Adding 100 000 milliseconds to the orgTime of File f1 1955 f1.setLastModified(orgTime + 100000); 1956 lastModified = f1.lastModified(); 1957 assertTrue("Test 3: LastModifed time incorrect: " + lastModified, 1958 lastModified == (orgTime + 100000)); 1959 // Adding 10 000 000 milliseconds from the orgTime of File f1 1960 f1.setLastModified(orgTime + 10000000); 1961 lastModified = f1.lastModified(); 1962 assertTrue("Test 4: LastModifed time incorrect: " + lastModified, 1963 lastModified == (orgTime + 10000000)); 1964 // Trying to set time to an exact number 1965 f1.setLastModified(315550800000L); 1966 lastModified = f1.lastModified(); 1967 assertTrue("Test 5: LastModified time incorrect: " + lastModified, 1968 lastModified == 315550800000L); 1969 String osName = System.getProperty("os.name", "unknown"); 1970 if (osName.equals("Windows 2000") || osName.equals("Windows NT")) { 1971 // Trying to set time to a large exact number 1972 boolean result = f1.setLastModified(4354837199000L); 1973 long next = f1.lastModified(); 1974 // Dec 31 23:59:59 EST 2107 is overflow on FAT file systems, and 1975 // the call fails 1976 assertTrue("Test 6: LastModified time incorrect: " + next, 1977 !result || next == 4354837199000L); 1978 } 1979 // Trying to set time to a negative number 1980 try { 1981 f1.setLastModified(-25); 1982 fail("IllegalArgumentException Not Thrown."); 1983 } catch (IllegalArgumentException e) { 1984 } 1985 } catch (IOException e) { 1986 fail("Unexpected IOException during test : " + e.getMessage()); 1987 } finally { 1988 if (f1 != null) 1989 f1.delete(); 1990 } 1991 } 1992 1993 /** 1994 * @tests java.io.File#setReadOnly() 1995 */ 1996 public void test_setReadOnly() { 1997 // Test for method java.io.File.setReadOnly() 1998 1999 File f1 = null; 2000 File f2 = null; 2001 try { 2002 f1 = File.createTempFile("hyts_tf", ".tmp"); 2003 f2 = File.createTempFile("hyts_tf", ".tmp"); 2004 // Assert is flawed because canWrite does not work. 2005 // assertTrue("File f1 Is Set To ReadOnly." , f1.canWrite()); 2006 f1.setReadOnly(); 2007 // Assert is flawed because canWrite does not work. 2008 // assertTrue("File f1 Is Not Set To ReadOnly." , !f1.canWrite()); 2009 try { 2010 // Attempt to write to a file that is setReadOnly. 2011 new FileOutputStream(f1); 2012 fail("IOException not thrown."); 2013 } catch (IOException e) { 2014 } 2015 Runtime r = Runtime.getRuntime(); 2016 Process p; 2017 boolean onUnix = File.separatorChar == '/'; 2018 if (onUnix) 2019 p = r.exec("chmod +w " + f1.getAbsolutePath()); 2020 else 2021 p = r.exec("attrib -r \"" + f1.getAbsolutePath() + "\""); 2022 p.waitFor(); 2023 // Assert is flawed because canWrite does not work. 2024 // assertTrue("File f1 Is Set To ReadOnly." , f1.canWrite()); 2025 try { 2026 FileOutputStream fos = new FileOutputStream(f1); 2027 fos.write(fileString.getBytes()); 2028 fos.close(); 2029 assertTrue("File Was Not Able To Be Written To.", 2030 f1.length() == fileString.length()); 2031 } catch (IOException e) { 2032 fail( 2033 "Test 1: Unexpected IOException While Attempting To Write To File." 2034 + e); 2035 } 2036 assertTrue("File f1 Did Not Delete", f1.delete()); 2037 2038 // Assert is flawed because canWrite does not work. 2039 // assertTrue("File f2 Is Set To ReadOnly." , f2.canWrite()); 2040 FileOutputStream fos = new FileOutputStream(f2); 2041 // Write to a file. 2042 fos.write(fileString.getBytes()); 2043 fos.close(); 2044 f2.setReadOnly(); 2045 // Assert is flawed because canWrite does not work. 2046 // assertTrue("File f2 Is Not Set To ReadOnly." , !f2.canWrite()); 2047 try { 2048 // Attempt to write to a file that has previously been written 2049 // to. 2050 // and is now set to read only. 2051 fos = new FileOutputStream(f2); 2052 fail("IOException not thrown."); 2053 } catch (IOException e) { 2054 } 2055 r = Runtime.getRuntime(); 2056 if (onUnix) 2057 p = r.exec("chmod +w " + f2.getAbsolutePath()); 2058 else 2059 p = r.exec("attrib -r \"" + f2.getAbsolutePath() + "\""); 2060 p.waitFor(); 2061 assertTrue("File f2 Is Set To ReadOnly.", f2.canWrite()); 2062 try { 2063 fos = new FileOutputStream(f2); 2064 fos.write(fileString.getBytes()); 2065 fos.close(); 2066 } catch (IOException e) { 2067 fail( 2068 "Test 2: Unexpected IOException While Attempting To Write To File." 2069 + e); 2070 } 2071 f2.setReadOnly(); 2072 assertTrue("File f2 Did Not Delete", f2.delete()); 2073 // Similarly, trying to delete a read-only directory should succeed 2074 f2 = new File(System.getProperty("user.dir"), "deltestdir"); 2075 f2.mkdir(); 2076 f2.setReadOnly(); 2077 assertTrue("Directory f2 Did Not Delete", f2.delete()); 2078 assertTrue("Directory f2 Did Not Delete", !f2.exists()); 2079 2080 } catch (IOException e) { 2081 fail("Unexpected IOException during test : " + e.getMessage()); 2082 } catch (InterruptedException e) { 2083 fail("Unexpected InterruptedException During Test." + e); 2084 } finally { 2085 if (f1 != null) 2086 f1.delete(); 2087 if (f2 != null) 2088 f2.delete(); 2089 } 2090 } 2091 2092 /** 2093 * @tests java.io.File#toString() 2094 */ 2095 public void test_toString() { 2096 // Test for method java.lang.String java.io.File.toString() 2097 String fileName = System.getProperty("user.home") + slash + "input.tst"; 2098 File f = new File(fileName); 2099 assertTrue("Incorrect string returned", f.toString().equals(fileName)); 2100 2101 if (File.separatorChar == '\\') { 2102 String result = new File("c:\\").toString(); 2103 assertTrue("Removed backslash: " + result, result.equals("c:\\")); 2104 } 2105 } 2106 2107 /** 2108 * @tests java.io.File#toURI() 2109 */ 2110 public void test_toURI() { 2111 // Test for method java.io.File.toURI() 2112 try { 2113 // Need a directory that exists 2114 File dir = new File(System.getProperty("user.dir")); 2115 2116 // Test for toURI when the file is a directory. 2117 String newURIPath = dir.getAbsolutePath(); 2118 newURIPath = newURIPath.replace(File.separatorChar, '/'); 2119 if (!newURIPath.startsWith("/")) 2120 newURIPath = "/" + newURIPath; 2121 if (!newURIPath.endsWith("/")) 2122 newURIPath += '/'; 2123 2124 URI uri = dir.toURI(); 2125 assertTrue("Test 1A: Incorrect URI Returned.", new File(uri) 2126 .equals(dir.getAbsoluteFile())); 2127 assertTrue("Test 1B: Incorrect URI Returned.", uri.equals(new URI( 2128 "file", null, newURIPath, null, null))); 2129 2130 // Test for toURI with a file name with illegal chars. 2131 File f = new File(dir, "te% \u20ac st.tst"); 2132 newURIPath = f.getAbsolutePath(); 2133 newURIPath = newURIPath.replace(File.separatorChar, '/'); 2134 if (!newURIPath.startsWith("/")) 2135 newURIPath = "/" + newURIPath; 2136 2137 uri = f.toURI(); 2138 assertTrue("Test 2A: Incorrect URI Returned.", new File(uri) 2139 .equals(f.getAbsoluteFile())); 2140 assertTrue("Test 2B: Incorrect URI Returned.", uri.equals(new URI( 2141 "file", null, newURIPath, null, null))); 2142 2143 // Regression test for HARMONY-3207 2144 dir = new File(""); // current directory 2145 uri = dir.toURI(); 2146 assertTrue("Test current dir: URI does not end with slash.", 2147 uri.toString().endsWith("/")); 2148 } catch (URISyntaxException e1) { 2149 fail("Unexpected URISyntaxException: " + e1); 2150 } 2151 } 2152 2153 /** 2154 * @tests java.io.File#toURL() 2155 */ 2156 public void test_toURL() { 2157 // Test for method java.io.File.toURL() 2158 2159 try { 2160 // Need a directory that exists 2161 File dir = new File(System.getProperty("user.dir")); 2162 2163 // Test for toURL when the file is a directory. 2164 String newDirURL = dir.getAbsolutePath(); 2165 newDirURL = newDirURL.replace(File.separatorChar, '/'); 2166 if (newDirURL.startsWith("/")) 2167 newDirURL = "file:" + newDirURL; 2168 else 2169 newDirURL = "file:/" + newDirURL; 2170 if (!newDirURL.endsWith("/")) 2171 newDirURL += '/'; 2172 assertTrue("Test 1: Incorrect URL Returned.", newDirURL.equals(dir 2173 .toURL().toString())); 2174 2175 // Test for toURL with a file. 2176 File f = new File(dir, "test.tst"); 2177 String newURL = f.getAbsolutePath(); 2178 newURL = newURL.replace(File.separatorChar, '/'); 2179 if (newURL.startsWith("/")) 2180 newURL = "file:" + newURL; 2181 else 2182 newURL = "file:/" + newURL; 2183 assertTrue("Test 2: Incorrect URL Returned.", newURL.equals(f 2184 .toURL().toString())); 2185 2186 // Regression test for HARMONY-3207 2187 dir = new File(""); // current directory 2188 newDirURL = dir.toURL().toString(); 2189 assertTrue("Test current dir: URL does not end with slash.", 2190 newDirURL.endsWith("/")); 2191 } catch (java.net.MalformedURLException e) { 2192 fail( 2193 "Unexpected java.net.MalformedURLException During Test."); 2194 } 2195 2196 } 2197 2198 /** 2199 * @tests java.io.File#toURI() 2200 */ 2201 public void test_toURI2() { 2202 2203 File f = new File(System.getProperty("user.dir"), "a/b/c/../d/e/./f"); 2204 2205 String path = f.getAbsolutePath(); 2206 path = path.replace(File.separatorChar, '/'); 2207 if (!path.startsWith("/")) 2208 path = "/" + path; 2209 2210 try { 2211 URI uri1 = new URI("file", null, path, null); 2212 URI uri2 = f.toURI(); 2213 assertEquals("uris not equal", uri1, uri2); 2214 } catch (URISyntaxException e1) { 2215 fail("Unexpected URISyntaxException," + e1); 2216 } 2217 } 2218 2219 /** 2220 * @tests java.io.File#toURL() 2221 */ 2222 public void test_toURL2() { 2223 2224 File f = new File(System.getProperty("user.dir"), "a/b/c/../d/e/./f"); 2225 2226 String path = f.getAbsolutePath(); 2227 path = path.replace(File.separatorChar, '/'); 2228 if (!path.startsWith("/")) 2229 path = "/" + path; 2230 2231 try { 2232 URL url1 = new URL("file", "", path); 2233 URL url2 = f.toURL(); 2234 assertEquals("urls not equal", url1, url2); 2235 } catch (MalformedURLException e) { 2236 fail("Unexpected MalformedURLException," + e); 2237 } 2238 } 2239 2240 /** 2241 * @tests java.io.File#deleteOnExit() 2242 */ 2243 public void test_deleteOnExit() throws IOException, InterruptedException { 2244 File dir = new File("dir4filetest"); 2245 dir.mkdir(); 2246 assertTrue(dir.exists()); 2247 File subDir = new File("dir4filetest/subdir"); 2248 subDir.mkdir(); 2249 assertTrue(subDir.exists()); 2250 2251 Support_Exec.execJava(new String[] { 2252 "tests.support.Support_DeleteOnExitTest", 2253 dir.getAbsolutePath(), subDir.getAbsolutePath() }, 2254 new String[] {}, false); 2255 assertFalse(dir.exists()); 2256 assertFalse(subDir.exists()); 2257 } 2258 2259 /** 2260 * @tests serilization 2261 */ 2262 public void test_objectStreamClass_getFields() throws Exception { 2263 //Regression for HARMONY-2674 2264 ObjectStreamClass objectStreamClass = ObjectStreamClass 2265 .lookup(File.class); 2266 ObjectStreamField[] objectStreamFields = objectStreamClass.getFields(); 2267 assertEquals(1, objectStreamFields.length); 2268 ObjectStreamField objectStreamField = objectStreamFields[0]; 2269 assertEquals("path", objectStreamField.getName()); 2270 assertEquals(String.class, objectStreamField.getType()); 2271 } 2272 2273 /** 2274 * Sets up the fixture, for example, open a network connection. This method 2275 * is called before a test is executed. 2276 */ 2277 protected void setUp() { 2278 /** Setup the temporary directory */ 2279 String userDir = System.getProperty("user.dir"); 2280 if (userDir == null) 2281 userDir = "j:\\jcl-builddir\\temp\\source"; 2282 if (!userDir.regionMatches((userDir.length() - 1), slash, 0, 1)) 2283 userDir += slash; 2284 tempDirectory = new File(userDir + "tempDir" 2285 + String.valueOf(System.currentTimeMillis())); 2286 if (!tempDirectory.mkdir()) 2287 System.out.println("Setup for FileTest failed."); 2288 2289 /** Setup the temporary file */ 2290 tempFile = new File(tempDirectory, "tempfile"); 2291 FileOutputStream tempStream; 2292 try { 2293 tempStream = new FileOutputStream(tempFile.getPath(), false); 2294 tempStream.close(); 2295 } catch (IOException e) { 2296 System.out.println("Setup for FileTest failed."); 2297 return; 2298 } 2299 } 2300 2301 /** 2302 * Tears down the fixture, for example, close a network connection. This 2303 * method is called after a test is executed. 2304 */ 2305 protected void tearDown() { 2306 if (tempFile.exists() && !tempFile.delete()) 2307 System.out 2308 .println("FileTest.tearDown() failed, could not delete file!"); 2309 if (!tempDirectory.delete()) 2310 System.out 2311 .println("FileTest.tearDown() failed, could not delete directory!"); 2312 } 2313} 2314