From ee3a960a253e02dc424ddf49e00a5621a6292287 Mon Sep 17 00:00:00 2001 From: Samuel Pastva Date: Fri, 2 Sep 2016 13:59:55 +0200 Subject: [PATCH 1/3] Add checks for cache dir validity --- .../java/io/rx_cache/internal/Locale.java | 2 + .../java/io/rx_cache/internal/RxCache.java | 6 +++ .../rx_cache/internal/InvalidUsageTest.java | 51 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 runtime/src/test/java/io/rx_cache/internal/InvalidUsageTest.java diff --git a/core/src/main/java/io/rx_cache/internal/Locale.java b/core/src/main/java/io/rx_cache/internal/Locale.java index 22ba86b..95c56c6 100644 --- a/core/src/main/java/io/rx_cache/internal/Locale.java +++ b/core/src/main/java/io/rx_cache/internal/Locale.java @@ -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 "; diff --git a/runtime/src/main/java/io/rx_cache/internal/RxCache.java b/runtime/src/main/java/io/rx_cache/internal/RxCache.java index a6f36b4..88c5e9c 100644 --- a/runtime/src/main/java/io/rx_cache/internal/RxCache.java +++ b/runtime/src/main/java/io/rx_cache/internal/RxCache.java @@ -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); diff --git a/runtime/src/test/java/io/rx_cache/internal/InvalidUsageTest.java b/runtime/src/test/java/io/rx_cache/internal/InvalidUsageTest.java new file mode 100644 index 0000000..1099204 --- /dev/null +++ b/runtime/src/test/java/io/rx_cache/internal/InvalidUsageTest.java @@ -0,0 +1,51 @@ +package io.rx_cache.internal; + +import org.junit.ClassRule; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runners.MethodSorters; + +import java.io.File; +import java.security.InvalidParameterException; + +/** + * Created by daemontus on 02/09/16. + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class InvalidUsageTest { + @ClassRule + public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test(expected = InvalidParameterException.class) + public void _00_Cache_Directory_Null() { + new RxCache.Builder() + .persistence(null, Jolyglot$.newInstance()); + } + + @Test(expected = InvalidParameterException.class) + public void _01_Jolyglot_Null() { + new RxCache.Builder() + .persistence(temporaryFolder.getRoot(), null); + } + + @Test(expected = InvalidParameterException.class) + public void _00_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 _00_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()); + } +} From cd201ace7b6893f3b6342a1db8854bc893114ff1 Mon Sep 17 00:00:00 2001 From: Samuel Pastva Date: Fri, 2 Sep 2016 15:18:21 +0200 Subject: [PATCH 2/3] Remove unnecessary annotations and method numbers --- ....java => RxCacheBuilderValidationTest.java} | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) rename runtime/src/test/java/io/rx_cache/internal/{InvalidUsageTest.java => RxCacheBuilderValidationTest.java} (73%) diff --git a/runtime/src/test/java/io/rx_cache/internal/InvalidUsageTest.java b/runtime/src/test/java/io/rx_cache/internal/RxCacheBuilderValidationTest.java similarity index 73% rename from runtime/src/test/java/io/rx_cache/internal/InvalidUsageTest.java rename to runtime/src/test/java/io/rx_cache/internal/RxCacheBuilderValidationTest.java index 1099204..d45cfea 100644 --- a/runtime/src/test/java/io/rx_cache/internal/InvalidUsageTest.java +++ b/runtime/src/test/java/io/rx_cache/internal/RxCacheBuilderValidationTest.java @@ -1,10 +1,7 @@ package io.rx_cache.internal; -import org.junit.ClassRule; -import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.junit.runners.MethodSorters; import java.io.File; import java.security.InvalidParameterException; @@ -12,32 +9,31 @@ /** * Created by daemontus on 02/09/16. */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class InvalidUsageTest { - @ClassRule - public static TemporaryFolder temporaryFolder = new TemporaryFolder(); +public class RxCacheBuilderValidationTest { + + private TemporaryFolder temporaryFolder = new TemporaryFolder(); @Test(expected = InvalidParameterException.class) - public void _00_Cache_Directory_Null() { + public void Cache_Directory_Null() { new RxCache.Builder() .persistence(null, Jolyglot$.newInstance()); } @Test(expected = InvalidParameterException.class) - public void _01_Jolyglot_Null() { + public void Jolyglot_Null() { new RxCache.Builder() .persistence(temporaryFolder.getRoot(), null); } @Test(expected = InvalidParameterException.class) - public void _00_Cache_Directory_Not_Exist() { + 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 _00_Cache_Directory_Not_Writable() { + 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"); From 72f660ead4d76863b7f6565089a46599fa0b4bf8 Mon Sep 17 00:00:00 2001 From: Samuel Pastva Date: Fri, 2 Sep 2016 15:41:39 +0200 Subject: [PATCH 3/3] Fix temporary folder usage --- .../io/rx_cache/internal/RxCacheBuilderValidationTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/runtime/src/test/java/io/rx_cache/internal/RxCacheBuilderValidationTest.java b/runtime/src/test/java/io/rx_cache/internal/RxCacheBuilderValidationTest.java index d45cfea..2ea44f9 100644 --- a/runtime/src/test/java/io/rx_cache/internal/RxCacheBuilderValidationTest.java +++ b/runtime/src/test/java/io/rx_cache/internal/RxCacheBuilderValidationTest.java @@ -1,5 +1,6 @@ package io.rx_cache.internal; +import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -11,7 +12,8 @@ */ public class RxCacheBuilderValidationTest { - private TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); @Test(expected = InvalidParameterException.class) public void Cache_Directory_Null() {