Non trovato nulla. Così l'ho programmato da solo. Non è molto sofisticato e lento per file di grandi dimensioni, ma sembra funzionare.
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import org.junit.Assert;
/**
* Assertion for recursively testing directories.
*
* @author andreas
*/
public class AssertFile {
private AssertFile() {
throw new RuntimeException("This class should not be instantiated");
}
/**
* Asserts that two directories are recursively equal. If they are not, an {@link AssertionError} is thrown with the
* given message.<br/>
* There will be a binary comparison of all files under expected with all files under actual. File attributes will
* not be considered.<br/>
* Missing or additional files are considered an error.<br/>
*
* @param expected
* Path expected directory
* @param actual
* Path actual directory
*/
public static final void assertPathEqualsRecursively(final Path expected, final Path actual) {
Assert.assertNotNull(expected);
Assert.assertNotNull(actual);
final Path absoluteExpected = expected.toAbsolutePath();
final Path absoluteActual = actual.toAbsolutePath();
try {
Files.walkFileTree(expected, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path expectedDir, BasicFileAttributes attrs)
throws IOException {
Path relativeExpectedDir = absoluteExpected.relativize(expectedDir.toAbsolutePath());
Path actualDir = absoluteActual.resolve(relativeExpectedDir);
if (!Files.exists(actualDir)) {
Assert.fail(String.format("Directory \'%s\' missing in target.", expectedDir.getFileName()));
}
Assert.assertEquals(String.format("Directory size of \'%s\' differ. ", relativeExpectedDir),
expectedDir.toFile().list().length, actualDir.toFile().list().length);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path expectedFile, BasicFileAttributes attrs) throws IOException {
Path relativeExpectedFile = absoluteExpected.relativize(expectedFile.toAbsolutePath());
Path actualFile = absoluteActual.resolve(relativeExpectedFile);
if (!Files.exists(actualFile)) {
Assert.fail(String.format("File \'%s\' missing in target.", expectedFile.getFileName()));
}
Assert.assertEquals(String.format("File size of \'%s\' differ. ", relativeExpectedFile),
Files.size(expectedFile), Files.size(actualFile));
Assert.assertArrayEquals(String.format("File content of \'%s\' differ. ", relativeExpectedFile),
Files.readAllBytes(expectedFile), Files.readAllBytes(actualFile));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
Assert.fail(exc.getMessage());
return FileVisitResult.TERMINATE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
Assert.fail(e.getMessage());
}
}
}
Thx per i suggerimenti! Fortunatamente potrei usare Java7 e posso usare Files.walkFileTree(). Per il confronto binario potrei usare Files.readAllBytes (Path path) e confrontare con Assert.assertArrayEquals(). Non ho intenzione di ricorrere a routine specifiche. – Andreas
sarebbe interessante per vedere se c'è un modo migliore/lib out-of-the-box per farlo. Si prega di aggiornare se trovate qualcosa! – aviad