diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index 4d5aa6a35..5da460d6c 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -988,12 +988,16 @@ public class FileUtils { * @since Ant 1.5 */ 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 // normalize - we always add one so we never think /foo was a // 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)) { return p.substring(l.length()); } else { diff --git a/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java b/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java index f36d2adf1..116b0e5c7 100644 --- a/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java +++ b/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java @@ -1,7 +1,7 @@ /* * 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. * * 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"))); assertEquals(fu.normalize("/foobar").getAbsolutePath(), 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"))); } /**