Author: luca
Date: Wed Jun 17 12:57:32 2015
New Revision: 1686005

URL: http://svn.apache.org/r1686005
Log:
Inserting new DirectorySelector class to support recursive crawling and 
selection of directories containing specified files (OODT-854)

Added:
    
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/io/DirectorySelector.java
   (with props)
    
oodt/trunk/commons/src/test/java/org/apache/oodt/commons/io/DirectorySelectorTest.java
   (with props)

Added: 
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/io/DirectorySelector.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/commons/src/main/java/org/apache/oodt/commons/io/DirectorySelector.java?rev=1686005&view=auto
==============================================================================
--- 
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/io/DirectorySelector.java
 (added)
+++ 
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/io/DirectorySelector.java
 Wed Jun 17 12:57:32 2015
@@ -0,0 +1,104 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more 
contributor
+// license agreements.  See the NOTICE.txt 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.oodt.commons.io;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Class that traverses a directory tree 
+ * and selects those directories that contain ALL of the requested files.
+ * If no requested files are specified, an empty list will be returned.
+ * 
+ * @author Luca Cinquini
+ */
+public class DirectorySelector {
+       
+       private List<String> files;
+       
+       private FileFilter directoryFilter;
+       
+       /**
+        * Creates a new directory selector for the specified files
+        * 
+        * @param files the list of files that this class will look for
+        */
+       public DirectorySelector(List<String> files) {
+               
+               // list of requested files
+               this.files = files;
+               
+               // File filter that selects directories
+               this.directoryFilter = new FileFilter() {
+                       public boolean accept(File file) {
+                               return file.isDirectory();
+                       }
+               };
+               
+       }
+       
+    /**
+     * Looks for files in all sub-directories starting from rootDir.
+     * 
+     * @param rootDir starting root directory
+     * @return list of matching sub-directories as 'file:///path/to/dir' URIs
+     */
+       public List<String> traverseDir(File rootDir) {
+                               
+               List<String> subDirs = new ArrayList<String>();
+               
+               if (rootDir.exists() && files!=null && files.size()>0) {
+                       this.traverseDir(rootDir, subDirs);
+               }
+               
+               return subDirs;
+               
+       }
+       
+       /**
+        * Internal recursion method.
+        * 
+        * @param dir
+        * @param subDirs
+        */
+    private void traverseDir(File dir, List<String> subDirs) {
+       
+       // loop over required files,
+       // include only if all files are found
+               boolean include = true;
+       for (String file : files) {
+               File requiredFile = new File(dir, file);
+               if (!requiredFile.exists()) {
+                       include = false;
+               }
+       }
+       
+       // include this directory
+       if (include) {
+               subDirs.add("file://"+dir.getAbsolutePath());
+       }
+       
+       // recursion over sub-directories
+       File[] subdirs = dir.listFiles( directoryFilter );
+        for (File subdir : subdirs) {
+               traverseDir(subdir, subDirs);
+        }
+       
+    }
+
+}

Propchange: 
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/io/DirectorySelector.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
oodt/trunk/commons/src/test/java/org/apache/oodt/commons/io/DirectorySelectorTest.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/java/org/apache/oodt/commons/io/DirectorySelectorTest.java?rev=1686005&view=auto
==============================================================================
--- 
oodt/trunk/commons/src/test/java/org/apache/oodt/commons/io/DirectorySelectorTest.java
 (added)
+++ 
oodt/trunk/commons/src/test/java/org/apache/oodt/commons/io/DirectorySelectorTest.java
 Wed Jun 17 12:57:32 2015
@@ -0,0 +1,84 @@
+package org.apache.oodt.commons.io;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+public class DirectorySelectorTest extends TestCase {
+       
+       File workingDir;
+       String relPath = "org/apache/oodt/commons/io/";
+       File thisDir;
+       String thisClass = "DirectorySelectorTest.class";
+       
+       public DirectorySelectorTest(String name) {
+               
+               super(name);
+               
+               workingDir = new 
File(DirectorySelectorTest.class.getProtectionDomain().getCodeSource().getLocation().getPath());
+               thisDir = new File(workingDir, relPath);
+
+       }
+       
+       /**
+        * Tests starting from directory containing this class file,
+        * no recursion involved
+        */
+       public void testPositiveSelectionWithoutRecursion() {
+               
+               DirectorySelector ds = new DirectorySelector(Arrays.asList( new 
String[] { thisClass } ));
+               List<String> dirs = ds.traverseDir(thisDir);
+               assertEquals(1, dirs.size());
+               assertEquals("file://"+thisDir.getAbsolutePath(), dirs.get(0));
+               
+       }
+       
+       /**
+        * Tests starting from top-level tests directory,
+        * involves recursion
+        */
+       public void testPositiveSelectionWithRecursion() {
+               
+               DirectorySelector ds = new DirectorySelector(Arrays.asList( new 
String[] { thisClass } ));
+               List<String> dirs = ds.traverseDir(workingDir);
+               assertEquals(1, dirs.size());
+               assertEquals("file://"+thisDir.getAbsolutePath(), dirs.get(0));
+               
+       }
+       
+       /**
+        * Tests that no directories are selected if passing an invalid file.
+        */
+       public void testNegativeSelectionForInvalidFile() {
+               
+               DirectorySelector ds = new DirectorySelector(Arrays.asList( new 
String[] { "doesNotExist.txt" } ));
+               List<String> dirs = ds.traverseDir(workingDir);
+               assertEquals(0, dirs.size());
+
+       }
+       
+       /**
+        * Tests that no directories are returned when no files are specified.
+        */
+       public void testNegativeSelectionForNoFiles() {
+               
+               DirectorySelector ds = new DirectorySelector(Arrays.asList( new 
String[] {} ));
+               List<String> dirs = ds.traverseDir(workingDir);
+               assertEquals(0, dirs.size());
+               
+       }
+       
+       /**
+        * Tests that no directories are returned when starting from an invalid 
directory.
+        */
+       public void testNegativeSelectionForInvalidDirectory() {
+               
+               DirectorySelector ds = new DirectorySelector(Arrays.asList( new 
String[] { thisClass } ));
+               List<String> dirs = ds.traverseDir( new 
File("/tmp/does/not/exist") );
+               assertEquals(0, dirs.size());
+               
+       }
+
+}

Propchange: 
oodt/trunk/commons/src/test/java/org/apache/oodt/commons/io/DirectorySelectorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to