Permalink
Please sign in to comment.
Browse files
Merge pull request #629 from geralt-encore/rx-completable
rx.Completable support for write operations
- Loading branch information...
Showing
with
1,100 additions
and 5 deletions.
- +1 −1 build.gradle
- +18 −0 storio-common/src/main/java/com/pushtorefresh/storio/operations/PreparedWriteOperation.java
- +46 −0 ...in/java/com/pushtorefresh/storio/operations/internal/OnSubscribeExecuteAsBlockingCompletable.java
- +68 −0 ...ava/com/pushtorefresh/storio/operations/internal/OnSubscribeExecuteAsBlockingCompletableTest.java
- +2 −2 storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDelete.java
- +21 −0 ...sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteByQuery.java
- +21 −0 ...ain/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjects.java
- +21 −0 ...-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObject.java
- +2 −2 storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPut.java
- +21 −0 .../src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjects.java
- +21 −0 ...sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValues.java
- +21 −0 ...rc/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValuesIterable.java
- +21 −0 storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutObject.java
- +38 −0 storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/design/DeleteOperationDesignTest.java
- +36 −0 storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/design/PutOperationDesignTest.java
- +9 −0 storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/DeleteStub.java
- +74 −0 ...te/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteByQueryTest.java
- +172 −0 ...java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjectsTest.java
- +58 −0 ...ite/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObjectTest.java
- +219 −0 .../test/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjectsTest.java
- +114 −0 ...est/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValuesIterableTest.java
- +15 −0 ...te/src/test/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValuesTest.java
- +62 −0 ...io-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutObjectTest.java
- +10 −0 storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/put/PutContentValuesStub.java
- +9 −0 storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/put/PutObjectsStub.java
2
build.gradle
18
storio-common/src/main/java/com/pushtorefresh/storio/operations/PreparedWriteOperation.java
@@ -0,0 +1,18 @@ | ||
+package com.pushtorefresh.storio.operations; | ||
+ | ||
+import android.support.annotation.CheckResult; | ||
+import android.support.annotation.NonNull; | ||
+ | ||
+import rx.Completable; | ||
+ | ||
+/** | ||
+ * Common API of prepared write operations | ||
+ * | ||
+ * @param <Result> type of result | ||
+ */ | ||
+public interface PreparedWriteOperation<Result> extends PreparedOperation<Result> { | ||
+ | ||
+ @NonNull | ||
+ @CheckResult | ||
+ Completable asRxComletable(); | ||
+} |
46
...com/pushtorefresh/storio/operations/internal/OnSubscribeExecuteAsBlockingCompletable.java
@@ -0,0 +1,46 @@ | ||
+package com.pushtorefresh.storio.operations.internal; | ||
+ | ||
+import android.support.annotation.NonNull; | ||
+ | ||
+import com.pushtorefresh.storio.StorIOException; | ||
+import com.pushtorefresh.storio.operations.PreparedOperation; | ||
+ | ||
+import rx.Completable; | ||
+ | ||
+/** | ||
+ * Required to avoid problems with ClassLoader when RxJava is not in ClassPath | ||
+ * We can not use anonymous classes from RxJava directly in StorIO, ClassLoader won't be happy :( | ||
+ * <p> | ||
+ * For internal usage only! | ||
+ */ | ||
+public final class OnSubscribeExecuteAsBlockingCompletable implements Completable.CompletableOnSubscribe { | ||
+ | ||
+ @NonNull | ||
+ private final PreparedOperation preparedOperation; | ||
+ | ||
+ private OnSubscribeExecuteAsBlockingCompletable(@NonNull PreparedOperation preparedOperation) { | ||
+ this.preparedOperation = preparedOperation; | ||
+ } | ||
+ | ||
+ /** | ||
+ * Creates new instance of {@link OnSubscribeExecuteAsBlockingCompletable} | ||
+ * | ||
+ * @param preparedOperation non-null instance of {@link PreparedOperation} which will be used to provide result to subscribers | ||
+ * @return new instance of {@link OnSubscribeExecuteAsBlockingCompletable} | ||
+ */ | ||
+ @NonNull | ||
+ public static Completable.CompletableOnSubscribe newInstance(@NonNull PreparedOperation preparedOperation) { | ||
+ return new OnSubscribeExecuteAsBlockingCompletable(preparedOperation); | ||
+ } | ||
+ | ||
+ @Override | ||
+ public void call(@NonNull Completable.CompletableSubscriber subscriber) { | ||
+ try { | ||
+ preparedOperation.executeAsBlocking(); | ||
+ | ||
+ subscriber.onCompleted(); | ||
+ } catch (StorIOException e) { | ||
+ subscriber.onError(e); | ||
+ } | ||
+ } | ||
+} |
68
...pushtorefresh/storio/operations/internal/OnSubscribeExecuteAsBlockingCompletableTest.java
@@ -0,0 +1,68 @@ | ||
+package com.pushtorefresh.storio.operations.internal; | ||
+ | ||
+import com.pushtorefresh.storio.StorIOException; | ||
+import com.pushtorefresh.storio.operations.PreparedWriteOperation; | ||
+ | ||
+import org.junit.Test; | ||
+ | ||
+import rx.Completable; | ||
+import rx.observers.TestSubscriber; | ||
+ | ||
+import static org.mockito.Mockito.mock; | ||
+import static org.mockito.Mockito.times; | ||
+import static org.mockito.Mockito.verify; | ||
+import static org.mockito.Mockito.verifyZeroInteractions; | ||
+import static org.mockito.Mockito.when; | ||
+ | ||
+public class OnSubscribeExecuteAsBlockingCompletableTest { | ||
+ | ||
+ @SuppressWarnings("ResourceType") | ||
+ @Test | ||
+ public void shouldExecuteAsBlockingAfterSubscription() { | ||
+ final PreparedWriteOperation preparedOperation = mock(PreparedWriteOperation.class); | ||
+ | ||
+ TestSubscriber testSubscriber = new TestSubscriber(); | ||
+ | ||
+ verifyZeroInteractions(preparedOperation); | ||
+ | ||
+ Completable completable = Completable.create(OnSubscribeExecuteAsBlockingCompletable.newInstance(preparedOperation)); | ||
+ | ||
+ verifyZeroInteractions(preparedOperation); | ||
+ | ||
+ completable.subscribe(testSubscriber); | ||
+ | ||
+ testSubscriber.assertNoErrors(); | ||
+ testSubscriber.assertCompleted(); | ||
+ | ||
+ verify(preparedOperation).executeAsBlocking(); | ||
+ verify(preparedOperation, times(0)).asRxObservable(); | ||
+ verify(preparedOperation, times(0)).asRxSingle(); | ||
+ verify(preparedOperation, times(0)).asRxComletable(); | ||
+ } | ||
+ | ||
+ @SuppressWarnings({"ThrowableInstanceNeverThrown", "ResourceType"}) | ||
+ @Test | ||
+ public void shouldCallOnErrorIfExceptionOccured() { | ||
+ final PreparedWriteOperation preparedOperation = mock(PreparedWriteOperation.class); | ||
+ | ||
+ StorIOException expectedException = new StorIOException("test exception"); | ||
+ | ||
+ when(preparedOperation.executeAsBlocking()).thenThrow(expectedException); | ||
+ | ||
+ TestSubscriber testSubscriber = new TestSubscriber(); | ||
+ | ||
+ Completable completable = Completable.create(OnSubscribeExecuteAsBlockingCompletable.newInstance(preparedOperation)); | ||
+ | ||
+ verifyZeroInteractions(preparedOperation); | ||
+ | ||
+ completable.subscribe(testSubscriber); | ||
+ | ||
+ testSubscriber.assertError(expectedException); | ||
+ testSubscriber.assertTerminalEvent(); | ||
+ | ||
+ verify(preparedOperation).executeAsBlocking(); | ||
+ verify(preparedOperation, times(0)).asRxObservable(); | ||
+ verify(preparedOperation, times(0)).asRxSingle(); | ||
+ verify(preparedOperation, times(0)).asRxComletable(); | ||
+ } | ||
+} |
4
...qlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDelete.java
21
...rc/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteByQuery.java
21
.../com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjects.java
21
...src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObject.java
4
storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPut.java
21
...n/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjects.java
21
...rc/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValues.java
21
...java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValuesIterable.java
21
...qlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutObject.java
38
...qlite/src/test/java/com/pushtorefresh/storio/sqlite/design/DeleteOperationDesignTest.java
36
...o-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/design/PutOperationDesignTest.java
9
...io-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/DeleteStub.java
74
...est/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteByQueryTest.java
172
.../pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjectsTest.java
58
...test/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObjectTest.java
219
...va/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjectsTest.java
114
.../com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValuesIterableTest.java
15
...est/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValuesTest.java
62
...e/src/test/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutObjectTest.java
10
...te/src/test/java/com/pushtorefresh/storio/sqlite/operations/put/PutContentValuesStub.java
9
...o-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/put/PutObjectsStub.java
0 comments on commit
52c1883