@Test public void testContentChecksumCache() throws IOException { Database database = new Database(); // Round 1: Add file history & version DatabaseVersion databaseVersion1 = TestDatabaseUtil.createDatabaseVersion(); // - history 1, version 1 FileVersion fileVersion1 = TestDatabaseUtil.createFileVersion("samechecksum1.jpg"); fileVersion1.setChecksum(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}); PartialFileHistory fileHistory1 = new PartialFileHistory(11111111111111111L); databaseVersion1.addFileHistory(fileHistory1); databaseVersion1.addFileVersionToHistory(fileHistory1.getFileId(), fileVersion1); database.addDatabaseVersion(databaseVersion1); assertNotNull(database.getFileHistories(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0})); assertEquals(1, database.getFileHistories(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}).size()); // Round 2: Add two other versions with same checksum to new database version DatabaseVersion databaseVersion2 = TestDatabaseUtil.createDatabaseVersion(databaseVersion1); // - history 1, version 2 FileVersion fileVersion11 = TestDatabaseUtil.createFileVersion("samechecksum2-renamed.jpg", fileVersion1); fileVersion11.setChecksum(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}); // same checksum! fileVersion11.setStatus(FileStatus.RENAMED); PartialFileHistory fileHistory11 = new PartialFileHistory(11111111111111111L); // same ID as above databaseVersion2.addFileHistory(fileHistory11); databaseVersion2.addFileVersionToHistory(fileHistory11.getFileId(), fileVersion11); // - history 2, version 1 FileVersion fileVersion2 = TestDatabaseUtil.createFileVersion("samechecksum2.jpg"); fileVersion2.setChecksum(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}); // same checksum! PartialFileHistory fileHistory2 = new PartialFileHistory(22222222222222222L); // different ID databaseVersion2.addFileHistory(fileHistory2); databaseVersion2.addFileVersionToHistory(fileHistory2.getFileId(), fileVersion2); // - history 3, version 1 FileVersion fileVersion3 = TestDatabaseUtil.createFileVersion("samechecksum3.jpg"); fileVersion3.setChecksum(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}); // same checksum! PartialFileHistory fileHistory3 = new PartialFileHistory(33333333333333333L); // different ID databaseVersion2.addFileHistory(fileHistory3); databaseVersion2.addFileVersionToHistory(fileHistory3.getFileId(), fileVersion3); database.addDatabaseVersion(databaseVersion2); assertNotNull(database.getFileHistories(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0})); assertEquals(3, database.getFileHistories(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}).size()); }
public static FileVersion createFileVersion(String path) { FileVersion fileVersion = new FileVersion(); fileVersion.setChecksum(new FileChecksum(TestFileUtil.createRandomArray(20))); fileVersion.setLastModified(new Date()); fileVersion.setPath(path); fileVersion.setStatus(FileStatus.NEW); fileVersion.setType(FileType.FILE); fileVersion.setUpdated(new Date()); fileVersion.setVersion(1L); return fileVersion; }
private FileVersion createNewFileVersion( FileVersion lastFileVersion, FileProperties fileProperties) { FileVersion fileVersion = null; // Version if (lastFileVersion == null) { fileVersion = new FileVersion(); fileVersion.setVersion(1L); fileVersion.setStatus(FileStatus.NEW); } else { fileVersion = lastFileVersion.clone(); fileVersion.setVersion(lastFileVersion.getVersion() + 1); } // Simple attributes fileVersion.setPath(fileProperties.getRelativePath()); fileVersion.setLinkTarget(fileProperties.getLinkTarget()); fileVersion.setType(fileProperties.getType()); fileVersion.setSize(fileProperties.getSize()); fileVersion.setChecksum(fileProperties.getChecksum()); fileVersion.setLastModified(new Date(fileProperties.getLastModified())); fileVersion.setUpdated(new Date()); // Permissions if (EnvironmentUtil.isWindows()) { fileVersion.setDosAttributes(fileProperties.getDosAttributes()); fileVersion.setPosixPermissions(DEFAULT_POSIX_PERMISSIONS); } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) { fileVersion.setPosixPermissions(fileProperties.getPosixPermissions()); fileVersion.setDosAttributes(DEFAULT_DOS_ATTRIBUTES); } // Status if (lastFileVersion != null) { if (fileVersion.getType() == FileType.FILE && FileChecksum.fileChecksumEquals( fileVersion.getChecksum(), lastFileVersion.getChecksum())) { fileVersion.setStatus(FileStatus.CHANGED); } else if (!fileVersion.getPath().equals(lastFileVersion.getPath())) { fileVersion.setStatus(FileStatus.RENAMED); } else { fileVersion.setStatus(FileStatus.CHANGED); } } return fileVersion; }