@Test public void concurrent_download() throws IOException { FileHashes hashes = mock(FileHashes.class); when(hashes.of(any(File.class))).thenReturn("ABCDE"); final FileCache cache = new FileCache(tempFolder.newFolder(), log, hashes); FileCache.Downloader downloader = new FileCache.Downloader() { public void download(String filename, File toFile) throws IOException { // Emulate a concurrent download that adds file to cache before File cachedFile = new File(new File(cache.getDir(), "ABCDE"), "sonar-foo-plugin-1.5.jar"); FileUtils.write(cachedFile, "downloaded by other"); FileUtils.write(toFile, "downloaded by me"); } }; // do not fail File cachedFile = cache.get("sonar-foo-plugin-1.5.jar", "ABCDE", downloader); assertThat(cachedFile).isNotNull().exists().isFile(); assertThat(cachedFile.getName()).isEqualTo("sonar-foo-plugin-1.5.jar"); assertThat(cachedFile.getParentFile().getParentFile()).isEqualTo(cache.getDir()); assertThat(FileUtils.readFileToString(cachedFile)).contains("downloaded by"); }
@Test public void found_in_cache() throws IOException { FileCache cache = FileCache.create(tempFolder.newFolder(), log); // populate the cache. Assume that hash is correct. File cachedFile = new File(new File(cache.getDir(), "ABCDE"), "sonar-foo-plugin-1.5.jar"); FileUtils.write(cachedFile, "body"); assertThat(cache.get("sonar-foo-plugin-1.5.jar", "ABCDE")) .isNotNull() .exists() .isEqualTo(cachedFile); }
public File get(String inode) throws DotStateException, DotDataException, DotSecurityException { File file = fileCache.get(inode); if ((file == null) || !InodeUtils.isSet(file.getInode())) { file = (File) HibernateUtil.load(File.class, inode); fileCache.add(file); WorkingCache.removeAssetFromCache(file); WorkingCache.addToWorkingAssetToCache(file); LiveCache.removeAssetFromCache(file); if (file.isLive()) { LiveCache.addToLiveAssetToCache(file); } } return file; }
@Test public void download_corrupted_file() throws IOException { thrown.expect(IllegalStateException.class); thrown.expectMessage("INVALID HASH"); FileHashes hashes = mock(FileHashes.class); FileCache cache = new FileCache(tempFolder.newFolder(), log, hashes); when(hashes.of(any(File.class))).thenReturn("VWXYZ"); FileCache.Downloader downloader = new FileCache.Downloader() { public void download(String filename, File toFile) throws IOException { FileUtils.write(toFile, "corrupted body"); } }; cache.get("sonar-foo-plugin-1.5.jar", "ABCDE", downloader); }
@Test public void download_and_add_to_cache() throws IOException { FileHashes hashes = mock(FileHashes.class); FileCache cache = new FileCache(tempFolder.newFolder(), log, hashes); when(hashes.of(any(File.class))).thenReturn("ABCDE"); FileCache.Downloader downloader = new FileCache.Downloader() { public void download(String filename, File toFile) throws IOException { FileUtils.write(toFile, "body"); } }; File cachedFile = cache.get("sonar-foo-plugin-1.5.jar", "ABCDE", downloader); assertThat(cachedFile).isNotNull().exists().isFile(); assertThat(cachedFile.getName()).isEqualTo("sonar-foo-plugin-1.5.jar"); assertThat(cachedFile.getParentFile().getParentFile()).isEqualTo(cache.getDir()); assertThat(FileUtils.readFileToString(cachedFile)).isEqualTo("body"); }
@Test public void not_in_cache() throws IOException { FileCache cache = FileCache.create(tempFolder.newFolder(), log); assertThat(cache.get("sonar-foo-plugin-1.5.jar", "ABCDE")).isNull(); }