Skip to content

Commit

Permalink
Merge pull request #110 from carlonzo/status
Browse files Browse the repository at this point in the history
Created status for Hawk
  • Loading branch information
orhanobut committed Dec 17, 2015
2 parents 93bba11 + 45a9b91 commit 2c46f25
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 14 deletions.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
22 changes: 21 additions & 1 deletion hawk/src/main/java/com/orhanobut/hawk/Hawk.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public static <T> boolean put(String key, T value) {
if (key == null) {
throw new NullPointerException("Key cannot be null");
}
Utils.validateBuild();

//if the value is null, simply remove it
if (value == null) {
return remove(key);
Expand Down Expand Up @@ -111,10 +113,12 @@ public static <T> T get(String key) {
if (key == null) {
throw new NullPointerException("Key cannot be null");
}
Utils.validateBuild();
String fullText = internal.getStorage().get(key);
if (fullText == null) {
return null;
}

DataInfo dataInfo = DataHelper.getDataInfo(fullText);
byte[] bytes = internal.getEncryption().decrypt(dataInfo.getCipherText());

Expand Down Expand Up @@ -165,7 +169,7 @@ public static <T> Observable<T> getObservable(String key) {
* @param key of the data
* @param defaultValue of the default value if the value doesn't exists
* @param <T> type of the data
* @return Observable</T>
* @return Observable<T>
*/
public static <T> Observable<T> getObservable(final String key, final T defaultValue) {
Utils.checkRx();
Expand Down Expand Up @@ -212,6 +216,7 @@ public static Chain chain(int capacity) {
* @return the size
*/
public static long count() {
Utils.validateBuild();
return internal.getStorage().count();
}

Expand All @@ -222,6 +227,7 @@ public static long count() {
* @return true if clear is successful
*/
public static boolean clear() {
Utils.validateBuild();
return internal.getStorage().clear();
}

Expand All @@ -232,6 +238,7 @@ public static boolean clear() {
* @return true if remove is successful
*/
public static boolean remove(String key) {
Utils.validateBuild();
return internal.getStorage().remove(key);
}

Expand All @@ -242,6 +249,7 @@ public static boolean remove(String key) {
* @return true if all removals are successful
*/
public static boolean remove(String... keys) {
Utils.validateBuild();
return internal.getStorage().remove(keys);
}

Expand All @@ -252,6 +260,7 @@ public static boolean remove(String... keys) {
* @return true if it exists in the storage
*/
public static boolean contains(String key) {
Utils.validateBuild();
return internal.getStorage().contains(key);
}

Expand All @@ -261,6 +270,7 @@ public static boolean contains(String key) {
* @return true if reset is successful
*/
public static boolean resetCrypto() {
Utils.validateBuild();
return internal.getEncryption().reset();
}

Expand All @@ -271,6 +281,15 @@ public static LogLevel getLogLevel() {
return internal.getLogLevel();
}

/**
* 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;
}

/**
* Provides the ability to chain put invocations:
* <code>Hawk.chain().put("foo", 0).put("bar", false).commit()</code>
Expand Down Expand Up @@ -300,6 +319,7 @@ public <T> Chain put(String key, T value) {
if (key == null) {
throw new NullPointerException("Key cannot be null");
}
Utils.validateBuild();
String encodedText = zip(value);
if (encodedText == null) {
Log.d("HAWK", "Key : " + key + " is not added, encryption failed");
Expand Down
3 changes: 2 additions & 1 deletion hawk/src/main/java/com/orhanobut/hawk/SqliteStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -167,6 +167,7 @@ public String get(String key) {
return null;
}
String value = cursor.getString(1);
cursor.close();
db.close();
return value;
}
Expand Down
7 changes: 7 additions & 0 deletions hawk/src/main/java/com/orhanobut/hawk/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ public static void checkRx() {
"make sure that you have it in your dependencies");
}
}

static void validateBuild() {
if (!Hawk.isBuilt()) {
throw new IllegalStateException("Hawk is not built. " +
"Please call build() and wait the initialisation finishes.");
}
}
}
107 changes: 97 additions & 10 deletions hawk/src/test/java/com/orhanobut/hawk/HawkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import rx.functions.Func1;
import rx.schedulers.Schedulers;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -56,7 +54,9 @@ public void init() {
}

@After public void tearDown() {
Hawk.clear();
if (Hawk.isBuilt()) {
Hawk.clear();
}
}

@Test public void initWithInvalidValues() {
Expand Down Expand Up @@ -90,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");
Expand Down Expand Up @@ -351,12 +351,11 @@ public void init() {
.observeOn(Schedulers.io())
.subscribe(new Subscriber<String>() {
@Override public void onCompleted() {
assertTrue(true);
latch.countDown();
}

@Override public void onError(Throwable e) {
assertTrue(false);
fail();
latch.countDown();
}

Expand All @@ -374,7 +373,6 @@ public void init() {
.observeOn(Schedulers.io())
.subscribe(new Subscriber<String>() {
@Override public void onCompleted() {
assertTrue(true);
latch.countDown();
}

Expand Down Expand Up @@ -408,21 +406,110 @@ public void init() {
})
.subscribe(new Observer<String>() {
@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");
}
});

assertThat(latch.await(LATCH_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)).isTrue();
}

@Test public void statusNotBuiltBeforeBuild() {
Hawk.init(context);
assertThat(Hawk.isBuilt()).isFalse();
}

@Test public void statusBuiltAfterBuild() {
Hawk.init(context).build();
assertThat(Hawk.isBuilt()).isTrue();
}

@Test public void testGetThrowsExceptionWhenNotBuilt() {
Hawk.init(context);
try {
Hawk.get(KEY);
fail("Did not throw an exception");
} catch (IllegalStateException ignored) {
}
}

@Test public void testPutThrowsExceptionWhenNotBuilt() {
Hawk.init(context);
try {
Hawk.put(KEY, "value");
fail("Did not throw an exception");
} catch (IllegalStateException ignored) {
}
}

@Test public void testClearThrowsExceptionWhenNotBuilt() {
Hawk.init(context);
try {
Hawk.clear();
fail("Did not throw an exception");
} catch (IllegalStateException ignored) {
}
}

@Test public void testContainsThrowsExceptionWhenNotBuilt() {
Hawk.init(context);
try {
Hawk.contains(KEY);
fail("Did not throw an exception");
} catch (IllegalStateException ignored) {
}
}

@Test public void testRemoveThrowsExceptionWhenNotBuilt() {
Hawk.init(context);
try {
Hawk.remove(KEY);
fail("Did not throw an exception");
} catch (IllegalStateException ignored) {
}
}

@Test public void testRemoveMultiKeysThrowsExceptionWhenNotBuilt() {
Hawk.init(context);
try {
Hawk.remove(KEY, KEY);
fail("Did not throw an exception");
} catch (IllegalStateException ignored) {
}
}

@Test public void testResetCryptoThrowsExceptionWhenNotBuilt() {
Hawk.init(context);
try {
Hawk.resetCrypto();
fail("Did not throw an exception");
} catch (IllegalStateException ignored) {
}
}

@Test public void testCountThrowsExceptionWhenNotBuilt() {
Hawk.init(context);
try {
Hawk.count();
fail("Did not throw an exception");
} catch (IllegalStateException ignored) {
}
}

@Test public void testPutInChainThrowsExceptionWhenNotBuilt() {
Hawk.init(context);
try {
Hawk.chain().put(KEY, "value");
fail("Did not throw an exception");
} catch (IllegalStateException ignored) {
}
}
}

0 comments on commit 2c46f25

Please sign in to comment.