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();
}
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");
}
}
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);
}
}
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);
}
}
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);
}
Aggregations