CopyContentIfNotExistsVisitor extends SimpleFileVisitor<Path> { @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { Path target = targetDirectory.resolve(srcDirectory.relativize(file)); if (!exists(target)) { copy(file, target); } return FileVisitResult.CONTINUE; } CopyContentIfNotExistsVisitor(Path srcDirectory, Path targetDirectory); @Override FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs); @Override FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs);  }
@Test public void should_copy_given_file_from_source_directory() throws Exception { write(source.toPath().resolve("pbButton.json"), "contents".getBytes()); visitor.visitFile(source.toPath().resolve("pbButton.json"), null); assertThat(readAllBytes(destination.toPath().resolve("pbButton.json"))).isEqualTo("contents".getBytes()); }
@Test public void should_not_copy_given_file_from_source_directory_if_it_already_exist() throws Exception { write(source.toPath().resolve("pbButton.json"), "contents from source".getBytes()); write(destination.toPath().resolve("pbButton.json"), "contents from destination".getBytes()); visitor.visitFile(source.toPath().resolve("pbButton.json"), null); assertThat(readAllBytes(destination.toPath().resolve("pbButton.json"))).isEqualTo("contents from destination".getBytes()); }