stevel      2003/09/06 00:02:31

  Modified:    src/main/org/apache/tools/ant/taskdefs Exit.java
               src/etc/testcases/taskdefs fail.xml
               src/testcases/org/apache/tools/ant/taskdefs FailTest.java
  Log:
  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...
  
  Revision  Changes    Path
  1.23      +46 -6     ant/src/main/org/apache/tools/ant/taskdefs/Exit.java
  
  Index: Exit.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Exit.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- Exit.java 3 Sep 2003 16:47:17 -0000       1.22
  +++ Exit.java 6 Sep 2003 07:02:31 -0000       1.23
  @@ -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 @@
    * Exits the active build, giving an additional message
    * if available.
    *
  - * @author <a href="mailto:[EMAIL PROTECTED]">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:[EMAIL PROTECTED]">Nico Seessle</a>
  + * @author steve loughran
    * @since Ant 1.2
    *
    * @ant.task name="fail" category="control"
  @@ -99,15 +108,38 @@
       }
   
       /**
  -     * 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 @@
           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;
  
  
  
  1.5       +4 -0      ant/src/etc/testcases/taskdefs/fail.xml
  
  Index: fail.xml
  ===================================================================
  RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/fail.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- fail.xml  24 Dec 2001 08:40:40 -0000      1.4
  +++ fail.xml  6 Sep 2003 07:02:31 -0000       1.5
  @@ -22,4 +22,8 @@
       <fail unless="foo" />
     </target>
   
  +  <target name="testIfAndUnless">
  +    <fail unless="unless" if="if"/>
  +  </target>
  +
   </project>
  
  
  
  1.9       +35 -2     
ant/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java
  
  Index: FailTest.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- FailTest.java     7 Mar 2003 11:23:11 -0000       1.8
  +++ FailTest.java     6 Sep 2003 07:02:31 -0000       1.9
  @@ -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 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 @@
               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");
  +        }
  +    }
  +
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to