commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r418641 - in /jakarta/commons/proper/io/trunk/src: java/org/apache/commons/io/ test/org/apache/commons/io/ test/org/apache/commons/io/filefilter/
Date Sun, 02 Jul 2006 19:42:19 GMT
Author: scolebourne
Date: Sun Jul  2 12:42:19 2006
New Revision: 418641

URL: http://svn.apache.org/viewvc?rev=418641&view=rev
Log:
Test and fix case sensitive wildcard filter

Modified:
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOCase.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java?rev=418641&r1=418640&r2=418641&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java Sun
Jul  2 12:42:19 2006
@@ -1134,6 +1134,8 @@
         if (caseSensitivity == null) {
             caseSensitivity = IOCase.SENSITIVE;
         }
+        filename = caseSensitivity.convertCase(filename);
+        wildcardMatcher = caseSensitivity.convertCase(wildcardMatcher);
         String[] wcs = splitOnTokens(wildcardMatcher);
         boolean anyChars = false;
         int textIdx = 0;
@@ -1179,7 +1181,7 @@
                         }
                     } else {
                         // matching from current position
-                        if (!caseSensitivity.checkRegionMatches(filename, textIdx, wcs[wcsIdx]))
{
+                        if (!filename.startsWith(wcs[wcsIdx], textIdx)) {
                             // couldnt match token
                             break;
                         }

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOCase.java?rev=418641&r1=418640&r2=418641&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOCase.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOCase.java Sun Jul  2
12:42:19 2006
@@ -31,7 +31,7 @@
  * class to compare filenames.
  *
  * @author Stephen Colebourne
- * @version $Id$
+ * @version $Id: IOCase.java 417090 2006-06-25 23:18:33Z scolebourne $
  * @since Commons IO 1.3
  */
 public final class IOCase implements Serializable {
@@ -187,6 +187,20 @@
      */
     public boolean checkRegionMatches(String str, int strStartIndex, String search) {
         return str.regionMatches(!sensitive, strStartIndex, search, 0, search.length());
+    }
+
+    /**
+     * Converts the case of the input String to a standard format.
+     * Subsequent operations can then use standard String methods.
+     * 
+     * @param str  the string to convert, null returns null
+     * @return the lower-case version if case-insensitive
+     */
+    String convertCase(String str) {
+        if (str == null) {
+            return null;
+        }
+        return sensitive ? str : str.toLowerCase();
     }
 
     //-----------------------------------------------------------------------

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java?rev=418641&r1=418640&r2=418641&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java
(original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java
Sun Jul  2 12:42:19 2006
@@ -45,8 +45,13 @@
         assertEquals(true, FilenameUtils.wildcardMatch("Foo Bar Foo", "F*o Bar*"));
         assertEquals(true, FilenameUtils.wildcardMatch("Adobe Acrobat Installer", "Ad*er"));
         assertEquals(true, FilenameUtils.wildcardMatch("Foo", "*Foo"));
+        assertEquals(true, FilenameUtils.wildcardMatch("BarFoo", "*Foo"));
         assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo*"));
+        assertEquals(true, FilenameUtils.wildcardMatch("FooBar", "Foo*"));
+        assertEquals(false, FilenameUtils.wildcardMatch("FOO", "*Foo"));
+        assertEquals(false, FilenameUtils.wildcardMatch("BARFOO", "*Foo"));
         assertEquals(false, FilenameUtils.wildcardMatch("FOO", "Foo*"));
+        assertEquals(false, FilenameUtils.wildcardMatch("FOOBAR", "Foo*"));
     }
 
     public void testMatchOnSystem() {
@@ -63,8 +68,13 @@
         assertEquals(true, FilenameUtils.wildcardMatchOnSystem("Foo Bar Foo", "F*o Bar*"));
         assertEquals(true, FilenameUtils.wildcardMatchOnSystem("Adobe Acrobat Installer",
"Ad*er"));
         assertEquals(true, FilenameUtils.wildcardMatchOnSystem("Foo", "*Foo"));
+        assertEquals(true, FilenameUtils.wildcardMatchOnSystem("BarFoo", "*Foo"));
         assertEquals(true, FilenameUtils.wildcardMatchOnSystem("Foo", "Foo*"));
+        assertEquals(true, FilenameUtils.wildcardMatchOnSystem("FooBar", "Foo*"));
+        assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem("FOO", "*Foo"));
+        assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem("BARFOO", "*Foo"));
         assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem("FOO", "Foo*"));
+        assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem("FOOBAR", "Foo*"));
     }
 
     public void testMatchCaseSpecified() {
@@ -82,9 +92,23 @@
         assertEquals(true, FilenameUtils.wildcardMatch("Adobe Acrobat Installer", "Ad*er",
IOCase.SENSITIVE));
         assertEquals(true, FilenameUtils.wildcardMatch("Foo", "*Foo", IOCase.SENSITIVE));
         assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo*", IOCase.SENSITIVE));
+        assertEquals(true, FilenameUtils.wildcardMatch("Foo", "*Foo", IOCase.SENSITIVE));
+        assertEquals(true, FilenameUtils.wildcardMatch("BarFoo", "*Foo", IOCase.SENSITIVE));
+        assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo*", IOCase.SENSITIVE));
+        assertEquals(true, FilenameUtils.wildcardMatch("FooBar", "Foo*", IOCase.SENSITIVE));
+        
+        assertEquals(false, FilenameUtils.wildcardMatch("FOO", "*Foo", IOCase.SENSITIVE));
+        assertEquals(false, FilenameUtils.wildcardMatch("BARFOO", "*Foo", IOCase.SENSITIVE));
         assertEquals(false, FilenameUtils.wildcardMatch("FOO", "Foo*", IOCase.SENSITIVE));
+        assertEquals(false, FilenameUtils.wildcardMatch("FOOBAR", "Foo*", IOCase.SENSITIVE));
+        assertEquals(true, FilenameUtils.wildcardMatch("FOO", "*Foo", IOCase.INSENSITIVE));
+        assertEquals(true, FilenameUtils.wildcardMatch("BARFOO", "*Foo", IOCase.INSENSITIVE));
         assertEquals(true, FilenameUtils.wildcardMatch("FOO", "Foo*", IOCase.INSENSITIVE));
+        assertEquals(true, FilenameUtils.wildcardMatch("FOOBAR", "Foo*", IOCase.INSENSITIVE));
+        assertEquals(WINDOWS, FilenameUtils.wildcardMatch("FOO", "*Foo", IOCase.SYSTEM));
+        assertEquals(WINDOWS, FilenameUtils.wildcardMatch("BARFOO", "*Foo", IOCase.SYSTEM));
         assertEquals(WINDOWS, FilenameUtils.wildcardMatch("FOO", "Foo*", IOCase.SYSTEM));
+        assertEquals(WINDOWS, FilenameUtils.wildcardMatch("FOOBAR", "Foo*", IOCase.SYSTEM));
     }
 
     public void testSplitOnTokens() {

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java?rev=418641&r1=418640&r2=418641&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java
(original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java
Sun Jul  2 12:42:19 2006
@@ -27,6 +27,7 @@
 import junit.textui.TestRunner;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOCase;
 import org.apache.commons.io.testtools.FileBasedTestCase;
 
 /**
@@ -34,6 +35,8 @@
  */
 public class FileFilterTestCase extends FileBasedTestCase {
 
+    private static final boolean WINDOWS = (File.separatorChar == '\\');
+
     public FileFilterTestCase(String name) {
         super(name);
     }
@@ -320,7 +323,7 @@
         assertEquals(true, f.getFileFilters().isEmpty());
     }
 
-    public void testWildcard() throws Exception {
+    public void testDeprecatedWildcard() throws Exception {
         IOFileFilter filter = new WildcardFilter("*.txt");
         List patternList = Arrays.asList( new String[] { "*.txt", "*.xml", "*.gif" } );
         IOFileFilter listFilter = new WildcardFilter( patternList );
@@ -380,7 +383,81 @@
             // expected
         }
     }
-    
+
+    public void testWildcard() throws Exception {
+        IOFileFilter filter = new WildcardFileFilter("*.txt");
+        assertFiltering(filter, new File("log.txt"), true);
+        assertFiltering(filter, new File("log.TXT"), false);
+        
+        filter = new WildcardFileFilter("*.txt", IOCase.SENSITIVE);
+        assertFiltering(filter, new File("log.txt"), true);
+        assertFiltering(filter, new File("log.TXT"), false);
+        
+        filter = new WildcardFileFilter("*.txt", IOCase.INSENSITIVE);
+        assertFiltering(filter, new File("log.txt"), true);
+        assertFiltering(filter, new File("log.TXT"), true);
+        
+        filter = new WildcardFileFilter("*.txt", IOCase.SYSTEM);
+        assertFiltering(filter, new File("log.txt"), true);
+        assertFiltering(filter, new File("log.TXT"), WINDOWS);
+        
+        filter = new WildcardFileFilter("*.txt", (IOCase) null);
+        assertFiltering(filter, new File("log.txt"), true);
+        assertFiltering(filter, new File("log.TXT"), false);
+        
+        filter = new WildcardFileFilter(new String[] {"*.java", "*.class"});
+        assertFiltering(filter, new File("Test.java"), true);
+        assertFiltering(filter, new File("Test.class"), true);
+        assertFiltering(filter, new File("Test.jsp"), false);
+        
+        filter = new WildcardFileFilter(new String[] {"*.java", "*.class"}, IOCase.SENSITIVE);
+        assertFiltering(filter, new File("Test.java"), true);
+        assertFiltering(filter, new File("Test.JAVA"), false);
+        
+        filter = new WildcardFileFilter(new String[] {"*.java", "*.class"}, IOCase.INSENSITIVE);
+        assertFiltering(filter, new File("Test.java"), true);
+        assertFiltering(filter, new File("Test.JAVA"), true);
+        
+        filter = new WildcardFileFilter(new String[] {"*.java", "*.class"}, IOCase.SYSTEM);
+        assertFiltering(filter, new File("Test.java"), true);
+        assertFiltering(filter, new File("Test.JAVA"), WINDOWS);
+        
+        filter = new WildcardFileFilter(new String[] {"*.java", "*.class"}, (IOCase) null);
+        assertFiltering(filter, new File("Test.java"), true);
+        assertFiltering(filter, new File("Test.JAVA"), false);
+        
+        List patternList = Arrays.asList( new String[] { "*.txt", "*.xml", "*.gif" } );
+        IOFileFilter listFilter = new WildcardFileFilter( patternList );
+        assertFiltering(listFilter, new File("Test.txt"), true);
+        assertFiltering(listFilter, new File("Test.xml"), true);
+        assertFiltering(listFilter, new File("Test.gif"), true);
+        assertFiltering(listFilter, new File("Test.bmp"), false);
+        
+        File txtFile = new File( "test.txt" );
+        File bmpFile = new File( "test.bmp" );
+        File dir = new File( "src/java" );
+        assertTrue( listFilter.accept( txtFile ) );
+        assertTrue( !listFilter.accept( bmpFile ) );
+        assertTrue( !listFilter.accept( dir ) );
+        
+        assertTrue( listFilter.accept( txtFile.getParentFile(), txtFile.getName() ) );
+        assertTrue( !listFilter.accept( bmpFile.getParentFile(), bmpFile.getName() ) );
+        assertTrue( !listFilter.accept( dir.getParentFile(), dir.getName() ) );
+        
+        try {
+            new WildcardFileFilter((String) null);
+            fail();
+        } catch (IllegalArgumentException ex) {}
+        try {
+            new WildcardFileFilter((String[]) null);
+            fail();
+        } catch (IllegalArgumentException ex) {}
+        try {
+            new WildcardFileFilter((List) null);
+            fail();
+        } catch (IllegalArgumentException ex) {}
+    }
+
     public void testDelegateFileFilter() throws Exception {
     	OrFileFilter orFilter = new OrFileFilter();
     	File testFile = new File( "test.txt" );



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message