android.database.CursorWrapper Java Examples

The following examples show how to use android.database.CursorWrapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CursorUtil.java    From droitatedDB with Apache License 2.0 6 votes vote down vote up
/**
 * Convert a standard Android {@link Cursor} into a {@link org.droitateddb.cursor.ObjectCursor}.<br>
 * This is only possible for {@link Cursor}s that where queried over a {@link ContentResolver} from a
 * {@link ContentProvider} derived by {@link BaseContentProvider}.
 *
 * @param <T>    Entity class represented within the Cursor
 * @param cursor Android {@link Cursor}
 * @return The {@link org.droitateddb.cursor.ObjectCursor} representation of the given Android {@link Cursor}
 * @throws NullPointerException     When the given cursor is null
 * @throws IllegalArgumentException When the given cursor is not of the type {@link CursorWrapper}, which will be returned by a
 *                                  {@link ContentResolver}
 * @throws IllegalStateException    When the wrapped cursor within the given cursor is not of type {@link org.droitateddb.cursor.ObjectCursor}. This indicates
 *                                  that the given cursor was not queried from a derivation {@link BaseContentProvider}
 */
@SuppressWarnings("unchecked")
public static <T> ObjectCursor<T> getObjectCursor(final Cursor cursor) {
	if (cursor == null) {
		throw new NullPointerException("The given cursor is null");
	}
	if (!(cursor instanceof CursorWrapper)) {
		throw new IllegalArgumentException(
				"The given cursor is not of type " + CursorWrapper.class.getCanonicalName() + ". It has type " + cursor.getClass().getCanonicalName() +
						". Was it queried with a ContentResolver?");
	}

	CursorWrapper wrapper = (CursorWrapper) cursor;
	Cursor wrappedCursor = wrapper.getWrappedCursor();

	if (!(wrappedCursor instanceof ObjectCursor)) {
		throw new IllegalStateException(
				"The wrapped cursor of the given CursorWrapper is not of type " + ObjectCursor.class.getCanonicalName() + ". It has type " +
						wrappedCursor.getClass().getCanonicalName() +
						". Was it queried over a ContentResolver from BaseContentProvider derived ContentProvider?");
	}
	return (ObjectCursor<T>) wrappedCursor;
}
 
Example #2
Source File: ContentProviderTest.java    From droitatedDB with Apache License 2.0 4 votes vote down vote up
@Test
public void crudTest() {

    ContentResolver resolver = context.getContentResolver();
    Comment c1 = new Comment("asdf");
    EntityService<Comment> entityService = entityService(Comment.class);

    entityService.save(c1);
    assertThat(entityService.get()).hasSize(1);
    Uri uri = CommentContentProvider.uri(DB.CommentTable.TABLE_NAME);

    // create
    ContentValues values = new ContentValues();
    values.put("name", "aName");
    Uri itemLocation = resolver.insert(uri, values);
    assertThat("content://org.droitateddb.test.data.generated.provider.comment/comment/2").isEqualTo(itemLocation.toString());
    assertThat(entityService.get()).hasSize(2);
    assertThat(entityService.get(2).getName()).isEqualTo("aName");

    // update
    values.put("name", "otherName");
    int update = resolver.update(itemLocation, values, null, null);
    assertThat(1).isEqualTo(update);
    assertThat(entityService.get(2).getName()).isEqualTo("otherName");

    // read
    Cursor cursor = resolver.query(itemLocation, DB.CommentTable.PROJECTION, null, null, null);
    assertThat(cursor).isNotNull();
    // Wrap to unwrap ... this is a bit strange, normally Android wraps the Cursor but robolectric doesn't!
    CursorWrapper wrapped = new CursorWrapper(cursor);
    ObjectCursor<Comment> objectCursor = CursorUtil.getObjectCursor(wrapped);

    assertThat(objectCursor.size()).isEqualTo(1);
    Comment curserloadedObject = objectCursor.getOne();
    assertThat(curserloadedObject.getName()).isEqualTo("otherName");
    assertSameFields(curserloadedObject, entityService.get(curserloadedObject.getId()));

    // read all
    Cursor nextCursor = resolver.query(uri, DB.CommentTable.PROJECTION, null, null, null);
    assertThat(nextCursor).isNotNull();
    // Wrap to unwrap ... this is a bit strange, normally Android wraps the Cursor but robolectric doesn't!
    CursorWrapper nextWrapped = new CursorWrapper(nextCursor);
    ObjectCursor<Comment> allObjectCursor = CursorUtil.getObjectCursor(nextWrapped);

    assertThat(allObjectCursor.size()).isEqualTo(2);

    // delete
    int delete = resolver.delete(itemLocation, null, null);
    assertThat(entityService.get()).hasSize(1);
    assertThat(1).isEqualTo(delete);

}
 
Example #3
Source File: ContentProviderTest.java    From droitatedDB with Apache License 2.0 4 votes vote down vote up
private Cursor getSingleCursor() {
    Cursor cursor = context.getContentResolver().query(CommentContentProvider.uri(DB.SingleTable.TABLE_NAME), DB.SingleTable.PROJECTION, null, null, null);
    // Wrap to unwrap ... this is a bit strange, normally Android wraps the Cursor but robolectric doesn't!
    CursorWrapper wrapped = new CursorWrapper(cursor);
    return wrapped;
}
 
Example #4
Source File: CursorUtilTest.java    From droitatedDB with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void throwsIllegalArgumentExceptionForWrongWrappedCursor() throws Exception {
	CursorUtil.getObjectCursor(new CursorWrapper(new CursorDummy()));
}