You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2001/10/01 15:32:36 UTC
cvs commit: jakarta-ant/src/testcases/org/apache/tools/ant/util FileUtilsTest.java
bodewig 01/10/01 06:32:36
Modified: src/main/org/apache/tools/ant/util FileUtils.java
SourceFileScanner.java
src/testcases/org/apache/tools/ant/util FileUtilsTest.java
Log:
Make FileUtils.resolveFile more robust - modelled after File(File, String).
This avoids the explicit checks for null directories in
SourceFileScanner as well.
Revision Changes Path
1.5 +10 -1 jakarta-ant/src/main/org/apache/tools/ant/util/FileUtils.java
Index: FileUtils.java
===================================================================
RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/util/FileUtils.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- FileUtils.java 2001/08/18 14:59:40 1.4
+++ FileUtils.java 2001/10/01 13:32:36 1.5
@@ -299,7 +299,9 @@
* @param file the "reference" file for relative paths. This
* instance must be an absolute file and must not contain
* "./" or "../" sequences (same for \ instead
- * of /).
+ * of /). If it is null, this call is equivalent to
+ * <code>new java.io.File(filename)</code>.
+ *
* @param filename a file name
*
* @return an absolute file that doesn't contain "./" or
@@ -327,6 +329,10 @@
return normalize(filename);
}
+ if (file == null) {
+ return new File(filename);
+ }
+
File helpFile = new File(file.getAbsolutePath());
StringTokenizer tok = new StringTokenizer(filename, File.separator);
while (tok.hasMoreTokens()) {
@@ -361,6 +367,9 @@
* <li>DOS style paths that start with a drive letter will have
* \ as the separator.</li>
* </ul>
+ *
+ * @throws java.lang.NullPointerException if the file path is
+ * equal to null.
*/
public File normalize(String path) {
String orig = path;
1.8 +2 -12 jakarta-ant/src/main/org/apache/tools/ant/util/SourceFileScanner.java
Index: SourceFileScanner.java
===================================================================
RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/util/SourceFileScanner.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- SourceFileScanner.java 2001/10/01 07:44:42 1.7
+++ SourceFileScanner.java 2001/10/01 13:32:36 1.8
@@ -121,12 +121,7 @@
continue;
}
- File src = null;
- if (srcDir == null) {
- src = new File(files[i]);
- } else {
- src = fileUtils.resolveFile(srcDir, files[i]);
- }
+ File src = fileUtils.resolveFile(srcDir, files[i]);
if (src.lastModified() > now) {
task.log("Warning: "+files[i]+" modified in the future.",
@@ -136,12 +131,7 @@
boolean added = false;
targetList.setLength(0);
for (int j=0; !added && j<targets.length; j++) {
- File dest = null;
- if (destDir == null) {
- dest = new File(targets[j]);
- } else {
- dest = fileUtils.resolveFile(destDir, targets[j]);
- }
+ File dest = fileUtils.resolveFile(destDir, targets[j]);
if (!dest.exists()) {
task.log(files[i]+" added as "+dest.getAbsolutePath()+" doesn\'t exist.",
1.4 +15 -0 jakarta-ant/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java
Index: FileUtilsTest.java
===================================================================
RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- FileUtilsTest.java 2001/08/18 14:59:40 1.3
+++ FileUtilsTest.java 2001/10/01 13:32:36 1.4
@@ -259,6 +259,21 @@
}
/**
+ * Test handling of null arguments.
+ */
+ public void testNullArgs() {
+ try {
+ fu.normalize(null);
+ fail("successfully normalized a null-file");
+ } catch (NullPointerException npe) {
+ // Expected exception caught
+ }
+
+ File f = fu.resolveFile(null, "a");
+ assertEquals(f, new File("a"));
+ }
+
+ /**
* adapt file separators to local conventions
*/
private String localize(String path) {