From e251b77d75706720cf54d0ca37f48503d1f0013a Mon Sep 17 00:00:00 2001 From: Carlo Marinangeli Date: Tue, 17 Nov 2015 00:52:31 +0000 Subject: [PATCH 1/4] Added status --- .../main/java/com/orhanobut/hawk/Hawk.java | 43 +++++++- .../java/com/orhanobut/hawk/HawkBuilder.java | 36 +++++-- .../com/orhanobut/hawk/SqliteStorage.java | 3 +- .../java/com/orhanobut/hawk/HawkTest.java | 99 ++++++++++++++++--- 4 files changed, 161 insertions(+), 20 deletions(-) diff --git a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java index 7edbb61..27a2dbb 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java +++ b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java @@ -47,6 +47,11 @@ public static boolean put(String key, T value) { if (key == null) { throw new NullPointerException("Key cannot be null"); } + + if (!isInitialised()) { + return false; + } + //if the value is null, simply remove it if (value == null) { return remove(key); @@ -111,10 +116,15 @@ public static T get(String key) { if (key == null) { throw new NullPointerException("Key cannot be null"); } + if (!isInitialised()) { + return null; + } + String fullText = internal.getStorage().get(key); if (fullText == null) { return null; } + DataInfo dataInfo = DataHelper.getDataInfo(fullText); byte[] bytes = internal.getEncryption().decrypt(dataInfo.getCipherText()); @@ -165,7 +175,7 @@ public static Observable getObservable(String key) { * @param key of the data * @param defaultValue of the default value if the value doesn't exists * @param type of the data - * @return Observable + * @return Observable */ public static Observable getObservable(final String key, final T defaultValue) { Utils.checkRx(); @@ -212,6 +222,9 @@ public static Chain chain(int capacity) { * @return the size */ public static long count() { + if (!isInitialised()) { + return 0; + } return internal.getStorage().count(); } @@ -222,6 +235,9 @@ public static long count() { * @return true if clear is successful */ public static boolean clear() { + if (!isInitialised()) { + return false; + } return internal.getStorage().clear(); } @@ -232,6 +248,9 @@ public static boolean clear() { * @return true if remove is successful */ public static boolean remove(String key) { + if (!isInitialised()) { + return false; + } return internal.getStorage().remove(key); } @@ -242,6 +261,9 @@ public static boolean remove(String key) { * @return true if all removals are successful */ public static boolean remove(String... keys) { + if (!isInitialised()) { + return false; + } return internal.getStorage().remove(keys); } @@ -252,6 +274,9 @@ public static boolean remove(String... keys) { * @return true if it exists in the storage */ public static boolean contains(String key) { + if (!isInitialised()) { + return false; + } return internal.getStorage().contains(key); } @@ -261,6 +286,9 @@ public static boolean contains(String key) { * @return true if reset is successful */ public static boolean resetCrypto() { + if (!isInitialised()) { + return false; + } return internal.getEncryption().reset(); } @@ -271,6 +299,16 @@ public static LogLevel getLogLevel() { return internal.getLogLevel(); } + public static boolean isInitialised() { + return internal != null; + } + + private static void validate() { + if (!isInitialised()) { + throw new IllegalStateException("Hawk is not"); + } + } + /** * Provides the ability to chain put invocations: * Hawk.chain().put("foo", 0).put("bar", false).commit() @@ -300,6 +338,9 @@ public Chain put(String key, T value) { if (key == null) { throw new NullPointerException("Key cannot be null"); } + if (!isInitialised()) { + throw new IllegalStateException("Hawk has not been built"); + } String encodedText = zip(value); if (encodedText == null) { Log.d("HAWK", "Key : " + key + " is not added, encryption failed"); diff --git a/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java b/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java index 0e9e79d..aec4f3d 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java +++ b/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java @@ -5,6 +5,7 @@ import android.text.TextUtils; import com.google.gson.Gson; +import com.orhanobut.hawk.Hawk.Status; import rx.Observable; import rx.Subscriber; @@ -36,6 +37,7 @@ public class HawkBuilder { private Parser parser; private Encryption encryption; private Callback callback; + private Status status; public enum EncryptionMethod { HIGHEST, MEDIUM, NO_ENCRYPTION @@ -46,6 +48,7 @@ public HawkBuilder(Context context) { throw new NullPointerException("Context should not be null"); } this.context = context.getApplicationContext(); + this.status = Status.NOT_INITIALISED; } public HawkBuilder setEncryptionMethod(EncryptionMethod encryptionMethod) { @@ -128,11 +131,19 @@ Encryption getEncryption() { return encryption; } + public boolean isEncrypted() { + return encryptionMethod != EncryptionMethod.NO_ENCRYPTION; + } + + Status getStatus() { + return status; + } + private void validate() { if (getEncryptionMethod() == EncryptionMethod.HIGHEST) { if (TextUtils.isEmpty(getPassword())) { throw new IllegalStateException("Password cannot be null " + - "if encryption mode is highest"); + "if encryption mode is highest"); } } } @@ -140,7 +151,8 @@ private void validate() { public void build() { if (callback != null) { new Handler().post(new Runnable() { - @Override public void run() { + @Override + public void run() { try { startBuild(); callback.onSuccess(); @@ -155,9 +167,16 @@ public void build() { } private void startBuild() { - validate(); - setEncryption(); - Hawk.onHawkBuilt(this); + try { + validate(); + setEncryption(); + Hawk.onHawkBuilt(this); + + status = Status.INITIALISED; + } catch (Exception e) { + status = Status.NOT_INITIALISED; + throw e; + } } private void setEncryption() { @@ -206,15 +225,18 @@ public interface Callback { public Observable buildRx() { Utils.checkRx(); return Observable.defer(new Func0>() { - @Override public Observable call() { + @Override + public Observable call() { return Observable.create(new Observable.OnSubscribe() { - @Override public void call(Subscriber subscriber) { + @Override + public void call(Subscriber subscriber) { try { startBuild(); subscriber.onNext(true); subscriber.onCompleted(); } catch (Exception e) { subscriber.onError(e); + status = Status.NOT_INITIALISED; } } }); diff --git a/hawk/src/main/java/com/orhanobut/hawk/SqliteStorage.java b/hawk/src/main/java/com/orhanobut/hawk/SqliteStorage.java index c180e4c..d77f5a0 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/SqliteStorage.java +++ b/hawk/src/main/java/com/orhanobut/hawk/SqliteStorage.java @@ -139,7 +139,7 @@ public boolean delete(String... keys) { if (key == null) { continue; } - int count = db.delete(TABLE_NAME, COL_KEY + "='" + key + "'", null); + db.delete(TABLE_NAME, COL_KEY + "='" + key + "'", null); } db.setTransactionSuccessful(); } catch (Exception e) { @@ -167,6 +167,7 @@ public String get(String key) { return null; } String value = cursor.getString(1); + cursor.close(); db.close(); return value; } diff --git a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java index 1af57d5..965eff7 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java +++ b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java @@ -27,6 +27,7 @@ import rx.schedulers.Schedulers; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; import static org.assertj.core.api.Assertions.assertThat; @@ -350,17 +351,20 @@ public void init() { Hawk.getObservable(KEY) .observeOn(Schedulers.io()) .subscribe(new Subscriber() { - @Override public void onCompleted() { + @Override + public void onCompleted() { assertTrue(true); latch.countDown(); } - @Override public void onError(Throwable e) { + @Override + public void onError(Throwable e) { assertTrue(false); latch.countDown(); } - @Override public void onNext(String s) { + @Override + public void onNext(String s) { assertThat(s).isEqualTo("hawk"); } }); @@ -373,17 +377,20 @@ public void init() { Hawk.getObservable(KEY, "test") .observeOn(Schedulers.io()) .subscribe(new Subscriber() { - @Override public void onCompleted() { + @Override + public void onCompleted() { assertTrue(true); latch.countDown(); } - @Override public void onError(Throwable e) { + @Override + public void onError(Throwable e) { fail(); latch.countDown(); } - @Override public void onNext(String s) { + @Override + public void onNext(String s) { assertThat(s).isEqualTo("test"); } }); @@ -397,27 +404,32 @@ public void init() { .buildRx() .observeOn(Schedulers.io()) .concatMap(new Func1>() { - @Override public Observable call(Boolean aBoolean) { + @Override + public Observable call(Boolean aBoolean) { return Hawk.putObservable(KEY, "hawk"); } }) .concatMap(new Func1>() { - @Override public Observable call(Boolean aBoolean) { + @Override + public Observable call(Boolean aBoolean) { return Hawk.getObservable(KEY); } }) .subscribe(new Observer() { - @Override public void onCompleted() { + @Override + public void onCompleted() { assertTrue(true); latch.countDown(); } - @Override public void onError(Throwable throwable) { + @Override + public void onError(Throwable throwable) { assertTrue(false); latch.countDown(); } - @Override public void onNext(String storedValue) { + @Override + public void onNext(String storedValue) { assertEquals(storedValue, "hawk"); } }); @@ -425,4 +437,69 @@ public void init() { assertThat(latch.await(LATCH_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)).isTrue(); } + @Test public void statusNotInitialisedBeforeBuild() { + Hawk.init(context); + assertEquals(Hawk.getStatus(), Hawk.Status.NOT_INITIALISED); + } + + @Test public void statusInitialisedAfterBuild() { + Hawk.init(context).build(); + assertEquals(Hawk.getStatus(), Hawk.Status.INITIALISED); + } + + @Test public void testGetReturnsNullWhenNotInitialised() { + Hawk.init(context); + assertEquals(Hawk.get(KEY), null); + } + + @Test public void testGetReturnsDefaultWhenNotInitialised() { + Hawk.init(context); + String defaultValue = "default"; + assertEquals(Hawk.get(KEY, defaultValue), defaultValue); + } + + @Test public void testPutReturnsFalseWhenNotInitialised() { + Hawk.init(context); + assertFalse(Hawk.put(KEY, "value")); + } + + @Test public void testClearWhenNotInitialised() { + Hawk.init(context); + assertFalse(Hawk.clear()); + } + + @Test public void testContainsReturnsFalseWhenNotInitialised() { + Hawk.init(context); + assertFalse(Hawk.contains(KEY)); + } + + @Test public void testRemoveReturnsFalseWhenNotInitialised() { + Hawk.init(context); + assertFalse(Hawk.remove(KEY)); + } + + @Test public void testRemoveMultiKeysReturnsFalseWhenNotInitialised() { + Hawk.init(context); + assertFalse(Hawk.remove(KEY, KEY)); + } + + @Test public void testResetCryptoReturnsFalseWhenNotInitialised() { + Hawk.init(context); + assertFalse(Hawk.resetCrypto()); + } + + @Test public void testCountReturnsZeroWhenNotInitialised() { + Hawk.init(context); + assertEquals(Hawk.count(), 0); + } + + @Test public void testPutInChainThrowsExceptionWhenNotInitialised() { + Hawk.init(context); + try { + Hawk.chain().put(KEY, "value"); + fail("Did not throw an exception"); + }catch (IllegalStateException ise){ + assertThat(ise).hasMessage("Hawk has not been built"); + } + } } From d7f5de9995cb315feba88be938ac399600a7c163 Mon Sep 17 00:00:00 2001 From: Carlo Marinangeli Date: Mon, 30 Nov 2015 19:13:24 +0000 Subject: [PATCH 2/4] reformat and fix test --- .../java/com/orhanobut/hawk/HawkBuilder.java | 36 ++++------------- .../java/com/orhanobut/hawk/HawkTest.java | 39 +++++++------------ 2 files changed, 21 insertions(+), 54 deletions(-) diff --git a/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java b/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java index aec4f3d..0e9e79d 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java +++ b/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java @@ -5,7 +5,6 @@ import android.text.TextUtils; import com.google.gson.Gson; -import com.orhanobut.hawk.Hawk.Status; import rx.Observable; import rx.Subscriber; @@ -37,7 +36,6 @@ public class HawkBuilder { private Parser parser; private Encryption encryption; private Callback callback; - private Status status; public enum EncryptionMethod { HIGHEST, MEDIUM, NO_ENCRYPTION @@ -48,7 +46,6 @@ public HawkBuilder(Context context) { throw new NullPointerException("Context should not be null"); } this.context = context.getApplicationContext(); - this.status = Status.NOT_INITIALISED; } public HawkBuilder setEncryptionMethod(EncryptionMethod encryptionMethod) { @@ -131,19 +128,11 @@ Encryption getEncryption() { return encryption; } - public boolean isEncrypted() { - return encryptionMethod != EncryptionMethod.NO_ENCRYPTION; - } - - Status getStatus() { - return status; - } - private void validate() { if (getEncryptionMethod() == EncryptionMethod.HIGHEST) { if (TextUtils.isEmpty(getPassword())) { throw new IllegalStateException("Password cannot be null " + - "if encryption mode is highest"); + "if encryption mode is highest"); } } } @@ -151,8 +140,7 @@ private void validate() { public void build() { if (callback != null) { new Handler().post(new Runnable() { - @Override - public void run() { + @Override public void run() { try { startBuild(); callback.onSuccess(); @@ -167,16 +155,9 @@ public void run() { } private void startBuild() { - try { - validate(); - setEncryption(); - Hawk.onHawkBuilt(this); - - status = Status.INITIALISED; - } catch (Exception e) { - status = Status.NOT_INITIALISED; - throw e; - } + validate(); + setEncryption(); + Hawk.onHawkBuilt(this); } private void setEncryption() { @@ -225,18 +206,15 @@ public interface Callback { public Observable buildRx() { Utils.checkRx(); return Observable.defer(new Func0>() { - @Override - public Observable call() { + @Override public Observable call() { return Observable.create(new Observable.OnSubscribe() { - @Override - public void call(Subscriber subscriber) { + @Override public void call(Subscriber subscriber) { try { startBuild(); subscriber.onNext(true); subscriber.onCompleted(); } catch (Exception e) { subscriber.onError(e); - status = Status.NOT_INITIALISED; } } }); diff --git a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java index 965eff7..cae8c34 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java +++ b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java @@ -351,20 +351,17 @@ public void init() { Hawk.getObservable(KEY) .observeOn(Schedulers.io()) .subscribe(new Subscriber() { - @Override - public void onCompleted() { + @Override public void onCompleted() { assertTrue(true); latch.countDown(); } - @Override - public void onError(Throwable e) { + @Override public void onError(Throwable e) { assertTrue(false); latch.countDown(); } - @Override - public void onNext(String s) { + @Override public void onNext(String s) { assertThat(s).isEqualTo("hawk"); } }); @@ -377,20 +374,17 @@ public void onNext(String s) { Hawk.getObservable(KEY, "test") .observeOn(Schedulers.io()) .subscribe(new Subscriber() { - @Override - public void onCompleted() { + @Override public void onCompleted() { assertTrue(true); latch.countDown(); } - @Override - public void onError(Throwable e) { + @Override public void onError(Throwable e) { fail(); latch.countDown(); } - @Override - public void onNext(String s) { + @Override public void onNext(String s) { assertThat(s).isEqualTo("test"); } }); @@ -404,32 +398,27 @@ public void onNext(String s) { .buildRx() .observeOn(Schedulers.io()) .concatMap(new Func1>() { - @Override - public Observable call(Boolean aBoolean) { + @Override public Observable call(Boolean aBoolean) { return Hawk.putObservable(KEY, "hawk"); } }) .concatMap(new Func1>() { - @Override - public Observable call(Boolean aBoolean) { + @Override public Observable call(Boolean aBoolean) { return Hawk.getObservable(KEY); } }) .subscribe(new Observer() { - @Override - public void onCompleted() { + @Override public void onCompleted() { assertTrue(true); latch.countDown(); } - @Override - public void onError(Throwable throwable) { + @Override public void onError(Throwable throwable) { assertTrue(false); latch.countDown(); } - @Override - public void onNext(String storedValue) { + @Override public void onNext(String storedValue) { assertEquals(storedValue, "hawk"); } }); @@ -439,12 +428,12 @@ public void onNext(String storedValue) { @Test public void statusNotInitialisedBeforeBuild() { Hawk.init(context); - assertEquals(Hawk.getStatus(), Hawk.Status.NOT_INITIALISED); + assertFalse(Hawk.isInitialised()); } @Test public void statusInitialisedAfterBuild() { Hawk.init(context).build(); - assertEquals(Hawk.getStatus(), Hawk.Status.INITIALISED); + assertTrue(Hawk.isInitialised()); } @Test public void testGetReturnsNullWhenNotInitialised() { @@ -498,7 +487,7 @@ public void onNext(String storedValue) { try { Hawk.chain().put(KEY, "value"); fail("Did not throw an exception"); - }catch (IllegalStateException ise){ + } catch (IllegalStateException ise) { assertThat(ise).hasMessage("Hawk has not been built"); } } From 3321f172e1b4230aa32b63ae5efcfa84e16c0dd6 Mon Sep 17 00:00:00 2001 From: Carlo Marinangeli Date: Mon, 14 Dec 2015 13:43:27 +0100 Subject: [PATCH 3/4] refactor after comments. removed enum for status and throw exception when Hawk is not built. --- gradle/wrapper/gradle-wrapper.properties | 4 +- .../main/java/com/orhanobut/hawk/Hawk.java | 51 ++++------- .../main/java/com/orhanobut/hawk/Utils.java | 7 ++ .../java/com/orhanobut/hawk/HawkTest.java | 87 ++++++++++++------- 4 files changed, 81 insertions(+), 68 deletions(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 7e0e650..b7cebe0 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Nov 30 21:22:31 GMT 2015 +#Mon Dec 14 13:10:49 CET 2015 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip diff --git a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java index 27a2dbb..6f98c24 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java +++ b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java @@ -47,10 +47,7 @@ public static boolean put(String key, T value) { if (key == null) { throw new NullPointerException("Key cannot be null"); } - - if (!isInitialised()) { - return false; - } + Utils.validate(); //if the value is null, simply remove it if (value == null) { @@ -116,10 +113,7 @@ public static T get(String key) { if (key == null) { throw new NullPointerException("Key cannot be null"); } - if (!isInitialised()) { - return null; - } - + Utils.validate(); String fullText = internal.getStorage().get(key); if (fullText == null) { return null; @@ -222,9 +216,7 @@ public static Chain chain(int capacity) { * @return the size */ public static long count() { - if (!isInitialised()) { - return 0; - } + Utils.validate(); return internal.getStorage().count(); } @@ -235,9 +227,7 @@ public static long count() { * @return true if clear is successful */ public static boolean clear() { - if (!isInitialised()) { - return false; - } + Utils.validate(); return internal.getStorage().clear(); } @@ -248,9 +238,7 @@ public static boolean clear() { * @return true if remove is successful */ public static boolean remove(String key) { - if (!isInitialised()) { - return false; - } + Utils.validate(); return internal.getStorage().remove(key); } @@ -261,9 +249,7 @@ public static boolean remove(String key) { * @return true if all removals are successful */ public static boolean remove(String... keys) { - if (!isInitialised()) { - return false; - } + Utils.validate(); return internal.getStorage().remove(keys); } @@ -274,9 +260,7 @@ public static boolean remove(String... keys) { * @return true if it exists in the storage */ public static boolean contains(String key) { - if (!isInitialised()) { - return false; - } + Utils.validate(); return internal.getStorage().contains(key); } @@ -286,9 +270,7 @@ public static boolean contains(String key) { * @return true if reset is successful */ public static boolean resetCrypto() { - if (!isInitialised()) { - return false; - } + Utils.validate(); return internal.getEncryption().reset(); } @@ -299,16 +281,15 @@ public static LogLevel getLogLevel() { return internal.getLogLevel(); } - public static boolean isInitialised() { + /** + * Use this method to verify if Hawk is ready to be used. + * + * @return true if correctly initialised and built. False otherwise. + */ + public static boolean isBuilt() { return internal != null; } - private static void validate() { - if (!isInitialised()) { - throw new IllegalStateException("Hawk is not"); - } - } - /** * Provides the ability to chain put invocations: * Hawk.chain().put("foo", 0).put("bar", false).commit() @@ -338,9 +319,7 @@ public Chain put(String key, T value) { if (key == null) { throw new NullPointerException("Key cannot be null"); } - if (!isInitialised()) { - throw new IllegalStateException("Hawk has not been built"); - } + Utils.validate(); String encodedText = zip(value); if (encodedText == null) { Log.d("HAWK", "Key : " + key + " is not added, encryption failed"); diff --git a/hawk/src/main/java/com/orhanobut/hawk/Utils.java b/hawk/src/main/java/com/orhanobut/hawk/Utils.java index 08ea471..ad21106 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/Utils.java +++ b/hawk/src/main/java/com/orhanobut/hawk/Utils.java @@ -22,4 +22,11 @@ public static void checkRx() { "make sure that you have it in your dependencies"); } } + + public static void validate() { + if (!Hawk.isBuilt()) { + throw new IllegalStateException("Hawk is not built. " + + "Please call build() and wait the initialisation finishes."); + } + } } diff --git a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java index cae8c34..2bb65f0 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java +++ b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java @@ -57,7 +57,9 @@ public void init() { } @After public void tearDown() { - Hawk.clear(); + if (Hawk.isBuilt()) { + Hawk.clear(); + } } @Test public void initWithInvalidValues() { @@ -426,69 +428,94 @@ public void init() { assertThat(latch.await(LATCH_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)).isTrue(); } - @Test public void statusNotInitialisedBeforeBuild() { + @Test public void statusNotBuiltBeforeBuild() { Hawk.init(context); - assertFalse(Hawk.isInitialised()); + assertFalse(Hawk.isBuilt()); } - @Test public void statusInitialisedAfterBuild() { + @Test public void statusBuiltAfterBuild() { Hawk.init(context).build(); - assertTrue(Hawk.isInitialised()); - } - - @Test public void testGetReturnsNullWhenNotInitialised() { - Hawk.init(context); - assertEquals(Hawk.get(KEY), null); + assertTrue(Hawk.isBuilt()); } - @Test public void testGetReturnsDefaultWhenNotInitialised() { + @Test public void testGetThrowsExceptionWhenNotBuilt() { Hawk.init(context); - String defaultValue = "default"; - assertEquals(Hawk.get(KEY, defaultValue), defaultValue); + try { + Hawk.get(KEY); + fail("Did not throw an exception"); + } catch (IllegalStateException ignored) { + } } - @Test public void testPutReturnsFalseWhenNotInitialised() { + @Test public void testPutThrowsExceptionWhenNotBuilt() { Hawk.init(context); - assertFalse(Hawk.put(KEY, "value")); + try { + Hawk.put(KEY, "value"); + fail("Did not throw an exception"); + } catch (IllegalStateException ignored) { + } } - @Test public void testClearWhenNotInitialised() { + @Test public void testClearThrowsExceptionWhenNotBuilt() { Hawk.init(context); - assertFalse(Hawk.clear()); + try { + Hawk.clear(); + fail("Did not throw an exception"); + } catch (IllegalStateException ignored) { + } } - @Test public void testContainsReturnsFalseWhenNotInitialised() { + @Test public void testContainsThrowsExceptionWhenNotBuilt() { Hawk.init(context); - assertFalse(Hawk.contains(KEY)); + try { + Hawk.contains(KEY); + fail("Did not throw an exception"); + } catch (IllegalStateException ignored) { + } } - @Test public void testRemoveReturnsFalseWhenNotInitialised() { + @Test public void testRemoveThrowsExceptionWhenNotBuilt() { Hawk.init(context); - assertFalse(Hawk.remove(KEY)); + try { + Hawk.remove(KEY); + fail("Did not throw an exception"); + } catch (IllegalStateException ignored) { + } } - @Test public void testRemoveMultiKeysReturnsFalseWhenNotInitialised() { + @Test public void testRemoveMultiKeysThrowsExceptionWhenNotBuilt() { Hawk.init(context); - assertFalse(Hawk.remove(KEY, KEY)); + try { + Hawk.remove(KEY, KEY); + fail("Did not throw an exception"); + } catch (IllegalStateException ignored) { + } } - @Test public void testResetCryptoReturnsFalseWhenNotInitialised() { + @Test public void testResetCryptoThrowsExceptionWhenNotBuilt() { Hawk.init(context); - assertFalse(Hawk.resetCrypto()); + try { + Hawk.resetCrypto(); + fail("Did not throw an exception"); + } catch (IllegalStateException ignored) { + } } - @Test public void testCountReturnsZeroWhenNotInitialised() { + @Test public void testCountThrowsExceptionWhenNotBuilt() { Hawk.init(context); - assertEquals(Hawk.count(), 0); + try { + Hawk.count(); + fail("Did not throw an exception"); + } catch (IllegalStateException ignored) { + } } - @Test public void testPutInChainThrowsExceptionWhenNotInitialised() { + @Test public void testPutInChainThrowsExceptionWhenNotBuilt() { Hawk.init(context); try { Hawk.chain().put(KEY, "value"); fail("Did not throw an exception"); - } catch (IllegalStateException ise) { - assertThat(ise).hasMessage("Hawk has not been built"); + } catch (IllegalStateException ignored) { } } } From 45a9b91148c6d6041499713b46f0f0682203a462 Mon Sep 17 00:00:00 2001 From: Carlo Marinangeli Date: Tue, 15 Dec 2015 14:43:40 +0000 Subject: [PATCH 4/4] rename validate() method and removed junit assertions --- .../src/main/java/com/orhanobut/hawk/Hawk.java | 18 +++++++++--------- .../main/java/com/orhanobut/hawk/Utils.java | 2 +- .../test/java/com/orhanobut/hawk/HawkTest.java | 18 ++++++------------ 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java index 6f98c24..6e4b95d 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java +++ b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java @@ -47,7 +47,7 @@ public static boolean put(String key, T value) { if (key == null) { throw new NullPointerException("Key cannot be null"); } - Utils.validate(); + Utils.validateBuild(); //if the value is null, simply remove it if (value == null) { @@ -113,7 +113,7 @@ public static T get(String key) { if (key == null) { throw new NullPointerException("Key cannot be null"); } - Utils.validate(); + Utils.validateBuild(); String fullText = internal.getStorage().get(key); if (fullText == null) { return null; @@ -216,7 +216,7 @@ public static Chain chain(int capacity) { * @return the size */ public static long count() { - Utils.validate(); + Utils.validateBuild(); return internal.getStorage().count(); } @@ -227,7 +227,7 @@ public static long count() { * @return true if clear is successful */ public static boolean clear() { - Utils.validate(); + Utils.validateBuild(); return internal.getStorage().clear(); } @@ -238,7 +238,7 @@ public static boolean clear() { * @return true if remove is successful */ public static boolean remove(String key) { - Utils.validate(); + Utils.validateBuild(); return internal.getStorage().remove(key); } @@ -249,7 +249,7 @@ public static boolean remove(String key) { * @return true if all removals are successful */ public static boolean remove(String... keys) { - Utils.validate(); + Utils.validateBuild(); return internal.getStorage().remove(keys); } @@ -260,7 +260,7 @@ public static boolean remove(String... keys) { * @return true if it exists in the storage */ public static boolean contains(String key) { - Utils.validate(); + Utils.validateBuild(); return internal.getStorage().contains(key); } @@ -270,7 +270,7 @@ public static boolean contains(String key) { * @return true if reset is successful */ public static boolean resetCrypto() { - Utils.validate(); + Utils.validateBuild(); return internal.getEncryption().reset(); } @@ -319,7 +319,7 @@ public Chain put(String key, T value) { if (key == null) { throw new NullPointerException("Key cannot be null"); } - Utils.validate(); + Utils.validateBuild(); String encodedText = zip(value); if (encodedText == null) { Log.d("HAWK", "Key : " + key + " is not added, encryption failed"); diff --git a/hawk/src/main/java/com/orhanobut/hawk/Utils.java b/hawk/src/main/java/com/orhanobut/hawk/Utils.java index ad21106..d8a2908 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/Utils.java +++ b/hawk/src/main/java/com/orhanobut/hawk/Utils.java @@ -23,7 +23,7 @@ public static void checkRx() { } } - public static void validate() { + static void validateBuild() { if (!Hawk.isBuilt()) { throw new IllegalStateException("Hawk is not built. " + "Please call build() and wait the initialisation finishes."); diff --git a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java index 2bb65f0..226af4f 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java +++ b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java @@ -26,9 +26,6 @@ import rx.functions.Func1; import rx.schedulers.Schedulers; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; import static org.assertj.core.api.Assertions.assertThat; @@ -93,7 +90,7 @@ public void init() { assertThat(fooBar).isNotNull(); assertThat(fooBar.name).isEqualTo("hawk"); - assertTrue(Hawk.put("innerClass", new FooBar.InnerFoo())); + assertThat(Hawk.put("innerClass", new FooBar.InnerFoo())).isTrue(); FooBar.InnerFoo innerFoo = Hawk.get("innerClass"); assertThat(innerFoo).isNotNull(); assertThat(innerFoo.name).isEqualTo("hawk"); @@ -354,12 +351,11 @@ public void init() { .observeOn(Schedulers.io()) .subscribe(new Subscriber() { @Override public void onCompleted() { - assertTrue(true); latch.countDown(); } @Override public void onError(Throwable e) { - assertTrue(false); + fail(); latch.countDown(); } @@ -377,7 +373,6 @@ public void init() { .observeOn(Schedulers.io()) .subscribe(new Subscriber() { @Override public void onCompleted() { - assertTrue(true); latch.countDown(); } @@ -411,17 +406,16 @@ public void init() { }) .subscribe(new Observer() { @Override public void onCompleted() { - assertTrue(true); latch.countDown(); } @Override public void onError(Throwable throwable) { - assertTrue(false); + fail(); latch.countDown(); } @Override public void onNext(String storedValue) { - assertEquals(storedValue, "hawk"); + assertThat(storedValue).isEqualTo("hawk"); } }); @@ -430,12 +424,12 @@ public void init() { @Test public void statusNotBuiltBeforeBuild() { Hawk.init(context); - assertFalse(Hawk.isBuilt()); + assertThat(Hawk.isBuilt()).isFalse(); } @Test public void statusBuiltAfterBuild() { Hawk.init(context).build(); - assertTrue(Hawk.isBuilt()); + assertThat(Hawk.isBuilt()).isTrue(); } @Test public void testGetThrowsExceptionWhenNotBuilt() {