Search in sources :

Example 6 with FileVersionComparison

use of org.syncany.database.FileVersionComparator.FileVersionComparison in project syncany by syncany.

the class TestAssertUtil method assertFileEquals.

public static void assertFileEquals(File expectedFile, File actualFile, FileChange... allowedChanges) throws ArrayComparisonFailure, Exception {
    if (expectedFile == null && actualFile == null) {
        return;
    }
    assertNotNull("Files are not equal: Actual file is " + actualFile + ", expected file is null.", expectedFile);
    assertNotNull("Files are not equal: Expected file is " + expectedFile + ", actual file is null.", actualFile);
    Path root = Paths.get(actualFile.getAbsolutePath()).getRoot();
    FileVersionComparator fileVersionComparator = new FileVersionComparator(root.toFile(), "SHA1");
    if (!FileUtil.exists(expectedFile)) {
        fail("Files are not equal: Expected file " + expectedFile + " does not exist.");
    }
    if (!FileUtil.exists(actualFile)) {
        fail("Files are not equal: Actual file " + actualFile + " does not exist.");
    }
    if (FileUtil.isSymlink(actualFile) && FileUtil.isSymlink(expectedFile)) {
        return;
    }
    if (actualFile.isDirectory() != expectedFile.isDirectory()) {
        fail("Files are not equal: Comparing a directory with a file (actual is dir = " + actualFile.isDirectory() + ", expected is dir = " + expectedFile.isDirectory() + ")");
    }
    if (actualFile.isDirectory() && expectedFile.isDirectory()) {
        return;
    }
    if (actualFile.length() != expectedFile.length()) {
        fail("Files are not equal: Actual file size (" + actualFile + " = " + actualFile.length() + ") does not match expected file size (" + expectedFile + " = " + expectedFile.length() + ")");
    }
    byte[] expectedFileChecksum = TestFileUtil.createChecksum(expectedFile);
    byte[] actualFileChecksum = TestFileUtil.createChecksum(actualFile);
    assertArrayEquals("Files are not equal: Actual file checksum (" + StringUtil.toHex(actualFileChecksum) + ") and expected file checksum (" + StringUtil.toHex(expectedFileChecksum) + ") do not match.", expectedFileChecksum, actualFileChecksum);
    FileProperties actualFileProperties = fileVersionComparator.captureFileProperties(actualFile, null, true);
    FileProperties expectedFileProperties = fileVersionComparator.captureFileProperties(expectedFile, null, true);
    FileVersionComparison fileVersionComparison = fileVersionComparator.compare(expectedFileProperties, actualFileProperties, true);
    List<FileChange> allowedChangesList = new ArrayList<FileChange>(Arrays.asList(allowedChanges));
    allowedChangesList.add(FileChange.CHANGED_PATH);
    if (!CollectionUtil.containsOnly(fileVersionComparison.getFileChanges(), allowedChangesList)) {
        fail("Files are not equal: Actual file differs from expected file: " + fileVersionComparison.getFileChanges());
    }
}
Also used : Path(java.nio.file.Path) FileProperties(org.syncany.database.FileVersionComparator.FileProperties) FileVersionComparison(org.syncany.database.FileVersionComparator.FileVersionComparison) ArrayList(java.util.ArrayList) FileChange(org.syncany.database.FileVersionComparator.FileChange) FileVersionComparator(org.syncany.database.FileVersionComparator)

Aggregations

FileVersionComparison (org.syncany.database.FileVersionComparator.FileVersionComparison)6 File (java.io.File)3 ChangeFileSystemAction (org.syncany.operations.down.actions.ChangeFileSystemAction)3 DeleteFileSystemAction (org.syncany.operations.down.actions.DeleteFileSystemAction)3 FileSystemAction (org.syncany.operations.down.actions.FileSystemAction)3 NewFileSystemAction (org.syncany.operations.down.actions.NewFileSystemAction)3 NewSymlinkFileSystemAction (org.syncany.operations.down.actions.NewSymlinkFileSystemAction)3 RenameFileSystemAction (org.syncany.operations.down.actions.RenameFileSystemAction)3 SetAttributesFileSystemAction (org.syncany.operations.down.actions.SetAttributesFileSystemAction)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 FileVersion (org.syncany.database.FileVersion)2 FileVersionComparator (org.syncany.database.FileVersionComparator)2 Date (java.util.Date)1 Test (org.junit.Test)1 Config (org.syncany.config.Config)1 FileChecksum (org.syncany.database.FileContent.FileChecksum)1 FileChange (org.syncany.database.FileVersionComparator.FileChange)1 FileProperties (org.syncany.database.FileVersionComparator.FileProperties)1 PartialFileHistory (org.syncany.database.PartialFileHistory)1