Is there a matcher for recursive directory comparisons?

I write unit tests for IM and file export. I need to test the resulting byte by byte. I myself have performed the routine for flat directories and I know how to do this recursively. But I do not want to reinvent the wheel.

So, is there something like the following examples?

Matchers.matches(Path actual, equalsRecursive(Path value)); 

or

 FileAssertions.equalsRecursive(Path actual, Path value); 
+7
java junit recursion hamcrest
source share
2 answers

I do not know such matches. So IMO you have to do it yourself. The 2 options I could think of are as follows:

  • Use Apache Commons FileUtils to

    1.1. make sure that each file / subdirectory (recursive call goes here) exists inside the current directory under test. For each subdir, we get a collection of files, iterate and use the directoryContains method for the corresponding subdir.

    1.2 ensure that the contents of the two corresponding files are equal using the contentEquals method. I'm not sure what will happen if you pass 2 directories to this method.

  • The second option: if you run your tests on a Linux box, you can run the Linux command from Java using the Runtime.exec() docs here . The only command you need to execute is diff -r <directory1> <directory2>

Good luck

+2
source share

I did not find anything. Therefore, I programmed it myself. It is not very complicated and slow for large files, but it seems to work.

 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()); } } } 
0
source share

All Articles