Skip to content
This repository has been archived by the owner on Aug 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #58 from daemontus/dir-existence-check
Browse files Browse the repository at this point in the history
Add checks for cache dir validity. Closes #57
  • Loading branch information
VictorAlbertos authored Sep 2, 2016
2 parents bc75fed + 72f660e commit e6fe319
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/io/rx_cache/internal/Locale.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface Locale {
String NOT_DATA_RETURN_WHEN_CALLING_OBSERVABLE_LOADER =
"The Loader provided did not return any data and there is not data to load from the Cache";
String REPOSITORY_DISK_ADAPTER_CAN_NOT_BE_NULL = "File cache directory can not be null";
String REPOSITORY_DISK_ADAPTER_DOES_NOT_EXIST = "File cache directory does not exist";
String REPOSITORY_DISK_ADAPTER_IS_NOT_WRITABLE = "File cache directory is not writable";
String JSON_CONVERTER_CAN_NOT_BE_NULL = "JsonConverter can not be null";
String NOT_OBSERVABLE_LOADER_FOUND = " requires an instance of type observable";
String JUST_ONE_INSTANCE = " requires just one instance of type ";
Expand Down
6 changes: 6 additions & 0 deletions runtime/src/main/java/io/rx_cache/internal/RxCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public RxCache persistence(File cacheDirectory, JolyglotGenerics jolyglot) {
if (cacheDirectory == null) {
throw new InvalidParameterException(Locale.REPOSITORY_DISK_ADAPTER_CAN_NOT_BE_NULL);
}
if (!cacheDirectory.exists()) {
throw new InvalidParameterException(Locale.REPOSITORY_DISK_ADAPTER_DOES_NOT_EXIST);
}
if (!cacheDirectory.canWrite()) {
throw new InvalidParameterException(Locale.REPOSITORY_DISK_ADAPTER_IS_NOT_WRITABLE);
}

if (jolyglot == null) {
throw new InvalidParameterException(Locale.JSON_CONVERTER_CAN_NOT_BE_NULL);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.rx_cache.internal;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.security.InvalidParameterException;

/**
* Created by daemontus on 02/09/16.
*/
public class RxCacheBuilderValidationTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test(expected = InvalidParameterException.class)
public void Cache_Directory_Null() {
new RxCache.Builder()
.persistence(null, Jolyglot$.newInstance());
}

@Test(expected = InvalidParameterException.class)
public void Jolyglot_Null() {
new RxCache.Builder()
.persistence(temporaryFolder.getRoot(), null);
}

@Test(expected = InvalidParameterException.class)
public void Cache_Directory_Not_Exist() {
File cacheDir = new File(temporaryFolder.getRoot(), "non_existent_folder");
new RxCache.Builder()
.persistence(cacheDir, Jolyglot$.newInstance());
}

@Test(expected = InvalidParameterException.class)
public void Cache_Directory_Not_Writable() {
File cacheDir = new File(temporaryFolder.getRoot(), "non_existent_folder");
if (!cacheDir.mkdirs()) {
throw new IllegalStateException("Cannot create temporary directory");
}
if (!cacheDir.setWritable(false, false)) {
throw new IllegalStateException("Cannot modify permissions");
}
new RxCache.Builder()
.persistence(cacheDir, Jolyglot$.newInstance());
}
}

0 comments on commit e6fe319

Please sign in to comment.