Search in sources :

Example 21 with StorIOException

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

the class PreparedGetCursorTest method shouldWrapExceptionIntoStorIOExceptionForSingle.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForSingle() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    //noinspection unchecked
    final GetResolver<Cursor> getResolver = mock(GetResolver.class);
    when(getResolver.performGet(eq(storIOSQLite), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
    final TestSubscriber<Cursor> testSubscriber = new TestSubscriber<Cursor>();
    new PreparedGetCursor.Builder(storIOSQLite).withQuery(Query.builder().table("test_table").build()).withGetResolver(getResolver).prepare().asRxSingle().subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent(60, SECONDS);
    testSubscriber.assertError(StorIOException.class);
    StorIOException storIOException = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
    IllegalStateException cause = (IllegalStateException) storIOException.getCause();
    assertThat(cause).hasMessage("test exception");
}
Also used : Query(com.pushtorefresh.storio.sqlite.queries.Query) StorIOException(com.pushtorefresh.storio.StorIOException) TestSubscriber(rx.observers.TestSubscriber) Cursor(android.database.Cursor) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 22 with StorIOException

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

the class PreparedGetCursorTest method shouldWrapExceptionIntoStorIOExceptionForBlocking.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForBlocking() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    //noinspection unchecked
    final GetResolver<Cursor> getResolver = mock(GetResolver.class);
    when(getResolver.performGet(eq(storIOSQLite), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
    try {
        new PreparedGetCursor.Builder(storIOSQLite).withQuery(Query.builder().table("test_table").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.sqlite.queries.Query) StorIOException(com.pushtorefresh.storio.StorIOException) Cursor(android.database.Cursor) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 23 with StorIOException

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

the class PreparedPutContentValuesIterableTest method shouldFinishTransactionIfExceptionHasOccurredObservable.

@Test
public void shouldFinishTransactionIfExceptionHasOccurredObservable() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
    when(storIOSQLite.lowLevel()).thenReturn(internal);
    //noinspection unchecked
    final PutResolver<ContentValues> putResolver = mock(PutResolver.class);
    final List<ContentValues> contentValues = singletonList(mock(ContentValues.class));
    when(putResolver.performPut(same(storIOSQLite), any(ContentValues.class))).thenThrow(new IllegalStateException("test exception"));
    final TestSubscriber<PutResults<ContentValues>> testSubscriber = new TestSubscriber<PutResults<ContentValues>>();
    new PreparedPutContentValuesIterable.Builder(storIOSQLite, contentValues).withPutResolver(putResolver).useTransaction(true).prepare().asRxObservable().subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent();
    testSubscriber.assertNoValues();
    testSubscriber.assertError(StorIOException.class);
    //noinspection ThrowableResultOfMethodCallIgnored
    StorIOException expected = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
    IllegalStateException cause = (IllegalStateException) expected.getCause();
    assertThat(cause).hasMessage("test exception");
    verify(internal).beginTransaction();
    verify(internal, never()).setTransactionSuccessful();
    verify(internal).endTransaction();
    verify(storIOSQLite).lowLevel();
    verify(storIOSQLite).defaultScheduler();
    verify(putResolver).performPut(same(storIOSQLite), any(ContentValues.class));
    verifyNoMoreInteractions(storIOSQLite, internal, putResolver);
}
Also used : ContentValues(android.content.ContentValues) StorIOException(com.pushtorefresh.storio.StorIOException) TestSubscriber(rx.observers.TestSubscriber) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 24 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 DeleteResolver<T> deleteResolver;
        if (explicitDeleteResolver != null) {
            deleteResolver = explicitDeleteResolver;
        } else {
            final ContentResolverTypeMapping<T> typeMapping = storIOContentResolver.lowLevel().typeMapping((Class<T>) 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();
        }
        return deleteResolver.performDelete(storIOContentResolver, object);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Delete operation. object = " + object, exception);
    }
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 25 with StorIOException

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

the class PreparedPutObject method executeAsBlocking.

/**
     * Executes Put 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 results of Put Operation.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public PutResult executeAsBlocking() {
    try {
        final PutResolver<T> putResolver;
        if (explicitPutResolver != null) {
            putResolver = explicitPutResolver;
        } else {
            final ContentResolverTypeMapping<T> typeMapping = storIOContentResolver.lowLevel().typeMapping((Class<T>) 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();
        }
        return putResolver.performPut(storIOContentResolver, object);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Put operation. object = " + object, exception);
    }
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

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