diff --git a/src/etc/testcases/taskdefs/fail.xml b/src/etc/testcases/taskdefs/fail.xml index a55aa13ff..6d0150cee 100644 --- a/src/etc/testcases/taskdefs/fail.xml +++ b/src/etc/testcases/taskdefs/fail.xml @@ -22,4 +22,8 @@ + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/Exit.java b/src/main/org/apache/tools/ant/taskdefs/Exit.java index b85f80edd..7feafa230 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Exit.java +++ b/src/main/org/apache/tools/ant/taskdefs/Exit.java @@ -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 Nico Seessle + * The if and unless attributes make the + * failure conditional -both probe for the named property being defined. + * The if tests for the property being defined, the + * unless for a property being undefined. + * + * If both attributes are set, then the test fails only if both tests + * are true. i.e. + *
fail := defined(ifProperty) && !defined(unlessProperty)
* + * @author Nico Seessle + * @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; diff --git a/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java b/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java index c78a51062..ca39309f3 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java @@ -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"); + } + } + }