DbAdapter implements StorageService { public List<Table> getTables() { Cursor c = null; List<Table> tables = new ArrayList<>(); try { final SQLiteDatabase db = mDb.getReadableDatabase(); c = db.query(TABLES_TABLE, null, null, null, null, null, null); while (c.moveToNext()) { String name = c.getString(c.getColumnIndex(KEY_TABLE)); String token = c.getString(c.getColumnIndex(KEY_TOKEN)); tables.add(new Table(name, token)); } } catch (final SQLiteException e) { Logger.log(TAG, "Failed to get all tables" + e.getMessage(), Logger.SDK_DEBUG); } finally { if (null != c) c.close(); mDb.close(); } return tables; } DbAdapter(Context context); static DbAdapter getInstance(Context context); int addEvent(Table table, String data); int count(Table table); Batch getEvents(Table table, int limit); List<Table> getTables(); int deleteEvents(Table table, String lastId); void deleteTable(Table table); void vacuum(); static final String KEY_DATA; static final String KEY_TOKEN; static final String KEY_TABLE; static final String KEY_CREATED_AT; static final String TABLES_TABLE; static final String REPORTS_TABLE; }
@Test public void getTables() { Cursor cursor = mock(Cursor.class); SQLiteDatabase db = mock(SQLiteDatabase.class); when(db.query(anyString(), any(String[].class), anyString(), any(String[].class), anyString(),anyString(), anyString())).thenReturn(cursor); when(handler.getReadableDatabase()).thenReturn(db); when(cursor.moveToNext()).thenReturn(true, true, true, false); when(cursor.getString(anyInt())).thenReturn("table1", "token1", "table2", "token2", "table3", "token3"); List<Table> tables = adapter.getTables(); assertEquals(tables.size(), 3); int i = 1; for (Table table : tables) { assertEquals(table.name, "table" + i); assertEquals(table.token, "token" + i++); } verify(cursor, times(1)).close(); verify(handler, times(1)).close(); }