public MockTaskForCompare(int id, @NonNull DownloadTask task) { this.id = id; this.url = task.url; this.parentFile = task.getParentFile(); this.providedPathFile = task.providedPathFile; this.filename = task.getFilename(); }
@NonNull @Override public BreakpointInfo createAndInsert(@NonNull DownloadTask task) { final int id = task.getId(); BreakpointInfo newInfo = new BreakpointInfo(id, task.getUrl(), task.getParentFile(), task.getFilename()); synchronized (this) { storedInfos.put(id, newInfo); unStoredTasks.remove(id); } return newInfo; }
public boolean isSameFrom(DownloadTask task) { if (!parentFile.equals(task.getParentFile())) { return false; } if (!url.equals(task.getUrl())) return false; final String otherFilename = task.getFilename(); if (otherFilename != null && otherFilename.equals(filenameHolder.get())) return true; if (taskOnlyProvidedParentPath) { // filename is provided by response. if (!task.isFilenameFromResponse()) return false; return otherFilename == null || otherFilename.equals(filenameHolder.get()); } return false; }
@Before public void setup() { when(OkDownload.with().context()).thenReturn(application); initMocks(this); when(task.getFile()).thenReturn(existFile); when(task.getParentFile()).thenReturn(new File(parentPath)); multiPointOutputStream = spy(new MultiPointOutputStream(task, info, store, syncRunnable)); doReturn(syncFuture).when(multiPointOutputStream).executeSyncRunnableAsync(); multiPointOutputStream.syncFuture = syncFuture; multiPointOutputStream.runSyncThread = runSyncThread; }
@Test public void createAndInsert() { final DownloadTask task = mock(DownloadTask.class); when(task.getId()).thenReturn(insertedId); when(task.getParentFile()).thenReturn(new File("/p-path/")); when(task.getFilename()).thenReturn("filename"); when(task.getUrl()).thenReturn("url"); storeOnCache.createAndInsert(task); final BreakpointInfo info = storeOnCache.get(insertedId); assertThat(info).isNotNull(); assertThat(info.id).isEqualTo(insertedId); }
@NonNull final File parentFile = task.getParentFile(); @Nullable final File targetFile = task.getFile();
if (file == null) throw new FileNotFoundException("Filename is not ready!"); final File parentFile = task.getParentFile(); if (!parentFile.exists() && !parentFile.mkdirs()) { throw new IOException("Create parent folder failed!");
@Test public void inspectCompleted_collection() throws IOException { mockOkDownload(); final DownloadStrategy downloadStrategy = OkDownload.with().downloadStrategy(); final CallbackDispatcher callbackDispatcher = OkDownload.with().callbackDispatcher(); final DownloadListener listener = callbackDispatcher.dispatch(); final DownloadTask task = mock(DownloadTask.class); when(task.isPassIfAlreadyCompleted()).thenReturn(true); when(task.getId()).thenReturn(0); when(task.getUrl()).thenReturn("url"); when(task.getParentFile()).thenReturn(existFile.getParentFile()); final BreakpointStore store = OkDownload.with().breakpointStore(); doReturn(existFile.getName()).when(store).getResponseFilename("url"); doReturn(true).when(downloadStrategy).validFilenameFromStore(task); final Collection<DownloadTask> completedCollection = new ArrayList<>(); assertThat(dispatcher.inspectCompleted(task, completedCollection)).isTrue(); verify(listener, never()).taskEnd(eq(task), any(EndCause.class), nullable(Exception.class)); assertThat(completedCollection).containsExactly(task); }
final DownloadTask task = mock(DownloadTask.class); when(task.getUrl()).thenReturn("url"); when(task.getParentFile()).thenReturn(file.getParentFile()); file.getParentFile().mkdirs(); file.createNewFile();
@Test public void inspectCompleted() throws IOException { mockOkDownload(); final CallbackDispatcher callbackDispatcher = OkDownload.with().callbackDispatcher(); final DownloadTask task = mock(DownloadTask.class); when(task.isPassIfAlreadyCompleted()).thenReturn(true); when(task.getId()).thenReturn(0); when(task.getUrl()).thenReturn("url"); when(task.getParentFile()).thenReturn(existFile.getParentFile()); final BreakpointStore store = OkDownload.with().breakpointStore(); doReturn(existFile.getName()).when(store).getResponseFilename("url"); // valid filename failed. final DownloadStrategy downloadStrategy = OkDownload.with().downloadStrategy(); doReturn(false).when(downloadStrategy).validFilenameFromStore(task); assertThat(dispatcher.inspectCompleted(task)).isFalse(); verify(callbackDispatcher, never()).dispatch(); // valid filename success. doReturn(true).when(downloadStrategy).validFilenameFromStore(task); assertThat(dispatcher.inspectCompleted(task)).isTrue(); final DownloadListener listener = callbackDispatcher.dispatch(); verify(listener).taskEnd(eq(task), eq(EndCause.COMPLETED), nullable(Exception.class)); verify(downloadStrategy).validInfoOnCompleted(eq(task), eq(this.store)); }
@Test public void isSameFrom() { BreakpointInfo info = new BreakpointInfo(1, "url", new File("p-path"), "filename"); DownloadTask task = mock(DownloadTask.class); // no filename -> false when(task.getUrl()).thenReturn("url"); when(task.getParentFile()).thenReturn(new File("p-path")); assertThat(info.isSameFrom(task)).isFalse(); // same filename -> true when(task.getFilename()).thenReturn("filename"); assertThat(info.isSameFrom(task)).isTrue(); // is directory but provided same filename -> true when(task.isFilenameFromResponse()).thenReturn(true); assertThat(info.isSameFrom(task)).isTrue(); info = new BreakpointInfo(1, "url", new File("p-path"), null); assertThat(info.isSameFrom(task)).isFalse(); // not directory with filename -> false (don't know whether same yet) when(task.isFilenameFromResponse()).thenReturn(false); assertThat(info.isSameFrom(task)).isFalse(); // is directory and no filename -> true when(task.getFilename()).thenReturn(null); when(task.isFilenameFromResponse()).thenReturn(true); assertThat(info.isSameFrom(task)).isTrue(); // not same url -> false when(task.getUrl()).thenReturn("not-same-url"); assertThat(info.isSameFrom(task)).isFalse(); }
when(task.getParentFile()).thenReturn(file.getParentFile()); when(task.getFilename()).thenReturn(null); when(info.getTotalLength()).thenReturn(1L);
/** * Valid info for {@code task} on completed state. */ public void validInfoOnCompleted(@NonNull DownloadTask task, @NonNull DownloadStore store) { BreakpointInfo info = store.getAfterCompleted(task.getId()); if (info == null) { info = new BreakpointInfo(task.getId(), task.getUrl(), task.getParentFile(), task.getFilename()); final long size; if (Util.isUriContentScheme(task.getUri())) { size = Util.getSizeFromContentUri(task.getUri()); } else { final File file = task.getFile(); if (file == null) { size = 0; Util.w(TAG, "file is not ready on valid info for task on complete state " + task); } else { size = file.length(); } } info.addBlock(new BlockInfo(0, size, size)); } DownloadTask.TaskHideWrapper.setBreakpointInfo(task, info); }
@Test public void createFinder() { DownloadTask task = StatusUtil.createFinder(url, file.getParent(), null); assertThat(task.getFile()).isNull(); assertFile(task.getParentFile()).isEqualTo(file.getParentFile()); task = StatusUtil.createFinder(url, file.getParent(), file.getName()); assertFile(task.getFile()).isEqualTo(file); }
@Test public void get_createAndInsert_onSyncToFilesystemSuccess_update() throws IOException { final int id1 = store.findOrCreateId(mockTask()); final int id2 = store.findOrCreateId(mockTask()); assertThat(id1).isNotEqualTo(id2); verify(onCache, times(2)).findOrCreateId(any(DownloadTask.class)); final DownloadTask task = mock(DownloadTask.class); when(task.getId()).thenReturn(id2); when(task.getUrl()).thenReturn("url"); when(task.getParentFile()).thenReturn(new File("p-path")); doReturn("filename").when(task).getFilename(); store.createAndInsert(task); final BreakpointInfo info2 = onCache.get(id2); assertThat(info2).isNotNull(); verify(helper).insert(info2); info2.addBlock(new BlockInfo(0, 20, 5)); store.onSyncToFilesystemSuccess(info2, 0, 10); verify(onCache).onSyncToFilesystemSuccess(info2, 0, 10); verify(helper).updateBlockIncrease(info2, 0, 15); info2.setEtag("new-etag"); store.update(info2); verify(onCache).update(info2); verify(helper).updateInfo(info2); }
@Test public void taskBuilder_constructWithFile() { final String url = "https://jacksgong.com"; final File noExistFile = new File(parentPath, "no-exist"); DownloadTask task = new DownloadTask.Builder(url, noExistFile).build(); assertThat(task.getFilename()).isEqualTo(noExistFile.getName()); assertThat(task.getFile().getAbsolutePath()).isEqualTo(noExistFile.getAbsolutePath()); final File existFile = new File(parentPath, filename); task = new DownloadTask.Builder(url, existFile).build(); assertThat(task.getFilename()).isEqualTo(existFile.getName()); assertThat(task.getFile().getAbsolutePath()).isEqualTo(existFile.getAbsolutePath()); final File existParentFile = new File(parentPath); task = new DownloadTask.Builder(url, existParentFile).build(); assertThat(task.getFilename()).isNull(); assertThat(task.getFile()).isNull(); assertFile(task.getParentFile()).isEqualTo(existParentFile); final File onlyFile = new File("/path"); task = new DownloadTask.Builder(url, onlyFile).build(); assertThat(task.getFilename()).isEqualTo("path"); assertFile(task.getFile()).isEqualTo(onlyFile); assertFile(task.getParentFile()).isEqualTo(new File("/")); }
assertThat(task.getFilename()).isNull(); assertThat(task.getFile()).isNull(); assertFile(task.getParentFile()).isEqualTo(new File(parentPath)); assertThat(task.getFilename()).isEqualTo(filename); assertFile(task.getFile()).isEqualTo(new File(parentPath, filename)); assertFile(task.getParentFile()).isEqualTo(new File(parentPath)); assertThat(task.getFilename()).isNull(); assertThat(task.getFile()).isNull(); assertFile(task.getParentFile()).isEqualTo(new File(parentPath)); assertThat(task.getFilename()).isEqualTo("not-exist"); assertFile(task.getFile()).isEqualTo(new File("/not-exist")); assertFile(task.getParentFile()).isEqualTo(new File("/")); assertThat(task.getFilename()).isNull(); assertThat(task.getFile()).isNull(); assertFile(task.getParentFile()).isEqualTo(new File("not-exist")); assertThat(task.getFilename()).isEqualTo("filename"); assertFile(task.getFile()).isEqualTo(new File("not-exist/filename")); assertFile(task.getParentFile()).isEqualTo(new File("not-exist")); assertThat(task.getFilename()).isEqualTo("filename"); assertFile(task.getFile()).isEqualTo(new File("not-exist", "filename")); assertFile(task.getParentFile()).isEqualTo(new File("not-exist")); assertThat(task.getFilename()).isNull();