Search in sources :

Example 6 with StorIOException

use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.

the class OnSubscribeExecuteAsBlockingCompletableTest method shouldCallOnErrorIfExceptionOccurred.

@SuppressWarnings({ "ThrowableInstanceNeverThrown", "ResourceType" })
@Test
public void shouldCallOnErrorIfExceptionOccurred() {
    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, never()).asRxObservable();
    verify(preparedOperation, never()).asRxSingle();
    verify(preparedOperation, never()).asRxCompletable();
}
Also used : Completable(rx.Completable) PreparedWriteOperation(com.pushtorefresh.storio.operations.PreparedWriteOperation) StorIOException(com.pushtorefresh.storio.StorIOException) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 7 with StorIOException

use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.

the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForBlocking.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForBlocking() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    //noinspection unchecked
    final GetResolver<Integer> getResolver = mock(GetResolver.class);
    when(getResolver.performGet(eq(storIOContentResolver), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
    try {
        new PreparedGetNumberOfResults.Builder(storIOContentResolver).withQuery(Query.builder().uri(mock(Uri.class)).build()).withGetResolver(getResolver).prepare().executeAsBlocking();
        failBecauseExceptionWasNotThrown(StorIOException.class);
    } catch (StorIOException expected) {
        IllegalStateException cause = (IllegalStateException) expected.getCause();
        assertThat(cause).hasMessage("test exception");
    }
}
Also used : Query(com.pushtorefresh.storio.contentresolver.queries.Query) StorIOException(com.pushtorefresh.storio.StorIOException) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 8 with StorIOException

use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.

the class PreparedDeleteObject method executeAsBlocking.

/**
     * Executes Delete Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null result of Delete Operation.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public DeleteResult executeAsBlocking() {
    try {
        final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
        final DeleteResolver<T> deleteResolver;
        if (explicitDeleteResolver != null) {
            deleteResolver = explicitDeleteResolver;
        } else {
            final SQLiteTypeMapping<T> typeMapping = lowLevel.typeMapping((Class<T>) 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");
            }
            deleteResolver = typeMapping.deleteResolver();
        }
        final DeleteResult deleteResult = deleteResolver.performDelete(storIOSQLite, object);
        if (deleteResult.numberOfRowsDeleted() > 0) {
            lowLevel.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables()));
        }
        return deleteResult;
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Delete operation. object = " + object, exception);
    }
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 9 with StorIOException

use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.

the class PreparedExecuteSQL method executeAsBlocking.

/**
     * Executes SQL Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return just a new instance of {@link Object}, actually Execute SQL should return {@code void},
     * but we can not return instance of {@link Void} so we just return {@link Object}
     * and you don't have to deal with {@code null}.
     */
@WorkerThread
@NonNull
@Override
public Object executeAsBlocking() {
    try {
        final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
        lowLevel.executeSQL(rawQuery);
        final Set<String> affectedTables = rawQuery.affectsTables();
        if (affectedTables.size() > 0) {
            lowLevel.notifyAboutChanges(Changes.newInstance(affectedTables));
        }
        return new Object();
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during ExecuteSQL operation. query = " + rawQuery, exception);
    }
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 10 with StorIOException

use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.

the class PreparedGetCursor method asRxObservable.

/**
     * Creates "Hot" {@link Observable} which will be subscribed to changes of tables from query
     * and will emit result each time change occurs.
     * <p>
     * First result will be emitted immediately after subscription,
     * other emissions will occur only if changes of tables from query will occur during lifetime of
     * the {@link Observable}.
     * <dl>
     * <dt><b>Scheduler:</b></dt>
     * <dd>Operates on {@link StorIOSQLite#defaultScheduler()} if not {@code null}.</dd>
     * </dl>
     * <p>
     * Please don't forget to unsubscribe from this {@link Observable} because
     * it's "Hot" and endless.
     *
     * @return non-null {@link Observable} which will emit non-null
     * list with mapped results and will be subscribed to changes of tables from query.
     */
@NonNull
@CheckResult
@Override
public Observable<Cursor> asRxObservable() {
    throwExceptionIfRxJavaIsNotAvailable("asRxObservable()");
    final Set<String> tables;
    if (query != null) {
        tables = new HashSet<String>(1);
        tables.add(query.table());
    } else if (rawQuery != null) {
        tables = rawQuery.observesTables();
    } else {
        throw new StorIOException("Please specify query");
    }
    final Observable<Cursor> observable;
    if (!tables.isEmpty()) {
        observable = storIOSQLite.observeChangesInTables(// each change triggers executeAsBlocking
        tables).map(MapSomethingToExecuteAsBlocking.newInstance(this)).startWith(// start stream with first query result
        Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))).onBackpressureLatest();
    } else {
        observable = Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this));
    }
    return RxJavaUtils.subscribeOn(storIOSQLite, observable);
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) Cursor(android.database.Cursor) NonNull(android.support.annotation.NonNull) CheckResult(android.support.annotation.CheckResult)

Aggregations

StorIOException (com.pushtorefresh.storio.StorIOException)40 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)24 Test (org.junit.Test)23 TestSubscriber (rx.observers.TestSubscriber)17 NonNull (android.support.annotation.NonNull)15 WorkerThread (android.support.annotation.WorkerThread)15 ContentValues (android.content.ContentValues)10 Cursor (android.database.Cursor)8 Query (com.pushtorefresh.storio.sqlite.queries.Query)6 HashMap (java.util.HashMap)6 StorIOContentResolver (com.pushtorefresh.storio.contentresolver.StorIOContentResolver)5 DeleteQuery (com.pushtorefresh.storio.sqlite.queries.DeleteQuery)4 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)4 Query (com.pushtorefresh.storio.contentresolver.queries.Query)3 HashSet (java.util.HashSet)3 Uri (android.net.Uri)2 CheckResult (android.support.annotation.CheckResult)2 Nullable (android.support.annotation.Nullable)2 ContentResolverTypeMapping (com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping)2 SQLiteTypeMapping (com.pushtorefresh.storio.sqlite.SQLiteTypeMapping)2