Пример #1
0
  @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");
  }
Пример #2
0
  @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);
  }
Пример #3
0
  @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");
  }