diff --git a/src/etc/testcases/taskdefs/basename.xml b/src/etc/testcases/taskdefs/basename.xml index 143819758..3574880fe 100644 --- a/src/etc/testcases/taskdefs/basename.xml +++ b/src/etc/testcases/taskdefs/basename.xml @@ -22,4 +22,20 @@ + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/Basename.java b/src/main/org/apache/tools/ant/taskdefs/Basename.java index 7898f67e9..9fff23629 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Basename.java +++ b/src/main/org/apache/tools/ant/taskdefs/Basename.java @@ -118,20 +118,25 @@ public class Basename extends Task { // The method executing the task public void execute() throws BuildException { - String value; if (property == null) { throw new BuildException("property attribute required", location); } if (file == null) { throw new BuildException("file attribute required", location); - } else { - value = file.getName(); - if (suffix != null && value.endsWith(suffix)) { - int pos = value.indexOf('.'); - value = value.substring(0, pos); - } - getProject().setNewProperty(property, value); } + String value = file.getName(); + if (suffix != null && value.endsWith(suffix)) { + // if the suffix does not starts with a '.' and the + // char preceding the suffix is a '.', we assume the user + // wants to remove the '.' as well (see docs) + int pos = value.length() - suffix.length(); + if (pos > 0 && suffix.charAt(0) != '.' + && value.charAt(pos - 1) == '.') { + pos--; + } + value = value.substring(0, pos); + } + getProject().setNewProperty(property, value); } } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java b/src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java index 02d34a67a..b18d240c4 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java @@ -84,20 +84,38 @@ public class BasenameTest extends BuildFileTest { public void test4() { executeTarget("test4"); - String expected = "foo.txt"; String checkprop = project.getProperty("file.w.suf"); - if (!checkprop.equals(expected)) { - fail("basename failed"); - } + assertEquals("foo.txt", checkprop); } public void test5() { executeTarget("test5"); - String expected = "foo"; String checkprop = project.getProperty("file.wo.suf"); - if (!checkprop.equals(expected)) { - fail("basename failed"); - } + assertEquals("foo", checkprop); } + public void testMultipleDots() { + executeTarget("testMultipleDots"); + String checkprop = project.getProperty("file.wo.suf"); + assertEquals("foo.bar", checkprop); + } + + public void testNoDots() { + executeTarget("testNoDots"); + String checkprop = project.getProperty("file.wo.suf"); + assertEquals("foo.bar", checkprop); + } + + public void testValueEqualsSuffixWithDot() { + executeTarget("testValueEqualsSuffixWithDot"); + String checkprop = project.getProperty("file.wo.suf"); + assertEquals("", checkprop); + } + + public void testValueEqualsSuffixWithoutDot() { + executeTarget("testValueEqualsSuffixWithoutDot"); + String checkprop = project.getProperty("file.wo.suf"); + assertEquals("", checkprop); + } + }