Search in sources :

Example 1 with SqlDatabase

use of org.syncany.database.SqlDatabase in project syncany by syncany.

the class EmptyFileScenarioTest method testEmptyFileCreateAndSync.

@Test
public void testEmptyFileCreateAndSync() throws Exception {
    // Setup 
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);
    // Run 
    clientA.createNewFile("A-file1.jpg", 0);
    clientA.up();
    clientB.down();
    assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead());
    assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile());
    clientB.createNewFile("B-file2", 0);
    clientB.moveFile("A-file1.jpg", "B-file1-moved");
    clientB.up();
    SqlDatabase database = clientB.loadLocalDatabase();
    DatabaseVersionHeader lastDatabaseVersionHeaderBeforeUp = database.getLastDatabaseVersionHeader();
    // double-up, has caused problems
    clientB.up();
    DatabaseVersionHeader lastDatabaseVersionHeaderAfterUp = database.getLastDatabaseVersionHeader();
    assertEquals("Nothing changed. Local database file should not change.", lastDatabaseVersionHeaderBeforeUp, lastDatabaseVersionHeaderAfterUp);
    clientA.down();
    assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead());
    assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile());
    Map<String, File> beforeSyncDownFileList = clientB.getLocalFilesExcludeLockedAndNoRead();
    // double-down, has caused problems		
    clientA.down();
    assertFileListEquals("No change in file lists expected. Nothing changed", beforeSyncDownFileList, clientA.getLocalFilesExcludeLockedAndNoRead());
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
}
Also used : TestClient(org.syncany.tests.util.TestClient) SqlDatabase(org.syncany.database.SqlDatabase) TransferSettings(org.syncany.plugins.transfer.TransferSettings) DatabaseVersionHeader(org.syncany.database.DatabaseVersionHeader) File(java.io.File) Test(org.junit.Test)

Example 2 with SqlDatabase

use of org.syncany.database.SqlDatabase in project syncany by syncany.

the class FileLockedScenarioTest method runUpAndTestForEmptyDatabase.

private void runUpAndTestForEmptyDatabase(TransferSettings connection, TestClient client) throws Exception {
    UpOperationResult upResult = client.up();
    StatusOperationResult statusResult = upResult.getStatusResult();
    // Test 1: Check result sets for inconsistencies
    assertFalse("Status command expected to return NO changes.", statusResult.getChangeSet().hasChanges());
    assertFalse("File should NOT be uploaded while it is locked.", upResult.getChangeSet().hasChanges());
    // Test 2: Check database for inconsistencies
    SqlDatabase database = client.loadLocalDatabase();
    assertEquals("File should NOT be uploaded while it is locked.", 0, database.getFileList("large-test-file", null, false, false, false, null).size());
    assertNull("There should NOT be a new database version, because file should not have been added.", database.getLastDatabaseVersionHeader());
    // Test 3: Check file system for inconsistencies
    File repoPath = ((LocalTransferSettings) connection).getPath();
    String[] repoFileList = repoPath.list(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith("database-");
        }
    });
    assertEquals("Repository should NOT contain any files.", 0, repoFileList.length);
}
Also used : FilenameFilter(java.io.FilenameFilter) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) SqlDatabase(org.syncany.database.SqlDatabase) RandomAccessFile(java.io.RandomAccessFile) LockFile(org.syncany.tests.integration.scenarios.framework.LockFile) File(java.io.File) UnlockFile(org.syncany.tests.integration.scenarios.framework.UnlockFile) UpOperationResult(org.syncany.operations.up.UpOperationResult) StatusOperationResult(org.syncany.operations.status.StatusOperationResult)

Example 3 with SqlDatabase

use of org.syncany.database.SqlDatabase in project syncany by syncany.

the class CallUpWhileStillWritingFileScenarioTest method testUpWhileWritingFile.

@Test
public void testUpWhileWritingFile() throws Exception {
    // Setup
    final TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    final TestClient clientA = new TestClient("A", testConnection);
    final TestClient clientB = new TestClient("B", testConnection);
    final File testFile = clientA.getLocalFile("large-test-file");
    final long testFileLength = 100 * 1024 * 1024;
    Thread writeFileThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                logger.log(Level.INFO, "Started thread to write file to " + testFile + "  ...");
                FileOutputStream fos = new FileOutputStream(testFile);
                Random randomEngine = new Random();
                byte[] buf = new byte[4096];
                int writtenLen = 0;
                while (writtenLen < testFileLength) {
                    randomEngine.nextBytes(buf);
                    fos.write(buf, 0, buf.length);
                    writtenLen += buf.length;
                }
                fos.close();
                logger.log(Level.INFO, "Ended thread to write file to " + testFile + "  ...");
            } catch (IOException e) {
                logger.log(Level.FINE, "Thread failed to write to file", e);
            }
        }
    }, "writerThread");
    // Before start: setup up databases (takes a while)
    clientA.status();
    clientB.status();
    // Run!
    writeFileThread.start();
    Thread.sleep(50);
    logger.log(Level.INFO, "Started clientA.up()");
    UpOperationResult upResult = clientA.up();
    StatusOperationResult statusResult = upResult.getStatusResult();
    logger.log(Level.INFO, "Ended clientA.up()");
    writeFileThread.join();
    // Test 1: Check result sets for inconsistencies
    assertTrue("Status command expected to return changes.", statusResult.getChangeSet().hasChanges());
    assertFalse("File should NOT be uploaded while still writing (no half-file upload).", upResult.getChangeSet().hasChanges());
    // Test 2: Check database for inconsistencies
    SqlDatabase database = clientA.loadLocalDatabase();
    assertEquals("File should NOT be uploaded while still writing (no half-file upload).", 0, database.getFileList("large-test-file", null, false, false, false, null).size());
    assertNull("There should NOT be a new database version, because file should not have been added.", database.getLastDatabaseVersionHeader());
    // Test 3: Check file system for inconsistencies
    File repoPath = new File(((LocalTransferSettings) testConnection).getPath() + "/databases");
    String[] repoFileList = repoPath.list(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith("database-");
        }
    });
    assertEquals("Repository should NOT contain any files.", 0, repoFileList.length);
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
}
Also used : LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) TransferSettings(org.syncany.plugins.transfer.TransferSettings) IOException(java.io.IOException) UpOperationResult(org.syncany.operations.up.UpOperationResult) FilenameFilter(java.io.FilenameFilter) Random(java.util.Random) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) TestClient(org.syncany.tests.util.TestClient) FileOutputStream(java.io.FileOutputStream) SqlDatabase(org.syncany.database.SqlDatabase) File(java.io.File) StatusOperationResult(org.syncany.operations.status.StatusOperationResult) Test(org.junit.Test)

Example 4 with SqlDatabase

use of org.syncany.database.SqlDatabase 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();
}
Also used : Path(java.nio.file.Path) Set(java.util.Set) ChangeSet(org.syncany.operations.ChangeSet) TestClient(org.syncany.tests.util.TestClient) SqlDatabase(org.syncany.database.SqlDatabase) TransferSettings(org.syncany.plugins.transfer.TransferSettings) File(java.io.File) ChangeSet(org.syncany.operations.ChangeSet) StatusOperationResult(org.syncany.operations.status.StatusOperationResult) UpOperationResult(org.syncany.operations.up.UpOperationResult) Test(org.junit.Test)

Example 5 with SqlDatabase

use of org.syncany.database.SqlDatabase in project syncany by syncany.

the class UpOperationTest method testUploadLocalDatabase.

@Test
public void testUploadLocalDatabase() throws Exception {
    int fileSize = 1230 * 1024;
    int fileAmount = 3;
    List<File> originalFiles = TestFileUtil.createRandomFilesInDirectory(testConfig.getLocalDir(), fileSize, fileAmount);
    // Run!
    AbstractTransferOperation op = new UpOperation(testConfig);
    op.execute();
    // Get databases (for comparison)
    LocalTransferSettings localConnection = (LocalTransferSettings) testConfig.getConnection();
    File localDatabaseDir = testConfig.getDatabaseDir();
    File remoteDatabaseFile = new File(localConnection.getPath() + "/databases/database-" + testConfig.getMachineName() + "-0000000001");
    assertNotNull(localDatabaseDir.listFiles());
    assertTrue(localDatabaseDir.listFiles().length > 0);
    assertTrue(remoteDatabaseFile.exists());
    // - Memory database
    DatabaseXmlSerializer dDAO = new DatabaseXmlSerializer(testConfig.getTransformer());
    MemoryDatabase remoteDatabase = new MemoryDatabase();
    dDAO.load(remoteDatabase, remoteDatabaseFile, null, null, DatabaseReadType.FULL);
    DatabaseVersion remoteDatabaseVersion = remoteDatabase.getLastDatabaseVersion();
    // - Sql Database
    SqlDatabase localDatabase = new SqlDatabase(testConfig);
    Map<FileHistoryId, PartialFileHistory> localFileHistories = localDatabase.getFileHistoriesWithFileVersions();
    // Compare!
    assertEquals(localDatabase.getLastDatabaseVersionHeader(), remoteDatabaseVersion.getHeader());
    assertEquals(localFileHistories.size(), fileAmount);
    assertEquals(localDatabase.getFileHistoriesWithFileVersions().size(), remoteDatabaseVersion.getFileHistories().size());
    Collection<PartialFileHistory> remoteFileHistories = remoteDatabaseVersion.getFileHistories();
    List<FileVersion> remoteFileVersions = new ArrayList<FileVersion>();
    List<FileVersion> localFileVersions = new ArrayList<FileVersion>();
    for (PartialFileHistory partialFileHistory : remoteFileHistories) {
        remoteFileVersions.add(partialFileHistory.getLastVersion());
        assertNotNull(localFileHistories.get(partialFileHistory.getFileHistoryId()));
    }
    for (PartialFileHistory partialFileHistory : localFileHistories.values()) {
        localFileVersions.add(partialFileHistory.getLastVersion());
    }
    assertTrue(CollectionUtil.containsExactly(localFileVersions, remoteFileVersions));
    compareFileVersionsAgainstOriginalFiles(originalFiles, localFileVersions);
    compareFileVersionsAgainstOriginalFiles(originalFiles, remoteFileVersions);
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) ArrayList(java.util.ArrayList) DatabaseXmlSerializer(org.syncany.database.dao.DatabaseXmlSerializer) PartialFileHistory(org.syncany.database.PartialFileHistory) AbstractTransferOperation(org.syncany.operations.AbstractTransferOperation) UpOperation(org.syncany.operations.up.UpOperation) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) FileVersion(org.syncany.database.FileVersion) MemoryDatabase(org.syncany.database.MemoryDatabase) SqlDatabase(org.syncany.database.SqlDatabase) File(java.io.File) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Aggregations

SqlDatabase (org.syncany.database.SqlDatabase)9 File (java.io.File)8 Test (org.junit.Test)7 TransferSettings (org.syncany.plugins.transfer.TransferSettings)6 TestClient (org.syncany.tests.util.TestClient)6 StatusOperationResult (org.syncany.operations.status.StatusOperationResult)5 UpOperationResult (org.syncany.operations.up.UpOperationResult)5 LocalTransferSettings (org.syncany.plugins.local.LocalTransferSettings)5 FilenameFilter (java.io.FilenameFilter)3 RandomAccessFile (java.io.RandomAccessFile)2 DatabaseVersionHeader (org.syncany.database.DatabaseVersionHeader)2 LockFile (org.syncany.tests.integration.scenarios.framework.LockFile)2 UnlockFile (org.syncany.tests.integration.scenarios.framework.UnlockFile)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 Set (java.util.Set)1 DatabaseVersion (org.syncany.database.DatabaseVersion)1