Author: bodewig
Date: Fri Oct 2 04:20:18 2009
New Revision: 820881
URL: http://svn.apache.org/viewvc?rev=820881&view=rev
Log:
Make target's if/unless treat true/false strings as booleans
Modified:
ant/core/trunk/src/main/org/apache/tools/ant/PropertyHelper.java
ant/core/trunk/src/main/org/apache/tools/ant/Target.java
ant/core/trunk/src/tests/antunit/core/target-test-helper.xml
ant/core/trunk/src/tests/antunit/core/target-test.xml
Modified: ant/core/trunk/src/main/org/apache/tools/ant/PropertyHelper.java
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/PropertyHelper.java?rev=820881&r1=820880&r2=820881&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/PropertyHelper.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/PropertyHelper.java Fri Oct 2
04:20:18 2009
@@ -1117,4 +1117,43 @@
return result;
}
+ /**
+ * If the given object can be interpreted as a true/false value,
+ * turn it into a matching Boolean - otherwise return null.
+ * @since Ant 1.8.0
+ */
+ public static Boolean toBoolean(Object value) {
+ if (value instanceof Boolean) {
+ return (Boolean) value;
+ }
+ if (value instanceof String) {
+ String s = (String) value;
+ if (Project.toBoolean(s)) {
+ return Boolean.TRUE;
+ }
+ if ("off".equalsIgnoreCase(s)
+ || "false".equalsIgnoreCase(s)
+ || "no".equalsIgnoreCase(s)) {
+ return Boolean.FALSE;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns true if the value is not-null, can be interpreted as a
+ * true value or cannot be interpreted as a false value and a
+ * property of the value's name exists.
+ * @since Ant 1.8.0
+ */
+ public boolean testIfCondition(Object value) {
+ if (value == null) {
+ return false;
+ }
+ Boolean b = toBoolean(value);
+ if (b != null) {
+ return b.booleanValue();
+ }
+ return getProperty(String.valueOf(value)) != null;
+ }
}
Modified: ant/core/trunk/src/main/org/apache/tools/ant/Target.java
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/Target.java?rev=820881&r1=820880&r2=820881&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/Target.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/Target.java Fri Oct 2
04:20:18 2009
@@ -462,10 +462,7 @@
}
PropertyHelper propertyHelper =
PropertyHelper.getPropertyHelper(getProject());
Object o = propertyHelper.parseProperties(ifCondition);
- if (o instanceof Boolean) {
- return ((Boolean) o).booleanValue();
- }
- return propertyHelper.getProperty(String.valueOf(o)) != null;
+ return propertyHelper.testIfCondition(o);
}
/**
@@ -483,9 +480,6 @@
}
PropertyHelper propertyHelper =
PropertyHelper.getPropertyHelper(getProject());
Object o = propertyHelper.parseProperties(unlessCondition);
- if (o instanceof Boolean) {
- return !((Boolean) o).booleanValue();
- }
- return propertyHelper.getProperty(String.valueOf(o)) == null;
+ return !propertyHelper.testIfCondition(o);
}
}
Modified: ant/core/trunk/src/tests/antunit/core/target-test-helper.xml
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/core/target-test-helper.xml?rev=820881&r1=820880&r2=820881&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/core/target-test-helper.xml (original)
+++ ant/core/trunk/src/tests/antunit/core/target-test-helper.xml Fri Oct 2
04:20:18 2009
@@ -33,7 +33,25 @@
<echo>unless-false called</echo>
</target>
+ <target name="if-string-true" if="${true}">
+ <echo>if-string-true called</echo>
+ </target>
+
+ <target name="unless-string-true" unless="${true}">
+ <echo>unless-string-true called</echo>
+ </target>
+
+ <target name="if-string-false" if="${false}">
+ <echo>if-string-false called</echo>
+ </target>
+
+ <target name="unless-string-false" unless="${false}">
+ <echo>unless-string-false called</echo>
+ </target>
+
<target name="all"
- depends="if-true,unless-true,if-false,unless-false"/>
+ depends="if-true,unless-true,if-false,unless-false,
+ if-string-true,unless-string-true,
+ if-string-false,unless-string-false"/>
</project>
Modified: ant/core/trunk/src/tests/antunit/core/target-test.xml
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/core/target-test.xml?rev=820881&r1=820880&r2=820881&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/core/target-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/core/target-test.xml Fri Oct 2 04:20:18
2009
@@ -46,6 +46,10 @@
<au:assertLogContains text="unless-false called"/>
<au:assertLogDoesntContain text="if-true called"/>
<au:assertLogDoesntContain text="if-false called"/>
+ <au:assertLogContains text="unless-string-true called"/>
+ <au:assertLogContains text="unless-string-false called"/>
+ <au:assertLogDoesntContain text="if-string-true called"/>
+ <au:assertLogDoesntContain text="if-string-false called"/>
</target>
<target name="testReferencesSet" depends="setUp">
@@ -55,5 +59,24 @@
<au:assertLogContains text="unless-false called"/>
<au:assertLogContains text="if-true called"/>
<au:assertLogDoesntContain text="if-false called"/>
+ <au:assertLogContains text="unless-string-true called"/>
+ <au:assertLogContains text="unless-string-false called"/>
+ <au:assertLogDoesntContain text="if-string-true called"/>
+ <au:assertLogDoesntContain text="if-string-false called"/>
+ </target>
+
+ <target name="testProperttiesSet">
+ <ant antfile="target-test-helper.xml" target="all">
+ <property name="true" value="true"/>
+ <property name="false" value="false"/>
+ </ant>
+ <au:assertLogDoesntContain text="unless-string-true called"/>
+ <au:assertLogContains text="unless-string-false called"/>
+ <au:assertLogContains text="if-string-true called"/>
+ <au:assertLogDoesntContain text="if-string-false called"/>
+ <au:assertLogContains text="unless-true called"/>
+ <au:assertLogContains text="unless-false called"/>
+ <au:assertLogDoesntContain text="if-true called"/>
+ <au:assertLogDoesntContain text="if-false called"/>
</target>
</project>