use of org.odk.collect.utilities.Result in project collect by opendatakit.
the class FormSaveViewModelTest method createAnswerFile_whenThereIsAnError_returnsNull_andSetsAnswerFileErrorToFilePath.
@Test
public void createAnswerFile_whenThereIsAnError_returnsNull_andSetsAnswerFileErrorToFilePath() throws Exception {
File tempDir = Files.createTempDir();
tempDir.setWritable(false);
when(formController.getInstanceFile()).thenReturn(new File(tempDir + File.separator + "instance.xml"));
File externalFile = File.createTempFile("external", ".file");
LiveData<Result<File>> answerFile = viewModel.createAnswerFile(externalFile);
scheduler.runBackground();
assertThat(answerFile.getValue().getOrNull(), nullValue());
assertThat(viewModel.getAnswerFileError().getValue(), equalTo(externalFile.getAbsolutePath()));
}
use of org.odk.collect.utilities.Result in project collect by opendatakit.
the class FormSaveViewModelTest method createAnswerFile_forSameFile_returnsSameName.
@Test
public void createAnswerFile_forSameFile_returnsSameName() throws Exception {
File tempDir = Files.createTempDir();
when(formController.getInstanceFile()).thenReturn(new File(tempDir + File.separator + "instance.xml"));
File externalFile = File.createTempFile("external", ".file");
LiveData<Result<File>> fileName1 = viewModel.createAnswerFile(externalFile);
scheduler.runBackground();
LiveData<Result<File>> fileName2 = viewModel.createAnswerFile(externalFile);
scheduler.runBackground();
assertThat(fileName1.getValue().getOrNull(), is(fileName2.getValue().getOrNull()));
}
use of org.odk.collect.utilities.Result in project collect by opendatakit.
the class FormSaveViewModelTest method createAnswerFile_copiesFileToInstanceFolder_andReturnsNewName.
@Test
public void createAnswerFile_copiesFileToInstanceFolder_andReturnsNewName() throws Exception {
File tempDir = Files.createTempDir();
when(formController.getInstanceFile()).thenReturn(new File(tempDir + File.separator + "instance.xml"));
File externalFile = File.createTempFile("external", ".file");
LiveData<Result<File>> answerFile = viewModel.createAnswerFile(externalFile);
scheduler.runBackground();
assertThat(tempDir.listFiles().length, is(1));
assertThat(answerFile.getValue().getOrNull().getName(), is(tempDir.listFiles()[0].getName()));
}
use of org.odk.collect.utilities.Result in project collect by opendatakit.
the class FormSaveViewModel method createAnswerFile.
@Override
public LiveData<Result<File>> createAnswerFile(File file) {
MutableLiveData<Result<File>> liveData = new MutableLiveData<>(null);
isSavingAnswerFile.setValue(true);
scheduler.immediate(() -> {
String newFileHash = Md5.getMd5Hash(file);
String instanceDir = formController.getInstanceFile().getParent();
File[] answerFiles = new File(instanceDir).listFiles();
for (File answerFile : answerFiles) {
if (Md5.getMd5Hash(answerFile).equals(newFileHash)) {
return answerFile;
}
}
String fileName = file.getName();
String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
String newFileName = System.currentTimeMillis() + "." + extension;
String newFilePath = instanceDir + File.separator + newFileName;
try (InputStream inputStream = new FileInputStream(file)) {
try (OutputStream outputStream = new FileOutputStream(newFilePath)) {
IOUtils.copy(inputStream, outputStream);
}
} catch (IOException e) {
Timber.e(e);
return null;
}
return new File(newFilePath);
}, answerFile -> {
liveData.setValue(new Result<>(answerFile));
isSavingAnswerFile.setValue(false);
if (answerFile == null) {
answerFileError.setValue(file.getAbsolutePath());
}
});
return liveData;
}
Aggregations