You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ec...@apache.org on 2018/02/18 19:32:44 UTC

svn commit: r1824691 - in /commons/proper/vfs/trunk: commons-vfs2/src/main/java/org/apache/commons/vfs2/PatternFileSelector.java commons-vfs2/src/test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java src/changes/changes.xml

Author: ecki
Date: Sun Feb 18 19:32:43 2018
New Revision: 1824691

URL: http://svn.apache.org/viewvc?rev=1824691&view=rev
Log:
[VFS-652] Adjust documentation of PatternFileSelector and add tests.

Modified:
    commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/PatternFileSelector.java
    commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java
    commons/proper/vfs/trunk/src/changes/changes.xml

Modified: commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/PatternFileSelector.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/PatternFileSelector.java?rev=1824691&r1=1824690&r2=1824691&view=diff
==============================================================================
--- commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/PatternFileSelector.java (original)
+++ commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/PatternFileSelector.java Sun Feb 18 19:32:43 2018
@@ -16,10 +16,16 @@
  */
 package org.apache.commons.vfs2;
 
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 /**
- * A {@link FileSelector} that selects based on regular expressions matched against base filename.
+ * A {@link FileSelector} that selects based on regular expressions.
+ * <p>
+ * The regular expression specified in one of the constructors is
+ * {@linkplain Matcher#matches() matched} against {@link FileName#getPath()}
+ * of all candidate files. If you want to match only against the base filename,
+ * make sure to prefix the pattern with {@code ".*\\/"}.
  *
  * @since 2.1
  */
@@ -32,6 +38,8 @@ public class PatternFileSelector impleme
 
     /**
      * Creates a new selector for the given pattern.
+     * <p>
+     * See {@link PatternFileSelector} for a specification how the pattern is matched.
      *
      * @param pattern The regular expressed used by this selector.
      */
@@ -41,8 +49,12 @@ public class PatternFileSelector impleme
 
     /**
      * Creates a new selector for the given pattern.
+     * <p>
+     * See {@link PatternFileSelector} for a specification how the pattern is matched.
      *
      * @param regex The regular expressed used by this selector.
+     *
+     * @see Pattern#compile(String, int)
      */
     public PatternFileSelector(final String regex) {
         this(Pattern.compile(regex));
@@ -50,9 +62,10 @@ public class PatternFileSelector impleme
 
     /**
      * Creates a new selector for the given Pattern and flags.
+     * <p>
+     * See {@link PatternFileSelector} for a specification how the pattern is matched.
      *
      * @param regex The expression to be compiled
-     *
      * @param flags Match flags, a bit mask.
      *
      * @see Pattern#compile(String, int)
@@ -63,6 +76,8 @@ public class PatternFileSelector impleme
 
     /**
      * Determines if a file or folder should be selected.
+     * <p>
+     * See {@link PatternFileSelector} for a specification how the pattern is matched.
      *
      * @param fileInfo The file selection information.
      * @return true if the file should be selected, false otherwise.
@@ -79,6 +94,9 @@ public class PatternFileSelector impleme
 
     /**
      * Determines whether a folder should be traversed.
+     * <p>
+     * This implementation always returns true to make sure all
+     * leafs are inspected.
      *
      * @param fileInfo The file selection information.
      * @return true if descendants should be traversed, false otherwise.

Modified: commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java?rev=1824691&r1=1824690&r2=1824691&view=diff
==============================================================================
--- commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java (original)
+++ commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java Sun Feb 18 19:32:43 2018
@@ -16,6 +16,8 @@
  */
 package org.apache.commons.vfs2;
 
+import static org.junit.Assert.assertEquals;
+
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Set;
@@ -52,9 +54,9 @@ public class PatternFileSelectorTest {
         BaseFolder = VFS.getManager().resolveFile("ram://" + PatternFileSelectorTest.class.getName());
         BaseFolder.deleteAll();
         BaseFolder.createFolder();
-        BaseFolder.resolveFile("a.htm").createFile();
-        BaseFolder.resolveFile("a.html").createFile();
-        BaseFolder.resolveFile("a.xhtml").createFile();
+        BaseFolder.resolveFile("aa.htm").createFile();
+        BaseFolder.resolveFile("aa.html").createFile();
+        BaseFolder.resolveFile("aa.xhtml").createFile();
         BaseFolder.resolveFile("b.htm").createFile();
         BaseFolder.resolveFile("b.html").createFile();
         BaseFolder.resolveFile("b.xhtml").createFile();
@@ -129,6 +131,30 @@ public class PatternFileSelectorTest {
         }
     }
 
+    /**
+     * Tests matching partial file names
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testMatchPartial() throws Exception {
+        final FileObject[] list = BaseFolder.findFiles(new PatternFileSelector(".*a.htm"));
+        Assert.assertEquals(1, list.length);
+        assertEquals(list[0].getName().getBaseName(), "aa.htm");
+    }
+
+    /**
+     * Tests matching partial file names with delimiter
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testMatchPartialDelimited() throws Exception {
+        final FileObject[] list = BaseFolder.findFiles(new PatternFileSelector("^.*\\/b.htm$"));
+        Assert.assertEquals(1, list.length);
+        assertEquals(list[0].getName().getBaseName(), "b.htm");
+    }
+
     static FileObject getBaseFolder() {
         return BaseFolder;
     }

Modified: commons/proper/vfs/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1824691&r1=1824690&r2=1824691&view=diff
==============================================================================
--- commons/proper/vfs/trunk/src/changes/changes.xml (original)
+++ commons/proper/vfs/trunk/src/changes/changes.xml Sun Feb 18 19:32:43 2018
@@ -50,6 +50,9 @@ The <action> type attribute can be add,u
 <!--        [Local] Need an easy way to convert from a FileObject to a File. -->
 <!--       </action> -->
 <!-- START Might need to be moved to the next version -->
+      <action issue="VFS-652" dev="ecki" type="fix">
+        PatternFileSelector documentation to describe actual matching against getPath().
+      </action>
       <action issue="VFS-650" dev="ggregory" type="update">
         Update Apache Commons Compress from 1.15 to 1.16.1.
       </action>
@@ -103,7 +106,7 @@ The <action> type attribute can be add,u
       <action issue="VFS-644" dev="ggregory" type="fix">
         AbstractFileSystem.streamClosed() always sets openStream count to zero.
       </action>
-    </release>  
+    </release>
     <release version="2.1" date="2016-05-19" description="New features and bug fix release.