Browse Source

Make target's if/unless treat true/false strings as booleans

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@820881 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 15 years ago
parent
commit
c6b7ba94ce
4 changed files with 83 additions and 9 deletions
  1. +39
    -0
      src/main/org/apache/tools/ant/PropertyHelper.java
  2. +2
    -8
      src/main/org/apache/tools/ant/Target.java
  3. +19
    -1
      src/tests/antunit/core/target-test-helper.xml
  4. +23
    -0
      src/tests/antunit/core/target-test.xml

+ 39
- 0
src/main/org/apache/tools/ant/PropertyHelper.java View File

@@ -1117,4 +1117,43 @@ public class PropertyHelper implements GetProperty {
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;
}
}

+ 2
- 8
src/main/org/apache/tools/ant/Target.java View File

@@ -462,10 +462,7 @@ public class Target implements TaskContainer {
}
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 @@ public class Target implements TaskContainer {
}
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);
}
}

+ 19
- 1
src/tests/antunit/core/target-test-helper.xml View File

@@ -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>

+ 23
- 0
src/tests/antunit/core/target-test.xml View File

@@ -46,6 +46,10 @@ public class CreateBoolean extends ProjectComponent {
<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 @@ public class CreateBoolean extends ProjectComponent {
<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>

Loading…
Cancel
Save