Browse Source

removeLeadingPath didn't work as expected when trying to remove a path

from itself.
PR: 19979


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274596 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
b3e94490f5
2 changed files with 17 additions and 4 deletions
  1. +7
    -3
      src/main/org/apache/tools/ant/util/FileUtils.java
  2. +10
    -1
      src/testcases/org/apache/tools/ant/util/FileUtilsTest.java

+ 7
- 3
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -988,12 +988,16 @@ public class FileUtils {
* @since Ant 1.5 * @since Ant 1.5
*/ */
public String removeLeadingPath(File leading, File path) { public String removeLeadingPath(File leading, File path) {
String l = normalize(leading.getAbsolutePath()).getAbsolutePath();
String p = normalize(path.getAbsolutePath()).getAbsolutePath();
if (l.equals(p)) {
return "";
}

// if leading's path ends with a slash, it will be stripped by // if leading's path ends with a slash, it will be stripped by
// normalize - we always add one so we never think /foo was a // normalize - we always add one so we never think /foo was a
// parent directory of /foobar // parent directory of /foobar
String l = normalize(leading.getAbsolutePath()).getAbsolutePath()
+ File.separator;
String p = normalize(path.getAbsolutePath()).getAbsolutePath();
l += File.separator;
if (p.startsWith(l)) { if (p.startsWith(l)) {
return p.substring(l.length()); return p.substring(l.length());
} else { } else {


+ 10
- 1
src/testcases/org/apache/tools/ant/util/FileUtilsTest.java View File

@@ -1,7 +1,7 @@
/* /*
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -406,6 +406,15 @@ public class FileUtilsTest extends TestCase {
fu.removeLeadingPath(new File("/foo"), new File("/bar"))); fu.removeLeadingPath(new File("/foo"), new File("/bar")));
assertEquals(fu.normalize("/foobar").getAbsolutePath(), assertEquals(fu.normalize("/foobar").getAbsolutePath(),
fu.removeLeadingPath(new File("/foo"), new File("/foobar"))); fu.removeLeadingPath(new File("/foo"), new File("/foobar")));
// bugzilla report 19979
assertEquals("", fu.removeLeadingPath(new File("/foo/bar"),
new File("/foo/bar")));
assertEquals("", fu.removeLeadingPath(new File("/foo/bar"),
new File("/foo/bar/")));
assertEquals("", fu.removeLeadingPath(new File("/foo/bar/"),
new File("/foo/bar/")));
assertEquals("", fu.removeLeadingPath(new File("/foo/bar/"),
new File("/foo/bar")));
} }


/** /**


Loading…
Cancel
Save