git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@820881 13f79535-47bb-0310-9956-ffa450edef68master
@@ -1117,4 +1117,43 @@ public class PropertyHelper implements GetProperty { | |||||
return result; | 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; | |||||
} | |||||
} | } |
@@ -462,10 +462,7 @@ public class Target implements TaskContainer { | |||||
} | } | ||||
PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(getProject()); | PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(getProject()); | ||||
Object o = propertyHelper.parseProperties(ifCondition); | 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 @@ public class Target implements TaskContainer { | |||||
} | } | ||||
PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(getProject()); | PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(getProject()); | ||||
Object o = propertyHelper.parseProperties(unlessCondition); | Object o = propertyHelper.parseProperties(unlessCondition); | ||||
if (o instanceof Boolean) { | |||||
return !((Boolean) o).booleanValue(); | |||||
} | |||||
return propertyHelper.getProperty(String.valueOf(o)) == null; | |||||
return !propertyHelper.testIfCondition(o); | |||||
} | } | ||||
} | } |
@@ -33,7 +33,25 @@ | |||||
<echo>unless-false called</echo> | <echo>unless-false called</echo> | ||||
</target> | </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" | <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> | </project> |
@@ -46,6 +46,10 @@ public class CreateBoolean extends ProjectComponent { | |||||
<au:assertLogContains text="unless-false called"/> | <au:assertLogContains text="unless-false called"/> | ||||
<au:assertLogDoesntContain text="if-true called"/> | <au:assertLogDoesntContain text="if-true called"/> | ||||
<au:assertLogDoesntContain text="if-false 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> | ||||
<target name="testReferencesSet" depends="setUp"> | <target name="testReferencesSet" depends="setUp"> | ||||
@@ -55,5 +59,24 @@ public class CreateBoolean extends ProjectComponent { | |||||
<au:assertLogContains text="unless-false called"/> | <au:assertLogContains text="unless-false called"/> | ||||
<au:assertLogContains text="if-true called"/> | <au:assertLogContains text="if-true called"/> | ||||
<au:assertLogDoesntContain text="if-false 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> | </target> | ||||
</project> | </project> |