Author: ggregory
Date: Thu Oct 27 06:20:52 2011
New Revision: 1189627

URL: http://svn.apache.org/viewvc?rev=1189627&view=rev
Log:
[VFS-370] Add a FileExtensionSelector class.

Added:
    
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileExtensionSelectorTest.java
Modified:
    
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileExtensionSelector.java

Modified: 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileExtensionSelector.java
URL: 
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileExtensionSelector.java?rev=1189627&r1=1189626&r2=1189627&view=diff
==============================================================================
--- 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileExtensionSelector.java
 (original)
+++ 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileExtensionSelector.java
 Thu Oct 27 06:20:52 2011
@@ -16,34 +16,87 @@
  */
 package org.apache.commons.vfs2;
 
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
 /**
- * A {@link FileSelector} that selects the given file extension.
- *
- * @author <a href="http://commons.apache.org/vfs/team-list.html";>Commons VFS 
team</a>
+ * A {@link FileSelector} that selects based on file extensions.
+ * <p>
+ * The extension comparison is case insensitive.
+ * </p>
+ * <p>
+ * The selector makes a copy of a given Collection or array. Changing the 
object passed in the constructors will not affect the selector.
+ * </p>
+ * 
+ * @since 2.1
  */
-public class FileExtensionSelector
-    implements FileSelector {
-    
-    private final String extension;
+public class FileExtensionSelector implements FileSelector
+{
+
+    /**
+     * The extensions to select.
+     */
+    private final Set<String> extensions = new HashSet<String>();
+
+    /**
+     * Creates a new selector for the given extensions.
+     * 
+     * @param extensions
+     *            The extensions to be included by this selector.
+     */
+    public FileExtensionSelector(Collection<String> extensions)
+    {
+        if (extensions != null)
+        {
+            this.extensions.addAll(extensions);
+        }
+    }
 
-    public FileExtensionSelector(String extension) {
-        this.extension = extension;
+    /**
+     * Creates a new selector for the given extensions.
+     * 
+     * @param extensions
+     *            The extensions to be included by this selector.
+     */
+    public FileExtensionSelector(String... extensions)
+    {
+        if (extensions != null)
+        {
+            this.extensions.addAll(Arrays.asList(extensions));
+        }
     }
 
     /**
      * Determines if a file or folder should be selected.
-     * @param fileInfo The file selection information.
+     * 
+     * @param fileInfo
+     *            The file selection information.
      * @return true if the file should be selected, false otherwise.
      */
     public boolean includeFile(final FileSelectInfo fileInfo)
     {
-        return 
fileInfo.getFile().getName().getExtension().equalsIgnoreCase(this.extension);
+        if (this.extensions == null)
+        {
+            return false;
+        }
+        for (String extension : this.extensions)
+        {
+            if 
(fileInfo.getFile().getName().getExtension().equalsIgnoreCase(extension))
+            {
+                return true;
+            }
+        }
+        return false;
     }
 
     /**
      * Determines whether a folder should be traversed.
-     * @param fileInfo The file selection information.
-     * @return true if descendents should be traversed, false otherwise.
+     * 
+     * @param fileInfo
+     *            The file selection information.
+     * @return true if descendents should be traversed, fase otherwise.
      */
     public boolean traverseDescendents(final FileSelectInfo fileInfo)
     {

Added: 
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileExtensionSelectorTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileExtensionSelectorTest.java?rev=1189627&view=auto
==============================================================================
--- 
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileExtensionSelectorTest.java
 (added)
+++ 
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileExtensionSelectorTest.java
 Thu Oct 27 06:20:52 2011
@@ -0,0 +1,155 @@
+package org.apache.commons.vfs2;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class FileExtensionSelectorTest
+{
+    private static FileObject BaseFolder;
+
+    private static int FileCount;
+
+    private static int ExtensionCount;
+
+    private static int FilePerExtensionCount;
+
+    /**
+     * Creates a RAM FS.
+     * 
+     * @throws Exception
+     */
+    @BeforeClass
+    public static void setUpClass() throws Exception
+    {
+        BaseFolder = VFS.getManager().resolveFile("ram://" + 
FileExtensionSelectorTest.class.getName());
+        BaseFolder.resolveFile("a.htm").createFile();
+        BaseFolder.resolveFile("a.html").createFile();
+        BaseFolder.resolveFile("a.xhtml").createFile();
+        BaseFolder.resolveFile("b.htm").createFile();
+        BaseFolder.resolveFile("b.html").createFile();
+        BaseFolder.resolveFile("b.xhtml").createFile();
+        BaseFolder.resolveFile("c.htm").createFile();
+        BaseFolder.resolveFile("c.html").createFile();
+        BaseFolder.resolveFile("c.xhtml").createFile();
+        FileCount = BaseFolder.getChildren().length;
+        ExtensionCount = 3;
+        FilePerExtensionCount = 3;
+    }
+
+    /**
+     * Deletes RAM FS files.
+     * 
+     * @throws Exception
+     */
+    @AfterClass
+    public static void tearDownClass() throws Exception
+    {
+        if (BaseFolder != null)
+        {
+            BaseFolder.delete(Selectors.SELECT_ALL);
+        }
+    }
+
+    /**
+     * Tests an empty selector.
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testEmpty() throws Exception
+    {
+        FileSelector selector0 = new FileExtensionSelector();
+        FileObject[] foList = 
FileExtensionSelectorTest.BaseFolder.findFiles(selector0);
+        Assert.assertEquals(0, foList.length);
+    }
+
+    /**
+     * Tests many extensions at once.
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testManyExtensions() throws Exception
+    {
+        FileObject[] foList = 
FileExtensionSelectorTest.BaseFolder.findFiles(Selectors.SELECT_FILES);
+        Assert.assertTrue(foList.length > 0);
+        // gather file extensions.
+        Set<String> extensionSet = new HashSet<String>();
+        for (FileObject fo : foList)
+        {
+            extensionSet.add(fo.getName().getExtension());
+        }
+        Assert.assertTrue(extensionSet.size() > 0);
+        Assert.assertEquals(ExtensionCount, extensionSet.size());
+        // check all unique extensions
+        FileSelector selector = new FileExtensionSelector(extensionSet);
+        FileObject[] list = 
FileExtensionSelectorTest.BaseFolder.findFiles(selector);
+        Assert.assertEquals(FileCount, list.length);
+    }
+
+    /**
+     * Tests a null selector.
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testNullCollection() throws Exception
+    {
+        FileSelector selector0 = new 
FileExtensionSelector((Collection<String>) null);
+        FileObject[] foList = 
FileExtensionSelectorTest.BaseFolder.findFiles(selector0);
+        Assert.assertEquals(0, foList.length);
+    }
+
+    /**
+     * Tests a null selector.
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testNullString() throws Exception
+    {
+        FileSelector selector0 = new FileExtensionSelector((String) null);
+        FileObject[] foList = 
FileExtensionSelectorTest.BaseFolder.findFiles(selector0);
+        Assert.assertEquals(0, foList.length);
+    }
+
+    /**
+     * Tests a one extension selector.
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testOneExtension() throws Exception
+    {
+        FileObject[] foList = 
FileExtensionSelectorTest.BaseFolder.findFiles(Selectors.SELECT_FILES);
+        Assert.assertTrue(foList.length > 0);
+        // gather file extensions.
+        Set<String> extensionSet = new HashSet<String>();
+        for (FileObject fo : foList)
+        {
+            extensionSet.add(fo.getName().getExtension());
+        }
+        Assert.assertEquals(ExtensionCount, extensionSet.size());
+        // check each extension
+        for (String extension : extensionSet)
+        {
+            FileSelector selector = new FileExtensionSelector(extension);
+            FileObject[] list = 
FileExtensionSelectorTest.BaseFolder.findFiles(selector);
+            Assert.assertEquals(FilePerExtensionCount, list.length);
+        }
+        // check each file against itself
+        for (FileObject fo : foList)
+        {
+            FileSelector selector = new 
FileExtensionSelector(fo.getName().getExtension());
+            FileObject[] list = 
FileExtensionSelectorTest.BaseFolder.findFiles(selector);
+            Assert.assertEquals(FilePerExtensionCount, list.length);
+        }
+    }
+
+}


Reply via email to