FileTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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 System.setProperty("user.home", System.getProperty("java.io.tmpdir")); 1101 String base = System.getProperty("user.home"); 1102 String fname; 1103 File f1; 1104 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 1105 base += slash; 1106 fname = base + "filechk.tst"; 1107 f1 = new File(base, "filechk.tst"); 1108 File f2 = new File("filechk.tst"); 1109 File f3 = new File("c:"); 1110 File f4 = new File(base + "a" + slash + slash + ".." + slash 1111 + "filechk.tst"); 1112 assertTrue("getPath returned incorrect path(f1) " + f1.getPath(), f1 1113 .getPath().equals(fname)); 1114 assertTrue("getPath returned incorrect path(f2) " + f2.getPath(), f2 1115 .getPath().equals("filechk.tst")); 1116 assertTrue("getPath returned incorrect path(f3) " + f3.getPath(), f3 1117 .getPath().equals("c:")); 1118 assertTrue("getPath returned incorrect path(f4) " + f4.getPath(), f4 1119 .getPath().equals( 1120 base + "a" + slash + ".." + slash + "filechk.tst")); 1121 f1.delete(); 1122 f2.delete(); 1123 f3.delete(); 1124 f4.delete(); 1125 } 1126 1127 /** 1128 * @tests java.io.File#isAbsolute() 1129 */ 1130 @TestTargetNew( 1131 level = TestLevel.COMPLETE, 1132 notes = "Verifies isAbsolute() method.", 1133 method = "isAbsolute", 1134 args = {} 1135 ) 1136 public void test_isAbsolute() { 1137 // Test for method boolean java.io.File.isAbsolute() 1138 if (File.separatorChar == '\\') { 1139 File f = new File("c:\\test"); 1140 File f1 = new File("\\test"); 1141 // One or the other should be absolute on Windows or CE 1142 assertTrue("Absolute returned false", (f.isAbsolute() && !f1 1143 .isAbsolute()) 1144 || (!f.isAbsolute() && f1.isAbsolute())); 1145 } else { 1146 File f = new File("/test"); 1147 assertTrue("Absolute returned false", f.isAbsolute()); 1148 } 1149 assertTrue("Non-Absolute returned true", !new File("../test") 1150 .isAbsolute()); 1151 } 1152 1153 /** 1154 * @tests java.io.File#isDirectory() 1155 */ 1156 @TestTargetNew( 1157 level = TestLevel.PARTIAL_COMPLETE, 1158 method = "isDirectory", 1159 args = {} 1160 ) 1161 public void test_isDirectory() { 1162 // Test for method boolean java.io.File.isDirectory() 1163 1164 String base = System.getProperty("java.io.tmpdir"); 1165 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 1166 base += slash; 1167 File f = new File(base); 1168 assertTrue("Test 1: Directory returned false", f.isDirectory()); 1169 f = new File(base + "zxzxzxz" + platformId); 1170 assertTrue("Test 2: (Not created) directory returned true.", !f 1171 .isDirectory()); 1172 f.mkdir(); 1173 try { 1174 assertTrue("Test 3: Directory returned false.", f.isDirectory()); 1175 } finally { 1176 f.delete(); 1177 } 1178 } 1179 1180 /** 1181 * @tests java.io.File#isFile() 1182 */ 1183 @TestTargetNew( 1184 level = TestLevel.PARTIAL_COMPLETE, 1185 method = "isFile", 1186 args = {} 1187 ) 1188 public void test_isFile() { 1189 // Test for method boolean java.io.File.isFile() 1190 try { 1191 String base = System.getProperty("java.io.tmpdir"); 1192 File f = new File(base); 1193 assertTrue("Directory returned true as being a file.", !f.isFile()); 1194 if (!base.regionMatches((base.length() - 1), slash, 0, 1)) 1195 base += slash; 1196 f = new File(base, platformId + "amiafile"); 1197 assertTrue("Non-existent file returned true", !f.isFile()); 1198 FileOutputStream fos = new FileOutputStream(f); 1199 fos.close(); 1200 assertTrue("File returned false", f.isFile()); 1201 f.delete(); 1202 } catch (IOException e) { 1203 fail("IOException during isFile " + e.getMessage()); 1204 } 1205 } 1206 1207 /** 1208 * @tests java.io.File#isHidden() 1209 */ 1210 @TestTargetNew( 1211 level = TestLevel.PARTIAL_COMPLETE, 1212 method = "isHidden", 1213 args = {} 1214 ) 1215 public void test_isHidden() { 1216 // Test for method boolean java.io.File.isHidden() 1217 boolean onUnix = File.separatorChar == '/'; 1218 1219 try { 1220 File f = File.createTempFile("hyts_", ".tmp"); 1221 // On Unix hidden files are marked with a "." at the beginning 1222 // of the file name. 1223 if (onUnix) { 1224 File f2 = new File(System.getProperty("java.io.tmpdir"), ".test.tst" + platformId); 1225 FileOutputStream fos2 = new FileOutputStream(f2); 1226 fos2.close(); 1227 assertTrue("File returned hidden on Unix", !f.isHidden()); 1228 assertTrue("File returned visible on Unix", f2.isHidden()); 1229 assertTrue("File did not delete.", f2.delete()); 1230 } else { 1231 // For windows, the file is being set hidden by the attrib 1232 // command. 1233 Runtime r = Runtime.getRuntime(); 1234 assertTrue("File returned hidden", !f.isHidden()); 1235 Process p = r.exec("attrib +h \"" + f.getAbsolutePath() + "\""); 1236 p.waitFor(); 1237 assertTrue("File returned visible", f.isHidden()); 1238 p = r.exec("attrib -h \"" + f.getAbsolutePath() + "\""); 1239 p.waitFor(); 1240 assertTrue("File returned hidden", !f.isHidden()); 1241 } 1242 f.delete(); 1243 } catch (IOException e) { 1244 fail("Unexpected IOException during test : " + e.getMessage()); 1245 } catch (InterruptedException e) { 1246 fail("Unexpected InterruptedException during test : " 1247 + e.getMessage()); 1248 } 1249 } 1250 1251 /** 1252 * @tests java.io.File#lastModified() 1253 */ 1254 @TestTargetNew( 1255 level = TestLevel.PARTIAL_COMPLETE, 1256 method = "lastModified", 1257 args = {} 1258 ) 1259 public void test_lastModified() { 1260 // Test for method long java.io.File.lastModified() 1261 try { 1262 File f = new File(System.getProperty("java.io.tmpdir"), platformId 1263 + "lModTest.tst"); 1264 f.delete(); 1265 long lastModifiedTime = f.lastModified(); 1266 assertEquals("LastModified Time Should Have Returned 0.", 1267 0, lastModifiedTime); 1268 FileOutputStream fos = new FileOutputStream(f); 1269 fos.close(); 1270 f.setLastModified(315550800000L); 1271 lastModifiedTime = f.lastModified(); 1272 assertTrue("LastModified Time Incorrect: " + lastModifiedTime, 1273 lastModifiedTime == 315550800000L); 1274 f.delete(); 1275 1276 // Regression for Harmony-2146 1277 f = new File("/../"); 1278 assertTrue(f.lastModified() > 0); 1279 } catch (IOException e) { 1280 fail("Unexpected IOException during test : " + e.getMessage()); 1281 } 1282 } 1283 1284 /** 1285 * @tests java.io.File#length() 1286 */ 1287 @TestTargetNew( 1288 level = TestLevel.PARTIAL_COMPLETE, 1289 method = "length", 1290 args = {} 1291 ) 1292 public void test_length() throws Exception { 1293 // Test for method long java.io.File.length() 1294 try { 1295 File f = new File(System.getProperty("java.io.tmpdir"), platformId 1296 + "input.tst"); 1297 assertEquals("File length should have returned 0.", 0, f.length()); 1298 FileOutputStream fos = new FileOutputStream(f); 1299 fos.write(fileString.getBytes()); 1300 fos.close(); 1301 assertTrue("Incorrect file length returned: " + f.length(), f 1302 .length() == fileString.length()); 1303 f.delete(); 1304 } catch (IOException e) { 1305 fail("Unexpected IOException during test : " + e.getMessage()); 1306 } 1307 1308 // regression test for Harmony-1497 1309 File f = File.createTempFile("cts", ".tmp"); 1310 f.deleteOnExit(); 1311 RandomAccessFile raf = new RandomAccessFile(f, "rwd"); 1312 raf.write(0x41); 1313 assertEquals(1, f.length()); 1314 } 1315 1316 /** 1317 * @tests java.io.File#list() 1318 */ 1319 @TestTargetNew( 1320 level = TestLevel.PARTIAL_COMPLETE, 1321 method = "list", 1322 args = {} 1323 ) 1324 public void test_list() { 1325 // Test for method java.lang.String [] java.io.File.list() 1326 1327 String base = System.getProperty("java.io.tmpdir"); 1328 // Old test left behind "garbage files" so this time it creates a 1329 // directory 1330 // that is guaranteed not to already exist (and deletes it afterward.) 1331 int dirNumber = 1; 1332 boolean dirExists = true; 1333 File dir = null; 1334 dir = new File(base, platformId + String.valueOf(dirNumber)); 1335 while (dirExists) { 1336 if (dir.exists()) { 1337 dirNumber++; 1338 dir = new File(base, String.valueOf(dirNumber)); 1339 } else { 1340 dirExists = false; 1341 } 1342 } 1343 1344 String[] flist = dir.list(); 1345 1346 assertNull("Method list() Should Have Returned null.", flist); 1347 1348 assertTrue("Could not create parent directory for list test", dir 1349 .mkdir()); 1350 1351 String[] files = { "mtzz1.xx", "mtzz2.xx", "mtzz3.yy", "mtzz4.yy" }; 1352 try { 1353 assertEquals("Method list() Should Have Returned An Array Of Length 0.", 1354 0, dir.list().length); 1355 1356 File file = new File(dir, "notADir.tst"); 1357 try { 1358 FileOutputStream fos = new FileOutputStream(file); 1359 fos.close(); 1360 assertNull( 1361 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1362 file.list()); 1363 } catch (IOException e) { 1364 fail("Unexpected IOException during test : " + e.getMessage()); 1365 } finally { 1366 file.delete(); 1367 } 1368 1369 try { 1370 for (int i = 0; i < files.length; i++) { 1371 File f = new File(dir, files[i]); 1372 FileOutputStream fos = new FileOutputStream(f); 1373 fos.close(); 1374 } 1375 } catch (IOException e) { 1376 fail("Unexpected IOException during test : " + e.getMessage()); 1377 } 1378 1379 flist = dir.list(); 1380 if (flist.length != files.length) { 1381 fail("Incorrect list returned"); 1382 } 1383 1384 // Checking to make sure the correct files were are listed in the 1385 // array. 1386 boolean[] check = new boolean[flist.length]; 1387 for (int i = 0; i < check.length; i++) 1388 check[i] = false; 1389 for (int i = 0; i < files.length; i++) { 1390 for (int j = 0; j < flist.length; j++) { 1391 if (flist[j].equals(files[i])) { 1392 check[i] = true; 1393 break; 1394 } 1395 } 1396 } 1397 int checkCount = 0; 1398 for (int i = 0; i < check.length; i++) { 1399 if (check[i] == false) 1400 checkCount++; 1401 } 1402 assertEquals("Invalid file returned in listing", 0, checkCount); 1403 1404 for (int i = 0; i < files.length; i++) { 1405 File f = new File(dir, files[i]); 1406 f.delete(); 1407 } 1408 1409 assertTrue("Could not delete parent directory for list test.", dir 1410 .delete()); 1411 } finally { 1412 for (int i = 0; i < files.length; i++) { 1413 File f = new File(dir, files[i]); 1414 f.delete(); 1415 } 1416 dir.delete(); 1417 } 1418 1419 } 1420 1421 /** 1422 * @tests java.io.File#listFiles() 1423 */ 1424 @TestTargetNew( 1425 level = TestLevel.PARTIAL_COMPLETE, 1426 method = "listFiles", 1427 args = {} 1428 ) 1429 public void test_listFiles() { 1430 // Test for method java.io.File.listFiles() 1431 1432 try { 1433 String base = System.getProperty("java.io.tmpdir"); 1434 // Finding a non-existent directory to create. 1435 int dirNumber = 1; 1436 boolean dirExists = true; 1437 File dir = new File(base, platformId + String.valueOf(dirNumber)); 1438 // Making sure that the directory does not exist. 1439 while (dirExists) { 1440 // If the directory exists, add one to the directory number 1441 // (making 1442 // it a new directory name.) 1443 if (dir.exists()) { 1444 dirNumber++; 1445 dir = new File(base, String.valueOf(dirNumber)); 1446 } else { 1447 dirExists = false; 1448 } 1449 } 1450 // Test for attempting to cal listFiles on a non-existent directory. 1451 assertNull("listFiles Should Return Null.", dir.listFiles()); 1452 1453 assertTrue("Failed To Create Parent Directory.", dir.mkdir()); 1454 1455 String[] files = { "1.tst", "2.tst", "3.tst", "" }; 1456 try { 1457 assertEquals("listFiles Should Return An Array Of Length 0.", 0, dir 1458 .listFiles().length); 1459 1460 File file = new File(dir, "notADir.tst"); 1461 try { 1462 FileOutputStream fos = new FileOutputStream(file); 1463 fos.close(); 1464 assertNull( 1465 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1466 file.listFiles()); 1467 } catch (IOException e) { 1468 fail("Unexpected IOException during test : " + e.getMessage()); 1469 } finally { 1470 file.delete(); 1471 } 1472 1473 for (int i = 0; i < (files.length - 1); i++) { 1474 File f = new File(dir, files[i]); 1475 FileOutputStream fos = new FileOutputStream(f); 1476 fos.close(); 1477 } 1478 1479 new File(dir, "doesNotExist.tst"); 1480 File[] flist = dir.listFiles(); 1481 1482 // Test to make sure that only the 3 files that were created are 1483 // listed. 1484 assertEquals("Incorrect Number Of Files Returned.", 1485 3, flist.length); 1486 1487 // Test to make sure that listFiles can read hidden files. 1488 boolean onUnix = File.separatorChar == '/'; 1489 boolean onWindows = File.separatorChar == '\\'; 1490 if (onWindows) { 1491 files[3] = "4.tst"; 1492 File f = new File(dir, "4.tst"); 1493 FileOutputStream fos = new FileOutputStream(f); 1494 fos.close(); 1495 Runtime r = Runtime.getRuntime(); 1496 Process p = r.exec("attrib +h \"" + f.getPath() + "\""); 1497 p.waitFor(); 1498 } 1499 if (onUnix) { 1500 files[3] = ".4.tst"; 1501 File f = new File(dir, ".4.tst"); 1502 FileOutputStream fos = new FileOutputStream(f); 1503 fos.close(); 1504 } 1505 flist = dir.listFiles(); 1506 assertEquals("Incorrect Number Of Files Returned.", 1507 4, flist.length); 1508 1509 // Checking to make sure the correct files were are listed in 1510 // the array. 1511 boolean[] check = new boolean[flist.length]; 1512 for (int i = 0; i < check.length; i++) 1513 check[i] = false; 1514 for (int i = 0; i < files.length; i++) { 1515 for (int j = 0; j < flist.length; j++) { 1516 if (flist[j].getName().equals(files[i])) { 1517 check[i] = true; 1518 break; 1519 } 1520 } 1521 } 1522 int checkCount = 0; 1523 for (int i = 0; i < check.length; i++) { 1524 if (check[i] == false) 1525 checkCount++; 1526 } 1527 assertEquals("Invalid file returned in listing", 0, checkCount); 1528 1529 if (onWindows) { 1530 Runtime r = Runtime.getRuntime(); 1531 Process p = r.exec("attrib -h \"" 1532 + new File(dir, files[3]).getPath() + "\""); 1533 p.waitFor(); 1534 } 1535 1536 for (int i = 0; i < files.length; i++) { 1537 File f = new File(dir, files[i]); 1538 f.delete(); 1539 } 1540 assertTrue("Parent Directory Not Deleted.", dir.delete()); 1541 } finally { 1542 for (int i = 0; i < files.length; i++) { 1543 File f = new File(dir, files[i]); 1544 f.delete(); 1545 } 1546 dir.delete(); 1547 } 1548 } catch (IOException e) { 1549 fail("Unexpected IOException during test : " + e.getMessage()); 1550 } catch (InterruptedException e) { 1551 fail("Unexpected InterruptedException during test : " + e.getMessage()); 1552 } 1553 } 1554 1555 /** 1556 * @tests java.io.File#listFiles(java.io.FileFilter) 1557 */ 1558 @TestTargetNew( 1559 level = TestLevel.PARTIAL_COMPLETE, 1560 method = "listFiles", 1561 args = {java.io.FileFilter.class} 1562 ) 1563 public void test_listFilesLjava_io_FileFilter() { 1564 // Test for method java.io.File.listFiles(File Filter filter) 1565 1566 String base = System.getProperty("java.io.tmpdir"); 1567 // Finding a non-existent directory to create. 1568 int dirNumber = 1; 1569 boolean dirExists = true; 1570 File baseDir = new File(base, platformId + String.valueOf(dirNumber)); 1571 // Making sure that the directory does not exist. 1572 while (dirExists) { 1573 // If the directory exists, add one to the directory number (making 1574 // it a new directory name.) 1575 if (baseDir.exists()) { 1576 dirNumber++; 1577 baseDir = new File(base, String.valueOf(dirNumber)); 1578 } else { 1579 dirExists = false; 1580 } 1581 } 1582 1583 // Creating a filter that catches directories. 1584 FileFilter dirFilter = new FileFilter() { 1585 public boolean accept(File f) { 1586 if (f.isDirectory()) 1587 return true; 1588 else 1589 return false; 1590 } 1591 }; 1592 1593 assertNull("listFiles Should Return Null.", baseDir 1594 .listFiles(dirFilter)); 1595 1596 assertTrue("Failed To Create Parent Directory.", baseDir.mkdir()); 1597 1598 File dir1 = null; 1599 String[] files = { "1.tst", "2.tst", "3.tst" }; 1600 try { 1601 assertEquals("listFiles Should Return An Array Of Length 0.", 0, baseDir 1602 .listFiles(dirFilter).length); 1603 1604 File file = new File(baseDir, "notADir.tst"); 1605 try { 1606 FileOutputStream fos = new FileOutputStream(file); 1607 fos.close(); 1608 assertNull( 1609 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1610 file.listFiles(dirFilter)); 1611 } catch (IOException e) { 1612 fail("Unexpected IOException During Test."); 1613 } finally { 1614 file.delete(); 1615 } 1616 1617 try { 1618 for (int i = 0; i < files.length; i++) { 1619 File f = new File(baseDir, files[i]); 1620 FileOutputStream fos = new FileOutputStream(f); 1621 fos.close(); 1622 } 1623 } catch (IOException e) { 1624 fail("Unexpected IOException during test : " + e.getMessage()); 1625 } 1626 dir1 = new File(baseDir, "Temp1"); 1627 dir1.mkdir(); 1628 1629 // Creating a filter that catches files. 1630 FileFilter fileFilter = new FileFilter() { 1631 public boolean accept(File f) { 1632 if (f.isFile()) 1633 return true; 1634 else 1635 return false; 1636 } 1637 }; 1638 1639 // Test to see if the correct number of directories are returned. 1640 File[] directories = baseDir.listFiles(dirFilter); 1641 assertEquals("Incorrect Number Of Directories Returned.", 1642 1, directories.length); 1643 1644 // Test to see if the directory was saved with the correct name. 1645 assertEquals("Incorrect Directory Returned.", 0, directories[0] 1646 .compareTo(dir1)); 1647 1648 // Test to see if the correct number of files are returned. 1649 File[] flist = baseDir.listFiles(fileFilter); 1650 assertTrue("Incorrect Number Of Files Returned.", 1651 flist.length == files.length); 1652 1653 // Checking to make sure the correct files were are listed in the 1654 // array. 1655 boolean[] check = new boolean[flist.length]; 1656 for (int i = 0; i < check.length; i++) 1657 check[i] = false; 1658 for (int i = 0; i < files.length; i++) { 1659 for (int j = 0; j < flist.length; j++) { 1660 if (flist[j].getName().equals(files[i])) { 1661 check[i] = true; 1662 break; 1663 } 1664 } 1665 } 1666 int checkCount = 0; 1667 for (int i = 0; i < check.length; i++) { 1668 if (check[i] == false) 1669 checkCount++; 1670 } 1671 assertEquals("Invalid file returned in listing", 0, checkCount); 1672 1673 for (int i = 0; i < files.length; i++) { 1674 File f = new File(baseDir, files[i]); 1675 f.delete(); 1676 } 1677 dir1.delete(); 1678 assertTrue("Parent Directory Not Deleted.", baseDir.delete()); 1679 } finally { 1680 for (int i = 0; i < files.length; i++) { 1681 File f = new File(baseDir, files[i]); 1682 f.delete(); 1683 } 1684 if (dir1 != null) 1685 dir1.delete(); 1686 baseDir.delete(); 1687 } 1688 } 1689 1690 /** 1691 * @tests java.io.File#listFiles(java.io.FilenameFilter) 1692 */ 1693 @TestTargetNew( 1694 level = TestLevel.PARTIAL_COMPLETE, 1695 method = "listFiles", 1696 args = {java.io.FilenameFilter.class} 1697 ) 1698 public void test_listFilesLjava_io_FilenameFilter() { 1699 // Test for method java.io.File.listFiles(FilenameFilter filter) 1700 1701 String base = System.getProperty("java.io.tmpdir"); 1702 // Finding a non-existent directory to create. 1703 int dirNumber = 1; 1704 boolean dirExists = true; 1705 File dir = new File(base, platformId + String.valueOf(dirNumber)); 1706 // Making sure that the directory does not exist. 1707 while (dirExists) { 1708 // If the directory exists, add one to the directory number (making 1709 // it a new directory name.) 1710 if (dir.exists()) { 1711 dirNumber++; 1712 dir = new File(base, platformId + String.valueOf(dirNumber)); 1713 } else { 1714 dirExists = false; 1715 } 1716 } 1717 1718 // Creating a filter that catches "*.tst" files. 1719 FilenameFilter tstFilter = new FilenameFilter() { 1720 public boolean accept(File f, String fileName) { 1721 // If the suffix is ".tst" then send it to the array 1722 if (fileName.endsWith(".tst")) 1723 return true; 1724 else 1725 return false; 1726 } 1727 }; 1728 1729 assertNull("listFiles Should Return Null.", 1730 dir.listFiles(tstFilter)); 1731 1732 assertTrue("Failed To Create Parent Directory.", dir.mkdir()); 1733 1734 String[] files = { "1.tst", "2.tst", "3.tmp" }; 1735 try { 1736 assertEquals("listFiles Should Return An Array Of Length 0.", 0, dir 1737 .listFiles(tstFilter).length); 1738 1739 File file = new File(dir, "notADir.tst"); 1740 try { 1741 FileOutputStream fos = new FileOutputStream(file); 1742 fos.close(); 1743 assertNull( 1744 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.", 1745 file.listFiles(tstFilter)); 1746 } catch (IOException e) { 1747 fail("Unexpected IOException during test : " + e.getMessage()); 1748 } finally { 1749 file.delete(); 1750 } 1751 1752 try { 1753 for (int i = 0; i < files.length; i++) { 1754 File f = new File(dir, files[i]); 1755 FileOutputStream fos = new FileOutputStream(f); 1756 fos.close(); 1757 } 1758 } catch (IOException e) { 1759 fail("Unexpected IOException During Test : " + e.getMessage()); 1760 } 1761 1762 // Creating a filter that catches "*.tmp" files. 1763 FilenameFilter tmpFilter = new FilenameFilter() { 1764 public boolean accept(File f, String fileName) { 1765 // If the suffix is ".tmp" then send it to the array 1766 if (fileName.endsWith(".tmp")) 1767 return true; 1768 else 1769 return false; 1770 } 1771 }; 1772 1773 // Tests to see if the correct number of files were returned. 1774 File[] flist = dir.listFiles(tstFilter); 1775 assertEquals("Incorrect Number Of Files Passed Through tstFilter.", 1776 2, flist.length); 1777 for (int i = 0; i < flist.length; i++) 1778 assertTrue("File Should Not Have Passed The tstFilter.", 1779 flist[i].getPath().endsWith(".tst")); 1780 1781 flist = dir.listFiles(tmpFilter); 1782 assertEquals("Incorrect Number Of Files Passed Through tmpFilter.", 1783 1, flist.length); 1784 assertTrue("File Should Not Have Passed The tmpFilter.", flist[0] 1785 .getPath().endsWith(".tmp")); 1786 1787 for (int i = 0; i < files.length; i++) { 1788 File f = new File(dir, files[i]); 1789 f.delete(); 1790 } 1791 assertTrue("Parent Directory Not Deleted.", dir.delete()); 1792 } finally { 1793 for (int i = 0; i < files.length; i++) { 1794 File f = new File(dir, files[i]); 1795 f.delete(); 1796 } 1797 dir.delete(); 1798 } 1799 } 1800 1801 /** 1802 * @tests java.io.File#list(java.io.FilenameFilter) 1803 */ 1804 @TestTargetNew( 1805 level = TestLevel.PARTIAL_COMPLETE, 1806 method = "list", 1807 args = {java.io.FilenameFilter.class} 1808 ) 1809 public void test_listLjava_io_FilenameFilter() { 1810 // Test for method java.lang.String [] 1811 // java.io.File.list(java.io.FilenameFilter) 1812 1813 String base = System.getProperty("java.io.tmpdir"); 1814 // Old test left behind "garbage files" so this time it creates a 1815 // directory 1816 // that is guaranteed not to already exist (and deletes it afterward.) 1817 int dirNumber = 1; 1818 boolean dirExists = true; 1819 File dir = new File(base, platformId + String.valueOf(dirNumber)); 1820 while (dirExists) { 1821 if (dir.exists()) { 1822 dirNumber++; 1823 dir = new File(base, String.valueOf(dirNumber)); 1824 } else { 1825 dirExists = false; 1826 } 1827 } 1828 1829 FilenameFilter filter = new FilenameFilter() { 1830 public boolean accept(File dir, String name) { 1831 return !name.equals("mtzz1.xx"); 1832 } 1833 }; 1834 1835 String[] flist = dir.list(filter); 1836 assertNull("Method list(FilenameFilter) Should Have Returned Null.", 1837 flist); 1838 1839 assertTrue("Could not create parent directory for test", dir.mkdir()); 1840 1841 String[] files = { "mtzz1.xx", "mtzz2.xx", "mtzz3.yy", "mtzz4.yy" }; 1842 try { 1843 /* 1844 * Do not return null when trying to use list(Filename Filter) on a 1845 * file rather than a directory. All other "list" methods return 1846 * null for this test case. 1847 */ 1848 /* 1849 * File file = new File(dir, "notADir.tst"); try { FileOutputStream 1850 * fos = new FileOutputStream(file); fos.close(); } catch 1851 * (IOException e) { fail("Unexpected IOException During 1852 * Test."); } flist = dir.list(filter); assertNull("listFiles 1853 * Should Have Returned Null When Used On A File Instead Of A 1854 * Directory.", flist); file.delete(); 1855 */ 1856 1857 flist = dir.list(filter); 1858 assertEquals("Array Of Length 0 Should Have Returned.", 1859 0, flist.length); 1860 1861 try { 1862 for (int i = 0; i < files.length; i++) { 1863 File f = new File(dir, files[i]); 1864 FileOutputStream fos = new FileOutputStream(f); 1865 fos.close(); 1866 } 1867 } catch (IOException e) { 1868 fail("Unexpected IOException during test : " + e.getMessage()); 1869 } 1870 1871 flist = dir.list(filter); 1872 1873 if (flist.length != files.length - 1) { 1874 fail("Incorrect list returned"); 1875 } 1876 1877 // Checking to make sure the correct files were are listed in the 1878 // array. 1879 boolean[] check = new boolean[flist.length]; 1880 for (int i = 0; i < check.length; i++) 1881 check[i] = false; 1882 String[] wantedFiles = { "mtzz2.xx", "mtzz3.yy", "mtzz4.yy" }; 1883 for (int i = 0; i < wantedFiles.length; i++) { 1884 for (int j = 0; j < flist.length; j++) { 1885 if (flist[j].equals(wantedFiles[i])) { 1886 check[i] = true; 1887 break; 1888 } 1889 } 1890 } 1891 int checkCount = 0; 1892 for (int i = 0; i < check.length; i++) { 1893 if (check[i] == false) 1894 checkCount++; 1895 } 1896 assertEquals("Invalid file returned in listing", 0, checkCount); 1897 1898 for (int i = 0; i < files.length; i++) { 1899 File f = new File(dir, files[i]); 1900 f.delete(); 1901 } 1902 assertTrue("Could not delete parent directory for test.", dir 1903 .delete()); 1904 } finally { 1905 for (int i = 0; i < files.length; i++) { 1906 File f = new File(dir, files[i]); 1907 f.delete(); 1908 } 1909 dir.delete(); 1910 } 1911 } 1912 1913 /** 1914 * @tests java.io.File#listRoots() 1915 */ 1916 @TestTargetNew( 1917 level = TestLevel.COMPLETE, 1918 notes = "Verifies listRoots() method.", 1919 method = "listRoots", 1920 args = {} 1921 ) 1922 public void test_listRoots() { 1923 // Test for method java.io.File.listRoots() 1924 1925 File[] roots = File.listRoots(); 1926 boolean onUnix = File.separatorChar == '/'; 1927 boolean onWindows = File.separatorChar == '\\'; 1928 if (onUnix) { 1929 assertEquals("Incorrect Number Of Root Directories.", 1930 1, roots.length); 1931 String fileLoc = roots[0].getPath(); 1932 assertTrue("Incorrect Root Directory Returned.", fileLoc 1933 .startsWith(slash)); 1934 } else if (onWindows) { 1935 // Need better test for Windows 1936 assertTrue("Incorrect Number Of Root Directories.", 1937 roots.length > 0); 1938 } 1939 } 1940 1941 /** 1942 * @tests java.io.File#mkdir() 1943 */ 1944 @TestTargetNew( 1945 level = TestLevel.PARTIAL_COMPLETE, 1946 method = "mkdir", 1947 args = {} 1948 ) 1949 public void test_mkdir() throws IOException { 1950 // Test for method boolean java.io.File.mkdir() 1951 1952 String base = System.getProperty("java.io.tmpdir"); 1953 // Old test left behind "garbage files" so this time it creates a 1954 // directory 1955 // that is guaranteed not to already exist (and deletes it afterward.) 1956 int dirNumber = 1; 1957 boolean dirExists = true; 1958 File dir = new File(base, String.valueOf(dirNumber)); 1959 while (dirExists) { 1960 if (dir.exists()) { 1961 dirNumber++; 1962 dir = new File(base, String.valueOf(dirNumber)); 1963 } else { 1964 dirExists = false; 1965 } 1966 } 1967 1968 assertTrue("mkdir failed", dir.mkdir() && dir.exists()); 1969 dir.deleteOnExit(); 1970 1971 String longDirName = "abcdefghijklmnopqrstuvwx";// 24 chars 1972 StringBuilder sb = new StringBuilder(dir + File.separator); 1973 StringBuilder sb2 = new StringBuilder(dir + File.separator); 1974 1975 // Test make a long path 1976 while (dir.getCanonicalPath().length() < 256 - longDirName.length()) { 1977 sb.append(longDirName + File.separator); 1978 dir = new File(sb.toString()); 1979 assertTrue("mkdir failed", dir.mkdir() && dir.exists()); 1980 dir.deleteOnExit(); 1981 } 1982 1983 while (dir.getCanonicalPath().length() < 256) { 1984 sb.append(0); 1985 dir = new File(sb.toString()); 1986 assertTrue("mkdir " + dir.getCanonicalPath().length() + " failed", 1987 dir.mkdir() && dir.exists()); 1988 dir.deleteOnExit(); 1989 } 1990 1991 // Test make many paths 1992 while (dir.getCanonicalPath().length() < 256) { 1993 sb2.append(0); 1994 dir = new File(sb2.toString()); 1995 assertTrue("mkdir " + dir.getCanonicalPath().length() + " failed", 1996 dir.mkdir() && dir.exists()); 1997 dir.deleteOnExit(); 1998 } 1999 } 2000 2001 /** 2002 * @tests java.io.File#mkdirs() 2003 */ 2004 @TestTargetNew( 2005 level = TestLevel.PARTIAL_COMPLETE, 2006 method = "mkdirs", 2007 args = {} 2008 ) 2009 public void test_mkdirs() { 2010 // Test for method boolean java.io.File.mkdirs() 2011 2012 String userHome = System.getProperty("java.io.tmpdir"); 2013 if (!userHome.endsWith(slash)) 2014 userHome += slash; 2015 File f = new File(userHome + "mdtest" + platformId + slash + "mdtest2", 2016 "p.tst"); 2017 File g = new File(userHome + "mdtest" + platformId + slash + "mdtest2"); 2018 File h = new File(userHome + "mdtest" + platformId); 2019 f.mkdirs(); 2020 try { 2021 assertTrue("Base Directory not created", h.exists()); 2022 assertTrue("Directories not created", g.exists()); 2023 assertTrue("File not created", f.exists()); 2024 } finally { 2025 f.delete(); 2026 g.delete(); 2027 h.delete(); 2028 } 2029 } 2030 2031 /** 2032 * @tests java.io.File#renameTo(java.io.File) 2033 */ 2034 @TestTargetNew( 2035 level = TestLevel.PARTIAL_COMPLETE, 2036 method = "renameTo", 2037 args = {java.io.File.class} 2038 ) 2039 public void test_renameToLjava_io_File() { 2040 // Test for method boolean java.io.File.renameTo(java.io.File) 2041 String base = System.getProperty("java.io.tmpdir"); 2042 File dir = new File(base, platformId); 2043 dir.mkdir(); 2044 File f = new File(dir, "xxx.xxx"); 2045 File rfile = new File(dir, "yyy.yyy"); 2046 File f2 = new File(dir, "zzz.zzz"); 2047 try { 2048 FileOutputStream fos = new FileOutputStream(f); 2049 fos.write(fileString.getBytes()); 2050 fos.close(); 2051 long lengthOfFile = f.length(); 2052 2053 rfile.delete(); // in case it already exists 2054 2055 try { 2056 f.renameTo(null); 2057 fail("Test 1: NullPointerException expected."); 2058 } catch (NullPointerException e) { 2059 // Expected. 2060 } 2061 2062 assertTrue("Test 2: File rename failed.", f.renameTo(rfile)); 2063 assertTrue("Test 3: File rename failed.", rfile.exists()); 2064 assertTrue("Test 4: Size Of File Changed.", 2065 rfile.length() == lengthOfFile); 2066 2067 fos = new FileOutputStream(rfile); 2068 fos.close(); 2069 2070 f2.delete(); // in case it already exists 2071 assertTrue("Test 5: File rename failed.", rfile.renameTo(f2)); 2072 assertTrue("Test 6: File rename failed.", f2.exists()); 2073 } catch (IOException e) { 2074 fail("Unexpected IOException during test : " + e.getMessage()); 2075 } finally { 2076 f.delete(); 2077 rfile.delete(); 2078 f2.delete(); 2079 dir.delete(); 2080 } 2081 } 2082 2083 /** 2084 * @tests java.io.File#setLastModified(long) 2085 */ 2086 @TestTargetNew( 2087 level = TestLevel.PARTIAL_COMPLETE, 2088 method = "setLastModified", 2089 args = {long.class} 2090 ) 2091 public void test_setLastModifiedJ() { 2092 // Test for method java.io.File.setLastModified() 2093 File f1 = null; 2094 try { 2095 // f1 = File.createTempFile("hyts_tf" , ".tmp"); 2096 // jclRM does not include File.createTempFile 2097 f1 = new File(System.getProperty("java.io.tmpdir"), 2098 platformId + "hyts_tf_slm.tst"); 2099 f1.createNewFile(); 2100 long orgTime = f1.lastModified(); 2101 // Subtracting 100 000 milliseconds from the orgTime of File f1 2102 f1.setLastModified(orgTime - 100000); 2103 long lastModified = f1.lastModified(); 2104 assertTrue("Test 1: LastModifed time incorrect: " + lastModified, 2105 lastModified == (orgTime - 100000)); 2106 // Subtracting 10 000 000 milliseconds from the orgTime of File f1 2107 f1.setLastModified(orgTime - 10000000); 2108 lastModified = f1.lastModified(); 2109 assertTrue("Test 2: LastModifed time incorrect: " + lastModified, 2110 lastModified == (orgTime - 10000000)); 2111 // Adding 100 000 milliseconds to the orgTime of File f1 2112 f1.setLastModified(orgTime + 100000); 2113 lastModified = f1.lastModified(); 2114 assertTrue("Test 3: LastModifed time incorrect: " + lastModified, 2115 lastModified == (orgTime + 100000)); 2116 // Adding 10 000 000 milliseconds from the orgTime of File f1 2117 f1.setLastModified(orgTime + 10000000); 2118 lastModified = f1.lastModified(); 2119 assertTrue("Test 4: LastModifed time incorrect: " + lastModified, 2120 lastModified == (orgTime + 10000000)); 2121 // Trying to set time to an exact number 2122 f1.setLastModified(315550800000L); 2123 lastModified = f1.lastModified(); 2124 assertTrue("Test 5: LastModified time incorrect: " + lastModified, 2125 lastModified == 315550800000L); 2126 String osName = System.getProperty("os.name", "unknown"); 2127 if (osName.equals("Windows 2000") || osName.equals("Windows NT")) { 2128 // Trying to set time to a large exact number 2129 boolean result = f1.setLastModified(4354837199000L); 2130 long next = f1.lastModified(); 2131 // Dec 31 23:59:59 EST 2107 is overflow on FAT file systems, and 2132 // the call fails 2133 assertTrue("Test 6: LastModified time incorrect: " + next, 2134 !result || next == 4354837199000L); 2135 } 2136 // Trying to set time to a negative number 2137 try { 2138 f1.setLastModified(-25); 2139 fail("IllegalArgumentException not thrown."); 2140 } catch (IllegalArgumentException e) { 2141 } 2142 } catch (IOException e) { 2143 fail("Unexpected IOException during test : " + e.getMessage()); 2144 } finally { 2145 if (f1 != null) 2146 f1.delete(); 2147 } 2148 } 2149 2150 /** 2151 * @tests java.io.File#setReadOnly() 2152 */ 2153 @TestTargets({ 2154 @TestTargetNew( 2155 level = TestLevel.PARTIAL_COMPLETE, 2156 method = "setReadOnly", 2157 args = {} 2158 ), 2159 @TestTargetNew( 2160 level = TestLevel.PARTIAL_COMPLETE, 2161 method = "canWrite", 2162 args = {} 2163 ) 2164 }) 2165 @KnownFailure("canWrite() returns true even when a file is marked " + 2166 "read-only (Test 2). It is also possible to open this file " + 2167 "for writing (Test 3).") 2168 public void test_setReadOnly() { 2169 // Test for method java.io.File.setReadOnly() 2170 2171 File f1 = null; 2172 File f2 = null; 2173 Runtime r = Runtime.getRuntime(); 2174 Process p; 2175 try { 2176 f1 = File.createTempFile("hyts_tf", ".tmp"); 2177 f2 = File.createTempFile("hyts_tf", ".tmp"); 2178 2179 assertTrue("Test 1: File is read-only." , f1.canWrite()); 2180 f1.setReadOnly(); 2181 assertTrue("Test 2: File is not read-only." , !f1.canWrite()); 2182 2183 try { 2184 // Attempt to write to a file that is read-only. 2185 new FileOutputStream(f1); 2186 fail("Test 3: IOException expected."); 2187 } catch (IOException e) { 2188 // Expected. 2189 } 2190 p = r.exec("chmod +w " + f1.getAbsolutePath()); 2191 p.waitFor(); 2192 2193 assertTrue("Test 4: File is read-only." , f1.canWrite()); 2194 try { 2195 FileOutputStream fos = new FileOutputStream(f1); 2196 fos.write(fileString.getBytes()); 2197 fos.close(); 2198 assertTrue("Test 5: Unable to write to the file.", 2199 f1.length() == fileString.length()); 2200 } catch (IOException e) { 2201 fail("Test 6: Unexpected IOException while attempting to " + 2202 "write to the file. " + e); 2203 } 2204 assertTrue("Test 7: File has not been deleted.", f1.delete()); 2205 2206 // Assert is flawed because canWrite does not work. 2207 // assertTrue("File f2 Is Set To ReadOnly." , f2.canWrite()); 2208 FileOutputStream fos = new FileOutputStream(f2); 2209 // Write to a file. 2210 fos.write(fileString.getBytes()); 2211 fos.close(); 2212 f2.setReadOnly(); 2213 // Assert is flawed because canWrite does not work. 2214 // assertTrue("File f2 Is Not Set To ReadOnly." , !f2.canWrite()); 2215 try { 2216 // Attempt to write to a file that has previously been written 2217 // to. 2218 // and is now set to read only. 2219 fos = new FileOutputStream(f2); 2220 fail("Test 8: IOException expected."); 2221 } catch (IOException e) { 2222 } 2223 p = r.exec("chmod +w " + f2.getAbsolutePath()); 2224 p.waitFor(); 2225 assertTrue("Test 9: File is read-only.", f2.canWrite()); 2226 try { 2227 fos = new FileOutputStream(f2); 2228 fos.write(fileString.getBytes()); 2229 fos.close(); 2230 } catch (IOException e) { 2231 fail("Test 10: Unexpected IOException while attempting to " + 2232 "write to the file. " + e); 2233 } 2234 f2.setReadOnly(); 2235 assertTrue("Test 11: File has not been deleted.", f2.delete()); 2236 // Similarly, trying to delete a read-only directory should succeed 2237 f2 = new File(System.getProperty("java.io.tmpdir"), "deltestdir"); 2238 f2.mkdir(); 2239 f2.setReadOnly(); 2240 assertTrue("Test 12: Directory has not been deleted.", 2241 f2.delete()); 2242 assertTrue("Test 13: Directory has not been deleted.", 2243 ! f2.exists()); 2244 2245 } catch (IOException e) { 2246 fail("Test 14: Unexpected IOException: " + e.getMessage()); 2247 } catch (InterruptedException e) { 2248 fail("Test 15: Unexpected InterruptedException: " + e); 2249 } finally { 2250 if (f1 != null) 2251 f1.delete(); 2252 if (f2 != null) 2253 f2.delete(); 2254 } 2255 } 2256 2257 /** 2258 * @tests java.io.File#toString() 2259 */ 2260 @TestTargetNew( 2261 level = TestLevel.COMPLETE, 2262 notes = "Verifies toString() method.", 2263 method = "toString", 2264 args = {} 2265 ) 2266 public void test_toString() { 2267 // Test for method java.lang.String java.io.File.toString() 2268 String fileName = System.getProperty("java.io.tmpdir") + slash + "input.tst"; 2269 File f = new File(fileName); 2270 assertTrue("Incorrect string returned", f.toString().equals(fileName)); 2271 2272 if (File.separatorChar == '\\') { 2273 String result = new File("c:\\").toString(); 2274 assertTrue("Removed backslash: " + result, result.equals("c:\\")); 2275 } 2276 } 2277 2278 /** 2279 * @tests java.io.File#toURI() 2280 */ 2281 @TestTargetNew( 2282 level = TestLevel.PARTIAL_COMPLETE, 2283 notes = "Verifies toURI() method.", 2284 method = "toURI", 2285 args = {} 2286 ) 2287 public void test_toURI() { 2288 // Test for method java.io.File.toURI() 2289 try { 2290 // Need a directory that exists 2291 File dir = new File(System.getProperty("java.io.tmpdir")); 2292 2293 // Test for toURI when the file is a directory. 2294 String newURIPath = dir.getAbsolutePath(); 2295 newURIPath = newURIPath.replace(File.separatorChar, '/'); 2296 if (!newURIPath.startsWith("/")) 2297 newURIPath = "/" + newURIPath; 2298 if (!newURIPath.endsWith("/")) 2299 newURIPath += '/'; 2300 2301 URI uri = dir.toURI(); 2302 assertTrue("Test 1A: Incorrect URI Returned.", new File(uri) 2303 .equals(dir.getAbsoluteFile())); 2304 assertTrue("Test 1B: Incorrect URI Returned.", uri.equals(new URI( 2305 "file", null, newURIPath, null, null))); 2306 2307 // Test for toURI with a file name with illegal chars. 2308 File f = new File(dir, "te% \u20ac st.tst"); 2309 newURIPath = f.getAbsolutePath(); 2310 newURIPath = newURIPath.replace(File.separatorChar, '/'); 2311 if (!newURIPath.startsWith("/")) 2312 newURIPath = "/" + newURIPath; 2313 2314 uri = f.toURI(); 2315 assertTrue("Test 2A: Incorrect URI Returned.", new File(uri) 2316 .equals(f.getAbsoluteFile())); 2317 assertTrue("Test 2B: Incorrect URI Returned.", uri.equals(new URI( 2318 "file", null, newURIPath, null, null))); 2319 2320 // Regression test for HARMONY-3207 2321 dir = new File(""); // current directory 2322 uri = dir.toURI(); 2323 assertTrue("Test current dir: URI does not end with slash.", 2324 uri.toString().endsWith("/")); 2325 } catch (URISyntaxException e1) { 2326 fail("Unexpected URISyntaxException: " + e1); 2327 } 2328 } 2329 2330 /** 2331 * @tests java.io.File#toURI() 2332 */ 2333 @TestTargetNew( 2334 level = TestLevel.PARTIAL_COMPLETE, 2335 notes = "Verifies that toURI() method works with URIs created with null parameters.", 2336 method = "toURI", 2337 args = {} 2338 ) 2339 public void test_toURI2() { 2340 2341 File f = new File(System.getProperty("ctsdir"), "a/b/c/../d/e/./f"); 2342 2343 String path = f.getAbsolutePath(); 2344 path = path.replace(File.separatorChar, '/'); 2345 if (!path.startsWith("/")) 2346 path = "/" + path; 2347 2348 try { 2349 URI uri1 = new URI("file", null, path, null); 2350 URI uri2 = f.toURI(); 2351 assertEquals("uris not equal", uri1, uri2); 2352 } catch (URISyntaxException e1) { 2353 fail("Unexpected URISyntaxException," + e1); 2354 } 2355 } 2356 2357 /** 2358 * @tests java.io.File#toURL() 2359 */ 2360 @TestTargetNew( 2361 level = TestLevel.PARTIAL_COMPLETE, 2362 method = "toURL", 2363 args = {} 2364 ) 2365 public void test_toURL() { 2366 // Test for method java.io.File.toURL() 2367 2368 try { 2369 // Need a directory that exists 2370 File dir = new File(System.getProperty("java.io.tmpdir")); 2371 2372 // Test for toURL when the file is a directory. 2373 String newDirURL = dir.getAbsolutePath(); 2374 newDirURL = newDirURL.replace(File.separatorChar, '/'); 2375 if (newDirURL.startsWith("/")) 2376 newDirURL = "file:" + newDirURL; 2377 else 2378 newDirURL = "file:/" + newDirURL; 2379 if (!newDirURL.endsWith("/")) 2380 newDirURL += '/'; 2381 assertEquals("Test 1: Incorrect URL returned;", newDirURL, 2382 dir.toURL().toString()); 2383 2384 // Test for toURL with a file. 2385 File f = new File(dir, "test.tst"); 2386 String newURL = f.getAbsolutePath(); 2387 newURL = newURL.replace(File.separatorChar, '/'); 2388 if (newURL.startsWith("/")) 2389 newURL = "file:" + newURL; 2390 else 2391 newURL = "file:/" + newURL; 2392 assertEquals("Test 2: Incorrect URL returned;", newURL, 2393 f.toURL().toString()); 2394 } catch (java.net.MalformedURLException e) { 2395 fail("Unexpected java.net.MalformedURLException during test."); 2396 } 2397 } 2398 2399 /** 2400 * @tests java.io.File#toURL() 2401 */ 2402 @TestTargetNew( 2403 level = TestLevel.PARTIAL_COMPLETE, 2404 method = "toURL", 2405 args = {} 2406 ) 2407 public void test_toURL2() { 2408 2409 File f = new File(System.getProperty("java.io.tmpdir"), "a/b/c/../d/e/./f"); 2410 2411 String path = f.getAbsolutePath(); 2412 path = path.replace(File.separatorChar, '/'); 2413 if (!path.startsWith("/")) 2414 path = "/" + path; 2415 2416 try { 2417 URL url1 = new URL("file", "", path); 2418 URL url2 = f.toURL(); 2419 assertEquals("urls not equal", url1, url2); 2420 } catch (MalformedURLException e) { 2421 fail("Unexpected MalformedURLException," + e); 2422 } 2423 } 2424 2425 /** 2426 * @tests java.io.File#toURL() 2427 */ 2428 @TestTargetNew( 2429 level = TestLevel.PARTIAL_COMPLETE, 2430 method = "toURL", 2431 args = {} 2432 ) 2433 @AndroidOnly("Incorrectly fails on the RI.") 2434 public void test_toURL3() throws MalformedURLException { 2435 File dir = new File(""); // current directory 2436 String newDirURL = dir.toURL().toString(); 2437 assertTrue("Test 1: URL does not end with slash.", 2438 newDirURL.endsWith("/")); 2439 } 2440 2441 /** 2442 * @tests java.io.File#deleteOnExit() 2443 */ 2444 @TestTargetNew( 2445 level = TestLevel.PARTIAL_COMPLETE, 2446 notes = "", 2447 method = "deleteOnExit", 2448 args = {} 2449 ) 2450 @AndroidOnly("This test only runs on Android because it instantiates " + 2451 "a second Dalvik VM.") 2452 public void test_deleteOnExit() throws IOException, InterruptedException { 2453 String cts = System.getProperty("java.io.tmpdir"); 2454 File dir = new File(cts + "/hello"); 2455 dir.mkdir(); 2456 assertTrue(dir.exists()); 2457 File subDir = new File(cts + "/hello/world"); 2458 subDir.mkdir(); 2459 assertTrue(subDir.exists()); 2460 2461 Support_Exec.execJava(new String[] { 2462 "tests.support.Support_DeleteOnExitTest", 2463 dir.getAbsolutePath(), subDir.getAbsolutePath() }, 2464 new String[] {}, false); 2465 Thread.sleep(2000); 2466 assertFalse(dir.exists()); 2467 assertFalse(subDir.exists()); 2468 } 2469 2470 /** 2471 * @tests serialization 2472 */ 2473 @TestTargetNew( 2474 level = TestLevel.COMPLETE, 2475 notes = "regression test", 2476 method = "!Serialization", 2477 args = {} 2478 ) 2479 public void test_objectStreamClass_getFields() throws Exception { 2480 //Regression for HARMONY-2674 2481 ObjectStreamClass objectStreamClass = ObjectStreamClass 2482 .lookup(File.class); 2483 ObjectStreamField[] objectStreamFields = objectStreamClass.getFields(); 2484 assertEquals(1, objectStreamFields.length); 2485 ObjectStreamField objectStreamField = objectStreamFields[0]; 2486 assertEquals("path", objectStreamField.getName()); 2487 assertEquals(String.class, objectStreamField.getType()); 2488 } 2489 2490 /** 2491 * Sets up the fixture, for example, open a network connection. This method 2492 * is called before a test is executed. 2493 */ 2494 protected void setUp() throws Exception { 2495 // Make sure that system properties are set correctly 2496 String userDir = System.getProperty("java.io.tmpdir"); 2497 if (userDir == null) 2498 throw new Exception("System property java.io.tmpdir not defined."); 2499 System.setProperty("java.io.tmpdir", userDir); 2500 2501 /** Setup the temporary directory */ 2502 if (!userDir.regionMatches((userDir.length() - 1), slash, 0, 1)) 2503 userDir += slash; 2504 tempDirectory = new File(userDir + "tempDir" 2505 + String.valueOf(System.currentTimeMillis())); 2506 if (!tempDirectory.mkdir()) 2507 System.out.println("Setup for FileTest failed (1)."); 2508 2509 /** Setup the temporary file */ 2510 tempFile = new File(tempDirectory, "tempfile"); 2511 FileOutputStream tempStream; 2512 try { 2513 tempStream = new FileOutputStream(tempFile.getPath(), false); 2514 tempStream.close(); 2515 } catch (IOException e) { 2516 System.out.println("Setup for FileTest failed (2)."); 2517 return; 2518 } 2519 } 2520 2521 /** 2522 * Tears down the fixture, for example, close a network connection. This 2523 * method is called after a test is executed. 2524 */ 2525 protected void tearDown() { 2526 if (tempFile.exists() && !tempFile.delete()) 2527 System.out 2528 .println("FileTest.tearDown() failed, could not delete file!"); 2529 if (!tempDirectory.delete()) 2530 System.out 2531 .println("FileTest.tearDown() failed, could not delete directory!"); 2532 } 2533} 2534