michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.db; michael@0: michael@0: import java.util.ArrayList; michael@0: michael@0: import org.json.simple.JSONArray; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: import org.mozilla.gecko.sync.repositories.NullCursorException; michael@0: import org.mozilla.gecko.sync.repositories.android.ClientsDatabase; michael@0: import org.mozilla.gecko.sync.repositories.android.RepoUtils; michael@0: import org.mozilla.gecko.sync.repositories.domain.ClientRecord; michael@0: import org.mozilla.gecko.sync.setup.Constants; michael@0: michael@0: import android.database.Cursor; michael@0: import android.test.AndroidTestCase; michael@0: michael@0: public class TestClientsDatabase extends AndroidTestCase { michael@0: michael@0: protected ClientsDatabase db; michael@0: michael@0: public void setUp() { michael@0: db = new ClientsDatabase(mContext); michael@0: db.wipeDB(); michael@0: } michael@0: michael@0: public void testStoreAndFetch() { michael@0: ClientRecord record = new ClientRecord(); michael@0: String profileConst = Constants.DEFAULT_PROFILE; michael@0: db.store(profileConst, record); michael@0: michael@0: Cursor cur = null; michael@0: try { michael@0: // Test stored item gets fetched correctly. michael@0: cur = db.fetchClientsCursor(record.guid, profileConst); michael@0: assertTrue(cur.moveToFirst()); michael@0: assertEquals(1, cur.getCount()); michael@0: michael@0: String guid = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ACCOUNT_GUID); michael@0: String profileId = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_PROFILE); michael@0: String clientName = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_NAME); michael@0: String clientType = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_TYPE); michael@0: michael@0: assertEquals(record.guid, guid); michael@0: assertEquals(profileConst, profileId); michael@0: assertEquals(record.name, clientName); michael@0: assertEquals(record.type, clientType); michael@0: } catch (NullCursorException e) { michael@0: fail("Should not have NullCursorException"); michael@0: } finally { michael@0: if (cur != null) { michael@0: cur.close(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: public void testStoreAndFetchSpecificCommands() { michael@0: String accountGUID = Utils.generateGuid(); michael@0: ArrayList args = new ArrayList(); michael@0: args.add("URI of Page"); michael@0: args.add("Sender GUID"); michael@0: args.add("Title of Page"); michael@0: String jsonArgs = JSONArray.toJSONString(args); michael@0: michael@0: Cursor cur = null; michael@0: try { michael@0: db.store(accountGUID, "displayURI", jsonArgs); michael@0: michael@0: // This row should not show up in the fetch. michael@0: args.add("Another arg."); michael@0: db.store(accountGUID, "displayURI", JSONArray.toJSONString(args)); michael@0: michael@0: // Test stored item gets fetched correctly. michael@0: cur = db.fetchSpecificCommand(accountGUID, "displayURI", jsonArgs); michael@0: assertTrue(cur.moveToFirst()); michael@0: assertEquals(1, cur.getCount()); michael@0: michael@0: String guid = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ACCOUNT_GUID); michael@0: String commandType = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_COMMAND); michael@0: String fetchedArgs = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ARGS); michael@0: michael@0: assertEquals(accountGUID, guid); michael@0: assertEquals("displayURI", commandType); michael@0: assertEquals(jsonArgs, fetchedArgs); michael@0: } catch (NullCursorException e) { michael@0: fail("Should not have NullCursorException"); michael@0: } finally { michael@0: if (cur != null) { michael@0: cur.close(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: public void testFetchCommandsForClient() { michael@0: String accountGUID = Utils.generateGuid(); michael@0: ArrayList args = new ArrayList(); michael@0: args.add("URI of Page"); michael@0: args.add("Sender GUID"); michael@0: args.add("Title of Page"); michael@0: String jsonArgs = JSONArray.toJSONString(args); michael@0: michael@0: Cursor cur = null; michael@0: try { michael@0: db.store(accountGUID, "displayURI", jsonArgs); michael@0: michael@0: // This row should ALSO show up in the fetch. michael@0: args.add("Another arg."); michael@0: db.store(accountGUID, "displayURI", JSONArray.toJSONString(args)); michael@0: michael@0: // Test both stored items with the same GUID but different command are fetched. michael@0: cur = db.fetchCommandsForClient(accountGUID); michael@0: assertTrue(cur.moveToFirst()); michael@0: assertEquals(2, cur.getCount()); michael@0: } catch (NullCursorException e) { michael@0: fail("Should not have NullCursorException"); michael@0: } finally { michael@0: if (cur != null) { michael@0: cur.close(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: @SuppressWarnings("resource") michael@0: public void testDelete() { michael@0: ClientRecord record1 = new ClientRecord(); michael@0: ClientRecord record2 = new ClientRecord(); michael@0: String profileConst = Constants.DEFAULT_PROFILE; michael@0: michael@0: db.store(profileConst, record1); michael@0: db.store(profileConst, record2); michael@0: michael@0: Cursor cur = null; michael@0: try { michael@0: // Test record doesn't exist after delete. michael@0: db.deleteClient(record1.guid, profileConst); michael@0: cur = db.fetchClientsCursor(record1.guid, profileConst); michael@0: assertFalse(cur.moveToFirst()); michael@0: assertEquals(0, cur.getCount()); michael@0: michael@0: // Test record2 still there after deleting record1. michael@0: cur = db.fetchClientsCursor(record2.guid, profileConst); michael@0: assertTrue(cur.moveToFirst()); michael@0: assertEquals(1, cur.getCount()); michael@0: michael@0: String guid = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ACCOUNT_GUID); michael@0: String profileId = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_PROFILE); michael@0: String clientName = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_NAME); michael@0: String clientType = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_TYPE); michael@0: michael@0: assertEquals(record2.guid, guid); michael@0: assertEquals(profileConst, profileId); michael@0: assertEquals(record2.name, clientName); michael@0: assertEquals(record2.type, clientType); michael@0: } catch (NullCursorException e) { michael@0: fail("Should not have NullCursorException"); michael@0: } finally { michael@0: if (cur != null) { michael@0: cur.close(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: @SuppressWarnings("resource") michael@0: public void testWipe() { michael@0: ClientRecord record1 = new ClientRecord(); michael@0: ClientRecord record2 = new ClientRecord(); michael@0: String profileConst = Constants.DEFAULT_PROFILE; michael@0: michael@0: db.store(profileConst, record1); michael@0: db.store(profileConst, record2); michael@0: michael@0: michael@0: Cursor cur = null; michael@0: try { michael@0: // Test before wipe the records are there. michael@0: cur = db.fetchClientsCursor(record2.guid, profileConst); michael@0: assertTrue(cur.moveToFirst()); michael@0: assertEquals(1, cur.getCount()); michael@0: cur = db.fetchClientsCursor(record2.guid, profileConst); michael@0: assertTrue(cur.moveToFirst()); michael@0: assertEquals(1, cur.getCount()); michael@0: michael@0: // Test after wipe neither record exists. michael@0: db.wipeClientsTable(); michael@0: cur = db.fetchClientsCursor(record2.guid, profileConst); michael@0: assertFalse(cur.moveToFirst()); michael@0: assertEquals(0, cur.getCount()); michael@0: cur = db.fetchClientsCursor(record1.guid, profileConst); michael@0: assertFalse(cur.moveToFirst()); michael@0: assertEquals(0, cur.getCount()); michael@0: } catch (NullCursorException e) { michael@0: fail("Should not have NullCursorException"); michael@0: } finally { michael@0: if (cur != null) { michael@0: cur.close(); michael@0: } michael@0: } michael@0: } michael@0: }