1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/tests/background/junit3/src/db/TestClientsDatabaseAccessor.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,128 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +package org.mozilla.gecko.background.db; 1.8 + 1.9 +import java.util.ArrayList; 1.10 +import java.util.List; 1.11 +import java.util.Map; 1.12 + 1.13 +import org.mozilla.gecko.background.testhelpers.CommandHelpers; 1.14 +import org.mozilla.gecko.sync.CommandProcessor.Command; 1.15 +import org.mozilla.gecko.sync.Utils; 1.16 +import org.mozilla.gecko.sync.repositories.NullCursorException; 1.17 +import org.mozilla.gecko.sync.repositories.android.ClientsDatabaseAccessor; 1.18 +import org.mozilla.gecko.sync.repositories.domain.ClientRecord; 1.19 + 1.20 +import android.content.Context; 1.21 +import android.database.Cursor; 1.22 +import android.test.AndroidTestCase; 1.23 + 1.24 +public class TestClientsDatabaseAccessor extends AndroidTestCase { 1.25 + 1.26 + public class StubbedClientsDatabaseAccessor extends ClientsDatabaseAccessor { 1.27 + public StubbedClientsDatabaseAccessor(Context mContext) { 1.28 + super(mContext); 1.29 + } 1.30 + } 1.31 + 1.32 + StubbedClientsDatabaseAccessor db; 1.33 + 1.34 + public void setUp() { 1.35 + db = new StubbedClientsDatabaseAccessor(mContext); 1.36 + db.wipeDB(); 1.37 + } 1.38 + 1.39 + public void tearDown() { 1.40 + db.close(); 1.41 + } 1.42 + 1.43 + public void testStoreArrayListAndFetch() throws NullCursorException { 1.44 + ArrayList<ClientRecord> list = new ArrayList<ClientRecord>(); 1.45 + ClientRecord record1 = new ClientRecord(Utils.generateGuid()); 1.46 + ClientRecord record2 = new ClientRecord(Utils.generateGuid()); 1.47 + ClientRecord record3 = new ClientRecord(Utils.generateGuid()); 1.48 + 1.49 + list.add(record1); 1.50 + list.add(record2); 1.51 + db.store(list); 1.52 + 1.53 + ClientRecord r1 = db.fetchClient(record1.guid); 1.54 + ClientRecord r2 = db.fetchClient(record2.guid); 1.55 + ClientRecord r3 = db.fetchClient(record3.guid); 1.56 + 1.57 + assertNotNull(r1); 1.58 + assertNotNull(r2); 1.59 + assertNull(r3); 1.60 + assertTrue(record1.equals(r1)); 1.61 + assertTrue(record2.equals(r2)); 1.62 + assertFalse(record3.equals(r3)); 1.63 + } 1.64 + 1.65 + public void testStoreAndFetchCommandsForClient() { 1.66 + String accountGUID1 = Utils.generateGuid(); 1.67 + String accountGUID2 = Utils.generateGuid(); 1.68 + 1.69 + Command command1 = CommandHelpers.getCommand1(); 1.70 + Command command2 = CommandHelpers.getCommand2(); 1.71 + Command command3 = CommandHelpers.getCommand3(); 1.72 + 1.73 + Cursor cur = null; 1.74 + try { 1.75 + db.store(accountGUID1, command1); 1.76 + db.store(accountGUID1, command2); 1.77 + db.store(accountGUID2, command3); 1.78 + 1.79 + List<Command> commands = db.fetchCommandsForClient(accountGUID1); 1.80 + assertEquals(2, commands.size()); 1.81 + assertEquals(1, commands.get(0).args.size()); 1.82 + assertEquals(1, commands.get(1).args.size()); 1.83 + } catch (NullCursorException e) { 1.84 + fail("Should not have NullCursorException"); 1.85 + } finally { 1.86 + if (cur != null) { 1.87 + cur.close(); 1.88 + } 1.89 + } 1.90 + } 1.91 + 1.92 + public void testNumClients() { 1.93 + final int COUNT = 5; 1.94 + ArrayList<ClientRecord> list = new ArrayList<ClientRecord>(); 1.95 + for (int i = 0; i < 5; i++) { 1.96 + list.add(new ClientRecord()); 1.97 + } 1.98 + db.store(list); 1.99 + assertEquals(COUNT, db.clientsCount()); 1.100 + } 1.101 + 1.102 + public void testFetchAll() throws NullCursorException { 1.103 + ArrayList<ClientRecord> list = new ArrayList<ClientRecord>(); 1.104 + ClientRecord record1 = new ClientRecord(Utils.generateGuid()); 1.105 + ClientRecord record2 = new ClientRecord(Utils.generateGuid()); 1.106 + 1.107 + list.add(record1); 1.108 + list.add(record2); 1.109 + 1.110 + boolean thrown = false; 1.111 + try { 1.112 + Map<String, ClientRecord> records = db.fetchAllClients(); 1.113 + 1.114 + assertNotNull(records); 1.115 + assertEquals(0, records.size()); 1.116 + 1.117 + db.store(list); 1.118 + records = db.fetchAllClients(); 1.119 + assertNotNull(records); 1.120 + assertEquals(2, records.size()); 1.121 + assertTrue(record1.equals(records.get(record1.guid))); 1.122 + assertTrue(record2.equals(records.get(record2.guid))); 1.123 + 1.124 + // put() should throw an exception since records is immutable. 1.125 + records.put(null, null); 1.126 + } catch (UnsupportedOperationException e) { 1.127 + thrown = true; 1.128 + } 1.129 + assertTrue(thrown); 1.130 + } 1.131 +}