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, 27 Nov 2004 01:22:06 GMT
scolebourne    2004/11/26 17:22:06

  Modified:    io/src/test/org/apache/commons/io FilenameUtilsTestCase.java
               io/src/java/org/apache/commons/io FilenameUtils.java
  Log:
  Refactor normalize method, and simplify getPrefixLength
  
  Revision  Changes    Path
  1.21      +204 -216  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.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- FilenameUtilsTestCase.java	23 Nov 2004 00:04:29 -0000	1.20
  +++ FilenameUtilsTestCase.java	27 Nov 2004 01:22:05 -0000	1.21
  @@ -38,6 +38,7 @@
    */
   public class FilenameUtilsTestCase extends FileBasedTestCase {
       
  +    private static final String SEP = "" + File.separatorChar;
       private static final boolean WINDOWS = (File.separatorChar == '\\');
   
       private File testFile1;
  @@ -80,8 +81,7 @@
           FileUtils.deleteDirectory(getTestDirectory());
       }
   
  -    // catPath
  -
  +    //-----------------------------------------------------------------------
       public void testCatPath() {
           // TODO StringIndexOutOfBoundsException thrown if file doesn't contain slash.
           // Is this acceptable?
  @@ -93,96 +93,126 @@
           assertEquals("C:\\a" + File.separator + "d", FilenameUtils.catPath("C:\\a\\b\\c",
"../d"));
       }
   
  -    // resolveFile
  -
  -    public void testResolveFileDotDot() throws Exception {
  -        File file = FilenameUtils.resolveFile(getTestDirectory(), "..");
  -        assertEquals(
  -            "Check .. operator",
  -            file,
  -            getTestDirectory().getParentFile());
  -    }
  -
  -    public void testResolveFileDot() throws Exception {
  -        File file = FilenameUtils.resolveFile(getTestDirectory(), ".");
  -        assertEquals("Check . operator", file, getTestDirectory());
  -    }
  -
  -    // normalize
  -
  +    //-----------------------------------------------------------------------
       public void testNormalize() throws Exception {
  -        String[] src =
  -            {
  -                null,
  -                "",
  -                "/",
  -                "///",
  -                "/foo",
  -                "/foo//",
  -                "/./",
  -                "/foo/./",
  -                "/foo/./bar",
  -                "/foo/../bar",
  -                "/foo/../bar/../baz",
  -                "/foo/bar/../../baz",
  -                "/././",
  -                "/foo/./../bar",
  -                "/foo/.././bar/",
  -                "//foo//./bar",
  -                "/../",
  -                "/foo/../../",
  -                "../foo",
  -                "foo/../../bar",
  -                "foo/../bar" };
  -
  -        String[] dest =
  -            {
  -                null,
  -                "",
  -                "/",
  -                "/",
  -                "/foo",
  -                "/foo/",
  -                "/",
  -                "/foo/",
  -                "/foo/bar",
  -                "/bar",
  -                "/baz",
  -                "/baz",
  -                "/",
  -                "/bar",
  -                "/bar/",
  -                "/foo/bar",
  -                null,
  -                null,
  -                null,
  -                null,
  -                "bar" };
  -
  -        assertEquals("Oops, test writer goofed", src.length, dest.length);
  -
  -        for (int i = 0; i < src.length; i++) {
  -            String destStr = FilenameUtils.separatorsToSystem(dest[i]);
  -            String resultStr = FilenameUtils.normalize(src[i]);
  -            assertEquals(
  -                "Check if '" + src[i] + "' normalized to '" + destStr + "', was '" + resultStr
+ "'",
  -                destStr, resultStr);
  -        }
  -    }
  -
  -    private String replaceAll(
  -        String text,
  -        String lookFor,
  -        String replaceWith) {
  -        StringBuffer sb = new StringBuffer(text);
  -        while (true) {
  -            int idx = sb.toString().indexOf(lookFor);
  -            if (idx < 0) {
  -                break;
  -            }
  -            sb.replace(idx, idx + lookFor.length(), replaceWith);
  -        }
  -        return sb.toString();
  +        assertEquals(null, FilenameUtils.normalize(null));
  +        assertEquals(null, FilenameUtils.normalize(":"));
  +        assertEquals(null, FilenameUtils.normalize("1:\\a\\b\\c.txt"));
  +        assertEquals(null, FilenameUtils.normalize("1:"));
  +        assertEquals(null, FilenameUtils.normalize("1:a"));
  +        assertEquals(null, FilenameUtils.normalize("\\\\\\a\\b\\c.txt"));
  +        assertEquals(null, FilenameUtils.normalize("\\\\a"));
  +        assertEquals(null, FilenameUtils.normalize("~"));
  +        assertEquals(null, FilenameUtils.normalize("~user"));
  +        
  +        assertEquals("a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("a\\b/c.txt"));
  +        assertEquals("" + SEP + "a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("\\a\\b/c.txt"));
  +        assertEquals("C:" + SEP + "a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("C:\\a\\b/c.txt"));
  +        assertEquals("" + SEP + "" + SEP + "server" + SEP + "a" + SEP + "b" + SEP + "c.txt",
FilenameUtils.normalize("\\\\server\\a\\b/c.txt"));
  +        assertEquals("~" + SEP + "a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("~\\a\\b/c.txt"));
  +        assertEquals("~user" + SEP + "a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("~user\\a\\b/c.txt"));
  +        
  +        assertEquals("a" + SEP + "c", FilenameUtils.normalize("a/b/../c"));
  +        assertEquals("c", FilenameUtils.normalize("a/b/../../c"));
  +        assertEquals("c", FilenameUtils.normalize("a/b/../../c/"));
  +        assertEquals(null, FilenameUtils.normalize("a/b/../../../c"));
  +        assertEquals("a", FilenameUtils.normalize("a/b/.."));
  +        assertEquals("a", FilenameUtils.normalize("a/b/../"));
  +        assertEquals("", FilenameUtils.normalize("a/b/../.."));
  +        assertEquals("", FilenameUtils.normalize("a/b/../../"));
  +        assertEquals(null, FilenameUtils.normalize("a/b/../../.."));
  +        assertEquals("a" + SEP + "d", FilenameUtils.normalize("a/b/../c/../d"));
  +        assertEquals("a" + SEP + "d", FilenameUtils.normalize("a/b/../c/../d/"));
  +        assertEquals("a" + SEP + "b" + SEP + "d", FilenameUtils.normalize("a/b//d"));
  +        assertEquals("a" + SEP + "b", FilenameUtils.normalize("a/b/././."));
  +        assertEquals("a" + SEP + "b", FilenameUtils.normalize("a/b/./././"));
  +        assertEquals("a", FilenameUtils.normalize("./a/"));
  +        assertEquals("a", FilenameUtils.normalize("./a"));
  +        assertEquals("", FilenameUtils.normalize("./"));
  +        assertEquals("", FilenameUtils.normalize("."));
  +        assertEquals(null, FilenameUtils.normalize("../a"));
  +        assertEquals(null, FilenameUtils.normalize(".."));
  +        assertEquals("", FilenameUtils.normalize(""));
  +        
  +        assertEquals(SEP + "a" + SEP + "c", FilenameUtils.normalize("/a/b/../c"));
  +        assertEquals(SEP + "c", FilenameUtils.normalize("/a/b/../../c"));
  +        assertEquals(null, FilenameUtils.normalize("/a/b/../../../c"));
  +        assertEquals(SEP + "a", FilenameUtils.normalize("/a/b/.."));
  +        assertEquals(SEP + "", FilenameUtils.normalize("/a/b/../.."));
  +        assertEquals(null, FilenameUtils.normalize("/a/b/../../.."));
  +        assertEquals(SEP + "a" + SEP + "d", FilenameUtils.normalize("/a/b/../c/../d"));
  +        assertEquals(SEP + "a" + SEP + "b" + SEP + "d", FilenameUtils.normalize("/a/b//d"));
  +        assertEquals(SEP + "a" + SEP + "b", FilenameUtils.normalize("/a/b/././."));
  +        assertEquals(SEP + "a", FilenameUtils.normalize("/./a"));
  +        assertEquals(SEP + "", FilenameUtils.normalize("/./"));
  +        assertEquals(SEP + "", FilenameUtils.normalize("/."));
  +        assertEquals(null, FilenameUtils.normalize("/../a"));
  +        assertEquals(null, FilenameUtils.normalize("/.."));
  +        assertEquals(SEP + "", FilenameUtils.normalize("/"));
  +        
  +        assertEquals("~" + SEP + "a" + SEP + "c", FilenameUtils.normalize("~/a/b/../c"));
  +        assertEquals("~" + SEP + "c", FilenameUtils.normalize("~/a/b/../../c"));
  +        assertEquals(null, FilenameUtils.normalize("~/a/b/../../../c"));
  +        assertEquals("~" + SEP + "a", FilenameUtils.normalize("~/a/b/.."));
  +        assertEquals("~" + SEP + "", FilenameUtils.normalize("~/a/b/../.."));
  +        assertEquals(null, FilenameUtils.normalize("~/a/b/../../.."));
  +        assertEquals("~" + SEP + "a" + SEP + "d", FilenameUtils.normalize("~/a/b/../c/../d"));
  +        assertEquals("~" + SEP + "a" + SEP + "b" + SEP + "d", FilenameUtils.normalize("~/a/b//d"));
  +        assertEquals("~" + SEP + "a" + SEP + "b", FilenameUtils.normalize("~/a/b/././."));
  +        assertEquals("~" + SEP + "a", FilenameUtils.normalize("~/./a"));
  +        assertEquals("~" + SEP + "", FilenameUtils.normalize("~/./"));
  +        assertEquals("~" + SEP + "", FilenameUtils.normalize("~/."));
  +        assertEquals(null, FilenameUtils.normalize("~/../a"));
  +        assertEquals(null, FilenameUtils.normalize("~/.."));
  +        assertEquals("~" + SEP + "", FilenameUtils.normalize("~/"));
  +        
  +        assertEquals("~user" + SEP + "a" + SEP + "c", FilenameUtils.normalize("~user/a/b/../c"));
  +        assertEquals("~user" + SEP + "c", FilenameUtils.normalize("~user/a/b/../../c"));
  +        assertEquals(null, FilenameUtils.normalize("~user/a/b/../../../c"));
  +        assertEquals("~user" + SEP + "a", FilenameUtils.normalize("~user/a/b/.."));
  +        assertEquals("~user" + SEP + "", FilenameUtils.normalize("~user/a/b/../.."));
  +        assertEquals(null, FilenameUtils.normalize("~user/a/b/../../.."));
  +        assertEquals("~user" + SEP + "a" + SEP + "d", FilenameUtils.normalize("~user/a/b/../c/../d"));
  +        assertEquals("~user" + SEP + "a" + SEP + "b" + SEP + "d", FilenameUtils.normalize("~user/a/b//d"));
  +        assertEquals("~user" + SEP + "a" + SEP + "b", FilenameUtils.normalize("~user/a/b/././."));
  +        assertEquals("~user" + SEP + "a", FilenameUtils.normalize("~user/./a"));
  +        assertEquals("~user" + SEP + "", FilenameUtils.normalize("~user/./"));
  +        assertEquals("~user" + SEP + "", FilenameUtils.normalize("~user/."));
  +        assertEquals(null, FilenameUtils.normalize("~user/../a"));
  +        assertEquals(null, FilenameUtils.normalize("~user/.."));
  +        assertEquals("~user" + SEP + "", FilenameUtils.normalize("~user/"));
  +        
  +        assertEquals("C:" + SEP + "a" + SEP + "c", FilenameUtils.normalize("C:/a/b/../c"));
  +        assertEquals("C:" + SEP + "c", FilenameUtils.normalize("C:/a/b/../../c"));
  +        assertEquals(null, FilenameUtils.normalize("C:/a/b/../../../c"));
  +        assertEquals("C:" + SEP + "a", FilenameUtils.normalize("C:/a/b/.."));
  +        assertEquals("C:" + SEP + "", FilenameUtils.normalize("C:/a/b/../.."));
  +        assertEquals(null, FilenameUtils.normalize("C:/a/b/../../.."));
  +        assertEquals("C:" + SEP + "a" + SEP + "d", FilenameUtils.normalize("C:/a/b/../c/../d"));
  +        assertEquals("C:" + SEP + "a" + SEP + "b" + SEP + "d", FilenameUtils.normalize("C:/a/b//d"));
  +        assertEquals("C:" + SEP + "a" + SEP + "b", FilenameUtils.normalize("C:/a/b/././."));
  +        assertEquals("C:" + SEP + "a", FilenameUtils.normalize("C:/./a"));
  +        assertEquals("C:" + SEP + "", FilenameUtils.normalize("C:/./"));
  +        assertEquals("C:" + SEP + "", FilenameUtils.normalize("C:/."));
  +        assertEquals(null, FilenameUtils.normalize("C:/../a"));
  +        assertEquals(null, FilenameUtils.normalize("C:/.."));
  +        assertEquals("C:" + SEP + "", FilenameUtils.normalize("C:/"));
  +        
  +        assertEquals(SEP + SEP + "server" + SEP + "a" + SEP + "c", FilenameUtils.normalize("//server/a/b/../c"));
  +        assertEquals(SEP + SEP + "server" + SEP + "c", FilenameUtils.normalize("//server/a/b/../../c"));
  +        assertEquals(null, FilenameUtils.normalize("//server/a/b/../../../c"));
  +        assertEquals(SEP + SEP + "server" + SEP + "a", FilenameUtils.normalize("//server/a/b/.."));
  +        assertEquals(SEP + SEP + "server" + SEP + "", FilenameUtils.normalize("//server/a/b/../.."));
  +        assertEquals(null, FilenameUtils.normalize("//server/a/b/../../.."));
  +        assertEquals(SEP + SEP + "server" + SEP + "a" + SEP + "d", FilenameUtils.normalize("//server/a/b/../c/../d"));
  +        assertEquals(SEP + SEP + "server" + SEP + "a" + SEP + "b" + SEP + "d", FilenameUtils.normalize("//server/a/b//d"));
  +        assertEquals(SEP + SEP + "server" + SEP + "a" + SEP + "b", FilenameUtils.normalize("//server/a/b/././."));
  +        assertEquals(SEP + SEP + "server" + SEP + "a", FilenameUtils.normalize("//server/./a"));
  +        assertEquals(SEP + SEP + "server" + SEP + "", FilenameUtils.normalize("//server/./"));
  +        assertEquals(SEP + SEP + "server" + SEP + "", FilenameUtils.normalize("//server/."));
  +        assertEquals(null, FilenameUtils.normalize("//server/../a"));
  +        assertEquals(null, FilenameUtils.normalize("//server/.."));
  +        assertEquals(SEP + SEP + "server" + SEP + "", FilenameUtils.normalize("//server/"));
       }
   
       //-----------------------------------------------------------------------
  @@ -225,42 +255,31 @@
       //-----------------------------------------------------------------------
       public void testGetPrefixLength() {
           assertEquals(-1, FilenameUtils.getPrefixLength(null));
  -        if (WINDOWS) {
  -            assertEquals(-1, FilenameUtils.getPrefixLength("1:\\a\\b\\c.txt"));
  -            assertEquals(-1, FilenameUtils.getPrefixLength("1:"));
  -            assertEquals(-1, FilenameUtils.getPrefixLength("1:a"));
  -            assertEquals(-1, FilenameUtils.getPrefixLength("\\\\\\a\\b\\c.txt"));
  -            assertEquals(-1, FilenameUtils.getPrefixLength("\\\\a"));
  -            
  -            assertEquals(0, FilenameUtils.getPrefixLength("a\\b\\c.txt"));
  -            assertEquals(1, FilenameUtils.getPrefixLength("\\a\\b\\c.txt"));
  -            assertEquals(3, FilenameUtils.getPrefixLength("C:\\a\\b\\c.txt"));
  -            assertEquals(9, FilenameUtils.getPrefixLength("\\\\server\\a\\b\\c.txt"));
  -            
  -            assertEquals(0, FilenameUtils.getPrefixLength("a/b/c.txt"));
  -            assertEquals(1, FilenameUtils.getPrefixLength("/a/b/c.txt"));
  -            assertEquals(3, FilenameUtils.getPrefixLength("C:/a/b/c.txt"));
  -            assertEquals(9, FilenameUtils.getPrefixLength("//server/a/b/c.txt"));
  -            
  -            assertEquals(0, FilenameUtils.getPrefixLength("~/a/b/c.txt"));
  -            assertEquals(0, FilenameUtils.getPrefixLength("~user/a/b/c.txt"));
  -        } else {
  -            assertEquals(-1, FilenameUtils.getPrefixLength("~"));
  -            assertEquals(-1, FilenameUtils.getPrefixLength("~user"));
  -            
  -            assertEquals(0, FilenameUtils.getPrefixLength("a/b/c.txt"));
  -            assertEquals(1, FilenameUtils.getPrefixLength("/a/b/c.txt"));
  -            assertEquals(2, FilenameUtils.getPrefixLength("~/a/b/c.txt"));
  -            assertEquals(6, FilenameUtils.getPrefixLength("~user/a/b/c.txt"));
  -            
  -            assertEquals(0, FilenameUtils.getPrefixLength("a\\b\\c.txt"));
  -            assertEquals(1, FilenameUtils.getPrefixLength("\\a\\b\\c.txt"));
  -            assertEquals(2, FilenameUtils.getPrefixLength("~\\a\\b\\c.txt"));
  -            assertEquals(6, FilenameUtils.getPrefixLength("~user\\a\\b\\c.txt"));
  -            
  -            assertEquals(0, FilenameUtils.getPrefixLength("C:\\a\\b\\c.txt"));
  -            assertEquals(1, FilenameUtils.getPrefixLength("\\\\server\\a\\b\\c.txt"));
  -        }
  +        assertEquals(-1, FilenameUtils.getPrefixLength(":"));
  +        assertEquals(-1, FilenameUtils.getPrefixLength("1:\\a\\b\\c.txt"));
  +        assertEquals(-1, FilenameUtils.getPrefixLength("1:"));
  +        assertEquals(-1, FilenameUtils.getPrefixLength("1:a"));
  +        assertEquals(-1, FilenameUtils.getPrefixLength("\\\\\\a\\b\\c.txt"));
  +        assertEquals(-1, FilenameUtils.getPrefixLength("\\\\a"));
  +        assertEquals(-1, FilenameUtils.getPrefixLength("~"));
  +        assertEquals(-1, FilenameUtils.getPrefixLength("~user"));
  +        
  +        assertEquals(0, FilenameUtils.getPrefixLength("a\\b\\c.txt"));
  +        assertEquals(1, FilenameUtils.getPrefixLength("\\a\\b\\c.txt"));
  +        assertEquals(3, FilenameUtils.getPrefixLength("C:\\a\\b\\c.txt"));
  +        assertEquals(9, FilenameUtils.getPrefixLength("\\\\server\\a\\b\\c.txt"));
  +        
  +        assertEquals(0, FilenameUtils.getPrefixLength("a/b/c.txt"));
  +        assertEquals(1, FilenameUtils.getPrefixLength("/a/b/c.txt"));
  +        assertEquals(3, FilenameUtils.getPrefixLength("C:/a/b/c.txt"));
  +        assertEquals(9, FilenameUtils.getPrefixLength("//server/a/b/c.txt"));
  +        assertEquals(2, FilenameUtils.getPrefixLength("~/a/b/c.txt"));
  +        assertEquals(6, FilenameUtils.getPrefixLength("~user/a/b/c.txt"));
  +        
  +        assertEquals(0, FilenameUtils.getPrefixLength("a\\b\\c.txt"));
  +        assertEquals(1, FilenameUtils.getPrefixLength("\\a\\b\\c.txt"));
  +        assertEquals(2, FilenameUtils.getPrefixLength("~\\a\\b\\c.txt"));
  +        assertEquals(6, FilenameUtils.getPrefixLength("~user\\a\\b\\c.txt"));
       }
       
       public void testIndexOfLastSeparator() {
  @@ -284,42 +303,31 @@
       //-----------------------------------------------------------------------
       public void testGetPrefix() {
           assertEquals(null, FilenameUtils.getPrefix(null));
  -        if (WINDOWS) {
  -            assertEquals(null, FilenameUtils.getPrefix("1:\\a\\b\\c.txt"));
  -            assertEquals(null, FilenameUtils.getPrefix("1:"));
  -            assertEquals(null, FilenameUtils.getPrefix("1:a"));
  -            assertEquals(null, FilenameUtils.getPrefix("\\\\\\a\\b\\c.txt"));
  -            assertEquals(null, FilenameUtils.getPrefix("\\\\a"));
  -            
  -            assertEquals("", FilenameUtils.getPrefix("a\\b\\c.txt"));
  -            assertEquals("\\", FilenameUtils.getPrefix("\\a\\b\\c.txt"));
  -            assertEquals("C:\\", FilenameUtils.getPrefix("C:\\a\\b\\c.txt"));
  -            assertEquals("\\\\server\\", FilenameUtils.getPrefix("\\\\server\\a\\b\\c.txt"));
  -            
  -            assertEquals("", FilenameUtils.getPrefix("a/b/c.txt"));
  -            assertEquals("/", FilenameUtils.getPrefix("/a/b/c.txt"));
  -            assertEquals("C:/", FilenameUtils.getPrefix("C:/a/b/c.txt"));
  -            assertEquals("//server/", FilenameUtils.getPrefix("//server/a/b/c.txt"));
  -            
  -            assertEquals("", FilenameUtils.getPrefix("~/a/b/c.txt"));
  -            assertEquals("", FilenameUtils.getPrefix("~user/a/b/c.txt"));
  -        } else {
  -            assertEquals(null, FilenameUtils.getPrefix("~"));
  -            assertEquals(null, FilenameUtils.getPrefix("~user"));
  -            
  -            assertEquals("", FilenameUtils.getPrefix("a/b/c.txt"));
  -            assertEquals("/", FilenameUtils.getPrefix("/a/b/c.txt"));
  -            assertEquals("~/", FilenameUtils.getPrefix("~/a/b/c.txt"));
  -            assertEquals("~user/", FilenameUtils.getPrefix("~user/a/b/c.txt"));
  -            
  -            assertEquals("", FilenameUtils.getPrefix("a\\b\\c.txt"));
  -            assertEquals("\\", FilenameUtils.getPrefix("\\a\\b\\c.txt"));
  -            assertEquals("~\\", FilenameUtils.getPrefix("~\\a\\b\\c.txt"));
  -            assertEquals("~user\\", FilenameUtils.getPrefix("~user\\a\\b\\c.txt"));
  -            
  -            assertEquals("", FilenameUtils.getPrefix("C:\\a\\b\\c.txt"));
  -            assertEquals("\\", FilenameUtils.getPrefix("\\\\server\\a\\b\\c.txt"));
  -        }
  +        assertEquals(null, FilenameUtils.getPrefix(":"));
  +        assertEquals(null, FilenameUtils.getPrefix("1:\\a\\b\\c.txt"));
  +        assertEquals(null, FilenameUtils.getPrefix("1:"));
  +        assertEquals(null, FilenameUtils.getPrefix("1:a"));
  +        assertEquals(null, FilenameUtils.getPrefix("\\\\\\a\\b\\c.txt"));
  +        assertEquals(null, FilenameUtils.getPrefix("\\\\a"));
  +        assertEquals(null, FilenameUtils.getPrefix("~"));
  +        assertEquals(null, FilenameUtils.getPrefix("~user"));
  +        
  +        assertEquals("", FilenameUtils.getPrefix("a\\b\\c.txt"));
  +        assertEquals("\\", FilenameUtils.getPrefix("\\a\\b\\c.txt"));
  +        assertEquals("C:\\", FilenameUtils.getPrefix("C:\\a\\b\\c.txt"));
  +        assertEquals("\\\\server\\", FilenameUtils.getPrefix("\\\\server\\a\\b\\c.txt"));
  +        
  +        assertEquals("", FilenameUtils.getPrefix("a/b/c.txt"));
  +        assertEquals("/", FilenameUtils.getPrefix("/a/b/c.txt"));
  +        assertEquals("C:/", FilenameUtils.getPrefix("C:/a/b/c.txt"));
  +        assertEquals("//server/", FilenameUtils.getPrefix("//server/a/b/c.txt"));
  +        assertEquals("~/", FilenameUtils.getPrefix("~/a/b/c.txt"));
  +        assertEquals("~user/", FilenameUtils.getPrefix("~user/a/b/c.txt"));
  +        
  +        assertEquals("", FilenameUtils.getPrefix("a\\b\\c.txt"));
  +        assertEquals("\\", FilenameUtils.getPrefix("\\a\\b\\c.txt"));
  +        assertEquals("~\\", FilenameUtils.getPrefix("~\\a\\b\\c.txt"));
  +        assertEquals("~user\\", FilenameUtils.getPrefix("~user\\a\\b\\c.txt"));
       }
   
       public void testGetPath() {
  @@ -329,32 +337,22 @@
           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"));
  -        if (WINDOWS) {
  -            assertEquals(null, FilenameUtils.getPath("1:/a/b/c.txt"));
  -            assertEquals(null, FilenameUtils.getPath("1:"));
  -            assertEquals(null, FilenameUtils.getPath("1:a"));
  -            assertEquals(null, FilenameUtils.getPath("///a/b/c.txt"));
  -            assertEquals(null, FilenameUtils.getPath("//a"));
  -            
  -            assertEquals("a/b", FilenameUtils.getPath("a/b/c.txt"));
  -            assertEquals("a/b", FilenameUtils.getPath("/a/b/c.txt"));
  -            assertEquals("a/b", FilenameUtils.getPath("C:/a/b/c.txt"));
  -            assertEquals("a/b", FilenameUtils.getPath("//server/a/b/c.txt"));
  -            
  -            assertEquals("~/a/b", FilenameUtils.getPath("~/a/b/c.txt"));
  -            assertEquals("~user/a/b", FilenameUtils.getPath("~user/a/b/c.txt"));
  -        } else {
  -            assertEquals(null, FilenameUtils.getPath("~"));
  -            assertEquals(null, FilenameUtils.getPath("~user"));
  -            
  -            assertEquals("a/b", FilenameUtils.getPath("a/b/c.txt"));
  -            assertEquals("a/b", FilenameUtils.getPath("/a/b/c.txt"));
  -            assertEquals("a/b", FilenameUtils.getPath("~/a/b/c.txt"));
  -            assertEquals("a/b", FilenameUtils.getPath("~user/a/b/c.txt"));
  -            
  -            assertEquals("C:/a/b", FilenameUtils.getPath("C:/a/b/c.txt"));
  -            assertEquals("/server/a/b", FilenameUtils.getPath("//server/a/b/c.txt"));
  -        }
  +        
  +        assertEquals(null, FilenameUtils.getPath(":"));
  +        assertEquals(null, FilenameUtils.getPath("1:/a/b/c.txt"));
  +        assertEquals(null, FilenameUtils.getPath("1:"));
  +        assertEquals(null, FilenameUtils.getPath("1:a"));
  +        assertEquals(null, FilenameUtils.getPath("///a/b/c.txt"));
  +        assertEquals(null, FilenameUtils.getPath("//a"));
  +        assertEquals(null, FilenameUtils.getPath("~"));
  +        assertEquals(null, FilenameUtils.getPath("~user"));
  +        
  +        assertEquals("a/b", FilenameUtils.getPath("a/b/c.txt"));
  +        assertEquals("a/b", FilenameUtils.getPath("/a/b/c.txt"));
  +        assertEquals("a/b", FilenameUtils.getPath("C:/a/b/c.txt"));
  +        assertEquals("a/b", FilenameUtils.getPath("//server/a/b/c.txt"));
  +        assertEquals("a/b", FilenameUtils.getPath("~/a/b/c.txt"));
  +        assertEquals("a/b", FilenameUtils.getPath("~user/a/b/c.txt"));
       }
   
       public void testGetFullPath() {
  @@ -364,32 +362,22 @@
           assertEquals("a/b", FilenameUtils.getFullPath("a/b/c"));
           assertEquals("a/b/c", FilenameUtils.getFullPath("a/b/c/"));
           assertEquals("a\\b", FilenameUtils.getFullPath("a\\b\\c"));
  -        if (WINDOWS) {
  -            assertEquals(null, FilenameUtils.getFullPath("1:/a/b/c.txt"));
  -            assertEquals(null, FilenameUtils.getFullPath("1:"));
  -            assertEquals(null, FilenameUtils.getFullPath("1:a"));
  -            assertEquals(null, FilenameUtils.getFullPath("///a/b/c.txt"));
  -            assertEquals(null, FilenameUtils.getFullPath("//a"));
  -            
  -            assertEquals("a/b", FilenameUtils.getFullPath("a/b/c.txt"));
  -            assertEquals("/a/b", FilenameUtils.getFullPath("/a/b/c.txt"));
  -            assertEquals("C:/a/b", FilenameUtils.getFullPath("C:/a/b/c.txt"));
  -            assertEquals("//server/a/b", FilenameUtils.getFullPath("//server/a/b/c.txt"));
  -            
  -            assertEquals("~/a/b", FilenameUtils.getFullPath("~/a/b/c.txt"));
  -            assertEquals("~user/a/b", FilenameUtils.getFullPath("~user/a/b/c.txt"));
  -        } else {
  -            assertEquals(null, FilenameUtils.getFullPath("~"));
  -            assertEquals(null, FilenameUtils.getFullPath("~user"));
  -            
  -            assertEquals("a/b", FilenameUtils.getFullPath("a/b/c.txt"));
  -            assertEquals("/a/b", FilenameUtils.getFullPath("/a/b/c.txt"));
  -            assertEquals("~/a/b", FilenameUtils.getFullPath("~/a/b/c.txt"));
  -            assertEquals("~user/a/b", FilenameUtils.getFullPath("~user/a/b/c.txt"));
  -            
  -            assertEquals("C:/a/b", FilenameUtils.getFullPath("C:/a/b/c.txt"));
  -            assertEquals("//server/a/b", FilenameUtils.getFullPath("//server/a/b/c.txt"));
  -        }
  +        
  +        assertEquals(null, FilenameUtils.getFullPath(":"));
  +        assertEquals(null, FilenameUtils.getFullPath("1:/a/b/c.txt"));
  +        assertEquals(null, FilenameUtils.getFullPath("1:"));
  +        assertEquals(null, FilenameUtils.getFullPath("1:a"));
  +        assertEquals(null, FilenameUtils.getFullPath("///a/b/c.txt"));
  +        assertEquals(null, FilenameUtils.getFullPath("//a"));
  +        assertEquals(null, FilenameUtils.getFullPath("~"));
  +        assertEquals(null, FilenameUtils.getFullPath("~user"));
  +        
  +        assertEquals("a/b", FilenameUtils.getFullPath("a/b/c.txt"));
  +        assertEquals("/a/b", FilenameUtils.getFullPath("/a/b/c.txt"));
  +        assertEquals("C:/a/b", FilenameUtils.getFullPath("C:/a/b/c.txt"));
  +        assertEquals("//server/a/b", FilenameUtils.getFullPath("//server/a/b/c.txt"));
  +        assertEquals("~/a/b", FilenameUtils.getFullPath("~/a/b/c.txt"));
  +        assertEquals("~user/a/b", FilenameUtils.getFullPath("~user/a/b/c.txt"));
       }
   
       public void testGetName() {
  
  
  
  1.29      +87 -142   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.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- FilenameUtils.java	26 Nov 2004 19:18:28 -0000	1.28
  +++ FilenameUtils.java	27 Nov 2004 01:22:05 -0000	1.29
  @@ -16,9 +16,8 @@
   package org.apache.commons.io;
   
   import java.io.File;
  -import java.io.IOException;
  -import java.util.Collection;
   import java.util.ArrayList;
  +import java.util.Collection;
   
   /**
    * Utility class that provides methods to manipulate filenames and filepaths.
  @@ -112,22 +111,29 @@
        * The input may contain separators in either Unix or Windows format.
        * The output will contain separators in the format of the system.
        * <p>
  -     * A double slash will be merged to a single slash (thus UNC names are not handled).
  -     * A single dot path segment will be removed with no other effect.
  +     * A trailing slash will be removed.
  +     * A double slash will be merged to a single slash (but UNC names are handled).
  +     * A single dot path segment will be removed.
        * A double dot will cause that path segment and the one before to be removed.
        * If the double dot has no parent path segment to work with, <code>null</code>
        * is returned.
        * <pre>
  -     * /foo//               -->     /foo/
  -     * /foo/./              -->     /foo/
  -     * /foo/../bar          -->     /bar
  -     * /foo/../bar/         -->     /bar/
  -     * /foo/../bar/../baz   -->     /baz
  -     * //foo//./bar         -->     /foo/bar
  -     * /../                 -->     null
  -     * ../foo               -->     null
  -     * foo/../../bar        -->     null
  -     * foo/../bar           -->     bar
  +     * /foo//               -->   /foo
  +     * /foo/./              -->   /foo
  +     * /foo/../bar          -->   /bar
  +     * /foo/../bar/         -->   /bar
  +     * /foo/../bar/../baz   -->   /baz
  +     * //foo//./bar         -->   /foo/bar
  +     * /../                 -->   null
  +     * ../foo               -->   null
  +     * foo/../../bar        -->   null
  +     * foo/../bar           -->   bar
  +     * //server/foo/../bar  -->   //server/bar
  +     * //server/../bar      -->   null
  +     * C:\foo\..\bar        -->   C:\bar
  +     * C:\..\bar            -->   null
  +     * ~/foo/../bar         -->   ~/bar
  +     * ~/../bar             -->   null
        * </pre>
        *
        * @param filename  the filename to normalize, null returns null
  @@ -137,23 +143,30 @@
           if (filename == null) {
               return null;
           }
  -        char[] array = filename.toCharArray();
  +        int size = filename.length();
  +        if (size == 0) {
  +            return filename;
  +        }
           int prefix = getPrefixLength(filename);
           if (prefix < 0) {
               return null;
           }
           
  -        // TODO: Use prefix
  +        char[] array = new char[size + 2];  // +1 for possible extra slash, +2 for arraycopy
  +        filename.getChars(0, filename.length(), array, 0);
           
  -        int size = array.length;
  -        // fix separators
  +        // fix separators throughout
           for (int i = 0; i < array.length; i++) {
               if (array[i] == OTHER_SEPARATOR) {
                   array[i] = SYSTEM_SEPARATOR;
               }
           }
  +        if (isSeparator(array[size - 1]) == false) {
  +            array[size++] = SYSTEM_SEPARATOR;
  +        }
  +        
           // adjoining slashes
  -        for (int i = 1; i < size; i++) {
  +        for (int i = prefix + 1; i < size; i++) {
               if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == SYSTEM_SEPARATOR)
{
                   System.arraycopy(array, i, array, i - 1, size - i);
                   size--;
  @@ -161,38 +174,44 @@
               }
           }
           // dot slash
  -        for (int i = 2; i < size; i++) {
  +        for (int i = prefix + 1; i < size; i++) {
               if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == '.' &&
  -                    array[i - 2] == SYSTEM_SEPARATOR) {
  -                System.arraycopy(array, i, array, i - 2, size - i);
  +                    (i == prefix + 1 || array[i - 2] == SYSTEM_SEPARATOR)) {
  +                System.arraycopy(array, i + 1, array, i - 1, size - i);
                   size -=2;
                   i--;
               }
           }
           // double dot slash
           outer:
  -        for (int i = 2; i < size; i++) {
  -            if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == '.' &&
  -                    array[i - 2] == '.' && (i == 2 || array[i - 3] == SYSTEM_SEPARATOR))
{
  -                if (i == 2) {
  +        for (int i = prefix + 2; i < size; i++) {
  +            if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == '.' &&
array[i - 2] == '.' &&
  +                    (i == prefix + 2 || array[i - 3] == SYSTEM_SEPARATOR)) {
  +                if (i == prefix + 2) {
                       return null;
                   }
                   int j;
  -                for (j = i - 4 ; j >= 0; j--) {
  +                for (j = i - 4 ; j >= prefix; j--) {
                       if (array[j] == SYSTEM_SEPARATOR) {
  -                        System.arraycopy(array, i, array, j, size - i);
  +                        System.arraycopy(array, i + 1, array, j + 1, size - i);
                           size -= (i - j);
                           i = j + 1;
                           continue outer;
                       }
                   }
  -                System.arraycopy(array, i + 1, array, 0, size - i - 1);
  -                size -= (i + 1);
  -                i = 1;
  +                System.arraycopy(array, i + 1, array, prefix, size - i);
  +                size -= (i + 1 - prefix);
  +                i = prefix + 1;
               }
           }
           
  -        return new String(array, 0, size);
  +        if (size <= 0) {  // should never be less than 0
  +            return "";
  +        }
  +        if (size <= prefix) {  // should never be less than prefix
  +            return new String(array, 0, size);
  +        }
  +        return new String(array, 0, size - 1);
       }
   
       /**
  @@ -241,79 +260,6 @@
                   append(File.separator).append(pth).toString();
       }
   
  -    /**
  -     * Resolve a file <code>filename</code> to it's canonical form. If
  -     * <code>filename</code> is relative (doesn't start with <code>/</code>),
  -     * it will be resolved relative to <code>baseFile</code>, otherwise it
is
  -     * treated as a normal root-relative path.
  -     *
  -     * @param baseFile Where to resolve <code>filename</code> from, if
  -     * <code>filename</code> is relative.
  -     * @param filename Absolute or relative file path to resolve.
  -     * @return The canonical <code>File</code> of <code>filename</code>.
  -     */
  -     // KILL? Decide whether this is worth keeping?
  -    public static File resolveFile(File baseFile, String filename) {
  -        String filenm = filename;
  -        if ('/' != File.separatorChar) {
  -            filenm = filename.replace('/', File.separatorChar);
  -        }
  -
  -        if ('\\' != File.separatorChar) {
  -            filenm = filename.replace('\\', File.separatorChar);
  -        }
  -
  -        // deal with absolute files
  -        if (filenm.startsWith(File.separator)) {
  -            File file = new File(filenm);
  -
  -            try {
  -                file = file.getCanonicalFile();
  -            } catch (IOException ioe) {
  -                // ignore
  -            }
  -
  -            return file;
  -        }
  -        // FIXME: I'm almost certain this // removal is unnecessary, as
  -        // getAbsoluteFile() strips
  -        // them. However, I'm not sure about this UNC stuff. (JT)
  -        char[] chars = filename.toCharArray();
  -        StringBuffer sb = new StringBuffer();
  -
  -        //remove duplicate file separators in succession - except
  -        //on win32 at start of filename as UNC filenames can
  -        //be \\AComputer\AShare\myfile.txt
  -        int start = 0;
  -        if ('\\' == File.separatorChar) {
  -            sb.append(filenm.charAt(0));
  -            start++;
  -        }
  -
  -        for (int i = start; i < chars.length; i++) {
  -            boolean doubleSeparator =
  -                File.separatorChar == chars[i]
  -                    && File.separatorChar == chars[i - 1];
  -
  -            if (!doubleSeparator) {
  -                sb.append(chars[i]);
  -            }
  -        }
  -
  -        filenm = sb.toString();
  -
  -        //must be relative
  -        File file = (new File(baseFile, filenm)).getAbsoluteFile();
  -
  -        try {
  -            file = file.getCanonicalFile();
  -        } catch ( IOException ioe) {
  -            ;
  -        }
  -
  -        return file;
  -    }
  -
       //-----------------------------------------------------------------------
       /**
        * Converts all separators to the Unix separator of forward slash.
  @@ -377,6 +323,8 @@
        * ~/a/b/c.txt         --> "~/"        --> current user relative
        * ~user/a/b/c.txt     --> "~user/"    --> named user relative
        * </pre>
  +     * Both sets of prefixes will be matched regardless of the system
  +     * on which the code runs.
        * 
        * @param filename  the filename to find the prefix in, null returns -1
        * @return the length of the prefix, -1 if invalid or null
  @@ -389,37 +337,14 @@
           if (len == 0) {
               return 0;
           }
  -        if (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR) {
  -            char ch0 = filename.charAt(0);
  -            if (len == 1) {
  -                return (isSeparator(ch0) ? 1 : 0);
  -            } else {
  -                char ch1 = filename.charAt(1);
  -                if (ch1 == ':') {
  -                    ch0 = Character.toUpperCase(ch0);
  -                    if (ch0 < 'A' || ch0 > 'Z' || len == 2 || isSeparator(filename.charAt(2))
== false) {
  -                        return -1;
  -                    }
  -                    return 3;
  -                } else if (isSeparator(ch0) && isSeparator(ch1)) {
  -                    int posUnix = filename.indexOf(UNIX_SEPARATOR, 2);
  -                    int posWin = filename.indexOf(WINDOWS_SEPARATOR, 2);
  -                    if ((posUnix == -1 && posWin == -1) || posUnix == 2 || posWin
== 2) {
  -                        return -1;
  -                    }
  -                    posUnix = (posUnix == -1 ? posWin : posUnix);
  -                    posWin = (posWin == -1 ? posUnix : posWin);
  -                    return Math.min(posUnix, posWin) + 1;
  -                } else {
  -                    return (isSeparator(ch0) ? 1 : 0);
  -                }
  +        char ch0 = filename.charAt(0);
  +        if (len == 1) {
  +            if (ch0 == '~' || ch0 == ':') {
  +                return -1;
               }
  +            return (isSeparator(ch0) ? 1 : 0);
           } else {
  -            char ch0 = filename.charAt(0);
               if (ch0 == '~') {
  -                if (len == 1) {
  -                    return -1;
  -                }
                   int posUnix = filename.indexOf(UNIX_SEPARATOR, 1);
                   int posWin = filename.indexOf(WINDOWS_SEPARATOR, 1);
                   if (posUnix == -1 && posWin == -1) {
  @@ -428,6 +353,24 @@
                   posUnix = (posUnix == -1 ? posWin : posUnix);
                   posWin = (posWin == -1 ? posUnix : posWin);
                   return Math.min(posUnix, posWin) + 1;
  +            }
  +            char ch1 = filename.charAt(1);
  +            if (ch1 == ':') {
  +                ch0 = Character.toUpperCase(ch0);
  +                if (ch0 < 'A' || ch0 > 'Z' || len == 2 || isSeparator(filename.charAt(2))
== false) {
  +                    return -1;
  +                }
  +                return 3;
  +                
  +            } else if (isSeparator(ch0) && isSeparator(ch1)) {
  +                int posUnix = filename.indexOf(UNIX_SEPARATOR, 2);
  +                int posWin = filename.indexOf(WINDOWS_SEPARATOR, 2);
  +                if ((posUnix == -1 && posWin == -1) || posUnix == 2 || posWin ==
2) {
  +                    return -1;
  +                }
  +                posUnix = (posUnix == -1 ? posWin : posUnix);
  +                posWin = (posWin == -1 ? posUnix : posWin);
  +                return Math.min(posUnix, posWin) + 1;
               } else {
                   return (isSeparator(ch0) ? 1 : 0);
               }
  @@ -513,10 +456,11 @@
        * This method will handle a file in either Unix or Windows format.
        * The text before the last forward or backslash is returned.
        * <pre>
  -     * ~/a/b/c.txt --> a/b
  -     * a.txt       --> ""
  -     * a/b/c       --> a/b
  -     * a/b/c/      --> a/b/c
  +     * C:\a\b\c.txt --> a\b
  +     * ~/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
  @@ -544,10 +488,11 @@
        * This method will handle a file in either Unix or Windows format.
        * The text before the last forward or backslash is returned.
        * <pre>
  -     * ~/a/b/c.txt --> ~/a/b
  -     * a.txt       --> ""
  -     * a/b/c       --> a/b
  -     * a/b/c/      --> a/b/c
  +     * C:\a\b\c.txt --> C:\a\b
  +     * ~/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
  
  
  

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