@Test
public void testDownloadFile_lastModifiedFail() throws Exception {

    CountingAnswer debugAnswer = new CountingAnswer(null);
    CountingAnswer setLastModifiedAnswerTrue = new CountingAnswer(true);
    CountingAnswer setLastModifiedAnswerFalse = new CountingAnswer(false);
    RuleDatabaseItemUpdateRunnable itemUpdateRunnable = new RuleDatabaseItemUpdateRunnable(realTask, mockContext, mock(Configuration.Item.class));
    FileOutputStream fos = mock(FileOutputStream.class);
    InputStream is = mock(InputStream.class);
    when(connection.getInputStream()).thenReturn(is);
    when(singleWriterMultipleReaderFile.startWrite()).thenReturn(fos);
    when(is.read(any(byte[].class))).thenReturn(-1);
    when(Log.d(anyString(), anyString())).then(debugAnswer);
    // Scenario 0: Connection has no last modified & we cannot set (0, 0)
    when(connection.getLastModified()).thenReturn(0L);
    when(file.setLastModified(anyLong())).then(setLastModifiedAnswerFalse);
    itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection);
    assertEquals(0, failAnswer.numCalls);
    assertEquals(1, debugAnswer.numCalls);
    assertEquals(1, finishAnswer.numCalls);
    // Scenario 1: Connect has no last modified & we can set (0, 1);
    when(connection.getLastModified()).thenReturn(0L);
    when(file.setLastModified(anyLong())).then(setLastModifiedAnswerTrue);
    itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection);
    assertEquals(0, failAnswer.numCalls);
    assertEquals(2, debugAnswer.numCalls);
    assertEquals(2, finishAnswer.numCalls);
    // Scenario 2: Connect has last modified & we cannot set (1, 0);
    when(connection.getLastModified()).thenReturn(1L);
    when(file.setLastModified(anyLong())).then(setLastModifiedAnswerFalse);
    itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection);
    assertEquals(0, failAnswer.numCalls);
    assertEquals(3, debugAnswer.numCalls);
    assertEquals(3, finishAnswer.numCalls);
    // Scenario 4: Connect has last modified & we cannot set (1, 1);
    when(connection.getLastModified()).thenReturn(1L);
    when(file.setLastModified(anyLong())).then(setLastModifiedAnswerTrue);
    itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection);
    assertEquals(0, failAnswer.numCalls);
    assertEquals(3, debugAnswer.numCalls); // as before
    assertEquals(4, finishAnswer.numCalls);
}