### Question:
Directory implements Serializable { public void copyTo(final Directory target, final boolean incremental) throws IOException { if (!getFile().exists()) { throw new DirectoryException( "Source directory '" + getFile() + "' does not exist"); } target.create(); if (!incremental) { target.deleteContents(); } final File[] files = listContents(s_matchAllFilesFilter, true, false); final StreamCopier streamCopier = new StreamCopier(4096, false); for (final File file : files) { final File source = getFile(file); final File destination = target.getFile(file); if (source.isDirectory()) { destination.mkdirs(); } else { if (!incremental || !destination.exists() || source.lastModified() > destination.lastModified()) { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(source); out = new FileOutputStream(destination); streamCopier.copy(in, out); } finally { Closer.close(in); Closer.close(out); } } } } } Directory();  Directory(final File directory); static FileFilter getMatchAllFilesFilter(); void create(); File getFile(); File getFile(final File child); File[] listContents(final FileFilter filter); File[] listContents(final FileFilter filter,
                             final boolean includeDirectories,
                             final boolean absolutePaths); void deleteContents(); void delete(); File rebaseFromCWD(final File file); File relativeFile(final File file, final boolean mustBeChild); List<File> rebasePath(final String path); boolean isParentOf(final File file); void copyTo(final Directory target, final boolean incremental); String[] getWarnings(); @Override int hashCode(); @Override boolean equals(final Object o);  }### Answer:
@Test public void testCopyTo() throws Exception { final Set<File> files = new HashSet<File>() {{ add(new File("a file")); add(new File("directory/.afile")); add(new File("directory/b/c/d/e")); }}; for (final File relativeFile : files) { final File absoluteFile = new File(getDirectory(), relativeFile.getPath()); createRandomFile(absoluteFile); } final Directory sourceDirectory = new Directory(getDirectory()); final File output = new File(getDirectory(), "output"); final Directory outputDirectory = new Directory(output); outputDirectory.create(); final File overwritten = new File(output, "should be deleted"); createRandomFile(overwritten); assertTrue(overwritten.exists()); sourceDirectory.copyTo(outputDirectory, false); assertFalse(overwritten.exists()); final File[] contents = outputDirectory.listContents(Directory.getMatchAllFilesFilter()); for (final File content : contents) { assertTrue("Original contains '" + content + "'", files.contains(content)); } assertEquals(files.size(), contents.length); sourceDirectory.copyTo(outputDirectory, true); final File[] contents2 = outputDirectory.listContents(Directory.getMatchAllFilesFilter()); for (int i = 0; i < contents2.length; ++i) { if (!contents2[i].getPath().startsWith("output")) { assertTrue("Original contains '" + contents2[i] + "'", files.contains(contents2[i])); } } final File[] contents3 = new Directory(new File("output/output")) .listContents(Directory.getMatchAllFilesFilter()); for (final File element : contents3) { assertTrue("Original contains '" + element + "'", files.contains(element)); } assertEquals(files.size() * 2, contents2.length); final Directory missingSourceDirectory = new Directory(sourceDirectory.getFile(new File("missing"))); final Directory missingOutputDirectory = new Directory(outputDirectory.getFile(new File("notthere"))); assertFalse(missingSourceDirectory.getFile().exists()); assertFalse(missingOutputDirectory.getFile().exists()); try { missingSourceDirectory.copyTo(missingOutputDirectory, false); fail("Expected DirectoryException"); } catch (final Directory.DirectoryException e) { } assertFalse(missingSourceDirectory.getFile().exists()); assertFalse(missingOutputDirectory.getFile().exists()); }