mbenson     2005/01/26 13:20:30

  Modified:    src/main/org/apache/tools/ant/taskdefs Tag: ANT_16_BRANCH
                        Available.java
               src/etc/testcases/taskdefs Tag: ANT_16_BRANCH available.xml
               src/testcases/org/apache/tools/ant/taskdefs Tag:
                        ANT_16_BRANCH AvailableTest.java
  Log:
  <available> returned false positives when checking a file
  passed in with the current basedir leading twice:
  e.g. ${basedir}${file.separator}${basedir}${file.separator}foo .
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.57.2.5  +27 -35    ant/src/main/org/apache/tools/ant/taskdefs/Available.java
  
  Index: Available.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Available.java,v
  retrieving revision 1.57.2.4
  retrieving revision 1.57.2.5
  diff -u -r1.57.2.4 -r1.57.2.5
  --- Available.java    9 Mar 2004 17:01:32 -0000       1.57.2.4
  +++ Available.java    26 Jan 2005 21:19:51 -0000      1.57.2.5
  @@ -1,5 +1,5 @@
   /*
  - * Copyright  2000-2004 The Apache Software Foundation
  + * Copyright  2000-2005 The Apache Software Foundation
    *
    *  Licensed under the Apache License, Version 2.0 (the "License");
    *  you may not use this file except in compliance with the License.
  @@ -38,10 +38,12 @@
    * @ant.task category="control"
    */
   public class Available extends Task implements Condition {
  +    private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
   
       private String property;
       private String classname;
  -    private String file;
  +    private String filename;
  +    private File file;
       private Path filepath;
       private String resource;
       private FileDir type;
  @@ -143,8 +145,8 @@
        * @param file the name of the file which is required.
        */
       public void setFile(File file) {
  -        this.file = FileUtils.newFileUtils()
  -            .removeLeadingPath(getProject().getBaseDir(), file);
  +        this.file = file;
  +        this.filename = 
FILE_UTILS.removeLeadingPath(getProject().getBaseDir(), file);
       }
   
       /**
  @@ -165,7 +167,8 @@
        */
       public void setType(String type) {
           log("DEPRECATED - The setType(String) method has been deprecated."
  -            + " Use setType(Available.FileDir) instead.");
  +            + " Use setType(Available.FileDir) instead.",
  +            Project.MSG_WARN);
           this.type = new FileDir();
           this.type.setValue(type);
       }
  @@ -211,8 +214,11 @@
                           + " property."
                           + StringUtils.LINE_SEP
                           + "  Build file should not reuse the same property"
  -                        + " name for different values.");
  +                        + " name for different values.",
  +                        Project.MSG_WARN);
                   }
  +                // NB: this makes use of Project#setProperty rather than 
Project#setNewProperty
  +                //     due to backwards compatiblity reasons
                   getProject().setProperty(property, value);
               }
           } finally {
  @@ -231,7 +237,6 @@
               throw new BuildException("At least one of (classname|file|"
                                        + "resource) is required", 
getLocation());
           }
  -
           if (type != null) {
               if (file == null) {
                   throw new BuildException("The type attribute is only valid "
  @@ -239,50 +244,42 @@
                                            + "attribute.", getLocation());
               }
           }
  -
           if (classpath != null) {
               classpath.setProject(getProject());
               this.loader = getProject().createClassLoader(classpath);
           }
  -
           String appendix = "";
           if (isTask) {
               appendix = " to set property " + property;
           } else {
               setTaskName("available");
           }
  -
           if ((classname != null) && !checkClass(classname)) {
               log("Unable to load class " + classname + appendix,
                   Project.MSG_VERBOSE);
               return false;
           }
  -
           if ((file != null) && !checkFile()) {
  +            StringBuffer buf = new StringBuffer("Unable to find ");
               if (type != null) {
  -                log("Unable to find " + type + " " + file + appendix,
  -                    Project.MSG_VERBOSE);
  -            } else {
  -                log("Unable to find " + file + appendix, 
Project.MSG_VERBOSE);
  +                buf.append(type).append(' ');
               }
  +            buf.append(filename).append(appendix);
  +            log(buf.toString(), Project.MSG_VERBOSE);
               return false;
           }
  -
           if ((resource != null) && !checkResource(resource)) {
               log("Unable to load resource " + resource + appendix,
                   Project.MSG_VERBOSE);
               return false;
           }
  -
           if (loader != null) {
               loader.cleanup();
               loader = null;
           }
  -
           if (!isTask) {
               setTaskName(null);
           }
  -
           return true;
       }
   
  @@ -304,7 +301,7 @@
        */
       private boolean checkFile() {
           if (filepath == null) {
  -            return checkFile(getProject().resolveFile(file), file);
  +            return checkFile(file, filename);
           } else {
               String[] paths = filepath.list();
               for (int i = 0; i < paths.length; ++i) {
  @@ -313,7 +310,7 @@
   
                   // **   full-pathname specified == path in list
                   // **   simple name specified   == path in list
  -                if (path.exists() && file.equals(paths[i])) {
  +                if (path.exists() && filename.equals(paths[i])) {
                       if (type == null) {
                           log("Found: " + path, Project.MSG_VERBOSE);
                           return true;
  @@ -329,12 +326,10 @@
                       // not the requested type
                       return false;
                   }
  -
  -                FileUtils fileUtils = FileUtils.newFileUtils();
  -                File parent = fileUtils.getParentFile(path);
  +                File parent = path.getParentFile();
                   // **   full-pathname specified == parent dir of path in list
                   if (parent != null && parent.exists()
  -                    && file.equals(parent.getAbsolutePath())) {
  +                    && filename.equals(parent.getAbsolutePath())) {
                       if (type == null) {
                           log("Found: " + parent, Project.MSG_VERBOSE);
                           return true;
  @@ -345,29 +340,26 @@
                       // not the requested type
                       return false;
                   }
  -
                   // **   simple name specified   == path in list + name
                   if (path.exists() && path.isDirectory()) {
  -                    if (checkFile(new File(path, file),
  -                                  file + " in " + path)) {
  +                    if (checkFile(new File(path, filename),
  +                                  filename + " in " + path)) {
                           return true;
                       }
                   }
  -
                   // **   simple name specified   == parent dir + name
                   if (parent != null && parent.exists()) {
  -                    if (checkFile(new File(parent, file),
  -                                  file + " in " + parent)) {
  +                    if (checkFile(new File(parent, filename),
  +                                  filename + " in " + parent)) {
                           return true;
                       }
                   }
  -
                   // **   simple name specified   == parent of parent dir + 
name
                   if (parent != null) {
  -                    File grandParent = fileUtils.getParentFile(parent);
  +                    File grandParent = parent.getParentFile();
                       if (grandParent != null && grandParent.exists()) {
  -                        if (checkFile(new File(grandParent, file),
  -                                      file + " in " + grandParent)) {
  +                        if (checkFile(new File(grandParent, filename),
  +                                      filename + " in " + grandParent)) {
                               return true;
                           }
                       }
  
  
  
  No                   revision
  No                   revision
  1.11.2.2  +10 -0     ant/src/etc/testcases/taskdefs/available.xml
  
  Index: available.xml
  ===================================================================
  RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/available.xml,v
  retrieving revision 1.11.2.1
  retrieving revision 1.11.2.2
  diff -u -r1.11.2.1 -r1.11.2.2
  --- available.xml     22 Oct 2003 13:27:37 -0000      1.11.2.1
  +++ available.xml     26 Jan 2005 21:20:28 -0000      1.11.2.2
  @@ -152,4 +152,14 @@
       <available file="pvcs.xml" filepath="..:optional"
                  property="test" />
     </target>
  +
  +  <target name="testDoubleBasedir">
  +    <echo>testing ${basedir}${file.separator}${ant.file}</echo>
  +    <fail>
  +      <condition>
  +        <available file="${basedir}${file.separator}${ant.file}" />
  +      </condition>
  +    </fail>
  +  </target>
  +
   </project>
  
  
  
  No                   revision
  No                   revision
  1.12.2.5  +12 -6     
ant/src/testcases/org/apache/tools/ant/taskdefs/AvailableTest.java
  
  Index: AvailableTest.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/AvailableTest.java,v
  retrieving revision 1.12.2.4
  retrieving revision 1.12.2.5
  diff -u -r1.12.2.4 -r1.12.2.5
  --- AvailableTest.java        9 Mar 2004 17:02:01 -0000       1.12.2.4
  +++ AvailableTest.java        26 Jan 2005 21:20:28 -0000      1.12.2.5
  @@ -1,5 +1,5 @@
   /*
  - * Copyright  2000-2002,2004 The Apache Software Foundation
  + * Copyright  2000-2002, 2004-2005 The Apache Software Foundation
    *
    *  Licensed under the Apache License, Version 2.0 (the "License");
    *  you may not use this file except in compliance with the License.
  @@ -22,6 +22,7 @@
   import org.apache.tools.ant.util.JavaEnvUtils;
   
   /**
  + * JUnit test for the Available task/condition.
    */
   public class AvailableTest extends BuildFileTest {
   
  @@ -164,25 +165,25 @@
       // Core class that exists in system classpath is ignored, but found in 
specified classpath
       public void test21() {
           executeTarget("test21");
  -        assertEquals("true",project.getProperty("test"));
  +        assertEquals("true", project.getProperty("test"));
       }
   
       // Core class that exists in system classpath is not ignored with 
ignoresystemclass="false"
       public void test22() {
           executeTarget("test22");
  -        assertEquals("true",project.getProperty("test"));
  +        assertEquals("true", project.getProperty("test"));
       }
   
       // Core class that exists in system classpath is not ignored with 
default ignoresystemclasses value
       public void test23() {
           executeTarget("test23");
  -        assertEquals("true",project.getProperty("test"));
  +        assertEquals("true", project.getProperty("test"));
       }
   
       // Class is found in specified classpath
       public void test24() {
           executeTarget("test24");
  -        assertEquals("true",project.getProperty("test"));
  +        assertEquals("true", project.getProperty("test"));
       }
   
       // File is not found in specified filepath
  @@ -194,6 +195,11 @@
       // File is not found in specified filepath
       public void testSearchInPathIsThere() {
           executeTarget("searchInPathIsThere");
  -        assertEquals("true",project.getProperty("test"));
  +        assertEquals("true", project.getProperty("test"));
  +    }
  +
  +    // test when file begins with basedir twice
  +    public void testDoubleBasedir() {
  +        executeTarget("testDoubleBasedir");
       }
   }
  
  
  

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

Reply via email to