Skip to content

Commit 2c46f25

Browse files
committed
Merge pull request #110 from carlonzo/status
Created status for Hawk
2 parents 93bba11 + 45a9b91 commit 2c46f25

File tree

5 files changed

+129
-14
lines changed

5 files changed

+129
-14
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Mon Nov 30 21:22:31 GMT 2015
1+
#Mon Dec 14 13:10:49 CET 2015
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip

hawk/src/main/java/com/orhanobut/hawk/Hawk.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public static <T> boolean put(String key, T value) {
4747
if (key == null) {
4848
throw new NullPointerException("Key cannot be null");
4949
}
50+
Utils.validateBuild();
51+
5052
//if the value is null, simply remove it
5153
if (value == null) {
5254
return remove(key);
@@ -111,10 +113,12 @@ public static <T> T get(String key) {
111113
if (key == null) {
112114
throw new NullPointerException("Key cannot be null");
113115
}
116+
Utils.validateBuild();
114117
String fullText = internal.getStorage().get(key);
115118
if (fullText == null) {
116119
return null;
117120
}
121+
118122
DataInfo dataInfo = DataHelper.getDataInfo(fullText);
119123
byte[] bytes = internal.getEncryption().decrypt(dataInfo.getCipherText());
120124

@@ -165,7 +169,7 @@ public static <T> Observable<T> getObservable(String key) {
165169
* @param key of the data
166170
* @param defaultValue of the default value if the value doesn't exists
167171
* @param <T> type of the data
168-
* @return Observable</T>
172+
* @return Observable<T>
169173
*/
170174
public static <T> Observable<T> getObservable(final String key, final T defaultValue) {
171175
Utils.checkRx();
@@ -212,6 +216,7 @@ public static Chain chain(int capacity) {
212216
* @return the size
213217
*/
214218
public static long count() {
219+
Utils.validateBuild();
215220
return internal.getStorage().count();
216221
}
217222

@@ -222,6 +227,7 @@ public static long count() {
222227
* @return true if clear is successful
223228
*/
224229
public static boolean clear() {
230+
Utils.validateBuild();
225231
return internal.getStorage().clear();
226232
}
227233

@@ -232,6 +238,7 @@ public static boolean clear() {
232238
* @return true if remove is successful
233239
*/
234240
public static boolean remove(String key) {
241+
Utils.validateBuild();
235242
return internal.getStorage().remove(key);
236243
}
237244

@@ -242,6 +249,7 @@ public static boolean remove(String key) {
242249
* @return true if all removals are successful
243250
*/
244251
public static boolean remove(String... keys) {
252+
Utils.validateBuild();
245253
return internal.getStorage().remove(keys);
246254
}
247255

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

@@ -261,6 +270,7 @@ public static boolean contains(String key) {
261270
* @return true if reset is successful
262271
*/
263272
public static boolean resetCrypto() {
273+
Utils.validateBuild();
264274
return internal.getEncryption().reset();
265275
}
266276

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

284+
/**
285+
* Use this method to verify if Hawk is ready to be used.
286+
*
287+
* @return true if correctly initialised and built. False otherwise.
288+
*/
289+
public static boolean isBuilt() {
290+
return internal != null;
291+
}
292+
274293
/**
275294
* Provides the ability to chain put invocations:
276295
* <code>Hawk.chain().put("foo", 0).put("bar", false).commit()</code>
@@ -300,6 +319,7 @@ public <T> Chain put(String key, T value) {
300319
if (key == null) {
301320
throw new NullPointerException("Key cannot be null");
302321
}
322+
Utils.validateBuild();
303323
String encodedText = zip(value);
304324
if (encodedText == null) {
305325
Log.d("HAWK", "Key : " + key + " is not added, encryption failed");

hawk/src/main/java/com/orhanobut/hawk/SqliteStorage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public boolean delete(String... keys) {
139139
if (key == null) {
140140
continue;
141141
}
142-
int count = db.delete(TABLE_NAME, COL_KEY + "='" + key + "'", null);
142+
db.delete(TABLE_NAME, COL_KEY + "='" + key + "'", null);
143143
}
144144
db.setTransactionSuccessful();
145145
} catch (Exception e) {
@@ -167,6 +167,7 @@ public String get(String key) {
167167
return null;
168168
}
169169
String value = cursor.getString(1);
170+
cursor.close();
170171
db.close();
171172
return value;
172173
}

hawk/src/main/java/com/orhanobut/hawk/Utils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ public static void checkRx() {
2222
"make sure that you have it in your dependencies");
2323
}
2424
}
25+
26+
static void validateBuild() {
27+
if (!Hawk.isBuilt()) {
28+
throw new IllegalStateException("Hawk is not built. " +
29+
"Please call build() and wait the initialisation finishes.");
30+
}
31+
}
2532
}

hawk/src/test/java/com/orhanobut/hawk/HawkTest.java

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import rx.functions.Func1;
2727
import rx.schedulers.Schedulers;
2828

29-
import static junit.framework.Assert.assertEquals;
30-
import static junit.framework.Assert.assertTrue;
3129
import static junit.framework.Assert.fail;
3230
import static org.assertj.core.api.Assertions.assertThat;
3331

@@ -56,7 +54,9 @@ public void init() {
5654
}
5755

5856
@After public void tearDown() {
59-
Hawk.clear();
57+
if (Hawk.isBuilt()) {
58+
Hawk.clear();
59+
}
6060
}
6161

6262
@Test public void initWithInvalidValues() {
@@ -90,7 +90,7 @@ public void init() {
9090
assertThat(fooBar).isNotNull();
9191
assertThat(fooBar.name).isEqualTo("hawk");
9292

93-
assertTrue(Hawk.put("innerClass", new FooBar.InnerFoo()));
93+
assertThat(Hawk.put("innerClass", new FooBar.InnerFoo())).isTrue();
9494
FooBar.InnerFoo innerFoo = Hawk.get("innerClass");
9595
assertThat(innerFoo).isNotNull();
9696
assertThat(innerFoo.name).isEqualTo("hawk");
@@ -351,12 +351,11 @@ public void init() {
351351
.observeOn(Schedulers.io())
352352
.subscribe(new Subscriber<String>() {
353353
@Override public void onCompleted() {
354-
assertTrue(true);
355354
latch.countDown();
356355
}
357356

358357
@Override public void onError(Throwable e) {
359-
assertTrue(false);
358+
fail();
360359
latch.countDown();
361360
}
362361

@@ -374,7 +373,6 @@ public void init() {
374373
.observeOn(Schedulers.io())
375374
.subscribe(new Subscriber<String>() {
376375
@Override public void onCompleted() {
377-
assertTrue(true);
378376
latch.countDown();
379377
}
380378

@@ -408,21 +406,110 @@ public void init() {
408406
})
409407
.subscribe(new Observer<String>() {
410408
@Override public void onCompleted() {
411-
assertTrue(true);
412409
latch.countDown();
413410
}
414411

415412
@Override public void onError(Throwable throwable) {
416-
assertTrue(false);
413+
fail();
417414
latch.countDown();
418415
}
419416

420417
@Override public void onNext(String storedValue) {
421-
assertEquals(storedValue, "hawk");
418+
assertThat(storedValue).isEqualTo("hawk");
422419
}
423420
});
424421

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

425+
@Test public void statusNotBuiltBeforeBuild() {
426+
Hawk.init(context);
427+
assertThat(Hawk.isBuilt()).isFalse();
428+
}
429+
430+
@Test public void statusBuiltAfterBuild() {
431+
Hawk.init(context).build();
432+
assertThat(Hawk.isBuilt()).isTrue();
433+
}
434+
435+
@Test public void testGetThrowsExceptionWhenNotBuilt() {
436+
Hawk.init(context);
437+
try {
438+
Hawk.get(KEY);
439+
fail("Did not throw an exception");
440+
} catch (IllegalStateException ignored) {
441+
}
442+
}
443+
444+
@Test public void testPutThrowsExceptionWhenNotBuilt() {
445+
Hawk.init(context);
446+
try {
447+
Hawk.put(KEY, "value");
448+
fail("Did not throw an exception");
449+
} catch (IllegalStateException ignored) {
450+
}
451+
}
452+
453+
@Test public void testClearThrowsExceptionWhenNotBuilt() {
454+
Hawk.init(context);
455+
try {
456+
Hawk.clear();
457+
fail("Did not throw an exception");
458+
} catch (IllegalStateException ignored) {
459+
}
460+
}
461+
462+
@Test public void testContainsThrowsExceptionWhenNotBuilt() {
463+
Hawk.init(context);
464+
try {
465+
Hawk.contains(KEY);
466+
fail("Did not throw an exception");
467+
} catch (IllegalStateException ignored) {
468+
}
469+
}
470+
471+
@Test public void testRemoveThrowsExceptionWhenNotBuilt() {
472+
Hawk.init(context);
473+
try {
474+
Hawk.remove(KEY);
475+
fail("Did not throw an exception");
476+
} catch (IllegalStateException ignored) {
477+
}
478+
}
479+
480+
@Test public void testRemoveMultiKeysThrowsExceptionWhenNotBuilt() {
481+
Hawk.init(context);
482+
try {
483+
Hawk.remove(KEY, KEY);
484+
fail("Did not throw an exception");
485+
} catch (IllegalStateException ignored) {
486+
}
487+
}
488+
489+
@Test public void testResetCryptoThrowsExceptionWhenNotBuilt() {
490+
Hawk.init(context);
491+
try {
492+
Hawk.resetCrypto();
493+
fail("Did not throw an exception");
494+
} catch (IllegalStateException ignored) {
495+
}
496+
}
497+
498+
@Test public void testCountThrowsExceptionWhenNotBuilt() {
499+
Hawk.init(context);
500+
try {
501+
Hawk.count();
502+
fail("Did not throw an exception");
503+
} catch (IllegalStateException ignored) {
504+
}
505+
}
506+
507+
@Test public void testPutInChainThrowsExceptionWhenNotBuilt() {
508+
Hawk.init(context);
509+
try {
510+
Hawk.chain().put(KEY, "value");
511+
fail("Did not throw an exception");
512+
} catch (IllegalStateException ignored) {
513+
}
514+
}
428515
}

0 commit comments

Comments
 (0)