FileCache { @CheckForNull public File get(String filename, String hash) { Path cachedFile = dir.resolve(hash).resolve(filename); if (Files.exists(cachedFile)) { return cachedFile.toFile(); } logger.debug(String.format("No file found in the cache with name %s and hash %s", filename, hash)); return null; } FileCache(Path dir, FileHashes fileHashes, Logger logger); File getDir(); @CheckForNull File get(String filename, String hash); File get(String filename, String hash, Downloader downloader);  }
@Test public void not_in_cache() throws IOException { assertThat(cache.get("sonar-foo-plugin-1.5.jar", "ABCDE")).isNull(); }
@Test public void fail_to_download() { when(fileHashes.of(any(File.class))).thenReturn("ABCDE"); FileCache.Downloader downloader = new FileCache.Downloader() { public void download(String filename, File toFile) throws IOException { throw new IOException("fail"); } }; thrown.expect(IllegalStateException.class); thrown.expectMessage("Fail to download"); cache.get("sonar-foo-plugin-1.5.jar", "ABCDE", downloader); }
@Test public void fail_create_temp_file() throws IOException { when(fileHashes.of(any(File.class))).thenReturn("ABCDE"); new File(temp.getRoot(), "_tmp").delete(); thrown.expect(IllegalStateException.class); thrown.expectMessage("Fail to create temp file"); cache.get("sonar-foo-plugin-1.5.jar", "ABCDE", mock(FileCache.Downloader.class)); }
@Test public void download_corrupted_file() throws IOException { thrown.expect(IllegalStateException.class); thrown.expectMessage("INVALID HASH"); when(fileHashes.of(any(File.class))).thenReturn("VWXYZ"); FileCache.Downloader downloader = new FileCache.Downloader() { public void download(String filename, File toFile) throws IOException { write(toFile, "corrupted body"); } }; cache.get("sonar-foo-plugin-1.5.jar", "ABCDE", downloader); }