ExchangeFileUtils { public void deleteOldBackups(File directory, int noOfBackups) { if (null != directory && directory.isDirectory()) { Collection<File> fileList = fetchBackupFiles(directory); if (CollectionUtils.isNotEmpty(fileList)) { File[] fileArray = fileList.toArray(new File[fileList.size()]); Arrays.sort(fileArray, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR); int count = numOfFilesToDelete(noOfBackups, fileArray.length); for (int i = 0; i < count; i++) { fileArray[i].delete(); } } } }  String generateUniqueFilename(String fileLocation); void deleteOldBackups(File directory, int noOfBackups);  }
@Test public void deleteBackupsWhenFilesExceedMaxNo() throws IOException, InterruptedException { ExchangeFileUtils fileUtils = new ExchangeFileUtils(); createBackupFiles(fileUtils, 3); URL url = ExchangeFileUtilsTest.class.getClass().getResource(FILE_NAME_WITH_PATH); String exFile = URLDecoder.decode(url.getFile(), "UTF-8"); fileUtils.deleteOldBackups((new File(exFile)).getParentFile(), 2); URL dir = ExchangeFileUtilsTest.class.getClass().getResource(DIRECTORY_NAME); File targetFolder = new File(URLDecoder.decode(dir.getFile(), "UTF-8")); assertEquals("Target folder should have 2 files", 2, targetFolder. listFiles().length); }