ImageDiskStore { @Nullable public Uri save(@NonNull final Context context, @NonNull final byte[] bytes) { final Uri uri = generateUri(context, null); final File file = createFile(uri); if (file == null) { return null; } try { writeToFile(file, bytes); return uri; } catch (final IOException e) { LOG.error("Failed to write file", e); return null; } }  @Nullable Uri save(@NonNull final Context context, @NonNull final byte[] bytes); @Nullable Uri save(@NonNull final Context context, @NonNull final Uri fromUri); boolean update(@NonNull final Uri uri, @NonNull final byte[] bytes); void delete(@NonNull final Uri uri); static void clear(@NonNull final Context context);  }
@Test public void should_saveByteArray_toOwnFolder_inAppFilesDir() throws Exception { final ImageDiskStore imageDiskStore = new ImageDiskStore(); final Application appContext = getApplicationContext(); imageDiskStore.save(appContext, getTestJpeg()); final File storeDir = new File(appContext.getFilesDir(), ImageDiskStore.STORE_DIR); assertThat(storeDir.listFiles()).hasLength(1); }
@Test public void should_returnUri_toFile_ofSavedByteArray() throws Exception { final ImageDiskStore imageDiskStore = new ImageDiskStore(); final Application appContext = getApplicationContext(); final Uri uri = imageDiskStore.save(appContext, getTestJpeg()); final File storeDir = new File(appContext.getFilesDir(), ImageDiskStore.STORE_DIR); assertThat(storeDir.listFiles()[0].getAbsolutePath()).isEqualTo(uri.getPath()); }
@Test public void should_notUseExtension_whenSavingByteArray() throws Exception { final ImageDiskStore imageDiskStore = new ImageDiskStore(); final Application appContext = getApplicationContext(); imageDiskStore.save(appContext, getTestJpeg()); final File storeDir = new File(appContext.getFilesDir(), ImageDiskStore.STORE_DIR); assertThat(storeDir.listFiles()[0].getName()).doesNotContainMatch("^.+\\..+$"); }
@Test public void should_saveUri_toOwnFolder_inAppFilesDir() throws Exception { final ImageDiskStore imageDiskStore = new ImageDiskStore(); final Application appContext = getApplicationContext(); final Uri uri = getUriAndRegisterInputStream(appContext); imageDiskStore.save(appContext, uri); final File storeDir = new File(appContext.getFilesDir(), ImageDiskStore.STORE_DIR); assertThat(storeDir.listFiles()).hasLength(1); }
@Test public void should_preserveExtension_whenSavingUri() throws Exception { final ImageDiskStore imageDiskStore = new ImageDiskStore(); final Application appContext = getApplicationContext(); final Uri uri = getUriAndRegisterInputStream(appContext); imageDiskStore.save(appContext, uri); final File storeDir = new File(appContext.getFilesDir(), ImageDiskStore.STORE_DIR); assertThat(storeDir.listFiles()[0].getName()).endsWith(".jpg"); }
@Test public void should_returnUri_toFile_ofSavedUri() throws Exception { final ImageDiskStore imageDiskStore = new ImageDiskStore(); final Application appContext = getApplicationContext(); final Uri uri = getUriAndRegisterInputStream(appContext); final Uri resultUri = imageDiskStore.save(appContext, uri); final File storeDir = new File(appContext.getFilesDir(), ImageDiskStore.STORE_DIR); assertThat(storeDir.listFiles()[0].getAbsolutePath()).isEqualTo(resultUri.getPath()); }
@Test public void should_generateFilenames_basedOnTheCurrentTime_inMilliseconds() throws Exception { final ImageDiskStore imageDiskStore = new ImageDiskStore(); final Application appContext = getApplicationContext(); imageDiskStore.save(appContext, getTestJpeg()); final File storeDir = new File(appContext.getFilesDir(), ImageDiskStore.STORE_DIR); final String fileName = storeDir.listFiles()[0].getName(); final Calendar calendarFileNameDate = Calendar.getInstance(); calendarFileNameDate.setTimeInMillis(Long.parseLong(fileName)); final Calendar calendarNow = Calendar.getInstance(); calendarNow.get(Calendar.YEAR); assertThat(calendarFileNameDate.get(Calendar.YEAR)).isEqualTo( calendarNow.get(Calendar.YEAR)); assertThat(calendarFileNameDate.get(Calendar.MONTH)).isEqualTo( calendarNow.get(Calendar.MONTH)); assertThat(calendarFileNameDate.get(Calendar.DAY_OF_MONTH)).isEqualTo( calendarNow.get(Calendar.DAY_OF_MONTH)); assertThat(calendarFileNameDate.get(Calendar.HOUR)).isEqualTo( calendarNow.get(Calendar.HOUR)); assertThat(calendarFileNameDate.get(Calendar.MINUTE)).isEqualTo( calendarNow.get(Calendar.MINUTE)); }