@Test
public void handleError_withPreAllocateException() {
final DownloadTaskAdapter mockTaskAdapter = mock(DownloadTaskAdapter.class);
final ProgressAssist mockProgressAssist = mock(ProgressAssist.class);
when(mockTaskAdapter.getProgressAssist()).thenReturn(mockProgressAssist);
when(mockTaskAdapter.getRetryAssist()).thenReturn(null);
when(mockProgressAssist.getSofarBytes()).thenReturn(1L);
final Exception mockException = mock(PreAllocateException.class);
compatListenerAssist.handleError(mockTaskAdapter, mockException);
verify(callback, never()).retry(
any(DownloadTaskAdapter.class), any(Throwable.class), anyInt(), anyLong());
final ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
final ArgumentCaptor<DownloadTaskAdapter> taskCaptor = ArgumentCaptor
.forClass(DownloadTaskAdapter.class);
verify(callback).error(taskCaptor.capture(), throwableCaptor.capture());
assertThat(taskCaptor.getValue()).isEqualTo(mockTaskAdapter);
assertThat(throwableCaptor.getValue())
.isExactlyInstanceOf(FileDownloadOutOfSpaceException.class);
}
private void handlePreAllocate(long totalLength, String path)
throws IOException, IllegalAccessException {
FileDownloadOutputStream outputStream = null;
try {
if (totalLength != TOTAL_VALUE_IN_CHUNKED_RESOURCE) {
outputStream = FileDownloadUtils.createOutputStream(model.getTempFilePath());
final long breakpointBytes = new File(path).length();
final long requiredSpaceBytes = totalLength - breakpointBytes;
final long freeSpaceBytes = FileDownloadUtils.getFreeSpaceBytes(path);
if (freeSpaceBytes < requiredSpaceBytes) {
// throw a out of space exception.
throw new FileDownloadOutOfSpaceException(freeSpaceBytes,
requiredSpaceBytes, breakpointBytes);
} else if (!FileDownloadProperties.getImpl().fileNonPreAllocation) {
// pre allocate.
outputStream.setLength(totalLength);
}
}
} finally {
if (outputStream != null) outputStream.close();
}
}