commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/io/src/java/org/apache/commons/io FilenameUtils.java
Date Sat, 30 Oct 2004 23:59:17 GMT
scolebourne    2004/10/30 16:59:17

  Modified:    io/src/test/org/apache/commons/io FilenameUtilsTestCase.java
               io/src/java/org/apache/commons/io FilenameUtils.java
  Log:
  Add isExtension methods to test for matching extensions in FilenameUtils
  
  Revision  Changes    Path
  1.18      +132 -10   jakarta-commons/io/src/test/org/apache/commons/io/FilenameUtilsTestCase.java
  
  Index: FilenameUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/FilenameUtilsTestCase.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- FilenameUtilsTestCase.java	30 Oct 2004 23:23:53 -0000	1.17
  +++ FilenameUtilsTestCase.java	30 Oct 2004 23:59:17 -0000	1.18
  @@ -17,13 +17,16 @@
   
   import java.io.File;
   import java.io.IOException;
  -
  -import org.apache.commons.io.testtools.FileBasedTestCase;
  +import java.util.ArrayList;
  +import java.util.Arrays;
  +import java.util.Collection;
   
   import junit.framework.Test;
   import junit.framework.TestSuite;
   import junit.textui.TestRunner;
   
  +import org.apache.commons.io.testtools.FileBasedTestCase;
  +
   /**
    * This is used to test FilenameUtils for correctness.
    *
  @@ -34,6 +37,8 @@
    * @see FilenameUtils
    */
   public class FilenameUtilsTestCase extends FileBasedTestCase {
  +    
  +    private static final boolean WINDOWS = (File.separatorChar == '\\');
   
       private File testFile1;
       private File testFile2;
  @@ -191,20 +196,20 @@
       }
   
       public void testSeparatorsToSystem() {
  -        if (File.separatorChar == '/') {
  -            assertEquals(null, FilenameUtils.separatorsToSystem(null));
  -            assertEquals("/a/b/c", FilenameUtils.separatorsToSystem("/a/b/c"));
  -            assertEquals("/a/b/c.txt", FilenameUtils.separatorsToSystem("/a/b/c.txt"));
  -            assertEquals("/a/b/c", FilenameUtils.separatorsToSystem("/a/b\\c"));
  -            assertEquals("/a/b/c", FilenameUtils.separatorsToSystem("\\a\\b\\c"));
  -            assertEquals("D:/a/b/c", FilenameUtils.separatorsToSystem("D:\\a\\b\\c"));
  -        } else {
  +        if (WINDOWS) {
               assertEquals(null, FilenameUtils.separatorsToSystem(null));
               assertEquals("\\a\\b\\c", FilenameUtils.separatorsToSystem("\\a\\b\\c"));
               assertEquals("\\a\\b\\c.txt", FilenameUtils.separatorsToSystem("\\a\\b\\c.txt"));
               assertEquals("\\a\\b\\c", FilenameUtils.separatorsToSystem("\\a\\b/c"));
               assertEquals("\\a\\b\\c", FilenameUtils.separatorsToSystem("/a/b/c"));
               assertEquals("D:\\a\\b\\c", FilenameUtils.separatorsToSystem("D:/a/b/c"));
  +        } else {
  +            assertEquals(null, FilenameUtils.separatorsToSystem(null));
  +            assertEquals("/a/b/c", FilenameUtils.separatorsToSystem("/a/b/c"));
  +            assertEquals("/a/b/c.txt", FilenameUtils.separatorsToSystem("/a/b/c.txt"));
  +            assertEquals("/a/b/c", FilenameUtils.separatorsToSystem("/a/b\\c"));
  +            assertEquals("/a/b/c", FilenameUtils.separatorsToSystem("\\a\\b\\c"));
  +            assertEquals("D:/a/b/c", FilenameUtils.separatorsToSystem("D:\\a\\b\\c"));
           }
       }
   
  @@ -276,6 +281,123 @@
           assertEquals("a\\b\\c", FilenameUtils.removeExtension("a\\b\\c"));
           assertEquals("C:\\temp\\foo.bar\\README", FilenameUtils.removeExtension("C:\\temp\\foo.bar\\README"));
           assertEquals("../filename", FilenameUtils.removeExtension("../filename.ext"));
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    public void testIsExtension() {
  +        assertEquals(false, FilenameUtils.isExtension(null, (String) null));
  +        assertEquals(false, FilenameUtils.isExtension("file.txt", (String) null));
  +        assertEquals(false, FilenameUtils.isExtension("file.txt", ""));
  +        assertEquals(true, FilenameUtils.isExtension("file.txt", "txt"));
  +        assertEquals(false, FilenameUtils.isExtension("file.txt", "rtf"));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a/b/file.txt", (String) null));
  +        assertEquals(false, FilenameUtils.isExtension("a/b/file.txt", ""));
  +        assertEquals(true, FilenameUtils.isExtension("a/b/file.txt", "txt"));
  +        assertEquals(false, FilenameUtils.isExtension("a/b/file.txt", "rtf"));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a.b/file.txt", (String) null));
  +        assertEquals(false, FilenameUtils.isExtension("a.b/file.txt", ""));
  +        assertEquals(true, FilenameUtils.isExtension("a.b/file.txt", "txt"));
  +        assertEquals(false, FilenameUtils.isExtension("a.b/file.txt", "rtf"));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a\\b\\file.txt", (String) null));
  +        assertEquals(false, FilenameUtils.isExtension("a\\b\\file.txt", ""));
  +        assertEquals(true, FilenameUtils.isExtension("a\\b\\file.txt", "txt"));
  +        assertEquals(false, FilenameUtils.isExtension("a\\b\\file.txt", "rtf"));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", (String) null));
  +        assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", ""));
  +        assertEquals(true, FilenameUtils.isExtension("a.b\\file.txt", "txt"));
  +        assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", "rtf"));
  +        
  +        if (WINDOWS) {
  +            assertEquals(true, FilenameUtils.isExtension("a.b\\file.txt", "TXT"));
  +        } else {
  +            assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", "TXT"));
  +        }
  +    }
  +
  +    public void testIsExtensionArray() {
  +        assertEquals(false, FilenameUtils.isExtension(null, (String[]) null));
  +        assertEquals(false, FilenameUtils.isExtension("file.txt", (String[]) null));
  +        assertEquals(false, FilenameUtils.isExtension("file.txt", new String[0]));
  +        assertEquals(true, FilenameUtils.isExtension("file.txt", new String[] {"txt"}));
  +        assertEquals(false, FilenameUtils.isExtension("file.txt", new String[] {"rtf"}));
  +        assertEquals(true, FilenameUtils.isExtension("file.txt", new String[] {"rtf", "txt"}));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a/b/file.txt", (String[]) null));
  +        assertEquals(false, FilenameUtils.isExtension("a/b/file.txt", new String[0]));
  +        assertEquals(true, FilenameUtils.isExtension("a/b/file.txt", new String[] {"txt"}));
  +        assertEquals(false, FilenameUtils.isExtension("a/b/file.txt", new String[] {"rtf"}));
  +        assertEquals(true, FilenameUtils.isExtension("a/b/file.txt", new String[] {"rtf",
"txt"}));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a.b/file.txt", (String[]) null));
  +        assertEquals(false, FilenameUtils.isExtension("a.b/file.txt", new String[0]));
  +        assertEquals(true, FilenameUtils.isExtension("a.b/file.txt", new String[] {"txt"}));
  +        assertEquals(false, FilenameUtils.isExtension("a.b/file.txt", new String[] {"rtf"}));
  +        assertEquals(true, FilenameUtils.isExtension("a.b/file.txt", new String[] {"rtf",
"txt"}));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a\\b\\file.txt", (String[]) null));
  +        assertEquals(false, FilenameUtils.isExtension("a\\b\\file.txt", new String[0]));
  +        assertEquals(true, FilenameUtils.isExtension("a\\b\\file.txt", new String[] {"txt"}));
  +        assertEquals(false, FilenameUtils.isExtension("a\\b\\file.txt", new String[] {"rtf"}));
  +        assertEquals(true, FilenameUtils.isExtension("a\\b\\file.txt", new String[] {"rtf",
"txt"}));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", (String[]) null));
  +        assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", new String[0]));
  +        assertEquals(true, FilenameUtils.isExtension("a.b\\file.txt", new String[] {"txt"}));
  +        assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", new String[] {"rtf"}));
  +        assertEquals(true, FilenameUtils.isExtension("a.b\\file.txt", new String[] {"rtf",
"txt"}));
  +        
  +        if (WINDOWS) {
  +            assertEquals(true, FilenameUtils.isExtension("a.b\\file.txt", new String[]
{"TXT"}));
  +            assertEquals(true, FilenameUtils.isExtension("a.b\\file.txt", new String[]
{"TXT", "RTF"}));
  +        } else {
  +            assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", new String[]
{"TXT"}));
  +            assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", new String[]
{"TXT", "RTF"}));
  +        }
  +    }
  +
  +    public void testIsExtensionCollection() {
  +        assertEquals(false, FilenameUtils.isExtension(null, (Collection) null));
  +        assertEquals(false, FilenameUtils.isExtension("file.txt", (Collection) null));
  +        assertEquals(false, FilenameUtils.isExtension("file.txt", new ArrayList()));
  +        assertEquals(true, FilenameUtils.isExtension("file.txt", new ArrayList(Arrays.asList(new
String[] {"txt"}))));
  +        assertEquals(false, FilenameUtils.isExtension("file.txt", new ArrayList(Arrays.asList(new
String[] {"rtf"}))));
  +        assertEquals(true, FilenameUtils.isExtension("file.txt", new ArrayList(Arrays.asList(new
String[] {"rtf", "txt"}))));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a/b/file.txt", (Collection) null));
  +        assertEquals(false, FilenameUtils.isExtension("a/b/file.txt", new ArrayList()));
  +        assertEquals(true, FilenameUtils.isExtension("a/b/file.txt", new ArrayList(Arrays.asList(new
String[] {"txt"}))));
  +        assertEquals(false, FilenameUtils.isExtension("a/b/file.txt", new ArrayList(Arrays.asList(new
String[] {"rtf"}))));
  +        assertEquals(true, FilenameUtils.isExtension("a/b/file.txt", new ArrayList(Arrays.asList(new
String[] {"rtf", "txt"}))));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a.b/file.txt", (Collection) null));
  +        assertEquals(false, FilenameUtils.isExtension("a.b/file.txt", new ArrayList()));
  +        assertEquals(true, FilenameUtils.isExtension("a.b/file.txt", new ArrayList(Arrays.asList(new
String[] {"txt"}))));
  +        assertEquals(false, FilenameUtils.isExtension("a.b/file.txt", new ArrayList(Arrays.asList(new
String[] {"rtf"}))));
  +        assertEquals(true, FilenameUtils.isExtension("a.b/file.txt", new ArrayList(Arrays.asList(new
String[] {"rtf", "txt"}))));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a\\b\\file.txt", (Collection) null));
  +        assertEquals(false, FilenameUtils.isExtension("a\\b\\file.txt", new ArrayList()));
  +        assertEquals(true, FilenameUtils.isExtension("a\\b\\file.txt", new ArrayList(Arrays.asList(new
String[] {"txt"}))));
  +        assertEquals(false, FilenameUtils.isExtension("a\\b\\file.txt", new ArrayList(Arrays.asList(new
String[] {"rtf"}))));
  +        assertEquals(true, FilenameUtils.isExtension("a\\b\\file.txt", new ArrayList(Arrays.asList(new
String[] {"rtf", "txt"}))));
  +        
  +        assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", (Collection) null));
  +        assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", new ArrayList()));
  +        assertEquals(true, FilenameUtils.isExtension("a.b\\file.txt", new ArrayList(Arrays.asList(new
String[] {"txt"}))));
  +        assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", new ArrayList(Arrays.asList(new
String[] {"rtf"}))));
  +        assertEquals(true, FilenameUtils.isExtension("a.b\\file.txt", new ArrayList(Arrays.asList(new
String[] {"rtf", "txt"}))));
  +        
  +        if (WINDOWS) {
  +            assertEquals(true, FilenameUtils.isExtension("a.b\\file.txt", new ArrayList(Arrays.asList(new
String[] {"TXT"}))));
  +            assertEquals(true, FilenameUtils.isExtension("a.b\\file.txt", new ArrayList(Arrays.asList(new
String[] {"TXT", "RTF"}))));
  +        } else {
  +            assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", new ArrayList(Arrays.asList(new
String[] {"TXT"}))));
  +            assertEquals(false, FilenameUtils.isExtension("a.b\\file.txt", new ArrayList(Arrays.asList(new
String[] {"TXT", "RTF"}))));
  +        }
       }
   
   }
  
  
  
  1.24      +86 -1     jakarta-commons/io/src/java/org/apache/commons/io/FilenameUtils.java
  
  Index: FilenameUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/FilenameUtils.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- FilenameUtils.java	30 Oct 2004 23:23:54 -0000	1.23
  +++ FilenameUtils.java	30 Oct 2004 23:59:17 -0000	1.24
  @@ -17,6 +17,7 @@
   
   import java.io.File;
   import java.io.IOException;
  +import java.util.Collection;
   
   /**
    * Utility class that provides methods to manipulate filenames and filepaths.
  @@ -465,6 +466,90 @@
           } else {
               return filename.substring(0, index);
           }
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Checks whether the extension of the filename is that specified.
  +     * <p>
  +     * This method obtains the extension as the textual part of the filename
  +     * after the last dot. There must be no directory separator after the dot.
  +     * The extension check is case sensitive on Unix and case insensitive on Windows.
  +     *
  +     * @param filename  the filename to query, null returns false
  +     * @param extension  the extension to check for, null or empty checks for no extension
  +     * @return true if the filename has the specified extension
  +     */
  +    public static boolean isExtension(String filename, String extension) {
  +        if (filename == null) {
  +            return false;
  +        }
  +        if (extension == null || extension.length() == 0) {
  +            return (indexOfExtension(filename) == -1);
  +        }
  +        String fileExt = getExtension(filename);
  +        if (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR) {
  +            return fileExt.equalsIgnoreCase(extension);
  +        } else {
  +            return fileExt.equals(extension);
  +        }
  +    }
  +
  +    /**
  +     * Checks whether the extension of the filename is one of those specified.
  +     * <p>
  +     * This method obtains the extension as the textual part of the filename
  +     * after the last dot. There must be no directory separator after the dot.
  +     * The extension check is case sensitive on Unix and case insensitive on Windows.
  +     *
  +     * @param filename  the filename to query, null returns false
  +     * @param extensions  the extensions to check for, null checks for no extension
  +     * @return true if the filename is one of the extensions
  +     */
  +    public static boolean isExtension(String filename, String[] extensions) {
  +        if (filename == null) {
  +            return false;
  +        }
  +        if (extensions == null) {
  +            return (indexOfExtension(filename) == -1);
  +        }
  +        String fileExt = getExtension(filename);
  +        if (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR) {
  +            for (int i = 0; i < extensions.length; i++) {
  +                if (fileExt.equalsIgnoreCase(extensions[i])) {
  +                    return true;
  +                }
  +            }
  +        } else {
  +            for (int i = 0; i < extensions.length; i++) {
  +                if (fileExt.equals(extensions[i])) {
  +                    return true;
  +                }
  +            }
  +        }
  +        return false;
  +    }
  +
  +    /**
  +     * Checks whether the extension of the filename is one of those specified.
  +     * <p>
  +     * This method obtains the extension as the textual part of the filename
  +     * after the last dot. There must be no directory separator after the dot.
  +     * The extension check is case sensitive on Unix and case insensitive on Windows.
  +     *
  +     * @param filename  the filename to query, null returns false
  +     * @param extensions  the extensions to check for, null checks for no extension
  +     * @return true if the filename is one of the extensions
  +     */
  +    public static boolean isExtension(String filename, Collection extensions) {
  +        if (filename == null) {
  +            return false;
  +        }
  +        if (extensions == null) {
  +            return (indexOfExtension(filename) == -1);
  +        }
  +        String[] array = (String[]) extensions.toArray(new String[extensions.size()]);
  +        return isExtension(filename, array);
       }
   
   }
  
  
  

---------------------------------------------------------------------
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