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