Search in sources :

Example 1 with TrayException

use of net.grandcentrix.tray.core.TrayException in project tray by grandcentrix.

the class TrayProviderHelperTest method testQueryFails.

public void testQueryFails() throws Exception {
    Uri uri = new TrayUri(getProviderMockContext()).get();
    MockContentProvider mockContentProvider = new MockContentProvider(getProviderMockContext()) {

        @Override
        public Cursor query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {
            throw new IllegalStateException("something serious is wrong");
        }
    };
    getProviderMockContext().addProvider(uri.getAuthority(), mockContentProvider);
    getProviderMockContext().enableMockResolver(true);
    try {
        mProviderHelper.queryProvider(getUri(MODULE_A, KEY_A));
        fail("did not throw");
    } catch (TrayException e) {
        e.getCause().getMessage().equals("something serious is wrong");
    }
    final List<TrayItem> trayItems = mProviderHelper.queryProviderSafe(getUri(MODULE_A, KEY_A));
    assertEquals(0, trayItems.size());
}
Also used : TrayException(net.grandcentrix.tray.core.TrayException) TrayItem(net.grandcentrix.tray.core.TrayItem) MockContentProvider(android.test.mock.MockContentProvider) Uri(android.net.Uri)

Example 2 with TrayException

use of net.grandcentrix.tray.core.TrayException in project tray by grandcentrix.

the class TrayProviderHelper method queryProvider.

/**
     * sends a query for TrayItems to the provider
     *
     * @param uri path to data
     * @return list of items
     * @throws TrayException when something is wrong with the provider/database
     */
@NonNull
public List<TrayItem> queryProvider(@NonNull final Uri uri) throws TrayException {
    final Cursor cursor;
    try {
        cursor = mContext.getContentResolver().query(uri, null, null, null, null);
    } catch (Throwable e) {
        throw new TrayException("Hard error accessing the ContentProvider", e);
    }
    // Return Preference if found
    if (cursor == null) {
        // When running in here, please check if your ContentProvider has the correct authority
        throw new TrayException("could not access stored data with uri " + uri);
    }
    final ArrayList<TrayItem> list = new ArrayList<>();
    for (boolean hasItem = cursor.moveToFirst(); hasItem; hasItem = cursor.moveToNext()) {
        final TrayItem trayItem = cursorToTrayItem(cursor);
        list.add(trayItem);
    }
    cursor.close();
    return list;
}
Also used : TrayException(net.grandcentrix.tray.core.TrayException) TrayItem(net.grandcentrix.tray.core.TrayItem) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) NonNull(android.support.annotation.NonNull)

Example 3 with TrayException

use of net.grandcentrix.tray.core.TrayException in project tray by grandcentrix.

the class TrayProviderHelperTest method testQueryProviderWithUnregisteredProvider.

public void testQueryProviderWithUnregisteredProvider() throws Exception {
    final Context context = mock(Context.class);
    final ContentResolver contentResolver = mock(ContentResolver.class);
    when(context.getContentResolver()).thenReturn(contentResolver);
    final TrayProviderHelper trayProviderHelper = new TrayProviderHelper(context);
    final Uri uri = mTrayUri.get();
    try {
        trayProviderHelper.queryProvider(uri);
        fail();
    } catch (TrayException e) {
        assertTrue(e.getMessage().contains(uri.toString()));
    }
}
Also used : Context(android.content.Context) IsolatedContext(android.test.IsolatedContext) TrayException(net.grandcentrix.tray.core.TrayException) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

Aggregations

TrayException (net.grandcentrix.tray.core.TrayException)3 Uri (android.net.Uri)2 TrayItem (net.grandcentrix.tray.core.TrayItem)2 ContentResolver (android.content.ContentResolver)1 Context (android.content.Context)1 Cursor (android.database.Cursor)1 NonNull (android.support.annotation.NonNull)1 IsolatedContext (android.test.IsolatedContext)1 MockContentProvider (android.test.mock.MockContentProvider)1 ArrayList (java.util.ArrayList)1