From 0801a0dc92e177ea37e67e0c46f670bd27b84b97 Mon Sep 17 00:00:00 2001 From: Dmitry Nikitin Date: Sat, 18 Jul 2015 14:35:15 +0300 Subject: [PATCH] Throw exception if ContentResover#query returns null. Add custom exception. --- .../pushtorefresh/storio/StorIOException.java | 35 +++++ .../impl/DefaultStorIOContentResolver.java | 9 +- .../delete/PreparedDeleteByQuery.java | 21 ++- .../PreparedDeleteCollectionOfObjects.java | 94 ++++++------ .../delete/PreparedDeleteObject.java | 50 ++++--- .../operations/get/PreparedGetCursor.java | 25 +++- .../get/PreparedGetListOfObjects.java | 71 +++++---- .../put/PreparedPutCollectionOfObjects.java | 94 ++++++------ .../put/PreparedPutContentValues.java | 21 ++- .../put/PreparedPutContentValuesIterable.java | 31 ++-- .../operations/put/PreparedPutObject.java | 49 +++--- .../integration/IntegrationTest.java | 33 +++++ .../integration/QueryTest.java | 14 ++ .../integration/RxQueryTest.java | 19 +++ ...PreparedDeleteCollectionOfObjectsTest.java | 5 +- .../delete/PreparedDeleteObjectTest.java | 5 +- .../get/PreparedGetListOfObjectsTest.java | 5 +- .../PreparedPutCollectionOfObjectsTest.java | 5 +- .../operations/put/PreparedPutObjectTest.java | 5 +- .../delete/PreparedDeleteByQuery.java | 25 +++- .../PreparedDeleteCollectionOfObjects.java | 140 ++++++++++-------- .../delete/PreparedDeleteObject.java | 52 ++++--- .../execute/PreparedExecuteSQL.java | 31 ++-- .../operations/get/PreparedGetCursor.java | 61 ++++---- .../get/PreparedGetListOfObjects.java | 67 +++++---- .../put/PreparedPutCollectionOfObjects.java | 138 +++++++++-------- .../put/PreparedPutContentValues.java | 25 +++- .../put/PreparedPutContentValuesIterable.java | 74 +++++---- .../operations/put/PreparedPutObject.java | 50 ++++--- ...PreparedDeleteCollectionOfObjectsTest.java | 9 +- .../delete/PreparedDeleteObjectTest.java | 5 +- .../get/PreparedGetListOfObjectsTest.java | 9 +- .../PreparedPutCollectionOfObjectsTest.java | 9 +- 33 files changed, 786 insertions(+), 500 deletions(-) create mode 100644 storio-common/src/main/java/com/pushtorefresh/storio/StorIOException.java diff --git a/storio-common/src/main/java/com/pushtorefresh/storio/StorIOException.java b/storio-common/src/main/java/com/pushtorefresh/storio/StorIOException.java new file mode 100644 index 000000000..1b8da82ab --- /dev/null +++ b/storio-common/src/main/java/com/pushtorefresh/storio/StorIOException.java @@ -0,0 +1,35 @@ +package com.pushtorefresh.storio; + +/** + * Common {@link RuntimeException} for all errors and exceptions occurred during StorIO operations. + */ +public class StorIOException extends RuntimeException { + + /** + * {@inheritDoc} + */ + public StorIOException() { + super(); + } + + /** + * {@inheritDoc} + */ + public StorIOException(String detailMessage) { + super(detailMessage); + } + + /** + * {@inheritDoc} + */ + public StorIOException(String detailMessage, Throwable throwable) { + super(detailMessage, throwable); + } + + /** + * {@inheritDoc} + */ + public StorIOException(Throwable throwable) { + super(throwable); + } +} diff --git a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/impl/DefaultStorIOContentResolver.java b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/impl/DefaultStorIOContentResolver.java index 5026f67ed..4e41dc679 100644 --- a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/impl/DefaultStorIOContentResolver.java +++ b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/impl/DefaultStorIOContentResolver.java @@ -4,7 +4,6 @@ import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; -import android.database.MatrixCursor; import android.net.Uri; import android.os.Handler; import android.os.HandlerThread; @@ -263,9 +262,11 @@ public Cursor query(@NonNull Query query) { nullableString(query.sortOrder()) ); - return cursor == null - ? new MatrixCursor(null, 0) - : cursor; + if (cursor == null) { + throw new IllegalStateException("Cursor returned by content provider is null"); + } + + return cursor; } /** diff --git a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteByQuery.java b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteByQuery.java index 3c2b0cb08..454675b80 100644 --- a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteByQuery.java +++ b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteByQuery.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.contentresolver.queries.DeleteQuery; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; @@ -47,7 +48,11 @@ public final class PreparedDeleteByQuery extends PreparedDelete { @NonNull @Override public DeleteResult executeAsBlocking() { - return deleteResolver.performDelete(storIOContentResolver, deleteQuery); + try { + return deleteResolver.performDelete(storIOContentResolver, deleteQuery); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -67,11 +72,15 @@ public DeleteResult executeAsBlocking() { @NonNull @Override public Observable createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteCollectionOfObjects.java b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteCollectionOfObjects.java index 78019f51d..b81960346 100644 --- a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteCollectionOfObjects.java +++ b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteCollectionOfObjects.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; @@ -55,51 +56,56 @@ public final class PreparedDeleteCollectionOfObjects extends PreparedDelete executeAsBlocking() { - final StorIOContentResolver.Internal internal = storIOContentResolver.internal(); - - // Nullable - final List objectsAndDeleteResolvers; - - if (explicitDeleteResolver != null) { - objectsAndDeleteResolvers = null; - } else { - objectsAndDeleteResolvers = new ArrayList(objects.size()); + try { + final StorIOContentResolver.Internal internal = storIOContentResolver.internal(); + + // Nullable + final List objectsAndDeleteResolvers; + + if (explicitDeleteResolver != null) { + objectsAndDeleteResolvers = null; + } else { + objectsAndDeleteResolvers = new ArrayList(objects.size()); + + for (final T object : objects) { + final ContentResolverTypeMapping typeMapping + = (ContentResolverTypeMapping) internal.typeMapping(object.getClass()); + + if (typeMapping == null) { + throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + + "object = " + object + ", object.class = " + object.getClass() + "," + + "ContentProvider was not affected by this operation, please add type mapping for this type"); + } + + objectsAndDeleteResolvers.add(new SimpleImmutableEntry( + object, + typeMapping.deleteResolver() + )); + } + } - for (final T object : objects) { - final ContentResolverTypeMapping typeMapping - = (ContentResolverTypeMapping) internal.typeMapping(object.getClass()); + final Map results = new HashMap(objects.size()); - if (typeMapping == null) { - throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + - "object = " + object + ", object.class = " + object.getClass() + "," + - "ContentProvider was not affected by this operation, please add type mapping for this type"); + if (explicitDeleteResolver != null) { + for (final T object : objects) { + final DeleteResult deleteResult = explicitDeleteResolver.performDelete(storIOContentResolver, object); + results.put(object, deleteResult); } + } else { + for (final SimpleImmutableEntry> objectAndDeleteResolver : objectsAndDeleteResolvers) { + final T object = objectAndDeleteResolver.getKey(); + final DeleteResolver deleteResolver = objectAndDeleteResolver.getValue(); - objectsAndDeleteResolvers.add(new SimpleImmutableEntry( - object, - typeMapping.deleteResolver() - )); + final DeleteResult deleteResult = deleteResolver.performDelete(storIOContentResolver, object); + results.put(object, deleteResult); + } } - } - final Map results = new HashMap(objects.size()); + return DeleteResults.newInstance(results); - if (explicitDeleteResolver != null) { - for (final T object : objects) { - final DeleteResult deleteResult = explicitDeleteResolver.performDelete(storIOContentResolver, object); - results.put(object, deleteResult); - } - } else { - for (final SimpleImmutableEntry> objectAndDeleteResolver : objectsAndDeleteResolvers) { - final T object = objectAndDeleteResolver.getKey(); - final DeleteResolver deleteResolver = objectAndDeleteResolver.getValue(); - - final DeleteResult deleteResult = deleteResolver.performDelete(storIOContentResolver, object); - results.put(object, deleteResult); - } + } catch (Throwable throwable) { + throw new StorIOException(throwable); } - - return DeleteResults.newInstance(results); } /** @@ -119,11 +125,15 @@ public DeleteResults executeAsBlocking() { @NonNull @Override public Observable> createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteObject.java b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteObject.java index da93592be..d36002f56 100644 --- a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteObject.java +++ b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteObject.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; @@ -48,24 +49,29 @@ public final class PreparedDeleteObject extends PreparedDelete @NonNull @Override public DeleteResult executeAsBlocking() { - final DeleteResolver deleteResolver; - - if (explicitDeleteResolver != null) { - deleteResolver = explicitDeleteResolver; - } else { - final ContentResolverTypeMapping typeMapping - = storIOContentResolver.internal().typeMapping((Class) object.getClass()); - - if (typeMapping == null) { - throw new IllegalStateException("Object does not have type mapping: " + - "object = " + object + ", object.class = " + object.getClass() + "," + - "ContentProvider was not affected by this operation, please add type mapping for this type"); + try { + final DeleteResolver deleteResolver; + + if (explicitDeleteResolver != null) { + deleteResolver = explicitDeleteResolver; + } else { + final ContentResolverTypeMapping typeMapping + = storIOContentResolver.internal().typeMapping((Class) object.getClass()); + + if (typeMapping == null) { + throw new IllegalStateException("Object does not have type mapping: " + + "object = " + object + ", object.class = " + object.getClass() + "," + + "ContentProvider was not affected by this operation, please add type mapping for this type"); + } + + deleteResolver = typeMapping.deleteResolver(); } - deleteResolver = typeMapping.deleteResolver(); - } + return deleteResolver.performDelete(storIOContentResolver, object); - return deleteResolver.performDelete(storIOContentResolver, object); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -85,11 +91,15 @@ public DeleteResult executeAsBlocking() { @NonNull @Override public Observable createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetCursor.java b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetCursor.java index 5b6f8be06..9e75d5a14 100644 --- a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetCursor.java +++ b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetCursor.java @@ -5,6 +5,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.contentresolver.queries.Query; import com.pushtorefresh.storio.operations.internal.MapSomethingToExecuteAsBlocking; @@ -50,7 +51,11 @@ public final class PreparedGetCursor extends PreparedGet { @NonNull @Override public Cursor executeAsBlocking() { - return getResolver.performGet(storIOContentResolver, query); + try { + return getResolver.performGet(storIOContentResolver, query); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -73,13 +78,17 @@ public Cursor executeAsBlocking() { @NonNull @Override public Observable createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return storIOContentResolver - .observeChangesOfUri(query.uri()) // each change triggers executeAsBlocking - .map(MapSomethingToExecuteAsBlocking.newInstance(this)) - .startWith(Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))) // start stream with first query result - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return storIOContentResolver + .observeChangesOfUri(query.uri()) // each change triggers executeAsBlocking + .map(MapSomethingToExecuteAsBlocking.newInstance(this)) + .startWith(Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))) // start stream with first query result + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetListOfObjects.java b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetListOfObjects.java index 079ba32a0..f48149720 100644 --- a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetListOfObjects.java +++ b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetListOfObjects.java @@ -5,6 +5,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.contentresolver.queries.Query; @@ -64,40 +65,44 @@ public final class PreparedGetListOfObjects extends PreparedGet> { @NonNull @Override public List executeAsBlocking() { - final GetResolver getResolver; + try { + final GetResolver getResolver; + + if (explicitGetResolver != null) { + getResolver = explicitGetResolver; + } else { + final ContentResolverTypeMapping typeMapping = storIOContentResolver.internal().typeMapping(type); - if (explicitGetResolver != null) { - getResolver = explicitGetResolver; - } else { - final ContentResolverTypeMapping typeMapping = storIOContentResolver.internal().typeMapping(type); + if (typeMapping == null) { + throw new IllegalStateException("This type does not have type mapping: " + + "type = " + type + "," + + "ContentProvider was not touched by this operation, please add type mapping for this type"); + } - if (typeMapping == null) { - throw new IllegalStateException("This type does not have type mapping: " + - "type = " + type + "," + - "ContentProvider was not touched by this operation, please add type mapping for this type"); + getResolver = typeMapping.getResolver(); } - getResolver = typeMapping.getResolver(); - } + final Cursor cursor = getResolver.performGet(storIOContentResolver, query); - final Cursor cursor = getResolver.performGet(storIOContentResolver, query); + try { + final int count = cursor.getCount(); - try { - final int count = cursor.getCount(); + if (count == 0) { + return EMPTY_LIST; // it's immutable + } else { + final List list = new ArrayList(count); - if (count == 0) { - return EMPTY_LIST; // it's immutable - } else { - final List list = new ArrayList(count); + while (cursor.moveToNext()) { + list.add(getResolver.mapFromCursor(cursor)); + } - while (cursor.moveToNext()) { - list.add(getResolver.mapFromCursor(cursor)); + return unmodifiableList(list); } - - return unmodifiableList(list); + } finally { + cursor.close(); } - } finally { - cursor.close(); + } catch (Throwable throwable) { + throw new StorIOException(throwable); } } @@ -123,13 +128,17 @@ public List executeAsBlocking() { @NonNull @Override public Observable> createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return storIOContentResolver - .observeChangesOfUri(query.uri()) // each change triggers executeAsBlocking - .map(MapSomethingToExecuteAsBlocking.newInstance(this)) - .startWith(Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))) // start stream with first query result - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return storIOContentResolver + .observeChangesOfUri(query.uri()) // each change triggers executeAsBlocking + .map(MapSomethingToExecuteAsBlocking.newInstance(this)) + .startWith(Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))) // start stream with first query result + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutCollectionOfObjects.java b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutCollectionOfObjects.java index c9242bed6..ab97f69c5 100644 --- a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutCollectionOfObjects.java +++ b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutCollectionOfObjects.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; @@ -55,51 +56,56 @@ public final class PreparedPutCollectionOfObjects extends PreparedPut executeAsBlocking() { - final StorIOContentResolver.Internal internal = storIOContentResolver.internal(); - - // Nullable - final List>> objectsAndPutResolvers; - - if (explicitPutResolver != null) { - objectsAndPutResolvers = null; - } else { - objectsAndPutResolvers = new ArrayList>>(objects.size()); + try { + final StorIOContentResolver.Internal internal = storIOContentResolver.internal(); + + // Nullable + final List>> objectsAndPutResolvers; + + if (explicitPutResolver != null) { + objectsAndPutResolvers = null; + } else { + objectsAndPutResolvers = new ArrayList>>(objects.size()); + + for (final T object : objects) { + final ContentResolverTypeMapping typeMapping + = (ContentResolverTypeMapping) internal.typeMapping(object.getClass()); + + if (typeMapping == null) { + throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + + "object = " + object + ", object.class = " + object.getClass() + "," + + "ContentProvider was not affected by this operation, please add type mapping for this type"); + } + + objectsAndPutResolvers.add(new SimpleImmutableEntry>( + object, + typeMapping.putResolver() + )); + } + } - for (final T object : objects) { - final ContentResolverTypeMapping typeMapping - = (ContentResolverTypeMapping) internal.typeMapping(object.getClass()); + final Map results = new HashMap(objects.size()); - if (typeMapping == null) { - throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + - "object = " + object + ", object.class = " + object.getClass() + "," + - "ContentProvider was not affected by this operation, please add type mapping for this type"); + if (explicitPutResolver != null) { + for (final T object : objects) { + final PutResult putResult = explicitPutResolver.performPut(storIOContentResolver, object); + results.put(object, putResult); } + } else { + for (final SimpleImmutableEntry> objectAndPutResolver : objectsAndPutResolvers) { + final T object = objectAndPutResolver.getKey(); + final PutResolver putResolver = objectAndPutResolver.getValue(); - objectsAndPutResolvers.add(new SimpleImmutableEntry>( - object, - typeMapping.putResolver() - )); + final PutResult putResult = putResolver.performPut(storIOContentResolver, object); + results.put(object, putResult); + } } - } - final Map results = new HashMap(objects.size()); + return PutResults.newInstance(results); - if (explicitPutResolver != null) { - for (final T object : objects) { - final PutResult putResult = explicitPutResolver.performPut(storIOContentResolver, object); - results.put(object, putResult); - } - } else { - for (final SimpleImmutableEntry> objectAndPutResolver : objectsAndPutResolvers) { - final T object = objectAndPutResolver.getKey(); - final PutResolver putResolver = objectAndPutResolver.getValue(); - - final PutResult putResult = putResolver.performPut(storIOContentResolver, object); - results.put(object, putResult); - } + } catch (Throwable throwable) { + throw new StorIOException(throwable); } - - return PutResults.newInstance(results); } /** @@ -119,11 +125,15 @@ public PutResults executeAsBlocking() { @NonNull @Override public Observable> createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutContentValues.java b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutContentValues.java index f9fa9ef38..7b90ef1b6 100644 --- a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutContentValues.java +++ b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutContentValues.java @@ -4,6 +4,7 @@ import android.support.annotation.NonNull; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; @@ -45,7 +46,11 @@ public final class PreparedPutContentValues extends PreparedPut { @NonNull @Override public PutResult executeAsBlocking() { - return putResolver.performPut(storIOContentResolver, contentValues); + try { + return putResolver.performPut(storIOContentResolver, contentValues); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -65,11 +70,15 @@ public PutResult executeAsBlocking() { @NonNull @Override public Observable createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutContentValuesIterable.java b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutContentValuesIterable.java index 8b9148f4f..1637043be 100644 --- a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutContentValuesIterable.java +++ b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutContentValuesIterable.java @@ -4,6 +4,7 @@ import android.support.annotation.NonNull; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; @@ -49,14 +50,18 @@ public final class PreparedPutContentValuesIterable extends PreparedPut executeAsBlocking() { - final Map putResultsMap = new HashMap(); + try { + final Map putResultsMap = new HashMap(); - for (final ContentValues cv : contentValues) { - final PutResult putResult = putResolver.performPut(storIOContentResolver, cv); - putResultsMap.put(cv, putResult); - } + for (final ContentValues cv : contentValues) { + final PutResult putResult = putResolver.performPut(storIOContentResolver, cv); + putResultsMap.put(cv, putResult); + } - return PutResults.newInstance(putResultsMap); + return PutResults.newInstance(putResultsMap); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -76,11 +81,15 @@ public PutResults executeAsBlocking() { @NonNull @Override public Observable> createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutObject.java b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutObject.java index 42bedcc2a..b081525d3 100644 --- a/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutObject.java +++ b/storio-content-resolver/src/main/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutObject.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; @@ -49,24 +50,28 @@ public final class PreparedPutObject extends PreparedPut { @NonNull @Override public PutResult executeAsBlocking() { - final PutResolver putResolver; - - if (explicitPutResolver != null) { - putResolver = explicitPutResolver; - } else { - final ContentResolverTypeMapping typeMapping - = storIOContentResolver.internal().typeMapping((Class) object.getClass()); - - if (typeMapping == null) { - throw new IllegalStateException("Object does not have type mapping: " + - "object = " + object + ", object.class = " + object.getClass() + "," + - "ContentProvider was not affected by this operation, please add type mapping for this type"); + try { + final PutResolver putResolver; + + if (explicitPutResolver != null) { + putResolver = explicitPutResolver; + } else { + final ContentResolverTypeMapping typeMapping + = storIOContentResolver.internal().typeMapping((Class) object.getClass()); + + if (typeMapping == null) { + throw new IllegalStateException("Object does not have type mapping: " + + "object = " + object + ", object.class = " + object.getClass() + "," + + "ContentProvider was not affected by this operation, please add type mapping for this type"); + } + + putResolver = typeMapping.putResolver(); } - putResolver = typeMapping.putResolver(); + return putResolver.performPut(storIOContentResolver, object); + } catch (Throwable throwable) { + throw new StorIOException(throwable); } - - return putResolver.performPut(storIOContentResolver, object); } /** @@ -86,11 +91,15 @@ public PutResult executeAsBlocking() { @NonNull @Override public Observable createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/IntegrationTest.java b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/IntegrationTest.java index ca31c81ff..117c3ac4f 100644 --- a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/IntegrationTest.java +++ b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/IntegrationTest.java @@ -10,6 +10,7 @@ import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.contentresolver.impl.DefaultStorIOContentResolver; import com.pushtorefresh.storio.contentresolver.operations.delete.DeleteResult; +import com.pushtorefresh.storio.contentresolver.operations.get.PreparedGetCursor; import com.pushtorefresh.storio.contentresolver.operations.put.PutResult; import com.pushtorefresh.storio.contentresolver.queries.Query; import com.pushtorefresh.storio.test.AbstractEmissionChecker; @@ -32,6 +33,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public abstract class IntegrationTest { @@ -307,4 +312,32 @@ public void call(Changes changes) { return storedTweets; } + + @NonNull + PreparedGetCursor createQueryWithNullResult() { + + final ContentResolver internalContentResolver = mock(ContentResolver.class); + when(internalContentResolver + .query(any(Uri.class), + any(String[].class), + anyString(), + any(String[].class), + anyString())) + .thenReturn(null); + + final DefaultStorIOContentResolver defaultStorIOContentResolver = DefaultStorIOContentResolver.builder() + .contentResolver(internalContentResolver) + .addTypeMapping(User.class, ContentResolverTypeMapping.builder() + .putResolver(UserMeta.PUT_RESOLVER) + .getResolver(UserMeta.GET_RESOLVER) + .deleteResolver(UserMeta.DELETE_RESOLVER) + .build()) + .build(); + + return defaultStorIOContentResolver + .get() + .cursor() + .withQuery(mock(Query.class)) + .prepare(); + } } diff --git a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/QueryTest.java b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/QueryTest.java index 94ca4200b..402a279da 100644 --- a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/QueryTest.java +++ b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/QueryTest.java @@ -3,8 +3,10 @@ import android.database.Cursor; import android.support.annotation.NonNull; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.BuildConfig; import com.pushtorefresh.storio.contentresolver.operations.get.DefaultGetResolver; +import com.pushtorefresh.storio.contentresolver.operations.get.PreparedGetCursor; import com.pushtorefresh.storio.contentresolver.queries.Query; import org.junit.Test; @@ -18,6 +20,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) @@ -143,4 +146,15 @@ public User mapFromCursor(@NonNull Cursor cursor) { assertNull(userFromStorage.email()); } } + + @Test + public void shouldThrowExceptionIfCursorNullBlocking() { + final PreparedGetCursor queryWithNullResult = createQueryWithNullResult(); + try { + queryWithNullResult.executeAsBlocking(); + fail("StorIOException should be thrown"); + } catch (StorIOException expected) { + // it's okay, cursor was null + } + } } \ No newline at end of file diff --git a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/RxQueryTest.java b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/RxQueryTest.java index 41cf81c3a..2bf108fe5 100644 --- a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/RxQueryTest.java +++ b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/RxQueryTest.java @@ -1,9 +1,12 @@ package com.pushtorefresh.storio.contentresolver.integration; +import android.database.Cursor; import android.support.annotation.NonNull; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.BuildConfig; import com.pushtorefresh.storio.contentresolver.operations.delete.DeleteResult; +import com.pushtorefresh.storio.contentresolver.operations.get.PreparedGetCursor; import com.pushtorefresh.storio.contentresolver.operations.put.PutResult; import com.pushtorefresh.storio.contentresolver.queries.Query; import com.pushtorefresh.storio.test.AbstractEmissionChecker; @@ -20,6 +23,7 @@ import rx.Subscription; import rx.functions.Action1; +import rx.observers.TestSubscriber; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -168,4 +172,19 @@ public void queryShouldBeUpdatedAfterDelete() { emissionChecker.assertThatNoExpectedValuesLeft(); subscription.unsubscribe(); } + + @Test + public void shouldThrowExceptionIfCursorNullObservable() { + final PreparedGetCursor queryWithNullResult = createQueryWithNullResult(); + + final TestSubscriber testSubscriber = new TestSubscriber(); + + queryWithNullResult + .createObservable() + .subscribe(testSubscriber); + + testSubscriber.awaitTerminalEvent(); + testSubscriber.assertNoValues(); + testSubscriber.assertError(StorIOException.class); + } } diff --git a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteCollectionOfObjectsTest.java b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteCollectionOfObjectsTest.java index b702967d7..2a35cc913 100644 --- a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteCollectionOfObjectsTest.java +++ b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteCollectionOfObjectsTest.java @@ -1,5 +1,6 @@ package com.pushtorefresh.storio.contentresolver.operations.delete; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.contentresolver.queries.DeleteQuery; @@ -105,7 +106,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAffectingContentPr try { preparedDelete.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -138,7 +139,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAffectingContentPr testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOContentResolver).delete(); verify(storIOContentResolver).internal(); diff --git a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteObjectTest.java b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteObjectTest.java index e2aeb9c79..3de29d31c 100644 --- a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteObjectTest.java +++ b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/delete/PreparedDeleteObjectTest.java @@ -1,5 +1,6 @@ package com.pushtorefresh.storio.contentresolver.operations.delete; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.contentresolver.queries.DeleteQuery; @@ -100,7 +101,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAffectingContentPr try { preparedDelete.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -131,7 +132,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAffectingContentPr testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOContentResolver).delete(); verify(storIOContentResolver).internal(); diff --git a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetListOfObjectsTest.java b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetListOfObjectsTest.java index 91d49f7ee..68f1ade8a 100644 --- a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetListOfObjectsTest.java +++ b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/get/PreparedGetListOfObjectsTest.java @@ -2,6 +2,7 @@ import android.net.Uri; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.Changes; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.contentresolver.queries.Query; @@ -112,7 +113,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAccessingContentPr try { preparedGet.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -147,7 +148,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAccessingContentPr testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOContentResolver).get(); verify(storIOContentResolver).internal(); diff --git a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutCollectionOfObjectsTest.java b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutCollectionOfObjectsTest.java index 787c61aa4..7ef1bd728 100644 --- a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutCollectionOfObjectsTest.java +++ b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutCollectionOfObjectsTest.java @@ -2,6 +2,7 @@ import android.content.ContentValues; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.contentresolver.queries.InsertQuery; import com.pushtorefresh.storio.contentresolver.queries.UpdateQuery; @@ -108,7 +109,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAffectingContentPr try { preparedPut.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -142,7 +143,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAffectingContentPr testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOContentResolver).put(); verify(storIOContentResolver).internal(); diff --git a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutObjectTest.java b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutObjectTest.java index f82fdf5e5..67e472b57 100644 --- a/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutObjectTest.java +++ b/storio-content-resolver/src/test/java/com/pushtorefresh/storio/contentresolver/operations/put/PreparedPutObjectTest.java @@ -2,6 +2,7 @@ import android.content.ContentValues; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.contentresolver.StorIOContentResolver; import com.pushtorefresh.storio.contentresolver.queries.InsertQuery; import com.pushtorefresh.storio.contentresolver.queries.UpdateQuery; @@ -103,7 +104,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAffectingContentPr try { preparedPut.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -135,7 +136,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAffectingContentPr testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOContentResolver).put(); verify(storIOContentResolver).internal(); diff --git a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteByQuery.java b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteByQuery.java index f95ac2967..91bfc2775 100644 --- a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteByQuery.java +++ b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteByQuery.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; import com.pushtorefresh.storio.sqlite.Changes; import com.pushtorefresh.storio.sqlite.StorIOSQLite; @@ -44,9 +45,13 @@ public final class PreparedDeleteByQuery extends PreparedDelete { @NonNull @Override public DeleteResult executeAsBlocking() { - final DeleteResult deleteResult = deleteResolver.performDelete(storIOSQLite, deleteQuery); - storIOSQLite.internal().notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables())); - return deleteResult; + try { + final DeleteResult deleteResult = deleteResolver.performDelete(storIOSQLite, deleteQuery); + storIOSQLite.internal().notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables())); + return deleteResult; + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -66,11 +71,15 @@ public DeleteResult executeAsBlocking() { @NonNull @Override public Observable createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjects.java b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjects.java index 491bd52a4..5e82c648a 100644 --- a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjects.java +++ b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjects.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; import com.pushtorefresh.storio.sqlite.Changes; import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; @@ -62,91 +63,96 @@ public final class PreparedDeleteCollectionOfObjects extends PreparedDelete executeAsBlocking() { - final StorIOSQLite.Internal internal = storIOSQLite.internal(); + try { + final StorIOSQLite.Internal internal = storIOSQLite.internal(); - // Nullable - final List>> objectsAndDeleteResolvers; + // Nullable + final List>> objectsAndDeleteResolvers; - if (explicitDeleteResolver != null) { - objectsAndDeleteResolvers = null; - } else { - objectsAndDeleteResolvers - = new ArrayList>>(objects.size()); + if (explicitDeleteResolver != null) { + objectsAndDeleteResolvers = null; + } else { + objectsAndDeleteResolvers + = new ArrayList>>(objects.size()); - for (final T object : objects) { - final SQLiteTypeMapping typeMapping - = (SQLiteTypeMapping) internal.typeMapping(object.getClass()); + for (final T object : objects) { + final SQLiteTypeMapping typeMapping + = (SQLiteTypeMapping) internal.typeMapping(object.getClass()); - if (typeMapping == null) { - throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + - "object = " + object + ", object.class = " + object.getClass() + "," + - "db was not affected by this operation, please add type mapping for this type"); - } + if (typeMapping == null) { + throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + + "object = " + object + ", object.class = " + object.getClass() + "," + + "db was not affected by this operation, please add type mapping for this type"); + } - objectsAndDeleteResolvers.add(new SimpleImmutableEntry>( - object, - typeMapping.deleteResolver() - )); + objectsAndDeleteResolvers.add(new SimpleImmutableEntry>( + object, + typeMapping.deleteResolver() + )); + } } - } - if (useTransaction) { - internal.beginTransaction(); - } + if (useTransaction) { + internal.beginTransaction(); + } - final Map results = new HashMap(objects.size()); - boolean transactionSuccessful = false; + final Map results = new HashMap(objects.size()); + boolean transactionSuccessful = false; - try { - if (explicitDeleteResolver != null) { - for (final T object : objects) { - final DeleteResult deleteResult = explicitDeleteResolver.performDelete(storIOSQLite, object); + try { + if (explicitDeleteResolver != null) { + for (final T object : objects) { + final DeleteResult deleteResult = explicitDeleteResolver.performDelete(storIOSQLite, object); - results.put(object, deleteResult); + results.put(object, deleteResult); - if (!useTransaction) { - internal.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables())); + if (!useTransaction) { + internal.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables())); + } } - } - } else { - for (final SimpleImmutableEntry> objectAndDeleteResolver : objectsAndDeleteResolvers) { - final T object = objectAndDeleteResolver.getKey(); - final DeleteResolver deleteResolver = objectAndDeleteResolver.getValue(); + } else { + for (final SimpleImmutableEntry> objectAndDeleteResolver : objectsAndDeleteResolvers) { + final T object = objectAndDeleteResolver.getKey(); + final DeleteResolver deleteResolver = objectAndDeleteResolver.getValue(); - final DeleteResult deleteResult = deleteResolver.performDelete(storIOSQLite, object); + final DeleteResult deleteResult = deleteResolver.performDelete(storIOSQLite, object); - results.put(object, deleteResult); + results.put(object, deleteResult); - if (!useTransaction) { - internal.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables())); + if (!useTransaction) { + internal.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables())); + } } } - } - if (useTransaction) { - internal.setTransactionSuccessful(); - transactionSuccessful = true; - } - } finally { - if (useTransaction) { - internal.endTransaction(); + if (useTransaction) { + internal.setTransactionSuccessful(); + transactionSuccessful = true; + } + } finally { + if (useTransaction) { + internal.endTransaction(); - // if delete was in transaction and it was successful -> notify about changes - if (transactionSuccessful) { - final Set affectedTables = new HashSet(1); // in most cases it will be one table + // if delete was in transaction and it was successful -> notify about changes + if (transactionSuccessful) { + final Set affectedTables = new HashSet(1); // in most cases it will be one table - for (final T object : results.keySet()) { - affectedTables.addAll(results.get(object).affectedTables()); - } + for (final T object : results.keySet()) { + affectedTables.addAll(results.get(object).affectedTables()); + } - // IMPORTANT: Notifying about change should be done after end of transaction - // It'll reduce number of possible deadlock situations - internal.notifyAboutChanges(Changes.newInstance(affectedTables)); + // IMPORTANT: Notifying about change should be done after end of transaction + // It'll reduce number of possible deadlock situations + internal.notifyAboutChanges(Changes.newInstance(affectedTables)); + } } } - } - return DeleteResults.newInstance(results); + return DeleteResults.newInstance(results); + + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -166,11 +172,15 @@ public DeleteResults executeAsBlocking() { @NonNull @Override public Observable> createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObject.java b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObject.java index 4846b24ab..7746265df 100644 --- a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObject.java +++ b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObject.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; import com.pushtorefresh.storio.sqlite.Changes; import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; @@ -49,28 +50,33 @@ public final class PreparedDeleteObject extends PreparedDelete @NonNull @Override public DeleteResult executeAsBlocking() { - final StorIOSQLite.Internal internal = storIOSQLite.internal(); + try { + final StorIOSQLite.Internal internal = storIOSQLite.internal(); - final DeleteResolver deleteResolver; + final DeleteResolver deleteResolver; - if (explicitDeleteResolver != null) { - deleteResolver = explicitDeleteResolver; - } else { - final SQLiteTypeMapping typeMapping - = internal.typeMapping((Class) object.getClass()); + if (explicitDeleteResolver != null) { + deleteResolver = explicitDeleteResolver; + } else { + final SQLiteTypeMapping typeMapping + = internal.typeMapping((Class) object.getClass()); - if (typeMapping == null) { - throw new IllegalStateException("Object does not have type mapping: " + - "object = " + object + ", object.class = " + object.getClass() + "," + - "db was not affected by this operation, please add type mapping for this type"); + if (typeMapping == null) { + throw new IllegalStateException("Object does not have type mapping: " + + "object = " + object + ", object.class = " + object.getClass() + "," + + "db was not affected by this operation, please add type mapping for this type"); + } + + deleteResolver = typeMapping.deleteResolver(); } - deleteResolver = typeMapping.deleteResolver(); - } + final DeleteResult deleteResult = deleteResolver.performDelete(storIOSQLite, object); + internal.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables())); + return deleteResult; - final DeleteResult deleteResult = deleteResolver.performDelete(storIOSQLite, object); - internal.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables())); - return deleteResult; + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -90,11 +96,15 @@ public DeleteResult executeAsBlocking() { @NonNull @Override public Observable createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/execute/PreparedExecuteSQL.java b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/execute/PreparedExecuteSQL.java index 910d01066..14791a1b5 100644 --- a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/execute/PreparedExecuteSQL.java +++ b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/execute/PreparedExecuteSQL.java @@ -3,6 +3,7 @@ import android.support.annotation.NonNull; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.operations.PreparedOperation; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; import com.pushtorefresh.storio.sqlite.Changes; @@ -48,15 +49,19 @@ public final class PreparedExecuteSQL implements PreparedOperation { @NonNull @Override public Object executeAsBlocking() { - storIOSQLite.internal().executeSQL(rawQuery); + try { + storIOSQLite.internal().executeSQL(rawQuery); - final Set affectedTables = rawQuery.affectsTables(); + final Set affectedTables = rawQuery.affectsTables(); - if (affectedTables.size() > 0) { - storIOSQLite.internal().notifyAboutChanges(Changes.newInstance(affectedTables)); - } + if (affectedTables.size() > 0) { + storIOSQLite.internal().notifyAboutChanges(Changes.newInstance(affectedTables)); + } - return new Object(); + return new Object(); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -80,11 +85,15 @@ public Object executeAsBlocking() { @NonNull @Override public Observable createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetCursor.java b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetCursor.java index e3ad3b4d5..f6167da9f 100644 --- a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetCursor.java +++ b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetCursor.java @@ -5,6 +5,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.operations.internal.MapSomethingToExecuteAsBlocking; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; import com.pushtorefresh.storio.sqlite.StorIOSQLite; @@ -54,12 +55,16 @@ public final class PreparedGetCursor extends PreparedGet { @WorkerThread @NonNull public Cursor executeAsBlocking() { - if (query != null) { - return getResolver.performGet(storIOSQLite, query); - } else if (rawQuery != null) { - return getResolver.performGet(storIOSQLite, rawQuery); - } else { - throw new IllegalStateException("Please specify query"); + try { + if (query != null) { + return getResolver.performGet(storIOSQLite, query); + } else if (rawQuery != null) { + return getResolver.performGet(storIOSQLite, rawQuery); + } else { + throw new IllegalStateException("Please specify query"); + } + } catch (Throwable throwable) { + throw new StorIOException(throwable); } } @@ -84,29 +89,33 @@ public Cursor executeAsBlocking() { @NonNull @Override public Observable createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - final Set tables; + final Set tables; - if (query != null) { - tables = new HashSet(1); - tables.add(query.table()); - } else if (rawQuery != null) { - tables = rawQuery.observesTables(); - } else { - throw new IllegalStateException("Please specify query"); - } + if (query != null) { + tables = new HashSet(1); + tables.add(query.table()); + } else if (rawQuery != null) { + tables = rawQuery.observesTables(); + } else { + throw new IllegalStateException("Please specify query"); + } - if (!tables.isEmpty()) { - return storIOSQLite - .observeChangesInTables(tables) // each change triggers executeAsBlocking - .map(MapSomethingToExecuteAsBlocking.newInstance(this)) - .startWith(Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))) // start stream with first query result - .subscribeOn(Schedulers.io()); - } else { - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + if (!tables.isEmpty()) { + return storIOSQLite + .observeChangesInTables(tables) // each change triggers executeAsBlocking + .map(MapSomethingToExecuteAsBlocking.newInstance(this)) + .startWith(Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))) // start stream with first query result + .subscribeOn(Schedulers.io()); + } else { + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } + } catch (Throwable throwable) { + throw new StorIOException(throwable); } } diff --git a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetListOfObjects.java b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetListOfObjects.java index 732a7b39e..e1a46d9b4 100644 --- a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetListOfObjects.java +++ b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetListOfObjects.java @@ -5,6 +5,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.operations.internal.MapSomethingToExecuteAsBlocking; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; @@ -71,48 +72,52 @@ public final class PreparedGetListOfObjects extends PreparedGet> { @NonNull @Override public List executeAsBlocking() { - final GetResolver getResolver; + try { + final GetResolver getResolver; - if (explicitGetResolver != null) { - getResolver = explicitGetResolver; - } else { - final SQLiteTypeMapping typeMapping = storIOSQLite.internal().typeMapping(type); + if (explicitGetResolver != null) { + getResolver = explicitGetResolver; + } else { + final SQLiteTypeMapping typeMapping = storIOSQLite.internal().typeMapping(type); - if (typeMapping == null) { - throw new IllegalStateException("This type does not have type mapping: " + - "type = " + type + "," + - "db was not touched by this operation, please add type mapping for this type"); + if (typeMapping == null) { + throw new IllegalStateException("This type does not have type mapping: " + + "type = " + type + "," + + "db was not touched by this operation, please add type mapping for this type"); + } + + getResolver = typeMapping.getResolver(); } - getResolver = typeMapping.getResolver(); - } + final Cursor cursor; - final Cursor cursor; + if (query != null) { + cursor = getResolver.performGet(storIOSQLite, query); + } else if (rawQuery != null) { + cursor = getResolver.performGet(storIOSQLite, rawQuery); + } else { + throw new IllegalStateException("Please specify query"); + } - if (query != null) { - cursor = getResolver.performGet(storIOSQLite, query); - } else if (rawQuery != null) { - cursor = getResolver.performGet(storIOSQLite, rawQuery); - } else { - throw new IllegalStateException("Please specify query"); - } + try { + final int count = cursor.getCount(); - try { - final int count = cursor.getCount(); + if (count == 0) { + return EMPTY_LIST; // it's immutable + } - if (count == 0) { - return EMPTY_LIST; // it's immutable - } + final List list = new ArrayList(count); - final List list = new ArrayList(count); + while (cursor.moveToNext()) { + list.add(getResolver.mapFromCursor(cursor)); + } - while (cursor.moveToNext()) { - list.add(getResolver.mapFromCursor(cursor)); + return unmodifiableList(list); + } finally { + cursor.close(); } - - return unmodifiableList(list); - } finally { - cursor.close(); + } catch (Throwable throwable) { + throw new StorIOException(throwable); } } diff --git a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjects.java b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjects.java index 000290445..bcd83f2d7 100644 --- a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjects.java +++ b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjects.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; import com.pushtorefresh.storio.sqlite.Changes; import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; @@ -57,89 +58,94 @@ public final class PreparedPutCollectionOfObjects extends PreparedPut executeAsBlocking() { - final StorIOSQLite.Internal internal = storIOSQLite.internal(); + try { + final StorIOSQLite.Internal internal = storIOSQLite.internal(); - // Nullable - final List>> objectsAndPutResolvers; + // Nullable + final List>> objectsAndPutResolvers; - if (explicitPutResolver != null) { - objectsAndPutResolvers = null; - } else { - objectsAndPutResolvers = new ArrayList>>(objects.size()); + if (explicitPutResolver != null) { + objectsAndPutResolvers = null; + } else { + objectsAndPutResolvers = new ArrayList>>(objects.size()); - for (final T object : objects) { - final SQLiteTypeMapping typeMapping - = (SQLiteTypeMapping) internal.typeMapping(object.getClass()); + for (final T object : objects) { + final SQLiteTypeMapping typeMapping + = (SQLiteTypeMapping) internal.typeMapping(object.getClass()); - if (typeMapping == null) { - throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + - "object = " + object + ", object.class = " + object.getClass() + "," + - "db was not affected by this operation, please add type mapping for this type"); - } + if (typeMapping == null) { + throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + + "object = " + object + ", object.class = " + object.getClass() + "," + + "db was not affected by this operation, please add type mapping for this type"); + } - objectsAndPutResolvers.add(new SimpleImmutableEntry>( - object, - typeMapping.putResolver() - )); + objectsAndPutResolvers.add(new SimpleImmutableEntry>( + object, + typeMapping.putResolver() + )); + } } - } - if (useTransaction) { - internal.beginTransaction(); - } + if (useTransaction) { + internal.beginTransaction(); + } - final Map results = new HashMap(objects.size()); - boolean transactionSuccessful = false; + final Map results = new HashMap(objects.size()); + boolean transactionSuccessful = false; - try { - if (explicitPutResolver != null) { - for (final T object : objects) { - final PutResult putResult = explicitPutResolver.performPut(storIOSQLite, object); - results.put(object, putResult); + try { + if (explicitPutResolver != null) { + for (final T object : objects) { + final PutResult putResult = explicitPutResolver.performPut(storIOSQLite, object); + results.put(object, putResult); - if (!useTransaction) { - internal.notifyAboutChanges(Changes.newInstance(putResult.affectedTables())); + if (!useTransaction) { + internal.notifyAboutChanges(Changes.newInstance(putResult.affectedTables())); + } } - } - } else { - for (final SimpleImmutableEntry> objectAndPutResolver : objectsAndPutResolvers) { - final T object = objectAndPutResolver.getKey(); - final PutResolver putResolver = objectAndPutResolver.getValue(); + } else { + for (final SimpleImmutableEntry> objectAndPutResolver : objectsAndPutResolvers) { + final T object = objectAndPutResolver.getKey(); + final PutResolver putResolver = objectAndPutResolver.getValue(); - final PutResult putResult = putResolver.performPut(storIOSQLite, object); + final PutResult putResult = putResolver.performPut(storIOSQLite, object); - results.put(object, putResult); + results.put(object, putResult); - if (!useTransaction) { - internal.notifyAboutChanges(Changes.newInstance(putResult.affectedTables())); + if (!useTransaction) { + internal.notifyAboutChanges(Changes.newInstance(putResult.affectedTables())); + } } } - } - if (useTransaction) { - internal.setTransactionSuccessful(); - transactionSuccessful = true; - } - } finally { - if (useTransaction) { - internal.endTransaction(); + if (useTransaction) { + internal.setTransactionSuccessful(); + transactionSuccessful = true; + } + } finally { + if (useTransaction) { + internal.endTransaction(); - // if delete was in transaction and it was successful -> notify about changes - if (transactionSuccessful) { - final Set affectedTables = new HashSet(1); // in most cases it will be 1 table + // if delete was in transaction and it was successful -> notify about changes + if (transactionSuccessful) { + final Set affectedTables = new HashSet(1); // in most cases it will be 1 table - for (final T object : results.keySet()) { - affectedTables.addAll(results.get(object).affectedTables()); - } + for (final T object : results.keySet()) { + affectedTables.addAll(results.get(object).affectedTables()); + } - // IMPORTANT: Notifying about change should be done after end of transaction - // It'll reduce number of possible deadlock situations - internal.notifyAboutChanges(Changes.newInstance(affectedTables)); + // IMPORTANT: Notifying about change should be done after end of transaction + // It'll reduce number of possible deadlock situations + internal.notifyAboutChanges(Changes.newInstance(affectedTables)); + } } } - } - return PutResults.newInstance(results); + return PutResults.newInstance(results); + + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -159,11 +165,15 @@ public PutResults executeAsBlocking() { @NonNull @Override public Observable> createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValues.java b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValues.java index 26cd521b6..3e268cb64 100644 --- a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValues.java +++ b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValues.java @@ -4,6 +4,7 @@ import android.support.annotation.NonNull; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; import com.pushtorefresh.storio.sqlite.Changes; import com.pushtorefresh.storio.sqlite.StorIOSQLite; @@ -44,9 +45,13 @@ public final class PreparedPutContentValues extends PreparedPut { @NonNull @Override public PutResult executeAsBlocking() { - final PutResult putResult = putResolver.performPut(storIOSQLite, contentValues); - storIOSQLite.internal().notifyAboutChanges(Changes.newInstance(putResult.affectedTables())); - return putResult; + try { + final PutResult putResult = putResolver.performPut(storIOSQLite, contentValues); + storIOSQLite.internal().notifyAboutChanges(Changes.newInstance(putResult.affectedTables())); + return putResult; + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -66,11 +71,15 @@ public PutResult executeAsBlocking() { @NonNull @Override public Observable createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValuesIterable.java b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValuesIterable.java index 1ad40a4ff..33fa42293 100644 --- a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValuesIterable.java +++ b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutContentValuesIterable.java @@ -4,6 +4,7 @@ import android.support.annotation.NonNull; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; import com.pushtorefresh.storio.sqlite.Changes; import com.pushtorefresh.storio.sqlite.StorIOSQLite; @@ -54,47 +55,52 @@ public final class PreparedPutContentValuesIterable extends PreparedPut executeAsBlocking() { - final StorIOSQLite.Internal internal = storIOSQLite.internal(); + try { + final StorIOSQLite.Internal internal = storIOSQLite.internal(); - final Map putResults = new HashMap(); + final Map putResults = new HashMap(); - if (useTransaction) { - internal.beginTransaction(); - } + if (useTransaction) { + internal.beginTransaction(); + } - boolean transactionSuccessful = false; + boolean transactionSuccessful = false; - try { - for (ContentValues contentValues : contentValuesIterable) { - final PutResult putResult = putResolver.performPut(storIOSQLite, contentValues); - putResults.put(contentValues, putResult); + try { + for (ContentValues contentValues : contentValuesIterable) { + final PutResult putResult = putResolver.performPut(storIOSQLite, contentValues); + putResults.put(contentValues, putResult); - if (!useTransaction) { - internal.notifyAboutChanges(Changes.newInstance(putResult.affectedTables())); + if (!useTransaction) { + internal.notifyAboutChanges(Changes.newInstance(putResult.affectedTables())); + } } - } - if (useTransaction) { - storIOSQLite.internal().setTransactionSuccessful(); - transactionSuccessful = true; - } - } finally { - if (useTransaction) { - storIOSQLite.internal().endTransaction(); + if (useTransaction) { + storIOSQLite.internal().setTransactionSuccessful(); + transactionSuccessful = true; + } + } finally { + if (useTransaction) { + storIOSQLite.internal().endTransaction(); - if (transactionSuccessful) { - final Set affectedTables = new HashSet(1); // in most cases it will be 1 table + if (transactionSuccessful) { + final Set affectedTables = new HashSet(1); // in most cases it will be 1 table - for (final ContentValues contentValues : putResults.keySet()) { - affectedTables.addAll(putResults.get(contentValues).affectedTables()); - } + for (final ContentValues contentValues : putResults.keySet()) { + affectedTables.addAll(putResults.get(contentValues).affectedTables()); + } - storIOSQLite.internal().notifyAboutChanges(Changes.newInstance(affectedTables)); + storIOSQLite.internal().notifyAboutChanges(Changes.newInstance(affectedTables)); + } } } - } - return PutResults.newInstance(putResults); + return PutResults.newInstance(putResults); + + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -114,11 +120,15 @@ public PutResults executeAsBlocking() { @NonNull @Override public Observable> createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutObject.java b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutObject.java index e80dacc22..ab16a4696 100644 --- a/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutObject.java +++ b/storio-sqlite/src/main/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutObject.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking; import com.pushtorefresh.storio.sqlite.Changes; import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; @@ -49,27 +50,32 @@ public final class PreparedPutObject extends PreparedPut { @NonNull @Override public PutResult executeAsBlocking() { - final StorIOSQLite.Internal internal = storIOSQLite.internal(); + try { + final StorIOSQLite.Internal internal = storIOSQLite.internal(); - final PutResolver putResolver; + final PutResolver putResolver; - if (explicitPutResolver != null) { - putResolver = explicitPutResolver; - } else { - final SQLiteTypeMapping typeMapping = internal.typeMapping((Class) object.getClass()); + if (explicitPutResolver != null) { + putResolver = explicitPutResolver; + } else { + final SQLiteTypeMapping typeMapping = internal.typeMapping((Class) object.getClass()); - if (typeMapping == null) { - throw new IllegalStateException("Object does not have type mapping: " + - "object = " + object + ", object.class = " + object.getClass() + "," + - "db was not affected by this operation, please add type mapping for this type"); + if (typeMapping == null) { + throw new IllegalStateException("Object does not have type mapping: " + + "object = " + object + ", object.class = " + object.getClass() + "," + + "db was not affected by this operation, please add type mapping for this type"); + } + + putResolver = typeMapping.putResolver(); } - putResolver = typeMapping.putResolver(); - } + final PutResult putResult = putResolver.performPut(storIOSQLite, object); + internal.notifyAboutChanges(Changes.newInstance(putResult.affectedTables())); + return putResult; - final PutResult putResult = putResolver.performPut(storIOSQLite, object); - internal.notifyAboutChanges(Changes.newInstance(putResult.affectedTables())); - return putResult; + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** @@ -89,11 +95,15 @@ public PutResult executeAsBlocking() { @NonNull @Override public Observable createObservable() { - throwExceptionIfRxJavaIsNotAvailable("createObservable()"); - - return Observable - .create(OnSubscribeExecuteAsBlocking.newInstance(this)) - .subscribeOn(Schedulers.io()); + try { + throwExceptionIfRxJavaIsNotAvailable("createObservable()"); + + return Observable + .create(OnSubscribeExecuteAsBlocking.newInstance(this)) + .subscribeOn(Schedulers.io()); + } catch (Throwable throwable) { + throw new StorIOException(throwable); + } } /** diff --git a/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjectsTest.java b/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjectsTest.java index 84341b6cc..919a782ff 100644 --- a/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjectsTest.java +++ b/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteCollectionOfObjectsTest.java @@ -1,5 +1,6 @@ package com.pushtorefresh.storio.sqlite.operations.delete; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.queries.DeleteQuery; @@ -176,7 +177,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutTransactionWithout try { preparedDelete.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -210,7 +211,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutTransactionWithout testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOSQLite).delete(); verify(storIOSQLite).internal(); @@ -239,7 +240,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithTransactionWithoutAff try { preparedDelete.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -273,7 +274,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithTransactionWithoutAff testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOSQLite).delete(); verify(storIOSQLite).internal(); diff --git a/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObjectTest.java b/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObjectTest.java index caacc75b1..903ee3113 100644 --- a/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObjectTest.java +++ b/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/delete/PreparedDeleteObjectTest.java @@ -1,5 +1,6 @@ package com.pushtorefresh.storio.sqlite.operations.delete; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.queries.DeleteQuery; @@ -100,7 +101,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAffectingDbBlockin try { preparedDelete.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -131,7 +132,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAffectingDbAsObser testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOSQLite).delete(); verify(storIOSQLite).internal(); diff --git a/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetListOfObjectsTest.java b/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetListOfObjectsTest.java index c461bd3a6..4244dfef6 100644 --- a/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetListOfObjectsTest.java +++ b/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/get/PreparedGetListOfObjectsTest.java @@ -1,5 +1,6 @@ package com.pushtorefresh.storio.sqlite.operations.get; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.queries.Query; import com.pushtorefresh.storio.sqlite.queries.RawQuery; @@ -171,7 +172,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAccessingDbWithQue try { preparedGet.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -199,7 +200,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAccessingDbWithRaw try { preparedGet.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -233,7 +234,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAccessingDbWithQue testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOSQLite).get(); verify(storIOSQLite).internal(); @@ -263,7 +264,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutAccessingDbWithRaw testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOSQLite).get(); verify(storIOSQLite).internal(); diff --git a/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjectsTest.java b/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjectsTest.java index b43128e92..6088d76a6 100644 --- a/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjectsTest.java +++ b/storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/operations/put/PreparedPutCollectionOfObjectsTest.java @@ -2,6 +2,7 @@ import android.content.ContentValues; +import com.pushtorefresh.storio.StorIOException; import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.queries.InsertQuery; import com.pushtorefresh.storio.sqlite.queries.UpdateQuery; @@ -179,7 +180,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutTransactionWithout try { preparedPut.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -214,7 +215,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithoutTransactionWithout testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOSQLite).put(); verify(storIOSQLite).internal(); @@ -244,7 +245,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithTransactionWithoutAff try { preparedPut.executeAsBlocking(); fail(); - } catch (IllegalStateException expected) { + } catch (StorIOException expected) { // it's okay, no type mapping was found } @@ -279,7 +280,7 @@ public void shouldThrowExceptionIfNoTypeMappingWasFoundWithTransactionWithoutAff testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoValues(); - testSubscriber.assertError(IllegalStateException.class); + testSubscriber.assertError(StorIOException.class); verify(storIOSQLite).put(); verify(storIOSQLite).internal();