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