/luni/src/test/java/tests/api/java/io/FileTest.java
Java | 2540 lines | 1959 code | 212 blank | 369 comment | 199 complexity | 2e56c6b9368380173f07c01bef477072 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package tests.api.java.io;
- import java.io.File;
- import java.io.FileFilter;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FilenameFilter;
- import java.io.IOException;
- import java.io.ObjectStreamClass;
- import java.io.ObjectStreamField;
- import java.io.RandomAccessFile;
- import java.net.MalformedURLException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.net.URL;
- import dalvik.annotation.AndroidOnly;
- import dalvik.annotation.KnownFailure;
- import dalvik.annotation.TestLevel;
- import dalvik.annotation.TestTargetClass;
- import dalvik.annotation.TestTargetNew;
- import dalvik.annotation.TestTargets;
- import static tests.support.Support_Exec.javaProcessBuilder;
- import static tests.support.Support_Exec.execAndGetOutput;
- import tests.util.TestEnvironment;
- @TestTargetClass(File.class)
- public class FileTest extends junit.framework.TestCase {
- /** Location to store tests in */
- private File tempDirectory;
- /** Temp file that does exist */
- private File tempFile;
- /** File separator */
- private String slash = File.separator;
- 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";
- private static String platformId = "Android"
- + System.getProperty("java.vm.version").replace('.', '-');
- {
- // Delete all old temporary files
- File tempDir = new File(System.getProperty("java.io.tmpdir"));
- String[] files = tempDir.list();
- for (int i = 0; i < files.length; i++) {
- File f = new File(tempDir, files[i]);
- if (f.isDirectory()) {
- if (files[i].startsWith("hyts_resources"))
- deleteTempFolder(f);
- }
- if (files[i].startsWith("hyts_") || files[i].startsWith("hyjar_"))
- new File(tempDir, files[i]).delete();
- }
- }
- private void deleteTempFolder(File dir) {
- String files[] = dir.list();
- for (int i = 0; i < files.length; i++) {
- File f = new File(dir, files[i]);
- if (f.isDirectory())
- deleteTempFolder(f);
- else {
- f.delete();
- }
- }
- dir.delete();
- }
- /**
- * @tests java.io.File#File(java.io.File, java.lang.String)
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "File",
- args = {java.io.File.class, java.lang.String.class}
- )
- public void test_ConstructorLjava_io_FileLjava_lang_String() throws Exception {
- String error;
- String dirName = System.getProperty("java.io.tmpdir");
- System.setProperty("user.dir", dirName);
-
- File d = new File(dirName);
- File f = new File(d, "input.tst");
- if (!dirName.regionMatches((dirName.length() - 1), slash, 0, 1))
- dirName += slash;
- dirName += "input.tst";
- error = String.format("Test 1: Incorrect file created: %s; %s expected.", f.getPath(), dirName);
- assertTrue(error, f.getPath().equals(dirName));
- String fileName = null;
- try {
- f = new File(d, fileName);
- fail("Test 2: NullPointerException expected.");
- } catch (NullPointerException e) {
- }
- d = null;
- f = new File(d, "input.tst");
- error = String.format("Test 3: Incorrect file created: %s; %s expected.",
- f.getAbsolutePath(), dirName);
- assertTrue(error, f.getAbsolutePath().equals(dirName));
- // Regression test for Harmony-382
- File s = null;
- f = new File("/abc");
- d = new File(s, "/abc");
- assertEquals("Test 4: Incorrect file created;",
- f.getAbsolutePath(), d.getAbsolutePath());
- }
- /**
- * @tests java.io.File#File(java.lang.String)
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "Verifies File(java.lang.String) constructor.",
- method = "File",
- args = {java.lang.String.class}
- )
- public void test_ConstructorLjava_lang_String() {
- // Test for method java.io.File(java.lang.String)
- String fileName = null;
- try {
- new File(fileName);
- fail("Test 1: NullPointerException expected.");
- } catch (NullPointerException e) {
- }
- fileName = System.getProperty("java.io.tmpdir");
- if (!fileName.regionMatches((fileName.length() - 1), slash, 0, 1))
- fileName += slash;
- fileName += "input.tst";
- File f = new File(fileName);
- assertTrue("Created incorrect file " + f.getPath(), f.getPath().equals(
- fileName));
- }
- /**
- * @tests java.io.File#File(java.lang.String, java.lang.String)
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "File",
- args = {java.lang.String.class, java.lang.String.class}
- )
- public void test_ConstructorLjava_lang_StringLjava_lang_String() {
- String error;
- String dirName = null;
- String fileName = "input.tst";
- String userDir = System.getProperty("java.io.tmpdir");
- System.setProperty("user.dir", userDir);
-
- File f = new File(dirName, fileName);
- if (!userDir.regionMatches((userDir.length() - 1), slash, 0, 1))
- userDir += slash;
- userDir += "input.tst";
- error = String.format("Test 1: Incorrect file created: %s; %s expected.",
- f.getAbsolutePath(), userDir);
- assertTrue(error, f.getAbsolutePath().equals(userDir));
- dirName = System.getProperty("java.io.tmpdir");
- fileName = null;
- try {
- f = new File(dirName, fileName);
- fail("Test 2: NullPointerException expected.");
- } catch (NullPointerException e) {
- // Expected.
- }
- fileName = "input.tst";
- f = new File(dirName, fileName);
- assertTrue("Test 3: Incorrect file created.", f.getPath()
- .equals(userDir));
- // Regression test for Harmony-382
- String s = null;
- f = new File("/abc");
- File d = new File(s, "/abc");
- assertEquals("Test 4: Incorrect file created;", d.getAbsolutePath(), f
- .getAbsolutePath());
- assertEquals("Test3: Created Incorrect File", "/abc", f
- .getAbsolutePath());
- }
- /**
- * @tests java.io.File#File(java.lang.String, java.lang.String)
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "File",
- args = {java.lang.String.class, java.lang.String.class}
- )
- public void test_ConstructorLjava_lang_StringLjava_lang_String_112270() {
- File ref1 = new File("/dir1/file1");
- File file1 = new File("/", "/dir1/file1");
- assertEquals("wrong result 1: " + file1, ref1.getPath(), file1
- .getPath());
- File file2 = new File("/", "//dir1/file1");
- assertTrue("wrong result 2: " + file2, file2.getPath().equals(
- ref1.getPath()));
- File file3 = new File("\\", "\\dir1\\file1");
- assertTrue("wrong result 3: " + file3, file3.getPath().equals(
- "\\/\\dir1\\file1"));
- File file4 = new File("\\", "\\\\dir1\\file1");
- assertTrue("wrong result 4: " + file4, file4.getPath().equals(
- "\\/\\\\dir1\\file1"));
- File ref2 = new File("/lib/content-types.properties");
- File file5 = new File("/", "lib/content-types.properties");
- assertTrue("wrong result 5: " + file5, file5.getPath().equals(
- ref2.getPath()));
- }
- /**
- * @tests java.io.File#File(java.io.File, java.lang.String)
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "File",
- args = {java.io.File.class, java.lang.String.class}
- )
- public void test_ConstructorLjava_io_FileLjava_lang_String_112270() {
- File ref1 = new File("/dir1/file1");
- File root = new File("/");
- File file1 = new File(root, "/dir1/file1");
- assertTrue("wrong result 1: " + file1, file1.getPath().equals(
- ref1.getPath()));
- File file2 = new File(root, "//dir1/file1");
- assertTrue("wrong result 2: " + file2, file2.getPath().equals(
- ref1.getPath()));
- File file3 = new File(root, "\\dir1\\file1");
- assertTrue("wrong result 3: " + file3, file3.getPath().equals(
- "/\\dir1\\file1"));
- File file4 = new File(root, "\\\\dir1\\file1");
- assertTrue("wrong result 4: " + file4, file4.getPath().equals(
- "/\\\\dir1\\file1"));
- File ref2 = new File("/lib/content-types.properties");
- File file5 = new File(root, "lib/content-types.properties");
- assertTrue("wrong result 5: " + file5, file5.getPath().equals(
- ref2.getPath()));
- }
- /**
- * @tests java.io.File#File(java.net.URI)
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "Verifies File(java.net.URI) constructor with incorrect parameter.",
- method = "File",
- args = {java.net.URI.class}
- )
- public void test_ConstructorLjava_net_URI() {
- // Test for method java.io.File(java.net.URI)
- URI uri = null;
- try {
- new File(uri);
- fail("NullPointerException Not Thrown.");
- } catch (NullPointerException e) {
- }
- // invalid file URIs
- String[] uris = new String[] { "mailto:user@domain.com", // not
- // hierarchical
- "ftp:///path", // not file scheme
- "//host/path/", // not absolute
- "file://host/path", // non empty authority
- "file:///path?query", // non empty query
- "file:///path#fragment", // non empty fragment
- "file:///path?", "file:///path#" };
- for (int i = 0; i < uris.length; i++) {
- try {
- uri = new URI(uris[i]);
- } catch (URISyntaxException e) {
- fail("Unexpected exception:" + e);
- }
- try {
- new File(uri);
- fail("Expected IllegalArgumentException for new File(" + uri
- + ")");
- } catch (IllegalArgumentException e) {
- }
- }
- // a valid File URI
- try {
- File f = new File(new URI("file:///pa%20th/another\u20ac/pa%25th"));
- assertTrue("Created incorrect File " + f.getPath(), f.getPath()
- .equals(
- slash + "pa th" + slash + "another\u20ac" + slash
- + "pa%th"));
- } catch (URISyntaxException e) {
- fail("Unexpected exception:" + e);
- } catch (IllegalArgumentException e) {
- fail("Unexpected exception:" + e);
- }
- }
- /**
- * @tests java.io.File#canRead()
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "canRead",
- args = {}
- )
- public void test_canRead() {
- // Test for method boolean java.io.File.canRead()
- // canRead only returns if the file exists so cannot be fully tested.
- File f = new File(System.getProperty("java.io.tmpdir"), platformId
- + "canRead.tst");
- try {
- FileOutputStream fos = new FileOutputStream(f);
- fos.close();
- assertTrue("canRead returned false", f.canRead());
- f.delete();
- } catch (IOException e) {
- fail("Unexpected IOException: " + e.getMessage());
- } finally {
- f.delete();
- }
- }
- /**
- * @tests java.io.File#canWrite()
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "canWrite",
- args = {}
- )
- public void test_canWrite() {
- // Test for method boolean java.io.File.canWrite()
- // canWrite only returns if the file exists so cannot be fully tested.
- File f = new File(System.getProperty("java.io.tmpdir"), platformId
- + "canWrite.tst");
- try {
- FileOutputStream fos = new FileOutputStream(f);
- fos.close();
- assertTrue("canWrite returned false", f.canWrite());
- } catch (IOException e) {
- fail("Unexpected IOException: " + e.getMessage());
- } finally {
- f.delete();
- }
- }
- /**
- * @tests java.io.File#compareTo(java.io.File)
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "Verifies compareTo(java.io.File) method.",
- method = "compareTo",
- args = {java.io.File.class}
- )
- public void test_compareToLjava_io_File() {
- File f1 = new File("thisFile.file");
- File f2 = new File("thisFile.file");
- File f3 = new File("thatFile.file");
- assertEquals("Equal files did not answer zero for compareTo", 0, f1
- .compareTo(f2));
- assertTrue("f3.compareTo(f1) did not result in value < 0", f3
- .compareTo(f1) < 0);
- assertTrue("f1.compareTo(f3) did not result in vale > 0", f1
- .compareTo(f3) > 0);
- }
-
- /**
- * @tests java.io.File#createNewFile()
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- notes = "IOException checked.",
- method = "createNewFile",
- args = {}
- )
- public void test_createNewFile_EmptyString() {
- File f = new File("");
- try {
- f.createNewFile();
- fail("should throw IOException");
- } catch (IOException e) {
- // expected
- }
- }
- /**
- * @tests java.io.File#createNewFile()
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "createNewFile",
- args = {}
- )
- public void test_createNewFile() throws IOException {
- // Test for method java.io.File.createNewFile()
- String base = System.getProperty("java.io.tmpdir");
- boolean dirExists = true;
- int numDir = 1;
- File dir = new File(base, String.valueOf(numDir));
- // Making sure that the directory does not exist.
- while (dirExists) {
- // If the directory exists, add one to the directory number
- // (making
- // it a new directory name.)
- if (dir.exists()) {
- numDir++;
- dir = new File(base, String.valueOf(numDir));
- } else {
- dirExists = false;
- }
- }
- // Test for trying to create a file in a directory that does not
- // exist.
- try {
- // Try to create a file in a directory that does not exist
- File f1 = new File(dir, "tempfile.tst");
- f1.createNewFile();
- fail("Test 1: IOException expected.");
- } catch (IOException e) {
- // Expected.
- }
- dir.mkdir();
- File f1 = new File(dir, "tempfile.tst");
- File f2 = new File(dir, "tempfile.tst");
- f1.deleteOnExit();
- f2.deleteOnExit();
- dir.deleteOnExit();
- assertFalse("Test 2: File should not exist.", f1.isFile());
- f1.createNewFile();
- assertTrue("Test 3: File should exist.", f1.isFile());
- assertTrue("Test 4: File should exist.", f2.isFile());
- String dirName = f1.getParent();
- if (!dirName.endsWith(slash))
- dirName += slash;
- assertTrue("Test 5: File saved in the wrong directory.",
- dirName.equals(dir.getPath() + slash));
- assertEquals("Test 6: File saved with incorrect name;",
- "tempfile.tst", f1.getName());
- // Test for creating a file that already exists.
- assertFalse("Test 7: File already exists, createNewFile should " +
- "return false.", f2.createNewFile());
-
- // Trying to create an illegal file.
- String sep = File.separator;
- f1 = new File(sep+"a"+sep+".."+sep+".."+sep);
- try {
- f1.createNewFile();
- fail("Test 8: IOException expected.");
- } catch (IOException e) {
- // Expected.
- }
-
- f1 = new File(base);
- assertFalse("Test 9: False expected when trying to create an " +
- "existing file", f1.createNewFile());
- }
- /**
- * @tests java.io.File#createTempFile(java.lang.String, java.lang.String)
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "createTempFile",
- args = {java.lang.String.class, java.lang.String.class}
- )
- @AndroidOnly("The RI does not throw an IOException when an illegal" +
- "file prefix is passed to createTempFile.")
- public void test_createTempFileLjava_lang_StringLjava_lang_String() {
- // Test for method java.io.File.createTempFile(String, String)
- // Error protection against using a suffix without a "."?
- File f1 = null;
- File f2 = null;
- try {
- f1 = File.createTempFile("hyts_abc", ".tmp");
- f2 = File.createTempFile("hyts_tf", null);
- String fileLocation = f1.getParent();
- if (!fileLocation.endsWith(slash))
- ;
- fileLocation += slash;
- String tempDir = System.getProperty("java.io.tmpdir");
- if (!tempDir.endsWith(slash))
- tempDir += slash;
- assertTrue(
- "File did not save to the default temporary-file location.",
- fileLocation.equals(tempDir));
- // Test to see if correct suffix was used to create the tempfile.
- File currentFile;
- String fileName;
- // Testing two files, one with suffix ".tmp" and one with null
- for (int i = 0; i < 2; i++) {
- currentFile = i == 0 ? f1 : f2;
- fileName = currentFile.getPath();
- assertTrue("File Created With Incorrect Suffix.", fileName
- .endsWith(".tmp"));
- }
- // Tests to see if the correct prefix was used to create the
- // tempfiles.
- fileName = f1.getName();
- assertTrue("Test 1: File Created With Incorrect Prefix.", fileName
- .startsWith("hyts_abc"));
- fileName = f2.getName();
- assertTrue("Test 2: File Created With Incorrect Prefix.", fileName
- .startsWith("hyts_tf"));
- // Tests for creating a tempfile with a filename shorter than 3
- // characters.
- try {
- File f3 = File.createTempFile("ab", ".tst");
- f3.delete();
- fail("IllegalArgumentException Not Thrown.");
- } catch (IllegalArgumentException e) {
- }
- try {
- File f3 = File.createTempFile("a", ".tst");
- f3.delete();
- fail("IllegalArgumentException Not Thrown.");
- } catch (IllegalArgumentException e) {
- }
- try {
- File f3 = File.createTempFile("", ".tst");
- f3.delete();
- fail("IllegalArgumentException Not Thrown.");
- } catch (IllegalArgumentException e) {
- }
- try {
- // Providing an illegal file prefix.
- File f3 = File.createTempFile("/../../../../../", null);
- f3.delete();
- fail("IOException not thrown");
- } catch (IOException e) {
- }
- } catch (IOException e) {
- fail("Unexpected IOException: " + e.getMessage());
- } finally {
- if (f1 != null)
- f1.delete();
- if (f2 != null)
- f2.delete();
- }
- }
- /**
- * @tests java.io.File#createTempFile(java.lang.String, java.lang.String,
- * java.io.File)
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "createTempFile",
- args = {java.lang.String.class, java.lang.String.class, java.io.File.class}
- )
- public void test_createTempFileLjava_lang_StringLjava_lang_StringLjava_io_File() {
- // Test for method java.io.File.createTempFile(String, String, File)
- File f1 = null;
- File f2 = null;
- String base = System.getProperty("java.io.tmpdir");
- try {
- // Test to make sure that the tempfile was saved in the correct
- // location
- // and with the correct prefix/suffix.
- f1 = File.createTempFile("hyts_tf", null, null);
- File dir = new File(base);
- f2 = File.createTempFile("hyts_tf", ".tmp", dir);
- File currentFile;
- String fileLocation;
- String fileName;
- for (int i = 0; i < 2; i++) {
- currentFile = i == 0 ? f1 : f2;
- fileLocation = currentFile.getParent();
- if (!fileLocation.endsWith(slash))
- fileLocation += slash;
- if (!base.endsWith(slash))
- base += slash;
- assertTrue(
- "File not created in the default temporary-file location.",
- fileLocation.equals(base));
- fileName = currentFile.getName();
- assertTrue("File created with incorrect suffix.", fileName
- .endsWith(".tmp"));
- assertTrue("File created with incorrect prefix.", fileName
- .startsWith("hyts_tf"));
- currentFile.delete();
- }
- // Test for creating a tempfile in a directory that does not exist.
- int dirNumber = 1;
- boolean dirExists = true;
- // Set dir to a non-existent directory inside the temporary
- // directory
- dir = new File(base, String.valueOf(dirNumber));
- // Making sure that the directory does not exist.
- while (dirExists) {
- // If the directory exists, add one to the directory number
- // (making it
- // a new directory name.)
- if (dir.exists()) {
- dirNumber++;
- dir = new File(base, String.valueOf(dirNumber));
- } else {
- dirExists = false;
- }
- }
- try {
- // Try to create a file in a directory that does not exist
- File f3 = File.createTempFile("hyts_tf", null, dir);
- f3.delete();
- fail("IOException not thrown");
- } catch (IOException e) {
- }
- dir.delete();
- // Tests for creating a tempfile with a filename shorter than 3
- // characters.
- try {
- File f4 = File.createTempFile("ab", null, null);
- f4.delete();
- fail("IllegalArgumentException not thrown.");
- } catch (IllegalArgumentException e) {
- }
- try {
- File f4 = File.createTempFile("a", null, null);
- f4.delete();
- fail("IllegalArgumentException not thrown.");
- } catch (IllegalArgumentException e) {
- }
- try {
- File f4 = File.createTempFile("", null, null);
- f4.delete();
- fail("IllegalArgumentException not thrown.");
- } catch (IllegalArgumentException e) {
- }
- } catch (IOException e) {
- fail("Unexpected IOException: " + e.getMessage());
- } finally {
- if (f1 != null)
- f1.delete();
- if (f2 != null)
- f1.delete();
- }
- }
- /**
- * @tests java.io.File#delete()
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "delete",
- args = {}
- )
- public void test_delete() {
- // Test for method boolean java.io.File.delete()
- try {
- File dir = new File(System.getProperty("java.io.tmpdir"), platformId
- + "filechk");
- dir.mkdir();
- assertTrue("Directory Does Not Exist", dir.exists()
- && dir.isDirectory());
- File f = new File(dir, "filechk.tst");
- FileOutputStream fos = new FileOutputStream(f);
- fos.close();
- assertTrue("Error Creating File For Delete Test", f.exists());
- dir.delete();
- assertTrue("Directory Should Not Have Been Deleted.", dir.exists());
- f.delete();
- assertTrue("File Was Not Deleted", !f.exists());
- dir.delete();
- assertTrue("Directory Was Not Deleted", !dir.exists());
- } catch (IOException e) {
- fail("Unexpected IOException During Delete Test : "
- + e.getMessage());
- }
- }
- /**
- * @tests java.io.File#equals(java.lang.Object)
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "Verifies equals() method.",
- method = "equals",
- args = {java.lang.Object.class}
- )
- public void test_equalsLjava_lang_Object() {
- // Test for method boolean java.io.File.equals(java.lang.Object)
- File f1 = new File("filechk.tst");
- File f2 = new File("filechk.tst");
- File f3 = new File("xxxx");
- assertTrue("Equality test failed", f1.equals(f2));
- assertTrue("Files Should Not Return Equal.", !f1.equals(f3));
- f3 = new File("FiLeChK.tst");
- boolean onWindows = File.separatorChar == '\\';
- boolean onUnix = File.separatorChar == '/';
- if (onWindows)
- assertTrue("Files Should Return Equal.", f1.equals(f3));
- else if (onUnix)
- assertTrue("Files Should NOT Return Equal.", !f1.equals(f3));
- try {
- f1 = new File(System.getProperty("java.io.tmpdir"), "casetest.tmp");
- f2 = new File(System.getProperty("java.io.tmpdir"), "CaseTest.tmp");
- new FileOutputStream(f1).close(); // create the file
- if (f1.equals(f2)) {
- try {
- new FileInputStream(f2);
- } catch (IOException e) {
- fail("File system is case sensitive");
- }
- } else {
- boolean exception = false;
- try {
- new FileInputStream(f2);
- } catch (IOException e) {
- exception = true;
- }
- assertTrue("File system is case insensitive", exception);
- }
- f1.delete();
- } catch (IOException e) {
- fail("Unexpected using case sensitive test : " + e.getMessage());
- }
- }
- /**
- * @tests java.io.File#exists()
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "exists",
- args = {}
- )
- public void test_exists() {
- // Test for method boolean java.io.File.exists()
- try {
- File f = new File(System.getProperty("java.io.tmpdir"), platformId
- + "exists.tst");
- assertTrue("Exists returned true for non-existent file", !f
- .exists());
- FileOutputStream fos = new FileOutputStream(f);
- fos.close();
- assertTrue("Exists returned false file", f.exists());
- f.delete();
- } catch (IOException e) {
- fail("Unexpected IOException During Test : " + e.getMessage());
- }
- }
- /**
- * @tests java.io.File#getAbsoluteFile()
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "getAbsoluteFile",
- args = {}
- )
- public void test_getAbsoluteFile() {
- // Test for method java.io.File getAbsoluteFile()
- String base = System.getProperty("java.io.tmpdir");
- if (!base.endsWith(slash))
- base += slash;
- File f = new File(base, "temp.tst");
- File f2 = f.getAbsoluteFile();
- assertEquals("Test 1: Incorrect File Returned.", 0, f2.compareTo(f
- .getAbsoluteFile()));
- f = new File(base + "Temp" + slash + slash + "temp.tst");
- f2 = f.getAbsoluteFile();
- assertEquals("Test 2: Incorrect File Returned.", 0, f2.compareTo(f
- .getAbsoluteFile()));
- f = new File(base + slash + ".." + slash + "temp.tst");
- f2 = f.getAbsoluteFile();
- assertEquals("Test 3: Incorrect File Returned.", 0, f2.compareTo(f
- .getAbsoluteFile()));
- f.delete();
- f2.delete();
- }
- /**
- * @tests java.io.File#getAbsolutePath()
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "getAbsolutePath",
- args = {}
- )
- public void test_getAbsolutePath() {
- // Test for method java.lang.String java.io.File.getAbsolutePath()
- String base = System.getProperty("java.io.tmpdir");
- if (!base.regionMatches((base.length() - 1), slash, 0, 1))
- base += slash;
- File f = new File(base, "temp.tst");
- assertTrue("Test 1: Incorrect path returned.", f.getAbsolutePath()
- .equals(base + "temp.tst"));
- f = new File(base + "Temp" + slash + slash + slash + "Testing" + slash
- + "temp.tst");
- assertTrue("Test 2: Incorrect path returned.", f.getAbsolutePath()
- .equals(base + "Temp" + slash + "Testing" + slash + "temp.tst"));
- f = new File(base + "a" + slash + slash + ".." + slash + "temp.tst");
- assertTrue("Test 3: Incorrect path returned." + f.getAbsolutePath(), f
- .getAbsolutePath().equals(
- base + "a" + slash + ".." + slash + "temp.tst"));
- f.delete();
- }
- /**
- * @tests java.io.File#getCanonicalFile()
- */
- @TestTargetNew(
- level = TestLevel.SUFFICIENT,
- notes = "IOException not checked since it may only occur if" +
- "the native access to the filesystem fails.",
- method = "getCanonicalFile",
- args = {}
- )
- public void test_getCanonicalFile() {
- // Test for method java.io.File.getCanonicalFile()
- try {
- String base = System.getProperty("java.io.tmpdir");
- if (!base.endsWith(slash))
- base += slash;
- File f = new File(base, "temp.tst");
- File f2 = f.getCanonicalFile();
- assertEquals("Test 1: Incorrect file returned.", 0, f2
- .getCanonicalFile().compareTo(f.getCanonicalFile()));
- f = new File(base + "Temp" + slash + slash + "temp.tst");
- f2 = f.getCanonicalFile();
- assertEquals("Test 2: Incorrect file returned.", 0, f2
- .getCanonicalFile().compareTo(f.getCanonicalFile()));
- f = new File(base + "Temp" + slash + slash + ".." + slash
- + "temp.tst");
- f2 = f.getCanonicalFile();
- assertEquals("Test 3: Incorrect file returned.", 0, f2
- .getCanonicalFile().compareTo(f.getCanonicalFile()));
- // Test for when long directory/file names in Windows
- boolean onWindows = File.separatorChar == '\\';
- // String userDir = System.getProperty("java.io.tmpdir");
- if (onWindows) {
- File testdir = new File(base, "long-" + platformId);
- testdir.mkdir();
- File dir = new File(testdir, "longdirectory" + platformId);
- try {
- dir.mkdir();
- f = new File(dir, "longfilename.tst");
- f2 = f.getCanonicalFile();
- assertEquals("Test 4: Incorrect file returned.",
- 0, f2.getCanonicalFile().compareTo(
- f.getCanonicalFile()));
- FileOutputStream fos = new FileOutputStream(f);
- fos.close();
- f2 = new File(testdir + slash + "longdi~1" + slash
- + "longfi~1.tst");
- File canonicalf2 = f2.getCanonicalFile();
- /*
- * If the "short file name" doesn't exist, then assume that
- * the 8.3 file name compatibility is disabled.
- */
- if (canonicalf2.exists()) {
- assertTrue("Test 5: Incorrect file returned: "
- + canonicalf2, canonicalf2.compareTo(f
- .getCanonicalFile()) == 0);
- }
- } finally {
- f.delete();
- f2.delete();
- dir.delete();
- testdir.delete();
- }
- }
- } catch (IOException e) {
- fail ("Unexpected IOException during Test : " + e.getMessage());
- }
- }
- /**
- * @tests java.io.File#getCanonicalPath()
- */
- @TestTargetNew(
- level = TestLevel.SUFFICIENT,
- notes = "IOException not checked since it may only occur if" +
- "the native access to the filesystem fails.",
- method = "getCanonicalPath",
- args = {}
- )
- public void test_getCanonicalPath() {
- // Test for method java.lang.String java.io.File.getCanonicalPath()
- // Should work for Unix/Windows.
- String dots = "..";
- String expected;
- String error;
- String tmpDir = System.getProperty("java.io.tmpdir");
- System.setProperty("user.dir", tmpDir);
- try {
- String base = new File(tmpDir).getCanonicalPath();
- if (!base.regionMatches((base.length() - 1), slash, 0, 1))
- base += slash;
- File f = new File(base, "temp.tst");
- assertEquals("Test 1: Incorrect Path Returned.", base + "temp.tst", f
- .getCanonicalPath());
- f = new File(base + "Temp" + slash + dots + slash + "temp.tst");
- assertEquals("Test 2: Incorrect Path Returned.", base + "temp.tst", f
- .getCanonicalPath());
- // Finding a non-existent directory for tests 3 and 4
- // This is necessary because getCanonicalPath is case sensitive and
- // could
- // cause a failure in the test if the directory exists but with
- // different
- // case letters (e.g "Temp" and "temp")
- int dirNumber = 1;
- boolean dirExists = true;
- File dir1 = new File(base, String.valueOf(dirNumber));
- while (dirExists) {
- if (dir1.exists()) {
- dirNumber++;
- dir1 = new File(base, String.valueOf(dirNumber));
- } else {
- dirExists = false;
- }
- }
- f = new File(base + dirNumber + slash + dots + slash + dirNumber + slash
- + "temp.tst");
- assertEquals("Test 3: Incorrect path returned.", base + dirNumber + slash
- + "temp.tst", f.getCanonicalPath());
- f = new File(base + dirNumber + slash + "Temp" + slash + dots + slash + "Test"
- + slash + "temp.tst");
- assertEquals("Test 4: Incorrect path returned.", base + dirNumber + slash + "Test"
- + slash + "temp.tst", f.getCanonicalPath());
- f = new File("1234.567");
- expected = System.getProperty("user.dir") + "/1234.567";
- error = String.format("Test 5: Incorrect path %s returned; %s expected.",
- f.getCanonicalPath(), expected);
- assertTrue(error, f.getCanonicalPath().equals(expected));
- } catch (IOException e) {
- fail("Unexpected IOException During Test : " + e.getMessage());
- }
- }
- /**
- * @tests java.io.File#getName()
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "Verifies getName() method.",
- method = "getName",
- args = {}
- )
- public void test_getName() {
- // Test for method java.lang.String java.io.File.getName()
- File f = new File("name.tst");
- assertEquals("Test 1: Returned incorrect name",
- "name.tst", f.getName());
- f = new File("");
- assertTrue("Test 2: Returned incorrect name", f.getName().equals(""));
- f.delete();
- }
- /**
- * @tests java.io.File#getParent()
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "Verifies getParent() method.",
- method = "getParent",
- args = {}
- )
- public void test_getParent() throws Exception {
- // Test for method java.lang.String java.io.File.getParent()
- String dir = System.getProperty("java.io.tmpdir");
- if (dir == null)
- throw new Exception("System property java.io.tmpdir not defined.");
- File f = new File("p.tst");
- assertNull("Test 1: Incorrect path returned", f.getParent());
- f = new File(dir, "p.tst");
- assertTrue("Test 2: Incorrect path returned",
- f.getParent().equals(dir));
- try {
- f.delete();
- } catch (Exception e) {
- fail("Unexpected exception during tests : " + e.getMessage());
- }
- File f1 = new File("/directory");
- assertTrue("Wrong parent test 1", f1.getParent().equals(slash));
- f1 = new File("/directory/file");
- assertTrue("Wrong parent test 2", f1.getParent().equals(
- slash + "directory"));
- f1 = new File("directory/file");
- assertEquals("Wrong parent test 3", "directory", f1.getParent());
- f1 = new File("/");
- assertNull("Wrong parent test 4", f1.getParent());
- f1 = new File("directory");
- assertNull("Wrong parent test 5", f1.getParent());
- if (File.separatorChar == '\\' && new File("d:/").isAbsolute()) {
- f1 = new File("d:/directory");
- assertTrue("Wrong parent test 1a", f1.getParent().equals(
- "d:" + slash));
- f1 = new File("d:/directory/file");
- assertTrue("Wrong parent test 2a", f1.getParent().equals(
- "d:" + slash + "directory"));
- f1 = new File("d:directory/file");
- assertEquals("Wrong parent test 3a",
- "d:directory", f1.getParent());
- f1 = new File("d:/");
- assertNull("Wrong parent test 4a", f1.getParent());
- f1 = new File("d:directory");
- assertEquals("Wrong parent test 5a", "d:", f1.getParent());
- }
- }
- /**
- * @tests java.io.File#getParentFile()
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "Verifies getParentFile() method.",
- method = "getParentFile",
- args = {}
- )
- public void test_getParentFile() {
- // Test for method java.io.File.getParentFile()
- File f = new File("tempfile.tst");
- assertNull("Incorrect path returned", f.getParentFile());
- f = new File(System.getProperty("java.io.tmpdir"), "tempfile1.tmp");
- File f2 = new File(System.getProperty("java.io.tmpdir"), "tempfile2.tmp");
- File f3 = new File(System.getProperty("java.io.tmpdir"), "/a/tempfile.tmp");
- assertEquals("Incorrect file returned", 0, f.getParentFile().compareTo(
- f2.getParentFile()));
- assertTrue("Incorrect file returned", f.getParentFile().compareTo(
- f3.getParentFile()) != 0);
- f.delete();
- f2.delete();
- f3.delete();
- }
- /**
- * @tests java.io.File#getPath()
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "Verifies getPath() method.",
- method = "getPath",
- args = {}
- )
- public void test_getPath() {
- System.setProperty("user.dir", System.getProperty("java.io.tmpdir"));
- String base = System.getProperty("user.dir");
- String fname;
- File f1;
- if (!base.regionMatches((base.length() - 1), slash, 0, 1))
- base += slash;
- fname = base + "filechk.tst";
- f1 = new File(base, "filechk.tst");
- File f2 = new File("filechk.tst");
- File f3 = new File("c:");
- File f4 = new File(base + "a" + slash + slash + ".." + slash
- + "filechk.tst");
- assertTrue("getPath returned incorrect path(f1) " + f1.getPath(), f1
- .getPath().equals(fname));
- assertTrue("getPath returned incorrect path(f2) " + f2.getPath(), f2
- .getPath().equals("filechk.tst"));
- assertTrue("getPath returned incorrect path(f3) " + f3.getPath(), f3
- .getPath().equals("c:"));
- assertTrue("getPath returned incorrect path(f4) " + f4.getPath(), f4
- .getPath().equals(
- base + "a" + slash + ".." + slash + "filechk.tst"));
- f1.delete();
- f2.delete();
- f3.delete();
- f4.delete();
- }
- /**
- * @tests java.io.File#isAbsolute()
- */
- @TestTargetNew(
- level = TestLevel.COMPLETE,
- notes = "Verifies isAbsolute() method.",
- method = "isAbsolute",
- args = {}
- )
- public void test_isAbsolute() {
- // Test for method boolean java.io.File.isAbsolute()
- if (File.separatorChar == '\\') {
- File f = new File("c:\\test");
- File f1 = new File("\\test");
- // One or the other should be absolute on Windows or CE
- assertTrue("Absolute returned false", (f.isAbsolute() && !f1
- .isAbsolute())
- || (!f.isAbsolute() && f1.isAbsolute()));
- } else {
- File f = new File("/test");
- assertTrue("Absolute returned false", f.isAbsolute());
- }
- assertTrue("Non-Absolute returned true", !new File("../test")
- .isAbsolute());
- }
- /**
- * @tests java.io.File#isDirectory()
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "isDirectory",
- args = {}
- )
- public void test_isDirectory() {
- // Test for method boolean java.io.File.isDirectory()
- String base = System.getProperty("java.io.tmpdir");
- if (!base.regionMatches((base.length() - 1), slash, 0, 1))
- base += slash;
- File f = new File(base);
- assertTrue("Test 1: Directory returned false", f.isDirectory());
- f = new File(base + "zxzxzxz" + platformId);
- assertTrue("Test 2: (Not created) directory returned true.", !f
- .isDirectory());
- f.mkdir();
- try {
- assertTrue("Test 3: Directory returned false.", f.isDirectory());
- } finally {
- f.delete();
- }
- }
- /**
- * @tests java.io.File#isFile()
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "isFile",
- args = {}
- )
- public void test_isFile() {
- // Test for method boolean java.io.File.isFile()
- try {
- String base = System.getProperty("java.io.tmpdir");
- File f = new File(base);
- assertTrue("Directory returned true as being a file.", !f.isFile());
- if (!base.regionMatches((base.length() - 1), slash, 0, 1))
- base += slash;
- f = new File(base, platformId + "amiafile");
- assertTrue("Non-existent file returned true", !f.isFile());
- FileOutputStream fos = new FileOutputStream(f);
- fos.close();
- assertTrue("File returned false", f.isFile());
- f.delete();
- } catch (IOException e) {
- fail("IOException during isFile " + e.getMessage());
- }
- }
- /**
- * @tests java.io.File#isHidden()
- */
- @TestTargetNew(
- level = TestLevel.PARTIAL_COMPLETE,
- method = "isHidden",
- args = {}
- )
- public void test_isHidden() {
- // Test for method boolean java.io.File.isHidden()
- boolean onUnix = File.separatorChar == '/';
-
- try {
- File f = File.createTempFile("hyts_", ".tmp");
- // On Unix hidden files are marked with a "." at the beginning
- // of the file name.
- if (onUnix) {
- File f2 = new File(System.getProperty("java.io.tmpdir"), ".test.tst" + platformId);
- FileOutputStream fos2 = new FileOutputStream(f2);
- fos2.close();
- assertTrue("File returned hidden on Unix", !f.isHidden());
- assertTrue("File returned visible on Unix", f2.isHidden());
- assertTrue("File did not delete.", f2.delete());
- } else {
- // For windows, the file is being set hidden by the attrib
- // command.
- Runtime r = Runtime.getRuntime();
- assertTrue("File returned hidden", !f.isHidden());
- Process p = r.exec("attrib +h \"" + f.getAbsolutePath() + "\"");
- p.waitFor();
- assertTrue("File returned visible", f.isHidden());
- p = r.exec("attrib -h \"" + f.getAbsolutePath() + "\"");
- p.waitFor();
- assertTrue("File returned hidden", !f.isHidden());
- }
- f.delete();
- } catch (IOException e) {
- fail("Unexpected IOException during test : " + e.getMessage());
- } catch (InterruptedException e) {
- fail("Unexpected InterruptedExcepti…
Large files files are truncated, but you can click here to view the full file