From 72939923a1fbdb0b6b397d68db9e78ec2ac64150 Mon Sep 17 00:00:00 2001 From: Jan Materne Date: Thu, 20 Aug 2009 13:01:18 +0000 Subject: [PATCH] now support deletion of entries git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@806154 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 2 ++ docs/manual/OptionalTasks/propertyfile.html | 29 +++++++------------ .../taskdefs/optional/propertyfile.xml | 12 ++++++++ .../ant/taskdefs/optional/PropertyFile.java | 13 +++++++-- .../taskdefs/optional/PropertyFileTest.java | 16 ++++++++++ 5 files changed, 52 insertions(+), 20 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index c24d3aebd..3d68d1d5f 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -857,6 +857,8 @@ Other changes: * It is now possible to suppress the "FAILED" lines sent to Ant's logging system via 's new logFailedTests attribute. Bugzilla Report 35073. + + * now can delete entries. Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/OptionalTasks/propertyfile.html b/docs/manual/OptionalTasks/propertyfile.html index d914b2ebd..7db35606e 100644 --- a/docs/manual/OptionalTasks/propertyfile.html +++ b/docs/manual/OptionalTasks/propertyfile.html @@ -24,26 +24,12 @@

PropertyFile

-

by

- - - -
-

Table of Contents

-

Introduction

-

Ant provides an optional task for editing property files. This is very useful +

Ant provides an optional task for editing property files. This is very useful when wanting to make unattended modifications to configuration files for application -servers and applications. Currently, the task maintains a working property file with +servers and applications. Currently, the task maintains a working property file with the ability to add properties or make changes to existing ones. Comments and layout of the original properties file are preserved.

@@ -93,7 +79,7 @@ elements to specify actual modifications to the property file itself.

value Value to set (=), to add (+) or subtract (-) - At least one must be specified + At least one must be specified, if operation is not delete default @@ -108,7 +94,14 @@ elements to specify actual modifications to the property file itself.

operation - "+" or "=" (default) for all datatypes
"-" (for date and int only).
+ One of the following operations:

+ for all datatypes:
    +
  • "del" : deletes an entry
  • +
  • "+" : adds a value to the existing value
  • +
  • "=" : sets a value instead of the existing value (default)
  • +

for date and int only:
    +
  • "-" : subtracts a value from the existing value
  • +
No diff --git a/src/etc/testcases/taskdefs/optional/propertyfile.xml b/src/etc/testcases/taskdefs/optional/propertyfile.xml index a51368126..e656f5d24 100644 --- a/src/etc/testcases/taskdefs/optional/propertyfile.xml +++ b/src/etc/testcases/taskdefs/optional/propertyfile.xml @@ -38,7 +38,15 @@
+ + + + + + @@ -85,15 +93,19 @@
+ foo=3 + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java b/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java index 46df19b48..847702362 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java @@ -347,6 +347,11 @@ public class PropertyFile extends Task { */ protected void executeOn(Properties props) throws BuildException { checkParameters(); + + if (operation == Operation.DELETE_OPER) { + props.remove(key); + return; + } // type may be null because it wasn't set String oldValue = (String) props.get(key); @@ -508,7 +513,7 @@ public class PropertyFile extends Task { throw new BuildException("- is not supported for string " + "properties (key:" + key + ")"); } - if (value == null && defaultValue == null) { + if (value == null && defaultValue == null && operation != Operation.DELETE_OPER) { throw new BuildException("\"value\" and/or \"default\" " + "attribute must be specified (key:" + key + ")"); } @@ -574,10 +579,12 @@ public class PropertyFile extends Task { public static final int DECREMENT_OPER = 1; /** = */ public static final int EQUALS_OPER = 2; + /** del */ + public static final int DELETE_OPER = 3; /** {@inheritDoc}. */ public String[] getValues() { - return new String[] {"+", "-", "="}; + return new String[] {"+", "-", "=", "del"}; } /** @@ -590,6 +597,8 @@ public class PropertyFile extends Task { return INCREMENT_OPER; } else if ("-".equals(oper)) { return DECREMENT_OPER; + } else if ("del".equals(oper)) { + return DELETE_OPER; } return EQUALS_OPER; } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/PropertyFileTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/PropertyFileTest.java index 795d52cd4..012294bc8 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/PropertyFileTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/PropertyFileTest.java @@ -93,6 +93,22 @@ public class PropertyFileTest extends BuildFileTest { assertEquals(NEW_AGE, afterUpdate.getProperty(AGE_KEY)); assertEquals(NEW_DATE, afterUpdate.getProperty(DATE_KEY)); } + + public void testDeleteProperties() throws Exception { + Properties beforeUpdate = getTestProperties(); + assertEquals("Property '" + FNAME_KEY + "' should exist before deleting", + FNAME, beforeUpdate.getProperty(FNAME_KEY)); + assertEquals("Property '" + LNAME_KEY + "' should exist before deleting", + LNAME, beforeUpdate.getProperty(LNAME_KEY)); + + executeTarget("delete-properties"); + Properties afterUpdate = getTestProperties(); + + assertEquals("Property '" + LNAME_KEY + "' should exist after deleting", + LNAME, afterUpdate.getProperty(LNAME_KEY)); + assertNull("Property '" + FNAME_KEY + "' should be deleted", + afterUpdate.getProperty(FNAME_KEY)); + } public void testExerciseDefaultAndIncrement() throws Exception { executeTarget("exercise");