Browse Source

<available> returned false positives when checking a file

passed in with the current basedir leading twice.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277462 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 20 years ago
parent
commit
4c77f5ea1b
4 changed files with 42 additions and 34 deletions
  1. +3
    -0
      WHATSNEW
  2. +10
    -0
      src/etc/testcases/taskdefs/available.xml
  3. +17
    -28
      src/main/org/apache/tools/ant/taskdefs/Available.java
  4. +12
    -6
      src/testcases/org/apache/tools/ant/taskdefs/AvailableTest.java

+ 3
- 0
WHATSNEW View File

@@ -50,6 +50,9 @@ Fixed bugs:
<replace> can now handle files as long as there is enough disk space <replace> can now handle files as long as there is enough disk space
available. available.


* <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 .


Other changes: Other changes:
-------------- --------------


+ 10
- 0
src/etc/testcases/taskdefs/available.xml View File

@@ -152,4 +152,14 @@
<available file="pvcs.xml" filepath="..:optional" <available file="pvcs.xml" filepath="..:optional"
property="test" /> property="test" />
</target> </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> </project>

+ 17
- 28
src/main/org/apache/tools/ant/taskdefs/Available.java View File

@@ -42,7 +42,8 @@ public class Available extends Task implements Condition {


private String property; private String property;
private String classname; private String classname;
private String file;
private String filename;
private File file;
private Path filepath; private Path filepath;
private String resource; private String resource;
private FileDir type; private FileDir type;
@@ -144,7 +145,8 @@ public class Available extends Task implements Condition {
* @param file the name of the file which is required. * @param file the name of the file which is required.
*/ */
public void setFile(File file) { public void setFile(File file) {
this.file = FILE_UTILS.removeLeadingPath(getProject().getBaseDir(), file);
this.file = file;
this.filename = FILE_UTILS.removeLeadingPath(getProject().getBaseDir(), file);
} }


/** /**
@@ -235,7 +237,6 @@ public class Available extends Task implements Condition {
throw new BuildException("At least one of (classname|file|" throw new BuildException("At least one of (classname|file|"
+ "resource) is required", getLocation()); + "resource) is required", getLocation());
} }

if (type != null) { if (type != null) {
if (file == null) { if (file == null) {
throw new BuildException("The type attribute is only valid " throw new BuildException("The type attribute is only valid "
@@ -243,50 +244,42 @@ public class Available extends Task implements Condition {
+ "attribute.", getLocation()); + "attribute.", getLocation());
} }
} }

if (classpath != null) { if (classpath != null) {
classpath.setProject(getProject()); classpath.setProject(getProject());
this.loader = getProject().createClassLoader(classpath); this.loader = getProject().createClassLoader(classpath);
} }

String appendix = ""; String appendix = "";
if (isTask) { if (isTask) {
appendix = " to set property " + property; appendix = " to set property " + property;
} else { } else {
setTaskName("available"); setTaskName("available");
} }

if ((classname != null) && !checkClass(classname)) { if ((classname != null) && !checkClass(classname)) {
log("Unable to load class " + classname + appendix, log("Unable to load class " + classname + appendix,
Project.MSG_VERBOSE); Project.MSG_VERBOSE);
return false; return false;
} }

if ((file != null) && !checkFile()) { if ((file != null) && !checkFile()) {
StringBuffer buf = new StringBuffer("Unable to find ");
if (type != null) { 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; return false;
} }

if ((resource != null) && !checkResource(resource)) { if ((resource != null) && !checkResource(resource)) {
log("Unable to load resource " + resource + appendix, log("Unable to load resource " + resource + appendix,
Project.MSG_VERBOSE); Project.MSG_VERBOSE);
return false; return false;
} }

if (loader != null) { if (loader != null) {
loader.cleanup(); loader.cleanup();
loader = null; loader = null;
} }

if (!isTask) { if (!isTask) {
setTaskName(null); setTaskName(null);
} }

return true; return true;
} }


@@ -308,7 +301,7 @@ public class Available extends Task implements Condition {
*/ */
private boolean checkFile() { private boolean checkFile() {
if (filepath == null) { if (filepath == null) {
return checkFile(getProject().resolveFile(file), file);
return checkFile(file, filename);
} else { } else {
String[] paths = filepath.list(); String[] paths = filepath.list();
for (int i = 0; i < paths.length; ++i) { for (int i = 0; i < paths.length; ++i) {
@@ -317,7 +310,7 @@ public class Available extends Task implements Condition {


// ** full-pathname specified == path in list // ** full-pathname specified == path in list
// ** simple name 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) { if (type == null) {
log("Found: " + path, Project.MSG_VERBOSE); log("Found: " + path, Project.MSG_VERBOSE);
return true; return true;
@@ -333,11 +326,10 @@ public class Available extends Task implements Condition {
// not the requested type // not the requested type
return false; return false;
} }

File parent = path.getParentFile(); File parent = path.getParentFile();
// ** full-pathname specified == parent dir of path in list // ** full-pathname specified == parent dir of path in list
if (parent != null && parent.exists() if (parent != null && parent.exists()
&& file.equals(parent.getAbsolutePath())) {
&& filename.equals(parent.getAbsolutePath())) {
if (type == null) { if (type == null) {
log("Found: " + parent, Project.MSG_VERBOSE); log("Found: " + parent, Project.MSG_VERBOSE);
return true; return true;
@@ -348,29 +340,26 @@ public class Available extends Task implements Condition {
// not the requested type // not the requested type
return false; return false;
} }

// ** simple name specified == path in list + name // ** simple name specified == path in list + name
if (path.exists() && path.isDirectory()) { 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; return true;
} }
} }

// ** simple name specified == parent dir + name // ** simple name specified == parent dir + name
if (parent != null && parent.exists()) { 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; return true;
} }
} }

// ** simple name specified == parent of parent dir + name // ** simple name specified == parent of parent dir + name
if (parent != null) { if (parent != null) {
File grandParent = parent.getParentFile(); File grandParent = parent.getParentFile();
if (grandParent != null && grandParent.exists()) { 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; return true;
} }
} }


+ 12
- 6
src/testcases/org/apache/tools/ant/taskdefs/AvailableTest.java View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import org.apache.tools.ant.Project;
import org.apache.tools.ant.util.JavaEnvUtils; import org.apache.tools.ant.util.JavaEnvUtils;


/** /**
* JUnit test for the Available task/condition.
*/ */
public class AvailableTest extends BuildFileTest { public class AvailableTest extends BuildFileTest {


@@ -164,25 +165,25 @@ public class AvailableTest extends BuildFileTest {
// Core class that exists in system classpath is ignored, but found in specified classpath // Core class that exists in system classpath is ignored, but found in specified classpath
public void test21() { public void test21() {
executeTarget("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" // Core class that exists in system classpath is not ignored with ignoresystemclass="false"
public void test22() { public void test22() {
executeTarget("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 // Core class that exists in system classpath is not ignored with default ignoresystemclasses value
public void test23() { public void test23() {
executeTarget("test23"); executeTarget("test23");
assertEquals("true",project.getProperty("test"));
assertEquals("true", project.getProperty("test"));
} }


// Class is found in specified classpath // Class is found in specified classpath
public void test24() { public void test24() {
executeTarget("test24"); executeTarget("test24");
assertEquals("true",project.getProperty("test"));
assertEquals("true", project.getProperty("test"));
} }


// File is not found in specified filepath // File is not found in specified filepath
@@ -194,6 +195,11 @@ public class AvailableTest extends BuildFileTest {
// File is not found in specified filepath // File is not found in specified filepath
public void testSearchInPathIsThere() { public void testSearchInPathIsThere() {
executeTarget("searchInPathIsThere"); executeTarget("searchInPathIsThere");
assertEquals("true",project.getProperty("test"));
assertEquals("true", project.getProperty("test"));
}

// test when file begins with basedir twice
public void testDoubleBasedir() {
executeTarget("testDoubleBasedir");
} }
} }

Loading…
Cancel
Save