You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ni...@apache.org on 2006/07/24 21:41:30 UTC

svn commit: r425158 - in /jakarta/commons/proper/io/trunk/src: java/org/apache/commons/io/ test/org/apache/commons/io/

Author: niallp
Date: Mon Jul 24 12:41:29 2006
New Revision: 425158

URL: http://svn.apache.org/viewvc?rev=425158&view=rev
Log:
IO-86 DirectoryWalker modifications, delete FileFinder and rename FileFinderTestCase to DirectoryWalkerTestCase:
- Change DirectoryWalker methods to use a Collection rather than List
- Move the handleDirectoryEnd() method within the "process" check
- Change walk() method signature to take a Collection and not return anything

Added:
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/DirectoryWalkerTestCase.java
      - copied, changed from r425115, jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileFinderTestCase.java
Removed:
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileFinder.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileFinderTestCase.java
Modified:
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/DirectoryWalker.java

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/DirectoryWalker.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/DirectoryWalker.java?rev=425158&r1=425157&r2=425158&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/DirectoryWalker.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/DirectoryWalker.java Mon Jul 24 12:41:29 2006
@@ -17,42 +17,44 @@
 
 import java.io.File;
 import java.io.FileFilter;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.io.filefilter.IOFileFilter;
+import java.util.Collection;
 
 /**
  * Abstract class that walks through a directory hierarchy and provides
  * subclasses with convenient hooks to add specific behaviour.
  * <p>
- * This class operates with a {@link IOFileFilter file filter} and maximum
- * depth to limit the files and direcories visited. Commons IO supplies many
- * common filter implementations in the <code>filefilter</code> package.
+ * This class operates with a {@link FileFilter} and maximum depth to
+ * limit the files and direcories visited.
+ * Commons IO supplies many common filter implementations in the 
+ * <a href="filefilter/package-summary.html"> filefilter</a> package.
  * <p>
- * This class is extended within Commons IO to provide a file finder facility.
- * However there are many other possible extensions. For example, to delete all
+ * There are many possible extensions, for example, to delete all
  * files and '.svn' directories, and return a list of deleted files:
  * <pre>
  *  public class FileCleaner extends DirectoryWalker {
  *
- *    public List clean(File startDirectory) {
- *      return walk(startDirectory);
+ *    public FileCleaner() {
+ *      super(null, -1);
  *    }
  *
- *    protected boolean handleDirectoryStart(File directory, int depth, List results) {
- *      // skip contents of svn directories
- *      return !".svn".equals(directory.getName());
+ *    public List clean(File startDirectory) {
+ *      List results = new ArrayList();
+ *      walk(startDirectory, results);
+ *      return results;
  *    }
  *
- *    protected void handleDirectoryEnd(File directory, int depth, List results) {
- *      // delete svn directories
+ *    protected boolean handleDirectoryStart(File directory, int depth, Collection results) {
+ *      // delete svn directories and then skip
  *      if (".svn".equals(directory.getName())) {
  *        directory.delete();
+ *        return false;
+ *      } else {
+ *        return true;
  *      }
+ *
  *    }
  *
- *    protected void handleFile(File file, int depth, List results) {
+ *    protected void handleFile(File file, int depth, Collection results) {
  *      // delete file and add to list of deleted
  *      file.delete();
  *      results.add(file);
@@ -98,14 +100,12 @@
      * The event methods have the prefix <code>handle</code>.
      *
      * @param startDirectory  the directory to start from
-     * @return the collection of result objects, typically File objects
+     * @param results  the collection of result objects, may be updated
      */
-    protected List walk(File startDirectory) {
-        List results = new ArrayList();
+    protected void walk(File startDirectory, Collection results) {
         handleStart(startDirectory, results);
         walk(startDirectory, 0, results);
         handleEnd(results);
-        return results;
     }
 
     /**
@@ -113,9 +113,9 @@
      *
      * @param directory  the directory to examine
      * @param depth  the directory level (starting directory = 0)
-     * @return the collection of result objects
+     * @param results  the collection of result objects, may be updated
      */
-    private void walk(File directory, int depth, List results) {
+    private void walk(File directory, int depth, Collection results) {
         boolean process = handleDirectoryStart(directory, depth, results);
         if (process) {
             int childDepth = depth + 1;
@@ -133,8 +133,8 @@
                     }
                 }
             }
+            handleDirectoryEnd(directory, depth, results);
         }
-        handleDirectoryEnd(directory, depth, results);
     }
 
     //-----------------------------------------------------------------------
@@ -146,7 +146,7 @@
      * @param startDirectory  the directory to start from
      * @param results  the collection of result objects, may be updated
      */
-    protected void handleStart(File startDirectory, List results) {
+    protected void handleStart(File startDirectory, Collection results) {
         // do nothing - overridable by subclass
     }
 
@@ -165,7 +165,7 @@
      * @param results  the collection of result objects, may be updated
      * @return true to process this directory, false to skip this directory
      */
-    protected boolean handleDirectoryStart(File directory, int depth, List results) {
+    protected boolean handleDirectoryStart(File directory, int depth, Collection results) {
         // do nothing - overridable by subclass
         return true;
     }
@@ -179,7 +179,7 @@
      * @param depth  the current directory level (starting directory = 0)
      * @param results  the collection of result objects, may be updated
      */
-    protected void handleFile(File file, int depth, List results) {
+    protected void handleFile(File file, int depth, Collection results) {
         // do nothing - overridable by subclass
     }
 
@@ -201,7 +201,7 @@
      * @param depth The directory level (starting directory = 0)
      * @param results The collection of files found.
      */
-    protected void handleDirectoryEnd(File directory, int depth, List results) {
+    protected void handleDirectoryEnd(File directory, int depth, Collection results) {
         // do nothing - overridable by subclass
     }
 
@@ -212,7 +212,7 @@
      *
      * @param results  the collection of result objects, may be updated
      */
-    protected void handleEnd(List results) {
+    protected void handleEnd(Collection results) {
         // do nothing - overridable by subclass
     }
 

Copied: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/DirectoryWalkerTestCase.java (from r425115, jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileFinderTestCase.java)
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/DirectoryWalkerTestCase.java?p2=jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/DirectoryWalkerTestCase.java&p1=jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileFinderTestCase.java&r1=425115&r2=425158&rev=425158&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileFinderTestCase.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/DirectoryWalkerTestCase.java Mon Jul 24 12:41:29 2006
@@ -16,7 +16,10 @@
 package org.apache.commons.io;
 
 import java.io.File;
+import java.io.FileFilter;
 import java.util.List;
+import java.util.ArrayList;
+import java.util.Collection;
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -26,9 +29,17 @@
 import org.apache.commons.io.filefilter.NameFileFilter;
 import org.apache.commons.io.filefilter.OrFileFilter;
 
-public class FileFinderTestCase extends TestCase {
+/**
+ * This is used to test DirectoryWalker for correctness.
+ *
+ * @version $Id$
+ * @see DirectoryWalker
+ *
+ */
+public class DirectoryWalkerTestCase extends TestCase {
 
     // Directories
+    private static final File current      = new File(".");
     private static final File javaDir      = new File("src/java");
     private static final File orgDir       = new File(javaDir, "org");
     private static final File apacheDir    = new File(orgDir, "apache");
@@ -56,11 +67,11 @@
     private static final IOFileFilter NOT_SVN = FileFilterUtils.makeSVNAware(null);
 
     public static Test suite() {
-        return new TestSuite(FileFinderTestCase.class);
+        return new TestSuite(DirectoryWalkerTestCase.class);
     }
 
     /** Construct the TestCase using the name */
-    public FileFinderTestCase(String name) {
+    public DirectoryWalkerTestCase(String name) {
         super(name);
     }
 
@@ -80,7 +91,7 @@
      * Test Filtering
      */
     public void testFilter() {
-        List results = new FileFinder(dirsAndFilesFilter).find(javaDir);
+        List results = new TestFileFinder(dirsAndFilesFilter, -1).find(javaDir);
         assertEquals("Result Size", (1 + dirs.length + ioFiles.length + outputFiles.length), results.size());
         assertTrue("Start Dir", results.contains(javaDir));
         checkContainsFiles("Dir", dirs, results);
@@ -92,7 +103,7 @@
      * Test Filtering and limit to depth 0
      */
     public void testFilterAndLimitA() {
-        List results = new FileFinder(NOT_SVN, 0).find(javaDir);
+        List results = new TestFileFinder(NOT_SVN, 0).find(javaDir);
         assertEquals("[A] Result Size", 1, results.size());
         assertTrue("[A] Start Dir",   results.contains(javaDir));
     }
@@ -101,7 +112,7 @@
      * Test Filtering and limit to depth 1
      */
     public void testFilterAndLimitB() {
-        List results = new FileFinder(NOT_SVN, 1).find(javaDir);
+        List results = new TestFileFinder(NOT_SVN, 1).find(javaDir);
         assertEquals("[B] Result Size", 2, results.size());
         assertTrue("[B] Start Dir",   results.contains(javaDir));
         assertTrue("[B] Org Dir",     results.contains(orgDir));
@@ -111,7 +122,7 @@
      * Test Filtering and limit to depth 3
      */
     public void testFilterAndLimitC() {
-        List results = new FileFinder(NOT_SVN, 3).find(javaDir);
+        List results = new TestFileFinder(NOT_SVN, 3).find(javaDir);
         assertEquals("[A] Result Size", 4, results.size());
         assertTrue("[A] Start Dir",   results.contains(javaDir));
         assertTrue("[A] Org Dir",     results.contains(orgDir));
@@ -123,7 +134,7 @@
      * Test Filtering and limit to depth 5
      */
     public void testFilterAndLimitD() {
-        List results = new FileFinder(dirsAndFilesFilter, 5).find(javaDir);
+        List results = new TestFileFinder(dirsAndFilesFilter, 5).find(javaDir);
         assertEquals("[D] Result Size", (1 + dirs.length + ioFiles.length), results.size());
         assertTrue("[D] Start Dir", results.contains(javaDir));
         checkContainsFiles("[D] Dir", dirs, results);
@@ -134,7 +145,7 @@
      * Test Limiting to current directory
      */
     public void testLimitToCurrent() {
-        List results = new FileFinder(0).find();
+        List results = new TestFileFinder(null, 0).find(current);
         assertEquals("Result Size", 1, results.size());
         assertTrue("Current Dir", results.contains(new File(".")));
     }
@@ -143,24 +154,38 @@
      * test an invalid start directory
      */
     public void testMissingStartDirectory() {
+
+        // TODO is this what we want with invalid directory?
+        File invalidDir = new File("invalid-dir");
+        List results = new TestFileFinder(null, -1).find(invalidDir);
+        assertEquals("Result Size", 1, results.size());
+        assertTrue("Current Dir", results.contains(invalidDir));
+ 
+        // TODO is this what we want with Null directory?
         try {
-            FileFinder.ALL_FILES.find(new File("invalid-dir"));
-            fail("Invalid start directory didn't throw Exception");
-        } catch (IllegalArgumentException ignore) {
-            // expected result
-        }
-        try {
-            FileFinder.ALL_FILES.find(null);
+            new TestFileFinder(null, -1).find(null);
             fail("Null start directory didn't throw Exception");
-        } catch (IllegalArgumentException ignore) {
+        } catch (NullPointerException ignore) {
             // expected result
         }
     }
 
     /**
+     * test an invalid start directory
+     */
+    public void testHandleStartDirectoryFalse() {
+
+        List results = new TestFalseFileFinder(null, -1).find(current);
+        assertEquals("Result Size", 0, results.size());
+
+    }
+
+    // ------------ Convenience Test Methods ------------------------------------
+
+    /**
      * Check the files in the array are in the results list.
      */
-    private void checkContainsFiles(String prefix, File[] files, List results) {
+    private void checkContainsFiles(String prefix, File[] files, Collection results) {
         for (int i = 0; i < files.length; i++) {
             assertTrue(prefix + "["+i+"] " + files[i], results.contains(files[i]));
         }
@@ -176,6 +201,54 @@
             names[i] = files[i].getName();
         }
         return new NameFileFilter(names);
+    }
+
+    // ------------ Test DirectoryWalker implementation --------------------------
+
+    /**
+     * Test DirectoryWalker implementation that finds files in a directory hierarchy
+     * applying a file filter.
+     */
+    private static class TestFileFinder extends DirectoryWalker {
+
+        protected TestFileFinder(FileFilter filter, int depthLimit) {
+            super(filter, depthLimit);
+        }
+
+        /** find files. */
+        protected List find(File startDirectory) {
+           List results = new ArrayList();
+           walk(startDirectory, results);
+           return results;
+        }
+
+        /** Handles a directory end by adding the File to the result set. */
+        protected void handleDirectoryEnd(File directory, int depth, Collection results) {
+            results.add(directory);
+        }
+
+        /** Handles a file by adding the File to the result set. */
+        protected void handleFile(File file, int depth, Collection results) {
+            results.add(file);
+        }
+    }
+
+    // ------------ Test DirectoryWalker implementation --------------------------
+
+    /**
+     * Test DirectoryWalker implementation that always returns false
+     * from handleDirectoryStart()
+     */
+    private static class TestFalseFileFinder extends TestFileFinder {
+
+        protected TestFalseFileFinder(FileFilter filter, int depthLimit) {
+            super(filter, depthLimit);
+        }
+
+        /** Always returns false. */
+        protected boolean handleDirectoryStart(File directory, int depth, Collection results) {
+            return false;
+        }
     }
 
 }



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