### Question:
DownloadDispatcher { @Nullable public synchronized DownloadTask findSameTask(DownloadTask task) { Util.d(TAG, "findSameTask: " + task.getId()); for (DownloadCall call : readyAsyncCalls) { if (call.isCanceled()) continue; if (call.equalsTask(task)) return call.task; } for (DownloadCall call : runningAsyncCalls) { if (call.isCanceled()) continue; if (call.equalsTask(task)) return call.task; } for (DownloadCall call : runningSyncCalls) { if (call.isCanceled()) continue; if (call.equalsTask(task)) return call.task; } return null; } DownloadDispatcher();   DownloadDispatcher(List<DownloadCall> readyAsyncCalls,
                       List<DownloadCall> runningAsyncCalls,
                       List<DownloadCall> runningSyncCalls,
                       List<DownloadCall> finishingCalls); void setDownloadStore(@NonNull DownloadStore store); void enqueue(DownloadTask[] tasks); void enqueue(DownloadTask task); void execute(DownloadTask task); @SuppressWarnings("PMD.ForLoopsMustUseBraces") void cancelAll(); void cancel(IdentifiedTask[] tasks); boolean cancel(IdentifiedTask task); boolean cancel(int id); @Nullable synchronized DownloadTask findSameTask(DownloadTask task); synchronized boolean isRunning(DownloadTask task); synchronized boolean isPending(DownloadTask task); synchronized void flyingCanceled(DownloadCall call); synchronized void finish(DownloadCall call); synchronized boolean isFileConflictAfterRun(@NonNull DownloadTask task); static void setMaxParallelRunningCount(int maxParallelRunningCount);  }### Answer:
@Test public void findSameTask_readyAsyncCall() { final DownloadTask task = mock(DownloadTask.class); final DownloadStore store = mock(DownloadStore.class); final DownloadCall canceledCall = spy(DownloadCall .create(mock(DownloadTask.class), true, store)); final DownloadCall nonCanceledCall = spy(DownloadCall .create(mock(DownloadTask.class), true, store)); when(canceledCall.isCanceled()).thenReturn(true); when(canceledCall.equalsTask(task)).thenReturn(true); when(nonCanceledCall.equalsTask(task)).thenReturn(true); readyAsyncCalls.add(canceledCall); readyAsyncCalls.add(nonCanceledCall); assertThat(dispatcher.findSameTask(task)).isEqualTo(nonCanceledCall.task); }
@Test public void findSameTask_runningAsyncCall() { final DownloadTask task = mock(DownloadTask.class); final DownloadStore store = mock(DownloadStore.class); final DownloadCall canceledCall = spy(DownloadCall .create(mock(DownloadTask.class), true, store)); final DownloadCall nonCanceledCall = spy(DownloadCall .create(mock(DownloadTask.class), true, store)); when(canceledCall.equalsTask(task)).thenReturn(true); when(canceledCall.isCanceled()).thenReturn(true); when(nonCanceledCall.equalsTask(task)).thenReturn(true); runningAsyncCalls.add(canceledCall); runningAsyncCalls.add(nonCanceledCall); assertThat(dispatcher.findSameTask(task)).isEqualTo(nonCanceledCall.task); }
@Test public void findSameTask_runningSyncCall() { final DownloadTask task = mock(DownloadTask.class); final DownloadStore store = mock(DownloadStore.class); final DownloadCall canceledCall = spy(DownloadCall .create(mock(DownloadTask.class), false, store)); final DownloadCall nonCanceledCall = spy(DownloadCall .create(mock(DownloadTask.class), false, store)); when(canceledCall.isCanceled()).thenReturn(true); when(canceledCall.equalsTask(task)).thenReturn(true); when(nonCanceledCall.equalsTask(task)).thenReturn(true); runningSyncCalls.add(canceledCall); runningSyncCalls.add(nonCanceledCall); assertThat(dispatcher.findSameTask(task)).isEqualTo(nonCanceledCall.task); }
@Test public void findSameTask_nonMatch() { final DownloadTask task = mock(DownloadTask.class); assertThat(dispatcher.findSameTask(task)).isNull(); }