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