private GetObjectStub(boolean withTypeMapping) { this.withTypeMapping = withTypeMapping; storIOSQLite = mock(StorIOSQLite.class); internal = mock(StorIOSQLite.Internal.class); when(storIOSQLite.internal()).thenReturn(internal); query = Query.builder().table("test_table").build(); rawQuery = RawQuery.builder().query("select * from who_cares").observesTables("test_table").build(); //noinspection unchecked getResolver = mock(GetResolver.class); cursor = mock(Cursor.class); item = new TestItem(); when(cursor.getCount()).thenReturn(1); when(cursor.moveToNext()) .thenAnswer( new Answer<Boolean>() { int invocationsCount = 0; @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { return invocationsCount++ < 1; } }); when(storIOSQLite.get()).thenReturn(new PreparedGet.Builder(storIOSQLite)); when(storIOSQLite.observeChangesInTables(eq(singleton(query.table())))) .thenReturn(Observable.<Changes>empty()); assertThat(rawQuery.observesTables()).isNotNull(); when(storIOSQLite.observeChangesInTables(rawQuery.observesTables())) .thenReturn(Observable.<Changes>empty()); when(getResolver.performGet(storIOSQLite, query)).thenReturn(cursor); when(getResolver.performGet(storIOSQLite, rawQuery)).thenReturn(cursor); when(getResolver.mapFromCursor(cursor)).thenReturn(item); //noinspection unchecked typeMapping = mock(SQLiteTypeMapping.class); if (withTypeMapping) { when(internal.typeMapping(TestItem.class)).thenReturn(typeMapping); when(typeMapping.getResolver()).thenReturn(getResolver); } }
/** * 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.Internal internal = storIOSQLite.internal(); final PutResolver<T> putResolver; if (explicitPutResolver != null) { putResolver = explicitPutResolver; } else { final SQLiteTypeMapping<T> typeMapping = internal.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()) { internal.notifyAboutChanges(Changes.newInstance(putResult.affectedTables())); } return putResult; } catch (Exception exception) { throw new StorIOException(exception); } }