Browse Source

Take care of extreme cases to do some suffix magic.

Added some more testcases to document this behavior.

PR: 10769
Reported by: jan.grant@bristol.ac.uk (Jan Grant)


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273105 13f79535-47bb-0310-9956-ffa450edef68
master
Stephane Bailliez 23 years ago
parent
commit
4ce5b624e1
3 changed files with 55 additions and 16 deletions
  1. +16
    -0
      src/etc/testcases/taskdefs/basename.xml
  2. +13
    -8
      src/main/org/apache/tools/ant/taskdefs/Basename.java
  3. +26
    -8
      src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java

+ 16
- 0
src/etc/testcases/taskdefs/basename.xml View File

@@ -22,4 +22,20 @@
<basename property="file.wo.suf" file="foo.txt" suffix="txt"/> <basename property="file.wo.suf" file="foo.txt" suffix="txt"/>
</target> </target>


<target name="testMultipleDots">
<basename property="file.wo.suf" file="foo.bar.txt" suffix="txt"/>
</target>

<target name="testNoDots">
<basename property="file.wo.suf" file="foo.bartxt" suffix="txt"/>
</target>
<target name="testValueEqualsSuffixWithDot">
<basename property="file.wo.suf" file=".txt" suffix=".txt"/>
</target>

<target name="testValueEqualsSuffixWithoutDot">
<basename property="file.wo.suf" file=".txt" suffix="txt"/>
</target>
</project> </project>

+ 13
- 8
src/main/org/apache/tools/ant/taskdefs/Basename.java View File

@@ -118,20 +118,25 @@ public class Basename extends Task {


// The method executing the task // The method executing the task
public void execute() throws BuildException { public void execute() throws BuildException {
String value;
if (property == null) { if (property == null) {
throw new BuildException("property attribute required", location); throw new BuildException("property attribute required", location);
} }
if (file == null) { if (file == null) {
throw new BuildException("file attribute required", location); 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);
} }
} }



+ 26
- 8
src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java View File

@@ -84,20 +84,38 @@ public class BasenameTest extends BuildFileTest {


public void test4() { public void test4() {
executeTarget("test4"); executeTarget("test4");
String expected = "foo.txt";
String checkprop = project.getProperty("file.w.suf"); String checkprop = project.getProperty("file.w.suf");
if (!checkprop.equals(expected)) {
fail("basename failed");
}
assertEquals("foo.txt", checkprop);
} }
public void test5() { public void test5() {
executeTarget("test5"); executeTarget("test5");
String expected = "foo";
String checkprop = project.getProperty("file.wo.suf"); 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);
}

} }

Loading…
Cancel
Save