### Question:
BackupFileManager { int size() { return backupFileInfos.size(); } BackupFileManager(AccountServiceMetrics accountServiceMetrics, String backupDir, int maxBackupFileCount); int getLatestVersion();  }### Answer:
@Test public void testCleanup_EnoughBackupFilesWithVersion() throws IOException { final int startVersion = 1; final int endVersion = startVersion + maxBackupFile * 2; final long baseModifiedTime = System.currentTimeMillis() / 1000 - 100; final long interval = 2; String[] filenames = createBackupFilesWithVersion(accountBackupDir, startVersion, endVersion, baseModifiedTime, interval, false); saveAccountsToFile(Collections.singleton(refAccount), accountBackupDir.resolve(filenames[filenames.length - 1])); createBackupFilesWithVersion(accountBackupDir, startVersion, endVersion, baseModifiedTime, interval, true); createBackupFilesWithoutVersion(accountBackupDir, 10, baseModifiedTime, interval, false); createBackupFilesWithoutVersion(accountBackupDir, 10, baseModifiedTime, interval, true); BackupFileManager backup = new BackupFileManager(accountServiceMetrics, config.backupDir, config.maxBackupFileCount); assertEquals("Number of backup files mismatch", backup.size(), maxBackupFile); File[] remainingFiles = accountBackupDir.toFile().listFiles(); assertEquals("Number of backup files in storage mismatch", remainingFiles.length, maxBackupFile); for (File file : remainingFiles) { assertTrue("Filename " + file.getName() + " should follow version pattern ", BackupFileManager.versionFilenamePattern.matcher(file.getName()).find()); } }
@Test public void testCleanup_NotEnoughBackupFilesWithVersion() throws IOException { final int startVersion = 1; final int endVersion = startVersion + maxBackupFile / 2; final long baseModifiedTime = System.currentTimeMillis() / 1000 - 100; final long interval = 2; final int backupFileNoVersionCount = maxBackupFile; createBackupFilesWithoutVersion(accountBackupDir, backupFileNoVersionCount, baseModifiedTime, interval, false); createBackupFilesWithoutVersion(accountBackupDir, backupFileNoVersionCount, baseModifiedTime, interval, true); createBackupFilesWithVersion(accountBackupDir, startVersion, endVersion, baseModifiedTime + 2 * backupFileNoVersionCount * interval, interval, true); String[] filenames = createBackupFilesWithVersion(accountBackupDir, startVersion, endVersion, baseModifiedTime + 2 * backupFileNoVersionCount * interval, interval, false); saveAccountsToFile(Collections.singleton(refAccount), accountBackupDir.resolve(filenames[filenames.length - 1])); BackupFileManager backup = new BackupFileManager(accountServiceMetrics, config.backupDir, config.maxBackupFileCount); assertEquals("Number of backup files mismatch", backup.size(), maxBackupFile); File[] remainingFiles = accountBackupDir.toFile().listFiles(); assertEquals("Number of backup files in storage mismatch", remainingFiles.length, maxBackupFile); int versionFileCount = 0; int nonVersionFileCount = 0; for (File file : remainingFiles) { if (BackupFileManager.versionFilenamePattern.matcher(file.getName()).find()) { versionFileCount++; } else { nonVersionFileCount++; } } assertEquals("Number of backup files with version mismatch", versionFileCount, endVersion - startVersion + 1); assertEquals("Number of backup files without version ", nonVersionFileCount, maxBackupFile - (endVersion - startVersion + 1)); }