commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1234902 - in /commons/proper/io/trunk/src: main/java/org/apache/commons/io/FileUtils.java main/java/org/apache/commons/io/FilenameUtils.java test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
Date Mon, 23 Jan 2012 17:05:33 GMT
Author: ggregory
Date: Mon Jan 23 17:05:32 2012
New Revision: 1234902

URL: http://svn.apache.org/viewvc?rev=1234902&view=rev
Log:
Refactor new directortContains method for https://issues.apache.org/jira/browse/IO-291. Change
behavior: an unrealized child File is NOT contained in a directory in FileUtils. If you want
to test with file names, use FilenameUtils#directoryContains(String, String).

Modified:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/FilenameUtils.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1234902&r1=1234901&r2=1234902&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Mon Jan 23
17:05:32 2012
@@ -1453,31 +1453,31 @@ public class FileUtils {
      * @since 2.2
      */
     public static boolean directoryContains(final File directory, final File child) throws
IOException {
-
+        
         // Fail fast against NullPointerException
         if (directory == null) {
             throw new IllegalArgumentException("Directory must not be null");
         }
-
+    
         if (!directory.isDirectory()) {
             throw new IllegalArgumentException("Not a directory: " + directory);
         }
-
+    
         if (child == null) {
             return false;
         }
-
+    
+        if (!directory.exists() || !child.exists()) {
+            return false;
+        }
+    
         // Canonicalize paths (normalizes relative paths)
         String canonicalParent = directory.getCanonicalPath();
         String canonicalChild = child.getCanonicalPath();
-
-        if (IOCase.SYSTEM.checkEquals(canonicalParent, canonicalChild)) {
-            return false;
-        }
-
-        return IOCase.SYSTEM.checkStartsWith(canonicalChild, canonicalParent);
-    }
     
+        return FilenameUtils.directoryContains(canonicalParent, canonicalChild);
+    }
+
     /**
      * Cleans a directory without deleting it.
      *

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FilenameUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FilenameUtils.java?rev=1234902&r1=1234901&r2=1234902&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FilenameUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FilenameUtils.java Mon Jan
23 17:05:32 2012
@@ -17,6 +17,7 @@
 package org.apache.commons.io;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Stack;
@@ -510,6 +511,46 @@ public class FilenameUtils {
         }
     }
 
+    /**
+     * Determines whether the {@code parent} directory contains the {@code child} element
(a file or directory).
+     * <p>
+     * The files names are expected to be normalized.
+     * </p>
+     * 
+     * Edge cases:
+     * <ul>
+     * <li>A {@code directory} must not be null: if null, throw IllegalArgumentException</li>
+     * <li>A directory does not contain itself: return false</li>
+     * <li>A null child file is not contained in any parent: return false</li>
+     * </ul>
+     * 
+     * @param canonicalParent
+     *            the file to consider as the parent.
+     * @param canonicalChild
+     *            the file to consider as the child.
+     * @return true is the candidate leaf is under by the specified composite. False otherwise.
+     * @throws IOException
+     *             if an IO error occurs while checking the files.
+     * @since 2.2
+     */
+    public static boolean directoryContains(final String canonicalParent, final String canonicalChild)
throws IOException {
+        
+        // Fail fast against NullPointerException
+        if (canonicalParent == null) {
+            throw new IllegalArgumentException("Directory must not be null");
+        }
+    
+        if (canonicalChild == null) {
+            return false;
+        }
+        
+        if (IOCase.SYSTEM.checkEquals(canonicalParent, canonicalChild)) {
+            return false;
+        }
+    
+        return IOCase.SYSTEM.checkStartsWith(canonicalChild, canonicalParent);
+    }
+
     //-----------------------------------------------------------------------
     /**
      * Converts all separators to the Unix separator of forward slash.

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java?rev=1234902&r1=1234901&r2=1234902&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
(original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
Mon Jan 23 17:05:32 2012
@@ -146,7 +146,7 @@ public class FileUtilsDirectoryContainsT
     }
 
     /**
-     * Test to demonstrate a file which does not exist returns true
+     * Test to demonstrate a file which does not exist returns false
      * @throws IOException
      */
     @Test
@@ -154,7 +154,7 @@ public class FileUtilsDirectoryContainsT
         final File file = new File(top, "DOESNOTEXIST");
         assertTrue("Check directory exists", top.exists());
         assertFalse("Check file does not exist", file.exists());
-        assertTrue("Show Bug", FileUtils.directoryContains(top, file)); /** should be false
*/
+        assertFalse("Direcory does not contain unrealized file", FileUtils.directoryContains(top,
file));
     }
 
     @Test



Mime
View raw message