mobile/android/tests/background/junit3/src/db/TestClientsDatabaseAccessor.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial