fix issue #76, add additional create(FileSystem) within SourcePersisterFactory, some unit tests for SourcePersister (#77)
This commit is contained in:
parent
2ad00e4968
commit
d31b8c5f44
3 changed files with 85 additions and 1 deletions
|
@ -29,7 +29,7 @@ public class SourcePersister implements Persister<BufferedSource> {
|
|||
|
||||
@Inject
|
||||
public SourcePersister(FileSystem fileSystem) {
|
||||
this.sourceFileReader = new SourceFileReader(fileSystem);
|
||||
sourceFileReader = new SourceFileReader(fileSystem);
|
||||
sourceFileWriter = new SourceFileWriter(fileSystem);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.nytimes.android.external.fs;
|
|||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.nytimes.android.external.fs.filesystem.FileSystem;
|
||||
import com.nytimes.android.external.fs.filesystem.FileSystemFactory;
|
||||
import com.nytimes.android.external.store.base.Persister;
|
||||
|
||||
|
@ -31,4 +32,18 @@ public final class SourcePersisterFactory {
|
|||
}
|
||||
return new SourcePersister(FileSystemFactory.create(root));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link BufferedSource} persister with the provided fileSystem as the root of the
|
||||
* persistence {@link com.nytimes.android.external.fs.filesystem.FileSystem}.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@NonNull
|
||||
public static Persister<BufferedSource> create(@NonNull FileSystem fileSystem) throws IOException {
|
||||
if (fileSystem == null) {
|
||||
throw new IllegalArgumentException("fileSystem cannot be null.");
|
||||
}
|
||||
return new SourcePersister(fileSystem);
|
||||
}
|
||||
}
|
||||
|
|
69
filesystem/src/test/java/com/nytimes/android/external/fs/SourcePersisterTest.java
vendored
Normal file
69
filesystem/src/test/java/com/nytimes/android/external/fs/SourcePersisterTest.java
vendored
Normal file
|
@ -0,0 +1,69 @@
|
|||
package com.nytimes.android.external.fs;
|
||||
|
||||
import com.nytimes.android.external.fs.filesystem.FileSystem;
|
||||
import com.nytimes.android.external.store.base.impl.BarCode;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import okio.BufferedSource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class SourcePersisterTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@Mock
|
||||
FileSystem fileSystem;
|
||||
@Mock
|
||||
BufferedSource bufferedSource;
|
||||
|
||||
private SourcePersister sourcePersister;
|
||||
private final BarCode simple = new BarCode("type", "key");
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
sourcePersister = new SourcePersister(fileSystem);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readExists() throws FileNotFoundException {
|
||||
when(fileSystem.exists(SourcePersister.pathForBarcode(simple)))
|
||||
.thenReturn(true);
|
||||
when(fileSystem.read(SourcePersister.pathForBarcode(simple))).thenReturn(bufferedSource);
|
||||
|
||||
BufferedSource returnedValue = sourcePersister.read(simple).toBlocking().single();
|
||||
assertThat(returnedValue).isEqualTo(bufferedSource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readDoesNotExist() throws FileNotFoundException {
|
||||
expectedException.expect(NoSuchElementException.class);
|
||||
when(fileSystem.exists(SourcePersister.pathForBarcode(simple)))
|
||||
.thenReturn(false);
|
||||
|
||||
sourcePersister.read(simple).toBlocking().single();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void write() throws IOException {
|
||||
assertThat(sourcePersister.write(simple, bufferedSource).toBlocking().single()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathForBarcode() {
|
||||
assertThat(SourcePersister.pathForBarcode(simple)).isEqualTo("typekey");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue