Skip to content

Instantly share code, notes, and snippets.

@americanstone
Created February 19, 2018 14:56
Show Gist options
  • Save americanstone/f8f77cb92c7ddc0d1e7b44d59b076e55 to your computer and use it in GitHub Desktop.
Save americanstone/f8f77cb92c7ddc0d1e7b44d59b076e55 to your computer and use it in GitHub Desktop.
recursivelyCompareDirectories
/**
* Recursively compare the contents of the directory of downloaded
* files with the contents of the "ground truth" directory.
*/
public static void recursivelyCompareDirectories(File expected,
File generated)
throws IOException {
// Checks parameters.
assertTrue("Generated Folder doesn't exist: " + generated,
generated.exists());
assertTrue("Generated is not a folder?!?!: " + generated,
generated.isDirectory());
assertTrue("Expected Folder doesn't exist: " + expected,
expected.exists());
assertTrue("Expected is not a folder?!?!: " + expected,
expected.isDirectory());
Files.walkFileTree(expected.toPath(),
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs)
throws IOException {
FileVisitResult result =
super.preVisitDirectory(dir, attrs);
// Get the relative file name from
// path "expected"
Path relativize =
expected.toPath().relativize(dir);
// Construct the path for the
// counterpart file in "generated"
File otherDir =
generated.toPath().resolve(relativize).toFile();
assertEquals("Folders doesn't contain same file!?!?",
Arrays.toString(dir.toFile().list()),
Arrays.toString(otherDir.list()));
return result;
}
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs)
throws IOException {
FileVisitResult result =
super.visitFile(file, attrs);
// Get the relative file name from
// path "expected".
Path relativize =
expected.toPath().relativize(file);
// Construct the path for the
// counterpart file in "generated".
Path fileInOther =
generated.toPath().resolve(relativize);
byte[] otherBytes = Files.readAllBytes(fileInOther);
byte[] thisBytes = Files.readAllBytes(file);
if (!Arrays.equals(otherBytes, thisBytes))
throw new AssertionFailedError(file + " is not equal to " + fileInOther);
return result;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment