scolebourne    2004/10/30 16:12:18

  Modified:    io/src/test/org/apache/commons/io FilenameUtilsTestCase.java
               io/src/java/org/apache/commons/io FilenameUtils.java
  Log:
  Refactor getXxx methods in FilenameUtils
  
  Revision  Changes    Path
  1.16      +36 -53    
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.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- FilenameUtilsTestCase.java        30 Oct 2004 22:43:21 -0000      1.15
  +++ FilenameUtilsTestCase.java        30 Oct 2004 23:12:18 -0000      1.16
  @@ -75,24 +75,6 @@
           FileUtils.deleteDirectory(getTestDirectory());
       }
   
  -    // removePath
  -
  -    public void testRemovePath() {
  -        String fileName =
  -            FilenameUtils.removePath(
  -                new File(getTestDirectory(), getName()).getAbsolutePath());
  -        assertEquals(getName(), fileName);
  -    }
  -
  -    // getPath
  -
  -    public void testGetPath() {
  -        String fileName =
  -            FilenameUtils.getPath(
  -                new File(getTestDirectory(), getName()).getAbsolutePath());
  -        assertEquals(getTestDirectory().getAbsolutePath(), fileName);
  -    }
  -
       // catPath
   
       public void testCatPath() {
  @@ -189,40 +171,6 @@
           return sb.toString();
       }
   
  -    public void testGetExtension() {
  -        String[][] tests = {
  -                { "filename.ext", "ext" }, 
  -                { "README", "" }, 
  -                { "domain.dot.com", "com" }, 
  -                { "image.jpeg", "jpeg" },
  -                { "a.b/c", "" },
  -                { "a.b/c.txt", "txt" },
  -                { "a/b/c", "" },
  -        };
  -        for (int i = 0; i < tests.length; i++) {
  -            assertEquals(tests[i][1], FilenameUtils.getExtension(tests[i][0]));
  -            //assertEquals(tests[i][1], FilenameUtils.extension(tests[i][0]));
  -        }
  -    }
  -
  -    public void testGetExtensionWithPaths() {
  -        String[][] testsWithPaths =
  -            { { "/tmp/foo/filename.ext", "ext" }, {
  -                "C:\\temp\\foo\\filename.ext", "ext" }, {
  -                "/tmp/foo.bar/filename.ext", "ext" }, {
  -                "C:\\temp\\foo.bar\\filename.ext", "ext" }, {
  -                "/tmp/foo.bar/README", "" }, {
  -                "C:\\temp\\foo.bar\\README", "" }, {
  -                "../filename.ext", "ext" }
  -        };
  -        for (int i = 0; i < testsWithPaths.length; i++) {
  -            assertEquals(
  -                testsWithPaths[i][1],
  -                FilenameUtils.getExtension(testsWithPaths[i][0]));
  -            //assertEquals(testsWithPaths[i][1], 
FilenameUtils.extension(testsWithPaths[i][0]));
  -        }
  -    }
  -
       public void testRemoveExtension() {
           String[][] tests = { 
                   { "filename.ext", "filename" }, 
  @@ -314,6 +262,41 @@
           assertEquals(-1, FilenameUtils.indexOfExtension("a\\b\\c"));
           assertEquals(-1, FilenameUtils.indexOfExtension("a/b.notextension/c"));
           assertEquals(-1, FilenameUtils.indexOfExtension("a\\b.notextension\\c"));
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    public void testGetPath() {
  +        assertEquals(null, FilenameUtils.getPath(null));
  +        assertEquals("", FilenameUtils.getPath("noseperator.inthispath"));
  +        assertEquals("a/b", FilenameUtils.getPath("a/b/c.txt"));
  +        assertEquals("a/b", FilenameUtils.getPath("a/b/c"));
  +        assertEquals("a/b/c", FilenameUtils.getPath("a/b/c/"));
  +        assertEquals("a\\b", FilenameUtils.getPath("a\\b\\c"));
  +    }
  +
  +    public void testRemovePath() {
  +        assertEquals(null, FilenameUtils.getName(null));
  +        assertEquals("noseperator.inthispath", 
FilenameUtils.getName("noseperator.inthispath"));
  +        assertEquals("c.txt", FilenameUtils.getName("a/b/c.txt"));
  +        assertEquals("c", FilenameUtils.getName("a/b/c"));
  +        assertEquals("", FilenameUtils.getName("a/b/c/"));
  +        assertEquals("c", FilenameUtils.getName("a\\b\\c"));
  +    }
  +
  +    public void testGetExtension() {
  +        assertEquals(null, FilenameUtils.getExtension(null));
  +        assertEquals("ext", FilenameUtils.getExtension("file.ext"));
  +        assertEquals("", FilenameUtils.getExtension("README"));
  +        assertEquals("com", FilenameUtils.getExtension("domain.dot.com"));
  +        assertEquals("jpeg", FilenameUtils.getExtension("image.jpeg"));
  +        assertEquals("", FilenameUtils.getExtension("a.b/c"));
  +        assertEquals("txt", FilenameUtils.getExtension("a.b/c.txt"));
  +        assertEquals("", FilenameUtils.getExtension("a/b/c"));
  +        assertEquals("", FilenameUtils.getExtension("a.b\\c"));
  +        assertEquals("txt", FilenameUtils.getExtension("a.b\\c.txt"));
  +        assertEquals("", FilenameUtils.getExtension("a\\b\\c"));
  +        assertEquals("", FilenameUtils.getExtension("C:\\temp\\foo.bar\\README"));
  +        assertEquals("ext", FilenameUtils.getExtension("../filename.ext"));
       }
   
   }
  
  
  
  1.22      +91 -126   
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.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- FilenameUtils.java        30 Oct 2004 22:43:21 -0000      1.21
  +++ FilenameUtils.java        30 Oct 2004 23:12:18 -0000      1.22
  @@ -128,121 +128,6 @@
           return filename.substring(0, index);
       }
   
  -   /**
  -     * Gets the extension of a filename.
  -     * <p>
  -     * eg
  -     * <pre>
  -     * foo.txt      --> "txt"
  -     * a/b/c.jpg    --> "jpg"
  -     * a/b/c        --> ""
  -     * a.b/c.txt    --> "txt"
  -     * a.b/c        --> ""
  -     * </pre>
  -     *
  -     * @param filename the filename to retrieve the extension of.
  -     * @return the extension of filename or an empty string if none exists.
  -     */
  -    public static String getExtension(String filename) {
  -        String suffix = "";
  -        String shortFilename = filename;
  -        String ifilename = internalize(filename);
  -
  -        int lastDirSeparator = ifilename.lastIndexOf(INTERNAL_SEPARATOR_CHAR);
  -        if (lastDirSeparator > 0) {
  -            shortFilename = ifilename.substring(lastDirSeparator + 1);
  -        }
  -
  -        int index = shortFilename.lastIndexOf('.');
  -
  -        if (index > 0 && index < shortFilename.length() - 1) {
  -            suffix = shortFilename.substring(index + 1);
  -        }
  -
  -        return suffix;
  -    }
  -
  -    /**
  -     * Remove path from filename. Equivalent to the unix command
  -     * <code>basename</code>.
  -     * ie.
  -     * <pre>
  -     * a/b/c.txt --> c.txt
  -     * a.txt     --> a.txt
  -     * </pre>
  -     *
  -     * @param filepath the filepath
  -     * @return the filename minus path
  -     */
  -    // KILL? Just use StringUtils?
  -    public static String removePath(String filepath) {
  -        return removePath(filepath, File.separatorChar);
  -    }
  -
  -    /**
  -     * Remove path from filename.
  -     * ie.
  -     * <pre>
  -     * a/b/c.txt --> c.txt
  -     * a.txt     --> a.txt
  -     * </pre>
  -     *
  -     * @param filepath the filepath
  -     * @param fileSeparatorChar the file separator character to use
  -     * @return the filename minus path
  -     */
  -    // KILL: Why allow the char to be specified?
  -    public static String removePath( String filepath, char fileSeparatorChar) {
  -        int index = filepath.lastIndexOf(fileSeparatorChar);
  -
  -        if (-1 == index) {
  -            return filepath;
  -        } else {
  -            return filepath.substring(index + 1);
  -        }
  -    }
  -
  -    /**
  -     * Get path from filename. Roughly equivalent to the unix command
  -     * <code>dirname</code>.
  -     * ie.
  -     * <pre>
  -     * a/b/c.txt --> a/b
  -     * a.txt     --> ""
  -     * </pre>
  -     *
  -     * @param filepath the filepath
  -     * @return the filename minus path
  -     */
  -    // KILL? Just use StringUtils?
  -    public static String getPath(String filepath) {
  -        return getPath(filepath, File.separatorChar);
  -    }
  -
  -    /**
  -     * Get path from filename.
  -     * ie.
  -     * <pre>
  -     * a/b/c.txt --> a/b
  -     * a.txt     --> ""
  -     * </pre>
  -     *
  -     * @param filepath the filepath
  -     * @param fileSeparatorChar the file separator character to use
  -     * @return the filename minus path
  -     */
  -    // KILL: Why allow the char to be specified?
  -    public static String getPath( String filepath, char fileSeparatorChar) {
  -        int index = filepath.lastIndexOf(fileSeparatorChar);
  -        if (-1 == index) {
  -            return "";
  -        } else {
  -            return filepath.substring(0, index);
  -        }
  -    }
  -
  -
  -
       /**
        * Normalize a path.
        * Eliminates "/../" and "/./" in a string. Returns <code>null</code> if
  @@ -500,16 +385,16 @@
        * This method will handle a file in either Unix or Windows format.
        * The position of the last forward or backslash is returned.
        * 
  -     * @param path  the path to find the last path separator in
  +     * @param filename  the filename to find the last path separator in, null 
returns -1
        * @return the index of the last separator character, or -1 if there
        * is no such character.
        */
  -    public static int indexOfLastSeparator(String path) {
  -        if (path == null) {
  +    public static int indexOfLastSeparator(String filename) {
  +        if (filename == null) {
               return -1;
           }
  -        int lastUnixPos = path.lastIndexOf(UNIX_SEPARATOR);
  -        int lastWindowsPos = path.lastIndexOf(WINDOWS_SEPARATOR);
  +        int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR);
  +        int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
           return Math.max(lastUnixPos, lastWindowsPos);
       }
   
  @@ -520,17 +405,97 @@
        * To do this it uses [EMAIL PROTECTED] #indexOfLastSeparator(String)} which 
will
        * handle a file in either Unix or Windows format.
        * 
  -     * @param path  the path to find the last path separator in
  +     * @param filename  the filename to find the last path separator in, null 
returns -1
        * @return the index of the last separator character, or -1 if there
        * is no such character.
        */
  -    public static int indexOfExtension(String path) {
  -        if (path == null) {
  +    public static int indexOfExtension(String filename) {
  +        if (filename == null) {
               return -1;
           }
  -        int extensionPos = path.lastIndexOf(EXTENSION_SEPARATOR);
  -        int lastSeparator = indexOfLastSeparator(path);
  +        int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
  +        int lastSeparator = indexOfLastSeparator(filename);
           return (lastSeparator > extensionPos ? -1 : extensionPos);
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Gets the path from a full filename.
  +     * <p>
  +     * This method will handle a file in either Unix or Windows format.
  +     * The text before the last forward or backslash is returned.
  +     * This method is roughly equivalent to the unix command <code>dirname</code>.
  +     * <pre>
  +     * a/b/c.txt --> a/b
  +     * a.txt     --> ""
  +     * a/b/c     --> a/b
  +     * a/b/c/    --> a/b/c
  +     * </pre>
  +     *
  +     * @param filename  the filename to query, null returns null
  +     * @return the filename minus path
  +     */
  +    public static String getPath(String filename) {
  +        if (filename == null) {
  +            return null;
  +        }
  +        int index = indexOfLastSeparator(filename);
  +        if (index == -1) {
  +            return "";
  +        } else {
  +            return filename.substring(0, index);
  +        }
  +    }
  +
  +    /**
  +     * Gets the name minus the path from a full filename.
  +     * <p>
  +     * This method will handle a file in either Unix or Windows format.
  +     * The text after the last forward or backslash is returned.
  +     * This method is roughly equivalent to the unix command <code>basename</code>.
  +     * <pre>
  +     * a/b/c.txt --> c.txt
  +     * a.txt     --> a.txt
  +     * a/b/c     --> c
  +     * a/b/c/    --> ""
  +     * </pre>
  +     *
  +     * @param filename  the filename to query, null returns null
  +     * @return the filename minus path
  +     */
  +    public static String getName(String filename) {
  +        if (filename == null) {
  +            return null;
  +        }
  +        int index = indexOfLastSeparator(filename);
  +        return filename.substring(index + 1);
  +    }
  +
  +    /**
  +     * Gets the extension of a filename.
  +     * <p>
  +     * This method returns the textual part of the filename after the last dot.
  +     * There must be no directory separator after the dot.
  +     * <pre>
  +     * foo.txt      --> "txt"
  +     * a/b/c.jpg    --> "jpg"
  +     * a/b.txt/c    --> ""
  +     * a/b/c        --> ""
  +     * </pre>
  +     *
  +     * @param filename the filename to retrieve the extension of.
  +     * @return the extension of filename or an empty string if none exists.
  +     */
  +    public static String getExtension(String filename) {
  +        if (filename == null) {
  +            return null;
  +        }
  +        int index = indexOfExtension(filename);
  +        if (index == -1) {
  +            return "";
  +        } else {
  +            return filename.substring(index + 1);
  +        }
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to