Skip to content

Commit

Permalink
refactor: remove useless code on process-file-strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacksgong committed Apr 27, 2018
1 parent b4d90a5 commit f2bb8f3
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,15 @@
package com.liulishuo.okdownload.core.file;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.liulishuo.okdownload.DownloadTask;
import com.liulishuo.okdownload.OkDownload;
import com.liulishuo.okdownload.core.breakpoint.BlockInfo;
import com.liulishuo.okdownload.core.breakpoint.BreakpointInfo;
import com.liulishuo.okdownload.core.breakpoint.DownloadStore;
import com.liulishuo.okdownload.core.cause.ResumeFailedCause;
import com.liulishuo.okdownload.core.dispatcher.CallbackDispatcher;
import com.liulishuo.okdownload.core.download.DownloadStrategy;

import java.io.File;
import java.io.IOException;

import static com.liulishuo.okdownload.core.cause.ResumeFailedCause.FILE_NOT_EXIST;
import static com.liulishuo.okdownload.core.cause.ResumeFailedCause.INFO_DIRTY;
import static com.liulishuo.okdownload.core.cause.ResumeFailedCause.OUTPUT_STREAM_NOT_SUPPORT;

public class ProcessFileStrategy {
private final FileLock fileLock = new FileLock();

Expand Down Expand Up @@ -65,111 +56,8 @@ public void discardProcess(@NonNull DownloadTask task) throws IOException {
return fileLock;
}

public void discardOldFile(@NonNull File oldFile) {
oldFile.delete();
}

/**
* @see DownloadStrategy#resumeAvailableResponseCheck
*/
public ResumeAvailableLocalCheck resumeAvailableLocalCheck(DownloadTask task,
BreakpointInfo info) {
return new ResumeAvailableLocalCheck(task, info);
}

public boolean isPreAllocateLength() {
// if support seek, enable pre-allocate length.
return OkDownload.with().outputStreamFactory().supportSeek();
}

public static class ResumeAvailableLocalCheck {
Boolean isAvailable;
boolean fileExist;
boolean infoRight;
boolean outputStreamSupport;
private final DownloadTask task;
private final BreakpointInfo info;

protected ResumeAvailableLocalCheck(DownloadTask task, BreakpointInfo info) {
this.task = task;
this.info = info;
}

public boolean isInfoRightToResume() {
final int blockCount = info.getBlockCount();

if (blockCount <= 0) return false;
if (info.isChunked()) return false;
if (info.getFile() == null) return false;
final File fileOnTask = task.getFile();
if (!info.getFile().equals(fileOnTask)) return false;

for (int i = 0; i < blockCount; i++) {
BlockInfo blockInfo = info.getBlock(i);
if (blockInfo.getContentLength() <= 0) return false;
}

return true;
}

public boolean isOutputStreamSupportResume() {
final boolean supportSeek = OkDownload.with().outputStreamFactory().supportSeek();
if (supportSeek) return true;

if (info.getBlockCount() != 1) return false;
if (OkDownload.with().processFileStrategy().isPreAllocateLength()) return false;

return true;
}

public boolean isFileExistToResume() {
final File file = task.getFile();
return file != null && file.exists();
}

public boolean isAvailable() {
checkIfNeed();
return this.isAvailable;
}

private void checkIfNeed() {
if (isAvailable == null) {
fileExist = isFileExistToResume();
infoRight = isInfoRightToResume();
outputStreamSupport = isOutputStreamSupportResume();
isAvailable = infoRight && fileExist && outputStreamSupport;
}
}

@Nullable public ResumeFailedCause getCause() {
checkIfNeed();

if (!infoRight) {
return INFO_DIRTY;
} else if (!fileExist) {
return FILE_NOT_EXIST;
} else if (!outputStreamSupport) {
return OUTPUT_STREAM_NOT_SUPPORT;
}

return null;
}

public void callbackCause() {
checkIfNeed();

final CallbackDispatcher dispatcher = OkDownload.with().callbackDispatcher();
if (isAvailable) {
dispatcher.dispatch().downloadFromBreakpoint(task, info);
} else {
final ResumeFailedCause failedCause = getCause();
if (failedCause == null) {
throw new IllegalStateException();
} else {
dispatcher.dispatch().downloadFromBeginning(task, info, failedCause);
}

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

package com.liulishuo.okdownload.core.file;

import com.liulishuo.okdownload.DownloadListener;
import com.liulishuo.okdownload.DownloadTask;
import com.liulishuo.okdownload.OkDownload;
import com.liulishuo.okdownload.core.breakpoint.BlockInfo;
import com.liulishuo.okdownload.core.breakpoint.BreakpointInfo;

import org.junit.After;
import org.junit.Before;
Expand All @@ -30,33 +26,19 @@
import java.io.File;
import java.io.IOException;

import static com.liulishuo.okdownload.TestUtils.mockOkDownload;
import static com.liulishuo.okdownload.core.cause.ResumeFailedCause.FILE_NOT_EXIST;
import static com.liulishuo.okdownload.core.cause.ResumeFailedCause.INFO_DIRTY;
import static com.liulishuo.okdownload.core.cause.ResumeFailedCause.OUTPUT_STREAM_NOT_SUPPORT;
import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

public class ProcessFileStrategyTest {
private ProcessFileStrategy strategy;

private ProcessFileStrategy.ResumeAvailableLocalCheck localCheck;

@Mock private DownloadTask task;
@Mock private BreakpointInfo info;
private final File existFile = new File("./exist-path");

@Before
public void setup() throws IOException {
initMocks(this);
strategy = new ProcessFileStrategy();
localCheck = spy(strategy.resumeAvailableLocalCheck(task, info));

existFile.createNewFile();
}
Expand All @@ -73,151 +55,4 @@ public void discardProcess() throws IOException {
strategy.discardProcess(task);
// nothing need to test.
}

@Test
public void resumeAvailableLocalCheck() throws IOException {
mockOkDownload();
final DownloadListener listener = OkDownload.with().callbackDispatcher().dispatch();

localCheck.isAvailable = false;
localCheck.fileExist = true;
localCheck.infoRight = true;

// output stream not support
localCheck.outputStreamSupport = false;
localCheck.callbackCause();
verify(listener).downloadFromBeginning(eq(task), eq(info), eq(OUTPUT_STREAM_NOT_SUPPORT));

// file not exist
localCheck.fileExist = false;
localCheck.callbackCause();
verify(listener).downloadFromBeginning(eq(task), eq(info), eq(FILE_NOT_EXIST));

// info not right
localCheck.infoRight = false;
localCheck.callbackCause();
verify(listener).downloadFromBeginning(eq(task), eq(info), eq(INFO_DIRTY));

// available.
localCheck.isAvailable = true;
localCheck.callbackCause();
verify(listener).downloadFromBreakpoint(eq(task), eq(info));
}

@Test
public void isInfoRightToResume() {
when(task.getFile()).thenReturn(existFile);
when(info.getFile()).thenReturn(existFile);
when(info.getBlockCount()).thenReturn(1);
final BlockInfo blockInfo = mock(BlockInfo.class);
when(info.getBlock(0)).thenReturn(blockInfo);
when(blockInfo.getContentLength()).thenReturn(1L);
assertThat(localCheck.isInfoRightToResume()).isTrue();

// there is a block with the value of content-length is 0.
when(blockInfo.getContentLength()).thenReturn(0L);
assertThat(localCheck.isInfoRightToResume()).isFalse();
when(blockInfo.getContentLength()).thenReturn(1L);

// the filename in task is not equal to the filename in info.
when(task.getFile()).thenReturn(null);
assertThat(localCheck.isInfoRightToResume()).isFalse();
when(task.getFile()).thenReturn(existFile);

// the file path in info is null.
when(info.getFile()).thenReturn(null);
assertThat(localCheck.isInfoRightToResume()).isFalse();
when(info.getFile()).thenReturn(existFile);

// is chunked
when(info.isChunked()).thenReturn(true);
assertThat(localCheck.isInfoRightToResume()).isFalse();
when(info.isChunked()).thenReturn(false);

// there isn't any block in info.
when(info.getBlockCount()).thenReturn(0);
assertThat(localCheck.isInfoRightToResume()).isFalse();
}

@Test
public void isOutputStreamSupportResume() throws IOException {
mockOkDownload();

final DownloadOutputStream.Factory outputStreamFactory = OkDownload.with()
.outputStreamFactory();
final ProcessFileStrategy strategyOnOkDownload = OkDownload.with().processFileStrategy();

// support seek
when(outputStreamFactory.supportSeek()).thenReturn(true);
assertThat(localCheck.isOutputStreamSupportResume()).isTrue();

// not support seek, but only 1 block and there isn't pre-allocation length.
when(outputStreamFactory.supportSeek()).thenReturn(false);
when(info.getBlockCount()).thenReturn(1);
when(strategyOnOkDownload.isPreAllocateLength()).thenReturn(false);
assertThat(localCheck.isOutputStreamSupportResume()).isTrue();

// not support seek, only 1 block, but there is pre-allocation length, so we can't append
// with it
when(info.getBlockCount()).thenReturn(1);
doReturn(true).when(strategyOnOkDownload).isPreAllocateLength();
doReturn(false).when(outputStreamFactory).supportSeek();
assertThat(localCheck.isOutputStreamSupportResume()).isFalse();


// not support seek, there is pre-allocation length, but there is more than on block count.
doReturn(false).when(strategyOnOkDownload).isPreAllocateLength();
doReturn(false).when(outputStreamFactory).supportSeek();
when(info.getBlockCount()).thenReturn(2);
assertThat(localCheck.isOutputStreamSupportResume()).isFalse();
}

@Test
public void isFileExistToResume() {
when(task.getFile()).thenReturn(existFile);
assertThat(localCheck.isFileExistToResume()).isTrue();

when(task.getFile()).thenReturn(null);
assertThat(localCheck.isFileExistToResume()).isFalse();

when(task.getFile()).thenReturn(new File("no-exist"));
assertThat(localCheck.isFileExistToResume()).isFalse();
}

@Test
public void isAvailable_allTrue() {
doReturn(true).when(localCheck).isFileExistToResume();
doReturn(true).when(localCheck).isInfoRightToResume();
doReturn(true).when(localCheck).isOutputStreamSupportResume();

assertThat(localCheck.isAvailable()).isTrue();
}

@Test
public void isAvailable_fileNotExist() {
doReturn(false).when(localCheck).isFileExistToResume();
doReturn(true).when(localCheck).isInfoRightToResume();
doReturn(true).when(localCheck).isOutputStreamSupportResume();

assertThat(localCheck.isAvailable()).isFalse();
}


@Test
public void isAvailable_infoNotRight() {
doReturn(true).when(localCheck).isFileExistToResume();
doReturn(false).when(localCheck).isInfoRightToResume();
doReturn(true).when(localCheck).isOutputStreamSupportResume();

assertThat(localCheck.isAvailable()).isFalse();
}

@Test
public void isAvailable_outputStreamNotSupport() {
doReturn(true).when(localCheck).isFileExistToResume();
doReturn(true).when(localCheck).isInfoRightToResume();
doReturn(false).when(localCheck).isOutputStreamSupportResume();

assertThat(localCheck.isAvailable()).isFalse();
}
}

0 comments on commit f2bb8f3

Please sign in to comment.