Browse Source

1. autogenerate text messages from if and unless clauses, when there is no text. Because it is very useful.

2. test cases to check for if and unless combinations are what we intended.

current behaviour is as follows:

if  unless  outcome

F     F     pass
T     F     fail
F     T     pass
T     T     pass

I am not sure this is what makes perfect sense, i'd expect if=F, unless=F to also fail, but what we have, we have. Though since it aint stated in the docs...


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275188 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 22 years ago
parent
commit
a8d3085b01
3 changed files with 85 additions and 8 deletions
  1. +4
    -0
      src/etc/testcases/taskdefs/fail.xml
  2. +46
    -6
      src/main/org/apache/tools/ant/taskdefs/Exit.java
  3. +35
    -2
      src/testcases/org/apache/tools/ant/taskdefs/FailTest.java

+ 4
- 0
src/etc/testcases/taskdefs/fail.xml View File

@@ -22,4 +22,8 @@
<fail unless="foo" />
</target>

<target name="testIfAndUnless">
<fail unless="unless" if="if"/>
</target>

</project>

+ 46
- 6
src/main/org/apache/tools/ant/taskdefs/Exit.java View File

@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -62,8 +62,17 @@ import org.apache.tools.ant.Task;
* Exits the active build, giving an additional message
* if available.
*
* @author <a href="mailto:nico@seessle.de">Nico Seessle</a>
* The <code>if</code> and <code>unless</code> attributes make the
* failure conditional -both probe for the named property being defined.
* The <code>if</code> tests for the property being defined, the
* <code>unless</code> for a property being undefined.
*
* If both attributes are set, then the test fails only if both tests
* are true. i.e.
* <pre>fail := defined(ifProperty) && !defined(unlessProperty)</pre>
*
* @author <a href="mailto:nico@seessle.de">Nico Seessle</a>
* @author steve loughran
* @since Ant 1.2
*
* @ant.task name="fail" category="control"
@@ -99,15 +108,38 @@ public class Exit extends Task {
}

/**
* Exits the actual build.
* evaluate both if and unless conditions, and if
* ifCondition is true or unlessCondition is false, throw a
* build exception to exit the build.
* The errore message is constructed from the text fields, or from
* the if and unless parameters (if present).
* @throws BuildException
*/
public void execute() throws BuildException {
if (testIfCondition() && testUnlessCondition()) {
String text=null;
if (message != null && message.length() > 0) {
throw new BuildException(message);
text=message;
} else {
throw new BuildException("No message");

if(getProject().getProperty(ifCondition) != null) {
text="if="+ifCondition;
}
if (unlessCondition!=null && unlessCondition.length()>0
&& getProject().getProperty(unlessCondition) == null) {
if (text == null) {
text = "";
} else {
text+=" and ";
}
text+="unless="+unlessCondition;
} else {
if(text==null) {
text = "No message";
}
}
}
throw new BuildException(text);
}
}

@@ -122,14 +154,22 @@ public class Exit extends Task {
message += getProject().replaceProperties(msg);
}

/**
* test the if condition
* @return true if there is no if condition, or the named property exists
*/
private boolean testIfCondition() {
if (ifCondition == null || "".equals(ifCondition)) {
return true;
}

return getProject().getProperty(ifCondition) != null;
}

/**
* test the unless condition
* @return true if there is no unless condition,
* or there is a named property but it doesnt exist
*/
private boolean testUnlessCondition() {
if (unlessCondition == null || "".equals(unlessCondition)) {
return true;


+ 35
- 2
src/testcases/org/apache/tools/ant/taskdefs/FailTest.java View File

@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -72,7 +72,9 @@ public class FailTest extends BuildFileTest {
}

public void test1() {
expectBuildException("test1", "it is required to fail :-)");
expectBuildExceptionContaining("test1",
"it is required to fail :-)",
"No message");
}

public void test2() {
@@ -107,4 +109,35 @@ public class FailTest extends BuildFileTest {
fail("foo has been defined, testUnless must not fail");
}
}

/**
* see that the different combinations work, and
* that the autogenerated text contains information
* about which condition was not met
*/
public void testIfAndUnless() {
//neither
executeTarget("testIfAndUnless");
project.setProperty("if", "");
expectBuildExceptionContaining("testIfAndUnless",
"expect fail on defined(if)",
"if=if and unless=unless");
project.setProperty("unless", "");
//this call should succeed as unless overrides if
executeTarget("testIfAndUnless");
}
/**
* see that the different combinations work, and
* that the autogenerated text contains information
* about which condition was not met
*/
public void testIfAndUnless2() {
project.setProperty("unless", "");
try {
executeTarget("testIfAndUnless");
} catch (BuildException be) {
fail("defined(if) && !defined(unless); testIfAndUnless must not fail");
}
}

}

Loading…
Cancel
Save