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: import java.util.List; michael@0: import java.util.Map; michael@0: michael@0: import org.mozilla.gecko.background.testhelpers.CommandHelpers; michael@0: import org.mozilla.gecko.sync.CommandProcessor.Command; 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.ClientsDatabaseAccessor; michael@0: import org.mozilla.gecko.sync.repositories.domain.ClientRecord; michael@0: michael@0: import android.content.Context; michael@0: import android.database.Cursor; michael@0: import android.test.AndroidTestCase; michael@0: michael@0: public class TestClientsDatabaseAccessor extends AndroidTestCase { michael@0: michael@0: public class StubbedClientsDatabaseAccessor extends ClientsDatabaseAccessor { michael@0: public StubbedClientsDatabaseAccessor(Context mContext) { michael@0: super(mContext); michael@0: } michael@0: } michael@0: michael@0: StubbedClientsDatabaseAccessor db; michael@0: michael@0: public void setUp() { michael@0: db = new StubbedClientsDatabaseAccessor(mContext); michael@0: db.wipeDB(); michael@0: } michael@0: michael@0: public void tearDown() { michael@0: db.close(); michael@0: } michael@0: michael@0: public void testStoreArrayListAndFetch() throws NullCursorException { michael@0: ArrayList list = new ArrayList(); michael@0: ClientRecord record1 = new ClientRecord(Utils.generateGuid()); michael@0: ClientRecord record2 = new ClientRecord(Utils.generateGuid()); michael@0: ClientRecord record3 = new ClientRecord(Utils.generateGuid()); michael@0: michael@0: list.add(record1); michael@0: list.add(record2); michael@0: db.store(list); michael@0: michael@0: ClientRecord r1 = db.fetchClient(record1.guid); michael@0: ClientRecord r2 = db.fetchClient(record2.guid); michael@0: ClientRecord r3 = db.fetchClient(record3.guid); michael@0: michael@0: assertNotNull(r1); michael@0: assertNotNull(r2); michael@0: assertNull(r3); michael@0: assertTrue(record1.equals(r1)); michael@0: assertTrue(record2.equals(r2)); michael@0: assertFalse(record3.equals(r3)); michael@0: } michael@0: michael@0: public void testStoreAndFetchCommandsForClient() { michael@0: String accountGUID1 = Utils.generateGuid(); michael@0: String accountGUID2 = Utils.generateGuid(); michael@0: michael@0: Command command1 = CommandHelpers.getCommand1(); michael@0: Command command2 = CommandHelpers.getCommand2(); michael@0: Command command3 = CommandHelpers.getCommand3(); michael@0: michael@0: Cursor cur = null; michael@0: try { michael@0: db.store(accountGUID1, command1); michael@0: db.store(accountGUID1, command2); michael@0: db.store(accountGUID2, command3); michael@0: michael@0: List commands = db.fetchCommandsForClient(accountGUID1); michael@0: assertEquals(2, commands.size()); michael@0: assertEquals(1, commands.get(0).args.size()); michael@0: assertEquals(1, commands.get(1).args.size()); 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 testNumClients() { michael@0: final int COUNT = 5; michael@0: ArrayList list = new ArrayList(); michael@0: for (int i = 0; i < 5; i++) { michael@0: list.add(new ClientRecord()); michael@0: } michael@0: db.store(list); michael@0: assertEquals(COUNT, db.clientsCount()); michael@0: } michael@0: michael@0: public void testFetchAll() throws NullCursorException { michael@0: ArrayList list = new ArrayList(); michael@0: ClientRecord record1 = new ClientRecord(Utils.generateGuid()); michael@0: ClientRecord record2 = new ClientRecord(Utils.generateGuid()); michael@0: michael@0: list.add(record1); michael@0: list.add(record2); michael@0: michael@0: boolean thrown = false; michael@0: try { michael@0: Map records = db.fetchAllClients(); michael@0: michael@0: assertNotNull(records); michael@0: assertEquals(0, records.size()); michael@0: michael@0: db.store(list); michael@0: records = db.fetchAllClients(); michael@0: assertNotNull(records); michael@0: assertEquals(2, records.size()); michael@0: assertTrue(record1.equals(records.get(record1.guid))); michael@0: assertTrue(record2.equals(records.get(record2.guid))); michael@0: michael@0: // put() should throw an exception since records is immutable. michael@0: records.put(null, null); michael@0: } catch (UnsupportedOperationException e) { michael@0: thrown = true; michael@0: } michael@0: assertTrue(thrown); michael@0: } michael@0: }