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