CachedContentCleanupJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap jobData = context.getJobDetail().getJobDataMap(); CachedContentCleaner cachedContentCleaner = cachedContentCleaner(jobData); cachedContentCleaner.execute("scheduled"); }  @Override void execute(JobExecutionContext context);  }
@Test public void filesNotInCacheAreDeleted() { cleaner.setMaxDeleteWatchCount(0); int numFiles = 300; long totalSize = 0; File[] files = new File[numFiles]; for (int i = 0; i < numFiles; i++) { UrlSource urlSource = UrlSource.values()[i % UrlSource.values().length]; File cacheFile = createCacheFile(urlSource, false); files[i] = cacheFile; totalSize += cacheFile.length(); } cleaner.execute(); for (File file : files) { assertFalse("File should have been deleted: " + file, file.exists()); } assertEquals("Incorrect number of deleted files", numFiles, cleaner.getNumFilesDeleted()); assertEquals("Incorrect total size of files deleted", totalSize, cleaner.getSizeFilesDeleted()); }
@Test public void filesNewerThanMinFileAgeMillisAreNotDeleted() throws InterruptedException { final long minFileAge = 1000; cleaner.setMinFileAgeMillis(minFileAge); cleaner.setMaxDeleteWatchCount(0); int numFiles = 10; File[] oldFiles = new File[numFiles]; for (int i = 0; i < numFiles; i++) { oldFiles[i] = createCacheFile(UrlSource.REVERSE_CACHE_LOOKUP, false); } Thread.sleep(minFileAge); File[] newFiles = new File[numFiles]; long newFilesTotalSize = 0; for (int i = 0; i < numFiles; i++) { newFiles[i] = createCacheFile(UrlSource.REVERSE_CACHE_LOOKUP, false); newFilesTotalSize += newFiles[i].length(); } cleaner.execute(); for (File file : oldFiles) { assertFalse("File should have been deleted: " + file, file.exists()); } for (File file : newFiles) { assertTrue("File should not have been deleted: " + file, file.exists()); } assertEquals("Incorrect number of deleted files", newFiles.length, cleaner.getNumFilesDeleted()); assertEquals("Incorrect total size of files deleted", newFilesTotalSize, cleaner.getSizeFilesDeleted()); }
@Test public void filesInCacheAreNotDeleted() { cleaner.setMaxDeleteWatchCount(0); String url = makeContentUrl(); int numFiles = 50; for (int i = 0; i < numFiles; i++) { ContentReader reader = cachingStore.getReader(url); reader.getContentString(); } cleaner.execute(); for (int i = 0; i < numFiles; i++) { File cacheFile = new File(cache.getCacheFilePath(url)); assertTrue("File should exist", cacheFile.exists()); } }