private void visitPath( Path transactionalLogsPath, Predicate<Path> exclude, ArchiveOutputStream stream ) throws IOException { Files.walkFileTree( transactionalLogsPath, onlyMatching( not( exclude ), throwExceptions( onDirectory( dir -> dumpDirectory( transactionalLogsPath, stream, dir ), onFile( file -> dumpFile( transactionalLogsPath, stream, file ), justContinue() ) ) ) ) ); }
@Parameterized.Parameters( name = "{0}" ) public static List<Object[]> formats() { return asList( new Object[]{"decorator", (Function<FileVisitor<Path>, FileVisitor<Path>>) FileVisitors.Decorator::new, false }, new Object[]{"onFile", (Function<FileVisitor<Path>, FileVisitor<Path>>) wrapped -> FileVisitors.onFile( noop(), wrapped ), false }, new Object[]{"onDirectory", (Function<FileVisitor<Path>, FileVisitor<Path>>) wrapped -> FileVisitors.onDirectory( noop(), wrapped ), false }, new Object[]{"throwExceptions", (Function<FileVisitor<Path>, FileVisitor<Path>>) FileVisitors::throwExceptions, true }, new Object[]{"onlyMatching", (Function<FileVisitor<Path>, FileVisitor<Path>>) wrapped -> FileVisitors.onlyMatching( Predicates.alwaysTrue(), wrapped ), false } ); }
@Test public void shouldNotDelegateVisitFileFailedIfPredicateDoesntMatch() throws IOException { onlyMatching( alwaysFalse(), wrapped).visitFileFailed( null, null ); verify( wrapped, never() ).visitFileFailed( any(), any() ); }
@Test public void shouldNotOperateOnFiles() throws IOException { Path file = Paths.get( "/some/path" ); onDirectory( operation, wrapped ).visitFile( file, null ); verify( operation, never() ).accept( file ); } }
@Test public void shouldNotOperateOnDirectories() throws IOException { Path dir = Paths.get( "/some/path" ); onFile( operation, wrapped ).preVisitDirectory( dir, null ); verify( operation, never() ).accept( dir ); }
@Test public void shouldThrowExceptionFromPostVisitDirectory() { IOException exception = new IOException(); try { throwExceptions( wrapped ).postVisitDirectory( null, exception ); fail( "Expected exception" ); } catch ( Exception e ) { assertThat( e, is( exception ) ); } } }
@Test void shouldJustContinue() throws IOException { assertThat( FileVisitors.justContinue().preVisitDirectory( null, null ), is( FileVisitResult.CONTINUE ) ); assertThat( FileVisitors.justContinue().visitFile( null, null ), is( FileVisitResult.CONTINUE ) ); assertThat( FileVisitors.justContinue().visitFileFailed( null, null ), is( FileVisitResult.CONTINUE ) ); assertThat( FileVisitors.justContinue().postVisitDirectory( null, null ), is( FileVisitResult.CONTINUE ) ); } }
@Test public void shouldNotDelegatePostVisitDirectoryIfPredicateDoesntMatch() throws IOException { onlyMatching( alwaysFalse(), wrapped).postVisitDirectory( null, null ); verify( wrapped, never() ).postVisitDirectory( any(), any() ); }
@Test public void shouldOperateOnDirectories() throws IOException { Path dir = Paths.get( "/some/path" ); onDirectory( operation, wrapped ).preVisitDirectory( dir, null ); verify( operation ).accept( dir ); }
@Test public void shouldOperateOnFiles() throws IOException { Path file = Paths.get( "/some/path" ); onFile( operation, wrapped ).visitFile( file, null ); verify( operation ).accept( file ); } }
@Test public void shouldThrowExceptionFromVisitFileFailed() { IOException exception = new IOException(); try { throwExceptions( wrapped ).visitFileFailed( null, exception ); fail( "Expected exception" ); } catch ( Exception e ) { assertThat( e, is( exception ) ); } }
private void visitPath( Path transactionalLogsPath, Predicate<Path> exclude, ArchiveOutputStream stream ) throws IOException { Files.walkFileTree( transactionalLogsPath, onlyMatching( not( exclude ), throwExceptions( onDirectory( dir -> dumpDirectory( transactionalLogsPath, stream, dir ), onFile( file -> dumpFile( transactionalLogsPath, stream, file ), justContinue() ) ) ) ) ); }
@Test public void shouldNotDelegatePreVisitDirectoryIfPredicateDoesntMatch() throws IOException { onlyMatching( alwaysFalse(), wrapped).preVisitDirectory( null, null ); verify( wrapped, never() ).preVisitDirectory( any(), any() ); }
@Test public void shouldNotDelegateVisitFileIfPredicateDoesntMatch() throws IOException { onlyMatching( alwaysFalse(), wrapped).visitFile( null, null ); verify( wrapped, never() ).visitFile( any(), any() ); }
@Test public void shouldContinueAfterPostVisitDirectoryIfPredicateDoesntMatch() throws IOException { assertThat( onlyMatching( alwaysFalse(), wrapped).postVisitDirectory( null, null ), is( FileVisitResult.CONTINUE)); }
@Test public void shouldContinueAfterVisitFileIfPredicateDoesntMatch() throws IOException { assertThat( onlyMatching( alwaysFalse(), wrapped).visitFile( null, null ), is( FileVisitResult.CONTINUE)); }
@Test public void shouldNotSkipSubtreeFromPreVisitDirectoryIfPredicateDoesntMatch() throws IOException { assertThat( onlyMatching( alwaysFalse(), wrapped).preVisitDirectory( null, null ), is( FileVisitResult.SKIP_SUBTREE)); }
@Test public void shouldContinueAfterVisitFileFailedIfPredicateDoesntMatch() throws IOException { assertThat( onlyMatching( alwaysFalse(), wrapped).visitFileFailed( null, null ), is( FileVisitResult.CONTINUE)); } }