Search in sources :

Example 86 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project incubator-weex by apache.

the class HistoryManager method buildHistoryItem.

public HistoryItem buildHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(number + 1);
        String text = cursor.getString(0);
        String display = cursor.getString(1);
        String format = cursor.getString(2);
        long timestamp = cursor.getLong(3);
        String details = cursor.getString(4);
        Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
        return new HistoryItem(result, display, details);
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) Result(com.google.zxing.Result)

Example 87 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project AppCenter-SDK-Android by Microsoft.

the class DatabaseManagerTest method getDatabaseFailedOnce.

@Test
public void getDatabaseFailedOnce() {
    /* Mocking instances. */
    Context contextMock = mock(Context.class);
    SQLiteOpenHelper helperMock = mock(SQLiteOpenHelper.class);
    when(helperMock.getWritableDatabase()).thenThrow(new RuntimeException()).thenReturn(mock(SQLiteDatabase.class));
    /* Instantiate real instance for DatabaseManager. */
    DatabaseManager databaseManager = new DatabaseManager(contextMock, "database", "table", 1, null, null);
    databaseManager.setSQLiteOpenHelper(helperMock);
    /* Get database. */
    SQLiteDatabase database = databaseManager.getDatabase();
    /* Verify. */
    assertNotNull(database);
    verify(contextMock).deleteDatabase("database");
}
Also used : Context(android.content.Context) SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 88 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project kripton by xcesco.

the class SQLiteUpdateTestDatabase method updateAndVerify.

/**
 * Allow to update database version to <i>version</i>. This method allows to
 * specify the destination version schema and compare it with schema
 * resulting by version update applied.
 *
 * @param version
 * @param schemaDefinitionInputStream
 * @return
 */
public SQLiteUpdateTestDatabase updateAndVerify(int version, final InputStream schemaDefinitionInputStream) {
    sqlite = new SQLiteOpenHelper(context, MIGRATION_TEST, factory, version, errorHandler) {

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            List<SQLiteUpdateTask> task = findTask(oldVersion, newVersion);
            for (SQLiteUpdateTask item : task) {
                item.execute(db, oldVersion, oldVersion + 1);
                oldVersion++;
            }
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            throw (new KriptonRuntimeException("Unsupported situation"));
        }
    };
    SQLiteSchemaVerifierHelper.verifySchema(sqlite.getWritableDatabase(), schemaDefinitionInputStream);
    return this;
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) List(java.util.List) KriptonRuntimeException(com.abubusoft.kripton.exception.KriptonRuntimeException) SQLiteUpdateTask(com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask)

Example 89 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project fdroidclient by f-droid.

the class DatabaseMigration method migrationsFromDbVersion42Onward.

@Test
public void migrationsFromDbVersion42Onward() {
    Preferences.setupForTests(context);
    SQLiteOpenHelper opener = new MigrationRunningOpenHelper(context);
    opener.getReadableDatabase();
    opener.close();
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) Test(org.junit.Test)

Example 90 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project AppCenter-SDK-Android by Microsoft.

the class DatabaseManagerTest method failsToDeleteLogDuringPutWhenFull.

@Test
public void failsToDeleteLogDuringPutWhenFull() {
    /* Mocking instances. */
    Context contextMock = mock(Context.class);
    SQLiteOpenHelper helperMock = mock(SQLiteOpenHelper.class);
    SQLiteDatabase sqLiteDatabase = mock(SQLiteDatabase.class);
    when(helperMock.getWritableDatabase()).thenReturn(sqLiteDatabase);
    /* Mock the select cursor we are using to find logs to evict to fail. */
    mockStatic(SQLiteUtils.class);
    Cursor cursor = mock(Cursor.class);
    SQLiteDiskIOException fatalException = new SQLiteDiskIOException();
    when(cursor.moveToNext()).thenThrow(fatalException);
    SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
    when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
    /* Simulate that database is full and that deletes fail because of the cursor. */
    when(sqLiteDatabase.insertOrThrow(anyString(), anyString(), any(ContentValues.class))).thenThrow(new SQLiteFullException());
    /* Instantiate real instance for DatabaseManager. */
    DatabaseManager databaseManager = new DatabaseManager(contextMock, "database", "table", 1, null, null, null);
    databaseManager.setSQLiteOpenHelper(helperMock);
    /* When we put a log, it will fail to purge. */
    assertEquals(-1, databaseManager.put(mock(ContentValues.class), "priority"));
}
Also used : Context(android.content.Context) SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) ContentValues(android.content.ContentValues) Returns(org.mockito.internal.stubbing.answers.Returns) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteFullException(android.database.sqlite.SQLiteFullException) SQLiteDiskIOException(android.database.sqlite.SQLiteDiskIOException) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)95 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)79 Cursor (android.database.Cursor)47 Test (org.junit.Test)35 ContentValues (android.content.ContentValues)19 Context (android.content.Context)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)16 Result (com.google.zxing.Result)10 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)9 SQLException (android.database.SQLException)7 ArrayList (java.util.ArrayList)7 SharedPreferences (android.content.SharedPreferences)5 DateFormat (java.text.DateFormat)5 SQLiteDiskIOException (android.database.sqlite.SQLiteDiskIOException)4 SQLiteFullException (android.database.sqlite.SQLiteFullException)4 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)4 Returns (org.mockito.internal.stubbing.answers.Returns)4 SQLiteException (android.database.sqlite.SQLiteException)3 DatabaseHelper (com.android.launcher3.LauncherProvider.DatabaseHelper)3 DBHelper (dev.sagar.smsblocker.tech.service.helper.DBHelper)3