Author: ggregory
Date: Mon Nov 14 14:10:16 2011
New Revision: 1201713
URL: http://svn.apache.org/viewvc?rev=1201713&view=rev
Log:
[IO-291] Add new function FileUtils.directoryContains.
Added:
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
(with props)
Modified:
commons/proper/io/trunk/src/changes/changes.xml
commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
Modified: commons/proper/io/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1201713&r1=1201712&r2=1201713&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Mon Nov 14 14:10:16 2011
@@ -40,6 +40,9 @@ The <action> type attribute can be add,u
<body>
<release version="2.1.1" date="TBA">
+ <action dev="sebb" type="add" issue="IO-291" due-to="ggregory">
+ Add new function FileUtils.directoryContains.
+ </action>
<action dev="sebb" type="add" issue="IO-275" due-to="CJ Aspromgos">
FileUtils.contentEquals and IOUtils.contentEquals - Add option to
ignore "line endings"
Added contentEqualsIgnoreEOL methods to both classes
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=1201713&r1=1201712&r2=1201713&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 Nov 14 14:10:16 2011
@@ -1430,6 +1430,55 @@ public class FileUtils {
}
/**
+ * Determines whether the {@code parent} directory contains the {@code
child} element (a file or directory).
+ * <p>
+ * Files are normalized before comparison.
+ * </p>
+ *
+ * Edge cases:
+ * <ul>
+ * <li>A {@code directory} must not be null: if null, throw
IllegalArgumentException</li>
+ * <li>A {@code directory} must be a directory: if not a directory, 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 directory
+ * the file to consider as the parent.
+ * @param child
+ * 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 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;
+ }
+
+ // 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);
+ }
+
+ /**
* Cleans a directory without deleting it.
*
* @param directory directory to clean
@@ -2546,4 +2595,5 @@ public class FileUtils {
return true;
}
}
+
}
Added:
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=1201713&view=auto
==============================================================================
---
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
(added)
+++
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
Mon Nov 14 14:10:16 2011
@@ -0,0 +1,160 @@
+/*
+ * 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 org.apache.commons.io;
+
+import org.apache.commons.io.testtools.FileBasedTestCase;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * This class ensure the correctness of {@link
FileUtils#directoryContains(File,File)}.
+ *
+ * @see FileUtils#directoryContains(File, File)
+ * @since 2.2
+ * @version $Id$
+ */
+public class FileUtilsDirectoryContainsTestCase extends FileBasedTestCase {
+
+ private File directory1;
+ private File directory2;
+ private File directory3;
+ private File file1;
+ private File file1ByRelativeDirectory2;
+ private File file2;
+ private File file2ByRelativeDirectory1;
+ private File file3;
+ final File top = getTestDirectory();
+
+ public FileUtilsDirectoryContainsTestCase(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ top.mkdirs();
+
+ directory1 = new File(top, "directory1");
+ directory2 = new File(top, "directory2");
+ directory3 = new File(directory2, "directory3");
+
+ directory1.mkdir();
+ directory2.mkdir();
+ directory3.mkdir();
+
+ file1 = new File(directory1, "file1");
+ file2 = new File(directory2, "file2");
+ file3 = new File(top, "file3");
+
+ // Tests case with relative path
+ file1ByRelativeDirectory2 = new File(getTestDirectory(),
"directory2/../directory1/file1");
+ file2ByRelativeDirectory1 = new File(getTestDirectory(),
"directory1/../directory2/file2");
+
+ FileUtils.touch(file1);
+ FileUtils.touch(file2);
+ FileUtils.touch(file3);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ FileUtils.deleteDirectory(top);
+ }
+
+ @Test
+ public void testCanonicalPath() throws IOException {
+ assertTrue(FileUtils.directoryContains(directory1,
file1ByRelativeDirectory2));
+ assertTrue(FileUtils.directoryContains(directory2,
file2ByRelativeDirectory1));
+
+ assertFalse(FileUtils.directoryContains(directory1,
file2ByRelativeDirectory1));
+ assertFalse(FileUtils.directoryContains(directory2,
file1ByRelativeDirectory2));
+ }
+
+ @Test
+ public void testDirectoryContainsDirectory() throws IOException {
+ assertTrue(FileUtils.directoryContains(top, directory1));
+ assertTrue(FileUtils.directoryContains(top, directory2));
+ assertTrue(FileUtils.directoryContains(top, directory3));
+ assertTrue(FileUtils.directoryContains(directory2, directory3));
+ }
+
+ @Test
+ public void testDirectoryContainsFile() throws IOException {
+ assertTrue(FileUtils.directoryContains(directory1, file1));
+ assertTrue(FileUtils.directoryContains(directory2, file2));
+ }
+
+ @Test
+ public void testDirectoryDoesNotContainFile() throws IOException {
+ assertFalse(FileUtils.directoryContains(directory1, file2));
+ assertFalse(FileUtils.directoryContains(directory2, file1));
+
+ assertFalse(FileUtils.directoryContains(directory1, file3));
+ assertFalse(FileUtils.directoryContains(directory2, file3));
+ }
+
+ @Test
+ public void testDirectoryDoesNotContainsDirectory() throws IOException {
+ assertFalse(FileUtils.directoryContains(directory1, top));
+ assertFalse(FileUtils.directoryContains(directory2, top));
+ assertFalse(FileUtils.directoryContains(directory3, top));
+ assertFalse(FileUtils.directoryContains(directory3, directory2));
+ }
+
+ @Test
+ public void testDirectoryDoesNotExist() throws IOException {
+ final File dir = new File("DOESNOTEXIST");
+ assertFalse(dir.exists());
+ try {
+ assertFalse(FileUtils.directoryContains(dir, file1));
+ fail("Expected " + IllegalArgumentException.class.getName());
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testSameFile() throws IOException {
+ try {
+ assertTrue(FileUtils.directoryContains(file1, file1));
+ fail("Expected " + IllegalArgumentException.class.getName());
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testFileDoesNotExist() throws IOException {
+ assertFalse(FileUtils.directoryContains(top, null));
+ final File file = new File("DOESNOTEXIST");
+ assertFalse(file.exists());
+ assertFalse(FileUtils.directoryContains(top, file));
+ }
+
+ @Test
+ public void testUnrealizedContainment() throws IOException {
+ final File dir = new File("DOESNOTEXIST");
+ final File file = new File(dir, "DOESNOTEXIST2");
+ assertFalse(dir.exists());
+ assertFalse(file.exists());
+ try {
+ assertTrue(FileUtils.directoryContains(dir, file));
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+}
Propchange:
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
------------------------------------------------------------------------------
svn:keywords = Id