use of org.syncany.database.FileVersionComparator.FileProperties in project syncany by syncany.
the class FileVersionComparatorTest method testCaptureFilePropertiesFromFile.
@Test
public void testCaptureFilePropertiesFromFile() throws Exception {
// Setup
Config config = TestConfigUtil.createTestLocalConfig();
FileVersionComparator versionComparator = new FileVersionComparator(config.getLocalDir(), config.getChunker().getChecksumAlgorithm());
// Create file
File somefile = new File(config.getLocalDir() + "/somefile");
Path somefilePath = Paths.get(somefile.getAbsolutePath());
TestFileUtil.createRandomFile(somefile, 100 * 1024);
somefile.setLastModified(1382196000);
if (EnvironmentUtil.isWindows()) {
Files.setAttribute(somefilePath, "dos:archive", false);
Files.setAttribute(somefilePath, "dos:hidden", false);
Files.setAttribute(somefilePath, "dos:readonly", false);
Files.setAttribute(somefilePath, "dos:system", false);
} else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
Files.setPosixFilePermissions(somefilePath, PosixFilePermissions.fromString("rw-rw-r-x"));
}
// Run
FileProperties fileProperties = versionComparator.captureFileProperties(somefile, null, true);
// Test
assertNotNull(fileProperties.getChecksum());
assertEquals(1382196000, fileProperties.getLastModified());
assertEquals("somefile", fileProperties.getRelativePath());
assertEquals(100 * 1024, fileProperties.getSize());
assertNull(fileProperties.getLinkTarget());
assertTrue(fileProperties.exists());
assertEquals(FileType.FILE, fileProperties.getType());
if (EnvironmentUtil.isWindows()) {
DosFileAttributes dosFileAttributes = FileUtil.dosAttrsFromString(fileProperties.getDosAttributes());
assertFalse(dosFileAttributes.isArchive());
assertFalse(dosFileAttributes.isHidden());
assertFalse(dosFileAttributes.isReadOnly());
assertFalse(dosFileAttributes.isSystem());
assertNull(fileProperties.getPosixPermissions());
} else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
assertEquals("rw-rw-r-x", fileProperties.getPosixPermissions());
assertNull(fileProperties.getDosAttributes());
}
// Tear down
TestConfigUtil.deleteTestLocalConfigAndData(config);
}
use of org.syncany.database.FileVersionComparator.FileProperties in project syncany by syncany.
the class FileVersionComparatorTest method testCaptureFilePropertiesFromFileVersion.
@Test
public void testCaptureFilePropertiesFromFileVersion() throws Exception {
// Setup
Config config = TestConfigUtil.createTestLocalConfig();
FileVersionComparator versionComparator = new FileVersionComparator(config.getLocalDir(), config.getChunker().getChecksumAlgorithm());
FileVersion fileVersion = new FileVersion();
fileVersion.setVersion(3L);
fileVersion.setChecksum(new FileChecksum(new byte[] { 0x11, 0x22, 0x33 }));
fileVersion.setLastModified(new Date(123456789));
fileVersion.setPath("folder/file");
fileVersion.setSize(999 * 1024L);
fileVersion.setLinkTarget(null);
fileVersion.setStatus(FileStatus.CHANGED);
fileVersion.setType(FileType.FILE);
if (EnvironmentUtil.isWindows()) {
fileVersion.setDosAttributes("rha-");
} else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
fileVersion.setPosixPermissions("rw-rw-r-x");
}
// Run
FileProperties fileProperties = versionComparator.captureFileProperties(fileVersion);
// Test
assertEquals(new FileChecksum(new byte[] { 0x11, 0x22, 0x33 }), fileProperties.getChecksum());
assertEquals(123456789, fileProperties.getLastModified());
assertEquals("folder/file", fileProperties.getRelativePath());
assertEquals(999 * 1024, fileProperties.getSize());
assertNull(fileProperties.getLinkTarget());
assertTrue(fileProperties.exists());
assertEquals(FileType.FILE, fileProperties.getType());
if (EnvironmentUtil.isWindows()) {
DosFileAttributes dosFileAttributes = FileUtil.dosAttrsFromString(fileProperties.getDosAttributes());
assertTrue(dosFileAttributes.isArchive());
assertTrue(dosFileAttributes.isHidden());
assertTrue(dosFileAttributes.isReadOnly());
assertFalse(dosFileAttributes.isSystem());
assertNull(fileProperties.getPosixPermissions());
} else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
assertEquals("rw-rw-r-x", fileProperties.getPosixPermissions());
assertNull(fileProperties.getDosAttributes());
}
// Tear down
TestConfigUtil.deleteTestLocalConfigAndData(config);
}
use of org.syncany.database.FileVersionComparator.FileProperties 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());
}
}
Aggregations