use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.
the class OnSubscribeExecuteAsBlockingSingleTest method shouldCallOnErrorIfExceptionOccurred.
@SuppressWarnings("CheckResult")
@Test
public void shouldCallOnErrorIfExceptionOccurred() {
//noinspection unchecked
final PreparedOperation<Object> preparedOperation = mock(PreparedOperation.class);
StorIOException expectedException = new StorIOException("test exception");
when(preparedOperation.executeAsBlocking()).thenThrow(expectedException);
TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>();
Single<Object> single = Single.create(OnSubscribeExecuteAsBlockingSingle.newInstance(preparedOperation));
verifyZeroInteractions(preparedOperation);
single.subscribe(testSubscriber);
testSubscriber.assertError(expectedException);
testSubscriber.assertTerminalEvent();
verify(preparedOperation).executeAsBlocking();
verify(preparedOperation, never()).asRxSingle();
verify(preparedOperation, never()).asRxObservable();
}
use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.
the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForObservable.
@Test
public void shouldWrapExceptionIntoStorIOExceptionForObservable() {
final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
Uri testUri = mock(Uri.class);
when(storIOContentResolver.observeChangesOfUri(eq(testUri))).thenReturn(Observable.<Changes>empty());
//noinspection unchecked
final GetResolver<Integer> getResolver = mock(GetResolver.class);
when(getResolver.performGet(eq(storIOContentResolver), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
final TestSubscriber<Integer> testSubscriber = new TestSubscriber<Integer>();
new PreparedGetNumberOfResults.Builder(storIOContentResolver).withQuery(Query.builder().uri(testUri).build()).withGetResolver(getResolver).prepare().asRxObservable().subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent(60, SECONDS);
testSubscriber.assertError(StorIOException.class);
assertThat(testSubscriber.getOnErrorEvents()).hasSize(1);
StorIOException storIOException = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
IllegalStateException cause = (IllegalStateException) storIOException.getCause();
assertThat(cause).hasMessage("test exception");
testSubscriber.unsubscribe();
}
use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.
the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForSingle.
@Test
public void shouldWrapExceptionIntoStorIOExceptionForSingle() {
final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
Uri testUri = mock(Uri.class);
//noinspection unchecked
final GetResolver<Integer> getResolver = mock(GetResolver.class);
when(getResolver.performGet(eq(storIOContentResolver), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
final TestSubscriber<Integer> testSubscriber = new TestSubscriber<Integer>();
new PreparedGetNumberOfResults.Builder(storIOContentResolver).withQuery(Query.builder().uri(testUri).build()).withGetResolver(getResolver).prepare().asRxSingle().subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent(60, SECONDS);
testSubscriber.assertError(StorIOException.class);
assertThat(testSubscriber.getOnErrorEvents()).hasSize(1);
StorIOException storIOException = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
IllegalStateException cause = (IllegalStateException) storIOException.getCause();
assertThat(cause).hasMessage("test exception");
testSubscriber.unsubscribe();
}
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 result of Put Operation.
*/
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public PutResult executeAsBlocking() {
try {
final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
final PutResolver<T> putResolver;
if (explicitPutResolver != null) {
putResolver = explicitPutResolver;
} 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");
}
putResolver = typeMapping.putResolver();
}
final PutResult putResult = putResolver.performPut(storIOSQLite, object);
if (putResult.wasInserted() || putResult.wasUpdated()) {
lowLevel.notifyAboutChanges(Changes.newInstance(putResult.affectedTables()));
}
return putResult;
} catch (Exception exception) {
throw new StorIOException("Error has occurred during Put operation. object = " + object, exception);
}
}
use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.
the class PreparedDeleteCollectionOfObjects 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 results of Delete Operation.
*/
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public DeleteResults<T> executeAsBlocking() {
try {
final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
// Nullable
final List<SimpleImmutableEntry<T, DeleteResolver<T>>> objectsAndDeleteResolvers;
if (explicitDeleteResolver != null) {
objectsAndDeleteResolvers = null;
} else {
objectsAndDeleteResolvers = new ArrayList<SimpleImmutableEntry<T, DeleteResolver<T>>>(objects.size());
for (final T object : objects) {
final SQLiteTypeMapping<T> typeMapping = (SQLiteTypeMapping<T>) lowLevel.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");
}
objectsAndDeleteResolvers.add(new SimpleImmutableEntry<T, DeleteResolver<T>>(object, typeMapping.deleteResolver()));
}
}
if (useTransaction) {
lowLevel.beginTransaction();
}
final Map<T, DeleteResult> results = new HashMap<T, DeleteResult>(objects.size());
boolean transactionSuccessful = false;
try {
if (explicitDeleteResolver != null) {
for (final T object : objects) {
final DeleteResult deleteResult = explicitDeleteResolver.performDelete(storIOSQLite, object);
results.put(object, deleteResult);
if (!useTransaction && deleteResult.numberOfRowsDeleted() > 0) {
lowLevel.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables()));
}
}
} else {
for (final SimpleImmutableEntry<T, DeleteResolver<T>> objectAndDeleteResolver : objectsAndDeleteResolvers) {
final T object = objectAndDeleteResolver.getKey();
final DeleteResolver<T> deleteResolver = objectAndDeleteResolver.getValue();
final DeleteResult deleteResult = deleteResolver.performDelete(storIOSQLite, object);
results.put(object, deleteResult);
if (!useTransaction && deleteResult.numberOfRowsDeleted() > 0) {
lowLevel.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables()));
}
}
}
if (useTransaction) {
lowLevel.setTransactionSuccessful();
transactionSuccessful = true;
}
} finally {
if (useTransaction) {
lowLevel.endTransaction();
// if delete was in transaction and it was successful -> notify about changes
if (transactionSuccessful) {
// in most cases it will be one table
final Set<String> affectedTables = new HashSet<String>(1);
for (final T object : results.keySet()) {
final DeleteResult deleteResult = results.get(object);
if (deleteResult.numberOfRowsDeleted() > 0) {
affectedTables.addAll(results.get(object).affectedTables());
}
}
// It'll reduce number of possible deadlock situations
if (!affectedTables.isEmpty()) {
lowLevel.notifyAboutChanges(Changes.newInstance(affectedTables));
}
}
}
}
return DeleteResults.newInstance(results);
} catch (Exception exception) {
throw new StorIOException("Error has occurred during Delete operation. objects = " + objects, exception);
}
}
Aggregations