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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package tests.api.java.io;
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;
31 import java.net.URISyntaxException;
34 import tests.support.Support_Exec;
35 import dalvik.annotation.AndroidOnly;
36 import dalvik.annotation.KnownFailure;
37 import dalvik.annotation.TestLevel;
38 import dalvik.annotation.TestTargetClass;
39 import dalvik.annotation.TestTargetNew;
40 import dalvik.annotation.TestTargets;
42 @TestTargetClass(File.class)
43 public class FileTest extends junit.framework.TestCase {
45 /** Location to store tests in */
46 private File tempDirectory;
48 /** Temp file that does exist */
49 private File tempFile;
52 private String slash = File.separator;
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";
56 private static String platformId = "Android"
57 + System.getProperty("java.vm.version").replace('.', '-');
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"))
69 if (files[i].startsWith("hyts_") || files[i].startsWith("hyjar_"))
70 new File(tempDir, files[i]).delete();
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]);
89 * @tests java.io.File#File(java.io.File, java.lang.String)
92 level = TestLevel.PARTIAL_COMPLETE,
94 args = {java.io.File.class, java.lang.String.class}
96 public void test_ConstructorLjava_io_FileLjava_lang_String() throws Exception {
98 String dirName = System.getProperty("java.io.tmpdir");
99 String oldUserDir = System.getProperty("user.dir");
100 System.setProperty("user.dir", dirName);
102 File d = new File(dirName);
103 File f = new File(d, "input.tst");
104 if (!dirName.regionMatches((dirName.length() - 1), slash, 0, 1))
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));
110 String fileName = null;
112 f = new File(d, fileName);
113 fail("Test 2: NullPointerException expected.");
114 } catch (NullPointerException e) {
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));
123 // Regression test for Harmony-382
125 f = new File("/abc");
126 d = new File(s, "/abc");
127 assertEquals("Test 4: Incorrect file created;",
128 f.getAbsolutePath(), d.getAbsolutePath());
130 System.setProperty("user.dir", oldUserDir);
134 * @tests java.io.File#File(java.lang.String)
137 level = TestLevel.COMPLETE,
138 notes = "Verifies File(java.lang.String) constructor.",
140 args = {java.lang.String.class}
142 public void test_ConstructorLjava_lang_String() {
143 // Test for method java.io.File(java.lang.String)
144 String fileName = null;
147 fail("Test 1: NullPointerException expected.");
148 } catch (NullPointerException e) {
151 fileName = System.getProperty("java.io.tmpdir");
152 if (!fileName.regionMatches((fileName.length() - 1), slash, 0, 1))
154 fileName += "input.tst";
156 File f = new File(fileName);
157 assertTrue("Created incorrect file " + f.getPath(), f.getPath().equals(
162 * @tests java.io.File#File(java.lang.String, java.lang.String)
165 level = TestLevel.PARTIAL_COMPLETE,
167 args = {java.lang.String.class, java.lang.String.class}
169 public void test_ConstructorLjava_lang_StringLjava_lang_String() {
171 String dirName = null;
172 String fileName = "input.tst";
174 String userDir = System.getProperty("java.io.tmpdir");
175 String oldUserDir = System.getProperty("user.dir");
176 System.setProperty("user.dir", userDir);
178 File f = new File(dirName, fileName);
179 if (!userDir.regionMatches((userDir.length() - 1), slash, 0, 1))
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));
186 dirName = System.getProperty("java.io.tmpdir");
189 f = new File(dirName, fileName);
190 fail("Test 2: NullPointerException expected.");
191 } catch (NullPointerException e) {
195 fileName = "input.tst";
196 f = new File(dirName, fileName);
197 assertTrue("Test 3: Incorrect file created.", f.getPath()
200 // Regression test for Harmony-382
202 f = new File("/abc");
203 File d = new File(s, "/abc");
204 assertEquals("Test 4: Incorrect file created;", d.getAbsolutePath(), f
206 assertEquals("Test3: Created Incorrect File", "/abc", f
209 System.setProperty("user.dir", oldUserDir);
213 * @tests java.io.File#File(java.lang.String, java.lang.String)
216 level = TestLevel.PARTIAL_COMPLETE,
218 args = {java.lang.String.class, java.lang.String.class}
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");
225 File file1 = new File("/", "/dir1/file1");
226 assertEquals("wrong result 1: " + file1, ref1.getPath(), file1
228 File file2 = new File("/", "//dir1/file1");
229 assertTrue("wrong result 2: " + file2, file2.getPath().equals(
231 File file3 = new File("\\", "\\dir1\\file1");
232 assertTrue("wrong result 3: " + file3, file3.getPath().equals(
234 File file4 = new File("\\", "\\\\dir1\\file1");
235 assertTrue("wrong result 4: " + file4, file4.getPath().equals(
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(
246 * @tests java.io.File#File(java.io.File, java.lang.String)
249 level = TestLevel.PARTIAL_COMPLETE,
251 args = {java.io.File.class, java.lang.String.class}
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");
258 File root = new File("/");
259 File file1 = new File(root, "/dir1/file1");
260 assertTrue("wrong result 1: " + file1, file1.getPath().equals(
262 File file2 = new File(root, "//dir1/file1");
263 assertTrue("wrong result 2: " + file2, file2.getPath().equals(
265 File file3 = new File(root, "\\dir1\\file1");
266 assertTrue("wrong result 3: " + file3, file3.getPath().equals(
268 File file4 = new File(root, "\\\\dir1\\file1");
269 assertTrue("wrong result 4: " + file4, file4.getPath().equals(
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(
279 * @tests java.io.File#File(java.net.URI)
282 level = TestLevel.COMPLETE,
283 notes = "Verifies File(java.net.URI) constructor with incorrect parameter.",
285 args = {java.net.URI.class}
287 public void test_ConstructorLjava_net_URI() {
288 // Test for method java.io.File(java.net.URI)
292 fail("NullPointerException Not Thrown.");
293 } catch (NullPointerException e) {
297 String[] uris = new String[] { "mailto:user@domain.com", // not
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#" };
306 for (int i = 0; i < uris.length; i++) {
308 uri = new URI(uris[i]);
309 } catch (URISyntaxException e) {
310 fail("Unexpected exception:" + e);
314 fail("Expected IllegalArgumentException for new File(" + uri
316 } catch (IllegalArgumentException e) {
322 File f = new File(new URI("file:///pa%20th/another\u20ac/pa%25th"));
323 assertTrue("Created incorrect File " + f.getPath(), f.getPath()
325 slash + "pa th" + slash + "another\u20ac" + slash
327 } catch (URISyntaxException e) {
328 fail("Unexpected exception:" + e);
329 } catch (IllegalArgumentException e) {
330 fail("Unexpected exception:" + e);
335 * @tests java.io.File#canRead()
338 level = TestLevel.PARTIAL_COMPLETE,
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
348 FileOutputStream fos = new FileOutputStream(f);
350 assertTrue("canRead returned false", f.canRead());
352 } catch (IOException e) {
353 fail("Unexpected IOException: " + e.getMessage());
360 * @tests java.io.File#canWrite()
363 level = TestLevel.PARTIAL_COMPLETE,
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
373 FileOutputStream fos = new FileOutputStream(f);
375 assertTrue("canWrite returned false", f.canWrite());
376 } catch (IOException e) {
377 fail("Unexpected IOException: " + e.getMessage());
384 * @tests java.io.File#compareTo(java.io.File)
387 level = TestLevel.COMPLETE,
388 notes = "Verifies compareTo(java.io.File) method.",
389 method = "compareTo",
390 args = {java.io.File.class}
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
398 assertTrue("f3.compareTo(f1) did not result in value < 0", f3
400 assertTrue("f1.compareTo(f3) did not result in vale > 0", f1
405 * @tests java.io.File#createNewFile()
408 level = TestLevel.PARTIAL_COMPLETE,
409 notes = "IOException checked.",
410 method = "createNewFile",
413 public void test_createNewFile_EmptyString() {
414 File f = new File("");
417 fail("should throw IOException");
418 } catch (IOException e) {
424 * @tests java.io.File#createNewFile()
427 level = TestLevel.PARTIAL_COMPLETE,
428 method = "createNewFile",
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;
436 File dir = new File(base, String.valueOf(numDir));
437 // Making sure that the directory does not exist.
439 // If the directory exists, add one to the directory number
441 // it a new directory name.)
444 dir = new File(base, String.valueOf(numDir));
450 // Test for trying to create a file in a directory that does not
453 // Try to create a file in a directory that does not exist
454 File f1 = new File(dir, "tempfile.tst");
456 fail("Test 1: IOException expected.");
457 } catch (IOException e) {
463 File f1 = new File(dir, "tempfile.tst");
464 File f2 = new File(dir, "tempfile.tst");
468 assertFalse("Test 2: File should not exist.", f1.isFile());
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))
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());
480 // Test for creating a file that already exists.
481 assertFalse("Test 7: File already exists, createNewFile should " +
482 "return false.", f2.createNewFile());
484 // Trying to create an illegal file.
485 String sep = File.separator;
486 f1 = new File(sep+"a"+sep+".."+sep+".."+sep);
489 fail("Test 8: IOException expected.");
490 } catch (IOException e) {
495 assertFalse("Test 9: False expected when trying to create an " +
496 "existing file", f1.createNewFile());
500 * @tests java.io.File#createTempFile(java.lang.String, java.lang.String)
503 level = TestLevel.PARTIAL_COMPLETE,
504 method = "createTempFile",
505 args = {java.lang.String.class, java.lang.String.class}
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 "."?
515 f1 = File.createTempFile("hyts_abc", ".tmp");
516 f2 = File.createTempFile("hyts_tf", null);
517 String fileLocation = f1.getParent();
518 if (!fileLocation.endsWith(slash))
520 fileLocation += slash;
521 String tempDir = System.getProperty("java.io.tmpdir");
522 if (!tempDir.endsWith(slash))
525 "File did not save to the default temporary-file location.",
526 fileLocation.equals(tempDir));
528 // Test to see if correct suffix was used to create the tempfile.
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
539 // Tests to see if the correct prefix was used to create the
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"));
548 // Tests for creating a tempfile with a filename shorter than 3
551 File f3 = File.createTempFile("ab", ".tst");
553 fail("IllegalArgumentException Not Thrown.");
554 } catch (IllegalArgumentException e) {
557 File f3 = File.createTempFile("a", ".tst");
559 fail("IllegalArgumentException Not Thrown.");
560 } catch (IllegalArgumentException e) {
563 File f3 = File.createTempFile("", ".tst");
565 fail("IllegalArgumentException Not Thrown.");
566 } catch (IllegalArgumentException e) {
569 // Providing an illegal file prefix.
570 File f3 = File.createTempFile("/../../../../../", null);
572 fail("IOException not thrown");
573 } catch (IOException e) {
576 } catch (IOException e) {
577 fail("Unexpected IOException: " + e.getMessage());
587 * @tests java.io.File#createTempFile(java.lang.String, java.lang.String,
591 level = TestLevel.PARTIAL_COMPLETE,
592 method = "createTempFile",
593 args = {java.lang.String.class, java.lang.String.class, java.io.File.class}
595 public void test_createTempFileLjava_lang_StringLjava_lang_StringLjava_io_File() {
596 // Test for method java.io.File.createTempFile(String, String, File)
599 String base = System.getProperty("java.io.tmpdir");
602 // Test to make sure that the tempfile was saved in the correct
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);
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))
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
624 assertTrue("File created with incorrect prefix.", fileName
625 .startsWith("hyts_tf"));
626 currentFile.delete();
629 // Test for creating a tempfile in a directory that does not exist.
631 boolean dirExists = true;
632 // Set dir to a non-existent directory inside the temporary
634 dir = new File(base, String.valueOf(dirNumber));
635 // Making sure that the directory does not exist.
637 // If the directory exists, add one to the directory number
639 // a new directory name.)
642 dir = new File(base, String.valueOf(dirNumber));
648 // Try to create a file in a directory that does not exist
649 File f3 = File.createTempFile("hyts_tf", null, dir);
651 fail("IOException not thrown");
652 } catch (IOException e) {
656 // Tests for creating a tempfile with a filename shorter than 3
659 File f4 = File.createTempFile("ab", null, null);
661 fail("IllegalArgumentException not thrown.");
662 } catch (IllegalArgumentException e) {
665 File f4 = File.createTempFile("a", null, null);
667 fail("IllegalArgumentException not thrown.");
668 } catch (IllegalArgumentException e) {
671 File f4 = File.createTempFile("", null, null);
673 fail("IllegalArgumentException not thrown.");
674 } catch (IllegalArgumentException e) {
677 } catch (IOException e) {
678 fail("Unexpected IOException: " + e.getMessage());
688 * @tests java.io.File#delete()
691 level = TestLevel.PARTIAL_COMPLETE,
695 public void test_delete() {
696 // Test for method boolean java.io.File.delete()
698 File dir = new File(System.getProperty("java.io.tmpdir"), platformId
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);
706 assertTrue("Error Creating File For Delete Test", f.exists());
708 assertTrue("Directory Should Not Have Been Deleted.", dir.exists());
710 assertTrue("File Was Not Deleted", !f.exists());
712 assertTrue("Directory Was Not Deleted", !dir.exists());
713 } catch (IOException e) {
714 fail("Unexpected IOException During Delete Test : "
720 * @tests java.io.File#equals(java.lang.Object)
723 level = TestLevel.COMPLETE,
724 notes = "Verifies equals() method.",
726 args = {java.lang.Object.class}
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");
734 assertTrue("Equality test failed", f1.equals(f2));
735 assertTrue("Files Should Not Return Equal.", !f1.equals(f3));
737 f3 = new File("FiLeChK.tst");
738 boolean onWindows = File.separatorChar == '\\';
739 boolean onUnix = File.separatorChar == '/';
741 assertTrue("Files Should Return Equal.", f1.equals(f3));
743 assertTrue("Files Should NOT Return Equal.", !f1.equals(f3));
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
751 new FileInputStream(f2);
752 } catch (IOException e) {
753 fail("File system is case sensitive");
756 boolean exception = false;
758 new FileInputStream(f2);
759 } catch (IOException e) {
762 assertTrue("File system is case insensitive", exception);
765 } catch (IOException e) {
766 fail("Unexpected using case sensitive test : " + e.getMessage());
771 * @tests java.io.File#exists()
774 level = TestLevel.PARTIAL_COMPLETE,
778 public void test_exists() {
779 // Test for method boolean java.io.File.exists()
781 File f = new File(System.getProperty("java.io.tmpdir"), platformId
783 assertTrue("Exists returned true for non-existent file", !f
785 FileOutputStream fos = new FileOutputStream(f);
787 assertTrue("Exists returned false file", f.exists());
789 } catch (IOException e) {
790 fail("Unexpected IOException During Test : " + e.getMessage());
795 * @tests java.io.File#getAbsoluteFile()
798 level = TestLevel.PARTIAL_COMPLETE,
799 method = "getAbsoluteFile",
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))
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()));
824 * @tests java.io.File#getAbsolutePath()
827 level = TestLevel.PARTIAL_COMPLETE,
828 method = "getAbsolutePath",
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))
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
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"));
851 * @tests java.io.File#getCanonicalFile()
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",
860 public void test_getCanonicalFile() {
861 // Test for method java.io.File.getCanonicalFile()
863 String base = System.getProperty("java.io.tmpdir");
864 if (!base.endsWith(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
876 f2 = f.getCanonicalFile();
877 assertEquals("Test 3: Incorrect file returned.", 0, f2
878 .getCanonicalFile().compareTo(f.getCanonicalFile()));
880 // Test for when long directory/file names in Windows
881 boolean onWindows = File.separatorChar == '\\';
882 // String userDir = System.getProperty("java.io.tmpdir");
884 File testdir = new File(base, "long-" + platformId);
886 File dir = new File(testdir, "longdirectory" + platformId);
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);
896 f2 = new File(testdir + slash + "longdi~1" + slash
898 File canonicalf2 = f2.getCanonicalFile();
900 * If the "short file name" doesn't exist, then assume that
901 * the 8.3 file name compatibility is disabled.
903 if (canonicalf2.exists()) {
904 assertTrue("Test 5: Incorrect file returned: "
905 + canonicalf2, canonicalf2.compareTo(f
906 .getCanonicalFile()) == 0);
915 } catch (IOException e) {
916 fail ("Unexpected IOException during Test : " + e.getMessage());
921 * @tests java.io.File#getCanonicalPath()
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",
930 public void test_getCanonicalPath() {
931 // Test for method java.lang.String java.io.File.getCanonicalPath()
932 // Should work for Unix/Windows.
936 String tmpDir = System.getProperty("java.io.tmpdir");
937 String oldUserDir = System.getProperty("user.dir");
938 System.setProperty("user.dir", tmpDir);
940 String base = new File(tmpDir).getCanonicalPath();
941 if (!base.regionMatches((base.length() - 1), slash, 0, 1))
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());
950 // Finding a non-existent directory for tests 3 and 4
951 // This is necessary because getCanonicalPath is case sensitive and
953 // cause a failure in the test if the directory exists but with
955 // case letters (e.g "Temp" and "temp")
957 boolean dirExists = true;
958 File dir1 = new File(base, String.valueOf(dirNumber));
962 dir1 = new File(base, String.valueOf(dirNumber));
967 f = new File(base + dirNumber + slash + dots + slash + dirNumber + slash
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());
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));
982 } catch (IOException e) {
983 fail("Unexpected IOException During Test : " + e.getMessage());
985 System.setProperty("user.dir", oldUserDir);
990 * @tests java.io.File#getName()
993 level = TestLevel.COMPLETE,
994 notes = "Verifies getName() method.",
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());
1005 assertTrue("Test 2: Returned incorrect name", f.getName().equals(""));
1011 * @tests java.io.File#getParent()
1014 level = TestLevel.COMPLETE,
1015 notes = "Verifies getParent() method.",
1016 method = "getParent",
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");
1023 throw new Exception("System property java.io.tmpdir not defined.");
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));
1032 } catch (Exception e) {
1033 fail("Unexpected exception during tests : " + e.getMessage());
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());
1044 assertNull("Wrong parent test 4", f1.getParent());
1045 f1 = new File("directory");
1046 assertNull("Wrong parent test 5", f1.getParent());
1048 if (File.separatorChar == '\\' && new File("d:/").isAbsolute()) {
1049 f1 = new File("d:/directory");
1050 assertTrue("Wrong parent test 1a", f1.getParent().equals(
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());
1066 * @tests java.io.File#getParentFile()
1069 level = TestLevel.COMPLETE,
1070 notes = "Verifies getParentFile() method.",
1071 method = "getParentFile",
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);
1091 * @tests java.io.File#getPath()
1094 level = TestLevel.COMPLETE,
1095 notes = "Verifies getPath() method.",
1099 public void test_getPath() {
1100 System.setProperty("user.home", System.getProperty("java.io.tmpdir"));
1101 String base = System.getProperty("user.home");
1104 if (!base.regionMatches((base.length() - 1), slash, 0, 1))
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
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
1120 base + "a" + slash + ".." + slash + "filechk.tst"));
1128 * @tests java.io.File#isAbsolute()
1131 level = TestLevel.COMPLETE,
1132 notes = "Verifies isAbsolute() method.",
1133 method = "isAbsolute",
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
1144 || (!f.isAbsolute() && f1.isAbsolute()));
1146 File f = new File("/test");
1147 assertTrue("Absolute returned false", f.isAbsolute());
1149 assertTrue("Non-Absolute returned true", !new File("../test")
1154 * @tests java.io.File#isDirectory()
1157 level = TestLevel.PARTIAL_COMPLETE,
1158 method = "isDirectory",
1161 public void test_isDirectory() {
1162 // Test for method boolean java.io.File.isDirectory()
1164 String base = System.getProperty("java.io.tmpdir");
1165 if (!base.regionMatches((base.length() - 1), slash, 0, 1))
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
1174 assertTrue("Test 3: Directory returned false.", f.isDirectory());
1181 * @tests java.io.File#isFile()
1184 level = TestLevel.PARTIAL_COMPLETE,
1188 public void test_isFile() {
1189 // Test for method boolean java.io.File.isFile()
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))
1196 f = new File(base, platformId + "amiafile");
1197 assertTrue("Non-existent file returned true", !f.isFile());
1198 FileOutputStream fos = new FileOutputStream(f);
1200 assertTrue("File returned false", f.isFile());
1202 } catch (IOException e) {
1203 fail("IOException during isFile " + e.getMessage());
1208 * @tests java.io.File#isHidden()
1211 level = TestLevel.PARTIAL_COMPLETE,
1212 method = "isHidden",
1215 public void test_isHidden() {
1216 // Test for method boolean java.io.File.isHidden()
1217 boolean onUnix = File.separatorChar == '/';
1220 File f = File.createTempFile("hyts_", ".tmp");
1221 // On Unix hidden files are marked with a "." at the beginning
1222 // of the file name.
1224 File f2 = new File(System.getProperty("java.io.tmpdir"), ".test.tst" + platformId);
1225 FileOutputStream fos2 = new FileOutputStream(f2);
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());
1231 // For windows, the file is being set hidden by the attrib
1233 Runtime r = Runtime.getRuntime();
1234 assertTrue("File returned hidden", !f.isHidden());
1235 Process p = r.exec("attrib +h \"" + f.getAbsolutePath() + "\"");
1237 assertTrue("File returned visible", f.isHidden());
1238 p = r.exec("attrib -h \"" + f.getAbsolutePath() + "\"");
1240 assertTrue("File returned hidden", !f.isHidden());
1243 } catch (IOException e) {
1244 fail("Unexpected IOException during test : " + e.getMessage());
1245 } catch (InterruptedException e) {
1246 fail("Unexpected InterruptedException during test : "
1252 * @tests java.io.File#lastModified()
1255 level = TestLevel.PARTIAL_COMPLETE,
1256 method = "lastModified",
1259 public void test_lastModified() {
1260 // Test for method long java.io.File.lastModified()
1262 File f = new File(System.getProperty("java.io.tmpdir"), platformId
1265 long lastModifiedTime = f.lastModified();
1266 assertEquals("LastModified Time Should Have Returned 0.",
1267 0, lastModifiedTime);
1268 FileOutputStream fos = new FileOutputStream(f);
1270 f.setLastModified(315550800000L);
1271 lastModifiedTime = f.lastModified();
1272 assertTrue("LastModified Time Incorrect: " + lastModifiedTime,
1273 lastModifiedTime == 315550800000L);
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());
1285 * @tests java.io.File#length()
1288 level = TestLevel.PARTIAL_COMPLETE,
1292 public void test_length() throws Exception {
1293 // Test for method long java.io.File.length()
1295 File f = new File(System.getProperty("java.io.tmpdir"), platformId
1297 assertEquals("File length should have returned 0.", 0, f.length());
1298 FileOutputStream fos = new FileOutputStream(f);
1299 fos.write(fileString.getBytes());
1301 assertTrue("Incorrect file length returned: " + f.length(), f
1302 .length() == fileString.length());
1304 } catch (IOException e) {
1305 fail("Unexpected IOException during test : " + e.getMessage());
1308 // regression test for Harmony-1497
1309 File f = File.createTempFile("cts", ".tmp");
1311 RandomAccessFile raf = new RandomAccessFile(f, "rwd");
1313 assertEquals(1, f.length());
1317 * @tests java.io.File#list()
1320 level = TestLevel.PARTIAL_COMPLETE,
1324 public void test_list() {
1325 // Test for method java.lang.String [] java.io.File.list()
1327 String base = System.getProperty("java.io.tmpdir");
1328 // Old test left behind "garbage files" so this time it creates a
1330 // that is guaranteed not to already exist (and deletes it afterward.)
1332 boolean dirExists = true;
1334 dir = new File(base, platformId + String.valueOf(dirNumber));
1338 dir = new File(base, String.valueOf(dirNumber));
1344 String[] flist = dir.list();
1346 assertNull("Method list() Should Have Returned null.", flist);
1348 assertTrue("Could not create parent directory for list test", dir
1351 String[] files = { "mtzz1.xx", "mtzz2.xx", "mtzz3.yy", "mtzz4.yy" };
1353 assertEquals("Method list() Should Have Returned An Array Of Length 0.",
1354 0, dir.list().length);
1356 File file = new File(dir, "notADir.tst");
1358 FileOutputStream fos = new FileOutputStream(file);
1361 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.",
1363 } catch (IOException e) {
1364 fail("Unexpected IOException during test : " + e.getMessage());
1370 for (int i = 0; i < files.length; i++) {
1371 File f = new File(dir, files[i]);
1372 FileOutputStream fos = new FileOutputStream(f);
1375 } catch (IOException e) {
1376 fail("Unexpected IOException during test : " + e.getMessage());
1380 if (flist.length != files.length) {
1381 fail("Incorrect list returned");
1384 // Checking to make sure the correct files were are listed in the
1386 boolean[] check = new boolean[flist.length];
1387 for (int i = 0; i < check.length; i++)
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])) {
1398 for (int i = 0; i < check.length; i++) {
1399 if (check[i] == false)
1402 assertEquals("Invalid file returned in listing", 0, checkCount);
1404 for (int i = 0; i < files.length; i++) {
1405 File f = new File(dir, files[i]);
1409 assertTrue("Could not delete parent directory for list test.", dir
1412 for (int i = 0; i < files.length; i++) {
1413 File f = new File(dir, files[i]);
1422 * @tests java.io.File#listFiles()
1425 level = TestLevel.PARTIAL_COMPLETE,
1426 method = "listFiles",
1429 public void test_listFiles() {
1430 // Test for method java.io.File.listFiles()
1433 String base = System.getProperty("java.io.tmpdir");
1434 // Finding a non-existent directory to create.
1436 boolean dirExists = true;
1437 File dir = new File(base, platformId + String.valueOf(dirNumber));
1438 // Making sure that the directory does not exist.
1440 // If the directory exists, add one to the directory number
1442 // it a new directory name.)
1445 dir = new File(base, String.valueOf(dirNumber));
1450 // Test for attempting to cal listFiles on a non-existent directory.
1451 assertNull("listFiles Should Return Null.", dir.listFiles());
1453 assertTrue("Failed To Create Parent Directory.", dir.mkdir());
1455 String[] files = { "1.tst", "2.tst", "3.tst", "" };
1457 assertEquals("listFiles Should Return An Array Of Length 0.", 0, dir
1458 .listFiles().length);
1460 File file = new File(dir, "notADir.tst");
1462 FileOutputStream fos = new FileOutputStream(file);
1465 "listFiles Should Have Returned Null When Used On A File Instead Of A Directory.",
1467 } catch (IOException e) {
1468 fail("Unexpected IOException during test : " + e.getMessage());
1473 for (int i = 0; i < (files.length - 1); i++) {
1474 File f = new File(dir, files[i]);
1475 FileOutputStream fos = new FileOutputStream(f);
1479 new File(dir, "doesNotExist.tst");
1480 File[] flist = dir.listFiles();
1482 // Test to make sure that only the 3 files that were created are
1484 assertEquals("Incorrect Number Of Files Returned.",
1487 // Test to make sure that listFiles can read hidden files.
1488 boolean onUnix = File.separatorChar == '/';
1489 boolean onWindows = File.separatorChar == '\\';
1492 File f = new File(dir, "4.tst");
1493 FileOutputStream fos = new FileOutputStream(f);
1495 Runtime r = Runtime.getRuntime();
1496 Process p = r.exec("attrib +h \"" + f.getPath() + "\"");
1500 files[3] = ".4.tst";
1501 File f = new File(dir, ".4.tst");
1502 FileOutputStream fos = new FileOutputStream(f);
1505 flist = dir.listFiles();
1506 assertEquals("Incorrect Number Of Files Returned.",
1509 // Checking to make sure the correct files were are listed in
1511 boolean[] check = new boolean[flist.length];
1512 for (int i = 0; i < check.length; i++)
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])) {
1523 for (int i = 0; i < check.length; i++) {
1524 if (check[i] == false)
1527 assertEquals("Invalid file returned in listing", 0, checkCount);
1530 Runtime r = Runtime.getRuntime();
1531 Process p = r.exec("attrib -h \""
1532 + new File(dir, files[3]).getPath() + "\"");
1536 for (int i = 0; i < files.length; i++) {
1537 File f = new File(dir, files[i]);
1540 assertTrue("Parent Directory Not Deleted.", dir.delete());
1542 for (int i = 0; i < files.length; i++) {
1543 File f = new File(dir, files[i]);
1548 } catch (IOException e) {
1549 fail("Unexpected IOException during test : " + e.getMessage());
1550 } catch (InterruptedException e) {
1551 fail("Unexpected InterruptedException during test : " + e.getMessage());
1556 * @tests java.io.File#listFiles(java.io.FileFilter)
1559 level = TestLevel.PARTIAL_COMPLETE,
1560 method = "listFiles",
1561 args = {java.io.FileFilter.class}
1563 public void test_listFilesLjava_io_FileFilter() {
1564 // Test for method java.io.File.listFiles(File Filter filter)
1566 String base = System.getProperty("java.io.tmpdir");
1567 // Finding a non-existent directory to create.
1569 boolean dirExists = true;
1570 File baseDir = new File(base, platformId + String.valueOf(dirNumber));
1571 // Making sure that the directory does not exist.
1573 // If the directory exists, add one to the directory number (making
1574 // it a new directory name.)
1575 if (baseDir.exists()) {
1577 baseDir = new File(base, String.valueOf(dirNumber));
1583 // Creating a filter that catches directories.
1584 FileFilter dirFilter = new FileFilter() {
1585 public boolean accept(File f) {
1586 if (f.isDirectory())
1593 assertNull("listFiles Should Return Null.", baseDir
1594 .listFiles(dirFilter));
1596 assertTrue("Failed To Create Parent Directory.", baseDir.mkdir());
1599 String[] files = { "1.tst", "2.tst", "3.tst" };
1601 assertEquals("listFiles Should Return An Array Of Length 0.", 0, baseDir
1602 .listFiles(dirFilter).length);
1604 File file = new File(baseDir, "notADir.tst");
1606 FileOutputStream fos = new FileOutputStream(file);
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.");
1618 for (int i = 0; i < files.length; i++) {
1619 File f = new File(baseDir, files[i]);
1620 FileOutputStream fos = new FileOutputStream(f);
1623 } catch (IOException e) {
1624 fail("Unexpected IOException during test : " + e.getMessage());
1626 dir1 = new File(baseDir, "Temp1");
1629 // Creating a filter that catches files.
1630 FileFilter fileFilter = new FileFilter() {
1631 public boolean accept(File f) {
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);
1644 // Test to see if the directory was saved with the correct name.
1645 assertEquals("Incorrect Directory Returned.", 0, directories[0]
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);
1653 // Checking to make sure the correct files were are listed in the
1655 boolean[] check = new boolean[flist.length];
1656 for (int i = 0; i < check.length; i++)
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])) {
1667 for (int i = 0; i < check.length; i++) {
1668 if (check[i] == false)
1671 assertEquals("Invalid file returned in listing", 0, checkCount);
1673 for (int i = 0; i < files.length; i++) {
1674 File f = new File(baseDir, files[i]);
1678 assertTrue("Parent Directory Not Deleted.", baseDir.delete());
1680 for (int i = 0; i < files.length; i++) {
1681 File f = new File(baseDir, files[i]);
1691 * @tests java.io.File#listFiles(java.io.FilenameFilter)
1694 level = TestLevel.PARTIAL_COMPLETE,
1695 method = "listFiles",
1696 args = {java.io.FilenameFilter.class}
1698 public void test_listFilesLjava_io_FilenameFilter() {
1699 // Test for method java.io.File.listFiles(FilenameFilter filter)
1701 String base = System.getProperty("java.io.tmpdir");
1702 // Finding a non-existent directory to create.
1704 boolean dirExists = true;
1705 File dir = new File(base, platformId + String.valueOf(dirNumber));
1706 // Making sure that the directory does not exist.
1708 // If the directory exists, add one to the directory number (making
1709 // it a new directory name.)
1712 dir = new File(base, platformId + String.valueOf(dirNumber));
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"))
1729 assertNull("listFiles Should Return Null.",
1730 dir.listFiles(tstFilter));
1732 assertTrue("Failed To Create Parent Directory.", dir.mkdir());
1734 String[] files = { "1.tst", "2.tst", "3.tmp" };
1736 assertEquals("listFiles Should Return An Array Of Length 0.", 0, dir
1737 .listFiles(tstFilter).length);
1739 File file = new File(dir, "notADir.tst");
1741 FileOutputStream fos = new FileOutputStream(file);
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());
1753 for (int i = 0; i < files.length; i++) {
1754 File f = new File(dir, files[i]);
1755 FileOutputStream fos = new FileOutputStream(f);
1758 } catch (IOException e) {
1759 fail("Unexpected IOException During Test : " + e.getMessage());
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"))
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.",
1777 for (int i = 0; i < flist.length; i++)
1778 assertTrue("File Should Not Have Passed The tstFilter.",
1779 flist[i].getPath().endsWith(".tst"));
1781 flist = dir.listFiles(tmpFilter);
1782 assertEquals("Incorrect Number Of Files Passed Through tmpFilter.",
1784 assertTrue("File Should Not Have Passed The tmpFilter.", flist[0]
1785 .getPath().endsWith(".tmp"));
1787 for (int i = 0; i < files.length; i++) {
1788 File f = new File(dir, files[i]);
1791 assertTrue("Parent Directory Not Deleted.", dir.delete());
1793 for (int i = 0; i < files.length; i++) {
1794 File f = new File(dir, files[i]);
1802 * @tests java.io.File#list(java.io.FilenameFilter)
1805 level = TestLevel.PARTIAL_COMPLETE,
1807 args = {java.io.FilenameFilter.class}
1809 public void test_listLjava_io_FilenameFilter() {
1810 // Test for method java.lang.String []
1811 // java.io.File.list(java.io.FilenameFilter)
1813 String base = System.getProperty("java.io.tmpdir");
1814 // Old test left behind "garbage files" so this time it creates a
1816 // that is guaranteed not to already exist (and deletes it afterward.)
1818 boolean dirExists = true;
1819 File dir = new File(base, platformId + String.valueOf(dirNumber));
1823 dir = new File(base, String.valueOf(dirNumber));
1829 FilenameFilter filter = new FilenameFilter() {
1830 public boolean accept(File dir, String name) {
1831 return !name.equals("mtzz1.xx");
1835 String[] flist = dir.list(filter);
1836 assertNull("Method list(FilenameFilter) Should Have Returned Null.",
1839 assertTrue("Could not create parent directory for test", dir.mkdir());
1841 String[] files = { "mtzz1.xx", "mtzz2.xx", "mtzz3.yy", "mtzz4.yy" };
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.
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();
1857 flist = dir.list(filter);
1858 assertEquals("Array Of Length 0 Should Have Returned.",
1862 for (int i = 0; i < files.length; i++) {
1863 File f = new File(dir, files[i]);
1864 FileOutputStream fos = new FileOutputStream(f);
1867 } catch (IOException e) {
1868 fail("Unexpected IOException during test : " + e.getMessage());
1871 flist = dir.list(filter);
1873 if (flist.length != files.length - 1) {
1874 fail("Incorrect list returned");
1877 // Checking to make sure the correct files were are listed in the
1879 boolean[] check = new boolean[flist.length];
1880 for (int i = 0; i < check.length; i++)
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])) {
1892 for (int i = 0; i < check.length; i++) {
1893 if (check[i] == false)
1896 assertEquals("Invalid file returned in listing", 0, checkCount);
1898 for (int i = 0; i < files.length; i++) {
1899 File f = new File(dir, files[i]);
1902 assertTrue("Could not delete parent directory for test.", dir
1905 for (int i = 0; i < files.length; i++) {
1906 File f = new File(dir, files[i]);
1914 * @tests java.io.File#listRoots()
1917 level = TestLevel.COMPLETE,
1918 notes = "Verifies listRoots() method.",
1919 method = "listRoots",
1922 public void test_listRoots() {
1923 // Test for method java.io.File.listRoots()
1925 File[] roots = File.listRoots();
1926 boolean onUnix = File.separatorChar == '/';
1927 boolean onWindows = File.separatorChar == '\\';
1929 assertEquals("Incorrect Number Of Root Directories.",
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.",
1942 * @tests java.io.File#mkdir()
1945 level = TestLevel.PARTIAL_COMPLETE,
1949 public void test_mkdir() throws IOException {
1950 // Test for method boolean java.io.File.mkdir()
1952 String base = System.getProperty("java.io.tmpdir");
1953 // Old test left behind "garbage files" so this time it creates a
1955 // that is guaranteed not to already exist (and deletes it afterward.)
1957 boolean dirExists = true;
1958 File dir = new File(base, String.valueOf(dirNumber));
1962 dir = new File(base, String.valueOf(dirNumber));
1968 assertTrue("mkdir failed", dir.mkdir() && dir.exists());
1971 String longDirName = "abcdefghijklmnopqrstuvwx";// 24 chars
1972 StringBuilder sb = new StringBuilder(dir + File.separator);
1973 StringBuilder sb2 = new StringBuilder(dir + File.separator);
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());
1983 while (dir.getCanonicalPath().length() < 256) {
1985 dir = new File(sb.toString());
1986 assertTrue("mkdir " + dir.getCanonicalPath().length() + " failed",
1987 dir.mkdir() && dir.exists());
1991 // Test make many paths
1992 while (dir.getCanonicalPath().length() < 256) {
1994 dir = new File(sb2.toString());
1995 assertTrue("mkdir " + dir.getCanonicalPath().length() + " failed",
1996 dir.mkdir() && dir.exists());
2002 * @tests java.io.File#mkdirs()
2005 level = TestLevel.PARTIAL_COMPLETE,
2009 public void test_mkdirs() {
2010 // Test for method boolean java.io.File.mkdirs()
2012 String userHome = System.getProperty("java.io.tmpdir");
2013 if (!userHome.endsWith(slash))
2015 File f = new File(userHome + "mdtest" + platformId + slash + "mdtest2",
2017 File g = new File(userHome + "mdtest" + platformId + slash + "mdtest2");
2018 File h = new File(userHome + "mdtest" + platformId);
2021 assertTrue("Base Directory not created", h.exists());
2022 assertTrue("Directories not created", g.exists());
2023 assertTrue("File not created", f.exists());
2032 * @tests java.io.File#renameTo(java.io.File)
2035 level = TestLevel.PARTIAL_COMPLETE,
2036 method = "renameTo",
2037 args = {java.io.File.class}
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);
2044 File f = new File(dir, "xxx.xxx");
2045 File rfile = new File(dir, "yyy.yyy");
2046 File f2 = new File(dir, "zzz.zzz");
2048 FileOutputStream fos = new FileOutputStream(f);
2049 fos.write(fileString.getBytes());
2051 long lengthOfFile = f.length();
2053 rfile.delete(); // in case it already exists
2057 fail("Test 1: NullPointerException expected.");
2058 } catch (NullPointerException e) {
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);
2067 fos = new FileOutputStream(rfile);
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());
2084 * @tests java.io.File#setLastModified(long)
2087 level = TestLevel.PARTIAL_COMPLETE,
2088 method = "setLastModified",
2091 public void test_setLastModifiedJ() {
2092 // Test for method java.io.File.setLastModified()
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");
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
2133 assertTrue("Test 6: LastModified time incorrect: " + next,
2134 !result || next == 4354837199000L);
2136 // Trying to set time to a negative number
2138 f1.setLastModified(-25);
2139 fail("IllegalArgumentException not thrown.");
2140 } catch (IllegalArgumentException e) {
2142 } catch (IOException e) {
2143 fail("Unexpected IOException during test : " + e.getMessage());
2151 * @tests java.io.File#setReadOnly()
2155 level = TestLevel.PARTIAL_COMPLETE,
2156 method = "setReadOnly",
2160 level = TestLevel.PARTIAL_COMPLETE,
2161 method = "canWrite",
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()
2173 Runtime r = Runtime.getRuntime();
2176 f1 = File.createTempFile("hyts_tf", ".tmp");
2177 f2 = File.createTempFile("hyts_tf", ".tmp");
2179 assertTrue("Test 1: File is read-only." , f1.canWrite());
2181 assertTrue("Test 2: File is not read-only." , !f1.canWrite());
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) {
2190 p = r.exec("chmod +w " + f1.getAbsolutePath());
2193 assertTrue("Test 4: File is read-only." , f1.canWrite());
2195 FileOutputStream fos = new FileOutputStream(f1);
2196 fos.write(fileString.getBytes());
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);
2204 assertTrue("Test 7: File has not been deleted.", f1.delete());
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);
2210 fos.write(fileString.getBytes());
2213 // Assert is flawed because canWrite does not work.
2214 // assertTrue("File f2 Is Not Set To ReadOnly." , !f2.canWrite());
2216 // Attempt to write to a file that has previously been written
2218 // and is now set to read only.
2219 fos = new FileOutputStream(f2);
2220 fail("Test 8: IOException expected.");
2221 } catch (IOException e) {
2223 p = r.exec("chmod +w " + f2.getAbsolutePath());
2225 assertTrue("Test 9: File is read-only.", f2.canWrite());
2227 fos = new FileOutputStream(f2);
2228 fos.write(fileString.getBytes());
2230 } catch (IOException e) {
2231 fail("Test 10: Unexpected IOException while attempting to " +
2232 "write to the file. " + e);
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");
2240 assertTrue("Test 12: Directory has not been deleted.",
2242 assertTrue("Test 13: Directory has not been deleted.",
2245 } catch (IOException e) {
2246 fail("Test 14: Unexpected IOException: " + e.getMessage());
2247 } catch (InterruptedException e) {
2248 fail("Test 15: Unexpected InterruptedException: " + e);
2258 * @tests java.io.File#toString()
2261 level = TestLevel.COMPLETE,
2262 notes = "Verifies toString() method.",
2263 method = "toString",
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));
2272 if (File.separatorChar == '\\') {
2273 String result = new File("c:\\").toString();
2274 assertTrue("Removed backslash: " + result, result.equals("c:\\"));
2279 * @tests java.io.File#toURI()
2282 level = TestLevel.PARTIAL_COMPLETE,
2283 notes = "Verifies toURI() method.",
2287 public void test_toURI() {
2288 // Test for method java.io.File.toURI()
2290 // Need a directory that exists
2291 File dir = new File(System.getProperty("java.io.tmpdir"));
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("/"))
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)));
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;
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)));
2320 // Regression test for HARMONY-3207
2321 dir = new File(""); // current directory
2323 assertTrue("Test current dir: URI does not end with slash.",
2324 uri.toString().endsWith("/"));
2325 } catch (URISyntaxException e1) {
2326 fail("Unexpected URISyntaxException: " + e1);
2331 * @tests java.io.File#toURI()
2334 level = TestLevel.PARTIAL_COMPLETE,
2335 notes = "Verifies that toURI() method works with URIs created with null parameters.",
2339 public void test_toURI2() {
2341 File f = new File(System.getProperty("java.io.tmpdir"), "a/b/c/../d/e/./f");
2343 String path = f.getAbsolutePath();
2344 path = path.replace(File.separatorChar, '/');
2345 if (!path.startsWith("/"))
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);
2358 * @tests java.io.File#toURL()
2361 level = TestLevel.PARTIAL_COMPLETE,
2365 public void test_toURL() {
2366 // Test for method java.io.File.toURL()
2369 // Need a directory that exists
2370 File dir = new File(System.getProperty("java.io.tmpdir"));
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;
2378 newDirURL = "file:/" + newDirURL;
2379 if (!newDirURL.endsWith("/"))
2381 assertEquals("Test 1: Incorrect URL returned;", newDirURL,
2382 dir.toURL().toString());
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;
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.");
2400 * @tests java.io.File#toURL()
2403 level = TestLevel.PARTIAL_COMPLETE,
2407 public void test_toURL2() {
2409 File f = new File(System.getProperty("java.io.tmpdir"), "a/b/c/../d/e/./f");
2411 String path = f.getAbsolutePath();
2412 path = path.replace(File.separatorChar, '/');
2413 if (!path.startsWith("/"))
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);
2426 * @tests java.io.File#toURL()
2429 level = TestLevel.PARTIAL_COMPLETE,
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("/"));
2442 * @tests java.io.File#deleteOnExit()
2445 level = TestLevel.PARTIAL_COMPLETE,
2447 method = "deleteOnExit",
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");
2456 assertTrue(dir.exists());
2457 File subDir = new File(cts + "/hello/world");
2459 assertTrue(subDir.exists());
2461 Support_Exec.execJava(new String[] {
2462 "tests.support.Support_DeleteOnExitTest",
2463 dir.getAbsolutePath(), subDir.getAbsolutePath() },
2464 new String[] {}, false);
2466 assertFalse(dir.exists());
2467 assertFalse(subDir.exists());
2471 * @tests serialization
2474 level = TestLevel.COMPLETE,
2475 notes = "regression test",
2476 method = "!Serialization",
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());
2491 * Sets up the fixture, for example, open a network connection. This method
2492 * is called before a test is executed.
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);
2501 /** Setup the temporary directory */
2502 if (!userDir.regionMatches((userDir.length() - 1), slash, 0, 1))
2504 tempDirectory = new File(userDir + "tempDir"
2505 + String.valueOf(System.currentTimeMillis()));
2506 if (!tempDirectory.mkdir())
2507 System.out.println("Setup for FileTest failed (1).");
2509 /** Setup the temporary file */
2510 tempFile = new File(tempDirectory, "tempfile");
2511 FileOutputStream tempStream;
2513 tempStream = new FileOutputStream(tempFile.getPath(), false);
2515 } catch (IOException e) {
2516 System.out.println("Setup for FileTest failed (2).");
2522 * Tears down the fixture, for example, close a network connection. This
2523 * method is called after a test is executed.
2525 protected void tearDown() {
2526 if (tempFile.exists() && !tempFile.delete())
2528 .println("FileTest.tearDown() failed, could not delete file!");
2529 if (!tempDirectory.delete())
2531 .println("FileTest.tearDown() failed, could not delete directory!");