commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject cvs commit: jakarta-commons-sandbox/io/src/test/org/apache/commons/io FileUtilsTestCase.java
Date Fri, 17 Oct 2003 20:15:46 GMT
matth       2003/10/17 13:15:46

  Modified:    io/src/java/org/apache/commons/io FileUtils.java
               io/src/test/org/apache/commons/io FileUtilsTestCase.java
  Added:       io/src/java/org/apache/commons/io/input package.html
               io/src/java/org/apache/commons/io/output package.html
  Log:
  Bugzilla #22738 from yours truly.
  
  Revision  Changes    Path
  1.16      +24 -22    jakarta-commons-sandbox/io/src/java/org/apache/commons/io/FileUtils.java
  
  Index: FileUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/io/src/java/org/apache/commons/io/FileUtils.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- FileUtils.java	13 Oct 2003 07:04:52 -0000	1.15
  +++ FileUtils.java	17 Oct 2003 20:15:46 -0000	1.16
  @@ -57,11 +57,12 @@
   import java.io.FileInputStream;
   import java.io.FileNotFoundException;
   import java.io.FileOutputStream;
  -import java.io.InputStream;
   import java.io.IOException;
  +import java.io.InputStream;
   import java.net.URL;
  +import java.util.ArrayList;
  +import java.util.Collection;
   import java.util.Date;
  -import java.util.Vector;
   
   /**
    * This class provides basic facilities for manipulating files and file paths.
  @@ -241,16 +242,6 @@
       }
   
       /**
  -     * Creates a file handle.
  -     *
  -     * @param fileName The name of the file.
  -     * @return A <code>File</code> instance.
  -     */
  -    public static File getFile(String fileName) {
  -        return new File(fileName);
  -    }
  -
  -    /**
        * Given a directory and an array of extensions... return an array of
        * compliant files.
        *
  @@ -264,9 +255,9 @@
           String directory,
           String[] extensions) {
   
  -        Vector files = new Vector();
  +        Collection files = new ArrayList();
   
  -        java.io.File currentDir = new java.io.File(directory);
  +        File currentDir = new File(directory);
   
           String[] unknownFiles = currentDir.list();
   
  @@ -279,7 +270,7 @@
                   directory
                       + System.getProperty("file.separator")
                       + unknownFiles[i];
  -            java.io.File currentFile = new java.io.File(currentFileName);
  +            File currentFile = new java.io.File(currentFileName);
   
               if (currentFile.isDirectory()) {
   
  @@ -293,14 +284,14 @@
   
                   String[] fetchFiles =
                       getFilesFromExtension(currentFileName, extensions);
  -                files = blendFilesToVector(files, fetchFiles);
  +                files = blendFiles(files, fetchFiles);
   
               } else {
                   //ok... add the file
   
                   String add = currentFile.getAbsolutePath();
                   if (isValidFile(add, extensions)) {
  -                    files.addElement(add);
  +                    files.add(add);
   
                   }
   
  @@ -310,7 +301,7 @@
           //ok... move the Vector into the files list...
   
           String[] foundFiles = new String[files.size()];
  -        files.copyInto(foundFiles);
  +        files.toArray(foundFiles);
   
           return foundFiles;
   
  @@ -319,13 +310,13 @@
       /**
        * Private hepler method for getFilesFromExtension()
        */
  -    private static Vector blendFilesToVector(Vector v, String[] files) {
  +    private static Collection blendFiles(Collection c, String[] files) {
   
           for (int i = 0; i < files.length; ++i) {
  -            v.addElement(files[i]);
  +            c.add(files[i]);
           }
   
  -        return v;
  +        return c;
       }
   
       /**
  @@ -1270,6 +1261,17 @@
           throws Exception {
           String content = fileRead(inFileName);
           fileWrite(outFileName, content);
  +    }
  +
  +    /**
  +     * Creates a file handle.
  +     *
  +     * @param fileName The name of the file.
  +     * @return A <code>File</code> instance.
  +     * @deprecated Use {@link java.io.File#Constructor(String)}
  +     */
  +    public static File getFile(String fileName) {
  +        return new File(fileName);
       }
   
   }
  
  
  
  1.1                  jakarta-commons-sandbox/io/src/java/org/apache/commons/io/input/package.html
  
  Index: package.html
  ===================================================================
  Implementations of input classes, such as |InputStream| and |Reader|.
  
  
  
  
  1.1                  jakarta-commons-sandbox/io/src/java/org/apache/commons/io/output/package.html
  
  Index: package.html
  ===================================================================
  Implementations of output classes, such as |OutputStream| and |Writer|.
  
  
  
  
  1.4       +317 -62   jakarta-commons-sandbox/io/src/test/org/apache/commons/io/FileUtilsTestCase.java
  
  Index: FileUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/io/src/test/org/apache/commons/io/FileUtilsTestCase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FileUtilsTestCase.java	13 Oct 2003 07:06:04 -0000	1.3
  +++ FileUtilsTestCase.java	17 Oct 2003 20:15:46 -0000	1.4
  @@ -60,10 +60,11 @@
   package org.apache.commons.io;
   
   import java.io.File;
  +import java.io.FileInputStream;
   import java.io.IOException;
   import java.net.URL;
   
  -import org.apache.commons.io.testtools.*;
  +import org.apache.commons.io.testtools.FileBasedTestCase;
   
   import junit.framework.Test;
   import junit.framework.TestSuite;
  @@ -72,16 +73,25 @@
   /**
    * This is used to test FileUtils for correctness.
    *
  - * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  + * @author Peter Donald
  + * @author Matthew Hawthorne
  + * @version $Id$
  + * @see FileUtils
    */
   public final class FileUtilsTestCase extends FileBasedTestCase {
   
       // Test data
  -    private static final int FILE1_SIZE = 1;
  -    private static final int FILE2_SIZE = 1024 * 4 + 1;
   
  -    private final File m_testFile1;
  -    private final File m_testFile2;
  +    /**
  +     * Size of test directory.
  +     */
  +    private static final int TEST_DIRECTORY_SIZE = 0;
  +
  +    private final File testFile1;
  +    private final File testFile2;
  +
  +    private static int testFile1Size;
  +    private static int testFile2Size;
   
       public static void main(String[] args) {
           TestRunner.run(suite());
  @@ -94,19 +104,22 @@
       public FileUtilsTestCase(final String name) throws IOException {
           super(name);
   
  -        m_testFile1 = new File(getTestDirectory(), "file1-test.txt");
  -        m_testFile2 = new File(getTestDirectory(), "file1a-test.txt");
  +        testFile1 = new File(getTestDirectory(), "file1-test.txt");
  +        testFile2 = new File(getTestDirectory(), "file1a-test.txt");
  +
  +        testFile1Size = (int)testFile1.length();
  +        testFile2Size = (int)testFile2.length();
       }
   
       /** @see junit.framework.TestCase#setUp() */
       protected void setUp() throws Exception {
           getTestDirectory().mkdirs();
  -        createFile(m_testFile1, FILE1_SIZE);
  -        createFile(m_testFile2, FILE2_SIZE);
  +        createFile(testFile1, testFile1Size);
  +        createFile(testFile2, testFile2Size);
           FileUtils.deleteDirectory(getTestDirectory());
           getTestDirectory().mkdirs();
  -        createFile(m_testFile1, FILE1_SIZE);
  -        createFile(m_testFile2, FILE2_SIZE);
  +        createFile(testFile1, testFile1Size);
  +        createFile(testFile2, testFile2Size);
       }
   
       /** @see junit.framework.TestCase#tearDown() */
  @@ -114,20 +127,242 @@
           FileUtils.deleteDirectory(getTestDirectory());
       }
   
  +    // byteCountToDisplaySize
  +
  +    public void testByteCountToDisplaySize() {
  +        assertEquals(FileUtils.byteCountToDisplaySize(0), "0 bytes");
  +        assertEquals(FileUtils.byteCountToDisplaySize(1024), "1 KB");
  +        assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024), "1 MB");
  +        assertEquals(
  +            FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024),
  +            "1 GB");
  +    }
  +
  +    // waitFor
  +
  +    public void testWaitFor() {
  +        FileUtils.waitFor("", -1);
  +
  +        FileUtils.waitFor("", 2);
  +    }
  +
  +    // toURL
  +
  +    public void testToURLs() throws Exception {
  +        final File[] files = new File[] { new File("file1"), new File("file2")};
  +
  +        final URL[] urls = FileUtils.toURLs(files);
  +
  +        // Path separator causes equality tests to fail
  +        //assertEquals(urls[0].getFile(), File.separator + files[0].getAbsolutePath());
  +        //assertEquals(urls[1].getFile(), File.separator + files[1].getAbsolutePath());
  +
  +    }
  +
  +    public void testGetFilesFromExtension() {
  +        // TODO I'm not sure what is supposed to happen here
  +        FileUtils.getFilesFromExtension("dir", null);
  +
  +        // Non-existent files
  +        final String[] emptyFileNames =
  +            FileUtils.getFilesFromExtension(
  +                getTestDirectory().getAbsolutePath(),
  +                new String[] { "java" });
  +        assertTrue(emptyFileNames.length == 0);
  +
  +        // Existing files
  +        // TODO Figure out how to test this
  +        /*
  +        final String[] fileNames =
  +            FileUtils.getFilesFromExtension(
  +                getClass().getResource("/java/util/").getFile(),
  +                new String[] { "class" });
  +        assertTrue(fileNames.length > 0);
  +        */
  +    }
  +
  +    // mkdir
  +
  +    public void testMkdir() {
  +        final File dir = new File(getTestDirectory(), "testdir");
  +        FileUtils.mkdir(dir.getAbsolutePath());
  +        dir.deleteOnExit();
  +    }
  +
  +    // contentEquals
  +
  +    public void testContentEquals() throws Exception {
  +        // Non-existent files
  +        final File file = new File(getTestDirectory(), getName());
  +        assertTrue(FileUtils.contentEquals(file, file));
  +
  +        // TODO Should comparing 2 directories throw an Exception instead of returning
false?
  +        // Directories
  +        assertTrue(
  +            !FileUtils.contentEquals(getTestDirectory(), getTestDirectory()));
  +
  +        // Different files
  +        final File objFile1 =
  +            new File(getTestDirectory(), getName() + ".object");
  +        objFile1.deleteOnExit();
  +        FileUtils.copyURLToFile(
  +            getClass().getResource("/java/lang/Object.class"),
  +            objFile1);
  +
  +        final File objFile2 =
  +            new File(getTestDirectory(), getName() + ".collection");
  +        objFile2.deleteOnExit();
  +        FileUtils.copyURLToFile(
  +            getClass().getResource("/java/util/Collection.class"),
  +            objFile2);
  +
  +        assertTrue(
  +            "Files should not be equal.",
  +            !FileUtils.contentEquals(objFile1, objFile2));
  +
  +        // Equal files
  +        file.createNewFile();
  +        assertTrue(FileUtils.contentEquals(file, file));
  +    }
  +
  +    // removePath
  +
  +    public void testRemovePath() {
  +        final String fileName =
  +            FileUtils.removePath(
  +                new File(getTestDirectory(), getName()).getAbsolutePath());
  +        assertEquals(getName(), fileName);
  +    }
  +
  +    // getPath
  +
  +    public void testGetPath() {
  +        final String fileName =
  +            FileUtils.getPath(
  +                new File(getTestDirectory(), getName()).getAbsolutePath());
  +        assertEquals(getTestDirectory().getAbsolutePath(), fileName);
  +    }
  +
  +    // copyURLToFile
  +
  +    public void testCopyURLToFile() throws Exception {
  +        // Creates file
  +        final File file = new File(getTestDirectory(), getName());
  +        file.deleteOnExit();
  +
  +        // Loads resource
  +        final String resourceName = "/java/lang/Object.class";
  +        FileUtils.copyURLToFile(getClass().getResource(resourceName), file);
  +
  +        // Tests that resuorce was copied correctly
  +        final FileInputStream fis = new FileInputStream(file);
  +        try {
  +            assertTrue(
  +                "Content is not equal.",
  +                IOUtils.contentEquals(
  +                    getClass().getResourceAsStream(resourceName),
  +                    fis));
  +        } finally {
  +            fis.close();
  +        }
  +    }
  +
  +    // catPath
  +
  +    public void testCatPath() {
  +        // TODO StringIndexOutOfBoundsException thrown if file doesn't contain slash.
  +        // Is this acceptable?
  +        //assertEquals("", FileUtils.catPath("a", "b"));
  +
  +        assertEquals("/a/c", FileUtils.catPath("/a/b", "c"));
  +        assertEquals("/a/d", FileUtils.catPath("/a/b/c", "../d"));
  +    }
  +
  +    // forceMkdir
  +
  +    public void testForceMkdir() throws Exception {
  +        // Tests with existing directory
  +        FileUtils.forceMkdir(getTestDirectory());
  +
  +        // Creates test file
  +        final File testFile = new File(getTestDirectory(), getName());
  +        testFile.deleteOnExit();
  +        testFile.createNewFile();
  +        assertTrue("Test file does not exist.", testFile.exists());
  +
  +        // Tests with existing file
  +        try {
  +            FileUtils.forceMkdir(testFile);
  +            fail("Exception expected.");
  +        } catch (IOException ex) {}
  +
  +        testFile.delete();
  +
  +        // Tests with non-existent directory
  +        FileUtils.forceMkdir(testFile);
  +        assertTrue("Directory was not created.", testFile.exists());
  +    }
  +
  +    // sizeOfDirectory
  +
  +    public void testSizeOfDirectory() throws Exception {
  +        final File file = new File(getTestDirectory(), getName());
  +
  +        // Non-existent file
  +        try {
  +            FileUtils.sizeOfDirectory(file);
  +            fail("Exception expected.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        // Creates file
  +        file.createNewFile();
  +        file.deleteOnExit();
  +
  +        // Existing file
  +        try {
  +            FileUtils.sizeOfDirectory(file);
  +            fail("Exception expected.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        // Existing directory
  +        file.delete();
  +        file.mkdir();
  +
  +        assertEquals(
  +            "Unexpected directory size",
  +            TEST_DIRECTORY_SIZE,
  +            FileUtils.sizeOfDirectory(file));
  +    }
  +
  +    // isFileNewer
  +
  +    // TODO Finish test
  +    public void XtestIsFileNewer() {}
  +
  +    // TODO Remove after debugging
  +    private final void log(Object obj) {
  +        System.out.println(
  +            FileUtilsTestCase.class +" " + getName() + " " + obj);
  +    }
  +
  +    // copyFile
  +
       public void testCopyFile1() throws Exception {
           final File destination = new File(getTestDirectory(), "copy1.txt");
  -        FileUtils.copyFile(m_testFile1, destination);
  +        FileUtils.copyFile(testFile1, destination);
           assertTrue("Check Exist", destination.exists());
  -        assertTrue("Check Full copy", destination.length() == FILE1_SIZE);
  +        assertTrue("Check Full copy", destination.length() == testFile1Size);
       }
   
       public void testCopyFile2() throws Exception {
           final File destination = new File(getTestDirectory(), "copy2.txt");
  -        FileUtils.copyFile(m_testFile2, destination);
  +        FileUtils.copyFile(testFile1, destination);
           assertTrue("Check Exist", destination.exists());
  -        assertTrue("Check Full copy", destination.length() == FILE2_SIZE);
  +        assertTrue("Check Full copy", destination.length() == testFile2Size);
       }
   
  +    // forceDelete
  +
       public void testForceDeleteAFile1() throws Exception {
           final File destination = new File(getTestDirectory(), "copy1.txt");
           destination.createNewFile();
  @@ -144,31 +379,39 @@
           assertTrue("Check No Exist", !destination.exists());
       }
   
  +    // copyFileToDirectory
  +
       public void testCopyFile1ToDir() throws Exception {
           final File directory = new File(getTestDirectory(), "subdir");
           if (!directory.exists())
               directory.mkdirs();
  -        final File destination = new File(directory, m_testFile1.getName());
  -        FileUtils.copyFileToDirectory(m_testFile1, directory);
  +        final File destination = new File(directory, testFile1.getName());
  +        FileUtils.copyFileToDirectory(testFile1, directory);
           assertTrue("Check Exist", destination.exists());
  -        assertTrue("Check Full copy", destination.length() == FILE1_SIZE);
  +        assertTrue("Check Full copy", destination.length() == testFile1Size);
       }
   
       public void testCopyFile2ToDir() throws Exception {
           final File directory = new File(getTestDirectory(), "subdir");
           if (!directory.exists())
               directory.mkdirs();
  -        final File destination = new File(directory, m_testFile2.getName());
  -        FileUtils.copyFileToDirectory(m_testFile2, directory);
  +        final File destination = new File(directory, testFile1.getName());
  +        FileUtils.copyFileToDirectory(testFile1, directory);
           assertTrue("Check Exist", destination.exists());
  -        assertTrue("Check Full copy", destination.length() == FILE2_SIZE);
  +        assertTrue("Check Full copy", destination.length() == testFile2Size);
       }
   
  +    // forceDelete
  +
       public void testForceDeleteDir() throws Exception {
           FileUtils.forceDelete(getTestDirectory().getParentFile());
  -        assertTrue("Check No Exist", !getTestDirectory().getParentFile().exists());
  +        assertTrue(
  +            "Check No Exist",
  +            !getTestDirectory().getParentFile().exists());
       }
   
  +    // resolveFile
  +
       public void testResolveFileDotDot() throws Exception {
           final File file = FileUtils.resolveFile(getTestDirectory(), "..");
           assertEquals(
  @@ -182,6 +425,8 @@
           assertEquals("Check . operator", file, getTestDirectory());
       }
   
  +    // normalize
  +
       public void testNormalize() throws Exception {
           final String[] src =
               {
  @@ -233,7 +478,10 @@
           }
       }
   
  -    private String replaceAll(String text, String lookFor, String replaceWith) {
  +    private String replaceAll(
  +        String text,
  +        String lookFor,
  +        String replaceWith) {
           StringBuffer sb = new StringBuffer(text);
           while (true) {
               int idx = sb.indexOf(lookFor);
  @@ -248,7 +496,7 @@
       /**
        *  Test the FileUtils implementation.
        */
  -    /// Used to exist as IOTestCase class
  +    // Used to exist as IOTestCase class
       public void testFileUtils() throws Exception {
           // Loads file from classpath
           final String path = "/test.txt";
  @@ -264,7 +512,9 @@
               "test.txt extension == \"txt\"",
               FileUtils.getExtension(filename).equals("txt"));
   
  -        assertTrue("Test file does not exist: " + filename, FileUtils.fileExists(filename));
  +        assertTrue(
  +            "Test file does not exist: " + filename,
  +            FileUtils.fileExists(filename));
   
           assertTrue(
               "Second test file does not exist",
  @@ -289,63 +539,68 @@
       }
   
       public void testGetExtension() {
  -        final String[][] tests = {
  -            {"filename.ext", "ext"},
  -            {"README", ""},
  -            {"domain.dot.com", "com"},
  -            {"image.jpeg", "jpeg"}};
  +        final String[][] tests = { { "filename.ext", "ext" }, {
  +                "README", "" }, {
  +                "domain.dot.com", "com" }, {
  +                "image.jpeg", "jpeg" }
  +        };
           for (int i = 0; i < tests.length; i++) {
               assertEquals(tests[i][1], FileUtils.getExtension(tests[i][0]));
               //assertEquals(tests[i][1], FileUtils.extension(tests[i][0]));
           }
       }
  -    
  +
       /* TODO: Reenable this test */
       public void DISABLED__testGetExtensionWithPaths() {
  -        final String[][] testsWithPaths = {
  -            {"/tmp/foo/filename.ext", "ext"},
  -            {"C:\\temp\\foo\\filename.ext", "ext"},
  -            {"/tmp/foo.bar/filename.ext", "ext"},
  -            {"C:\\temp\\foo.bar\\filename.ext", "ext"},
  -            {"/tmp/foo.bar/README", ""},
  -            {"C:\\temp\\foo.bar\\README", ""},
  -            {"../filename.ext", "ext"}};
  +        final String[][] testsWithPaths =
  +            { { "/tmp/foo/filename.ext", "ext" }, {
  +                "C:\\temp\\foo\\filename.ext", "ext" }, {
  +                "/tmp/foo.bar/filename.ext", "ext" }, {
  +                "C:\\temp\\foo.bar\\filename.ext", "ext" }, {
  +                "/tmp/foo.bar/README", "" }, {
  +                "C:\\temp\\foo.bar\\README", "" }, {
  +                "../filename.ext", "ext" }
  +        };
           for (int i = 0; i < testsWithPaths.length; i++) {
  -            assertEquals(testsWithPaths[i][1], FileUtils.getExtension(testsWithPaths[i][0]));
  +            assertEquals(
  +                testsWithPaths[i][1],
  +                FileUtils.getExtension(testsWithPaths[i][0]));
               //assertEquals(testsWithPaths[i][1], FileUtils.extension(testsWithPaths[i][0]));
           }
       }
   
       public void testRemoveExtension() {
  -        final String[][] tests = {
  -            {"filename.ext", "filename"},
  -            {"first.second.third.ext", "first.second.third"},
  -            {"README", "README"},
  -            {"domain.dot.com", "domain.dot"},
  -            {"image.jpeg", "image"}};
  -                                
  +        final String[][] tests = { { "filename.ext", "filename" }, {
  +                "first.second.third.ext", "first.second.third" }, {
  +                "README", "README" }, {
  +                "domain.dot.com", "domain.dot" }, {
  +                "image.jpeg", "image" }
  +        };
  +
           for (int i = 0; i < tests.length; i++) {
               assertEquals(tests[i][1], FileUtils.removeExtension(tests[i][0]));
               //assertEquals(tests[i][1], FileUtils.basename(tests[i][0]));
           }
       }
  -    
  +
       /* TODO: Reenable this test */
       public void DISABLED__testRemoveExtensionWithPaths() {
  -        final String[][] testsWithPaths = {
  -            {"/tmp/foo/filename.ext", "filename"},
  -            {"C:\\temp\\foo\\filename.ext", "filename"},
  -            {"/tmp/foo.bar/filename.ext", "filename"},
  -            {"C:\\temp\\foo.bar\\filename.ext", "filename"},
  -            {"/tmp/foo.bar/README", "README"},
  -            {"C:\\temp\\foo.bar\\README", "README"},
  -            {"../filename.ext", "filename"}};
  +        final String[][] testsWithPaths =
  +            { { "/tmp/foo/filename.ext", "filename" }, {
  +                "C:\\temp\\foo\\filename.ext", "filename" }, {
  +                "/tmp/foo.bar/filename.ext", "filename" }, {
  +                "C:\\temp\\foo.bar\\filename.ext", "filename" }, {
  +                "/tmp/foo.bar/README", "README" }, {
  +                "C:\\temp\\foo.bar\\README", "README" }, {
  +                "../filename.ext", "filename" }
  +        };
   
           for (int i = 0; i < testsWithPaths.length; i++) {
  -            assertEquals(testsWithPaths[i][1], FileUtils.removeExtension(testsWithPaths[i][0]));
  +            assertEquals(
  +                testsWithPaths[i][1],
  +                FileUtils.removeExtension(testsWithPaths[i][0]));
               //assertEquals(testsWithPaths[i][1], FileUtils.basename(testsWithPaths[i][0]));
           }
       }
   
   }
  -
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message