Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 package org.mozilla.gecko.background.db;
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.Map;
10 import org.mozilla.gecko.background.testhelpers.CommandHelpers;
11 import org.mozilla.gecko.sync.CommandProcessor.Command;
12 import org.mozilla.gecko.sync.Utils;
13 import org.mozilla.gecko.sync.repositories.NullCursorException;
14 import org.mozilla.gecko.sync.repositories.android.ClientsDatabaseAccessor;
15 import org.mozilla.gecko.sync.repositories.domain.ClientRecord;
17 import android.content.Context;
18 import android.database.Cursor;
19 import android.test.AndroidTestCase;
21 public class TestClientsDatabaseAccessor extends AndroidTestCase {
23 public class StubbedClientsDatabaseAccessor extends ClientsDatabaseAccessor {
24 public StubbedClientsDatabaseAccessor(Context mContext) {
25 super(mContext);
26 }
27 }
29 StubbedClientsDatabaseAccessor db;
31 public void setUp() {
32 db = new StubbedClientsDatabaseAccessor(mContext);
33 db.wipeDB();
34 }
36 public void tearDown() {
37 db.close();
38 }
40 public void testStoreArrayListAndFetch() throws NullCursorException {
41 ArrayList<ClientRecord> list = new ArrayList<ClientRecord>();
42 ClientRecord record1 = new ClientRecord(Utils.generateGuid());
43 ClientRecord record2 = new ClientRecord(Utils.generateGuid());
44 ClientRecord record3 = new ClientRecord(Utils.generateGuid());
46 list.add(record1);
47 list.add(record2);
48 db.store(list);
50 ClientRecord r1 = db.fetchClient(record1.guid);
51 ClientRecord r2 = db.fetchClient(record2.guid);
52 ClientRecord r3 = db.fetchClient(record3.guid);
54 assertNotNull(r1);
55 assertNotNull(r2);
56 assertNull(r3);
57 assertTrue(record1.equals(r1));
58 assertTrue(record2.equals(r2));
59 assertFalse(record3.equals(r3));
60 }
62 public void testStoreAndFetchCommandsForClient() {
63 String accountGUID1 = Utils.generateGuid();
64 String accountGUID2 = Utils.generateGuid();
66 Command command1 = CommandHelpers.getCommand1();
67 Command command2 = CommandHelpers.getCommand2();
68 Command command3 = CommandHelpers.getCommand3();
70 Cursor cur = null;
71 try {
72 db.store(accountGUID1, command1);
73 db.store(accountGUID1, command2);
74 db.store(accountGUID2, command3);
76 List<Command> commands = db.fetchCommandsForClient(accountGUID1);
77 assertEquals(2, commands.size());
78 assertEquals(1, commands.get(0).args.size());
79 assertEquals(1, commands.get(1).args.size());
80 } catch (NullCursorException e) {
81 fail("Should not have NullCursorException");
82 } finally {
83 if (cur != null) {
84 cur.close();
85 }
86 }
87 }
89 public void testNumClients() {
90 final int COUNT = 5;
91 ArrayList<ClientRecord> list = new ArrayList<ClientRecord>();
92 for (int i = 0; i < 5; i++) {
93 list.add(new ClientRecord());
94 }
95 db.store(list);
96 assertEquals(COUNT, db.clientsCount());
97 }
99 public void testFetchAll() throws NullCursorException {
100 ArrayList<ClientRecord> list = new ArrayList<ClientRecord>();
101 ClientRecord record1 = new ClientRecord(Utils.generateGuid());
102 ClientRecord record2 = new ClientRecord(Utils.generateGuid());
104 list.add(record1);
105 list.add(record2);
107 boolean thrown = false;
108 try {
109 Map<String, ClientRecord> records = db.fetchAllClients();
111 assertNotNull(records);
112 assertEquals(0, records.size());
114 db.store(list);
115 records = db.fetchAllClients();
116 assertNotNull(records);
117 assertEquals(2, records.size());
118 assertTrue(record1.equals(records.get(record1.guid)));
119 assertTrue(record2.equals(records.get(record2.guid)));
121 // put() should throw an exception since records is immutable.
122 records.put(null, null);
123 } catch (UnsupportedOperationException e) {
124 thrown = true;
125 }
126 assertTrue(thrown);
127 }
128 }