Search in sources :

Example 16 with FormsRepository

use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.

the class FormsRepositoryTest method getLatestByFormIdAndVersion_whenMultipleExist_returnsLatest.

@Test
public void getLatestByFormIdAndVersion_whenMultipleExist_returnsLatest() {
    Supplier<Long> mockClock = mock(Supplier.class);
    when(mockClock.get()).thenReturn(2L, 3L, 1L);
    FormsRepository formsRepository = buildSubject(mockClock);
    formsRepository.save(FormUtils.buildForm("1", "1", getFormFilesPath()).build());
    formsRepository.save(FormUtils.buildForm("1", "1", getFormFilesPath()).build());
    formsRepository.save(FormUtils.buildForm("1", "1", getFormFilesPath()).build());
    Form form = formsRepository.getLatestByFormIdAndVersion("1", "1");
    assertThat(form, notNullValue());
    assertThat(form.getDbId(), is(2L));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) Test(org.junit.Test)

Example 17 with FormsRepository

use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.

the class FormsRepositoryTest method delete_deletesFiles.

@Test
public void delete_deletesFiles() throws Exception {
    FormsRepository formsRepository = buildSubject();
    Form form = formsRepository.save(FormUtils.buildForm("id", "version", getFormFilesPath()).build());
    // FormRepository doesn't automatically create all form files
    File mediaDir = new File(form.getFormMediaPath());
    mediaDir.mkdir();
    File cacheFile = new File(form.getJrCacheFilePath());
    cacheFile.createNewFile();
    File formFile = new File(form.getFormFilePath());
    assertThat(formFile.exists(), is(true));
    assertThat(mediaDir.exists(), is(true));
    assertThat(cacheFile.exists(), is(true));
    formsRepository.delete(form.getDbId());
    assertThat(formFile.exists(), is(false));
    assertThat(mediaDir.exists(), is(false));
    assertThat(cacheFile.exists(), is(false));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) File(java.io.File) Test(org.junit.Test)

Example 18 with FormsRepository

use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.

the class FormsRepositoryTest method getOneByMd5Hash_returnsMatchingForm.

@Test
public void getOneByMd5Hash_returnsMatchingForm() {
    FormsRepository formsRepository = buildSubject();
    formsRepository.save(FormUtils.buildForm("id1", "version", getFormFilesPath()).build());
    Form form2 = formsRepository.save(FormUtils.buildForm("id2", "version", getFormFilesPath()).build());
    assertThat(formsRepository.getOneByMd5Hash(form2.getMD5Hash()), is(form2));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) Test(org.junit.Test)

Example 19 with FormsRepository

use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.

the class FormsRepositoryTest method save_whenFormHasId_updatesHash.

@Test
public void save_whenFormHasId_updatesHash() throws IOException {
    FormsRepository formsRepository = buildSubject();
    Form originalForm = formsRepository.save(FormUtils.buildForm("id", "version", getFormFilesPath()).displayName("original").build());
    String newFormBody = FormUtils.createXFormBody("id", "version", "A different title");
    File formFile = new File(originalForm.getFormFilePath());
    FileUtils.writeByteArrayToFile(formFile, newFormBody.getBytes());
    formsRepository.save(new Form.Builder(originalForm).displayName("changed").build());
    String expectedHash = Md5.getMd5Hash(formFile);
    assertThat(formsRepository.get(originalForm.getDbId()).getMD5Hash(), is(expectedHash));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) File(java.io.File) Test(org.junit.Test)

Example 20 with FormsRepository

use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.

the class FormsRepositoryTest method save_whenNoFormFilePath_explodes.

@Test(expected = Exception.class)
public void save_whenNoFormFilePath_explodes() {
    FormsRepository formsRepository = buildSubject();
    Form form = FormUtils.buildForm("id", "version", getFormFilesPath()).build();
    form = new Form.Builder(form).formFilePath(null).build();
    formsRepository.save(form);
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) Test(org.junit.Test)

Aggregations

FormsRepository (org.odk.collect.forms.FormsRepository)26 Form (org.odk.collect.forms.Form)21 Test (org.junit.Test)20 File (java.io.File)8 Instance (org.odk.collect.forms.instances.Instance)4 Intent (android.content.Intent)3 Uri (android.net.Uri)3 AsyncTask (android.os.AsyncTask)3 BroadcastReceiver (android.content.BroadcastReceiver)2 Context (android.content.Context)2 DialogInterface (android.content.DialogInterface)2 BUTTON_NEGATIVE (android.content.DialogInterface.BUTTON_NEGATIVE)2 BUTTON_POSITIVE (android.content.DialogInterface.BUTTON_POSITIVE)2 IntentFilter (android.content.IntentFilter)2 LocationManager (android.location.LocationManager)2 MediaPlayer (android.media.MediaPlayer)2 Build (android.os.Build)2 Bundle (android.os.Bundle)2 ContextMenu (android.view.ContextMenu)2 ContextMenuInfo (android.view.ContextMenu.ContextMenuInfo)2