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 public void test_delete() { 696 // Test for method boolean java.io.File.delete() 697 try { 698 File dir = new File(System.getProperty("java.io.tmpdir"), platformId 699 + "filechk"); 700 dir.mkdir(); 701 assertTrue("Directory Does Not Exist", dir.exists() 702 && dir.isDirectory()); 703 File f = new File(dir, "filechk.tst"); 704 FileOutputStream fos = new FileOutputStream(f); 705 fos.close(); 706 assertTrue("Error Creating File For Delete Test", f.exists()); 707 dir.delete(); 708 assertTrue("Directory Should Not Have Been Deleted.", dir.exists()); 709 f.delete(); 710 assertTrue("File Was Not Deleted", !f.exists()); 711 dir.delete(); 712 assertTrue("Directory Was Not Deleted", !dir.exists()); 713 } catch (IOException e) { 714 fail("Unexpected IOException During Delete Test : " 715 + e.getMessage()); 716 } 717 } 718 719 /** 720 * @tests java.io.File#equals(java.lang.Object) 721 */ 722 @TestTargetNew( 723 level = TestLevel.COMPLETE, 724 notes = "Verifies equals() method.", 725 method = "equals", 726 args = {java.lang.Object.class} 727 ) 728 public void test_equalsLjava_lang_Object() { 729 // Test for method boolean java.io.File.equals(java.lang.Object) 730 File f1 = new File("filechk.tst"); 731 File f2 = new File("filechk.tst"); 732 File f3 = new File("xxxx"); 733 734 assertTrue("Equality test failed", f1.equals(f2)); 735 assertTrue("Files Should Not Return Equal.", !f1.equals(f3)); 736 737 f3 = new File("FiLeChK.tst"); 738 boolean onWindows = File.separatorChar == '\\'; 739 boolean onUnix = File.separatorChar == '/'; 740 if (onWindows) 741 assertTrue("Files Should Return Equal.", f1.equals(f3)); 742 else if (onUnix) 743 assertTrue("Files Should NOT Return Equal.", !f1.equals(f3)); 744 745 try { 746 f1 = new File(System.getProperty("java.io.tmpdir"), "casetest.tmp"); 747 f2 = new File(System.getProperty("java.io.tmpdir"), "CaseTest.tmp"); 748 new FileOutputStream(f1).close(); // create the file 749 if (f1.equals(f2)) { 750 try { 751 new FileInputStream(f2); 752 } catch (IOException e) { 753 fail("File system is case sensitive"); 754 } 755 } else { 756 boolean exception = false; 757 try { 758 new FileInputStream(f2); 759 } catch (IOException e) { 760 exception = true; 761 } 762 assertTrue("File system is case insensitive", exception); 763 } 764 f1.delete(); 765 } catch (IOException e) { 766 fail("Unexpected using case sensitive test : " + e.getMessage()); 767 } 768 } 769 770 /** 771 * @tests java.io.File#exists() 772 */ 773 @TestTargetNew( 774 level = TestLevel.PARTIAL_COMPLETE, 775 method = "exists", 776 args = {} 777 ) 778 public void test_exists() { 779 // Test for method boolean java.io.File.exists() 780 try { 781 File f = new File(System.getProperty("java.io.tmpdir"), platformId 782 + "exists.tst"); 783 assertTrue("Exists returned true for non-existent file", !f 784 .exists()); 785 FileOutputStream fos = new FileOutputStream(f); 786 fos.close(); 787 assertTrue("Exists returned false file", f.exists()); 788 f.delete(); 789 } catch (IOException e) { 790 fail("Unexpected IOException During Test : " + e.getMessage()); 791 } 792 } 793 794 /** 795 * @tests java.io.File#getAbsoluteFile() 796 */ 797 @TestTargetNew( 798 level = TestLevel.PARTIAL_COMPLETE, 799 method = "getAbsoluteFile", 800 args = {} 801 ) 802 public void test_getAbsoluteFile() { 803 // Test for method java.io.File getAbsoluteFile() 804 String base = System.getProperty("java.io.tmpdir"); 805 if (!base.endsWith(slash)) 806 base += slash; 807 File f = new File(base, "temp.tst"); 808 File f2 = f.getAbsoluteFile(); 809 assertEquals("Test 1: Incorrect File Returned.", 0, f2.compareTo(f 810 .getAbsoluteFile())); 811 f = new File(base + "Temp" + slash + slash + "temp.tst"); 812 f2 = f.getAbsoluteFile(); 813 assertEquals("Test 2: Incorrect File Returned.", 0, f2.compareTo(f 814 .getAbsoluteFile())); 815 f = new File(base + slash + ".." + slash + "temp.tst"); 816 f2 = f.getAbsoluteFile(); 817 assertEquals("Test 3: Incorrect File Returned.", 0, f2.compareTo(f 818 .getAbsoluteFile())); 819 f.delete(); 820 f2.delete(); 821 } 822 823 /** 824 * @tests java.io.File#getAbsolutePath() 825 */ 826 @TestTargetNew( 827 level = TestLevel.PARTIAL_COMPLETE, 828 method = "getAbsolutePath", 829 args = {} 830 ) 831 public void test_getAbsolutePath() { 832 // Test for method java.lang.String java.io.File.getAbsolutePath() 833 String base = System.getProperty("java.io.tmpdir"); 834 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 835 base += slash; 836 File f = new File(base, "temp.tst"); 837 assertTrue("Test 1: Incorrect path returned.", f.getAbsolutePath() 838 .equals(base + "temp.tst")); 839 f = new File(base + "Temp" + slash + slash + slash + "Testing" + slash 840 + "temp.tst"); 841 assertTrue("Test 2: Incorrect path returned.", f.getAbsolutePath() 842 .equals(base + "Temp" + slash + "Testing" + slash + "temp.tst")); 843 f = new File(base + "a" + slash + slash + ".." + slash + "temp.tst"); 844 assertTrue("Test 3: Incorrect path returned." + f.getAbsolutePath(), f 845 .getAbsolutePath().equals( 846 base + "a" + slash + ".." + slash + "temp.tst")); 847 f.delete(); 848 } 849 850 /** 851 * @tests java.io.File#getCanonicalFile() 852 */ 853 @TestTargetNew( 854 level = TestLevel.SUFFICIENT, 855 notes = "IOException not checked since it may only occur if" + 856 "the native access to the filesystem fails.", 857 method = "getCanonicalFile", 858 args = {} 859 ) 860 public void test_getCanonicalFile() { 861 // Test for method java.io.File.getCanonicalFile() 862 try { 863 String base = System.getProperty("java.io.tmpdir"); 864 if (!base.endsWith(slash)) 865 base += slash; 866 File f = new File(base, "temp.tst"); 867 File f2 = f.getCanonicalFile(); 868 assertEquals("Test 1: Incorrect file returned.", 0, f2 869 .getCanonicalFile().compareTo(f.getCanonicalFile())); 870 f = new File(base + "Temp" + slash + slash + "temp.tst"); 871 f2 = f.getCanonicalFile(); 872 assertEquals("Test 2: Incorrect file returned.", 0, f2 873 .getCanonicalFile().compareTo(f.getCanonicalFile())); 874 f = new File(base + "Temp" + slash + slash + ".." + slash 875 + "temp.tst"); 876 f2 = f.getCanonicalFile(); 877 assertEquals("Test 3: Incorrect file returned.", 0, f2 878 .getCanonicalFile().compareTo(f.getCanonicalFile())); 879 880 // Test for when long directory/file names in Windows 881 boolean onWindows = File.separatorChar == '\\'; 882 // String userDir = System.getProperty("java.io.tmpdir"); 883 if (onWindows) { 884 File testdir = new File(base, "long-" + platformId); 885 testdir.mkdir(); 886 File dir = new File(testdir, "longdirectory" + platformId); 887 try { 888 dir.mkdir(); 889 f = new File(dir, "longfilename.tst"); 890 f2 = f.getCanonicalFile(); 891 assertEquals("Test 4: Incorrect file returned.", 892 0, f2.getCanonicalFile().compareTo( 893 f.getCanonicalFile())); 894 FileOutputStream fos = new FileOutputStream(f); 895 fos.close(); 896 f2 = new File(testdir + slash + "longdi~1" + slash 897 + "longfi~1.tst"); 898 File canonicalf2 = f2.getCanonicalFile(); 899 /* 900 * If the "short file name" doesn't exist, then assume that 901 * the 8.3 file name compatibility is disabled. 902 */ 903 if (canonicalf2.exists()) { 904 assertTrue("Test 5: Incorrect file returned: " 905 + canonicalf2, canonicalf2.compareTo(f 906 .getCanonicalFile()) == 0); 907 } 908 } finally { 909 f.delete(); 910 f2.delete(); 911 dir.delete(); 912 testdir.delete(); 913 } 914 } 915 } catch (IOException e) { 916 fail ("Unexpected IOException during Test : " + e.getMessage()); 917 } 918 } 919 920 /** 921 * @tests java.io.File#getCanonicalPath() 922 */ 923 @TestTargetNew( 924 level = TestLevel.SUFFICIENT, 925 notes = "IOException not checked since it may only occur if" + 926 "the native access to the filesystem fails.", 927 method = "getCanonicalPath", 928 args = {} 929 ) 930 public void test_getCanonicalPath() { 931 // Test for method java.lang.String java.io.File.getCanonicalPath() 932 // Should work for Unix/Windows. 933 String dots = ".."; 934 String expected; 935 String error; 936 String tmpDir = System.getProperty("java.io.tmpdir"); 937 String oldUserDir = System.getProperty("user.dir"); 938 System.setProperty("user.dir", tmpDir); 939 try { 940 String base = new File(tmpDir).getCanonicalPath(); 941 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 942 base += slash; 943 File f = new File(base, "temp.tst"); 944 assertEquals("Test 1: Incorrect Path Returned.", base + "temp.tst", f 945 .getCanonicalPath()); 946 f = new File(base + "Temp" + slash + dots + slash + "temp.tst"); 947 assertEquals("Test 2: Incorrect Path Returned.", base + "temp.tst", f 948 .getCanonicalPath()); 949 950 // Finding a non-existent directory for tests 3 and 4 951 // This is necessary because getCanonicalPath is case sensitive and 952 // could 953 // cause a failure in the test if the directory exists but with 954 // different 955 // case letters (e.g "Temp" and "temp") 956 int dirNumber = 1; 957 boolean dirExists = true; 958 File dir1 = new File(base, String.valueOf(dirNumber)); 959 while (dirExists) { 960 if (dir1.exists()) { 961 dirNumber++; 962 dir1 = new File(base, String.valueOf(dirNumber)); 963 } else { 964 dirExists = false; 965 } 966 } 967 f = new File(base + dirNumber + slash + dots + slash + dirNumber + slash 968 + "temp.tst"); 969 assertEquals("Test 3: Incorrect path returned.", base + dirNumber + slash 970 + "temp.tst", f.getCanonicalPath()); 971 f = new File(base + dirNumber + slash + "Temp" + slash + dots + slash + "Test" 972 + slash + "temp.tst"); 973 assertEquals("Test 4: Incorrect path returned.", base + dirNumber + slash + "Test" 974 + slash + "temp.tst", f.getCanonicalPath()); 975 976 f = new File("1234.567"); 977 expected = System.getProperty("user.dir") + "/1234.567"; 978 error = String.format("Test 5: Incorrect path %s returned; %s expected.", 979 f.getCanonicalPath(), expected); 980 assertTrue(error, f.getCanonicalPath().equals(expected)); 981 982 } catch (IOException e) { 983 fail("Unexpected IOException During Test : " + e.getMessage()); 984 } finally { 985 System.setProperty("user.dir", oldUserDir); 986 } 987 } 988 989 /** 990 * @tests java.io.File#getName() 991 */ 992 @TestTargetNew( 993 level = TestLevel.COMPLETE, 994 notes = "Verifies getName() method.", 995 method = "getName", 996 args = {} 997 ) 998 public void test_getName() { 999 // Test for method java.lang.String java.io.File.getName() 1000 File f = new File("name.tst"); 1001 assertEquals("Test 1: Returned incorrect name", 1002 "name.tst", f.getName()); 1003 1004 f = new File(""); 1005 assertTrue("Test 2: Returned incorrect name", f.getName().equals("")); 1006 1007 f.delete(); 1008 } 1009 1010 /** 1011 * @tests java.io.File#getParent() 1012 */ 1013 @TestTargetNew( 1014 level = TestLevel.COMPLETE, 1015 notes = "Verifies getParent() method.", 1016 method = "getParent", 1017 args = {} 1018 ) 1019 public void test_getParent() throws Exception { 1020 // Test for method java.lang.String java.io.File.getParent() 1021 String dir = System.getProperty("java.io.tmpdir"); 1022 if (dir == null) 1023 throw new Exception("System property java.io.tmpdir not defined."); 1024 1025 File f = new File("p.tst"); 1026 assertNull("Test 1: Incorrect path returned", f.getParent()); 1027 f = new File(dir, "p.tst"); 1028 assertTrue("Test 2: Incorrect path returned", 1029 f.getParent().equals(dir)); 1030 try { 1031 f.delete(); 1032 } catch (Exception e) { 1033 fail("Unexpected exception during tests : " + e.getMessage()); 1034 } 1035 1036 File f1 = new File("/directory"); 1037 assertTrue("Wrong parent test 1", f1.getParent().equals(slash)); 1038 f1 = new File("/directory/file"); 1039 assertTrue("Wrong parent test 2", f1.getParent().equals( 1040 slash + "directory")); 1041 f1 = new File("directory/file"); 1042 assertEquals("Wrong parent test 3", "directory", f1.getParent()); 1043 f1 = new File("/"); 1044 assertNull("Wrong parent test 4", f1.getParent()); 1045 f1 = new File("directory"); 1046 assertNull("Wrong parent test 5", f1.getParent()); 1047 1048 if (File.separatorChar == '\\' && new File("d:/").isAbsolute()) { 1049 f1 = new File("d:/directory"); 1050 assertTrue("Wrong parent test 1a", f1.getParent().equals( 1051 "d:" + slash)); 1052 f1 = new File("d:/directory/file"); 1053 assertTrue("Wrong parent test 2a", f1.getParent().equals( 1054 "d:" + slash + "directory")); 1055 f1 = new File("d:directory/file"); 1056 assertEquals("Wrong parent test 3a", 1057 "d:directory", f1.getParent()); 1058 f1 = new File("d:/"); 1059 assertNull("Wrong parent test 4a", f1.getParent()); 1060 f1 = new File("d:directory"); 1061 assertEquals("Wrong parent test 5a", "d:", f1.getParent()); 1062 } 1063 } 1064 1065 /** 1066 * @tests java.io.File#getParentFile() 1067 */ 1068 @TestTargetNew( 1069 level = TestLevel.COMPLETE, 1070 notes = "Verifies getParentFile() method.", 1071 method = "getParentFile", 1072 args = {} 1073 ) 1074 public void test_getParentFile() { 1075 // Test for method java.io.File.getParentFile() 1076 File f = new File("tempfile.tst"); 1077 assertNull("Incorrect path returned", f.getParentFile()); 1078 f = new File(System.getProperty("java.io.tmpdir"), "tempfile1.tmp"); 1079 File f2 = new File(System.getProperty("java.io.tmpdir"), "tempfile2.tmp"); 1080 File f3 = new File(System.getProperty("java.io.tmpdir"), "/a/tempfile.tmp"); 1081 assertEquals("Incorrect file returned", 0, f.getParentFile().compareTo( 1082 f2.getParentFile())); 1083 assertTrue("Incorrect file returned", f.getParentFile().compareTo( 1084 f3.getParentFile()) != 0); 1085 f.delete(); 1086 f2.delete(); 1087 f3.delete(); 1088 } 1089 1090 /** 1091 * @tests java.io.File#getPath() 1092 */ 1093 @TestTargetNew( 1094 level = TestLevel.COMPLETE, 1095 notes = "Verifies getPath() method.", 1096 method = "getPath", 1097 args = {} 1098 ) 1099 public void test_getPath() { 1100 String oldUserDir = System.getProperty("java.io.tmpdir"); 1101 System.setProperty("user.dir", System.getProperty("java.io.tmpdir")); 1102 String base = System.getProperty("user.dir"); 1103 String fname; 1104 File f1; 1105 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 1106 base += slash; 1107 fname = base + "filechk.tst"; 1108 f1 = new File(base, "filechk.tst"); 1109 File f2 = new File("filechk.tst"); 1110 File f3 = new File("c:"); 1111 File f4 = new File(base + "a" + slash + slash + ".." + slash 1112 + "filechk.tst"); 1113 assertTrue("getPath returned incorrect path(f1) " + f1.getPath(), f1 1114 .getPath().equals(fname)); 1115 assertTrue("getPath returned incorrect path(f2) " + f2.getPath(), f2 1116 .getPath().equals("filechk.tst")); 1117 assertTrue("getPath returned incorrect path(f3) " + f3.getPath(), f3 1118 .getPath().equals("c:")); 1119 assertTrue("getPath returned incorrect path(f4) " + f4.getPath(), f4 1120 .getPath().equals( 1121 base + "a" + slash + ".." + slash + "filechk.tst")); 1122 f1.delete(); 1123 f2.delete(); 1124 f3.delete(); 1125 f4.delete(); 1126 System.setProperty("user.dir", oldUserDir); 1127 } 1128 1129 /** 1130 * @tests java.io.File#isAbsolute() 1131 */ 1132 @TestTargetNew( 1133 level = TestLevel.COMPLETE, 1134 notes = "Verifies isAbsolute() method.", 1135 method = "isAbsolute", 1136 args = {} 1137 ) 1138 public void test_isAbsolute() { 1139 // Test for method boolean java.io.File.isAbsolute() 1140 if (File.separatorChar == '\\') { 1141 File f = new File("c:\\test"); 1142 File f1 = new File("\\test"); 1143 // One or the other should be absolute on Windows or CE 1144 assertTrue("Absolute returned false", (f.isAbsolute() && !f1 1145 .isAbsolute()) 1146 || (!f.isAbsolute() && f1.isAbsolute())); 1147 } else { 1148 File f = new File("/test"); 1149 assertTrue("Absolute returned false", f.isAbsolute()); 1150 } 1151 assertTrue("Non-Absolute returned true", !new File("../test") 1152 .isAbsolute()); 1153 } 1154 1155 /** 1156 * @tests java.io.File#isDirectory() 1157 */ 1158 @TestTargetNew( 1159 level = TestLevel.PARTIAL_COMPLETE, 1160 method = "isDirectory", 1161 args = {} 1162 ) 1163 public void test_isDirectory() { 1164 // Test for method boolean java.io.File.isDirectory() 1165 1166 String base = System.getProperty("java.io.tmpdir"); 1167 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 1168 base += slash; 1169 File f = new File(base); 1170 assertTrue("Test 1: Directory returned false", f.isDirectory()); 1171 f = new File(base + "zxzxzxz" + platformId); 1172 assertTrue("Test 2: (Not created) directory returned true.", !f 1173 .isDirectory()); 1174 f.mkdir(); 1175 try { 1176 assertTrue("Test 3: Directory returned false.", f.isDirectory()); 1177 } finally { 1178 f.delete(); 1179 } 1180 } 1181 1182 /** 1183 * @tests java.io.File#isFile() 1184 */ 1185 @TestTargetNew( 1186 level = TestLevel.PARTIAL_COMPLETE, 1187 method = "isFile", 1188 args = {} 1189 ) 1190 public void test_isFile() { 1191 // Test for method boolean java.io.File.isFile() 1192 try { 1193 String base = System.getProperty("java.io.tmpdir"); 1194 File f = new File(base); 1195 assertTrue("Directory returned true as being a file.", !f.isFile()); 1196 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 1197 base += slash; 1198 f = new File(base, platformId + "amiafile"); 1199 assertTrue("Non-existent file returned true", !f.isFile()); 1200 FileOutputStream fos = new FileOutputStream(f); 1201 fos.close(); 1202 assertTrue("File returned false", f.isFile()); 1203 f.delete(); 1204 } catch (IOException e) { 1205 fail("IOException during isFile " + e.getMessage()); 1206 } 1207 } 1208 1209 /** 1210 * @tests java.io.File#isHidden() 1211 */ 1212 @TestTargetNew( 1213 level = TestLevel.PARTIAL_COMPLETE, 1214 method = "isHidden", 1215 args = {} 1216 ) 1217 public void test_isHidden() { 1218 // Test for method boolean java.io.File.isHidden() 1219 boolean onUnix = File.separatorChar == '/'; 1220 1221 try { 1222 File f = File.createTempFile("hyts_", ".tmp"); 1223 // On Unix hidden files are marked with a "." at the beginning 1224 // of the file name. 1225 if (onUnix) { 1226 File f2 = new File(System.getProperty("java.io.tmpdir"), ".test.tst" + platformId); 1227 FileOutputStream fos2 = new FileOutputStream(f2); 1228 fos2.close(); 1229 assertTrue("File returned hidden on Unix", !f.isHidden()); 1230 assertTrue("File returned visible on Unix", f2.isHidden()); 1231 assertTrue("File did not delete.", f2.delete()); 1232 } else { 1233 // For windows, the file is being set hidden by the attrib 1234 // command. 1235 Runtime r = Runtime.getRuntime(); 1236 assertTrue("File returned hidden", !f.isHidden()); 1237 Process p = r.exec("attrib +h \"" + f.getAbsolutePath() + "\""); 1238 p.waitFor(); 1239 assertTrue("File returned visible", f.isHidden()); 1240 p = r.exec("attrib -h \"" + f.getAbsolutePath() + "\""); 1241 p.waitFor(); 1242 assertTrue("File returned hidden", !f.isHidden()); 1243 } 1244 f.delete(); 1245 } catch (IOException e) { 1246 fail("Unexpected IOException during test : " + e.getMessage()); 1247 } catch (InterruptedException e) { 1248 fail("Unexpected InterruptedException during test : " 1249 + e.getMessage()); 1250 } 1251 } 1252 1253 /** 1254 * @tests java.io.File#lastModified() 1255 */ 1256 @TestTargetNew( 1257 level = TestLevel.PARTIAL_COMPLETE, 1258 method = "lastModified", 1259 args = {} 1260 ) 1261 public void test_lastModified() { 1262 // Test for method long java.io.File.lastModified() 1263 try { 1264 File f = new File(System.getProperty("java.io.tmpdir"), platformId 1265 + "lModTest.tst"); 1266 f.delete(); 1267 long lastModifiedTime = f.lastModified(); 1268 assertEquals("LastModified Time Should Have Returned 0.", 1269 0, lastModifiedTime); 1270 FileOutputStream fos = new FileOutputStream(f); 1271 fos.close(); 1272 f.setLastModified(315550800000L); 1273 lastModifiedTime = f.lastModified(); 1274 assertTrue("LastModified Time Incorrect: " + lastModifiedTime, 1275 lastModifiedTime == 315550800000L); 1276 f.delete(); 1277 1278 // Regression for Harmony-2146 1279 f = new File("/../"); 1280 assertTrue(f.lastModified() > 0); 1281 } catch (IOException e) { 1282 fail("Unexpected IOException during test : " + e.getMessage()); 1283 } 1284 } 1285 1286 /** 1287 * @tests java.io.File#length() 1288 */ 1289 @TestTargetNew( 1290 level = TestLevel.PARTIAL_COMPLETE, 1291 method = "length", 1292 args = {} 1293 ) 1294 public void test_length() throws Exception { 1295 // Test for method long java.io.File.length() 1296 try { 1297 File f = new File(System.getProperty("java.io.tmpdir"), platformId 1298 + "input.tst"); 1299 assertEquals("File length should have returned 0.", 0, f.length()); 1300 FileOutputStream fos = new FileOutputStream(f); 1301 fos.write(fileString.getBytes()); 1302 fos.close(); 1303 assertTrue("Incorrect file length returned: " + f.length(), f 1304 .length() == fileString.length()); 1305 f.delete(); 1306 } catch (IOException e) { 1307 fail("Unexpected IOException during test : " + e.getMessage()); 1308 } 1309 1310 // regression test for Harmony-1497 1311 File f = File.createTempFile("cts", ".tmp"); 1312 f.deleteOnExit(); 1313 RandomAccessFile raf = new RandomAccessFile(f, "rwd"); 1314 raf.write(0x41); 1315 assertEquals(1, f.length()); 1316 } 1317 1318 /** 1319 * @tests java.io.File#list() 1320 */ 1321 @TestTargetNew( 1322 level = TestLevel.PARTIAL_COMPLETE, 1323 method = "list", 1324 args = {} 1325 ) 1326 public void test_list() { 1327 // Test for method java.lang.String [] java.io.File.list() 1328 1329 String base = System.getProperty("java.io.tmpdir"); 1330 // Old test left behind "garbage files" so this time it creates a 1331 // directory 1332 // that is guaranteed not to already exist (and deletes it afterward.) 1333 int dirNumber = 1; 1334 boolean dirExists = true; 1335 File dir = null; 1336 dir = new File(base, platformId + String.valueOf(dirNumber)); 1337 while (dirExists) { 1338 if (dir.exists()) { 1339 dirNumber++; 1340 dir = new File(base, String.valueOf(dirNumber)); 1341 } else { 1342 dirExists = false; 1343 } 1344 } 1345 1346 String[] flist = dir.list(); 1347 1348 assertNull("Method list() Should Have Returned null.", flist); 1349 1350 assertTrue("Could not create parent directory for list test", dir 1351 .mkdir()); 1352 1353 String[] files = { "mtzz1.xx", "mtzz2.xx", "mtzz3.yy", "mtzz4.yy" }; 1354 try { 1355 assertEquals("Method list() Should Have Returned An Array Of Length 0.", 1356 0, dir.list().length); 1357 1358 File file = new File(dir, "notADir.tst"); 1359 try { 1360 FileOutputStream fos = new FileOutputStream(file); 1361 fos.close(); 1362 assertNull( 1363 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1364 file.list()); 1365 } catch (IOException e) { 1366 fail("Unexpected IOException during test : " + e.getMessage()); 1367 } finally { 1368 file.delete(); 1369 } 1370 1371 try { 1372 for (int i = 0; i < files.length; i++) { 1373 File f = new File(dir, files[i]); 1374 FileOutputStream fos = new FileOutputStream(f); 1375 fos.close(); 1376 } 1377 } catch (IOException e) { 1378 fail("Unexpected IOException during test : " + e.getMessage()); 1379 } 1380 1381 flist = dir.list(); 1382 if (flist.length != files.length) { 1383 fail("Incorrect list returned"); 1384 } 1385 1386 // Checking to make sure the correct files were are listed in the 1387 // array. 1388 boolean[] check = new boolean[flist.length]; 1389 for (int i = 0; i < check.length; i++) 1390 check[i] = false; 1391 for (int i = 0; i < files.length; i++) { 1392 for (int j = 0; j < flist.length; j++) { 1393 if (flist[j].equals(files[i])) { 1394 check[i] = true; 1395 break; 1396 } 1397 } 1398 } 1399 int checkCount = 0; 1400 for (int i = 0; i < check.length; i++) { 1401 if (check[i] == false) 1402 checkCount++; 1403 } 1404 assertEquals("Invalid file returned in listing", 0, checkCount); 1405 1406 for (int i = 0; i < files.length; i++) { 1407 File f = new File(dir, files[i]); 1408 f.delete(); 1409 } 1410 1411 assertTrue("Could not delete parent directory for list test.", dir 1412 .delete()); 1413 } finally { 1414 for (int i = 0; i < files.length; i++) { 1415 File f = new File(dir, files[i]); 1416 f.delete(); 1417 } 1418 dir.delete(); 1419 } 1420 1421 } 1422 1423 /** 1424 * @tests java.io.File#listFiles() 1425 */ 1426 @TestTargetNew( 1427 level = TestLevel.PARTIAL_COMPLETE, 1428 method = "listFiles", 1429 args = {} 1430 ) 1431 public void test_listFiles() { 1432 // Test for method java.io.File.listFiles() 1433 1434 try { 1435 String base = System.getProperty("java.io.tmpdir"); 1436 // Finding a non-existent directory to create. 1437 int dirNumber = 1; 1438 boolean dirExists = true; 1439 File dir = new File(base, platformId + String.valueOf(dirNumber)); 1440 // Making sure that the directory does not exist. 1441 while (dirExists) { 1442 // If the directory exists, add one to the directory number 1443 // (making 1444 // it a new directory name.) 1445 if (dir.exists()) { 1446 dirNumber++; 1447 dir = new File(base, String.valueOf(dirNumber)); 1448 } else { 1449 dirExists = false; 1450 } 1451 } 1452 // Test for attempting to cal listFiles on a non-existent directory. 1453 assertNull("listFiles Should Return Null.", dir.listFiles()); 1454 1455 assertTrue("Failed To Create Parent Directory.", dir.mkdir()); 1456 1457 String[] files = { "1.tst", "2.tst", "3.tst", "" }; 1458 try { 1459 assertEquals("listFiles Should Return An Array Of Length 0.", 0, dir 1460 .listFiles().length); 1461 1462 File file = new File(dir, "notADir.tst"); 1463 try { 1464 FileOutputStream fos = new FileOutputStream(file); 1465 fos.close(); 1466 assertNull( 1467 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1468 file.listFiles()); 1469 } catch (IOException e) { 1470 fail("Unexpected IOException during test : " + e.getMessage()); 1471 } finally { 1472 file.delete(); 1473 } 1474 1475 for (int i = 0; i < (files.length - 1); i++) { 1476 File f = new File(dir, files[i]); 1477 FileOutputStream fos = new FileOutputStream(f); 1478 fos.close(); 1479 } 1480 1481 new File(dir, "doesNotExist.tst"); 1482 File[] flist = dir.listFiles(); 1483 1484 // Test to make sure that only the 3 files that were created are 1485 // listed. 1486 assertEquals("Incorrect Number Of Files Returned.", 1487 3, flist.length); 1488 1489 // Test to make sure that listFiles can read hidden files. 1490 boolean onUnix = File.separatorChar == '/'; 1491 boolean onWindows = File.separatorChar == '\\'; 1492 if (onWindows) { 1493 files[3] = "4.tst"; 1494 File f = new File(dir, "4.tst"); 1495 FileOutputStream fos = new FileOutputStream(f); 1496 fos.close(); 1497 Runtime r = Runtime.getRuntime(); 1498 Process p = r.exec("attrib +h \"" + f.getPath() + "\""); 1499 p.waitFor(); 1500 } 1501 if (onUnix) { 1502 files[3] = ".4.tst"; 1503 File f = new File(dir, ".4.tst"); 1504 FileOutputStream fos = new FileOutputStream(f); 1505 fos.close(); 1506 } 1507 flist = dir.listFiles(); 1508 assertEquals("Incorrect Number Of Files Returned.", 1509 4, flist.length); 1510 1511 // Checking to make sure the correct files were are listed in 1512 // the array. 1513 boolean[] check = new boolean[flist.length]; 1514 for (int i = 0; i < check.length; i++) 1515 check[i] = false; 1516 for (int i = 0; i < files.length; i++) { 1517 for (int j = 0; j < flist.length; j++) { 1518 if (flist[j].getName().equals(files[i])) { 1519 check[i] = true; 1520 break; 1521 } 1522 } 1523 } 1524 int checkCount = 0; 1525 for (int i = 0; i < check.length; i++) { 1526 if (check[i] == false) 1527 checkCount++; 1528 } 1529 assertEquals("Invalid file returned in listing", 0, checkCount); 1530 1531 if (onWindows) { 1532 Runtime r = Runtime.getRuntime(); 1533 Process p = r.exec("attrib -h \"" 1534 + new File(dir, files[3]).getPath() + "\""); 1535 p.waitFor(); 1536 } 1537 1538 for (int i = 0; i < files.length; i++) { 1539 File f = new File(dir, files[i]); 1540 f.delete(); 1541 } 1542 assertTrue("Parent Directory Not Deleted.", dir.delete()); 1543 } finally { 1544 for (int i = 0; i < files.length; i++) { 1545 File f = new File(dir, files[i]); 1546 f.delete(); 1547 } 1548 dir.delete(); 1549 } 1550 } catch (IOException e) { 1551 fail("Unexpected IOException during test : " + e.getMessage()); 1552 } catch (InterruptedException e) { 1553 fail("Unexpected InterruptedException during test : " + e.getMessage()); 1554 } 1555 } 1556 1557 /** 1558 * @tests java.io.File#listFiles(java.io.FileFilter) 1559 */ 1560 @TestTargetNew( 1561 level = TestLevel.PARTIAL_COMPLETE, 1562 method = "listFiles", 1563 args = {java.io.FileFilter.class} 1564 ) 1565 public void test_listFilesLjava_io_FileFilter() { 1566 // Test for method java.io.File.listFiles(File Filter filter) 1567 1568 String base = System.getProperty("java.io.tmpdir"); 1569 // Finding a non-existent directory to create. 1570 int dirNumber = 1; 1571 boolean dirExists = true; 1572 File baseDir = new File(base, platformId + String.valueOf(dirNumber)); 1573 // Making sure that the directory does not exist. 1574 while (dirExists) { 1575 // If the directory exists, add one to the directory number (making 1576 // it a new directory name.) 1577 if (baseDir.exists()) { 1578 dirNumber++; 1579 baseDir = new File(base, String.valueOf(dirNumber)); 1580 } else { 1581 dirExists = false; 1582 } 1583 } 1584 1585 // Creating a filter that catches directories. 1586 FileFilter dirFilter = new FileFilter() { 1587 public boolean accept(File f) { 1588 if (f.isDirectory()) 1589 return true; 1590 else 1591 return false; 1592 } 1593 }; 1594 1595 assertNull("listFiles Should Return Null.", baseDir 1596 .listFiles(dirFilter)); 1597 1598 assertTrue("Failed To Create Parent Directory.", baseDir.mkdir()); 1599 1600 File dir1 = null; 1601 String[] files = { "1.tst", "2.tst", "3.tst" }; 1602 try { 1603 assertEquals("listFiles Should Return An Array Of Length 0.", 0, baseDir 1604 .listFiles(dirFilter).length); 1605 1606 File file = new File(baseDir, "notADir.tst"); 1607 try { 1608 FileOutputStream fos = new FileOutputStream(file); 1609 fos.close(); 1610 assertNull( 1611 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1612 file.listFiles(dirFilter)); 1613 } catch (IOException e) { 1614 fail("Unexpected IOException During Test."); 1615 } finally { 1616 file.delete(); 1617 } 1618 1619 try { 1620 for (int i = 0; i < files.length; i++) { 1621 File f = new File(baseDir, files[i]); 1622 FileOutputStream fos = new FileOutputStream(f); 1623 fos.close(); 1624 } 1625 } catch (IOException e) { 1626 fail("Unexpected IOException during test : " + e.getMessage()); 1627 } 1628 dir1 = new File(baseDir, "Temp1"); 1629 dir1.mkdir(); 1630 1631 // Creating a filter that catches files. 1632 FileFilter fileFilter = new FileFilter() { 1633 public boolean accept(File f) { 1634 if (f.isFile()) 1635 return true; 1636 else 1637 return false; 1638 } 1639 }; 1640 1641 // Test to see if the correct number of directories are returned. 1642 File[] directories = baseDir.listFiles(dirFilter); 1643 assertEquals("Incorrect Number Of Directories Returned.", 1644 1, directories.length); 1645 1646 // Test to see if the directory was saved with the correct name. 1647 assertEquals("Incorrect Directory Returned.", 0, directories[0] 1648 .compareTo(dir1)); 1649 1650 // Test to see if the correct number of files are returned. 1651 File[] flist = baseDir.listFiles(fileFilter); 1652 assertTrue("Incorrect Number Of Files Returned.", 1653 flist.length == files.length); 1654 1655 // Checking to make sure the correct files were are listed in the 1656 // array. 1657 boolean[] check = new boolean[flist.length]; 1658 for (int i = 0; i < check.length; i++) 1659 check[i] = false; 1660 for (int i = 0; i < files.length; i++) { 1661 for (int j = 0; j < flist.length; j++) { 1662 if (flist[j].getName().equals(files[i])) { 1663 check[i] = true; 1664 break; 1665 } 1666 } 1667 } 1668 int checkCount = 0; 1669 for (int i = 0; i < check.length; i++) { 1670 if (check[i] == false) 1671 checkCount++; 1672 } 1673 assertEquals("Invalid file returned in listing", 0, checkCount); 1674 1675 for (int i = 0; i < files.length; i++) { 1676 File f = new File(baseDir, files[i]); 1677 f.delete(); 1678 } 1679 dir1.delete(); 1680 assertTrue("Parent Directory Not Deleted.", baseDir.delete()); 1681 } finally { 1682 for (int i = 0; i < files.length; i++) { 1683 File f = new File(baseDir, files[i]); 1684 f.delete(); 1685 } 1686 if (dir1 != null) 1687 dir1.delete(); 1688 baseDir.delete(); 1689 } 1690 } 1691 1692 /** 1693 * @tests java.io.File#listFiles(java.io.FilenameFilter) 1694 */ 1695 @TestTargetNew( 1696 level = TestLevel.PARTIAL_COMPLETE, 1697 method = "listFiles", 1698 args = {java.io.FilenameFilter.class} 1699 ) 1700 public void test_listFilesLjava_io_FilenameFilter() { 1701 // Test for method java.io.File.listFiles(FilenameFilter filter) 1702 1703 String base = System.getProperty("java.io.tmpdir"); 1704 // Finding a non-existent directory to create. 1705 int dirNumber = 1; 1706 boolean dirExists = true; 1707 File dir = new File(base, platformId + String.valueOf(dirNumber)); 1708 // Making sure that the directory does not exist. 1709 while (dirExists) { 1710 // If the directory exists, add one to the directory number (making 1711 // it a new directory name.) 1712 if (dir.exists()) { 1713 dirNumber++; 1714 dir = new File(base, platformId + String.valueOf(dirNumber)); 1715 } else { 1716 dirExists = false; 1717 } 1718 } 1719 1720 // Creating a filter that catches "*.tst" files. 1721 FilenameFilter tstFilter = new FilenameFilter() { 1722 public boolean accept(File f, String fileName) { 1723 // If the suffix is ".tst" then send it to the array 1724 if (fileName.endsWith(".tst")) 1725 return true; 1726 else 1727 return false; 1728 } 1729 }; 1730 1731 assertNull("listFiles Should Return Null.", 1732 dir.listFiles(tstFilter)); 1733 1734 assertTrue("Failed To Create Parent Directory.", dir.mkdir()); 1735 1736 String[] files = { "1.tst", "2.tst", "3.tmp" }; 1737 try { 1738 assertEquals("listFiles Should Return An Array Of Length 0.", 0, dir 1739 .listFiles(tstFilter).length); 1740 1741 File file = new File(dir, "notADir.tst"); 1742 try { 1743 FileOutputStream fos = new FileOutputStream(file); 1744 fos.close(); 1745 assertNull( 1746 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1747 file.listFiles(tstFilter)); 1748 } catch (IOException e) { 1749 fail("Unexpected IOException during test : " + e.getMessage()); 1750 } finally { 1751 file.delete(); 1752 } 1753 1754 try { 1755 for (int i = 0; i < files.length; i++) { 1756 File f = new File(dir, files[i]); 1757 FileOutputStream fos = new FileOutputStream(f); 1758 fos.close(); 1759 } 1760 } catch (IOException e) { 1761 fail("Unexpected IOException During Test : " + e.getMessage()); 1762 } 1763 1764 // Creating a filter that catches "*.tmp" files. 1765 FilenameFilter tmpFilter = new FilenameFilter() { 1766 public boolean accept(File f, String fileName) { 1767 // If the suffix is ".tmp" then send it to the array 1768 if (fileName.endsWith(".tmp")) 1769 return true; 1770 else 1771 return false; 1772 } 1773 }; 1774 1775 // Tests to see if the correct number of files were returned. 1776 File[] flist = dir.listFiles(tstFilter); 1777 assertEquals("Incorrect Number Of Files Passed Through tstFilter.", 1778 2, flist.length); 1779 for (int i = 0; i < flist.length; i++) 1780 assertTrue("File Should Not Have Passed The tstFilter.", 1781 flist[i].getPath().endsWith(".tst")); 1782 1783 flist = dir.listFiles(tmpFilter); 1784 assertEquals("Incorrect Number Of Files Passed Through tmpFilter.", 1785 1, flist.length); 1786 assertTrue("File Should Not Have Passed The tmpFilter.", flist[0] 1787 .getPath().endsWith(".tmp")); 1788 1789 for (int i = 0; i < files.length; i++) { 1790 File f = new File(dir, files[i]); 1791 f.delete(); 1792 } 1793 assertTrue("Parent Directory Not Deleted.", dir.delete()); 1794 } finally { 1795 for (int i = 0; i < files.length; i++) { 1796 File f = new File(dir, files[i]); 1797 f.delete(); 1798 } 1799 dir.delete(); 1800 } 1801 } 1802 1803 /** 1804 * @tests java.io.File#list(java.io.FilenameFilter) 1805 */ 1806 @TestTargetNew( 1807 level = TestLevel.PARTIAL_COMPLETE, 1808 method = "list", 1809 args = {java.io.FilenameFilter.class} 1810 ) 1811 public void test_listLjava_io_FilenameFilter() { 1812 // Test for method java.lang.String [] 1813 // java.io.File.list(java.io.FilenameFilter) 1814 1815 String base = System.getProperty("java.io.tmpdir"); 1816 // Old test left behind "garbage files" so this time it creates a 1817 // directory 1818 // that is guaranteed not to already exist (and deletes it afterward.) 1819 int dirNumber = 1; 1820 boolean dirExists = true; 1821 File dir = new File(base, platformId + String.valueOf(dirNumber)); 1822 while (dirExists) { 1823 if (dir.exists()) { 1824 dirNumber++; 1825 dir = new File(base, String.valueOf(dirNumber)); 1826 } else { 1827 dirExists = false; 1828 } 1829 } 1830 1831 FilenameFilter filter = new FilenameFilter() { 1832 public boolean accept(File dir, String name) { 1833 return !name.equals("mtzz1.xx"); 1834 } 1835 }; 1836 1837 String[] flist = dir.list(filter); 1838 assertNull("Method list(FilenameFilter) Should Have Returned Null.", 1839 flist); 1840 1841 assertTrue("Could not create parent directory for test", dir.mkdir()); 1842 1843 String[] files = { "mtzz1.xx", "mtzz2.xx", "mtzz3.yy", "mtzz4.yy" }; 1844 try { 1845 /* 1846 * Do not return null when trying to use list(Filename Filter) on a 1847 * file rather than a directory. All other "list" methods return 1848 * null for this test case. 1849 */ 1850 /* 1851 * File file = new File(dir, "notADir.tst"); try { FileOutputStream 1852 * fos = new FileOutputStream(file); fos.close(); } catch 1853 * (IOException e) { fail("Unexpected IOException During 1854 * Test."); } flist = dir.list(filter); assertNull("listFiles 1855 * Should Have Returned Null When Used On A File Instead Of A 1856 * Directory.", flist); file.delete(); 1857 */ 1858 1859 flist = dir.list(filter); 1860 assertEquals("Array Of Length 0 Should Have Returned.", 1861 0, flist.length); 1862 1863 try { 1864 for (int i = 0; i < files.length; i++) { 1865 File f = new File(dir, files[i]); 1866 FileOutputStream fos = new FileOutputStream(f); 1867 fos.close(); 1868 } 1869 } catch (IOException e) { 1870 fail("Unexpected IOException during test : " + e.getMessage()); 1871 } 1872 1873 flist = dir.list(filter); 1874 1875 if (flist.length != files.length - 1) { 1876 fail("Incorrect list returned"); 1877 } 1878 1879 // Checking to make sure the correct files were are listed in the 1880 // array. 1881 boolean[] check = new boolean[flist.length]; 1882 for (int i = 0; i < check.length; i++) 1883 check[i] = false; 1884 String[] wantedFiles = { "mtzz2.xx", "mtzz3.yy", "mtzz4.yy" }; 1885 for (int i = 0; i < wantedFiles.length; i++) { 1886 for (int j = 0; j < flist.length; j++) { 1887 if (flist[j].equals(wantedFiles[i])) { 1888 check[i] = true; 1889 break; 1890 } 1891 } 1892 } 1893 int checkCount = 0; 1894 for (int i = 0; i < check.length; i++) { 1895 if (check[i] == false) 1896 checkCount++; 1897 } 1898 assertEquals("Invalid file returned in listing", 0, checkCount); 1899 1900 for (int i = 0; i < files.length; i++) { 1901 File f = new File(dir, files[i]); 1902 f.delete(); 1903 } 1904 assertTrue("Could not delete parent directory for test.", dir 1905 .delete()); 1906 } finally { 1907 for (int i = 0; i < files.length; i++) { 1908 File f = new File(dir, files[i]); 1909 f.delete(); 1910 } 1911 dir.delete(); 1912 } 1913 } 1914 1915 /** 1916 * @tests java.io.File#listRoots() 1917 */ 1918 @TestTargetNew( 1919 level = TestLevel.COMPLETE, 1920 notes = "Verifies listRoots() method.", 1921 method = "listRoots", 1922 args = {} 1923 ) 1924 public void test_listRoots() { 1925 // Test for method java.io.File.listRoots() 1926 1927 File[] roots = File.listRoots(); 1928 boolean onUnix = File.separatorChar == '/'; 1929 boolean onWindows = File.separatorChar == '\\'; 1930 if (onUnix) { 1931 assertEquals("Incorrect Number Of Root Directories.", 1932 1, roots.length); 1933 String fileLoc = roots[0].getPath(); 1934 assertTrue("Incorrect Root Directory Returned.", fileLoc 1935 .startsWith(slash)); 1936 } else if (onWindows) { 1937 // Need better test for Windows 1938 assertTrue("Incorrect Number Of Root Directories.", 1939 roots.length > 0); 1940 } 1941 } 1942 1943 /** 1944 * @tests java.io.File#mkdir() 1945 */ 1946 @TestTargetNew( 1947 level = TestLevel.PARTIAL_COMPLETE, 1948 method = "mkdir", 1949 args = {} 1950 ) 1951 public void test_mkdir() throws IOException { 1952 // Test for method boolean java.io.File.mkdir() 1953 1954 String base = System.getProperty("java.io.tmpdir"); 1955 // Old test left behind "garbage files" so this time it creates a 1956 // directory 1957 // that is guaranteed not to already exist (and deletes it afterward.) 1958 int dirNumber = 1; 1959 boolean dirExists = true; 1960 File dir = new File(base, String.valueOf(dirNumber)); 1961 while (dirExists) { 1962 if (dir.exists()) { 1963 dirNumber++; 1964 dir = new File(base, String.valueOf(dirNumber)); 1965 } else { 1966 dirExists = false; 1967 } 1968 } 1969 1970 assertTrue("mkdir failed", dir.mkdir() && dir.exists()); 1971 dir.deleteOnExit(); 1972 1973 String longDirName = "abcdefghijklmnopqrstuvwx";// 24 chars 1974 StringBuilder sb = new StringBuilder(dir + File.separator); 1975 StringBuilder sb2 = new StringBuilder(dir + File.separator); 1976 1977 // Test make a long path 1978 while (dir.getCanonicalPath().length() < 256 - longDirName.length()) { 1979 sb.append(longDirName + File.separator); 1980 dir = new File(sb.toString()); 1981 assertTrue("mkdir failed", dir.mkdir() && dir.exists()); 1982 dir.deleteOnExit(); 1983 } 1984 1985 while (dir.getCanonicalPath().length() < 256) { 1986 sb.append(0); 1987 dir = new File(sb.toString()); 1988 assertTrue("mkdir " + dir.getCanonicalPath().length() + " failed", 1989 dir.mkdir() && dir.exists()); 1990 dir.deleteOnExit(); 1991 } 1992 1993 // Test make many paths 1994 while (dir.getCanonicalPath().length() < 256) { 1995 sb2.append(0); 1996 dir = new File(sb2.toString()); 1997 assertTrue("mkdir " + dir.getCanonicalPath().length() + " failed", 1998 dir.mkdir() && dir.exists()); 1999 dir.deleteOnExit(); 2000 } 2001 } 2002 2003 /** 2004 * @tests java.io.File#mkdirs() 2005 */ 2006 @TestTargetNew( 2007 level = TestLevel.PARTIAL_COMPLETE, 2008 method = "mkdirs", 2009 args = {} 2010 ) 2011 public void test_mkdirs() { 2012 // Test for method boolean java.io.File.mkdirs() 2013 2014 String userHome = System.getProperty("java.io.tmpdir"); 2015 if (!userHome.endsWith(slash)) 2016 userHome += slash; 2017 File f = new File(userHome + "mdtest" + platformId + slash + "mdtest2", 2018 "p.tst"); 2019 File g = new File(userHome + "mdtest" + platformId + slash + "mdtest2"); 2020 File h = new File(userHome + "mdtest" + platformId); 2021 f.mkdirs(); 2022 try { 2023 assertTrue("Base Directory not created", h.exists()); 2024 assertTrue("Directories not created", g.exists()); 2025 assertTrue("File not created", f.exists()); 2026 } finally { 2027 f.delete(); 2028 g.delete(); 2029 h.delete(); 2030 } 2031 } 2032 2033 /** 2034 * @tests java.io.File#renameTo(java.io.File) 2035 */ 2036 @TestTargetNew( 2037 level = TestLevel.PARTIAL_COMPLETE, 2038 method = "renameTo", 2039 args = {java.io.File.class} 2040 ) 2041 public void test_renameToLjava_io_File() { 2042 // Test for method boolean java.io.File.renameTo(java.io.File) 2043 String base = System.getProperty("java.io.tmpdir"); 2044 File dir = new File(base, platformId); 2045 dir.mkdir(); 2046 File f = new File(dir, "xxx.xxx"); 2047 File rfile = new File(dir, "yyy.yyy"); 2048 File f2 = new File(dir, "zzz.zzz"); 2049 try { 2050 FileOutputStream fos = new FileOutputStream(f); 2051 fos.write(fileString.getBytes()); 2052 fos.close(); 2053 long lengthOfFile = f.length(); 2054 2055 rfile.delete(); // in case it already exists 2056 2057 try { 2058 f.renameTo(null); 2059 fail("Test 1: NullPointerException expected."); 2060 } catch (NullPointerException e) { 2061 // Expected. 2062 } 2063 2064 assertTrue("Test 2: File rename failed.", f.renameTo(rfile)); 2065 assertTrue("Test 3: File rename failed.", rfile.exists()); 2066 assertTrue("Test 4: Size Of File Changed.", 2067 rfile.length() == lengthOfFile); 2068 2069 fos = new FileOutputStream(rfile); 2070 fos.close(); 2071 2072 f2.delete(); // in case it already exists 2073 assertTrue("Test 5: File rename failed.", rfile.renameTo(f2)); 2074 assertTrue("Test 6: File rename failed.", f2.exists()); 2075 } catch (IOException e) { 2076 fail("Unexpected IOException during test : " + e.getMessage()); 2077 } finally { 2078 f.delete(); 2079 rfile.delete(); 2080 f2.delete(); 2081 dir.delete(); 2082 } 2083 } 2084 2085 /** 2086 * @tests java.io.File#setLastModified(long) 2087 */ 2088 @TestTargetNew( 2089 level = TestLevel.PARTIAL_COMPLETE, 2090 method = "setLastModified", 2091 args = {long.class} 2092 ) 2093 public void test_setLastModifiedJ() { 2094 // Test for method java.io.File.setLastModified() 2095 File f1 = null; 2096 try { 2097 // f1 = File.createTempFile("hyts_tf" , ".tmp"); 2098 // jclRM does not include File.createTempFile 2099 f1 = new File(System.getProperty("java.io.tmpdir"), 2100 platformId + "hyts_tf_slm.tst"); 2101 f1.createNewFile(); 2102 long orgTime = f1.lastModified(); 2103 // Subtracting 100 000 milliseconds from the orgTime of File f1 2104 assertTrue(f1.setLastModified(orgTime - 100000)); 2105 long lastModified = f1.lastModified(); 2106 assertTrue("Test 1: LastModifed time incorrect: " + lastModified, 2107 lastModified == (orgTime - 100000)); 2108 // Subtracting 10 000 000 milliseconds from the orgTime of File f1 2109 assertTrue(f1.setLastModified(orgTime - 10000000)); 2110 lastModified = f1.lastModified(); 2111 assertTrue("Test 2: LastModifed time incorrect: " + lastModified, 2112 lastModified == (orgTime - 10000000)); 2113 // Adding 100 000 milliseconds to the orgTime of File f1 2114 assertTrue(f1.setLastModified(orgTime + 100000)); 2115 lastModified = f1.lastModified(); 2116 assertTrue("Test 3: LastModifed time incorrect: " + lastModified, 2117 lastModified == (orgTime + 100000)); 2118 // Adding 10 000 000 milliseconds from the orgTime of File f1 2119 assertTrue(f1.setLastModified(orgTime + 10000000)); 2120 lastModified = f1.lastModified(); 2121 assertTrue("Test 4: LastModifed time incorrect: " + lastModified, 2122 lastModified == (orgTime + 10000000)); 2123 // Trying to set time to an exact number 2124 assertTrue(f1.setLastModified(315550800000L)); 2125 lastModified = f1.lastModified(); 2126 assertTrue("Test 5: LastModified time incorrect: " + lastModified, 2127 lastModified == 315550800000L); 2128 String osName = System.getProperty("os.name", "unknown"); 2129 if (osName.equals("Windows 2000") || osName.equals("Windows NT")) { 2130 // Trying to set time to a large exact number 2131 boolean result = f1.setLastModified(4354837199000L); 2132 long next = f1.lastModified(); 2133 // Dec 31 23:59:59 EST 2107 is overflow on FAT file systems, and 2134 // the call fails 2135 assertTrue("Test 6: LastModified time incorrect: " + next, 2136 !result || next == 4354837199000L); 2137 } 2138 // Trying to set time to a negative number 2139 try { 2140 f1.setLastModified(-25); 2141 fail("IllegalArgumentException not thrown."); 2142 } catch (IllegalArgumentException e) { 2143 } 2144 2145 File f2 = new File("/does not exist.txt"); 2146 assertFalse(f2.setLastModified(42)); 2147 } catch (IOException e) { 2148 fail("Unexpected IOException during test : " + e.getMessage()); 2149 } finally { 2150 if (f1 != null) 2151 f1.delete(); 2152 } 2153 } 2154 2155 /** 2156 * @tests java.io.File#setReadOnly() 2157 */ 2158 @TestTargets({ 2159 @TestTargetNew( 2160 level = TestLevel.PARTIAL_COMPLETE, 2161 method = "setReadOnly", 2162 args = {} 2163 ), 2164 @TestTargetNew( 2165 level = TestLevel.PARTIAL_COMPLETE, 2166 method = "canWrite", 2167 args = {} 2168 ) 2169 }) 2170 @KnownFailure("canWrite() returns true even when a file is marked " + 2171 "read-only (Test 2). It is also possible to open this file " + 2172 "for writing (Test 3).") 2173 public void test_setReadOnly() { 2174 // Test for method java.io.File.setReadOnly() 2175 2176 File f1 = null; 2177 File f2 = null; 2178 Runtime r = Runtime.getRuntime(); 2179 Process p; 2180 try { 2181 f1 = File.createTempFile("hyts_tf", ".tmp"); 2182 f2 = File.createTempFile("hyts_tf", ".tmp"); 2183 2184 assertTrue("Test 1: File is read-only." , f1.canWrite()); 2185 f1.setReadOnly(); 2186 assertTrue("Test 2: File is not read-only." , !f1.canWrite()); 2187 2188 try { 2189 // Attempt to write to a file that is read-only. 2190 new FileOutputStream(f1); 2191 fail("Test 3: IOException expected."); 2192 } catch (IOException e) { 2193 // Expected. 2194 } 2195 p = r.exec("chmod +w " + f1.getAbsolutePath()); 2196 p.waitFor(); 2197 2198 assertTrue("Test 4: File is read-only." , f1.canWrite()); 2199 try { 2200 FileOutputStream fos = new FileOutputStream(f1); 2201 fos.write(fileString.getBytes()); 2202 fos.close(); 2203 assertTrue("Test 5: Unable to write to the file.", 2204 f1.length() == fileString.length()); 2205 } catch (IOException e) { 2206 fail("Test 6: Unexpected IOException while attempting to " + 2207 "write to the file. " + e); 2208 } 2209 assertTrue("Test 7: File has not been deleted.", f1.delete()); 2210 2211 // Assert is flawed because canWrite does not work. 2212 // assertTrue("File f2 Is Set To ReadOnly." , f2.canWrite()); 2213 FileOutputStream fos = new FileOutputStream(f2); 2214 // Write to a file. 2215 fos.write(fileString.getBytes()); 2216 fos.close(); 2217 f2.setReadOnly(); 2218 // Assert is flawed because canWrite does not work. 2219 // assertTrue("File f2 Is Not Set To ReadOnly." , !f2.canWrite()); 2220 try { 2221 // Attempt to write to a file that has previously been written 2222 // to. 2223 // and is now set to read only. 2224 fos = new FileOutputStream(f2); 2225 fail("Test 8: IOException expected."); 2226 } catch (IOException e) { 2227 } 2228 p = r.exec("chmod +w " + f2.getAbsolutePath()); 2229 p.waitFor(); 2230 assertTrue("Test 9: File is read-only.", f2.canWrite()); 2231 try { 2232 fos = new FileOutputStream(f2); 2233 fos.write(fileString.getBytes()); 2234 fos.close(); 2235 } catch (IOException e) { 2236 fail("Test 10: Unexpected IOException while attempting to " + 2237 "write to the file. " + e); 2238 } 2239 f2.setReadOnly(); 2240 assertTrue("Test 11: File has not been deleted.", f2.delete()); 2241 // Similarly, trying to delete a read-only directory should succeed 2242 f2 = new File(System.getProperty("java.io.tmpdir"), "deltestdir"); 2243 f2.mkdir(); 2244 f2.setReadOnly(); 2245 assertTrue("Test 12: Directory has not been deleted.", 2246 f2.delete()); 2247 assertTrue("Test 13: Directory has not been deleted.", 2248 ! f2.exists()); 2249 2250 } catch (IOException e) { 2251 fail("Test 14: Unexpected IOException: " + e.getMessage()); 2252 } catch (InterruptedException e) { 2253 fail("Test 15: Unexpected InterruptedException: " + e); 2254 } finally { 2255 if (f1 != null) 2256 f1.delete(); 2257 if (f2 != null) 2258 f2.delete(); 2259 } 2260 } 2261 2262 /** 2263 * @tests java.io.File#toString() 2264 */ 2265 @TestTargetNew( 2266 level = TestLevel.COMPLETE, 2267 notes = "Verifies toString() method.", 2268 method = "toString", 2269 args = {} 2270 ) 2271 public void test_toString() { 2272 // Test for method java.lang.String java.io.File.toString() 2273 String fileName = System.getProperty("java.io.tmpdir") + slash + "input.tst"; 2274 File f = new File(fileName); 2275 assertTrue("Incorrect string returned", f.toString().equals(fileName)); 2276 2277 if (File.separatorChar == '\\') { 2278 String result = new File("c:\\").toString(); 2279 assertTrue("Removed backslash: " + result, result.equals("c:\\")); 2280 } 2281 } 2282 2283 /** 2284 * @tests java.io.File#toURI() 2285 */ 2286 @TestTargetNew( 2287 level = TestLevel.PARTIAL_COMPLETE, 2288 notes = "Verifies toURI() method.", 2289 method = "toURI", 2290 args = {} 2291 ) 2292 public void test_toURI() { 2293 // Test for method java.io.File.toURI() 2294 try { 2295 // Need a directory that exists 2296 File dir = new File(System.getProperty("java.io.tmpdir")); 2297 2298 // Test for toURI when the file is a directory. 2299 String newURIPath = dir.getAbsolutePath(); 2300 newURIPath = newURIPath.replace(File.separatorChar, '/'); 2301 if (!newURIPath.startsWith("/")) 2302 newURIPath = "/" + newURIPath; 2303 if (!newURIPath.endsWith("/")) 2304 newURIPath += '/'; 2305 2306 URI uri = dir.toURI(); 2307 assertTrue("Test 1A: Incorrect URI Returned.", new File(uri) 2308 .equals(dir.getAbsoluteFile())); 2309 assertTrue("Test 1B: Incorrect URI Returned.", uri.equals(new URI( 2310 "file", null, newURIPath, null, null))); 2311 2312 // Test for toURI with a file name with illegal chars. 2313 File f = new File(dir, "te% \u20ac st.tst"); 2314 newURIPath = f.getAbsolutePath(); 2315 newURIPath = newURIPath.replace(File.separatorChar, '/'); 2316 if (!newURIPath.startsWith("/")) 2317 newURIPath = "/" + newURIPath; 2318 2319 uri = f.toURI(); 2320 assertTrue("Test 2A: Incorrect URI Returned.", new File(uri) 2321 .equals(f.getAbsoluteFile())); 2322 assertTrue("Test 2B: Incorrect URI Returned.", uri.equals(new URI( 2323 "file", null, newURIPath, null, null))); 2324 2325 // Regression test for HARMONY-3207 2326 dir = new File(""); // current directory 2327 uri = dir.toURI(); 2328 assertTrue("Test current dir: URI does not end with slash.", 2329 uri.toString().endsWith("/")); 2330 } catch (URISyntaxException e1) { 2331 fail("Unexpected URISyntaxException: " + e1); 2332 } 2333 } 2334 2335 /** 2336 * @tests java.io.File#toURI() 2337 */ 2338 @TestTargetNew( 2339 level = TestLevel.PARTIAL_COMPLETE, 2340 notes = "Verifies that toURI() method works with URIs created with null parameters.", 2341 method = "toURI", 2342 args = {} 2343 ) 2344 public void test_toURI2() { 2345 2346 File f = new File(System.getProperty("java.io.tmpdir"), "a/b/c/../d/e/./f"); 2347 2348 String path = f.getAbsolutePath(); 2349 path = path.replace(File.separatorChar, '/'); 2350 if (!path.startsWith("/")) 2351 path = "/" + path; 2352 2353 try { 2354 URI uri1 = new URI("file", null, path, null); 2355 URI uri2 = f.toURI(); 2356 assertEquals("uris not equal", uri1, uri2); 2357 } catch (URISyntaxException e1) { 2358 fail("Unexpected URISyntaxException," + e1); 2359 } 2360 } 2361 2362 /** 2363 * @tests java.io.File#toURL() 2364 */ 2365 @TestTargetNew( 2366 level = TestLevel.PARTIAL_COMPLETE, 2367 method = "toURL", 2368 args = {} 2369 ) 2370 public void test_toURL() { 2371 // Test for method java.io.File.toURL() 2372 2373 try { 2374 // Need a directory that exists 2375 File dir = new File(System.getProperty("java.io.tmpdir")); 2376 2377 // Test for toURL when the file is a directory. 2378 String newDirURL = dir.getAbsolutePath(); 2379 newDirURL = newDirURL.replace(File.separatorChar, '/'); 2380 if (newDirURL.startsWith("/")) 2381 newDirURL = "file:" + newDirURL; 2382 else 2383 newDirURL = "file:/" + newDirURL; 2384 if (!newDirURL.endsWith("/")) 2385 newDirURL += '/'; 2386 assertEquals("Test 1: Incorrect URL returned;", newDirURL, 2387 dir.toURL().toString()); 2388 2389 // Test for toURL with a file. 2390 File f = new File(dir, "test.tst"); 2391 String newURL = f.getAbsolutePath(); 2392 newURL = newURL.replace(File.separatorChar, '/'); 2393 if (newURL.startsWith("/")) 2394 newURL = "file:" + newURL; 2395 else 2396 newURL = "file:/" + newURL; 2397 assertEquals("Test 2: Incorrect URL returned;", newURL, 2398 f.toURL().toString()); 2399 } catch (java.net.MalformedURLException e) { 2400 fail("Unexpected java.net.MalformedURLException during test."); 2401 } 2402 } 2403 2404 /** 2405 * @tests java.io.File#toURL() 2406 */ 2407 @TestTargetNew( 2408 level = TestLevel.PARTIAL_COMPLETE, 2409 method = "toURL", 2410 args = {} 2411 ) 2412 public void test_toURL2() { 2413 2414 File f = new File(System.getProperty("java.io.tmpdir"), "a/b/c/../d/e/./f"); 2415 2416 String path = f.getAbsolutePath(); 2417 path = path.replace(File.separatorChar, '/'); 2418 if (!path.startsWith("/")) 2419 path = "/" + path; 2420 2421 try { 2422 URL url1 = new URL("file", "", path); 2423 URL url2 = f.toURL(); 2424 assertEquals("urls not equal", url1, url2); 2425 } catch (MalformedURLException e) { 2426 fail("Unexpected MalformedURLException," + e); 2427 } 2428 } 2429 2430 /** 2431 * @tests java.io.File#toURL() 2432 */ 2433 @TestTargetNew( 2434 level = TestLevel.PARTIAL_COMPLETE, 2435 method = "toURL", 2436 args = {} 2437 ) 2438 @AndroidOnly("Incorrectly fails on the RI.") 2439 public void test_toURL3() throws MalformedURLException { 2440 File dir = new File(""); // current directory 2441 String newDirURL = dir.toURL().toString(); 2442 assertTrue("Test 1: URL does not end with slash.", 2443 newDirURL.endsWith("/")); 2444 } 2445 2446 /** 2447 * @tests java.io.File#deleteOnExit() 2448 */ 2449 @TestTargetNew( 2450 level = TestLevel.PARTIAL_COMPLETE, 2451 notes = "", 2452 method = "deleteOnExit", 2453 args = {} 2454 ) 2455 @AndroidOnly("This test only runs on Android because it instantiates " + 2456 "a second Dalvik VM.") 2457 public void test_deleteOnExit() throws IOException, InterruptedException { 2458 String cts = System.getProperty("java.io.tmpdir"); 2459 File dir = new File(cts + "/hello"); 2460 dir.mkdir(); 2461 assertTrue(dir.exists()); 2462 File subDir = new File(cts + "/hello/world"); 2463 subDir.mkdir(); 2464 assertTrue(subDir.exists()); 2465 2466 URL url = getClass().getResource("/HelloWorld.txt"); 2467 String classPath = url.toString(); 2468 int idx = classPath.indexOf("!"); 2469 assertTrue("could not find the path of the test jar/apk", idx > 0); 2470 classPath = classPath.substring(9, idx); // cutting off jar:file: 2471 2472 Support_Exec.execJava(new String[] { 2473 "tests.support.Support_DeleteOnExitTest", 2474 dir.getAbsolutePath(), subDir.getAbsolutePath() }, 2475 new String[] { System.getProperty("java.class.path"), 2476 classPath }, false); 2477 Thread.sleep(2000); 2478 assertFalse(dir.exists()); 2479 assertFalse(subDir.exists()); 2480 } 2481 2482 /** 2483 * @tests serialization 2484 */ 2485 @TestTargetNew( 2486 level = TestLevel.COMPLETE, 2487 notes = "regression test", 2488 method = "!Serialization", 2489 args = {} 2490 ) 2491 public void test_objectStreamClass_getFields() throws Exception { 2492 //Regression for HARMONY-2674 2493 ObjectStreamClass objectStreamClass = ObjectStreamClass 2494 .lookup(File.class); 2495 ObjectStreamField[] objectStreamFields = objectStreamClass.getFields(); 2496 assertEquals(1, objectStreamFields.length); 2497 ObjectStreamField objectStreamField = objectStreamFields[0]; 2498 assertEquals("path", objectStreamField.getName()); 2499 assertEquals(String.class, objectStreamField.getType()); 2500 } 2501 2502 /** 2503 * Sets up the fixture, for example, open a network connection. This method 2504 * is called before a test is executed. 2505 */ 2506 protected void setUp() throws Exception { 2507 // Make sure that system properties are set correctly 2508 String userDir = System.getProperty("java.io.tmpdir"); 2509 if (userDir == null) 2510 throw new Exception("System property java.io.tmpdir not defined."); 2511 System.setProperty("java.io.tmpdir", userDir); 2512 2513 /** Setup the temporary directory */ 2514 if (!userDir.regionMatches((userDir.length() - 1), slash, 0, 1)) 2515 userDir += slash; 2516 tempDirectory = new File(userDir + "tempDir" 2517 + String.valueOf(System.currentTimeMillis())); 2518 if (!tempDirectory.mkdir()) 2519 System.out.println("Setup for FileTest failed (1)."); 2520 2521 /** Setup the temporary file */ 2522 tempFile = new File(tempDirectory, "tempfile"); 2523 FileOutputStream tempStream; 2524 try { 2525 tempStream = new FileOutputStream(tempFile.getPath(), false); 2526 tempStream.close(); 2527 } catch (IOException e) { 2528 System.out.println("Setup for FileTest failed (2)."); 2529 return; 2530 } 2531 } 2532 2533 /** 2534 * Tears down the fixture, for example, close a network connection. This 2535 * method is called after a test is executed. 2536 */ 2537 protected void tearDown() { 2538 if (tempFile.exists() && !tempFile.delete()) 2539 System.out 2540 .println("FileTest.tearDown() failed, could not delete file!"); 2541 if (!tempDirectory.delete()) 2542 System.out 2543 .println("FileTest.tearDown() failed, could not delete directory!"); 2544 } 2545} 2546