Search in sources :

Example 91 with SQLiteOpenHelper

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

the class DatabaseManagerTest method getDatabaseFailedThenCleanupFailedThenRetrySucceeded.

@Test
public void getDatabaseFailedThenCleanupFailedThenRetrySucceeded() {
    /* Mocking instances. */
    Context contextMock = mock(Context.class);
    SQLiteOpenHelper helperMock = mock(SQLiteOpenHelper.class);
    when(helperMock.getWritableDatabase()).thenThrow(new RuntimeException()).thenReturn(mock(SQLiteDatabase.class));
    when(contextMock.deleteDatabase("database")).thenReturn(false);
    /* Instantiate real instance for DatabaseManager. */
    DatabaseManager databaseManager = new DatabaseManager(contextMock, "database", "table", 1, null, 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 92 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project mobile-messaging-sdk-android by infobip.

the class SqliteMessageMigrationTest method test_shouldAddContentUrlColumnDuringMigration.

@Test
public void test_shouldAddContentUrlColumnDuringMigration() throws Exception {
    // Create SQLiteOpenHelper directly to perform raw operations on database
    SQLiteOpenHelper sqLiteOpenHelper = new SQLiteOpenHelper(context, PushDatabaseHelperImpl.DATABASE_NAME, null, PushDatabaseHelperImpl.VER_2017_FEB_14) {

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(SQL_CREATE_OLD_MESSAGES_TABLE);
            db.execSQL(SQL_CREATE_GEO_MESSAGES_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    };
    SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
    db.close();
    sqLiteOpenHelper.close();
    // Check that content_url column exists
    SQLiteDatabase database = databaseProvider.getDatabase();
    Cursor cursor = database.rawQuery("SELECT * FROM messages", null);
    assertNotEquals(-1, cursor.getColumnIndex("content_url"));
    cursor.close();
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 93 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project mobile-messaging-sdk-android by infobip.

the class SqliteMessageMigrationTest method test_shouldAddSendDateTimeToInternalData.

@Test
public void test_shouldAddSendDateTimeToInternalData() throws Exception {
    // Create SQLiteOpenHelper directly to perform raw operations on database
    context.deleteDatabase(PushDatabaseHelperImpl.DATABASE_NAME);
    SQLiteOpenHelper sqLiteOpenHelper = new SQLiteOpenHelper(context, PushDatabaseHelperImpl.DATABASE_NAME, null, PushDatabaseHelperImpl.VER_2017_MAY_15) {

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(SQL_CREATE_MAY_MESSAGES_TABLE);
            db.execSQL(SQL_CREATE_GEO_MESSAGES_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    };
    SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
    // Save new data to database
    ContentValues contentValues = new ContentValues();
    contentValues.put("id", "SomeMessageId");
    contentValues.put("title", "SomeMessageTitle");
    contentValues.put("body", "SomeMessageBody");
    contentValues.put("sound", "SomeMessageSound");
    contentValues.put("vibrate", 1);
    contentValues.put("icon", "SomeMessageIcon");
    contentValues.put("silent", 1);
    contentValues.put("category", "SomeMessageCategory");
    contentValues.put("_from", "SomeMessageFrom");
    contentValues.put("received_timestamp", 1234L);
    contentValues.put("seen_timestamp", 5678L);
    contentValues.put("internal_data", "{\"key1\":\"value1\"}");
    contentValues.put("custom_payload", "{\"key2\":\"value2\"}");
    contentValues.put("destination", "SomeMessageDestination");
    contentValues.put("status", "ERROR");
    contentValues.put("status_message", "SomeMessageStatusMessage");
    contentValues.put("content_url", "SomeMessageContentUrl");
    db.insertWithOnConflict(DatabaseContract.Tables.MESSAGES, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
    db.close();
    sqLiteOpenHelper.close();
    // Check that sent timestamp was added and other fields are the same
    SQLiteMessageStore messageStore = new SQLiteMessageStore();
    List<Message> messages = messageStore.findAll(context);
    assertEquals(1, messages.size());
    assertEquals("SomeMessageId", messages.get(0).getMessageId());
    assertEquals("SomeMessageTitle", messages.get(0).getTitle());
    assertEquals("SomeMessageBody", messages.get(0).getBody());
    assertEquals("SomeMessageSound", messages.get(0).getSound());
    assertEquals(true, messages.get(0).isVibrate());
    assertEquals("SomeMessageIcon", messages.get(0).getIcon());
    assertEquals(true, messages.get(0).isSilent());
    assertEquals("SomeMessageCategory", messages.get(0).getCategory());
    assertEquals("SomeMessageFrom", messages.get(0).getFrom());
    assertEquals(1234L, messages.get(0).getReceivedTimestamp());
    assertEquals(5678L, messages.get(0).getSeenTimestamp());
    assertEquals(1234L, messages.get(0).getSentTimestamp());
    JSONAssert.assertEquals("{\"key1\" : \"value1\", \"sendDateTime\":1234}", messages.get(0).getInternalData(), true);
    JSONAssert.assertEquals("{\"key2\" : \"value2\"}", messages.get(0).getCustomPayload(), true);
    assertEquals("SomeMessageDestination", messages.get(0).getDestination());
    assertEquals(Message.Status.ERROR, messages.get(0).getStatus());
    assertEquals("SomeMessageStatusMessage", messages.get(0).getStatusMessage());
    assertEquals("SomeMessageContentUrl", messages.get(0).getContentUrl());
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) ContentValues(android.content.ContentValues) Message(org.infobip.mobile.messaging.Message) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteMessageStore(org.infobip.mobile.messaging.storage.SQLiteMessageStore) Test(org.junit.Test)

Example 94 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project snowplow-android-tracker by snowplow.

the class EventStoreTest method testMigrationFromLegacyToNamespacedEventStore.

public void testMigrationFromLegacyToNamespacedEventStore() throws InterruptedException {
    Context context = getContext();
    // Create fake legacy database
    SQLiteOpenHelper legacyDB = new SQLiteOpenHelper(context, "snowplowEvents.sqlite", null, 1) {

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS 'events' (id INTEGER PRIMARY KEY)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        }
    };
    SQLiteDatabase db = legacyDB.getWritableDatabase();
    db.enableWriteAheadLogging();
    legacyDB.close();
    List<String> databaseList = Arrays.asList(context.databaseList());
    // old DB
    assertTrue(databaseList.contains("snowplowEvents.sqlite"));
    // old DB is closed so these should be false
    assertFalse(databaseList.contains("snowplowEvents.sqlite-wal"));
    assertFalse(databaseList.contains("snowplowEvents.sqlite-shm"));
    // new DB not yet created so these should be false
    assertFalse(databaseList.contains("snowplowEvents-namespace.sqlite"));
    assertFalse(databaseList.contains("snowplowEvents-namespace.sqlite-wal"));
    assertFalse(databaseList.contains("snowplowEvents-namespace.sqlite-shm"));
    // Migrate database when SQLiteEventStore is launched the first time
    SQLiteEventStore eventStore = new SQLiteEventStore(context, "namespace");
    openedEventStores.add(eventStore);
    waitUntilDatabaseOpen(eventStore);
    databaseList = Arrays.asList(context.databaseList());
    assertFalse(databaseList.contains("snowplowEvents.sqlite"));
    assertFalse(databaseList.contains("snowplowEvents.sqlite-wal"));
    assertFalse(databaseList.contains("snowplowEvents.sqlite-shm"));
    assertTrue(databaseList.contains("snowplowEvents-namespace.sqlite"));
    assertTrue(databaseList.contains("snowplowEvents-namespace.sqlite-wal"));
    assertTrue(databaseList.contains("snowplowEvents-namespace.sqlite-shm"));
}
Also used : Context(android.content.Context) SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 95 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project android_packages_apps_Trebuchet by LineageOS.

the class DbDowngradeHelperTest method testDowngrade_success_v22.

@Test
public void testDowngrade_success_v22() throws Exception {
    setupTestDb();
    SQLiteOpenHelper helper = new TestOpenHelper(22);
    assertEquals(22, helper.getWritableDatabase().getVersion());
    // Check column does not exist
    try (Cursor c = helper.getWritableDatabase().query(Favorites.TABLE_NAME, null, null, null, null, null, null)) {
        assertEquals(-1, c.getColumnIndex(Favorites.OPTIONS));
        // Check data is present
        assertEquals(10, c.getCount());
    }
    helper.close();
    helper = new DatabaseHelper(mContext, DB_FILE, false) {

        @Override
        public void onOpen(SQLiteDatabase db) {
        }
    };
    assertEquals(LauncherProvider.SCHEMA_VERSION, helper.getWritableDatabase().getVersion());
    try (Cursor c = helper.getWritableDatabase().query(Favorites.TABLE_NAME, null, null, null, null, null, null)) {
        // Check column exists
        assertNotSame(-1, c.getColumnIndex(Favorites.OPTIONS));
        // Check data is present
        assertEquals(10, c.getCount());
    }
    helper.close();
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) DatabaseHelper(com.android.launcher3.LauncherProvider.DatabaseHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) 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