use of org.syncany.operations.ChangeSet in project syncany by syncany.
the class ChangedAttributesScenarioTest method testChangeAttributes.
@Test
public void testChangeAttributes() throws Exception {
// Setup
TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
TestClient clientA = new TestClient("A", testConnection);
TestClient clientB = new TestClient("B", testConnection);
// Run
clientA.createNewFile("file1.jpg");
clientA.upWithForceChecksum();
clientB.down();
File bFile = clientB.getLocalFile("file1.jpg");
Path bFilePath = Paths.get(bFile.getAbsolutePath());
if (EnvironmentUtil.isWindows()) {
Files.setAttribute(bFilePath, "dos:readonly", true);
} else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
Files.setPosixFilePermissions(bFilePath, PosixFilePermissions.fromString("rwxrwxrwx"));
}
StatusOperationResult statusResult = clientB.status();
assertNotNull(statusResult);
ChangeSet changes = statusResult.getChangeSet();
assertTrue("Status-Operation should return changes.", changes.hasChanges());
UpOperationResult upResult = clientB.up();
StatusOperationResult statusResultFromUp = upResult.getStatusResult();
// Test 1: Check result sets for inconsistencies
assertTrue("Status should return changes.", statusResultFromUp.getChangeSet().hasChanges());
assertTrue("File should be uploaded.", upResult.getChangeSet().hasChanges());
// Test 2: Check database for inconsistencies
SqlDatabase database = clientB.loadLocalDatabase();
assertEquals("File should be uploaded.", 1, database.getFileList("file1.jpg", null, false, false, false, null).size());
assertEquals("There should be a new database version, because file should not have been added.", 2, database.getLocalDatabaseBranch().size());
// B down
clientA.down();
// Test 1: file1.jpg permissions
File aFile = clientA.getLocalFile("file1.jpg");
Path aFilePath = Paths.get(aFile.getAbsolutePath());
if (EnvironmentUtil.isWindows()) {
Object readOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly");
assertTrue("Read-only should be true.", (Boolean) readOnlyAttribute);
} else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(aFilePath);
assertEquals("Should be rwxrwxrwx.", "rwxrwxrwx", PosixFilePermissions.toString(posixFilePermissions));
}
// Test 2: The rest
assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead());
assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile());
// Tear down
clientA.deleteTestData();
clientB.deleteTestData();
}
use of org.syncany.operations.ChangeSet in project syncany by syncany.
the class LogOperation method createLightweightDatabaseVersion.
private LightweightDatabaseVersion createLightweightDatabaseVersion(DatabaseVersion databaseVersion) {
// Create changeset
ChangeSet changedFiles = new ChangeSet();
for (PartialFileHistory fileHistory : databaseVersion.getFileHistories()) {
FileVersion fileVersion = fileHistory.getLastVersion();
switch(fileVersion.getStatus()) {
case NEW:
changedFiles.getNewFiles().add(fileVersion.getPath());
break;
case CHANGED:
case RENAMED:
changedFiles.getChangedFiles().add(fileVersion.getPath());
break;
case DELETED:
changedFiles.getDeletedFiles().add(fileVersion.getPath());
break;
}
}
// Create lightweight database version
LightweightDatabaseVersion lightweightDatabaseVersion = new LightweightDatabaseVersion();
lightweightDatabaseVersion.setClient(databaseVersion.getHeader().getClient());
lightweightDatabaseVersion.setDate(databaseVersion.getHeader().getDate());
lightweightDatabaseVersion.setChangeSet(changedFiles);
return lightweightDatabaseVersion;
}
use of org.syncany.operations.ChangeSet in project syncany by syncany.
the class StatusOperationTest method testVeryRecentFileModificationWithoutSizeOrModifiedDateChange.
@Test
public void testVeryRecentFileModificationWithoutSizeOrModifiedDateChange() throws Exception {
// Setup
Config config = TestConfigUtil.createTestLocalConfig();
File testFile = TestFileUtil.createRandomFileInDirectory(config.getLocalDir(), 40);
StatusOperationOptions statusOptions = new StatusOperationOptions();
statusOptions.setForceChecksum(true);
UpOperationOptions syncUpOptions = new UpOperationOptions();
syncUpOptions.setStatusOptions(statusOptions);
// Perform 'up' and immediately change test file
// IMPORTANT: Do NOT sleep to enforce checksum-based comparison in 'status'
new UpOperation(config, syncUpOptions).execute();
TestFileUtil.changeRandomPartOfBinaryFile(testFile);
// Run 'status', this should run a checksum-based file comparison
ChangeSet changeSet = (new StatusOperation(config, statusOptions).execute()).getChangeSet();
assertEquals(changeSet.getChangedFiles().size(), 1);
// Cleanup
TestConfigUtil.deleteTestLocalConfigAndData(config);
}
use of org.syncany.operations.ChangeSet in project syncany by syncany.
the class StatusOperationTest method testStatusOperation.
@Test
public void testStatusOperation() throws Exception {
// Setup
Config config = TestConfigUtil.createTestLocalConfig();
// Add new files
List<File> originalFiles = TestFileUtil.createRandomFilesInDirectory(config.getLocalDir(), 500 * 1024, 3);
// Status
ChangeSet changeSet = (new StatusOperation(config).execute()).getChangeSet();
assertEquals(changeSet.getNewFiles().size(), originalFiles.size());
assertEquals(changeSet.getChangedFiles().size(), 0);
assertEquals(changeSet.getDeletedFiles().size(), 0);
// Up
new UpOperation(config).execute();
// Status
changeSet = (new StatusOperation(config).execute()).getChangeSet();
assertEquals(changeSet.getNewFiles().size(), 0);
assertEquals(changeSet.getChangedFiles().size(), 0);
assertEquals(changeSet.getDeletedFiles().size(), 0);
// Change all files, run 'status'
// TODO [low] StatusOperation relies on file modified time and size, any other methods?
Thread.sleep(2000);
for (File file : originalFiles) {
TestFileUtil.changeRandomPartOfBinaryFile(file);
}
changeSet = (new StatusOperation(config).execute()).getChangeSet();
assertEquals(changeSet.getNewFiles().size(), 0);
assertEquals(changeSet.getChangedFiles().size(), originalFiles.size());
assertEquals(changeSet.getDeletedFiles().size(), 0);
// Up
new UpOperation(config).execute();
// Delete all files, run 'status' again
for (File file : originalFiles) {
TestFileUtil.deleteFile(file);
}
changeSet = (new StatusOperation(config).execute()).getChangeSet();
assertEquals(changeSet.getNewFiles().size(), 0);
assertEquals(changeSet.getChangedFiles().size(), 0);
assertEquals(changeSet.getDeletedFiles().size(), originalFiles.size());
// Cleanup
TestConfigUtil.deleteTestLocalConfigAndData(config);
}
use of org.syncany.operations.ChangeSet in project syncany by syncany.
the class DirtyDatabaseScenarioTest method testDirtyCleanupDirty.
@Test
public void testDirtyCleanupDirty() throws Exception {
// Setup
TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
TestClient clientA = new TestClient("A", testConnection);
TestClient clientB = new TestClient("B", testConnection);
TestClient clientC = new TestClient("C", testConnection);
TestClient clientD = new TestClient("D", testConnection);
StatusOperationOptions statusOptions = new StatusOperationOptions();
statusOptions.setForceChecksum(true);
UpOperationOptions upOptionsForceEnabled = new UpOperationOptions();
upOptionsForceEnabled.setStatusOptions(statusOptions);
upOptionsForceEnabled.setForceUploadEnabled(true);
CleanupOperationOptions cleanupOptions = new CleanupOperationOptions();
cleanupOptions.setMinSecondsBetweenCleanups(0);
cleanupOptions.setForce(true);
// Run
// // 1. CREATE FIRST DIRTY VERSION
clientA.createNewFile("A-file1.jpg", 50 * 1024);
// (A1)
clientA.up(upOptionsForceEnabled);
clientB.down();
clientB.changeFile("A-file1.jpg");
// (A1,B1)
clientB.up(upOptionsForceEnabled);
clientA.down();
// conflict (winner)
clientA.changeFile("A-file1.jpg");
// (A2,B1)
clientA.up(upOptionsForceEnabled);
// conflict (loser)
clientB.changeFile("A-file1.jpg");
clientB.up(upOptionsForceEnabled);
// don't care about the conflict, just continue
clientA.createNewFolder("new folder at A");
// (A3,B1)
clientA.up(upOptionsForceEnabled);
// don't care about the conflict, just continue
clientB.createNewFolder("new folder at B");
clientB.up(upOptionsForceEnabled);
// resolve conflict (wins, no DIRTY)
clientA.down();
java.sql.Connection databaseConnectionA = DatabaseConnectionFactory.createConnection(clientA.getDatabaseFile(), false);
assertEquals("0", TestSqlUtil.runSqlSelect("select count(*) from databaseversion where status='DIRTY'", databaseConnectionA));
// resolve conflict (loses, creates DIRTY version)
clientB.down();
java.sql.Connection databaseConnectionB = DatabaseConnectionFactory.createConnection(clientB.getDatabaseFile(), false);
assertEquals("2", TestSqlUtil.runSqlSelect("select count(*) from databaseversion where status='DIRTY'", databaseConnectionB));
assertEquals("(A1,B2)\n(A1,B3)", TestSqlUtil.runSqlSelect("select vectorclock_serialized from databaseversion where status='DIRTY' order by id", databaseConnectionB));
assertEquals("(A1)\n(A1,B1)\n(A2,B1)\n(A3,B1)", TestSqlUtil.runSqlSelect("select vectorclock_serialized from databaseversion where status<>'DIRTY' order by id", databaseConnectionB));
StatusOperationResult statusResultBAfterDirty = clientB.status();
assertNotNull(statusResultBAfterDirty);
ChangeSet changeSetBAfterDirty = statusResultBAfterDirty.getChangeSet();
assertEquals(2, changeSetBAfterDirty.getNewFiles().size());
TestAssertUtil.assertConflictingFileExists("A-file1.jpg", clientB.getLocalFiles());
// (A3,B2)
clientB.up(upOptionsForceEnabled);
assertEquals("0", TestSqlUtil.runSqlSelect("select count(*) from databaseversion where status='DIRTY'", databaseConnectionB));
// (A1), (A1,B1), (A2,B1), (A3,B1)
assertEquals("4", TestSqlUtil.runSqlSelect("select count(*) from databaseversion", databaseConnectionA));
assertEquals("(A1)\n(A1,B1)\n(A2,B1)\n(A3,B1)", TestSqlUtil.runSqlSelect("select vectorclock_serialized from databaseversion order by id", databaseConnectionA));
assertEquals("5", TestSqlUtil.runSqlSelect("select count(*) from databaseversion", databaseConnectionB));
assertEquals("(A1)\n(A1,B1)\n(A2,B1)\n(A3,B1)\n(A3,B4)", TestSqlUtil.runSqlSelect("select vectorclock_serialized from databaseversion order by id", databaseConnectionB));
// // 2. NOW THAT CLIENT B RESOLVED IT, A GETS DIRTY
// No 'down'! This version will become DIRTY
clientA.changeFile("A-file1.jpg");
clientA.createNewFile("dirty1");
clientA.up(upOptionsForceEnabled);
// No 'down'! This version will become DIRTY
clientA.changeFile("A-file1.jpg");
clientA.createNewFile("dirty2");
clientA.up(upOptionsForceEnabled);
// No 'down'! This version will become DIRTY
clientA.changeFile("A-file1.jpg");
clientA.createNewFile("dirty3");
clientA.up(upOptionsForceEnabled);
// No 'down'! This version will become DIRTY
clientA.changeFile("A-file1.jpg");
clientA.createNewFile("dirty4");
clientA.up(upOptionsForceEnabled);
// No 'down'! This version will become DIRTY
clientA.changeFile("A-file1.jpg");
clientA.createNewFile("dirty5");
clientA.up(upOptionsForceEnabled);
// No 'down'! This version will become DIRTY
clientA.changeFile("A-file1.jpg");
clientA.createNewFile("dirty6");
clientA.up(upOptionsForceEnabled);
// No 'down'! This version will become DIRTY
clientA.changeFile("A-file1.jpg");
clientA.createNewFile("dirty7");
clientA.up(upOptionsForceEnabled);
assertEquals("11", TestSqlUtil.runSqlSelect("select count(*) from databaseversion", databaseConnectionA));
clientA.down();
assertEquals("12", TestSqlUtil.runSqlSelect("select count(*) from databaseversion", databaseConnectionA));
assertEquals("7", TestSqlUtil.runSqlSelect("select count(*) from databaseversion where status='DIRTY'", databaseConnectionA));
assertEquals("5", TestSqlUtil.runSqlSelect("select count(*) from databaseversion where status<>'DIRTY'", databaseConnectionA));
assertEquals("(A1)\n(A1,B1)\n(A2,B1)\n(A3,B1)\n(A3,B4)", TestSqlUtil.runSqlSelect("select vectorclock_serialized from databaseversion where status<>'DIRTY' order by id", databaseConnectionA));
// Does nothing; A versions lose against (A3,B2) // same as above!
clientB.down();
assertEquals("5", TestSqlUtil.runSqlSelect("select count(*) from databaseversion", databaseConnectionB));
assertEquals("(A1)\n(A1,B1)\n(A2,B1)\n(A3,B1)\n(A3,B4)", TestSqlUtil.runSqlSelect("select vectorclock_serialized from databaseversion order by id", databaseConnectionB));
// // 3. NEW CLIENT JOINS
clientC.down();
TestAssertUtil.assertSqlDatabaseEquals(clientB.getDatabaseFile(), clientC.getDatabaseFile());
// // 4. FORCE MERGE DATABASES ON CLIENT A
clientA.deleteFile("dirty1");
clientA.deleteFile("dirty2");
// upload DIRTY version
clientA.up(upOptionsForceEnabled);
assertEquals("6", TestSqlUtil.runSqlSelect("select count(*) from databaseversion", databaseConnectionA));
assertEquals("0", TestSqlUtil.runSqlSelect("select count(*) from databaseversion where status='DIRTY'", databaseConnectionA));
assertEquals("6", TestSqlUtil.runSqlSelect("select count(*) from databaseversion where status<>'DIRTY'", databaseConnectionA));
clientA.createNewFile("A-file2.jpg");
// For every X up's call 'cleanup' ("X" is larger than the max. length of file versions in a history)
int cleanupEveryXUps = 7;
for (int i = 1; i <= 21; i++) {
clientA.changeFile("A-file2.jpg");
clientA.up(upOptionsForceEnabled);
if (i % cleanupEveryXUps == 0) {
clientA.cleanup(cleanupOptions);
}
}
clientA.cleanup(cleanupOptions);
clientB.down();
clientC.down();
clientD.down();
TestAssertUtil.assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile());
TestAssertUtil.assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientC.getDatabaseFile());
TestAssertUtil.assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientD.getDatabaseFile());
// Tear down
clientA.deleteTestData();
clientB.deleteTestData();
clientC.deleteTestData();
clientD.deleteTestData();
}
Aggregations