6907737: (file) FileVisitor and Files.walkFileTree issues
Reviewed-by: sherman
diff --git a/test/java/nio/file/Files/MaxDepth.java b/test/java/nio/file/Files/MaxDepth.java
new file mode 100644
index 0000000..a895238
--- /dev/null
+++ b/test/java/nio/file/Files/MaxDepth.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.nio.file.*;
+import java.nio.file.attribute.*;
+import java.io.IOException;
+import java.util.*;
+
+/**
+ * Unit test for Files.walkFileTree to test maxDepth parameter
+ */
+
+public class MaxDepth {
+ public static void main(String[] args) throws Exception {
+ final Path top = Paths.get(args[0]);
+
+ for (int i=0; i<5; i++) {
+ Set<FileVisitOption> opts = Collections.emptySet();
+ final int maxDepth = i;
+ Files.walkFileTree(top, opts, maxDepth, new SimpleFileVisitor<Path>() {
+ // compute depth based on relative path to top directory
+ private int depth(Path file) {
+ Path rp = file.relativize(top);
+ return (rp == null) ? 0 : rp.getNameCount();
+ }
+
+ @Override
+ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
+ int d = depth(dir);
+ if (d == maxDepth)
+ throw new RuntimeException("Should not open directories at maxDepth");
+ if (d > maxDepth)
+ throw new RuntimeException("Too deep");
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
+ int d = depth(file);
+ if (d > maxDepth)
+ throw new RuntimeException("Too deep");
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ }
+ }
+}
diff --git a/test/java/nio/file/Files/Misc.java b/test/java/nio/file/Files/Misc.java
index d722f72..89e7c25 100644
--- a/test/java/nio/file/Files/Misc.java
+++ b/test/java/nio/file/Files/Misc.java
@@ -30,6 +30,7 @@
import java.nio.file.*;
import java.nio.file.attribute.Attributes;
+import java.nio.file.attribute.BasicFileAttributes;
import java.io.IOException;
import java.util.*;
@@ -117,25 +118,25 @@
SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { };
boolean ranTheGauntlet = false;
- try { visitor.preVisitDirectory(null);
+ BasicFileAttributes attrs = Attributes.readBasicFileAttributes(Paths.get("."));
+
+ try { visitor.preVisitDirectory(null, attrs);
} catch (NullPointerException x0) {
- try { visitor.preVisitDirectoryFailed(null, new IOException());
+ try { visitor.preVisitDirectory(dir, null);
} catch (NullPointerException x1) {
- try { visitor.preVisitDirectoryFailed(dir, null);
+ try { visitor.visitFile(null, attrs);
} catch (NullPointerException x2) {
- try { visitor.visitFile(null, Attributes.readBasicFileAttributes(Paths.get(".")));
- } catch (NullPointerException x3) {
try { visitor.visitFile(dir, null);
- } catch (NullPointerException x4) {
+ } catch (NullPointerException x3) {
try { visitor.visitFileFailed(null, new IOException());
- } catch (NullPointerException x5) {
+ } catch (NullPointerException x4) {
try { visitor.visitFileFailed(dir, null);
- } catch (NullPointerException x6) {
+ } catch (NullPointerException x5) {
try { visitor.postVisitDirectory(null, new IOException());
- } catch (NullPointerException x7) {
+ } catch (NullPointerException x6) {
// if we get here then all visit* methods threw NPE as expected
ranTheGauntlet = true;
- }}}}}}}}
+ }}}}}}}
if (!ranTheGauntlet)
throw new RuntimeException("A visit method did not throw NPE");
}
diff --git a/test/java/nio/file/Files/PrintFileTree.java b/test/java/nio/file/Files/PrintFileTree.java
index 044da8f..fdc17cd 100644
--- a/test/java/nio/file/Files/PrintFileTree.java
+++ b/test/java/nio/file/Files/PrintFileTree.java
@@ -56,29 +56,34 @@
final boolean reportCycles = printCycles;
Files.walkFileTree(dir, options, Integer.MAX_VALUE, new FileVisitor<FileRef>() {
- public FileVisitResult preVisitDirectory(FileRef dir) {
+ @Override
+ public FileVisitResult preVisitDirectory(FileRef dir, BasicFileAttributes attrs) {
System.out.println(dir);
return FileVisitResult.CONTINUE;
}
- public FileVisitResult preVisitDirectoryFailed(FileRef dir, IOException exc) {
- exc.printStackTrace();
- return FileVisitResult.CONTINUE;
- }
+ @Override
public FileVisitResult visitFile(FileRef file, BasicFileAttributes attrs) {
if (!attrs.isDirectory() || reportCycles)
System.out.println(file);
return FileVisitResult.CONTINUE;
}
- public FileVisitResult postVisitDirectory(FileRef dir, IOException exc) {
- if (exc != null) {
- exc.printStackTrace();
- return FileVisitResult.TERMINATE;
- }
+ @Override
+ public FileVisitResult postVisitDirectory(FileRef dir, IOException exc)
+ throws IOException
+ {
+ if (exc != null)
+ throw exc;
return FileVisitResult.CONTINUE;
}
- public FileVisitResult visitFileFailed(FileRef file, IOException exc) {
- exc.printStackTrace();
- return FileVisitResult.TERMINATE;
+ @Override
+ public FileVisitResult visitFileFailed(FileRef file, IOException exc)
+ throws IOException
+ {
+ if (reportCycles && (exc instanceof FileSystemLoopException)) {
+ System.out.println(file);
+ return FileVisitResult.CONTINUE;
+ }
+ throw exc;
}
});
}
diff --git a/test/java/nio/file/Files/SkipSiblings.java b/test/java/nio/file/Files/SkipSiblings.java
index 952fdea..5cd7e34 100644
--- a/test/java/nio/file/Files/SkipSiblings.java
+++ b/test/java/nio/file/Files/SkipSiblings.java
@@ -54,32 +54,28 @@
public static void main(String[] args) throws Exception {
Path dir = Paths.get(args[0]);
- Files.walkFileTree(dir, new FileVisitor<Path>() {
- public FileVisitResult preVisitDirectory(Path dir) {
+ Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
check(dir);
if (skip(dir))
return FileVisitResult.SKIP_SIBLINGS;
return FileVisitResult.CONTINUE;
}
- public FileVisitResult preVisitDirectoryFailed(Path dir, IOException exc) {
- throw new RuntimeException(exc);
- }
-
+ @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
check(file);
if (skip(file))
return FileVisitResult.SKIP_SIBLINGS;
return FileVisitResult.CONTINUE;
}
+ @Override
public FileVisitResult postVisitDirectory(Path dir, IOException x) {
if (x != null)
throw new RuntimeException(x);
check(dir);
return FileVisitResult.CONTINUE;
}
- public FileVisitResult visitFileFailed(Path file, IOException x) {
- throw new RuntimeException(x);
- }
});
}
}
diff --git a/test/java/nio/file/Files/TerminateWalk.java b/test/java/nio/file/Files/TerminateWalk.java
index 5299b87..b2858c4 100644
--- a/test/java/nio/file/Files/TerminateWalk.java
+++ b/test/java/nio/file/Files/TerminateWalk.java
@@ -49,22 +49,19 @@
public static void main(String[] args) throws Exception {
Path dir = Paths.get(args[0]);
- Files.walkFileTree(dir, new FileVisitor<Path>() {
- public FileVisitResult preVisitDirectory(Path dir) {
+ Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
return maybeTerminate();
}
- public FileVisitResult preVisitDirectoryFailed(Path dir, IOException exc) {
- return maybeTerminate();
- }
+ @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
return maybeTerminate();
}
+ @Override
public FileVisitResult postVisitDirectory(Path dir, IOException x) {
return maybeTerminate();
}
- public FileVisitResult visitFileFailed(Path file, IOException x) {
- return maybeTerminate();
- }
});
}
}
diff --git a/test/java/nio/file/Files/WalkWithSecurity.java b/test/java/nio/file/Files/WalkWithSecurity.java
index f438619..9044398 100644
--- a/test/java/nio/file/Files/WalkWithSecurity.java
+++ b/test/java/nio/file/Files/WalkWithSecurity.java
@@ -116,7 +116,7 @@
}
@Override
- public FileVisitResult preVisitDirectory(Path dir) {
+ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
System.out.println(dir);
count++;
return FileVisitResult.CONTINUE;
diff --git a/test/java/nio/file/Files/walk_file_tree.sh b/test/java/nio/file/Files/walk_file_tree.sh
index 50d141f..4f41d3c 100644
--- a/test/java/nio/file/Files/walk_file_tree.sh
+++ b/test/java/nio/file/Files/walk_file_tree.sh
@@ -22,9 +22,9 @@
#
# @test
-# @bug 4313887
+# @bug 4313887 6907737
# @summary Unit test for walkFileTree method
-# @build CreateFileTree PrintFileTree SkipSiblings TerminateWalk
+# @build CreateFileTree PrintFileTree SkipSiblings TerminateWalk MaxDepth
# @run shell walk_file_tree.sh
# if TESTJAVA isn't set then we assume an interactive run.
@@ -84,6 +84,10 @@
$JAVA TerminateWalk "$ROOT"
if [ $? != 0 ]; then failures=`expr $failures + 1`; fi
+# test maxDepth
+$JAVA MaxDepth "$ROOT"
+if [ $? != 0 ]; then failures=`expr $failures + 1`; fi
+
# clean-up
rm -r "$ROOT"
diff --git a/test/java/nio/file/TestUtil.java b/test/java/nio/file/TestUtil.java
index 753a4cd..c6acccd 100644
--- a/test/java/nio/file/TestUtil.java
+++ b/test/java/nio/file/TestUtil.java
@@ -44,15 +44,10 @@
return createTemporaryDirectory(System.getProperty("java.io.tmpdir"));
}
- static void removeAll(Path dir) {
+ static void removeAll(Path dir) throws IOException {
Files.walkFileTree(dir, new FileVisitor<Path>() {
@Override
- public FileVisitResult preVisitDirectory(Path dir) {
- return FileVisitResult.CONTINUE;
- }
- @Override
- public FileVisitResult preVisitDirectoryFailed(Path dir, IOException exc) {
- System.err.format("Error occured accessing directory %s\n", dir, exc);
+ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
return FileVisitResult.CONTINUE;
}
@Override