FileTreeWalker { void walk(final Path start) throws IOException { checkNotNull("start", start); walk(start, 0); } FileTreeWalker(final FileVisitor<? super Path> visitor,
                   final int maxDepth);   }
@Test public void testWalker() { final FileTreeWalker walker = new FileTreeWalker(simple, 1); walker.walk(Paths.get("/some/path")); assertThat(preDir.get()).isEqualTo(0); assertThat(postDir.get()).isEqualTo(0); assertThat(fileC.get()).isEqualTo(0); assertThat(failFile.get()).isEqualTo(1); final Path dir = newTempDir(null); final Path file1 = Files.createTempFile(dir, "foo", "bar"); final Path file2 = Files.createTempFile(dir, "foo", "bar"); cleanupVisitor(); walker.walk(dir); assertThat(preDir.get()).isEqualTo(1); assertThat(postDir.get()).isEqualTo(1); assertThat(fileC.get()).isEqualTo(2); assertThat(failFile.get()).isEqualTo(0); cleanupVisitor(); walker.walk(file1); assertThat(preDir.get()).isEqualTo(0); assertThat(postDir.get()).isEqualTo(0); assertThat(fileC.get()).isEqualTo(1); assertThat(failFile.get()).isEqualTo(0); }
@Test public void testWalkerDeep2() { final FileTreeWalker walker = new FileTreeWalker(simple, 2); final Path dir = newTempDir(null); final Path subDir = newTempDir(dir); final Path subSubDir = newTempDir(subDir); final Path subSubSubDir = newTempDir(subSubDir); cleanupVisitor(); walker.walk(dir); assertThat(preDir.get()).isEqualTo(2); assertThat(postDir.get()).isEqualTo(2); assertThat(fileC.get()).isEqualTo(1); assertThat(failFile.get()).isEqualTo(0); }
@Test public void testWalkerDeep1() { final FileTreeWalker walker = new FileTreeWalker(simple, 1); final Path dir = newTempDir(null); final Path subDir = newTempDir(dir); final Path subSubDir = newTempDir(subDir); final Path subSubSubDir = newTempDir(subSubDir); Files.createTempFile(dir, "foo", "bar"); Files.createTempFile(dir, "foo", "bar"); cleanupVisitor(); walker.walk(dir); assertThat(preDir.get()).isEqualTo(1); assertThat(postDir.get()).isEqualTo(1); assertThat(fileC.get()).isEqualTo(3); assertThat(failFile.get()).isEqualTo(0); }
@Test public void testWalkerDeep2ButTerminateOnDir() { final FileVisitor<Path> terminateOnDir = new FileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { preDir.addAndGet(1); if (preDir.get() > 1) { return FileVisitResult.TERMINATE; } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { fileC.addAndGet(1); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { failFile.addAndGet(1); return FileVisitResult.TERMINATE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { postDir.addAndGet(1); return FileVisitResult.CONTINUE; } }; final FileTreeWalker walker = new FileTreeWalker(terminateOnDir, 2); final Path dir = newTempDir(null); final Path subDir = newTempDir(dir); final Path subSubDir = newTempDir(subDir); final Path subSubSubDir = newTempDir(subSubDir); Files.createTempFile(dir, "foo", "bar"); Files.createTempFile(dir, "foo", "bar"); cleanupVisitor(); walker.walk(dir); assertThat(preDir.get()).isEqualTo(2); assertThat(postDir.get()).isEqualTo(0); assertThat(fileC.get()).isEqualTo(2); assertThat(failFile.get()).isEqualTo(0); }
@Test public void testWalkerDeep2ButSkipSibling() { final FileVisitor<Path> terminateOnDir = new FileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { preDir.addAndGet(1); if (preDir.get() > 1) { return FileVisitResult.SKIP_SIBLINGS; } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { fileC.addAndGet(1); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { failFile.addAndGet(1); return FileVisitResult.TERMINATE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { postDir.addAndGet(1); return FileVisitResult.CONTINUE; } }; final FileTreeWalker walker = new FileTreeWalker(terminateOnDir, 2); final Path dir = newTempDir(null); final Path subDir1 = newTempDir(dir); final Path subDir2 = newTempDir(dir); final Path subDir3 = newTempDir(dir); final Path subDir4 = newTempDir(dir); Files.createTempFile(dir, "foo", "bar"); Files.createTempFile(dir, "foo", "bar"); cleanupVisitor(); walker.walk(dir); assertThat(preDir.get()).isEqualTo(2); assertThat(postDir.get()).isEqualTo(1); assertThat(fileC.get()).isEqualTo(2); assertThat(failFile.get()).isEqualTo(0); }
@Test public void testWalkerDeep2ButThrowExceptionOnSibling() { final FileVisitor<Path> terminateOnDir = new FileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { preDir.addAndGet(1); if (preDir.get() > 1) { throw new IOException(); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { fileC.addAndGet(1); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { failFile.addAndGet(1); return FileVisitResult.TERMINATE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { postDir.addAndGet(1); if (exc == null) { throw new RuntimeException(); } return FileVisitResult.CONTINUE; } }; final FileTreeWalker walker = new FileTreeWalker(terminateOnDir, 2); final Path dir = newTempDir(null); final Path subDir1 = newTempDir(dir); final Path subDir2 = newTempDir(dir); final Path subDir3 = newTempDir(dir); final Path subDir4 = newTempDir(dir); Files.createTempFile(dir, "foo", "bar"); Files.createTempFile(dir, "foo", "bar"); cleanupVisitor(); walker.walk(dir); assertThat(preDir.get()).isEqualTo(2); assertThat(postDir.get()).isEqualTo(1); assertThat(fileC.get()).isEqualTo(2); assertThat(failFile.get()).isEqualTo(0); }
@Test public void testWalkerDeep2ButReturnNull() { final FileVisitor<Path> terminateOnDir = new FileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { preDir.addAndGet(1); if (preDir.get() > 1) { return null; } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { fileC.addAndGet(1); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { failFile.addAndGet(1); return null; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { postDir.addAndGet(1); if (exc == null) { throw new RuntimeException(); } return FileVisitResult.CONTINUE; } }; final FileTreeWalker walker = new FileTreeWalker(terminateOnDir, 2); final Path dir = newTempDir(null); final Path subDir1 = newTempDir(dir); final Path subDir2 = newTempDir(dir); final Path subDir3 = newTempDir(dir); final Path subDir4 = newTempDir(dir); Files.createTempFile(dir, "foo", "bar"); Files.createTempFile(dir, "foo", "bar"); cleanupVisitor(); walker.walk(dir); assertThat(preDir.get()).isEqualTo(2); assertThat(postDir.get()).isEqualTo(0); assertThat(fileC.get()).isEqualTo(2); assertThat(failFile.get()).isEqualTo(0); }