Search in sources :

Example 1 with FileDownloader

use of com.intellij.util.download.FileDownloader in project android by JetBrains.

the class DistributionService method getInstance.

public static DistributionService getInstance() {
    if (ourInstance == null) {
        DownloadableFileDescription description = DownloadableFileService.getInstance().createFileDescription(STATS_URL, DOWNLOAD_FILENAME);
        FileDownloader downloader = DownloadableFileService.getInstance().createDownloader(ImmutableList.of(description), "Distribution Stats");
        ourInstance = new DistributionService(downloader, CACHE_PATH, FALLBACK_URL);
    }
    return ourInstance;
}
Also used : DownloadableFileDescription(com.intellij.util.download.DownloadableFileDescription) FileDownloader(com.intellij.util.download.FileDownloader)

Example 2 with FileDownloader

use of com.intellij.util.download.FileDownloader in project android by JetBrains.

the class DistributionServiceTest method testFallbackToPrevious.

/**
   * Test that we pick up previously-downloaded distributions if a new one can't be loaded.
   */
public void testFallbackToPrevious() throws Exception {
    File newFile = new File(CACHE_PATH, "distributions_2.json");
    FileUtil.copy(new File(new File(myFixture.getTestDataPath(), DISTRIBUTION_PATH), "testPreviousDistributions.json"), newFile);
    if (!newFile.setLastModified(20000)) {
        fail();
    }
    File oldFile = new File(CACHE_PATH, "distributions.json");
    FileUtil.copy(new File(new File(myFixture.getTestDataPath(), DISTRIBUTION_PATH), "testPreviousDistributions2.json"), oldFile);
    if (!oldFile.setLastModified(10000)) {
        fail();
    }
    FileDownloader downloader = Mockito.mock(FileDownloader.class);
    Mockito.when(downloader.download(Matchers.any(File.class))).thenThrow(new RuntimeException("expected exception"));
    DistributionService service = new DistributionService(downloader, CACHE_PATH, myDistributionFileUrl);
    final FutureResult<Boolean> result = new FutureResult<>();
    service.refresh(() -> result.set(true), () -> {
        assert false;
    });
    assertTrue(result.get(5, TimeUnit.SECONDS));
    assertEquals(.3, service.getSupportedDistributionForApiLevel(16), 0.0001);
}
Also used : FutureResult(com.intellij.util.concurrency.FutureResult) FileDownloader(com.intellij.util.download.FileDownloader) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File)

Example 3 with FileDownloader

use of com.intellij.util.download.FileDownloader in project android by JetBrains.

the class DistributionServiceTest method testSimpleCase.

/**
   * Test that we get back the correct sum for an api level
   */
public void testSimpleCase() throws Exception {
    FileDownloader downloader = Mockito.mock(FileDownloader.class);
    Mockito.when(downloader.download(Matchers.any(File.class))).thenReturn(ImmutableList.of(Pair.create(myDistributionFile, myDescription)));
    DistributionService service = new DistributionService(downloader, CACHE_PATH, myDistributionFileUrl);
    assertEquals(0.7, service.getSupportedDistributionForApiLevel(16), 0.0001);
}
Also used : FileDownloader(com.intellij.util.download.FileDownloader) File(java.io.File)

Example 4 with FileDownloader

use of com.intellij.util.download.FileDownloader in project android by JetBrains.

the class DistributionServiceTest method testAsync.

/**
   * Test that refresh will run asynchronously.
   */
public void testAsync() throws Exception {
    final FileDownloader downloader = Mockito.mock(FileDownloader.class);
    final Semaphore s = new Semaphore();
    s.down();
    Mockito.when(downloader.download(Matchers.any(File.class))).thenAnswer(new Answer<List<Pair<File, DownloadableFileDescription>>>() {

        @Override
        public List<Pair<File, DownloadableFileDescription>> answer(InvocationOnMock invocation) throws Throwable {
            assertTrue(s.waitFor(5000));
            return ImmutableList.of(Pair.create(myDistributionFile, myDescription));
        }
    });
    final DistributionService service = new DistributionService(downloader, CACHE_PATH, myDistributionFileUrl);
    service.refresh(() -> {
        service.getSupportedDistributionForApiLevel(19);
        service.getDistributionForApiLevel(21);
        try {
            Mockito.verify(downloader).download(Matchers.any(File.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }, null);
    s.up();
}
Also used : DownloadableFileDescription(com.intellij.util.download.DownloadableFileDescription) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FileDownloader(com.intellij.util.download.FileDownloader) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Semaphore(com.intellij.util.concurrency.Semaphore) IOException(java.io.IOException) File(java.io.File)

Example 5 with FileDownloader

use of com.intellij.util.download.FileDownloader in project android by JetBrains.

the class DistributionServiceTest method testAsync2.

/**
   * Test that if we get another call to refresh while one is in progress, it's callbacks will be queued.
   */
public void testAsync2() throws Exception {
    final FileDownloader downloader = Mockito.mock(FileDownloader.class);
    final Semaphore s = new Semaphore();
    s.down();
    final Semaphore s2 = new Semaphore();
    s2.down();
    Mockito.when(downloader.download(Matchers.any(File.class))).thenAnswer(new Answer<List<Pair<File, DownloadableFileDescription>>>() {

        @Override
        public List<Pair<File, DownloadableFileDescription>> answer(InvocationOnMock invocation) throws Throwable {
            assertTrue(s.waitFor(5000));
            return ImmutableList.of(Pair.create(myDistributionFile, myDescription));
        }
    });
    DistributionService service = new DistributionService(downloader, CACHE_PATH, myDistributionFileUrl);
    final AtomicBoolean check = new AtomicBoolean(false);
    service.refresh(() -> check.set(true), null);
    service.refresh(() -> {
        assertTrue(check.get());
        s2.up();
    }, null);
    s.up();
    assertTrue(s2.waitFor(5000));
    service.getSupportedDistributionForApiLevel(19);
    try {
        Mockito.verify(downloader).download(Matchers.any(File.class));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DownloadableFileDescription(com.intellij.util.download.DownloadableFileDescription) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FileDownloader(com.intellij.util.download.FileDownloader) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) File(java.io.File)

Aggregations

FileDownloader (com.intellij.util.download.FileDownloader)8 File (java.io.File)6 DownloadableFileDescription (com.intellij.util.download.DownloadableFileDescription)4 List (java.util.List)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ImmutableList (com.google.common.collect.ImmutableList)2 FutureResult (com.intellij.util.concurrency.FutureResult)2 Semaphore (com.intellij.util.concurrency.Semaphore)2 IOException (java.io.IOException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 ExecutionException (com.intellij.execution.ExecutionException)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 Pair (com.intellij.openapi.util.Pair)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 ActionLink (com.intellij.ui.components.labels.ActionLink)1 DownloadableFileService (com.intellij.util.download.DownloadableFileService)1