### Question:
FileCache { public FileCacheEntry addStream(InputStream is, TaskMonitor monitor) throws IOException, CancelledException { File tmpFile = new File(newDir, UUID.randomUUID().toString()); try (FileOutputStream fos = new FileOutputStream(tmpFile)) { StreamCopyResult copyResults = FSUtilities.streamCopy(is, fos, monitor); fos.close(); String md5 = NumericUtilities.convertBytesToString(copyResults.md5); return addTmpFileToCache(tmpFile, md5, copyResults.bytesCopied); } finally { if (tmpFile.exists()) { Msg.debug(this, "Removing left-over temp file " + tmpFile); tmpFile.delete(); } } } FileCache(File cacheDir); synchronized void purge(); FileCacheEntry addFile(File f, TaskMonitor monitor); FileCacheEntry getFile(String md5); FileCacheEntry addStream(InputStream is, TaskMonitor monitor); FileCacheEntry pushStream(DerivedFilePushProducer pusher, TaskMonitor monitor); @Override String toString(); int getFileAddCount(); int getFileReUseCount(); long getStorageEstimateBytes(); long getMaxFileAgeMS(); static final int MD5_HEXSTR_LEN; }### Answer:
@Test public void testAgeOff() throws IOException, CancelledException { cache = new FileCache(cacheDir); waitForCleanup(); FileCacheEntry cfi = cache.addStream(toIS("This is a test1"), monitor); Assert.assertTrue(cfi.file.exists()); cfi.file.setLastModified(System.currentTimeMillis() - (DateUtils.MS_PER_DAY * 5)); File lastMaintFile = new File(cacheDir, ".lastmaint"); lastMaintFile.delete(); cache = new FileCache(cacheDir); waitForCondition(() -> cfi.file.exists()); }
@Test public void testAddStream() throws IOException, CancelledException { cache = new FileCache(cacheDir); FileCacheEntry fce = cache.addStream(toIS("This is a test1"), monitor); Assert.assertTrue(fce.file.exists()); Assert.assertEquals("10428da10f5aa2793cb73c0b680e1621", fce.md5); Assert.assertEquals(1, cache.getFileAddCount()); }
@Test public void testAgeOff() throws IOException, CancelledException { FileCache cache = new FileCache(cacheDir); FileCacheEntry cfi = cache.addStream(toIS("This is a test1"), monitor); Assert.assertTrue(cfi.file.exists()); cfi.file.setLastModified(System.currentTimeMillis() - (DateUtils.MS_PER_DAY * 5)); File lastMaintFile = new File(cacheDir, ".lastmaint"); lastMaintFile.delete(); new FileCache(cacheDir); Assert.assertFalse(cfi.file.exists()); }
@Test public void testAddStream() throws IOException, CancelledException { FileCache cache = new FileCache(cacheDir); FileCacheEntry fce = cache.addStream(toIS("This is a test1"), monitor); Assert.assertTrue(fce.file.exists()); Assert.assertEquals("10428da10f5aa2793cb73c0b680e1621", fce.md5); Assert.assertEquals(1, cache.getFileAddCount()); }