Browse Source

Make fail task use the same if/unless logic as target

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@821676 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 15 years ago
parent
commit
1d56ecb5ab
2 changed files with 82 additions and 20 deletions
  1. +36
    -20
      src/main/org/apache/tools/ant/taskdefs/Exit.java
  2. +46
    -0
      src/tests/antunit/taskdefs/fail-test.xml

+ 36
- 20
src/main/org/apache/tools/ant/taskdefs/Exit.java View File

@@ -19,6 +19,7 @@
package org.apache.tools.ant.taskdefs; package org.apache.tools.ant.taskdefs;


import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.PropertyHelper;
import org.apache.tools.ant.Task; import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ExitStatusException; import org.apache.tools.ant.ExitStatusException;
@@ -59,7 +60,7 @@ public class Exit extends Task {
} }


private String message; private String message;
private String ifCondition, unlessCondition;
private Object ifCondition, unlessCondition;
private NestedCondition nestedCondition; private NestedCondition nestedCondition;
private Integer status; private Integer status;


@@ -73,22 +74,43 @@ public class Exit extends Task {
} }


/** /**
* Only fail if a property of the given name exists in the current project.
* @param c property name
* Only fail if the given expression evaluates to true or the name
* of an existing property.
* @param c property name or evaluated expression
* @since Ant 1.8.0
*/ */
public void setIf(String c) {
public void setIf(Object c) {
ifCondition = c; ifCondition = c;
} }


/** /**
* Only fail if a property of the given name does not
* exist in the current project.
* @param c property name
* Only fail if the given expression evaluates to true or the name
* of an existing property.
* @param c property name or evaluated expression
*/ */
public void setUnless(String c) {
public void setIf(String c) {
setIf((Object) c);
}

/**
* Only fail if the given expression evaluates to false or tno
* property of the given name exists.
* @param c property name or evaluated expression
* @since Ant 1.8.0
*/
public void setUnless(Object c) {
unlessCondition = c; unlessCondition = c;
} }


/**
* Only fail if the given expression evaluates to false or tno
* property of the given name exists.
* @param c property name or evaluated expression
*/
public void setUnless(String c) {
setUnless((Object) c);
}

/** /**
* Set the status code to associate with the thrown Exception. * Set the status code to associate with the thrown Exception.
* @param i the <code>int</code> status * @param i the <code>int</code> status
@@ -117,12 +139,10 @@ public class Exit extends Task {
if (message != null && message.trim().length() > 0) { if (message != null && message.trim().length() > 0) {
text = message.trim(); text = message.trim();
} else { } else {
if (ifCondition != null && ifCondition.length() > 0
&& getProject().getProperty(ifCondition) != null) {
if (!testIfCondition()) {
text = "if=" + ifCondition; text = "if=" + ifCondition;
} }
if (unlessCondition != null && unlessCondition.length() > 0
&& getProject().getProperty(unlessCondition) == null) {
if (!testUnlessCondition()) {
if (text == null) { if (text == null) {
text = ""; text = "";
} else { } else {
@@ -173,10 +193,8 @@ public class Exit extends Task {
* @return true if there is no if condition, or the named property exists * @return true if there is no if condition, or the named property exists
*/ */
private boolean testIfCondition() { private boolean testIfCondition() {
if (ifCondition == null || "".equals(ifCondition)) {
return true;
}
return getProject().getProperty(ifCondition) != null;
return PropertyHelper.getPropertyHelper(getProject())
.testIfCondition(ifCondition);
} }


/** /**
@@ -185,10 +203,8 @@ public class Exit extends Task {
* or there is a named property but it doesn't exist * or there is a named property but it doesn't exist
*/ */
private boolean testUnlessCondition() { private boolean testUnlessCondition() {
if (unlessCondition == null || "".equals(unlessCondition)) {
return true;
}
return getProject().getProperty(unlessCondition) == null;
return PropertyHelper.getPropertyHelper(getProject())
.testUnlessCondition(unlessCondition);
} }


/** /**


+ 46
- 0
src/tests/antunit/taskdefs/fail-test.xml View File

@@ -0,0 +1,46 @@
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project name="echo-test" default="antunit"
xmlns:au="antlib:org.apache.ant.antunit">
<import file="../antunit-base.xml" />

<target name="testIfNotSet">
<au:expectfailure>
<fail unless="${if}"/>
</au:expectfailure>
<fail if="${if}"/>
</target>

<target name="testIfTrue">
<property name="if" value="true"/>
<fail unless="${if}"/>
<au:expectfailure>
<fail if="${if}"/>
</au:expectfailure>
</target>

<target name="testIfFalse">
<property name="if" value="false"/>
<au:expectfailure>
<fail unless="${if}"/>
</au:expectfailure>
<fail if="${if}"/>
</target>

</project>

Loading…
Cancel
Save