diff --git a/WHATSNEW b/WHATSNEW index 1ab45fb9a..4f2a703b6 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -32,6 +32,8 @@ Fixed bugs: * could append a newline character at the end of the file. +* 's prefix attribute failed to apply to nested elements. + Other changes: -------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java index 9ad75e595..a4bccc491 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java +++ b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java @@ -115,14 +115,14 @@ public class Tstamp extends Task { } SimpleDateFormat dstamp = new SimpleDateFormat ("yyyyMMdd"); - getProject().setNewProperty(prefix + "DSTAMP", dstamp.format(d)); + setProperty("DSTAMP", dstamp.format(d)); SimpleDateFormat tstamp = new SimpleDateFormat ("HHmm"); - getProject().setNewProperty(prefix + "TSTAMP", tstamp.format(d)); + setProperty("TSTAMP", tstamp.format(d)); SimpleDateFormat today = new SimpleDateFormat ("MMMM d yyyy", Locale.US); - getProject().setNewProperty(prefix + "TODAY", today.format(d)); + setProperty("TODAY", today.format(d)); } catch (Exception e) { throw new BuildException(e); @@ -134,11 +134,19 @@ public class Tstamp extends Task { * @return a ready to fill-in format */ public CustomFormat createFormat() { - CustomFormat cts = new CustomFormat(prefix); + CustomFormat cts = new CustomFormat(); customFormats.addElement(cts); return cts; } + /** + * helper that encapsulates prefix logic and property setting + * policy (i.e. we use setNewProperty instead of setProperty). + */ + private void setProperty(String name, String value) { + getProject().setNewProperty(prefix + name, value); + } + /** * This nested element that allows a property to be set * to the current date and time in a given format. @@ -157,14 +165,11 @@ public class Tstamp extends Task { private String variant; private int offset = 0; private int field = Calendar.DATE; - private String prefix = ""; /** - * Create a format with the current prefix - * @param prefix + * Create a format */ - public CustomFormat(String prefix) { - this.prefix = prefix; + public CustomFormat() { } /** @@ -172,7 +177,7 @@ public class Tstamp extends Task { * @param propertyName */ public void setProperty(String propertyName) { - this.propertyName = prefix + propertyName; + this.propertyName = propertyName; } /** @@ -306,7 +311,7 @@ public class Tstamp extends Task { if (timeZone != null){ sdf.setTimeZone(timeZone); } - project.setNewProperty(propertyName, sdf.format(date)); + Tstamp.this.setProperty(propertyName, sdf.format(date)); } } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/TStampTest.java b/src/testcases/org/apache/tools/ant/taskdefs/TStampTest.java index 49466e5ce..4e0ddf675 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/TStampTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/TStampTest.java @@ -121,6 +121,7 @@ public class TStampTest extends TestCase { assertEquals(expected, today); } + /** * verifies that custom props have priority over the * originals @@ -133,4 +134,16 @@ public class TStampTest extends TestCase { assertNotNull(prop); } + public void testFormatPrefix() throws Exception { + Tstamp.CustomFormat format = tstamp.createFormat(); + format.setProperty("format"); + format.setPattern("HH:mm:ss z"); + format.setTimezone("GMT"); + + tstamp.setPrefix("prefix"); + tstamp.execute(); + String prop= project.getProperty("prefix.format"); + assertNotNull(prop); + } + }